From a8e61b6602b1709158f8e6f13304fb7efbde14a4 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 5 Mar 2013 15:49:15 -0800 Subject: [PATCH] Added some ability to do progress bars under new sync --- .../todoroo/astrid/sync/SyncV2Provider.java | 3 +- .../astrid/actfm/sync/ActFmSyncThread.java | 68 +++++++++++++++++++ .../actfm/sync/ActFmSyncV2Provider.java | 12 +--- .../astrid/activity/TaskListFragment.java | 8 +++ 4 files changed, 81 insertions(+), 10 deletions(-) diff --git a/api/src/com/todoroo/astrid/sync/SyncV2Provider.java b/api/src/com/todoroo/astrid/sync/SyncV2Provider.java index d51c5335d..7372beb4a 100644 --- a/api/src/com/todoroo/astrid/sync/SyncV2Provider.java +++ b/api/src/com/todoroo/astrid/sync/SyncV2Provider.java @@ -86,7 +86,8 @@ abstract public class SyncV2Provider { utilities.recordSuccessfulSync(); utilities.reportLastError(); utilities.stopOngoing(); - callback.finished(); + if (callback != null) + callback.finished(); } @Override diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java index 38c1d83b5..0dbf3531e 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java @@ -12,8 +12,11 @@ import java.util.Set; import org.json.JSONArray; import org.json.JSONObject; +import android.app.Activity; import android.content.Intent; import android.util.Log; +import android.view.View; +import android.widget.ProgressBar; import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.service.Autowired; @@ -100,6 +103,12 @@ public class ActFmSyncThread { private boolean isTimeForBackgroundSync = false; + private final Object progressBarLock = new Object(); + + private Activity activity = null; + + private ProgressBar progressBar = null; + public static enum ModelType { TYPE_TASK, TYPE_TAG, @@ -166,6 +175,18 @@ public class ActFmSyncThread { } } + private final Runnable enqueueMessageProgressRunnable = new Runnable() { + @Override + public void run() { + synchronized (progressBarLock) { + if (progressBar != null) { + progressBar.setMax(progressBar.getMax() + 2); + progressBar.setVisibility(View.VISIBLE); + } + } + } + }; + public synchronized void enqueueMessage(ClientToServerMessage message, Runnable callback) { if (!pendingMessages.contains(message)) { pendingMessages.add(message); @@ -174,6 +195,49 @@ public class ActFmSyncThread { synchronized(monitor) { monitor.notifyAll(); } + + if (activity != null) { + activity.runOnUiThread(enqueueMessageProgressRunnable); + } + } + } + + public void setProgressBar(Activity activity, ProgressBar progressBar) { + synchronized(progressBarLock) { + int oldProgress = progressBar.getProgress(); + int oldMax = progressBar.getMax(); + this.activity = activity; + this.progressBar = progressBar; + if (this.activity != null && this.progressBar != null) { + this.progressBar.setMax(oldMax); + this.progressBar.setProgress(oldProgress); + if (oldProgress < oldMax && oldMax != 0) { + this.progressBar.setVisibility(View.VISIBLE); + } else { + this.progressBar.setVisibility(View.GONE); + } + } + } + } + + private final Runnable incrementProgressRunnable = new Runnable() { + @Override + public void run() { + synchronized(progressBarLock) { + if (progressBar != null) { + progressBar.incrementProgressBy(1); + if (progressBar.getProgress() == progressBar.getMax()) { + progressBar.setMax(0); + progressBar.setVisibility(View.GONE); + } + } + } + } + }; + + private void incrementProgress() { + if (activity != null) { + activity.runOnUiThread(incrementProgressRunnable); } } @@ -226,6 +290,7 @@ public class ActFmSyncThread { ClientToServerMessage message = pendingMessages.remove(0); if (message != null) messageBatch.add(message); + incrementProgress(); } if (!messageBatch.isEmpty() && checkForToken()) { @@ -235,6 +300,8 @@ public class ActFmSyncThread { if (serialized != null) { payload.put(serialized); syncLog("Sending: " + serialized); + } else { + incrementProgress(); // Don't let failed serialization mess up progress bar } } @@ -271,6 +338,7 @@ public class ActFmSyncThread { Set callbacksExecutedThisLoop = new HashSet(); for (ClientToServerMessage message : messageBatch) { + incrementProgress(); try { Runnable r = pendingCallbacks.remove(message); if (r != null && !callbacksExecutedThisLoop.contains(r)) { diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java index 1bf3e07e6..b2658609d 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java @@ -83,8 +83,6 @@ public class ActFmSyncV2Provider extends SyncV2Provider { new Thread(new Runnable() { public void run() { - callback.started(); - callback.incrementMax(160); final AtomicInteger finisher = new AtomicInteger(1); @@ -92,9 +90,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider { ActFmSyncThread.getInstance().setTimeForBackgroundSync(true); - startFeaturedListFetcher(callback, finisher); - - callback.incrementProgress(50); + startFeaturedListFetcher(finisher); } }).start(); } @@ -134,8 +130,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider { } /** fetch changes to tags */ - private void startFeaturedListFetcher(final SyncResultCallback callback, - final AtomicInteger finisher) { + private void startFeaturedListFetcher(final AtomicInteger finisher) { new Thread(new Runnable() { @Override public void run() { @@ -150,9 +145,8 @@ public class ActFmSyncV2Provider extends SyncV2Provider { } catch (IOException e) { handler.handleException("actfm-sync", e, e.toString()); //$NON-NLS-1$ } finally { - callback.incrementProgress(20); if(finisher.decrementAndGet() == 0) { - finishSync(callback); + finishSync(null); } } } diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java index b663119e9..7ad00c9c1 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListFragment.java @@ -51,6 +51,7 @@ import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; +import android.widget.ProgressBar; import android.widget.TextView; import com.timsu.astrid.R; @@ -69,6 +70,7 @@ import com.todoroo.astrid.actfm.ActFmLoginActivity; import com.todoroo.astrid.actfm.CommentsActivity; import com.todoroo.astrid.actfm.TagViewFragment; import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; +import com.todoroo.astrid.actfm.sync.ActFmSyncThread; import com.todoroo.astrid.activity.SortSelectionActivity.OnSortSelectedListener; import com.todoroo.astrid.adapter.TaskAdapter; import com.todoroo.astrid.adapter.TaskAdapter.OnCompletedTaskListener; @@ -711,6 +713,12 @@ public class TaskListFragment extends ListFragment implements OnScrollListener, showListsHelp(); } refreshFilterCount(); + + if (isCurrentTaskListFragment()) { + ProgressBar pb = (ProgressBar) getView().findViewById(R.id.progressBar); + ActFmSyncThread.getInstance().setProgressBar(getActivity(), pb); + } + initiateAutomaticSync(); }