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 1b1e65f9..716cd6f3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -177,39 +177,6 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return emails } - private fun getEvents(contactId: Int): SparseArray> { - val events = SparseArray>() - val uri = ContactsContract.Data.CONTENT_URI - val projection = arrayOf( - CommonDataKinds.Event.START_DATE, - CommonDataKinds.Event.TYPE - ) - val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?" - val selectionArgs = arrayOf(contactId.toString(), CommonDataKinds.Event.CONTENT_ITEM_TYPE) - var cursor: Cursor? = null - try { - cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) - if (cursor?.moveToFirst() == true) { - do { - val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) - val type = cursor.getIntValue(CommonDataKinds.Event.TYPE) - - if (events[contactId] == null) { - events.put(contactId, ArrayList()) - } - - events[contactId]!!.add(Event(startDate, type)) - } while (cursor.moveToNext()) - } - } catch (e: Exception) { - activity.showErrorToast(e) - } finally { - cursor?.close() - } - - return events - } - private fun getAddresses(contactId: Int? = null): SparseArray> { val addresses = SparseArray>() val uri = CommonDataKinds.StructuredPostal.CONTENT_URI @@ -248,6 +215,39 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return addresses } + private fun getEvents(contactId: Int): SparseArray> { + val events = SparseArray>() + val uri = ContactsContract.Data.CONTENT_URI + val projection = arrayOf( + CommonDataKinds.Event.START_DATE, + CommonDataKinds.Event.TYPE + ) + val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?" + val selectionArgs = arrayOf(contactId.toString(), CommonDataKinds.Event.CONTENT_ITEM_TYPE) + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) + if (cursor?.moveToFirst() == true) { + do { + val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) + val type = cursor.getIntValue(CommonDataKinds.Event.TYPE) + + if (events[contactId] == null) { + events.put(contactId, ArrayList()) + } + + events[contactId]!!.add(Event(startDate, type)) + } while (cursor.moveToNext()) + } + } catch (e: Exception) { + activity.showErrorToast(e) + } finally { + cursor?.close() + } + + return events + } + fun getContactWithId(id: Int, isLocalPrivate: Boolean): Contact? { if (id == 0) { return null @@ -432,6 +432,25 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } } + // delete addresses + ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply { + val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? " + val selectionArgs = arrayOf(contact.id.toString(), CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) + withSelection(selection, selectionArgs) + operations.add(build()) + } + + // add addresses + contact.addresses.forEach { + ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply { + withValue(ContactsContract.Data.RAW_CONTACT_ID, contact.id) + withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) + withValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, it.value) + withValue(CommonDataKinds.StructuredPostal.TYPE, it.type) + operations.add(build()) + } + } + // delete events ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply { val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? " @@ -555,6 +574,17 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } } + // addresses + contact.addresses.forEach { + ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply { + withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) + withValue(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, it.value) + withValue(CommonDataKinds.StructuredPostal.TYPE, it.type) + operations.add(build()) + } + } + // events contact.events.forEach { ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {