From a7fabed823c60952e68e6c36877ec27054d21b7f Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 25 Oct 2011 17:11:14 -0700 Subject: [PATCH] iOS style automatic sync of lists view, all tasks view, and specific lists when opened --- .../astrid/activity/FilterListActivity.java | 53 +++++++++++++++++-- .../astrid/activity/TaskListActivity.java | 29 ++++++++-- .../todoroo/astrid/adapter/FilterAdapter.java | 8 +-- 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/astrid/src/com/todoroo/astrid/activity/FilterListActivity.java b/astrid/src/com/todoroo/astrid/activity/FilterListActivity.java index 9992ac9a0..6ccf4177b 100644 --- a/astrid/src/com/todoroo/astrid/activity/FilterListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/FilterListActivity.java @@ -9,9 +9,12 @@ import org.json.JSONException; import android.app.AlertDialog; import android.app.ExpandableListActivity; +import android.app.Notification; +import android.app.PendingIntent; import android.app.PendingIntent.CanceledException; import android.app.ProgressDialog; import android.app.SearchManager; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; @@ -43,10 +46,14 @@ import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.ExceptionService; +import com.todoroo.andlib.service.NotificationManager; import com.todoroo.andlib.sql.Functions; import com.todoroo.andlib.sql.QueryTemplate; import com.todoroo.andlib.utility.AndroidUtilities; +import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DialogUtilities; +import com.todoroo.andlib.utility.Preferences; +import com.todoroo.astrid.actfm.ActFmPreferences; import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; import com.todoroo.astrid.actfm.sync.ActFmSyncService; import com.todoroo.astrid.adapter.FilterAdapter; @@ -61,6 +68,7 @@ import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.service.StatisticsConstants; import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.ThemeService; +import com.todoroo.astrid.utility.Constants; /** * Activity that displays a user's task lists and allows users @@ -80,6 +88,8 @@ public class FilterListActivity extends ExpandableListActivity { private static final int MENU_HELP_ID = Menu.FIRST + 1; private static final int MENU_REFRESH_ID = Menu.FIRST + 2; + private static final String LAST_TAG_REFRESH_KEY = "last_tag_refresh"; //$NON-NLS-1$ + private static final int CONTEXT_MENU_SHORTCUT = Menu.FIRST + 3; private static final int CONTEXT_MENU_INTENT = Menu.FIRST + 4; @@ -155,6 +165,8 @@ public class FilterListActivity extends ExpandableListActivity { startActivity(intent); } else { setUpList(); + if (actFmPreferenceService.isLoggedIn()) + onRefreshRequested(false); } } @@ -380,7 +392,7 @@ public class FilterListActivity extends ExpandableListActivity { } case MENU_REFRESH_ID: { - onRefreshRequested(); + onRefreshRequested(true); return true; } @@ -415,8 +427,26 @@ public class FilterListActivity extends ExpandableListActivity { /** * Refresh user tags */ - private void onRefreshRequested() { - final ProgressDialog progressDialog = DialogUtilities.progressDialog(this, getString(R.string.DLG_please_wait)); + private void onRefreshRequested(final boolean manual) { + if (!manual) { + long lastFetchDate = Preferences.getLong(LAST_TAG_REFRESH_KEY, 0); + if(DateUtilities.now() < lastFetchDate + 300000L) { + return; + } + } + final ProgressDialog progressDialog; + + final NotificationManager nm = new NotificationManager.AndroidNotificationManager(this); + final Notification notification = new Notification(android.R.drawable.stat_notify_sync, null, System.currentTimeMillis()); + final int notificationId = updateNotification(this, notification); + notification.flags |= Notification.FLAG_ONGOING_EVENT; + + if (manual) { + progressDialog = DialogUtilities.progressDialog(this, getString(R.string.DLG_please_wait)); + } else { + progressDialog = null; + nm.notify(notificationId, notification); + } new Thread(new Runnable() { @SuppressWarnings("nls") @@ -428,6 +458,7 @@ public class FilterListActivity extends ExpandableListActivity { runOnUiThread(new Runnable() { @Override public void run() { + Preferences.setLong(LAST_TAG_REFRESH_KEY, DateUtilities.now()); adapter.clear(); adapter.getLists(); } @@ -438,12 +469,26 @@ public class FilterListActivity extends ExpandableListActivity { } catch (JSONException e) { exceptionService.displayAndReportError(FilterListActivity.this, "refresh-tags-json", e); } finally { - DialogUtilities.dismissDialog(FilterListActivity.this, progressDialog); + if (manual) + DialogUtilities.dismissDialog(FilterListActivity.this, progressDialog); + else + nm.cancel(notificationId); } } }).start(); } + private int updateNotification(Context context, Notification notification) { + String notificationTitle = context.getString(R.string.actfm_notification_title); + Intent intent = new Intent(context, ActFmPreferences.class); + PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, + intent, 0); + notification.setLatestEventInfo(context, + notificationTitle, context.getString(R.string.SyP_progress), + notificationIntent); + return Constants.NOTIFICATION_SYNC; + } + private void showCreateShortcutDialog(final Intent shortcutIntent, final Filter filter) { FrameLayout frameLayout = new FrameLayout(this); diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java index 57991e6ed..545a50733 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java @@ -71,6 +71,8 @@ import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.widget.GestureService; import com.todoroo.andlib.widget.GestureService.GestureInterface; +import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; +import com.todoroo.astrid.actfm.sync.ActFmSyncProvider; import com.todoroo.astrid.activity.SortSelectionActivity.OnSortSelectedListener; import com.todoroo.astrid.adapter.TaskAdapter; import com.todoroo.astrid.adapter.TaskAdapter.OnCompletedTaskListener; @@ -168,6 +170,8 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, @Autowired UpgradeService upgradeService; + @Autowired ActFmPreferenceService actFmPreferenceService; + private final TaskContextActionExposer[] contextItemExposers = new TaskContextActionExposer[] { new ReminderDebugContextActions.MakeNotification(), new ReminderDebugContextActions.WhenReminder(), @@ -187,7 +191,7 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, private EditText quickAddBox; private Timer backgroundTimer; private final LinkedHashSet syncActions = new LinkedHashSet(); - private boolean isInbox; + private boolean isFilter; private final TaskListContextMenuExtensionLoader contextMenuExtensionLoader = new TaskListContextMenuExtensionLoader(); private VoiceInputAssistant voiceInputAssistant; @@ -281,12 +285,12 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, return; } else if(extras != null && extras.containsKey(TOKEN_FILTER)) { filter = extras.getParcelable(TOKEN_FILTER); - isInbox = true; + isFilter = true; } else { filter = CoreFilterExposer.buildInboxFilter(getResources()); findViewById(R.id.headerLogo).setVisibility(View.VISIBLE); findViewById(R.id.listLabel).setVisibility(View.GONE); - isInbox = false; + isFilter = false; } setUpTaskList(); @@ -466,6 +470,19 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER); } + private void initiateAutomaticSync() { + if (!actFmPreferenceService.isLoggedIn()) return; + long lastFetchDate = actFmPreferenceService.getLastSyncDate(); + if(DateUtilities.now() < lastFetchDate + 300000L) + return; + new Thread() { + @Override + public void run() { + new ActFmSyncProvider().synchronize(TaskListActivity.this); + } + }.start(); + } + // Subclasses can override these to customize extras in quickadd intent protected Intent getOnClickQuickAddIntent(Task t) { Intent intent = new Intent(TaskListActivity.this, TaskEditActivity.class); @@ -536,6 +553,10 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, registerReceiver(syncActionReceiver, new IntentFilter(AstridApiConstants.BROADCAST_SEND_SYNC_ACTIONS)); setUpBackgroundJobs(); + + if (filter.title.equals(getString(R.string.BFE_Active))) { + initiateAutomaticSync(); + } } @Override @@ -835,7 +856,7 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, * @param item task that was completed */ protected void onTaskCompleted(Task item) { - if(isInbox) + if(isFilter) StatisticsService.reportEvent(StatisticsConstants.TASK_COMPLETED_INBOX); else StatisticsService.reportEvent(StatisticsConstants.TASK_COMPLETED_FILTER); diff --git a/astrid/src/com/todoroo/astrid/adapter/FilterAdapter.java b/astrid/src/com/todoroo/astrid/adapter/FilterAdapter.java index 65c84a9c0..35970f475 100644 --- a/astrid/src/com/todoroo/astrid/adapter/FilterAdapter.java +++ b/astrid/src/com/todoroo/astrid/adapter/FilterAdapter.java @@ -80,7 +80,7 @@ public class FilterAdapter extends BaseExpandableListAdapter { private final DisplayMetrics metrics = new DisplayMetrics(); /** receiver for new filters */ - private final FilterReceiver filterReceiver = new FilterReceiver(); + //private final FilterReceiver filterReceiver = new FilterReceiver(); private final BladeFilterReceiver bladeFilterReceiver = new BladeFilterReceiver(); /** row layout to inflate */ @@ -448,8 +448,8 @@ public class FilterAdapter extends BaseExpandableListAdapter { * Call this method from your activity's onResume() method */ public void registerRecevier() { - activity.registerReceiver(filterReceiver, - new IntentFilter(AstridApiConstants.BROADCAST_SEND_FILTERS)); + /*activity.registerReceiver(filterReceiver, + new IntentFilter(AstridApiConstants.BROADCAST_SEND_FILTERS));//*/ activity.registerReceiver(bladeFilterReceiver, new IntentFilter(AstridApiConstants.BROADCAST_SEND_FILTERS)); if(getGroupCount() == 0) @@ -460,7 +460,7 @@ public class FilterAdapter extends BaseExpandableListAdapter { * Call this method from your activity's onResume() method */ public void unregisterRecevier() { - activity.unregisterReceiver(filterReceiver); + //activity.unregisterReceiver(filterReceiver); activity.unregisterReceiver(bladeFilterReceiver); }