handle local contact deleting
This commit is contained in:
parent
9634199605
commit
17e10c7016
7 changed files with 55 additions and 46 deletions
|
@ -25,6 +25,7 @@ import kotlinx.android.synthetic.main.item_website.view.*
|
|||
|
||||
class ViewContactActivity : ContactActivity() {
|
||||
private var isViewIntent = false
|
||||
private var wasEditLaunched = false
|
||||
private var showFields = 0
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
@ -64,7 +65,7 @@ class ViewContactActivity : ContactActivity() {
|
|||
}
|
||||
|
||||
when (item.itemId) {
|
||||
R.id.edit -> editContact(contact!!)
|
||||
R.id.edit -> editContact()
|
||||
R.id.share -> shareContact()
|
||||
R.id.open_with -> openWith()
|
||||
R.id.delete -> deleteContact()
|
||||
|
@ -101,7 +102,9 @@ class ViewContactActivity : ContactActivity() {
|
|||
Thread {
|
||||
contact = ContactsHelper(this).getContactWithId(contactId, intent.getBooleanExtra(IS_PRIVATE, false))
|
||||
if (contact == null) {
|
||||
toast(R.string.unknown_error_occurred)
|
||||
if (!wasEditLaunched) {
|
||||
toast(R.string.unknown_error_occurred)
|
||||
}
|
||||
finish()
|
||||
} else {
|
||||
runOnUiThread {
|
||||
|
@ -170,6 +173,11 @@ class ViewContactActivity : ContactActivity() {
|
|||
setupContactSource()
|
||||
}
|
||||
|
||||
private fun editContact() {
|
||||
wasEditLaunched = true
|
||||
editContact(contact!!)
|
||||
}
|
||||
|
||||
private fun openWith() {
|
||||
Intent().apply {
|
||||
action = ContactsContract.QuickContact.ACTION_QUICK_CONTACT
|
||||
|
|
|
@ -158,7 +158,10 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
|
|||
val positions = getSelectedItemPositions()
|
||||
contactItems.removeAll(contactsToRemove)
|
||||
|
||||
ContactsHelper(activity).deleteContacts(contactsToRemove)
|
||||
Thread {
|
||||
ContactsHelper(activity).deleteContacts(contactsToRemove)
|
||||
}.start()
|
||||
|
||||
if (contactItems.isEmpty()) {
|
||||
refreshListener?.refreshContacts(ALL_TABS_MASK)
|
||||
finishActMode()
|
||||
|
|
|
@ -56,7 +56,7 @@ abstract class ContactsDatabase : RoomDatabase() {
|
|||
emptyContact.id = FIRST_CONTACT_ID
|
||||
db!!.ContactsDao().apply {
|
||||
insertOrUpdate(emptyContact)
|
||||
deleteContactIds(FIRST_CONTACT_ID.toString())
|
||||
deleteContactId(FIRST_CONTACT_ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1490,41 +1490,41 @@ class ContactsHelper(val activity: Activity) {
|
|||
}
|
||||
|
||||
fun deleteContact(contact: Contact) {
|
||||
if (contact.source == SMT_PRIVATE) {
|
||||
activity.dbHelper.deleteContact(contact.id)
|
||||
} else {
|
||||
deleteContacts(arrayListOf(contact))
|
||||
}
|
||||
Thread {
|
||||
if (contact.source == SMT_PRIVATE) {
|
||||
activity.contactsDB.deleteContactId(contact.id)
|
||||
} else {
|
||||
deleteContacts(arrayListOf(contact))
|
||||
}
|
||||
}.start()
|
||||
}
|
||||
|
||||
fun deleteContacts(contacts: ArrayList<Contact>) {
|
||||
Thread {
|
||||
val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id.toString() }.toTypedArray()
|
||||
activity.dbHelper.deleteContacts(localContacts)
|
||||
val localContacts = contacts.filter { it.source == SMT_PRIVATE }.map { it.id }.toTypedArray()
|
||||
LocalContactsHelper(activity).deleteContactIds(localContacts)
|
||||
|
||||
try {
|
||||
val operations = ArrayList<ContentProviderOperation>()
|
||||
val selection = "${ContactsContract.RawContacts._ID} = ?"
|
||||
contacts.filter { it.source != SMT_PRIVATE }.forEach {
|
||||
ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).apply {
|
||||
val selectionArgs = arrayOf(it.id.toString())
|
||||
withSelection(selection, selectionArgs)
|
||||
operations.add(build())
|
||||
}
|
||||
|
||||
if (operations.size % BATCH_SIZE == 0) {
|
||||
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
|
||||
operations.clear()
|
||||
}
|
||||
try {
|
||||
val operations = ArrayList<ContentProviderOperation>()
|
||||
val selection = "${ContactsContract.RawContacts._ID} = ?"
|
||||
contacts.filter { it.source != SMT_PRIVATE }.forEach {
|
||||
ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).apply {
|
||||
val selectionArgs = arrayOf(it.id.toString())
|
||||
withSelection(selection, selectionArgs)
|
||||
operations.add(build())
|
||||
}
|
||||
|
||||
if (activity.hasPermission(PERMISSION_WRITE_CONTACTS)) {
|
||||
if (operations.size % BATCH_SIZE == 0) {
|
||||
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")
|
||||
|
|
|
@ -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)")
|
||||
}
|
||||
|
||||
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? {
|
||||
val contactValues = fillGroupValues(group)
|
||||
val id = mDb.insert(GROUPS_TABLE_NAME, null, contactValues)
|
||||
|
|
|
@ -21,6 +21,12 @@ class LocalContactsHelper(val activity: Activity) {
|
|||
return activity.contactsDB.insertOrUpdate(localContact) > 0
|
||||
}
|
||||
|
||||
fun deleteContactIds(ids: Array<Int>) {
|
||||
ids.forEach {
|
||||
activity.contactsDB.deleteContactId(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun toggleFavorites(ids: Array<Int>, addToFavorites: Boolean) {
|
||||
val isStarred = if (addToFavorites) 1 else 0
|
||||
ids.forEach {
|
||||
|
@ -43,7 +49,11 @@ class LocalContactsHelper(val activity: Activity) {
|
|||
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 filteredPhoneNumbers = ArrayList<PhoneNumber>()
|
||||
if (filterDuplicates) {
|
||||
|
|
|
@ -12,7 +12,7 @@ interface ContactsDao {
|
|||
fun getContacts(): List<LocalContact>
|
||||
|
||||
@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")
|
||||
fun updateStarred(isStarred: Int, id: Int)
|
||||
|
@ -20,6 +20,6 @@ interface ContactsDao {
|
|||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertOrUpdate(contact: LocalContact): Long
|
||||
|
||||
@Query("DELETE FROM contacts WHERE id IN (:ids)")
|
||||
fun deleteContactIds(ids: String)
|
||||
@Query("DELETE FROM contacts WHERE id = :id")
|
||||
fun deleteContactId(id: Int)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue