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 14 years ago
parent 1e247a7ff7
commit da8c3bb180

@ -253,6 +253,12 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</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 -->
<receiver android:name="com.todoroo.astrid.tags.TagsPlugin">

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

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

@ -1,20 +1,10 @@
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.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
@ -24,114 +14,28 @@ import com.todoroo.astrid.service.StatisticsService;
* @author Tim Su
*
*/
public class ProducteevBackgroundService extends Service {
/** 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
public class ProducteevBackgroundService extends SyncBackgroundService {
/** Receive the alarm - start the synchronize service! */
@Override
public void onStart(Intent intent, int startId) {
try {
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);
protected SyncProvider<?> getSyncProvider() {
return new ProducteevSyncProvider();
}
/**
* 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 */
private static Intent createAlarmIntent(Context context) {
Intent intent = new Intent(context, ProducteevBackgroundService.class);
intent.setAction(SYNC_ACTION);
return intent;
@Override
protected SyncProviderUtilities getSyncUtilities() {
return ProducteevUtilities.INSTANCE;
}
// --- utility methods
private static long computeNextSyncOffset(long interval) {
// 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
public void onCreate() {
super.onCreate();
StatisticsService.sessionStart(this);
}
@Override
public IBinder onBind(Intent intent) {
return null;
public void onDestroy() {
StatisticsService.sessionStop(this);
super.onDestroy();
}
}

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

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

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

Loading…
Cancel
Save