diff --git a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksBackgroundService.java b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksBackgroundService.java index 80a19326f..385e6d523 100644 --- a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksBackgroundService.java +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksBackgroundService.java @@ -5,16 +5,11 @@ */ package com.todoroo.astrid.gtasks; -import android.app.AlarmManager; -import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.IBinder; -import android.util.Log; import com.todoroo.andlib.service.ContextManager; -import com.todoroo.andlib.utility.DateUtilities; -import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider; import com.todoroo.astrid.sync.SyncResultCallbackAdapter; @@ -35,9 +30,6 @@ public class GtasksBackgroundService extends InjectingService { @Inject GtasksPreferenceService gtasksPreferenceService; @Inject GtasksSyncV2Provider gtasksSyncV2Provider; - /** Minimum time before an auto-sync */ - private static final long AUTO_SYNC_MIN_OFFSET = 5*60*1000L; - private final AtomicBoolean started = new AtomicBoolean(false); /** Receive the alarm - start the synchronize service! */ @@ -48,7 +40,7 @@ public class GtasksBackgroundService extends InjectingService { startSynchronization(this); } } catch (Exception e) { - log.error("{}-bg-sync", gtasksPreferenceService.getIdentifier(), e); + log.error(e.getMessage(), e); } } @@ -80,76 +72,4 @@ public class GtasksBackgroundService extends InjectingService { public IBinder onBind(Intent intent) { return null; } - - // --- alarm management - - /** - * Schedules repeating alarm for auto-synchronization - */ - public void scheduleService() { - int syncFrequencySeconds = 0; - try { - syncFrequencySeconds = Preferences.getIntegerFromString( - gtasksPreferenceService.getSyncIntervalKey(), -1); - } catch(ClassCastException e) { - Preferences.setStringFromInteger(gtasksPreferenceService.getSyncIntervalKey(), 0); - } - Context context = ContextManager.getContext(); - if(syncFrequencySeconds <= 0) { - unscheduleService(context); - return; - } - - // figure out synchronization frequency - long interval = 1000L * syncFrequencySeconds; - long offset = computeNextSyncOffset(interval); - - // give a little padding - offset = Math.max(offset, AUTO_SYNC_MIN_OFFSET); - - AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - PendingIntent pendingIntent = PendingIntent.getService(context, gtasksPreferenceService.getSyncIntervalKey(), - createAlarmIntent(context), PendingIntent.FLAG_UPDATE_CURRENT); - - Log.i("Astrid", "Autosync set for " + offset / 1000 //$NON-NLS-1$ //$NON-NLS-2$ - + " seconds repeating every " + syncFrequencySeconds); //$NON-NLS-1$ - - // cancel all existing - am.cancel(pendingIntent); - - // schedule new - am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + offset, - interval, pendingIntent); - } - - - /** - * Removes repeating alarm for auto-synchronization - */ - private void unscheduleService(Context context) { - AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); - PendingIntent pendingIntent = PendingIntent.getService(context, gtasksPreferenceService.getSyncIntervalKey(), - createAlarmIntent(context), PendingIntent.FLAG_UPDATE_CURRENT); - am.cancel(pendingIntent); - } - - /** Create the alarm intent */ - private Intent createAlarmIntent(Context context) { - return new Intent(context, getClass()); - } - - // --- utility methods - - private long computeNextSyncOffset(long interval) { - // figure out last synchronize time - long lastSyncDate = gtasksPreferenceService.getLastSyncDate(); - - // if user never synchronized, give them a full offset period before bg sync - if(lastSyncDate != 0) { - return Math.max(0, lastSyncDate + interval - DateUtilities.now()); - } else { - return interval; - } - } - } diff --git a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java index 5919e9613..a9dd18521 100644 --- a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksPreferences.java @@ -27,6 +27,7 @@ public class GtasksPreferences extends SyncProviderPreferences { @Autowired private GtasksPreferenceService gtasksPreferenceService; @Autowired private GtasksSyncV2Provider gtasksSyncV2Provider; + @Autowired private GtasksScheduler gtasksScheduler; public GtasksPreferences() { super(); @@ -82,6 +83,6 @@ public class GtasksPreferences extends SyncProviderPreferences { @Override protected void onPause() { super.onPause(); - new GtasksBackgroundService().scheduleService(); + gtasksScheduler.scheduleService(); } } diff --git a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksScheduler.java b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksScheduler.java new file mode 100644 index 000000000..f648f7f9b --- /dev/null +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksScheduler.java @@ -0,0 +1,97 @@ +package com.todoroo.astrid.gtasks; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import com.todoroo.andlib.service.ContextManager; +import com.todoroo.andlib.utility.DateUtilities; +import com.todoroo.andlib.utility.Preferences; + +import javax.inject.Inject; +import javax.inject.Singleton; + +@Singleton +public class GtasksScheduler { + + /** Minimum time before an auto-sync */ + private static final long AUTO_SYNC_MIN_OFFSET = 5*60*1000L; + + private final GtasksPreferenceService gtasksPreferenceService; + + @Inject + public GtasksScheduler(GtasksPreferenceService gtasksPreferenceService) { + this.gtasksPreferenceService = gtasksPreferenceService; + } + + /** + * Schedules repeating alarm for auto-synchronization + */ + public void scheduleService() { + int syncFrequencySeconds = 0; + try { + syncFrequencySeconds = Preferences.getIntegerFromString( + gtasksPreferenceService.getSyncIntervalKey(), -1); + } catch(ClassCastException e) { + Preferences.setStringFromInteger(gtasksPreferenceService.getSyncIntervalKey(), 0); + } + Context context = ContextManager.getContext(); + if(syncFrequencySeconds <= 0) { + unscheduleService(context); + return; + } + + // figure out synchronization frequency + long interval = 1000L * syncFrequencySeconds; + long offset = computeNextSyncOffset(interval); + + // give a little padding + offset = Math.max(offset, AUTO_SYNC_MIN_OFFSET); + + AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + PendingIntent pendingIntent = PendingIntent.getService(context, gtasksPreferenceService.getSyncIntervalKey(), + createAlarmIntent(context), PendingIntent.FLAG_UPDATE_CURRENT); + + Log.i("Astrid", "Autosync set for " + offset / 1000 //$NON-NLS-1$ //$NON-NLS-2$ + + " seconds repeating every " + syncFrequencySeconds); //$NON-NLS-1$ + + // cancel all existing + am.cancel(pendingIntent); + + // schedule new + am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + offset, + interval, pendingIntent); + } + + + /** + * Removes repeating alarm for auto-synchronization + */ + private void unscheduleService(Context context) { + AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); + PendingIntent pendingIntent = PendingIntent.getService(context, gtasksPreferenceService.getSyncIntervalKey(), + createAlarmIntent(context), PendingIntent.FLAG_UPDATE_CURRENT); + am.cancel(pendingIntent); + } + + /** Create the alarm intent */ + private Intent createAlarmIntent(Context context) { + return new Intent(context, GtasksBackgroundService.class); + } + + // --- utility methods + + private long computeNextSyncOffset(long interval) { + // figure out last synchronize time + long lastSyncDate = gtasksPreferenceService.getLastSyncDate(); + + // if user never synchronized, give them a full offset period before bg sync + if(lastSyncDate != 0) { + return Math.max(0, lastSyncDate + interval - DateUtilities.now()); + } else { + return interval; + } + } +} diff --git a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksStartupReceiver.java b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksStartupReceiver.java index 0b33bb861..ee159436e 100644 --- a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksStartupReceiver.java +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksStartupReceiver.java @@ -5,24 +5,23 @@ */ package com.todoroo.astrid.gtasks; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import com.todoroo.andlib.service.ContextManager; -import com.todoroo.astrid.service.AstridDependencyInjector; -public class GtasksStartupReceiver extends BroadcastReceiver { +import org.tasks.injection.InjectingBroadcastReceiver; - static { - AstridDependencyInjector.initialize(); - } +import javax.inject.Inject; + +public class GtasksStartupReceiver extends InjectingBroadcastReceiver { + + @Inject GtasksScheduler scheduler; @Override - /** Called when device is restarted */ public void onReceive(final Context context, Intent intent) { + super.onReceive(context, intent); ContextManager.setContext(context); - new GtasksBackgroundService().scheduleService(); + scheduler.scheduleService(); } - } diff --git a/astrid/src/main/java/com/todoroo/astrid/service/AstridDependencyInjector.java b/astrid/src/main/java/com/todoroo/astrid/service/AstridDependencyInjector.java index 18bcb1220..a7d14f1a7 100644 --- a/astrid/src/main/java/com/todoroo/astrid/service/AstridDependencyInjector.java +++ b/astrid/src/main/java/com/todoroo/astrid/service/AstridDependencyInjector.java @@ -19,6 +19,7 @@ import com.todoroo.astrid.dao.UserActivityDao; import com.todoroo.astrid.gtasks.GtasksListService; import com.todoroo.astrid.gtasks.GtasksMetadataService; import com.todoroo.astrid.gtasks.GtasksPreferenceService; +import com.todoroo.astrid.gtasks.GtasksScheduler; import com.todoroo.astrid.gtasks.GtasksTaskListUpdater; import com.todoroo.astrid.gtasks.sync.GtasksSyncService; import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider; @@ -78,6 +79,7 @@ public class AstridDependencyInjector extends AbstractDependencyInjector { @Inject GtasksTaskListUpdater gtasksTaskListUpdater; @Inject GtasksSyncV2Provider gtasksSyncV2Provider; @Inject WidgetHelper widgetHelper; + @Inject GtasksScheduler gtasksScheduler; /** * Initialize list of injectables. Special care must used when @@ -131,6 +133,7 @@ public class AstridDependencyInjector extends AbstractDependencyInjector { injectables.put("filterCounter", filterCounter); injectables.put("refreshScheduler", refreshScheduler); injectables.put("widgetHelper", widgetHelper); + injectables.put("gtasksScheduler", gtasksScheduler); } /**