From d8f9fa2a5f0aff6668f15e43b818a6de6af58ec1 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 24 Dec 2017 21:06:22 +0100 Subject: [PATCH] implement inserting new contact, without photo --- .../contacts/activities/ContactActivity.kt | 24 +++++++-- .../contacts/helpers/ContactsHelper.kt | 49 +++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt index 9053f193..0ea4cbe5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt @@ -330,9 +330,11 @@ class ContactActivity : SimpleActivity() { emails = getFilledEmails() source = contact_source.value - /*if (ContactsHelper(this@ContactActivity).updateContact(this)) { - finish() - }*/ + if (id == 0) { + insertNewContact() + } else { + updateContact() + } } } @@ -366,6 +368,22 @@ class ContactActivity : SimpleActivity() { 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() { val view = layoutInflater.inflate(R.layout.item_phone_number, contact_numbers_holder, false) updateTextColors(view as ViewGroup) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 83ae3d09..c8f5d545 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -266,6 +266,55 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } } + fun insertContact(contact: Contact): Boolean { + return try { + val operations = ArrayList() + 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) {