Fix background sync setting for gtasks (and producteev) should be set on prefs finish, also created startup receiver for gtasks.

pull/14/head
Tim Su 15 years ago
parent 1e247a7ff7
commit da8c3bb180

@ -253,6 +253,12 @@
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<receiver android:name="com.todoroo.astrid.gtasks.GtasksStartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<!-- tags --> <!-- tags -->
<receiver android:name="com.todoroo.astrid.tags.TagsPlugin"> <receiver android:name="com.todoroo.astrid.tags.TagsPlugin">

@ -1,12 +1,12 @@
package com.todoroo.astrid.gtasks; package com.todoroo.astrid.gtasks;
import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.astrid.gtasks.sync.GtasksSyncProvider; import com.todoroo.astrid.gtasks.sync.GtasksSyncProvider;
import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.sync.SyncBackgroundService; import com.todoroo.astrid.sync.SyncBackgroundService;
import com.todoroo.astrid.sync.SyncProvider; import com.todoroo.astrid.sync.SyncProvider;
import com.todoroo.astrid.sync.SyncProviderUtilities; import com.todoroo.astrid.sync.SyncProviderUtilities;
import com.todoroo.astrid.utility.Constants;
public class GtasksBackgroundService extends SyncBackgroundService { public class GtasksBackgroundService extends SyncBackgroundService {
@ -19,6 +19,8 @@ public class GtasksBackgroundService extends SyncBackgroundService {
@Override @Override
protected SyncProviderUtilities getSyncUtilities() { protected SyncProviderUtilities getSyncUtilities() {
if(gtasksPreferenceService == null)
DependencyInjectionService.getInstance().inject(this);
return gtasksPreferenceService; return gtasksPreferenceService;
} }

@ -51,4 +51,10 @@ public class GtasksPreferences extends SyncProviderPreferences {
return gtasksPreferenceService; return gtasksPreferenceService;
} }
@Override
protected void onPause() {
super.onPause();
new GtasksBackgroundService().scheduleService();
}
} }

@ -0,0 +1,26 @@
/**
* See the file "LICENSE" for the full license governing this code.
*/
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 {
static {
AstridDependencyInjector.initialize();
}
@Override
/** Called when device is restarted */
public void onReceive(final Context context, Intent intent) {
ContextManager.setContext(context);
new GtasksBackgroundService().scheduleService();
}
}

@ -21,8 +21,6 @@ import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import com.timsu.astrid.R;
/** /**
* Choose which account to upload track information to. * Choose which account to upload track information to.
* @author Sandor Dornbush * @author Sandor Dornbush
@ -76,7 +74,6 @@ public class AccountChooser {
// Let the user choose. // Let the user choose.
final AlertDialog.Builder builder = new AlertDialog.Builder(activity); final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.choose_account_title);
builder.setCancelable(false); builder.setCancelable(false);
builder.setPositiveButton(android.R.string.ok, builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@ -111,8 +108,7 @@ public class AccountChooser {
private void alertNoAccounts(final Activity activity, private void alertNoAccounts(final Activity activity,
final AccountHandler handler) { final AccountHandler handler) {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity); final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.no_account_found_title); builder.setMessage("No Accounts Found."); //$NON-NLS-1$
builder.setMessage(R.string.no_account_found);
builder.setCancelable(true); builder.setCancelable(true);
builder.setNegativeButton(android.R.string.ok, builder.setNegativeButton(android.R.string.ok,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {

@ -1,20 +1,10 @@
package com.todoroo.astrid.producteev; package com.todoroo.astrid.producteev;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import com.timsu.astrid.R;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.producteev.sync.ProducteevSyncProvider; import com.todoroo.astrid.producteev.sync.ProducteevSyncProvider;
import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.sync.SyncBackgroundService;
import com.todoroo.astrid.sync.SyncProvider;
import com.todoroo.astrid.sync.SyncProviderUtilities;
/** /**
* SynchronizationService is the service that performs Astrid's background * SynchronizationService is the service that performs Astrid's background
@ -24,114 +14,28 @@ import com.todoroo.astrid.service.StatisticsService;
* @author Tim Su * @author Tim Su
* *
*/ */
public class ProducteevBackgroundService extends Service { public class ProducteevBackgroundService extends SyncBackgroundService {
/** Minimum time before an auto-sync */
private static final long AUTO_SYNC_MIN_OFFSET = 5*60*1000L;
/** alarm identifier */
public static final String SYNC_ACTION = "sync"; //$NON-NLS-1$
// --- BroadcastReceiver abstract methods
/** Receive the alarm - start the synchronize service! */
@Override @Override
public void onStart(Intent intent, int startId) { protected SyncProvider<?> getSyncProvider() {
try { return new ProducteevSyncProvider();
if(intent != null && SYNC_ACTION.equals(intent.getAction()))
startSynchronization(this);
} catch (Exception e) {
PluginServices.getExceptionService().reportError("pdv-bg-sync", e); //$NON-NLS-1$
}
}
/** Start the actual synchronization */
private void startSynchronization(Context context) {
if(context == null || context.getResources() == null)
return;
ContextManager.setContext(context);
if(ProducteevUtilities.INSTANCE.isOngoing())
return;
PluginServices.getTaskService();
StatisticsService.sessionStart(context);
new ProducteevSyncProvider().synchronize(context);
StatisticsService.sessionStop(context);
}
// --- alarm management
/**
* Schedules repeating alarm for auto-synchronization
*/
public static void scheduleService() {
int syncFrequencySeconds = Preferences.getIntegerFromString(
R.string.producteev_PPr_interval_key, -1);
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, 0,
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 static void unscheduleService(Context context) {
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
createAlarmIntent(context), PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pendingIntent);
} }
/** Create the alarm intent */ @Override
private static Intent createAlarmIntent(Context context) { protected SyncProviderUtilities getSyncUtilities() {
Intent intent = new Intent(context, ProducteevBackgroundService.class); return ProducteevUtilities.INSTANCE;
intent.setAction(SYNC_ACTION);
return intent;
} }
// --- utility methods @Override
public void onCreate() {
super.onCreate();
private static long computeNextSyncOffset(long interval) { StatisticsService.sessionStart(this);
// figure out last synchronize time
long lastSyncDate = ProducteevUtilities.INSTANCE.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;
} }
@Override @Override
public IBinder onBind(Intent intent) { public void onDestroy() {
return null; StatisticsService.sessionStop(this);
super.onDestroy();
} }
} }

@ -44,6 +44,12 @@ public class ProducteevPreferences extends SyncProviderPreferences {
return ProducteevUtilities.INSTANCE; return ProducteevUtilities.INSTANCE;
} }
@Override
protected void onPause() {
super.onPause();
new ProducteevBackgroundService().scheduleService();
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

@ -20,7 +20,7 @@ public class ProducteevStartupReceiver extends BroadcastReceiver {
/** Called when device is restarted */ /** Called when device is restarted */
public void onReceive(final Context context, Intent intent) { public void onReceive(final Context context, Intent intent) {
ContextManager.setContext(context); ContextManager.setContext(context);
ProducteevBackgroundService.scheduleService(); new ProducteevBackgroundService().scheduleService();
} }
} }

@ -11,8 +11,8 @@ import android.app.AlertDialog;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.util.Log; import android.util.Log;
@ -29,7 +29,6 @@ import com.todoroo.astrid.backup.BackupConstants;
import com.todoroo.astrid.backup.BackupService; import com.todoroo.astrid.backup.BackupService;
import com.todoroo.astrid.backup.TasksXmlImporter; import com.todoroo.astrid.backup.TasksXmlImporter;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.producteev.ProducteevBackgroundService;
import com.todoroo.astrid.producteev.ProducteevUtilities; import com.todoroo.astrid.producteev.ProducteevUtilities;
import com.todoroo.astrid.reminders.ReminderStartupReceiver; import com.todoroo.astrid.reminders.ReminderStartupReceiver;
import com.todoroo.astrid.utility.AstridPreferences; import com.todoroo.astrid.utility.AstridPreferences;
@ -132,7 +131,6 @@ public class StartupService {
ProducteevUtilities.INSTANCE.stopOngoing(); ProducteevUtilities.INSTANCE.stopOngoing();
MilkUtilities.INSTANCE.stopOngoing(); MilkUtilities.INSTANCE.stopOngoing();
ProducteevBackgroundService.scheduleService();
BackupService.scheduleService(context); BackupService.scheduleService(context);
// get and display update messages // get and display update messages

Loading…
Cancel
Save