diff --git a/api/src/com/todoroo/astrid/sync/SyncV2Provider.java b/api/src/com/todoroo/astrid/sync/SyncV2Provider.java index d8c3a7d8a..246d2b776 100644 --- a/api/src/com/todoroo/astrid/sync/SyncV2Provider.java +++ b/api/src/com/todoroo/astrid/sync/SyncV2Provider.java @@ -1,7 +1,46 @@ package com.todoroo.astrid.sync; +import java.io.IOException; + +import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.service.DependencyInjectionService; +import com.todoroo.andlib.service.ExceptionService; + abstract public class SyncV2Provider { + + public class SyncExceptionHandler { + public void handleException(String tag, Exception e) { + //TODO: When Crittercism supports it, report error to them + getUtilities().setLastError(e.toString()); + + // occurs when application was closed + if(e instanceof IllegalStateException) { + exceptionService.reportError(tag + "-caught", e); //$NON-NLS-1$ + } + + // occurs when network error + else if(e instanceof IOException) { + exceptionService.reportError(tag + "-io", e); //$NON-NLS-1$ + } + + // unhandled error + else { + exceptionService.reportError(tag + "-unhandled", e); //$NON-NLS-1$ + } + } + } + + @Autowired + protected ExceptionService exceptionService; + + protected final SyncExceptionHandler handler; + + public SyncV2Provider() { + DependencyInjectionService.getInstance().inject(this); + handler = new SyncExceptionHandler(); + } + /** * @return sync provider name (displayed in sync menu) */ @@ -32,6 +71,11 @@ abstract public class SyncV2Provider { */ abstract public void signOut(); + /** + * @return sync utility instance + */ + abstract protected SyncProviderUtilities getUtilities(); + @Override public String toString() { return getName(); diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java index b84b836d5..1a41668c7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java @@ -59,6 +59,7 @@ import com.todoroo.astrid.service.StatisticsConstants; import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.TagDataService; import com.todoroo.astrid.service.TaskService; +import com.todoroo.astrid.sync.SyncV2Provider.SyncExceptionHandler; import com.todoroo.astrid.tags.TagService; import com.todoroo.astrid.utility.Flags; @@ -583,7 +584,7 @@ public final class ActFmSyncService { * Fetch tagData listing asynchronously */ public void fetchTagDataDashboard(boolean manual, final Runnable done) { - invokeFetchList("goal", manual, new ListItemProcessor() { + invokeFetchList("goal", manual, null, new ListItemProcessor() { @Override protected void mergeAndSave(JSONArray list, HashMap locals) throws JSONException { TagData remote = new TagData(); @@ -686,8 +687,8 @@ public final class ActFmSyncService { * @param manual * @param done */ - public void fetchActiveTasks(final boolean manual, Runnable done) { - invokeFetchList("task", manual, new ListItemProcessor() { + public void fetchActiveTasks(final boolean manual, SyncExceptionHandler handler, Runnable done) { + invokeFetchList("task", manual, handler, new ListItemProcessor() { @Override protected void mergeAndSave(JSONArray list, HashMap locals) throws JSONException { Task remote = new Task(); @@ -733,7 +734,7 @@ public final class ActFmSyncService { * @param done */ public void fetchTasksForTag(final TagData tagData, final boolean manual, Runnable done) { - invokeFetchList("task", manual, new ListItemProcessor() { + invokeFetchList("task", manual, null, new ListItemProcessor() { @Override protected void mergeAndSave(JSONArray list, HashMap locals) throws JSONException { Task remote = new Task(); @@ -782,7 +783,7 @@ public final class ActFmSyncService { * @param done */ public void fetchUpdatesForTag(final TagData tagData, final boolean manual, Runnable done) { - invokeFetchList("activity", manual, new UpdateListItemProcessor(), done, + invokeFetchList("activity", manual, null, new UpdateListItemProcessor(), done, "updates:" + tagData.getId(), "tag_id", tagData.getValue(TagData.REMOTE_ID)); } @@ -793,7 +794,7 @@ public final class ActFmSyncService { * @param runnable */ public void fetchUpdatesForTask(final Task task, boolean manual, Runnable done) { - invokeFetchList("activity", manual, new UpdateListItemProcessor(), done, + invokeFetchList("activity", manual, null, new UpdateListItemProcessor(), done, "comments:" + task.getId(), "task_id", task.getValue(Task.REMOTE_ID)); } @@ -803,7 +804,7 @@ public final class ActFmSyncService { * @param done */ public void fetchPersonalUpdates(boolean manual, Runnable done) { - invokeFetchList("activity", manual, new UpdateListItemProcessor(), done, "personal"); + invokeFetchList("activity", manual, null, new UpdateListItemProcessor(), done, "personal"); } private class UpdateListItemProcessor extends ListItemProcessor { @@ -931,7 +932,7 @@ public final class ActFmSyncService { } /** Call sync method */ - private void invokeFetchList(final String model, final boolean manual, + private void invokeFetchList(final String model, final boolean manual, final SyncExceptionHandler handler, final ListItemProcessor processor, final Runnable done, final String lastSyncKey, Object... params) { if(!checkForToken()) @@ -955,7 +956,10 @@ public final class ActFmSyncService { if(done != null) done.run(); } catch (IOException e) { - handleException("io-exception-list-" + model, e); + if (handler != null) + handler.handleException("io-exception-list-" + model, e); + else + handleException("io-exception-list-" + model, e); } catch (JSONException e) { handleException("json: " + result.toString(), e); } 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 65559ebe9..0aedd5480 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java @@ -13,8 +13,6 @@ 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.ExceptionService; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.utility.Preferences; @@ -38,23 +36,22 @@ public class ActFmSyncV2Provider extends SyncV2Provider { @Autowired ActFmSyncService actFmSyncService; - @Autowired ExceptionService exceptionService; - @Autowired TaskService taskService; static { AstridDependencyInjector.initialize(); } - public ActFmSyncV2Provider() { - DependencyInjectionService.getInstance().inject(this); - } - @Override public String getName() { return ContextManager.getString(R.string.actfm_APr_header); } + @Override + public ActFmPreferenceService getUtilities() { + return actFmPreferenceService; + } + @Override public void signOut() { actFmPreferenceService.setToken(null); @@ -98,9 +95,9 @@ public class ActFmSyncV2Provider extends SyncV2Provider { time = actFmSyncService.fetchTags(time); Preferences.setInt(LAST_TAG_FETCH_TIME, time); } catch (JSONException e) { - exceptionService.reportError("actfm-sync", e); //$NON-NLS-1$ + handler.handleException("actfm-sync", e); //$NON-NLS-1$ } catch (IOException e) { - exceptionService.reportError("actfm-sync", e); //$NON-NLS-1$ + handler.handleException("actfm-sync", e); //$NON-NLS-1$ } finally { callback.incrementProgress(20); if(finisher.decrementAndGet() == 0) { @@ -115,7 +112,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider { /** @return runnable to fetch changes to tags */ private void startTaskFetcher(final boolean manual, final SyncResultCallback callback, final AtomicInteger finisher) { - actFmSyncService.fetchActiveTasks(manual, new Runnable() { + actFmSyncService.fetchActiveTasks(manual, handler, new Runnable() { @Override public void run() { pushQueued(callback, finisher); diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncV2Provider.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncV2Provider.java index cf2095dc2..759575600 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncV2Provider.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncV2Provider.java @@ -16,7 +16,6 @@ import com.todoroo.andlib.data.AbstractModel; 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.sql.Criterion; import com.todoroo.andlib.sql.Join; import com.todoroo.andlib.sql.Query; @@ -61,15 +60,16 @@ public class GtasksSyncV2Provider extends SyncV2Provider { AstridDependencyInjector.initialize(); } - public GtasksSyncV2Provider() { - DependencyInjectionService.getInstance().inject(this); - } - @Override public String getName() { return ContextManager.getString(R.string.gtasks_GPr_header); } + @Override + public GtasksPreferenceService getUtilities() { + return gtasksPreferenceService; + } + @Override public void signOut() { gtasksPreferenceService.clearLastSyncDate(); @@ -97,7 +97,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider { try { gtasksListService.updateLists(invoker.allGtaskLists()); } catch (IOException e) { - // error updating lists + handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$ } finally { callback.incrementMax(25); } @@ -110,7 +110,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider { new Thread(new Runnable() { @Override public void run() { - synchronizeListHelper(list, invoker, callback); + synchronizeListHelper(list, invoker, handler, callback); if (finisher.decrementAndGet() == 0) { gtasksPreferenceService.recordSuccessfulSync(); callback.finished(); @@ -136,7 +136,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider { try { gtasksSyncService.pushTaskOnSave(task, task.getMergedValues(), invoker, false); } catch (IOException e) { - // Eh + handler.handleException("gtasks-sync-io", e); //$NON-NLS-1$ } finally { callback.incrementProgress(10); } @@ -166,7 +166,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider { String authToken = getValidatedAuthToken(); callback.incrementProgress(25); final GtasksInvoker service = new GtasksInvoker(authToken); - synchronizeListHelper(gtasksList, service, callback); + synchronizeListHelper(gtasksList, service, null, callback); } finally { callback.incrementProgress(25); callback.finished(); @@ -188,7 +188,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider { } - private void synchronizeListHelper(StoreObject list, GtasksInvoker invoker, SyncResultCallback callback) { + private void synchronizeListHelper(StoreObject list, GtasksInvoker invoker, SyncExceptionHandler handler, SyncResultCallback callback) { // Do stuff String listId = list.getValue(GtasksList.REMOTE_ID); long lastSyncDate; @@ -218,7 +218,8 @@ public class GtasksSyncV2Provider extends SyncV2Provider { gtasksTaskListUpdater.correctOrderAndIndentForList(listId); } } catch (IOException e) { - // Stuff + if (handler != null) + handler.handleException("gtasks-sync-io", e); //$NON-NLS-1$ } }