delete all account-links when disabling phonebooksync

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2021-03-26 12:14:17 +01:00
parent adc0a91dac
commit 17f3dae165
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
2 changed files with 31 additions and 2 deletions

View File

@ -869,11 +869,13 @@ public class SettingsController extends BaseController {
} }
@Override @Override
public void onChanged(Boolean newValue) { public void onChanged(Boolean isEnabled) {
if (newValue) { if (isEnabled) {
if(ContactAddressBookWorker.Companion.checkPermission(controller, context)){ if(ContactAddressBookWorker.Companion.checkPermission(controller, context)){
checkForPhoneNumber(); checkForPhoneNumber();
} }
} else {
ContactAddressBookWorker.Companion.deleteAll();
} }
} }
} }

View File

@ -83,6 +83,13 @@ class ContactAddressBookWorker(val context: Context, workerParameters: WorkerPar
Log.e(javaClass.simpleName, "No current user!") Log.e(javaClass.simpleName, "No current user!")
return Result.failure() return Result.failure()
} }
val deleteAll = inputData.getBoolean(DELETE_ALL, false)
if(deleteAll){
deleteAllLinkedAccounts()
return Result.success()
}
// Check if run already at the date // Check if run already at the date
val force = inputData.getBoolean(KEY_FORCE, false) val force = inputData.getBoolean(KEY_FORCE, false)
if (!force) { if (!force) {
@ -373,10 +380,22 @@ class ContactAddressBookWorker(val context: Context, workerParameters: WorkerPar
return numbers return numbers
} }
fun deleteAllLinkedAccounts() {
val rawContactUri = ContactsContract.RawContacts.CONTENT_URI
.buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, accountName)
.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType)
.build()
context.contentResolver.delete(rawContactUri, null, null)
Log.d(TAG, "deleted all linked accounts")
}
companion object { companion object {
const val TAG = "ContactAddressBook" const val TAG = "ContactAddressBook"
const val REQUEST_PERMISSION = 231 const val REQUEST_PERMISSION = 231
const val KEY_FORCE = "KEY_FORCE" const val KEY_FORCE = "KEY_FORCE"
const val DELETE_ALL = "DELETE_ALL"
fun run(context: Context) { fun run(context: Context) {
if (ContextCompat.checkSelfPermission(context, if (ContextCompat.checkSelfPermission(context,
@ -408,5 +427,13 @@ class ContactAddressBookWorker(val context: Context, workerParameters: WorkerPar
return true return true
} }
} }
fun deleteAll() {
WorkManager
.getInstance()
.enqueue(OneTimeWorkRequest.Builder(ContactAddressBookWorker::class.java)
.setInputData(Data.Builder().putBoolean(DELETE_ALL, true).build())
.build())
}
} }
} }