aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
diff options
context:
space:
mode:
authorAdithya Abraham Philip <adithyaphilip@gmail.com>2016-01-27 23:54:38 +0530
committerAdithya Abraham Philip <adithyaphilip@gmail.com>2016-01-27 23:58:55 +0530
commit160362d2bfa6254748b84027db958005a16c92f2 (patch)
tree70895ede073df6264a8c6de3de770e4188a93b2a /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
parent025d2c5d29d8d9730e5ef5a3b01b425ba6c99957 (diff)
downloadopen-keychain-160362d2bfa6254748b84027db958005a16c92f2.tar.gz
open-keychain-160362d2bfa6254748b84027db958005a16c92f2.tar.bz2
open-keychain-160362d2bfa6254748b84027db958005a16c92f2.zip
prevent rare KeyserverSyncAdapterService crash
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java19
1 files changed, 15 insertions, 4 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
index 57fce633e..381af0af8 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/KeychainApplication.java
@@ -27,6 +27,7 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Environment;
+import android.support.annotation.Nullable;
import android.widget.Toast;
import org.spongycastle.jce.provider.BouncyCastleProvider;
@@ -93,7 +94,7 @@ public class KeychainApplication extends Application {
FormattingUtils.getColorFromAttr(getApplicationContext(), R.attr.colorPrimary));
// Add OpenKeychain account to Android to link contacts with keys and keyserver sync
- createAccountIfNecessary();
+ createAccountIfNecessary(this);
// if first time, enable keyserver and contact sync
if (Preferences.getPreferences(this).isFirstTime()) {
@@ -116,20 +117,30 @@ public class KeychainApplication extends Application {
}
}
- private void createAccountIfNecessary() {
+ /**
+ * @return the OpenKeychain contact/sync account if it exists or was successfully created, null
+ * otherwise
+ */
+ public static @Nullable Account createAccountIfNecessary(Context context) {
try {
- AccountManager manager = AccountManager.get(this);
+ 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");
+ return null;
+ } else {
+ return account;
}
+ } else {
+ return accounts[0];
}
} catch (SecurityException e) {
Log.e(Constants.TAG, "SecurityException when adding the account", e);
- Toast.makeText(this, R.string.reinstall_openkeychain, Toast.LENGTH_LONG).show();
+ Toast.makeText(context, R.string.reinstall_openkeychain, Toast.LENGTH_LONG).show();
+ return null;
}
}