Skip to content

Commit

Permalink
Refactoring + fixed issue related to #34
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Jan 9, 2019
1 parent fa4d581 commit 83fe1e3
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void onPreCreate(@Nullable Bundle savedInstanceState) {
String title = getIntent().getStringExtra("title");
if (gid == null || theme == 0 || title == null) {
super.onCreate(savedInstanceState);
Toaster.with(MoreAboutDownloadActivity.this).message(R.string.failedLoading).ex(new IllegalArgumentException("gid = " + gid + ", theme = " + theme + ", title = " + title)).show();
Toaster.with(this).message(R.string.failedLoading).ex(new IllegalArgumentException("gid = " + gid + ", theme = " + theme + ", title = " + title)).show();
onBackPressed();
return;
}
Expand Down
22 changes: 15 additions & 7 deletions app/src/main/java/com/gianlu/aria2app/LoadingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.gianlu.aria2app.Main.MainActivity;
import com.gianlu.aria2app.NetIO.AbstractClient;
import com.gianlu.aria2app.NetIO.HttpClient;
import com.gianlu.aria2app.NetIO.NetInstanceHolder;
import com.gianlu.aria2app.NetIO.OnConnect;
import com.gianlu.aria2app.NetIO.WebSocketClient;
import com.gianlu.aria2app.ProfilesManager.CustomProfilesAdapter;
Expand All @@ -30,6 +31,7 @@

import org.json.JSONException;

import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -60,6 +62,7 @@ public class LoadingActivity extends ActivityWithDialog implements OnConnect, Dr
private String shortcutAction;
private Handler handler;
private MultiProfile.UserProfile aria2AndroidProfile = null;
private Closeable ongoingTest;

public static void startActivity(Context context) {
context.startActivity(new Intent(context, LoadingActivity.class)
Expand Down Expand Up @@ -107,7 +110,7 @@ protected void onCreate(Bundle savedInstanceState) {
if (goTo != null) startActivity(goTo);
}, 1000);

AbstractClient.invalidate();
NetInstanceHolder.close();

manager = ProfilesManager.get(this);
if (getIntent().getBooleanExtra("external", false)) {
Expand Down Expand Up @@ -222,11 +225,10 @@ private void tryConnecting(@Nullable MultiProfile profile) {
} else {
manager.setCurrent(profile);
MultiProfile.UserProfile single = profile.getProfile(this);
if (single.connectionMethod == MultiProfile.ConnectionMethod.WEBSOCKET) {
WebSocketClient.checkConnection(this, single, this);
} else {
HttpClient.checkConnection(this, single, this);
}
if (single.connectionMethod == MultiProfile.ConnectionMethod.WEBSOCKET)
ongoingTest = WebSocketClient.checkConnection(single, this);
else
ongoingTest = HttpClient.checkConnection(single, this);

handler.postDelayed(() -> {
cancel.setVisibility(View.VISIBLE);
Expand All @@ -236,7 +238,13 @@ private void tryConnecting(@Nullable MultiProfile profile) {
}

private void cancelConnection() {
AbstractClient.invalidate();
if (ongoingTest != null) {
try {
ongoingTest.close();
} catch (IOException ignored) {
}
}

displayPicker(hasShareData());
seeError.setVisibility(View.GONE);
}
Expand Down
35 changes: 9 additions & 26 deletions app/src/main/java/com/gianlu/aria2app/Main/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.gianlu.aria2app.NetIO.AriaRequests;
import com.gianlu.aria2app.NetIO.Downloader.FetchHelper;
import com.gianlu.aria2app.NetIO.GitHubApi;
import com.gianlu.aria2app.NetIO.NetInstanceHolder;
import com.gianlu.aria2app.NetIO.OnRefresh;
import com.gianlu.aria2app.NetIO.Updater.PayloadProvider;
import com.gianlu.aria2app.NetIO.Updater.Receiver;
Expand Down Expand Up @@ -102,7 +103,7 @@
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

public class MainActivity extends UpdaterActivity implements FloatingActionsMenu.OnFloatingActionsMenuUpdateListener, TutorialManager.Listener, HideSecondSpace, DrawerManager.ProfilesDrawerListener<MultiProfile>, DownloadCardsAdapter.Listener, SearchView.OnQueryTextListener, SearchView.OnCloseListener, MenuItem.OnActionExpandListener, AbstractClient.OnConnectivityChanged, OnRefresh, DrawerManager.MenuDrawerListener<DrawerItem>, FetchHelper.FetchDownloadCountListener {
public class MainActivity extends UpdaterActivity implements FloatingActionsMenu.OnFloatingActionsMenuUpdateListener, TutorialManager.Listener, HideSecondSpace, DrawerManager.ProfilesDrawerListener<MultiProfile>, DownloadCardsAdapter.Listener, SearchView.OnQueryTextListener, SearchView.OnCloseListener, MenuItem.OnActionExpandListener, OnRefresh, DrawerManager.MenuDrawerListener<DrawerItem>, FetchHelper.FetchDownloadCountListener {
private static final int REQUEST_READ_CODE = 12;
private final static Wants<DownloadsAndGlobalStats> MAIN_WANTS = Wants.downloadsAndStats();
private DrawerManager<MultiProfile, DrawerItem> drawerManager;
Expand Down Expand Up @@ -318,7 +319,7 @@ protected void onPostCreate() {
helper = Aria2Helper.instantiate(this);
} catch (ProfilesManager.NoCurrentProfileException | Aria2Helper.InitializingException ex) {
Logging.log(ex);
AbstractClient.invalidate();
NetInstanceHolder.close();
profilesManager.unsetLastProfile();
LoadingActivity.startActivity(this, ex);
return;
Expand All @@ -345,15 +346,15 @@ protected void onPostCreate() {
fabMenu.setOnFloatingActionsMenuUpdateListener(this);

FloatingActionButton fabSearch = findViewById(R.id.mainFab_search);
fabSearch.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, SearchActivity.class)));
fabSearch.setOnClickListener(v -> startActivity(new Intent(this, SearchActivity.class)));
FloatingActionButton fabAddURI = findViewById(R.id.mainFab_addURI);
fabAddURI.setOnClickListener(view -> startActivity(new Intent(MainActivity.this, AddUriActivity.class)));
fabAddURI.setOnClickListener(view -> startActivity(new Intent(this, AddUriActivity.class)));
FloatingActionButton fabAddTorrent = findViewById(R.id.mainFab_addTorrent);
fabAddTorrent.setOnClickListener(view -> startActivity(new Intent(MainActivity.this, AddTorrentActivity.class)));
fabAddTorrent.setOnClickListener(view -> startActivity(new Intent(this, AddTorrentActivity.class)));
FloatingActionButton fabAddMetalink = findViewById(R.id.mainFab_addMetalink);
fabAddMetalink.setOnClickListener(view -> startActivity(new Intent(MainActivity.this, AddMetalinkActivity.class)));
fabAddMetalink.setOnClickListener(view -> startActivity(new Intent(this, AddMetalinkActivity.class)));
FloatingActionButton fabBatchAdd = findViewById(R.id.mainFab_batchAdd);
fabBatchAdd.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, BatchAddActivity.class)));
fabBatchAdd.setOnClickListener(v -> startActivity(new Intent(this, BatchAddActivity.class)));

recyclerViewLayout.startLoading();

Expand Down Expand Up @@ -469,7 +470,6 @@ public void onException(@NonNull Exception ex) {

@Override
protected void onDestroy() {
AbstractClient.removeConnectivityListener(this);
if (adapter != null) adapter.activityDestroying(this);
super.onDestroy();
}
Expand Down Expand Up @@ -537,7 +537,7 @@ protected void onResume() {
if (profilesManager != null) profilesManager.reloadCurrentProfile();
} catch (IOException | JSONException | ProfilesManager.NoCurrentProfileException ex) {
Logging.log(ex);
AbstractClient.invalidate();
NetInstanceHolder.close();
profilesManager.unsetLastProfile();
LoadingActivity.startActivity(this, ex);
return;
Expand Down Expand Up @@ -805,13 +805,6 @@ public void onItemCountUpdated(int count) {
tutorialManager.tryShowingTutorials(this);
}

@Override
protected void onStart() {
super.onStart();

AbstractClient.addConnectivityListener(this);
}

@Override
public boolean onQueryTextSubmit(String query) {
if (adapter != null) adapter.filterWithQuery(query);
Expand Down Expand Up @@ -840,16 +833,6 @@ public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}

@Override
public void connectivityChanged(@NonNull final MultiProfile.UserProfile profile) {
if (drawerManager != null) drawerManager.setCurrentProfile(profile.getParent());

List<MultiProfile.UserProfile> profiles = profile.getParent().profiles;
if (!(profiles.size() >= 1 && profiles.get(0).connectivityCondition.type == MultiProfile.ConnectivityCondition.Type.ALWAYS)) {
Toaster.with(this).message(R.string.connectivityChanged).extra(profile.connectivityCondition).show();
}
}

@Override
public void refreshed() {
adapter = new DownloadCardsAdapter(this, new ArrayList<>(), this);
Expand Down
Loading

0 comments on commit 83fe1e3

Please sign in to comment.