mirror of https://github.com/tasks/tasks
parent
592124af79
commit
dd97040005
@ -1,54 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2012 Todoroo Inc
|
||||
*
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
|
||||
import com.todoroo.andlib.service.ContextManager;
|
||||
import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tasks.Broadcaster;
|
||||
import org.tasks.injection.InjectingService;
|
||||
import org.tasks.sync.RecordSyncStatusCallback;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksBackgroundService extends InjectingService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GtasksBackgroundService.class);
|
||||
|
||||
@Inject GtasksPreferenceService gtasksPreferenceService;
|
||||
@Inject GtasksSyncV2Provider gtasksSyncV2Provider;
|
||||
@Inject Broadcaster broadcaster;
|
||||
|
||||
private final AtomicBoolean started = new AtomicBoolean(false);
|
||||
|
||||
/** Receive the alarm - start the synchronize service! */
|
||||
@Override
|
||||
public void onStart(Intent intent, int startId) {
|
||||
try {
|
||||
if(intent != null && !started.getAndSet(true)) {
|
||||
ContextManager.setContext(this);
|
||||
|
||||
if(gtasksPreferenceService.isLoggedIn() && gtasksSyncV2Provider.isActive()) {
|
||||
gtasksSyncV2Provider.synchronizeActiveTasks(new RecordSyncStatusCallback(gtasksPreferenceService, broadcaster));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.todoroo.andlib.utility.DateUtilities;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tasks.injection.ForApplication;
|
||||
import org.tasks.preferences.Preferences;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class GtasksScheduler {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GtasksScheduler.class);
|
||||
|
||||
/** Minimum time before an auto-sync */
|
||||
private static final long AUTO_SYNC_MIN_OFFSET = 5*60*1000L;
|
||||
|
||||
private final GtasksPreferenceService gtasksPreferenceService;
|
||||
private Context context;
|
||||
private Preferences preferences;
|
||||
|
||||
@Inject
|
||||
public GtasksScheduler(GtasksPreferenceService gtasksPreferenceService, @ForApplication Context context, Preferences preferences) {
|
||||
this.gtasksPreferenceService = gtasksPreferenceService;
|
||||
this.context = context;
|
||||
this.preferences = preferences;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules repeating alarm for auto-synchronization
|
||||
*/
|
||||
public void scheduleService() {
|
||||
int syncFrequencySeconds = 0;
|
||||
try {
|
||||
syncFrequencySeconds = preferences.getIntegerFromString(
|
||||
gtasksPreferenceService.getSyncIntervalKey(), -1);
|
||||
} catch(ClassCastException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
preferences.setStringFromInteger(gtasksPreferenceService.getSyncIntervalKey(), 0);
|
||||
}
|
||||
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.info("Autosync set for {} seconds repeating every {}", offset / 1000, 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2012 Todoroo Inc
|
||||
*
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.gtasks;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.todoroo.andlib.service.ContextManager;
|
||||
|
||||
import org.tasks.injection.InjectingBroadcastReceiver;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GtasksStartupReceiver extends InjectingBroadcastReceiver {
|
||||
|
||||
@Inject GtasksScheduler scheduler;
|
||||
|
||||
@Override
|
||||
public void onReceive(final Context context, Intent intent) {
|
||||
super.onReceive(context, intent);
|
||||
ContextManager.setContext(context);
|
||||
scheduler.scheduleService();
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package org.tasks.scheduling;
|
||||
|
||||
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
|
||||
import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tasks.Broadcaster;
|
||||
import org.tasks.R;
|
||||
import org.tasks.preferences.Preferences;
|
||||
import org.tasks.sync.RecordSyncStatusCallback;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
public class GtasksBackgroundService extends RecurringIntervalIntentService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GtasksBackgroundService.class);
|
||||
|
||||
@Inject Preferences preferences;
|
||||
@Inject GtasksPreferenceService gtasksPreferenceService;
|
||||
@Inject GtasksSyncV2Provider gtasksSyncV2Provider;
|
||||
@Inject Broadcaster broadcaster;
|
||||
|
||||
public GtasksBackgroundService() {
|
||||
super(GtasksBackgroundService.class.getSimpleName());
|
||||
}
|
||||
|
||||
@Override
|
||||
void run() {
|
||||
if (gtasksPreferenceService.isOngoing()) {
|
||||
log.debug("aborting: sync ongoing");
|
||||
return;
|
||||
}
|
||||
if(gtasksPreferenceService.isLoggedIn() && gtasksSyncV2Provider.isActive()) {
|
||||
gtasksSyncV2Provider.synchronizeActiveTasks(new RecordSyncStatusCallback(gtasksPreferenceService, broadcaster));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
long intervalMillis() {
|
||||
try {
|
||||
return SECONDS.toMillis(preferences.getIntegerFromString(R.string.gtasks_GPr_interval_key, 0));
|
||||
} catch(Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
preferences.setString(R.string.gtasks_GPr_interval_key, "0");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
String getLastRunPreference() {
|
||||
return "gtasks_last_sync";
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package org.tasks.scheduling;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tasks.injection.InjectingIntentService;
|
||||
import org.tasks.preferences.Preferences;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.tasks.date.DateTimeUtils.currentTimeMillis;
|
||||
import static org.tasks.date.DateTimeUtils.newDateTime;
|
||||
|
||||
public abstract class RecurringIntervalIntentService extends InjectingIntentService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(RecurringIntervalIntentService.class);
|
||||
|
||||
private static final long PADDING = SECONDS.toMillis(1);
|
||||
|
||||
@Inject Preferences preferences;
|
||||
|
||||
public RecurringIntervalIntentService(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
super.onHandleIntent(intent);
|
||||
|
||||
long interval = intervalMillis();
|
||||
|
||||
if (interval <= 0) {
|
||||
log.debug("service disabled");
|
||||
return;
|
||||
}
|
||||
|
||||
long lastRun = preferences.getLong(getLastRunPreference(), 0);
|
||||
long now = currentTimeMillis();
|
||||
long nextRun = lastRun + interval;
|
||||
|
||||
if (nextRun < now + PADDING) {
|
||||
nextRun = now + interval;
|
||||
log.debug("running now [nextRun={}]", newDateTime(nextRun));
|
||||
preferences.setLong(getLastRunPreference(), now);
|
||||
run();
|
||||
} else {
|
||||
log.debug("will run at {} [lastRun={}]", newDateTime(nextRun), newDateTime(lastRun));
|
||||
}
|
||||
|
||||
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
PendingIntent pendingIntent = PendingIntent.getService(this, 0, new Intent(this, this.getClass()), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
alarmManager.set(AlarmManager.RTC, nextRun, pendingIntent);
|
||||
}
|
||||
|
||||
abstract void run();
|
||||
|
||||
abstract long intervalMillis();
|
||||
|
||||
abstract String getLastRunPreference();
|
||||
}
|
Loading…
Reference in New Issue