implement phone number and email updating at existing contacts

This commit is contained in:
tibbi 2017-12-24 23:15:11 +01:00
parent d8f9fa2a5f
commit b9af8116bc
1 changed files with 38 additions and 0 deletions

View File

@ -258,6 +258,44 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
operations.add(this.build()) operations.add(this.build())
} }
// delete phone numbers
ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply {
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? "
val selectionArgs = arrayOf(contact.id.toString(), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
withSelection(selection, selectionArgs)
operations.add(this.build())
}
// add phone numbers
contact.phoneNumbers.forEach {
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
withValue(ContactsContract.Data.RAW_CONTACT_ID, contact.id)
withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, it.value)
withValue(ContactsContract.CommonDataKinds.Phone.TYPE, it.type)
operations.add(this.build())
}
}
// delete emails
ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply {
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? "
val selectionArgs = arrayOf(contact.id.toString(), ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
withSelection(selection, selectionArgs)
operations.add(this.build())
}
// add emails
contact.emails.forEach {
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
withValue(ContactsContract.Data.RAW_CONTACT_ID, contact.id)
withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
withValue(ContactsContract.CommonDataKinds.Email.DATA, it.value)
withValue(ContactsContract.CommonDataKinds.Email.TYPE, it.type)
operations.add(this.build())
}
}
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
true true
} catch (e: Exception) { } catch (e: Exception) {