implement inserting new contact, without photo

This commit is contained in:
tibbi 2017-12-24 21:06:22 +01:00
parent 4b7dbc25b1
commit d8f9fa2a5f
2 changed files with 70 additions and 3 deletions

View File

@ -330,9 +330,11 @@ class ContactActivity : SimpleActivity() {
emails = getFilledEmails() emails = getFilledEmails()
source = contact_source.value source = contact_source.value
/*if (ContactsHelper(this@ContactActivity).updateContact(this)) { if (id == 0) {
finish() insertNewContact()
}*/ } else {
updateContact()
}
} }
} }
@ -366,6 +368,22 @@ class ContactActivity : SimpleActivity() {
return emails return emails
} }
private fun insertNewContact() {
if (ContactsHelper(this@ContactActivity).insertContact(contact!!)) {
finish()
} else {
toast(R.string.unknown_error_occurred)
}
}
private fun updateContact() {
if (ContactsHelper(this@ContactActivity).updateContact(contact!!)) {
finish()
} else {
toast(R.string.unknown_error_occurred)
}
}
private fun addNewPhoneNumberField() { private fun addNewPhoneNumberField() {
val view = layoutInflater.inflate(R.layout.item_phone_number, contact_numbers_holder, false) val view = layoutInflater.inflate(R.layout.item_phone_number, contact_numbers_holder, false)
updateTextColors(view as ViewGroup) updateTextColors(view as ViewGroup)

View File

@ -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 deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact))
fun deleteContacts(contacts: ArrayList<Contact>) { fun deleteContacts(contacts: ArrayList<Contact>) {