change sync interval on change in code

This commit is contained in:
Adithya Abraham Philip
2016-03-24 19:40:18 +05:30
parent f005bbad96
commit 4b6df7d17c
4 changed files with 63 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.PeriodicSync;
import android.content.SyncResult;
import android.database.Cursor;
import android.graphics.Bitmap;
@@ -529,6 +530,10 @@ public class KeyserverSyncAdapterService extends Service {
return builder.build();
}
/**
* creates a new sync if one does not exist, or updates an existing sync if the sync interval
* has changed.
*/
public static void enableKeyserverSync(Context context) {
Account account = KeychainApplication.createAccountIfNecessary(context);
@@ -539,12 +544,26 @@ public class KeyserverSyncAdapterService extends Service {
ContentResolver.setIsSyncable(account, Constants.PROVIDER_AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, Constants.PROVIDER_AUTHORITY, true);
ContentResolver.addPeriodicSync(
account,
Constants.PROVIDER_AUTHORITY,
new Bundle(),
SYNC_INTERVAL
);
boolean intervalChanged = false;
boolean syncExists = Preferences.getKeyserverSyncEnabled(context);
if (syncExists) {
long oldInterval = ContentResolver.getPeriodicSyncs(
account, Constants.PROVIDER_AUTHORITY).get(0).period;
if (oldInterval != SYNC_INTERVAL) {
intervalChanged = true;
}
}
if (!syncExists || intervalChanged) {
ContentResolver.addPeriodicSync(
account,
Constants.PROVIDER_AUTHORITY,
new Bundle(),
SYNC_INTERVAL
);
}
}
private boolean isSyncEnabled() {