mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
implement inserting new contact, without photo
This commit is contained in:
@ -266,6 +266,55 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
||||
}
|
||||
}
|
||||
|
||||
fun insertContact(contact: Contact): Boolean {
|
||||
return try {
|
||||
val operations = ArrayList<ContentProviderOperation>()
|
||||
ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).apply {
|
||||
withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
|
||||
withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
|
||||
operations.add(this.build())
|
||||
}
|
||||
|
||||
// names
|
||||
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
|
||||
withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
|
||||
withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
||||
withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contact.firstName)
|
||||
withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, contact.middleName)
|
||||
withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, contact.surname)
|
||||
operations.add(this.build())
|
||||
}
|
||||
|
||||
// phone numbers
|
||||
contact.phoneNumbers.forEach {
|
||||
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
|
||||
withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
// emails
|
||||
contact.emails.forEach {
|
||||
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
|
||||
withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
|
||||
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)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
activity.showErrorToast(e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact))
|
||||
|
||||
fun deleteContacts(contacts: ArrayList<Contact>) {
|
||||
|
Reference in New Issue
Block a user