From 75d3016177e3363cc1a2b4c049f3c8037abbce99 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Wed, 7 Sep 2011 17:12:04 -0700 Subject: [PATCH] Do full sync on c2dm receive Updates to timing intervals between sync tries Synchronized queue of failed pushes --- astrid/plugin-src/com/timsu/astrid/C2DMReceiver.java | 10 +++++++++- .../todoroo/astrid/actfm/sync/ActFmSyncService.java | 12 +++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/astrid/plugin-src/com/timsu/astrid/C2DMReceiver.java b/astrid/plugin-src/com/timsu/astrid/C2DMReceiver.java index 19358794a..cc54b8d2b 100644 --- a/astrid/plugin-src/com/timsu/astrid/C2DMReceiver.java +++ b/astrid/plugin-src/com/timsu/astrid/C2DMReceiver.java @@ -24,6 +24,8 @@ import com.todoroo.andlib.sql.QueryTemplate; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.actfm.TagViewActivity; +import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; +import com.todoroo.astrid.actfm.sync.ActFmSyncProvider; import com.todoroo.astrid.actfm.sync.ActFmSyncService; import com.todoroo.astrid.activity.ShortcutActivity; import com.todoroo.astrid.activity.TaskListActivity; @@ -50,10 +52,13 @@ public class C2DMReceiver extends BroadcastReceiver { private static final String PREF_REGISTRATION = "c2dm_key"; private static final String PREF_LAST_C2DM = "c2dm_last"; + private static final long FULL_SYNC_TIME_INTERVAL = 5000 * 60; // Min 5 minutes between full syncs + @Autowired ActFmSyncService actFmSyncService; @Autowired TaskService taskService; @Autowired TagDataService tagDataService; @Autowired UpdateDao updateDao; + @Autowired ActFmPreferenceService actFmPreferenceService; static { AstridDependencyInjector.initialize(); @@ -70,7 +75,10 @@ public class C2DMReceiver extends BroadcastReceiver { @Override public void run() { if(intent.hasExtra("web_update")) - handleWebUpdate(intent); + if (DateUtilities.now() - actFmPreferenceService.getLastSyncDate() > FULL_SYNC_TIME_INTERVAL) + new ActFmSyncProvider().synchronize(ContextManager.getContext()); + else + handleWebUpdate(intent); else handleMessage(intent); } 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 fe743af9d..adbfee731 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java @@ -6,9 +6,11 @@ package com.todoroo.astrid.actfm.sync; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; +import java.util.List; import java.util.Queue; import org.apache.http.entity.mime.MultipartEntity; @@ -82,27 +84,31 @@ public final class ActFmSyncService { @Autowired UpdateDao updateDao; @Autowired MetadataDao metadataDao; + public static final long TIME_BETWEEN_TRIES = 5000 * 60; // 5 minutes between tries for failed pushes + private String token; public ActFmSyncService() { DependencyInjectionService.getInstance().inject(this); } - private final Queue failedPushes = new LinkedList(); + private final List failedPushes = Collections.synchronizedList(new LinkedList()); public void initialize() { new Thread(new Runnable() { public void run() { while (true) { - AndroidUtilities.sleepDeep(5000 * 60); // 5 minutes between tries + AndroidUtilities.sleepDeep(TIME_BETWEEN_TRIES); if(failedPushes.size() > 0) { Queue toTry = new LinkedList(); // Copy into a second queue so we don't end up infinitely retrying in the same loop while(failedPushes.size() > 0) { - toTry.add(failedPushes.remove()); + toTry.add(failedPushes.remove(0)); } while(toTry.size() > 0) { if (!actFmPreferenceService.isOngoing()) { pushTask(toTry.remove()); + } else { // If normal sync ongoing/starts, let it deal with remaining sync + break; } } }