aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
diff options
context:
space:
mode:
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java29
1 files changed, 27 insertions, 2 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
index 3390fb729..57fce633e 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
@@ -17,6 +17,8 @@
package org.sufficientlysecure.keychain;
+import android.accounts.Account;
+import android.accounts.AccountManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
@@ -25,6 +27,7 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Environment;
+import android.widget.Toast;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.sufficientlysecure.keychain.provider.KeychainDatabase;
@@ -90,8 +93,13 @@ public class KeychainApplication extends Application {
FormattingUtils.getColorFromAttr(getApplicationContext(), R.attr.colorPrimary));
// Add OpenKeychain account to Android to link contacts with keys and keyserver sync
- KeyserverSyncAdapterService.enableKeyserverSync(this);
- ContactSyncAdapterService.enableContactsSync(this);
+ createAccountIfNecessary();
+
+ // if first time, enable keyserver and contact sync
+ if (Preferences.getPreferences(this).isFirstTime()) {
+ KeyserverSyncAdapterService.enableKeyserverSync(this);
+ ContactSyncAdapterService.enableContactsSync(this);
+ }
// Update keyserver list as needed
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<>();
@Override