handle local contact deleting

This commit is contained in:
tibbi 2018-11-06 09:53:49 +01:00
parent 9634199605
commit 17e10c7016
7 changed files with 55 additions and 46 deletions

View File

@ -25,6 +25,7 @@ import kotlinx.android.synthetic.main.item_website.view.*
class ViewContactActivity : ContactActivity() { class ViewContactActivity : ContactActivity() {
private var isViewIntent = false private var isViewIntent = false
private var wasEditLaunched = false
private var showFields = 0 private var showFields = 0
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -64,7 +65,7 @@ class ViewContactActivity : ContactActivity() {
} }
when (item.itemId) { when (item.itemId) {
R.id.edit -> editContact(contact!!) R.id.edit -> editContact()
R.id.share -> shareContact() R.id.share -> shareContact()
R.id.open_with -> openWith() R.id.open_with -> openWith()
R.id.delete -> deleteContact() R.id.delete -> deleteContact()
@ -101,7 +102,9 @@ class ViewContactActivity : ContactActivity() {
Thread { Thread {
contact = ContactsHelper(this).getContactWithId(contactId, intent.getBooleanExtra(IS_PRIVATE, false)) contact = ContactsHelper(this).getContactWithId(contactId, intent.getBooleanExtra(IS_PRIVATE, false))
if (contact == null) { if (contact == null) {
toast(R.string.unknown_error_occurred) if (!wasEditLaunched) {
toast(R.string.unknown_error_occurred)
}
finish() finish()
} else { } else {
runOnUiThread { runOnUiThread {
@ -170,6 +173,11 @@ class ViewContactActivity : ContactActivity() {
setupContactSource() setupContactSource()
} }
private fun editContact() {
wasEditLaunched = true
editContact(contact!!)
}
private fun openWith() { private fun openWith() {
Intent().apply { Intent().apply {
action = ContactsContract.QuickContact.ACTION_QUICK_CONTACT action = ContactsContract.QuickContact.ACTION_QUICK_CONTACT

View File

@ -158,7 +158,10 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
val positions = getSelectedItemPositions() val positions = getSelectedItemPositions()
contactItems.removeAll(contactsToRemove) contactItems.removeAll(contactsToRemove)
ContactsHelper(activity).deleteContacts(contactsToRemove) Thread {
ContactsHelper(activity).deleteContacts(contactsToRemove)
}.start()
if (contactItems.isEmpty()) { if (contactItems.isEmpty()) {
refreshListener?.refreshContacts(ALL_TABS_MASK) refreshListener?.refreshContacts(ALL_TABS_MASK)
finishActMode() finishActMode()

View File

@ -56,7 +56,7 @@ abstract class ContactsDatabase : RoomDatabase() {
emptyContact.id = FIRST_CONTACT_ID emptyContact.id = FIRST_CONTACT_ID
db!!.ContactsDao().apply { db!!.ContactsDao().apply {
insertOrUpdate(emptyContact) insertOrUpdate(emptyContact)
deleteContactIds(FIRST_CONTACT_ID.toString()) deleteContactId(FIRST_CONTACT_ID)
} }
} }
} }

View File

@ -1490,41 +1490,41 @@ class ContactsHelper(val activity: Activity) {
} }
fun deleteContact(contact: Contact) { fun deleteContact(contact: Contact) {
if (contact.source == SMT_PRIVATE) { Thread {
activity.dbHelper.deleteContact(contact.id) if (contact.source == SMT_PRIVATE) {
} else { activity.contactsDB.deleteContactId(contact.id)
deleteContacts(arrayListOf(contact)) } else {
} deleteContacts(arrayListOf(contact))
}
}.start()
} }
fun deleteContacts(contacts: ArrayList<Contact>) { fun deleteContacts(contacts: ArrayList<Contact>) {
Thread { val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id }.toTypedArray()
val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id.toString() }.toTypedArray() LocalContactsHelper(activity).deleteContactIds(localContacts)
activity.dbHelper.deleteContacts(localContacts)
try { try {
val operations = ArrayList<ContentProviderOperation>() val operations = ArrayList<ContentProviderOperation>()
val selection = "${ContactsContract.RawContacts._ID} = ?" val selection = "${ContactsContract.RawContacts._ID} = ?"
contacts.filter { it.source != SMT_PRIVATE }.forEach { contacts.filter { it.source != SMT_PRIVATE }.forEach {
ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).apply { ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).apply {
val selectionArgs = arrayOf(it.id.toString()) val selectionArgs = arrayOf(it.id.toString())
withSelection(selection, selectionArgs) withSelection(selection, selectionArgs)
operations.add(build()) operations.add(build())
}
if (operations.size % BATCH_SIZE == 0) {
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
operations.clear()
}
} }
if (activity.hasPermission(PERMISSION_WRITE_CONTACTS)) { if (operations.size % BATCH_SIZE == 0) {
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
operations.clear()
} }
} catch (e: Exception) {
activity.showErrorToast(e)
} }
}.start()
if (activity.hasPermission(PERMISSION_WRITE_CONTACTS)) {
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
}
} catch (e: Exception) {
activity.showErrorToast(e)
}
} }
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")

View File

@ -81,18 +81,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
db.execSQL("REPLACE INTO sqlite_sequence (name, seq) VALUES ('$GROUPS_TABLE_NAME', $FIRST_GROUP_ID)") db.execSQL("REPLACE INTO sqlite_sequence (name, seq) VALUES ('$GROUPS_TABLE_NAME', $FIRST_GROUP_ID)")
} }
fun deleteContact(id: Int) = deleteContacts(arrayOf(id.toString()))
fun deleteContacts(ids: Array<String>) {
if (ids.isEmpty()) {
return
}
val args = TextUtils.join(", ", ids)
val selection = "$CONTACTS_TABLE_NAME.$COL_ID IN ($args)"
mDb.delete(CONTACTS_TABLE_NAME, selection, null)
}
fun insertGroup(group: Group): Group? { fun insertGroup(group: Group): Group? {
val contactValues = fillGroupValues(group) val contactValues = fillGroupValues(group)
val id = mDb.insert(GROUPS_TABLE_NAME, null, contactValues) val id = mDb.insert(GROUPS_TABLE_NAME, null, contactValues)

View File

@ -21,6 +21,12 @@ class LocalContactsHelper(val activity: Activity) {
return activity.contactsDB.insertOrUpdate(localContact) > 0 return activity.contactsDB.insertOrUpdate(localContact) > 0
} }
fun deleteContactIds(ids: Array<Int>) {
ids.forEach {
activity.contactsDB.deleteContactId(it)
}
}
fun toggleFavorites(ids: Array<Int>, addToFavorites: Boolean) { fun toggleFavorites(ids: Array<Int>, addToFavorites: Boolean) {
val isStarred = if (addToFavorites) 1 else 0 val isStarred = if (addToFavorites) 1 else 0
ids.forEach { ids.forEach {
@ -43,7 +49,11 @@ class LocalContactsHelper(val activity: Activity) {
return scaledSizePhotoData return scaledSizePhotoData
} }
private fun convertLocalContactToContact(localContact: LocalContact): Contact { private fun convertLocalContactToContact(localContact: LocalContact?): Contact? {
if (localContact == null) {
return null
}
val filterDuplicates = activity.config.filterDuplicates val filterDuplicates = activity.config.filterDuplicates
val filteredPhoneNumbers = ArrayList<PhoneNumber>() val filteredPhoneNumbers = ArrayList<PhoneNumber>()
if (filterDuplicates) { if (filterDuplicates) {

View File

@ -12,7 +12,7 @@ interface ContactsDao {
fun getContacts(): List<LocalContact> fun getContacts(): List<LocalContact>
@Query("SELECT * FROM contacts WHERE id = :id") @Query("SELECT * FROM contacts WHERE id = :id")
fun getContactWithId(id: Int): LocalContact fun getContactWithId(id: Int): LocalContact?
@Query("UPDATE contacts SET starred = :isStarred WHERE id = :id") @Query("UPDATE contacts SET starred = :isStarred WHERE id = :id")
fun updateStarred(isStarred: Int, id: Int) fun updateStarred(isStarred: Int, id: Int)
@ -20,6 +20,6 @@ interface ContactsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(contact: LocalContact): Long fun insertOrUpdate(contact: LocalContact): Long
@Query("DELETE FROM contacts WHERE id IN (:ids)") @Query("DELETE FROM contacts WHERE id = :id")
fun deleteContactIds(ids: String) fun deleteContactId(id: Int)
} }