Fix SecurityException on Android 6 due to remove contacts on disabled sync

This commit is contained in:
Dominik Schürmann
2016-02-09 21:14:07 +01:00
parent b44f73c819
commit c22e2baa79
3 changed files with 12 additions and 5 deletions

View File

@@ -129,7 +129,7 @@ public class KeychainApplication extends Application {
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, "error when adding account via addAccountExplicitly");
return null; return null;
} else { } else {
return account; return account;

View File

@@ -17,6 +17,7 @@
package org.sufficientlysecure.keychain.service; package org.sufficientlysecure.keychain.service;
import android.Manifest;
import android.accounts.Account; import android.accounts.Account;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
@@ -26,12 +27,14 @@ import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SyncResult; import android.content.SyncResult;
import android.content.pm.PackageManager;
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.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.ContextCompat;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.KeychainApplication; import org.sufficientlysecure.keychain.KeychainApplication;
@@ -152,9 +155,7 @@ public class ContactSyncAdapterService extends Service {
public static void enableContactsSync(Context context) { public static void enableContactsSync(Context context) {
Account account = KeychainApplication.createAccountIfNecessary(context); Account account = KeychainApplication.createAccountIfNecessary(context);
if (account == null) { if (account == null) {
// nothing we can do
return; return;
} }
@@ -163,12 +164,18 @@ public class ContactSyncAdapterService extends Service {
} }
public static void deleteIfSyncDisabled(Context context) { public static void deleteIfSyncDisabled(Context context) {
if (!(ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED)) {
return;
}
Account account = KeychainApplication.createAccountIfNecessary(context); Account account = KeychainApplication.createAccountIfNecessary(context);
if (account == null) { if (account == null) {
return; return;
} }
// if user has disabled automatic sync, delete linked OpenKeychain contacts // if user has disabled automatic sync, delete linked OpenKeychain contacts
if(!ContentResolver.getSyncAutomatically(account, ContactsContract.AUTHORITY)) { if (!ContentResolver.getSyncAutomatically(account, ContactsContract.AUTHORITY)) {
new ContactHelper(context).deleteAllContacts(); new ContactHelper(context).deleteAllContacts();
} }
} }

View File

@@ -67,7 +67,7 @@ public abstract class BaseActivity extends AppCompatActivity {
public static void onResumeChecks(Context context) { public static void onResumeChecks(Context context) {
KeyserverSyncAdapterService.cancelUpdates(context); KeyserverSyncAdapterService.cancelUpdates(context);
// in case user has disabled sync from settings // in case user has disabled sync from Android account settings
ContactSyncAdapterService.deleteIfSyncDisabled(context); ContactSyncAdapterService.deleteIfSyncDisabled(context);
} }