Merge pull request #1633 from open-keychain/preference-fix

Fixed Keyserver and Contact Sync turning itself on
This commit is contained in:
Dominik Schürmann
2015-12-20 23:31:04 +01:00
4 changed files with 57 additions and 43 deletions

View File

@@ -17,6 +17,8 @@
package org.sufficientlysecure.keychain; package org.sufficientlysecure.keychain;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -25,6 +27,7 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.widget.Toast;
import org.spongycastle.jce.provider.BouncyCastleProvider; import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.provider.KeychainDatabase;
@@ -90,8 +93,13 @@ public class KeychainApplication extends Application {
FormattingUtils.getColorFromAttr(getApplicationContext(), R.attr.colorPrimary)); FormattingUtils.getColorFromAttr(getApplicationContext(), R.attr.colorPrimary));
// 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
KeyserverSyncAdapterService.enableKeyserverSync(this); createAccountIfNecessary();
ContactSyncAdapterService.enableContactsSync(this);
// if first time, enable keyserver and contact sync
if (Preferences.getPreferences(this).isFirstTime()) {
KeyserverSyncAdapterService.enableKeyserverSync(this);
ContactSyncAdapterService.enableContactsSync(this);
}
// Update keyserver list as needed // Update keyserver list as needed
Preferences.getPreferences(this).upgradePreferences(this); Preferences.getPreferences(this).upgradePreferences(this);
@@ -108,6 +116,23 @@ public class KeychainApplication extends Application {
} }
} }
private void createAccountIfNecessary() {
try {
AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType(Constants.ACCOUNT_TYPE);
Account account = new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE);
if (accounts.length == 0) {
if (!manager.addAccountExplicitly(account, null, null)) {
Log.d(Constants.TAG, "account already exists, the account is null, or another error occured");
}
}
} catch (SecurityException e) {
Log.e(Constants.TAG, "SecurityException when adding the account", e);
Toast.makeText(this, R.string.reinstall_openkeychain, Toast.LENGTH_LONG).show();
}
}
public static HashMap<String,Bitmap> qrCodeCache = new HashMap<>(); public static HashMap<String,Bitmap> qrCodeCache = new HashMap<>();
@Override @Override

View File

@@ -155,22 +155,10 @@ public class ContactSyncAdapterService extends Service {
} }
public static void enableContactsSync(Context context) { public static void enableContactsSync(Context context) {
try { AccountManager manager = AccountManager.get(context);
AccountManager manager = AccountManager.get(context); Account account = manager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
Account[] accounts = manager.getAccountsByType(Constants.ACCOUNT_TYPE);
Account account = new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE); ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
if (accounts.length == 0) { ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
if (!manager.addAccountExplicitly(account, null, null)) {
Log.d(Constants.TAG, "account already exists, the account is null, or another error occured");
}
}
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
} catch (SecurityException e) {
Log.e(Constants.TAG, "SecurityException when adding the account", e);
Toast.makeText(context, R.string.reinstall_openkeychain, Toast.LENGTH_LONG).show();
}
} }
} }

View File

@@ -26,7 +26,6 @@ import android.os.Messenger;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
@@ -55,7 +54,7 @@ public class KeyserverSyncAdapterService extends Service {
// how often a sync should be initiated, in s // how often a sync should be initiated, in s
public static final long SYNC_INTERVAL = public static final long SYNC_INTERVAL =
Constants.DEBUG_KEYSERVER_SYNC Constants.DEBUG_KEYSERVER_SYNC
? TimeUnit.MINUTES.toSeconds(2) : TimeUnit.DAYS.toSeconds(3); ? TimeUnit.MINUTES.toSeconds(1) : TimeUnit.DAYS.toSeconds(3);
// time since last update after which a key should be updated again, in s // time since last update after which a key should be updated again, in s
public static final long KEY_UPDATE_LIMIT = public static final long KEY_UPDATE_LIMIT =
Constants.DEBUG_KEYSERVER_SYNC ? 1 : TimeUnit.DAYS.toSeconds(7); Constants.DEBUG_KEYSERVER_SYNC ? 1 : TimeUnit.DAYS.toSeconds(7);
@@ -82,6 +81,12 @@ public class KeyserverSyncAdapterService extends Service {
// introduced due to https://github.com/open-keychain/open-keychain/issues/1573 // introduced due to https://github.com/open-keychain/open-keychain/issues/1573
return START_NOT_STICKY; // we can't act on this Intent and don't want it redelivered return START_NOT_STICKY; // we can't act on this Intent and don't want it redelivered
} }
if (!isSyncEnabled()) {
// if we have initiated a sync, but the user disabled it in preferences since
return START_NOT_STICKY;
}
switch (intent.getAction()) { switch (intent.getAction()) {
case ACTION_CANCEL: { case ACTION_CANCEL: {
mCancelled.set(true); mCancelled.set(true);
@@ -505,30 +510,24 @@ public class KeyserverSyncAdapterService extends Service {
} }
public static void enableKeyserverSync(Context context) { public static void enableKeyserverSync(Context context) {
try { AccountManager manager = AccountManager.get(context);
AccountManager manager = AccountManager.get(context); Account account = manager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
Account[] accounts = manager.getAccountsByType(Constants.ACCOUNT_TYPE);
Account account = new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE); ContentResolver.setIsSyncable(account, Constants.PROVIDER_AUTHORITY, 1);
if (accounts.length == 0) { ContentResolver.setSyncAutomatically(account, Constants.PROVIDER_AUTHORITY, true);
if (!manager.addAccountExplicitly(account, null, null)) { ContentResolver.addPeriodicSync(
Log.d(Constants.TAG, "account already exists, the account is null, or another error occured"); account,
} Constants.PROVIDER_AUTHORITY,
} new Bundle(),
// for keyserver sync SYNC_INTERVAL
ContentResolver.setIsSyncable(account, Constants.PROVIDER_AUTHORITY, 1); );
ContentResolver.setSyncAutomatically(account, Constants.PROVIDER_AUTHORITY, }
true);
ContentResolver.addPeriodicSync( private boolean isSyncEnabled() {
account, AccountManager manager = AccountManager.get(this);
Constants.PROVIDER_AUTHORITY, Account account = manager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
new Bundle(),
SYNC_INTERVAL return ContentResolver.getSyncAutomatically(account, Constants.PROVIDER_AUTHORITY);
);
} catch (SecurityException e) {
Log.e(Constants.TAG, "SecurityException when adding the account", e);
Toast.makeText(context, R.string.reinstall_openkeychain, Toast.LENGTH_LONG).show();
}
} }
private void startServiceWithUpdateAll() { private void startServiceWithUpdateAll() {

View File

@@ -476,6 +476,8 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
private boolean checkContactsPermission(String authority) { private boolean checkContactsPermission(String authority) {
if (!ContactsContract.AUTHORITY.equals(authority)) { if (!ContactsContract.AUTHORITY.equals(authority)) {
// provides convenience of not using separate checks for keyserver and contact sync
// in initializeSyncCheckBox
return true; return true;
} }