Merge pull request #1713 from open-keychain/unlink-contact

Deletes linked OpenKeychain contacts if sync is disabled
This commit is contained in:
Dominik Schürmann
2016-02-06 14:17:49 +01:00
5 changed files with 36 additions and 8 deletions

View File

@@ -118,8 +118,8 @@ public class KeychainApplication extends Application {
} }
/** /**
* @return the OpenKeychain contact/sync account if it exists or was successfully created, null * @return the OpenKeychain contact/keyserver sync account if it exists or was successfully
* otherwise * created, null otherwise
*/ */
public static @Nullable Account createAccountIfNecessary(Context context) { public static @Nullable Account createAccountIfNecessary(Context context) {
try { try {

View File

@@ -18,7 +18,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.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.AbstractThreadedSyncAdapter; import android.content.AbstractThreadedSyncAdapter;
@@ -35,6 +34,7 @@ import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationManagerCompat;
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.ui.SettingsActivity; import org.sufficientlysecure.keychain.ui.SettingsActivity;
import org.sufficientlysecure.keychain.util.ContactHelper; import org.sufficientlysecure.keychain.util.ContactHelper;
@@ -151,10 +151,25 @@ public class ContactSyncAdapterService extends Service {
} }
public static void enableContactsSync(Context context) { public static void enableContactsSync(Context context) {
AccountManager manager = AccountManager.get(context); Account account = KeychainApplication.createAccountIfNecessary(context);
Account account = manager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
if (account == null) {
// nothing we can do
return;
}
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1); ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true); ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
} }
public static void deleteIfSyncDisabled(Context context) {
Account account = KeychainApplication.createAccountIfNecessary(context);
if (account == null) {
return;
}
// if user has disabled automatic sync, delete linked OpenKeychain contacts
if(!ContentResolver.getSyncAutomatically(account, ContactsContract.AUTHORITY)) {
new ContactHelper(context).deleteAllContacts();
}
}
} }

View File

@@ -51,6 +51,8 @@ import android.widget.LinearLayout;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity; import org.sufficientlysecure.keychain.compatibility.AppCompatPreferenceActivity;
import org.sufficientlysecure.keychain.service.ContactSyncAdapterService;
import org.sufficientlysecure.keychain.ui.base.BaseActivity;
import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.util.ThemeChanger; import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
@@ -79,6 +81,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
BaseActivity.onResumeChecks(this);
if (mThemeChanger.changeTheme()) { if (mThemeChanger.changeTheme()) {
Intent intent = getIntent(); Intent intent = getIntent();
@@ -463,6 +466,8 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
} else { } else {
// disable syncs // disable syncs
ContentResolver.setSyncAutomatically(account, authority, false); ContentResolver.setSyncAutomatically(account, authority, false);
// immediately delete any linked contacts
ContactSyncAdapterService.deleteIfSyncDisabled(getActivity());
// cancel any ongoing/pending syncs // cancel any ongoing/pending syncs
ContentResolver.cancelSync(account, authority); ContentResolver.cancelSync(account, authority);
setSummary(syncCheckBox, authority, false); setSummary(syncCheckBox, authority, false);

View File

@@ -18,6 +18,7 @@
package org.sufficientlysecure.keychain.ui.base; package org.sufficientlysecure.keychain.ui.base;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
@@ -30,6 +31,7 @@ import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.service.ContactSyncAdapterService;
import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService; import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService;
import org.sufficientlysecure.keychain.ui.util.ThemeChanger; import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
@@ -52,7 +54,7 @@ public abstract class BaseActivity extends AppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
KeyserverSyncAdapterService.cancelUpdates(this); onResumeChecks(this);
if (mThemeChanger.changeTheme()) { if (mThemeChanger.changeTheme()) {
Intent intent = getIntent(); Intent intent = getIntent();
@@ -63,6 +65,12 @@ public abstract class BaseActivity extends AppCompatActivity {
} }
} }
public static void onResumeChecks(Context context) {
KeyserverSyncAdapterService.cancelUpdates(context);
// in case user has disabled sync from settings
ContactSyncAdapterService.deleteIfSyncDisabled(context);
}
protected void initLayout() { protected void initLayout() {
} }

View File

@@ -465,7 +465,7 @@ public class ContactHelper {
*/ */
public void writeKeysToContacts() { public void writeKeysToContacts() {
if (Constants.DEBUG_SYNC_REMOVE_CONTACTS) { if (Constants.DEBUG_SYNC_REMOVE_CONTACTS) {
debugDeleteRawContacts(); deleteAllContacts();
} }
writeKeysToMainProfileContact(); writeKeysToMainProfileContact();
@@ -671,7 +671,7 @@ public class ContactHelper {
* *
* @return number of rows deleted * @return number of rows deleted
*/ */
private int debugDeleteRawContacts() { public int deleteAllContacts() {
// CALLER_IS_SYNCADAPTER allows us to actually wipe the RawContact from the device, otherwise // CALLER_IS_SYNCADAPTER allows us to actually wipe the RawContact from the device, otherwise
// would be just flagged for deletion // would be just flagged for deletion
Uri deleteUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon(). Uri deleteUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().