Various fixes to get the dialogs looking right, and catching exceptions.

pull/14/head
Tim Su 17 years ago
parent c722008f89
commit ad15acbcf7

@ -25,7 +25,6 @@ public class SyncPreferences extends PreferenceActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Synchronizer.synchronize(SyncPreferences.this); Synchronizer.synchronize(SyncPreferences.this);
finish();
} }
}); });

@ -18,6 +18,7 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
package com.timsu.astrid.activities; package com.timsu.astrid.activities;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
@ -110,8 +111,8 @@ public class TaskList extends Activity {
// other instance variables // other instance variables
private Map<TagIdentifier, TagModelForView> tagMap; private Map<TagIdentifier, TagModelForView> tagMap;
private List<TaskModelForList> taskArray; private ArrayList<TaskModelForList> taskArray;
private Map<TaskModelForList, List<TagModelForView>> taskTags; private HashMap<TaskModelForList, LinkedList<TagModelForView>> taskTags;
private GestureDetector gestureDetector; private GestureDetector gestureDetector;
private View.OnTouchListener gestureTouchListener; private View.OnTouchListener gestureTouchListener;
@ -339,7 +340,7 @@ public class TaskList extends Activity {
// read tags and apply filters // read tags and apply filters
int hiddenTasks = 0; // # of tasks hidden int hiddenTasks = 0; // # of tasks hidden
int completedTasks = 0; // # of tasks on list that are done int completedTasks = 0; // # of tasks on list that are done
taskTags = new HashMap<TaskModelForList, List<TagModelForView>>(); taskTags = new HashMap<TaskModelForList, LinkedList<TagModelForView>>();
for(Iterator<TaskModelForList> i = taskArray.iterator(); i.hasNext();) { for(Iterator<TaskModelForList> i = taskArray.iterator(); i.hasNext();) {
TaskModelForList task = i.next(); TaskModelForList task = i.next();
@ -351,9 +352,9 @@ public class TaskList extends Activity {
} }
// get list of tags // get list of tags
List<TagIdentifier> tagIds = tagController.getTaskTags(this, LinkedList<TagIdentifier> tagIds = tagController.getTaskTags(this,
task.getTaskIdentifier()); task.getTaskIdentifier());
List<TagModelForView> tags = new LinkedList<TagModelForView>(); LinkedList<TagModelForView> tags = new LinkedList<TagModelForView>();
for(TagIdentifier tagId : tagIds) { for(TagIdentifier tagId : tagIds) {
TagModelForView tag = tagMap.get(tagId); TagModelForView tag = tagMap.get(tagId);
tags.add(tag); tags.add(tag);

@ -20,7 +20,6 @@
package com.timsu.astrid.data.sync; package com.timsu.astrid.data.sync;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
@ -60,8 +59,8 @@ public class SyncDataController extends AbstractController {
// --- sync mapping // --- sync mapping
/** Get all mappings for the given synchronization service */ /** Get all mappings for the given synchronization service */
public Set<SyncMapping> getSyncMapping(int syncServiceId) throws SQLException { public HashSet<SyncMapping> getSyncMapping(int syncServiceId) throws SQLException {
Set<SyncMapping> list = new HashSet<SyncMapping>(); HashSet<SyncMapping> list = new HashSet<SyncMapping>();
Cursor cursor = syncDatabase.query(SYNC_TABLE_NAME, Cursor cursor = syncDatabase.query(SYNC_TABLE_NAME,
SyncMapping.FIELD_LIST, SyncMapping.FIELD_LIST,
SyncMapping.SYNC_SERVICE + " = " + syncServiceId, SyncMapping.SYNC_SERVICE + " = " + syncServiceId,

@ -21,8 +21,6 @@ package com.timsu.astrid.data.tag;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import android.app.Activity; import android.app.Activity;
import android.content.ContentValues; import android.content.ContentValues;
@ -43,9 +41,9 @@ public class TagController extends AbstractController {
// --- tag batch operations // --- tag batch operations
/** Get a list of all tags */ /** Get a list of all tags */
public List<TagModelForView> getAllTags(Activity activity) public LinkedList<TagModelForView> getAllTags(Activity activity)
throws SQLException { throws SQLException {
List<TagModelForView> list = new LinkedList<TagModelForView>(); LinkedList<TagModelForView> list = new LinkedList<TagModelForView>();
Cursor cursor = tagDatabase.query(TAG_TABLE_NAME, Cursor cursor = tagDatabase.query(TAG_TABLE_NAME,
TagModelForView.FIELD_LIST, null, null, null, null, null, null); TagModelForView.FIELD_LIST, null, null, null, null, null, null);
activity.startManagingCursor(cursor); activity.startManagingCursor(cursor);
@ -63,17 +61,17 @@ public class TagController extends AbstractController {
// --- tag to task map batch operations // --- tag to task map batch operations
/** Get a list of all tags as an id => tag map */ /** Get a list of all tags as an id => tag map */
public Map<TagIdentifier, TagModelForView> getAllTagsAsMap(Activity activity) throws SQLException { public HashMap<TagIdentifier, TagModelForView> getAllTagsAsMap(Activity activity) throws SQLException {
Map<TagIdentifier, TagModelForView> map = new HashMap<TagIdentifier, TagModelForView>(); HashMap<TagIdentifier, TagModelForView> map = new HashMap<TagIdentifier, TagModelForView>();
for(TagModelForView tag : getAllTags(activity)) for(TagModelForView tag : getAllTags(activity))
map.put(tag.getTagIdentifier(), tag); map.put(tag.getTagIdentifier(), tag);
return map; return map;
} }
/** Get a list of tag identifiers for the given task */ /** Get a list of tag identifiers for the given task */
public List<TagIdentifier> getTaskTags(Activity activity, TaskIdentifier public LinkedList<TagIdentifier> getTaskTags(Activity activity, TaskIdentifier
taskId) throws SQLException { taskId) throws SQLException {
List<TagIdentifier> list = new LinkedList<TagIdentifier>(); LinkedList<TagIdentifier> list = new LinkedList<TagIdentifier>();
Cursor cursor = tagToTaskMapDatabase.query(TAG_TASK_MAP_NAME, Cursor cursor = tagToTaskMapDatabase.query(TAG_TASK_MAP_NAME,
TagToTaskMapping.FIELD_LIST, TagToTaskMapping.TASK + " = ?", TagToTaskMapping.FIELD_LIST, TagToTaskMapping.TASK + " = ?",
new String[] { taskId.idAsString() }, null, null, null); 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 */ /** Get a list of task identifiers for the given tag */
public List<TaskIdentifier> getTaggedTasks(Activity activity, TagIdentifier public LinkedList<TaskIdentifier> getTaggedTasks(Activity activity, TagIdentifier
tagId) throws SQLException { tagId) throws SQLException {
List<TaskIdentifier> list = new LinkedList<TaskIdentifier>(); LinkedList<TaskIdentifier> list = new LinkedList<TaskIdentifier>();
Cursor cursor = tagToTaskMapDatabase.query(TAG_TASK_MAP_NAME, Cursor cursor = tagToTaskMapDatabase.query(TAG_TASK_MAP_NAME,
TagToTaskMapping.FIELD_LIST, TagToTaskMapping.TAG + " = ?", TagToTaskMapping.FIELD_LIST, TagToTaskMapping.TAG + " = ?",
new String[] { tagId.idAsString() }, null, null, null); new String[] { tagId.idAsString() }, null, null, null);

@ -23,7 +23,6 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import android.app.Activity; import android.app.Activity;
import android.content.ContentValues; import android.content.ContentValues;
@ -47,8 +46,8 @@ public class TaskController extends AbstractController {
// --- task list operations // --- task list operations
/** Return a list of all active tasks with notifications */ /** Return a list of all active tasks with notifications */
public Set<TaskModelForNotify> getTasksWithNotifications() { public HashSet<TaskModelForNotify> getTasksWithNotifications() {
Set<TaskModelForNotify> list = new HashSet<TaskModelForNotify>(); HashSet<TaskModelForNotify> list = new HashSet<TaskModelForNotify>();
Cursor cursor = database.query(TASK_TABLE_NAME, TaskModelForNotify.FIELD_LIST, Cursor cursor = database.query(TASK_TABLE_NAME, TaskModelForNotify.FIELD_LIST,
String.format("%s < %d AND (%s != 0 OR %s != 0)", String.format("%s < %d AND (%s != 0 OR %s != 0)",
AbstractTaskModel.PROGRESS_PERCENTAGE, AbstractTaskModel.PROGRESS_PERCENTAGE,
@ -69,8 +68,8 @@ public class TaskController extends AbstractController {
} }
/** Return a list of all active tasks with deadlines */ /** Return a list of all active tasks with deadlines */
public List<TaskModelForNotify> getTasksWithDeadlines() { public ArrayList<TaskModelForNotify> getTasksWithDeadlines() {
List<TaskModelForNotify> list = new ArrayList<TaskModelForNotify>(); ArrayList<TaskModelForNotify> list = new ArrayList<TaskModelForNotify>();
Cursor cursor = database.query(TASK_TABLE_NAME, TaskModelForNotify.FIELD_LIST, Cursor cursor = database.query(TASK_TABLE_NAME, TaskModelForNotify.FIELD_LIST,
String.format("%s < %d AND (%s != 0 OR %s != 0)", String.format("%s < %d AND (%s != 0 OR %s != 0)",
AbstractTaskModel.PROGRESS_PERCENTAGE, AbstractTaskModel.PROGRESS_PERCENTAGE,
@ -105,8 +104,8 @@ public class TaskController extends AbstractController {
} }
/** Create a list of tasks from the db cursor given */ /** Create a list of tasks from the db cursor given */
public List<TaskModelForList> createTaskListFromCursor(Cursor cursor) { public ArrayList<TaskModelForList> createTaskListFromCursor(Cursor cursor) {
List<TaskModelForList> list = new ArrayList<TaskModelForList>(); ArrayList<TaskModelForList> list = new ArrayList<TaskModelForList>();
if(cursor.getCount() == 0) if(cursor.getCount() == 0)
return list; return list;
@ -120,8 +119,8 @@ public class TaskController extends AbstractController {
} }
/** Get identifiers for all tasks */ /** Get identifiers for all tasks */
public Set<TaskIdentifier> getActiveTaskIdentifiers() { public HashSet<TaskIdentifier> getActiveTaskIdentifiers() {
Set<TaskIdentifier> list = new HashSet<TaskIdentifier>(); HashSet<TaskIdentifier> list = new HashSet<TaskIdentifier>();
Cursor cursor = database.query(TASK_TABLE_NAME, new String[] { KEY_ROWID }, Cursor cursor = database.query(TASK_TABLE_NAME, new String[] { KEY_ROWID },
AbstractTaskModel.PROGRESS_PERCENTAGE + " < " + AbstractTaskModel.PROGRESS_PERCENTAGE + " < " +
AbstractTaskModel.COMPLETE_PERCENTAGE, null, null, null, null, null); AbstractTaskModel.COMPLETE_PERCENTAGE, null, null, null, null, null);

@ -51,23 +51,18 @@ public class RTMSyncService extends SynchronizationService {
} }
@Override @Override
void synchronizeService(final Activity activity) { protected void synchronize(final Activity activity) {
Date lastSyncDate = Preferences.getSyncRTMLastSync(activity); if(Preferences.shouldSyncRTM(activity) &&
if(lastSyncDate == null || true) { Preferences.getSyncRTMToken(activity) == null) {
DialogUtilities.okCancelDialog(activity, DialogUtilities.okCancelDialog(activity,
activity.getResources().getString(R.string.sync_rtm_notes), activity.getResources().getString(R.string.sync_rtm_notes),
new Dialog.OnClickListener() { new Dialog.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
RTMSyncService.super.synchronizeService(activity); authenticate(activity);
} }
}, null); }, null);
} else } else
super.synchronizeService(activity);
}
@Override
protected void synchronize(Activity activity) {
authenticate(activity); authenticate(activity);
} }
@ -112,9 +107,6 @@ public class RTMSyncService extends SynchronizationService {
rtmService = new ServiceImpl(new ApplicationInfo( rtmService = new ServiceImpl(new ApplicationInfo(
apiKey, sharedSecret, appName)); apiKey, sharedSecret, appName));
final String url = rtmService.beginAuthorization(Perms.delete); final String url = rtmService.beginAuthorization(Perms.delete);
syncHandler.post(new Runnable() {
@Override
public void run() {
progressDialog.dismiss(); progressDialog.dismiss();
Resources r = activity.getResources(); Resources r = activity.getResources();
DialogUtilities.okCancelDialog(activity, DialogUtilities.okCancelDialog(activity,
@ -127,8 +119,6 @@ public class RTMSyncService extends SynchronizationService {
activity.startActivity(intent); activity.startActivity(intent);
} }
}, null); }, null);
}
});
} else { } else {
performSync(activity); 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 { try {
syncHandler.post(new Runnable() { syncHandler.post(new Runnable() {
@Override @Override

@ -3,9 +3,8 @@ package com.timsu.astrid.sync;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
@ -40,16 +39,28 @@ public abstract class SynchronizationService {
this.id = id; this.id = id;
} }
// called off the UI thread. does some setup
void synchronizeService(final Activity activity) { void synchronizeService(final Activity activity) {
progressDialog = ProgressDialog.show(activity, "Synchronization", syncHandler.post(new Runnable() {
"Checking Authorization...");
new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
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();
}
});
synchronize(activity); synchronize(activity);
syncHandler.post(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
} }
}).start();; });
} }
/** Synchronize with the service */ /** Synchronize with the service */
@ -137,18 +148,18 @@ public abstract class SynchronizationService {
TagController tagController = Synchronizer.getTagController(activity); TagController tagController = Synchronizer.getTagController(activity);
// get data out of the database (note we get non-completed tasks only) // get data out of the database (note we get non-completed tasks only)
Set<SyncMapping> mappings = syncController.getSyncMapping(getId()); HashSet<SyncMapping> mappings = syncController.getSyncMapping(getId());
Set<TaskIdentifier> localTasks = taskController.getActiveTaskIdentifiers(); HashSet<TaskIdentifier> localTasks = taskController.getActiveTaskIdentifiers();
Map<TagIdentifier, TagModelForView> tags = HashMap<TagIdentifier, TagModelForView> tags =
tagController.getAllTagsAsMap(activity); tagController.getAllTagsAsMap(activity);
// build local maps / lists // build local maps / lists
Map<String, SyncMapping> remoteIdToSyncMapping = HashMap<String, SyncMapping> remoteIdToSyncMapping =
new HashMap<String, SyncMapping>(); new HashMap<String, SyncMapping>();
Map<TaskIdentifier, SyncMapping> localIdToSyncMapping = HashMap<TaskIdentifier, SyncMapping> localIdToSyncMapping =
new HashMap<TaskIdentifier, SyncMapping>(); new HashMap<TaskIdentifier, SyncMapping>();
Set<SyncMapping> localChanges = new HashSet<SyncMapping>(); HashSet<SyncMapping> localChanges = new HashSet<SyncMapping>();
Set<TaskIdentifier> mappedTasks = new HashSet<TaskIdentifier>(); HashSet<TaskIdentifier> mappedTasks = new HashSet<TaskIdentifier>();
for(SyncMapping mapping : mappings) { for(SyncMapping mapping : mappings) {
if(mapping.isUpdated()) if(mapping.isUpdated())
localChanges.add(mapping); localChanges.add(mapping);
@ -158,7 +169,7 @@ public abstract class SynchronizationService {
} }
// build remote map // build remote map
Map<TaskIdentifier, TaskProxy> remoteChangeMap = HashMap<TaskIdentifier, TaskProxy> remoteChangeMap =
new HashMap<TaskIdentifier, TaskProxy>(); new HashMap<TaskIdentifier, TaskProxy>();
for(TaskProxy remoteTask : remoteTasks) { for(TaskProxy remoteTask : remoteTasks) {
if(remoteIdToSyncMapping.containsKey(remoteTask.getRemoteId())) { if(remoteIdToSyncMapping.containsKey(remoteTask.getRemoteId())) {
@ -175,11 +186,11 @@ public abstract class SynchronizationService {
progressDialog.setProgress(0); progressDialog.setProgress(0);
} }
}); });
Set<TaskIdentifier> newlyCreatedTasks = new HashSet<TaskIdentifier>( HashSet<TaskIdentifier> newlyCreatedTasks = new HashSet<TaskIdentifier>(
localTasks); localTasks);
newlyCreatedTasks.removeAll(mappedTasks); newlyCreatedTasks.removeAll(mappedTasks);
for(TaskIdentifier taskId : newlyCreatedTasks) { for(TaskIdentifier taskId : newlyCreatedTasks) {
List<TagIdentifier> taskTags = LinkedList<TagIdentifier> taskTags =
tagController.getTaskTags(activity, taskId); tagController.getTaskTags(activity, taskId);
String listName = null; String listName = null;
if(taskTags.size() > 0) { if(taskTags.size() > 0) {
@ -212,7 +223,7 @@ public abstract class SynchronizationService {
progressDialog.setProgress(0); progressDialog.setProgress(0);
} }
}); });
Set<TaskIdentifier> deletedTasks = new HashSet<TaskIdentifier>(mappedTasks); HashSet<TaskIdentifier> deletedTasks = new HashSet<TaskIdentifier>(mappedTasks);
deletedTasks.removeAll(localTasks); deletedTasks.removeAll(localTasks);
for(TaskIdentifier taskId : deletedTasks) { for(TaskIdentifier taskId : deletedTasks) {
SyncMapping mapping = localIdToSyncMapping.get(taskId); SyncMapping mapping = localIdToSyncMapping.get(taskId);
@ -349,7 +360,7 @@ public abstract class SynchronizationService {
// --- helper classes // --- helper classes
private class SyncStats { protected class SyncStats {
int localCreatedTasks = 0; int localCreatedTasks = 0;
int localUpdatedTasks = 0; int localUpdatedTasks = 0;
int localDeletedTasks = 0; int localDeletedTasks = 0;
@ -401,7 +412,7 @@ public abstract class SynchronizationService {
this.outOf = outOf; this.outOf = outOf;
} }
public void run() { public void run() {
progressDialog.setProgress(10000*step/outOf); progressDialog.setProgress(100*step/outOf);
} }
} }
} }

@ -19,12 +19,15 @@ public class Synchronizer {
/** Synchronize all activated sync services */ /** Synchronize all activated sync services */
public static void synchronize(final Activity activity) { 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 // RTM sync
if(Preferences.shouldSyncRTM(activity)) { if(Preferences.shouldSyncRTM(activity)) {
services.get(SYNC_ID_RTM).synchronizeService(activity); services.get(SYNC_ID_RTM).synchronizeService(activity);
} }
closeControllers();
} }
/** Clears tokens if services are disabled */ /** Clears tokens if services are disabled */
@ -72,7 +75,7 @@ public class Synchronizer {
private static TaskController taskController = null; private static TaskController taskController = null;
private static TagController tagController = null; private static TagController tagController = null;
private static void closeControllers() { static void closeControllers() {
if(syncController != null) { if(syncController != null) {
syncController.close(); syncController.close();
syncController = null; syncController = null;

@ -145,6 +145,11 @@ public class Preferences {
/** Set RTM Last Successful Sync Date */ /** Set RTM Last Successful Sync Date */
public static void setSyncRTMLastSync(Context context, Date 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 editor = getPrefs(context).edit();
editor.putLong(P_SYNC_RTM_LAST_SYNC, date.getTime()); editor.putLong(P_SYNC_RTM_LAST_SYNC, date.getTime());
editor.commit(); editor.commit();

Loading…
Cancel
Save