diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index d533380f..a70ab7aa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -639,14 +639,23 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } } - fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact)) + fun deleteContact(contact: Contact) { + if (contact.source == SMT_PRIVATE) { + activity.dbHelper.deleteContact(contact.id) + } else { + deleteContacts(arrayListOf(contact)) + } + } fun deleteContacts(contacts: ArrayList) { + val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id.toString() }.toTypedArray() + activity.dbHelper.deleteContacts(localContacts) + try { val contactIDs = HashSet() val operations = ArrayList() val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" - contacts.forEach { + contacts.filter { it.source != SMT_PRIVATE }.forEach { ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply { val selectionArgs = arrayOf(it.id.toString()) withSelection(selection, selectionArgs) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt index 9343a4e0..59a8148b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -4,6 +4,7 @@ import android.content.ContentValues import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper +import android.text.TextUtils import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.extensions.getIntValue @@ -68,6 +69,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return mDb.update(CONTACTS_TABLE_NAME, contactValues, selection, selectionArgs) == 1 } + fun deleteContact(id: Int) = deleteContacts(arrayOf(id.toString())) + + fun deleteContacts(ids: Array) { + val args = TextUtils.join(", ", ids) + val selection = "$CONTACTS_TABLE_NAME.$COL_ID IN ($args)" + mDb.delete(CONTACTS_TABLE_NAME, selection, null) + } + private fun fillContactValues(contact: Contact): ContentValues { return ContentValues().apply { put(COL_FIRST_NAME, contact.firstName)