Suppress widget updates during sync

pull/14/head
Sam Bosley 12 years ago
parent 1b7732ea6a
commit 47cf53ec1f

@ -0,0 +1,50 @@
package com.todoroo.astrid.service;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.sync.SyncResultCallback;
import com.todoroo.astrid.widget.TasksWidget;
public class SyncResultCallbackWrapper implements SyncResultCallback {
private final SyncResultCallback wrapped;
public SyncResultCallbackWrapper(SyncResultCallback wrap) {
this.wrapped = wrap;
}
@Override
public void incrementMax(int incrementBy) {
wrapped.incrementMax(incrementBy);
}
@Override
public void incrementProgress(int incrementBy) {
wrapped.incrementProgress(incrementBy);
}
@Override
public void started() {
wrapped.started();
}
@Override
public void finished() {
wrapped.finished();
}
public static class WidgetUpdatingCallbackWrapper extends SyncResultCallbackWrapper {
public WidgetUpdatingCallbackWrapper(SyncResultCallback wrap) {
super(wrap);
}
@Override
public void started() {
super.started();
TasksWidget.suppressUpdateFlag = DateUtilities.now();
}
@Override
public void finished() {
super.finished();
TasksWidget.suppressUpdateFlag = 0L;
TasksWidget.updateWidgets(ContextManager.getContext());
}
}
}

@ -11,6 +11,7 @@ import java.util.List;
import com.todoroo.astrid.actfm.sync.ActFmSyncV2Provider;
import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider;
import com.todoroo.astrid.service.SyncResultCallbackWrapper.WidgetUpdatingCallbackWrapper;
import com.todoroo.astrid.sync.SyncResultCallback;
import com.todoroo.astrid.sync.SyncV2Provider;
@ -61,34 +62,19 @@ public class SyncV2Service {
* @param callback result callback
* @return true if any servide was logged in and initiated a sync
*/
public boolean synchronizeActiveTasks(final boolean manual, final SyncResultCallback callback) {
public boolean synchronizeActiveTasks(final boolean manual, SyncResultCallback callback) {
final List<SyncV2Provider> active = activeProviders();
if (active.size() == 0)
return false;
if (active.size() > 1) {
SyncResultCallback newCallback = new SyncResultCallback() {
SyncResultCallback newCallback = new WidgetUpdatingCallbackWrapper(callback) {
private int next = 1;
@Override
public void started() {
callback.started();
}
@Override
public void incrementProgress(int incrementBy) {
callback.incrementProgress(incrementBy);
}
@Override
public void incrementMax(int incrementBy) {
callback.incrementMax(incrementBy);
}
@Override
public void finished() {
callback.finished();
super.finished();
if (next < active.size())
active.get(next++).synchronizeActiveTasks(manual, this);
}
@ -96,7 +82,7 @@ public class SyncV2Service {
active.get(0).synchronizeActiveTasks(manual, newCallback);
} else if (active.size() == 1) {
active.get(0).synchronizeActiveTasks(manual, callback);
active.get(0).synchronizeActiveTasks(manual, new WidgetUpdatingCallbackWrapper(callback));
}
return true;
@ -112,7 +98,7 @@ public class SyncV2Service {
public void synchronizeList(Object list, boolean manual, SyncResultCallback callback) {
for(SyncV2Provider provider : providers) {
if(provider.isActive())
provider.synchronizeList(list, manual, callback);
provider.synchronizeList(list, manual, new WidgetUpdatingCallbackWrapper(callback));
}
}

@ -29,6 +29,7 @@ import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.activity.TaskEditActivity;
import com.todoroo.astrid.activity.TaskEditFragment;
@ -51,6 +52,9 @@ public class TasksWidget extends AppWidgetProvider {
public static final int THEME_LEGACY = -1;
public static long suppressUpdateFlag = 0; // Timestamp--don't update widgets if this flag is non-zero and now() is within 5 minutes
private static final long SUPPRESS_TIME = DateUtilities.ONE_MINUTE * 5;
static {
AstridDependencyInjector.initialize();
}
@ -75,6 +79,9 @@ public class TasksWidget extends AppWidgetProvider {
* @param id
*/
public static void updateWidgets(Context context) {
if (suppressUpdateFlag > 0 && DateUtilities.now() - suppressUpdateFlag < SUPPRESS_TIME)
return;
suppressUpdateFlag = 0;
context.startService(new Intent(context,
TasksWidget.WidgetUpdateService.class));
}

Loading…
Cancel
Save