prevent rare KeyserverSyncAdapterService crash
This commit is contained in:
parent
025d2c5d29
commit
160362d2bf
|
@ -27,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.support.annotation.Nullable;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.spongycastle.jce.provider.BouncyCastleProvider;
|
import org.spongycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
@ -93,7 +94,7 @@ 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
|
||||||
createAccountIfNecessary();
|
createAccountIfNecessary(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()) {
|
||||||
|
@ -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 {
|
try {
|
||||||
AccountManager manager = AccountManager.get(this);
|
AccountManager manager = AccountManager.get(context);
|
||||||
Account[] accounts = manager.getAccountsByType(Constants.ACCOUNT_TYPE);
|
Account[] accounts = manager.getAccountsByType(Constants.ACCOUNT_TYPE);
|
||||||
|
|
||||||
Account account = new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE);
|
Account account = new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE);
|
||||||
if (accounts.length == 0) {
|
if (accounts.length == 0) {
|
||||||
if (!manager.addAccountExplicitly(account, null, null)) {
|
if (!manager.addAccountExplicitly(account, null, null)) {
|
||||||
Log.d(Constants.TAG, "account already exists, the account is null, or another error occured");
|
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) {
|
} catch (SecurityException e) {
|
||||||
Log.e(Constants.TAG, "SecurityException when adding the account", 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package org.sufficientlysecure.keychain.service;
|
package org.sufficientlysecure.keychain.service;
|
||||||
|
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
|
||||||
import android.app.AlarmManager;
|
import android.app.AlarmManager;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
|
@ -28,6 +27,7 @@ import android.os.SystemClock;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
|
|
||||||
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.keyimport.ParcelableKeyRing;
|
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
|
||||||
import org.sufficientlysecure.keychain.operations.ImportOperation;
|
import org.sufficientlysecure.keychain.operations.ImportOperation;
|
||||||
|
@ -510,8 +510,12 @@ public class KeyserverSyncAdapterService extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void enableKeyserverSync(Context context) {
|
public static void enableKeyserverSync(Context context) {
|
||||||
AccountManager manager = AccountManager.get(context);
|
Account account = KeychainApplication.createAccountIfNecessary(context);
|
||||||
Account account = manager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
|
|
||||||
|
if (account == null) {
|
||||||
|
// account failed to be created for some reason, nothing we can do here
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
@ -524,10 +528,11 @@ public class KeyserverSyncAdapterService extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isSyncEnabled() {
|
private boolean isSyncEnabled() {
|
||||||
AccountManager manager = AccountManager.get(this);
|
Account account = KeychainApplication.createAccountIfNecessary(this);
|
||||||
Account account = manager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
|
|
||||||
|
|
||||||
return ContentResolver.getSyncAutomatically(account, Constants.PROVIDER_AUTHORITY);
|
// if account is null, it could not be created for some reason, so sync cannot exist
|
||||||
|
return account != null
|
||||||
|
&& ContentResolver.getSyncAutomatically(account, Constants.PROVIDER_AUTHORITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startServiceWithUpdateAll() {
|
private void startServiceWithUpdateAll() {
|
||||||
|
|
Loading…
Reference in a new issue