diff options
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure')
3 files changed, 36 insertions, 35 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java index 5d97dac8a..f7655f569 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java @@ -17,10 +17,7 @@ package org.sufficientlysecure.keychain; -import android.accounts.Account; -import android.accounts.AccountManager; import android.app.Application; -import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; @@ -28,12 +25,11 @@ import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Environment; -import android.provider.ContactsContract; -import android.widget.Toast; import org.spongycastle.jce.provider.BouncyCastleProvider; import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.provider.TemporaryFileProvider; +import org.sufficientlysecure.keychain.service.ContactSyncAdapterService; import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService; import org.sufficientlysecure.keychain.ui.ConsolidateDialogActivity; import org.sufficientlysecure.keychain.ui.util.FormattingUtils; @@ -93,7 +89,9 @@ public class KeychainApplication extends Application { brandGlowEffect(getApplicationContext(), FormattingUtils.getColorFromAttr(getApplicationContext(), R.attr.colorPrimary)); - setupAccountAsNeeded(this); + // Add OpenKeychain account to Android to link contacts with keys and keyserver sync + KeyserverSyncAdapterService.enableKeyserverSync(this); + ContactSyncAdapterService.initContactsSync(this); // Update keyserver list as needed Preferences.getPreferences(this).upgradePreferences(this); @@ -136,31 +134,6 @@ public class KeychainApplication extends Application { } } - /** - * Add OpenKeychain account to Android to link contacts with keys and keyserver sync - */ - public static void setupAccountAsNeeded(Context context) { - try { - AccountManager manager = AccountManager.get(context); - Account[] accounts = manager.getAccountsByType(Constants.ACCOUNT_TYPE); - - if (accounts.length == 0) { - Account account = new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE); - if (manager.addAccountExplicitly(account, null, null)) { - // for contact sync - ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1); - ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true); - KeyserverSyncAdapterService.enableKeyserverSync(context); - } else { - Log.e(Constants.TAG, "Adding account failed!"); - } - } - } catch (SecurityException e) { - Log.e(Constants.TAG, "SecurityException when adding the account", e); - Toast.makeText(context, R.string.reinstall_openkeychain, Toast.LENGTH_LONG).show(); - } - } - static void brandGlowEffect(Context context, int brandColor) { // no hack on Android 5 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java index 64f06fd15..a1c652245 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ContactSyncAdapterService.java @@ -18,6 +18,7 @@ package org.sufficientlysecure.keychain.service; import android.accounts.Account; +import android.accounts.AccountManager; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; @@ -25,13 +26,16 @@ import android.app.Service; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; import android.content.ContentResolver; +import android.content.Context; import android.content.Intent; import android.content.SyncResult; +import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.preference.PreferenceActivity; import android.provider.ContactsContract; import android.support.v4.app.NotificationCompat; +import android.widget.Toast; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; @@ -132,6 +136,11 @@ public class ContactSyncAdapterService extends Service { // } } + @Override + public IBinder onBind(Intent intent) { + return new ContactSyncAdapter().getSyncAdapterBinder(); + } + public static void requestContactsSync() { Bundle extras = new Bundle(); // no need to wait, do it immediately @@ -143,8 +152,27 @@ public class ContactSyncAdapterService extends Service { extras); } - @Override - public IBinder onBind(Intent intent) { - return new ContactSyncAdapter().getSyncAdapterBinder(); + public static void initContactsSync(Context context) { + try { + AccountManager manager = AccountManager.get(context); + 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"); + } + } + + ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1); + + // Enable by default for Android < 6, on Android >= 6 runtime permissions are required + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + 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(); + } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeyserverSyncAdapterService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeyserverSyncAdapterService.java index 122eb6cf4..fc3224e39 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeyserverSyncAdapterService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeyserverSyncAdapterService.java @@ -512,7 +512,7 @@ public class KeyserverSyncAdapterService extends Service { Account account = new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE); if (accounts.length == 0) { if (!manager.addAccountExplicitly(account, null, null)) { - Log.e(Constants.TAG, "Adding account failed!"); + Log.d(Constants.TAG, "account already exists, the account is null, or another error occured"); } } // for keyserver sync |