Merge pull request #1713 from open-keychain/unlink-contact
Deletes linked OpenKeychain contacts if sync is disabled
This commit is contained in:
@@ -118,8 +118,8 @@ public class KeychainApplication extends Application {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the OpenKeychain contact/sync account if it exists or was successfully created, null
|
||||
* otherwise
|
||||
* @return the OpenKeychain contact/keyserver sync account if it exists or was successfully
|
||||
* created, null otherwise
|
||||
*/
|
||||
public static @Nullable Account createAccountIfNecessary(Context context) {
|
||||
try {
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
package org.sufficientlysecure.keychain.service;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.AbstractThreadedSyncAdapter;
|
||||
@@ -35,6 +34,7 @@ import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v4.app.NotificationManagerCompat;
|
||||
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.KeychainApplication;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.ui.SettingsActivity;
|
||||
import org.sufficientlysecure.keychain.util.ContactHelper;
|
||||
@@ -151,10 +151,25 @@ public class ContactSyncAdapterService extends Service {
|
||||
}
|
||||
|
||||
public static void enableContactsSync(Context context) {
|
||||
AccountManager manager = AccountManager.get(context);
|
||||
Account account = manager.getAccountsByType(Constants.ACCOUNT_TYPE)[0];
|
||||
Account account = KeychainApplication.createAccountIfNecessary(context);
|
||||
|
||||
if (account == null) {
|
||||
// nothing we can do
|
||||
return;
|
||||
}
|
||||
|
||||
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,8 @@ import android.widget.LinearLayout;
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
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.ThemeChanger;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
@@ -79,6 +81,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
BaseActivity.onResumeChecks(this);
|
||||
|
||||
if (mThemeChanger.changeTheme()) {
|
||||
Intent intent = getIntent();
|
||||
@@ -463,6 +466,8 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
|
||||
} else {
|
||||
// disable syncs
|
||||
ContentResolver.setSyncAutomatically(account, authority, false);
|
||||
// immediately delete any linked contacts
|
||||
ContactSyncAdapterService.deleteIfSyncDisabled(getActivity());
|
||||
// cancel any ongoing/pending syncs
|
||||
ContentResolver.cancelSync(account, authority);
|
||||
setSummary(syncCheckBox, authority, false);
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package org.sufficientlysecure.keychain.ui.base;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBar;
|
||||
@@ -30,6 +31,7 @@ import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.service.ContactSyncAdapterService;
|
||||
import org.sufficientlysecure.keychain.service.KeyserverSyncAdapterService;
|
||||
import org.sufficientlysecure.keychain.ui.util.ThemeChanger;
|
||||
|
||||
@@ -52,7 +54,7 @@ public abstract class BaseActivity extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
KeyserverSyncAdapterService.cancelUpdates(this);
|
||||
onResumeChecks(this);
|
||||
|
||||
if (mThemeChanger.changeTheme()) {
|
||||
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() {
|
||||
|
||||
}
|
||||
|
||||
@@ -465,7 +465,7 @@ public class ContactHelper {
|
||||
*/
|
||||
public void writeKeysToContacts() {
|
||||
if (Constants.DEBUG_SYNC_REMOVE_CONTACTS) {
|
||||
debugDeleteRawContacts();
|
||||
deleteAllContacts();
|
||||
}
|
||||
|
||||
writeKeysToMainProfileContact();
|
||||
@@ -671,7 +671,7 @@ public class ContactHelper {
|
||||
*
|
||||
* @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
|
||||
// would be just flagged for deletion
|
||||
Uri deleteUri = ContactsContract.RawContacts.CONTENT_URI.buildUpon().
|
||||
|
||||
Reference in New Issue
Block a user