From 794abd5896259833615874ba4b528db68143ac46 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Mon, 25 Mar 2013 15:02:20 -0700 Subject: [PATCH] Added a sync ongoing notification to the status bar instead of progress bars, fixed a crash --- .../astrid/actfm/sync/ActFmSyncThread.java | 33 +++++++++++++++++++ astrid/res/values/strings-actfm.xml | 2 ++ .../todoroo/astrid/adapter/UpdateAdapter.java | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) 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 57d9e0e18..b55069ff5 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java @@ -14,17 +14,22 @@ import org.apache.http.entity.mime.MultipartEntity; import org.json.JSONArray; import org.json.JSONObject; +import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; +import android.support.v4.app.NotificationCompat; import android.util.Log; import com.crittercism.app.Crittercism; +import com.timsu.astrid.R; import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.DependencyInjectionService; +import com.todoroo.andlib.service.NotificationManager; +import com.todoroo.andlib.service.NotificationManager.AndroidNotificationManager; import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DateUtilities; @@ -109,6 +114,10 @@ public class ActFmSyncThread { private boolean isTimeForBackgroundSync = false; + private final NotificationManager notificationManager; + + private int notificationId = -1; + public static interface SyncMessageCallback { public void runOnSuccess(); public void runOnErrors(List errors); @@ -165,6 +174,7 @@ public class ActFmSyncThread { this.pendingCallbacks = Collections.synchronizedMap(new HashMap, SyncMessageCallback>()); this.monitor = syncMonitor; this.syncMigration = Preferences.getBoolean(AstridNewSyncMigrator.PREF_SYNC_MIGRATION, false); + this.notificationManager = new AndroidNotificationManager(ContextManager.getContext()); } public synchronized void startSyncThread() { @@ -228,6 +238,10 @@ public class ActFmSyncThread { synchronized(monitor) { while ((pendingMessages.isEmpty() && !timeForBackgroundSync()) || !actFmPreferenceService.isLoggedIn() || !syncMigration) { try { + if (pendingMessages.isEmpty() && notificationId >= 0) { + notificationManager.cancel(notificationId); + notificationId = -1; + } monitor.wait(); AndroidUtilities.sleepDeep(500L); // Wait briefly for large database operations to finish (e.g. adding a task with several tags may trigger a message before all saves are done--fix this?) @@ -276,6 +290,8 @@ public class ActFmSyncThread { continue; } + setupNotification(); + payload.put(getClientVersion()); JSONArray errors = null; @@ -445,6 +461,23 @@ public class ActFmSyncThread { } } + private void setupNotification() { + try { + NotificationCompat.Builder builder = new NotificationCompat.Builder(ContextManager.getContext()); + builder.setContentText(ContextManager.getString(R.string.actfm_sync_ongoing)) + .setContentTitle(ContextManager.getString(R.string.app_name)) + .setOngoing(true) + .setSmallIcon(android.R.drawable.stat_notify_sync) + .setContentIntent(PendingIntent.getActivity(ContextManager.getContext().getApplicationContext(), 0, new Intent(), 0)); + + + notificationManager.notify(0, builder.getNotification()); + notificationId = 0; + } catch (Exception e) { + e.printStackTrace(); + } + } + private boolean checkForToken() { if(!actFmPreferenceService.isLoggedIn()) return false; diff --git a/astrid/res/values/strings-actfm.xml b/astrid/res/values/strings-actfm.xml index beb9d1170..8572cd354 100644 --- a/astrid/res/values/strings-actfm.xml +++ b/astrid/res/values/strings-actfm.xml @@ -374,5 +374,7 @@ Create new list Leave list + Sync ongoing... + diff --git a/astrid/src/com/todoroo/astrid/adapter/UpdateAdapter.java b/astrid/src/com/todoroo/astrid/adapter/UpdateAdapter.java index eaa2e8362..f49d62a69 100644 --- a/astrid/src/com/todoroo/astrid/adapter/UpdateAdapter.java +++ b/astrid/src/com/todoroo/astrid/adapter/UpdateAdapter.java @@ -325,7 +325,7 @@ public class UpdateAdapter extends CursorAdapter { else commentPictureView.setUrl(pictureThumb); - if(imageCache.contains(pictureThumb) && updateBitmap == null) { + if (pictureThumb != null && imageCache.contains(pictureThumb) && updateBitmap == null) { try { commentPictureView.setDefaultImageBitmap(imageCache.get(pictureThumb)); } catch (IOException e) {