Disable contact sync by default on Android >= 6

This commit is contained in:
Dominik Schürmann
2015-11-23 11:43:44 +01:00
parent cd601988db
commit eda53d42de
3 changed files with 36 additions and 35 deletions

View File

@@ -17,10 +17,7 @@
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.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@@ -28,12 +25,11 @@ 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.provider.ContactsContract;
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;
import org.sufficientlysecure.keychain.provider.TemporaryFileProvider; import org.sufficientlysecure.keychain.provider.TemporaryFileProvider;
import org.sufficientlysecure.keychain.service.ContactSyncAdapterService;
import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService; import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService;
import org.sufficientlysecure.keychain.ui.ConsolidateDialogActivity; import org.sufficientlysecure.keychain.ui.ConsolidateDialogActivity;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils; import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
@@ -93,7 +89,9 @@ public class KeychainApplication extends Application {
brandGlowEffect(getApplicationContext(), brandGlowEffect(getApplicationContext(),
FormattingUtils.getColorFromAttr(getApplicationContext(), R.attr.colorPrimary)); 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 // Update keyserver list as needed
Preferences.getPreferences(this).upgradePreferences(this); 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) { static void brandGlowEffect(Context context, int brandColor) {
// no hack on Android 5 // no hack on Android 5
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

View File

@@ -18,6 +18,7 @@
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.Activity; import android.app.Activity;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@@ -25,13 +26,16 @@ import android.app.Service;
import android.content.AbstractThreadedSyncAdapter; import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient; import android.content.ContentProviderClient;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SyncResult; import android.content.SyncResult;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.provider.ContactsContract; import android.provider.ContactsContract;
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;
@@ -132,6 +136,11 @@ public class ContactSyncAdapterService extends Service {
// } // }
} }
@Override
public IBinder onBind(Intent intent) {
return new ContactSyncAdapter().getSyncAdapterBinder();
}
public static void requestContactsSync() { public static void requestContactsSync() {
Bundle extras = new Bundle(); Bundle extras = new Bundle();
// no need to wait, do it immediately // no need to wait, do it immediately
@@ -143,8 +152,27 @@ public class ContactSyncAdapterService extends Service {
extras); extras);
} }
@Override public static void initContactsSync(Context context) {
public IBinder onBind(Intent intent) { try {
return new ContactSyncAdapter().getSyncAdapterBinder(); 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();
}
} }
} }

View File

@@ -512,7 +512,7 @@ public class KeyserverSyncAdapterService extends Service {
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.e(Constants.TAG, "Adding account failed!"); Log.d(Constants.TAG, "account already exists, the account is null, or another error occured");
} }
} }
// for keyserver sync // for keyserver sync