From ad15acbcf72e1fb32c3b4f2f160b09b2e5e24d99 Mon Sep 17 00:00:00 2001 From: Tim Su Date: Thu, 15 Jan 2009 11:10:51 +0000 Subject: [PATCH] Various fixes to get the dialogs looking right, and catching exceptions. --- .../astrid/activities/SyncPreferences.java | 1 - src/com/timsu/astrid/activities/TaskList.java | 11 ++-- .../astrid/data/sync/SyncDataController.java | 5 +- .../timsu/astrid/data/tag/TagController.java | 18 +++---- .../astrid/data/task/TaskController.java | 17 +++--- src/com/timsu/astrid/sync/RTMSyncService.java | 52 +++++++++--------- .../astrid/sync/SynchronizationService.java | 53 +++++++++++-------- src/com/timsu/astrid/sync/Synchronizer.java | 7 ++- .../timsu/astrid/utilities/Preferences.java | 5 ++ 9 files changed, 92 insertions(+), 77 deletions(-) diff --git a/src/com/timsu/astrid/activities/SyncPreferences.java b/src/com/timsu/astrid/activities/SyncPreferences.java index f0b6ada74..2070da378 100644 --- a/src/com/timsu/astrid/activities/SyncPreferences.java +++ b/src/com/timsu/astrid/activities/SyncPreferences.java @@ -25,7 +25,6 @@ public class SyncPreferences extends PreferenceActivity { @Override public void onClick(View v) { Synchronizer.synchronize(SyncPreferences.this); - finish(); } }); diff --git a/src/com/timsu/astrid/activities/TaskList.java b/src/com/timsu/astrid/activities/TaskList.java index 4d35f1991..bf3e14ed9 100644 --- a/src/com/timsu/astrid/activities/TaskList.java +++ b/src/com/timsu/astrid/activities/TaskList.java @@ -18,6 +18,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package com.timsu.astrid.activities; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; @@ -110,8 +111,8 @@ public class TaskList extends Activity { // other instance variables private Map tagMap; - private List taskArray; - private Map> taskTags; + private ArrayList taskArray; + private HashMap> taskTags; private GestureDetector gestureDetector; private View.OnTouchListener gestureTouchListener; @@ -339,7 +340,7 @@ public class TaskList extends Activity { // read tags and apply filters int hiddenTasks = 0; // # of tasks hidden int completedTasks = 0; // # of tasks on list that are done - taskTags = new HashMap>(); + taskTags = new HashMap>(); for(Iterator i = taskArray.iterator(); i.hasNext();) { TaskModelForList task = i.next(); @@ -351,9 +352,9 @@ public class TaskList extends Activity { } // get list of tags - List tagIds = tagController.getTaskTags(this, + LinkedList tagIds = tagController.getTaskTags(this, task.getTaskIdentifier()); - List tags = new LinkedList(); + LinkedList tags = new LinkedList(); for(TagIdentifier tagId : tagIds) { TagModelForView tag = tagMap.get(tagId); tags.add(tag); diff --git a/src/com/timsu/astrid/data/sync/SyncDataController.java b/src/com/timsu/astrid/data/sync/SyncDataController.java index 254d37317..3c74b80c1 100644 --- a/src/com/timsu/astrid/data/sync/SyncDataController.java +++ b/src/com/timsu/astrid/data/sync/SyncDataController.java @@ -20,7 +20,6 @@ package com.timsu.astrid.data.sync; import java.util.HashSet; -import java.util.Set; import android.content.ContentValues; import android.content.Context; @@ -60,8 +59,8 @@ public class SyncDataController extends AbstractController { // --- sync mapping /** Get all mappings for the given synchronization service */ - public Set getSyncMapping(int syncServiceId) throws SQLException { - Set list = new HashSet(); + public HashSet getSyncMapping(int syncServiceId) throws SQLException { + HashSet list = new HashSet(); Cursor cursor = syncDatabase.query(SYNC_TABLE_NAME, SyncMapping.FIELD_LIST, SyncMapping.SYNC_SERVICE + " = " + syncServiceId, diff --git a/src/com/timsu/astrid/data/tag/TagController.java b/src/com/timsu/astrid/data/tag/TagController.java index 0cc271752..2285fbf3e 100644 --- a/src/com/timsu/astrid/data/tag/TagController.java +++ b/src/com/timsu/astrid/data/tag/TagController.java @@ -21,8 +21,6 @@ package com.timsu.astrid.data.tag; import java.util.HashMap; import java.util.LinkedList; -import java.util.List; -import java.util.Map; import android.app.Activity; import android.content.ContentValues; @@ -43,9 +41,9 @@ public class TagController extends AbstractController { // --- tag batch operations /** Get a list of all tags */ - public List getAllTags(Activity activity) + public LinkedList getAllTags(Activity activity) throws SQLException { - List list = new LinkedList(); + LinkedList list = new LinkedList(); Cursor cursor = tagDatabase.query(TAG_TABLE_NAME, TagModelForView.FIELD_LIST, null, null, null, null, null, null); activity.startManagingCursor(cursor); @@ -63,17 +61,17 @@ public class TagController extends AbstractController { // --- tag to task map batch operations /** Get a list of all tags as an id => tag map */ - public Map getAllTagsAsMap(Activity activity) throws SQLException { - Map map = new HashMap(); + public HashMap getAllTagsAsMap(Activity activity) throws SQLException { + HashMap map = new HashMap(); for(TagModelForView tag : getAllTags(activity)) map.put(tag.getTagIdentifier(), tag); return map; } /** Get a list of tag identifiers for the given task */ - public List getTaskTags(Activity activity, TaskIdentifier + public LinkedList getTaskTags(Activity activity, TaskIdentifier taskId) throws SQLException { - List list = new LinkedList(); + LinkedList list = new LinkedList(); Cursor cursor = tagToTaskMapDatabase.query(TAG_TASK_MAP_NAME, TagToTaskMapping.FIELD_LIST, TagToTaskMapping.TASK + " = ?", new String[] { taskId.idAsString() }, null, null, null); @@ -90,9 +88,9 @@ public class TagController extends AbstractController { } /** Get a list of task identifiers for the given tag */ - public List getTaggedTasks(Activity activity, TagIdentifier + public LinkedList getTaggedTasks(Activity activity, TagIdentifier tagId) throws SQLException { - List list = new LinkedList(); + LinkedList list = new LinkedList(); Cursor cursor = tagToTaskMapDatabase.query(TAG_TASK_MAP_NAME, TagToTaskMapping.FIELD_LIST, TagToTaskMapping.TAG + " = ?", new String[] { tagId.idAsString() }, null, null, null); diff --git a/src/com/timsu/astrid/data/task/TaskController.java b/src/com/timsu/astrid/data/task/TaskController.java index a14b5c7c0..ca7eba6ac 100644 --- a/src/com/timsu/astrid/data/task/TaskController.java +++ b/src/com/timsu/astrid/data/task/TaskController.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; -import java.util.Set; import android.app.Activity; import android.content.ContentValues; @@ -47,8 +46,8 @@ public class TaskController extends AbstractController { // --- task list operations /** Return a list of all active tasks with notifications */ - public Set getTasksWithNotifications() { - Set list = new HashSet(); + public HashSet getTasksWithNotifications() { + HashSet list = new HashSet(); Cursor cursor = database.query(TASK_TABLE_NAME, TaskModelForNotify.FIELD_LIST, String.format("%s < %d AND (%s != 0 OR %s != 0)", AbstractTaskModel.PROGRESS_PERCENTAGE, @@ -69,8 +68,8 @@ public class TaskController extends AbstractController { } /** Return a list of all active tasks with deadlines */ - public List getTasksWithDeadlines() { - List list = new ArrayList(); + public ArrayList getTasksWithDeadlines() { + ArrayList list = new ArrayList(); Cursor cursor = database.query(TASK_TABLE_NAME, TaskModelForNotify.FIELD_LIST, String.format("%s < %d AND (%s != 0 OR %s != 0)", AbstractTaskModel.PROGRESS_PERCENTAGE, @@ -105,8 +104,8 @@ public class TaskController extends AbstractController { } /** Create a list of tasks from the db cursor given */ - public List createTaskListFromCursor(Cursor cursor) { - List list = new ArrayList(); + public ArrayList createTaskListFromCursor(Cursor cursor) { + ArrayList list = new ArrayList(); if(cursor.getCount() == 0) return list; @@ -120,8 +119,8 @@ public class TaskController extends AbstractController { } /** Get identifiers for all tasks */ - public Set getActiveTaskIdentifiers() { - Set list = new HashSet(); + public HashSet getActiveTaskIdentifiers() { + HashSet list = new HashSet(); Cursor cursor = database.query(TASK_TABLE_NAME, new String[] { KEY_ROWID }, AbstractTaskModel.PROGRESS_PERCENTAGE + " < " + AbstractTaskModel.COMPLETE_PERCENTAGE, null, null, null, null, null); diff --git a/src/com/timsu/astrid/sync/RTMSyncService.java b/src/com/timsu/astrid/sync/RTMSyncService.java index 7ee6a282a..7324ed5e1 100644 --- a/src/com/timsu/astrid/sync/RTMSyncService.java +++ b/src/com/timsu/astrid/sync/RTMSyncService.java @@ -51,24 +51,19 @@ public class RTMSyncService extends SynchronizationService { } @Override - void synchronizeService(final Activity activity) { - Date lastSyncDate = Preferences.getSyncRTMLastSync(activity); - if(lastSyncDate == null || true) { + protected void synchronize(final Activity activity) { + if(Preferences.shouldSyncRTM(activity) && + Preferences.getSyncRTMToken(activity) == null) { DialogUtilities.okCancelDialog(activity, activity.getResources().getString(R.string.sync_rtm_notes), new Dialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - RTMSyncService.super.synchronizeService(activity); + authenticate(activity); } }, null); } else - super.synchronizeService(activity); - } - - @Override - protected void synchronize(Activity activity) { - authenticate(activity); + authenticate(activity); } @Override @@ -112,23 +107,18 @@ public class RTMSyncService extends SynchronizationService { rtmService = new ServiceImpl(new ApplicationInfo( apiKey, sharedSecret, appName)); final String url = rtmService.beginAuthorization(Perms.delete); - syncHandler.post(new Runnable() { + progressDialog.dismiss(); + Resources r = activity.getResources(); + DialogUtilities.okCancelDialog(activity, + r.getString(R.string.sync_auth_request, "RTM"), + new DialogInterface.OnClickListener() { @Override - public void run() { - progressDialog.dismiss(); - Resources r = activity.getResources(); - DialogUtilities.okCancelDialog(activity, - r.getString(R.string.sync_auth_request, "RTM"), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Intent intent = new Intent(Intent.ACTION_VIEW, - Uri.parse(url)); - activity.startActivity(intent); - } - }, null); + public void onClick(DialogInterface dialog, int which) { + Intent intent = new Intent(Intent.ACTION_VIEW, + Uri.parse(url)); + activity.startActivity(intent); } - }); + }, null); } else { performSync(activity); @@ -139,7 +129,17 @@ public class RTMSyncService extends SynchronizationService { } } - private void performSync(Activity activity) { + private void performSync(final Activity activity) { + new Thread(new Runnable() { + @Override + public void run() { + performSyncInNewThread(activity); + Synchronizer.closeControllers(); + } + }).start(); + } + + private void performSyncInNewThread(final Activity activity) { try { syncHandler.post(new Runnable() { @Override diff --git a/src/com/timsu/astrid/sync/SynchronizationService.java b/src/com/timsu/astrid/sync/SynchronizationService.java index 85e9f532f..8b89e4998 100644 --- a/src/com/timsu/astrid/sync/SynchronizationService.java +++ b/src/com/timsu/astrid/sync/SynchronizationService.java @@ -3,9 +3,8 @@ package com.timsu.astrid.sync; import java.io.IOException; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Set; import android.app.Activity; import android.app.ProgressDialog; @@ -40,16 +39,28 @@ public abstract class SynchronizationService { this.id = id; } + // called off the UI thread. does some setup void synchronizeService(final Activity activity) { - progressDialog = ProgressDialog.show(activity, "Synchronization", - "Checking Authorization..."); - - new Thread(new Runnable() { + syncHandler.post(new Runnable() { @Override public void run() { - synchronize(activity); + progressDialog = new ProgressDialog(activity); + progressDialog.setIcon(android.R.drawable.ic_dialog_alert); + progressDialog.setTitle("Synchronization"); + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progressDialog.setMax(100); + progressDialog.setMessage("Checking Authorization..."); + progressDialog.setProgress(0); + progressDialog.show(); } - }).start();; + }); + synchronize(activity); + syncHandler.post(new Runnable() { + @Override + public void run() { + progressDialog.dismiss(); + } + }); } /** Synchronize with the service */ @@ -137,18 +148,18 @@ public abstract class SynchronizationService { TagController tagController = Synchronizer.getTagController(activity); // get data out of the database (note we get non-completed tasks only) - Set mappings = syncController.getSyncMapping(getId()); - Set localTasks = taskController.getActiveTaskIdentifiers(); - Map tags = + HashSet mappings = syncController.getSyncMapping(getId()); + HashSet localTasks = taskController.getActiveTaskIdentifiers(); + HashMap tags = tagController.getAllTagsAsMap(activity); // build local maps / lists - Map remoteIdToSyncMapping = + HashMap remoteIdToSyncMapping = new HashMap(); - Map localIdToSyncMapping = + HashMap localIdToSyncMapping = new HashMap(); - Set localChanges = new HashSet(); - Set mappedTasks = new HashSet(); + HashSet localChanges = new HashSet(); + HashSet mappedTasks = new HashSet(); for(SyncMapping mapping : mappings) { if(mapping.isUpdated()) localChanges.add(mapping); @@ -158,7 +169,7 @@ public abstract class SynchronizationService { } // build remote map - Map remoteChangeMap = + HashMap remoteChangeMap = new HashMap(); for(TaskProxy remoteTask : remoteTasks) { if(remoteIdToSyncMapping.containsKey(remoteTask.getRemoteId())) { @@ -175,11 +186,11 @@ public abstract class SynchronizationService { progressDialog.setProgress(0); } }); - Set newlyCreatedTasks = new HashSet( + HashSet newlyCreatedTasks = new HashSet( localTasks); newlyCreatedTasks.removeAll(mappedTasks); for(TaskIdentifier taskId : newlyCreatedTasks) { - List taskTags = + LinkedList taskTags = tagController.getTaskTags(activity, taskId); String listName = null; if(taskTags.size() > 0) { @@ -212,7 +223,7 @@ public abstract class SynchronizationService { progressDialog.setProgress(0); } }); - Set deletedTasks = new HashSet(mappedTasks); + HashSet deletedTasks = new HashSet(mappedTasks); deletedTasks.removeAll(localTasks); for(TaskIdentifier taskId : deletedTasks) { SyncMapping mapping = localIdToSyncMapping.get(taskId); @@ -349,7 +360,7 @@ public abstract class SynchronizationService { // --- helper classes - private class SyncStats { + protected class SyncStats { int localCreatedTasks = 0; int localUpdatedTasks = 0; int localDeletedTasks = 0; @@ -401,7 +412,7 @@ public abstract class SynchronizationService { this.outOf = outOf; } public void run() { - progressDialog.setProgress(10000*step/outOf); + progressDialog.setProgress(100*step/outOf); } } } diff --git a/src/com/timsu/astrid/sync/Synchronizer.java b/src/com/timsu/astrid/sync/Synchronizer.java index 9c390ee3d..da406038f 100644 --- a/src/com/timsu/astrid/sync/Synchronizer.java +++ b/src/com/timsu/astrid/sync/Synchronizer.java @@ -19,12 +19,15 @@ public class Synchronizer { /** Synchronize all activated sync services */ public static void synchronize(final Activity activity) { + + // sync services do cleanup themselves, so currently we only + // support one sync service at a time. + // RTM sync if(Preferences.shouldSyncRTM(activity)) { services.get(SYNC_ID_RTM).synchronizeService(activity); } - closeControllers(); } /** Clears tokens if services are disabled */ @@ -72,7 +75,7 @@ public class Synchronizer { private static TaskController taskController = null; private static TagController tagController = null; - private static void closeControllers() { + static void closeControllers() { if(syncController != null) { syncController.close(); syncController = null; diff --git a/src/com/timsu/astrid/utilities/Preferences.java b/src/com/timsu/astrid/utilities/Preferences.java index f4f1de66c..0dc234758 100644 --- a/src/com/timsu/astrid/utilities/Preferences.java +++ b/src/com/timsu/astrid/utilities/Preferences.java @@ -145,6 +145,11 @@ public class Preferences { /** Set RTM Last Successful Sync Date */ public static void setSyncRTMLastSync(Context context, Date date) { + if(date == null) { + clearPref(context, P_SYNC_RTM_LAST_SYNC); + return; + } + Editor editor = getPrefs(context).edit(); editor.putLong(P_SYNC_RTM_LAST_SYNC, date.getTime()); editor.commit();