From 740dfb090e60397e93c4302ad917c5e69c15c4b5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 22 Feb 2018 18:06:16 +0100 Subject: [PATCH] add contact events when adding optional fields is active --- .../contacts/helpers/ContactsHelper.kt | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) 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 b224dd67..47790131 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -89,6 +89,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val key = addresses.keyAt(i) contacts[key]?.addresses = addresses.valueAt(i) } + + val events = getEvents() + size = events.size() + for (i in 0 until size) { + val key = events.keyAt(i) + contacts[key]?.events = events.valueAt(i) + } } activity.dbHelper.getContacts().forEach { @@ -216,28 +223,37 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return addresses } - private fun getEvents(contactId: Int): SparseArray> { + private fun getEvents(contactId: Int? = null): SparseArray> { val events = SparseArray>() val uri = ContactsContract.Data.CONTENT_URI val projection = arrayOf( + ContactsContract.Data.RAW_CONTACT_ID, 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 selection = "${ContactsContract.Data.MIMETYPE} = ?" + val selectionArgs = arrayOf(CommonDataKinds.Event.CONTENT_ITEM_TYPE) + + if (contactId != null) { + selection += " AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?" + selectionArgs[1] = contactId.toString() + } + 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 id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID) + val startDate = cursor.getStringValue(CommonDataKinds.Event.START_DATE) ?: continue val type = cursor.getIntValue(CommonDataKinds.Event.TYPE) - if (events[contactId] == null) { - events.put(contactId, ArrayList()) + if (events[id] == null) { + events.put(id, ArrayList()) } - events[contactId]!!.add(Event(startDate, type)) + events[id]!!.add(Event(startDate, type)) } while (cursor.moveToNext()) } } catch (e: Exception) {