Do full sync on c2dm receive

Updates to timing intervals between sync tries

Synchronized queue of failed pushes
pull/14/head
Sam Bosley 13 years ago
parent a46645d128
commit 75d3016177

@ -24,6 +24,8 @@ import com.todoroo.andlib.sql.QueryTemplate;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.TagViewActivity; 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.actfm.sync.ActFmSyncService;
import com.todoroo.astrid.activity.ShortcutActivity; import com.todoroo.astrid.activity.ShortcutActivity;
import com.todoroo.astrid.activity.TaskListActivity; 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_REGISTRATION = "c2dm_key";
private static final String PREF_LAST_C2DM = "c2dm_last"; 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 ActFmSyncService actFmSyncService;
@Autowired TaskService taskService; @Autowired TaskService taskService;
@Autowired TagDataService tagDataService; @Autowired TagDataService tagDataService;
@Autowired UpdateDao updateDao; @Autowired UpdateDao updateDao;
@Autowired ActFmPreferenceService actFmPreferenceService;
static { static {
AstridDependencyInjector.initialize(); AstridDependencyInjector.initialize();
@ -70,7 +75,10 @@ public class C2DMReceiver extends BroadcastReceiver {
@Override @Override
public void run() { public void run() {
if(intent.hasExtra("web_update")) 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 else
handleMessage(intent); handleMessage(intent);
} }

@ -6,9 +6,11 @@ package com.todoroo.astrid.actfm.sync;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Queue; import java.util.Queue;
import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.MultipartEntity;
@ -82,27 +84,31 @@ public final class ActFmSyncService {
@Autowired UpdateDao updateDao; @Autowired UpdateDao updateDao;
@Autowired MetadataDao metadataDao; @Autowired MetadataDao metadataDao;
public static final long TIME_BETWEEN_TRIES = 5000 * 60; // 5 minutes between tries for failed pushes
private String token; private String token;
public ActFmSyncService() { public ActFmSyncService() {
DependencyInjectionService.getInstance().inject(this); DependencyInjectionService.getInstance().inject(this);
} }
private final Queue<Long> failedPushes = new LinkedList<Long>(); private final List<Long> failedPushes = Collections.synchronizedList(new LinkedList<Long>());
public void initialize() { public void initialize() {
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
while (true) { while (true) {
AndroidUtilities.sleepDeep(5000 * 60); // 5 minutes between tries AndroidUtilities.sleepDeep(TIME_BETWEEN_TRIES);
if(failedPushes.size() > 0) { if(failedPushes.size() > 0) {
Queue<Long> toTry = new LinkedList<Long>(); // Copy into a second queue so we don't end up infinitely retrying in the same loop Queue<Long> toTry = new LinkedList<Long>(); // Copy into a second queue so we don't end up infinitely retrying in the same loop
while(failedPushes.size() > 0) { while(failedPushes.size() > 0) {
toTry.add(failedPushes.remove()); toTry.add(failedPushes.remove(0));
} }
while(toTry.size() > 0) { while(toTry.size() > 0) {
if (!actFmPreferenceService.isOngoing()) { if (!actFmPreferenceService.isOngoing()) {
pushTask(toTry.remove()); pushTask(toTry.remove());
} else { // If normal sync ongoing/starts, let it deal with remaining sync
break;
} }
} }
} }

Loading…
Cancel
Save