Browse Source

Added auto refresh to the activity

OnResume creates an AsyncTask which keeps running
until cancelled. It requests server updates with
a delay of two seconds between each update.

OnPause cancels the AsyncTask.

To handle multiselect gracefully, the fragment requests
the auto refresh to be paused. This pause is remembered
between resets, as long as the multiselect mode is active.
Once the users selects an action or exits multiselect,
the auto refresh resumes.

There are probably several things I haven't thought of,
and there should be a user setting for this maybe.

Signed-off-by: Jonas Kalderstam <jonas@kalderstam.se>
pull/148/merge
Jonas Kalderstam 11 years ago committed by Eric Kok
parent
commit
2bd8b88254
  1. 56
      core/src/org/transdroid/core/gui/TorrentsActivity.java
  2. 6
      core/src/org/transdroid/core/gui/TorrentsFragment.java

56
core/src/org/transdroid/core/gui/TorrentsActivity.java

@ -103,6 +103,7 @@ import android.app.SearchManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
@ -170,6 +171,11 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi
@FragmentById(resName = "torrent_details") @FragmentById(resName = "torrent_details")
protected DetailsFragment fragmentDetails; protected DetailsFragment fragmentDetails;
// Auto refresh task. Could be replaced by @Background(id="task-id")
private AsyncTask<Void, Void, Void> autoRefreshTask;
// Fragment uses this to pause the refresh across restarts
public boolean stopRefresh = false;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
// Set the theme according to the user preference // Set the theme according to the user preference
@ -274,6 +280,8 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
// Start auto refresh
startAutoRefresh();
// Refresh server settings // Refresh server settings
navigationSpinnerAdapter.updateServers(applicationSettings.getAllServerSettings()); navigationSpinnerAdapter.updateServers(applicationSettings.getAllServerSettings());
@ -301,6 +309,54 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi
} }
} }
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void startAutoRefresh() {
// Check if already running
if (autoRefreshTask != null || stopRefresh)
return;
autoRefreshTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
while (!isCancelled()) {
// X seconds seems reasonable if someone actually wants real time updates
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// Ignore
}
// Just in case it was cancelled during sleep
if (isCancelled())
return null;
refreshTorrents();
if (Daemon.supportsStats(currentConnection.getType()))
getAdditionalStats();
}
return null;
}
};
// Executes serially by default on Honeycomb, was parallel before
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
autoRefreshTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
else
autoRefreshTask.execute();
}
public void stopAutoRefresh() {
if (autoRefreshTask != null)
autoRefreshTask.cancel(true);
autoRefreshTask = null;
}
@Override
protected void onPause() {
stopAutoRefresh();
super.onPause();
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
Crouton.cancelAllCroutons(); Crouton.cancelAllCroutons();

6
core/src/org/transdroid/core/gui/TorrentsFragment.java

@ -261,6 +261,9 @@ public class TorrentsFragment extends SherlockFragment implements OnLabelPickedL
menu.findItem(R.id.action_stop).setVisible(Daemon.supportsStoppingStarting(daemonType)); menu.findItem(R.id.action_stop).setVisible(Daemon.supportsStoppingStarting(daemonType));
menu.findItem(R.id.action_setlabel).setVisible(Daemon.supportsSetLabel(daemonType)); menu.findItem(R.id.action_setlabel).setVisible(Daemon.supportsSetLabel(daemonType));
} }
// Pause autorefresh
((TorrentsActivity) getActivity()).stopRefresh = true;
((TorrentsActivity) getActivity()).stopAutoRefresh();
return true; return true;
} }
@ -329,6 +332,9 @@ public class TorrentsFragment extends SherlockFragment implements OnLabelPickedL
@Override @Override
public void onDestroyActionMode(ActionMode mode) { public void onDestroyActionMode(ActionMode mode) {
// Resume autorefresh
((TorrentsActivity) getActivity()).stopRefresh = false;
((TorrentsActivity) getActivity()).startAutoRefresh();
selectionManagerMode.onDestroyActionMode(mode); selectionManagerMode.onDestroyActionMode(mode);
} }

Loading…
Cancel
Save