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

@@ -100,6 +100,11 @@ public class KeychainApplication extends Application {
// Add OpenKeychain account to Android to link contacts with keys and keyserver sync // Add OpenKeychain account to Android to link contacts with keys and keyserver sync
createAccountIfNecessary(this); createAccountIfNecessary(this);
if (Preferences.getKeyserverSyncEnabled(this)) {
// will update a keyserver sync if the interval has changed
KeyserverSyncAdapterService.enableKeyserverSync(this);
}
// if first time, enable keyserver and contact sync // if first time, enable keyserver and contact sync
if (Preferences.getPreferences(this).isFirstTime()) { if (Preferences.getPreferences(this).isFirstTime()) {
KeyserverSyncAdapterService.enableKeyserverSync(this); KeyserverSyncAdapterService.enableKeyserverSync(this);

View File

@@ -11,6 +11,7 @@ import android.content.ContentProviderClient;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.PeriodicSync;
import android.content.SyncResult; import android.content.SyncResult;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@@ -529,6 +530,10 @@ public class KeyserverSyncAdapterService extends Service {
return builder.build(); 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) { public static void enableKeyserverSync(Context context) {
Account account = KeychainApplication.createAccountIfNecessary(context); Account account = KeychainApplication.createAccountIfNecessary(context);
@@ -539,12 +544,26 @@ public class KeyserverSyncAdapterService extends Service {
ContentResolver.setIsSyncable(account, Constants.PROVIDER_AUTHORITY, 1); ContentResolver.setIsSyncable(account, Constants.PROVIDER_AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, Constants.PROVIDER_AUTHORITY, true); ContentResolver.setSyncAutomatically(account, Constants.PROVIDER_AUTHORITY, true);
ContentResolver.addPeriodicSync(
account, boolean intervalChanged = false;
Constants.PROVIDER_AUTHORITY, boolean syncExists = Preferences.getKeyserverSyncEnabled(context);
new Bundle(),
SYNC_INTERVAL 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() { private boolean isSyncEnabled() {

View File

@@ -47,6 +47,7 @@ import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.KeychainApplication;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity; import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity;
import org.sufficientlysecure.keychain.service.ContactSyncAdapterService; import org.sufficientlysecure.keychain.service.ContactSyncAdapterService;
@@ -422,8 +423,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
super.onResume(); super.onResume();
// this needs to be done in onResume since the user can change sync values from Android // this needs to be done in onResume since the user can change sync values from Android
// settings and we need to reflect that change when the user navigates back // settings and we need to reflect that change when the user navigates back
AccountManager manager = AccountManager.get(getActivity()); final Account account = KeychainApplication.createAccountIfNecessary(getActivity());
final Account account = manager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
// for keyserver sync // for keyserver sync
initializeSyncCheckBox( initializeSyncCheckBox(
(SwitchPreference) findPreference(Constants.Pref.SYNC_KEYSERVER), (SwitchPreference) findPreference(Constants.Pref.SYNC_KEYSERVER),
@@ -441,8 +441,11 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
private void initializeSyncCheckBox(final SwitchPreference syncCheckBox, private void initializeSyncCheckBox(final SwitchPreference syncCheckBox,
final Account account, final Account account,
final String authority) { final String authority) {
boolean syncEnabled = ContentResolver.getSyncAutomatically(account, authority) // account is null if it could not be created for some reason
&& checkContactsPermission(authority); boolean syncEnabled =
account != null
&& ContentResolver.getSyncAutomatically(account, authority)
&& checkContactsPermission(authority);
syncCheckBox.setChecked(syncEnabled); syncCheckBox.setChecked(syncEnabled);
setSummary(syncCheckBox, authority, syncEnabled); setSummary(syncCheckBox, authority, syncEnabled);
@@ -464,6 +467,11 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
return false; return false;
} }
} else { } else {
if (account == null) {
// if account could not be created for some reason,
// we can't have our sync
return false;
}
// disable syncs // disable syncs
ContentResolver.setSyncAutomatically(account, authority, false); ContentResolver.setSyncAutomatically(account, authority, false);
// immediately delete any linked contacts // immediately delete any linked contacts

View File

@@ -19,7 +19,9 @@
package org.sufficientlysecure.keychain.util; package org.sufficientlysecure.keychain.util;
import android.accounts.Account;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Parcel; import android.os.Parcel;
@@ -29,6 +31,7 @@ import android.support.annotation.NonNull;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Constants.Pref; import org.sufficientlysecure.keychain.Constants.Pref;
import org.sufficientlysecure.keychain.KeychainApplication;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService; import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService;
@@ -76,9 +79,8 @@ public class Preferences {
/** /**
* Makes android's preference framework write to our file instead of default. * Makes android's preference framework write to our file instead of default.
* This allows us to use the "persistent" attribute to simplify code, which automatically * This allows us to use the xml "persistent" attribute to simplify code, which automatically
* writes and reads preference values. * writes and reads preference values.
* @param manager
*/ */
public static void setPreferenceManagerFileAndMode(PreferenceManager manager) { public static void setPreferenceManagerFileAndMode(PreferenceManager manager) {
manager.setSharedPreferencesName(PREF_FILE_NAME); manager.setSharedPreferencesName(PREF_FILE_NAME);
@@ -302,6 +304,23 @@ public class Preferences {
} }
/**
* @return true if a periodic sync exists and is set to run automatically, false otherwise
*/
public static boolean getKeyserverSyncEnabled(Context context) {
Account account = KeychainApplication.createAccountIfNecessary(context);
if (account == null) {
// if the account could not be created for some reason, we can't have a sync
return false;
}
String authority = Constants.PROVIDER_AUTHORITY;
return ContentResolver.getSyncAutomatically(account, authority) &&
!ContentResolver.getPeriodicSyncs(account, authority).isEmpty();
}
public CacheTTLPrefs getPassphraseCacheTtl() { public CacheTTLPrefs getPassphraseCacheTtl() {
Set<String> pref = mSharedPreferences.getStringSet(Constants.Pref.PASSPHRASE_CACHE_TTLS, null); Set<String> pref = mSharedPreferences.getStringSet(Constants.Pref.PASSPHRASE_CACHE_TTLS, null);
if (pref == null) { if (pref == null) {