add contact events when adding optional fields is active

This commit is contained in:
tibbi 2018-02-22 18:06:16 +01:00
parent 7f11945af4
commit 740dfb090e
1 changed files with 23 additions and 7 deletions

View File

@ -89,6 +89,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val key = addresses.keyAt(i) val key = addresses.keyAt(i)
contacts[key]?.addresses = addresses.valueAt(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 { activity.dbHelper.getContacts().forEach {
@ -216,28 +223,37 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return addresses return addresses
} }
private fun getEvents(contactId: Int): SparseArray<ArrayList<Event>> { private fun getEvents(contactId: Int? = null): SparseArray<ArrayList<Event>> {
val events = SparseArray<ArrayList<Event>>() val events = SparseArray<ArrayList<Event>>()
val uri = ContactsContract.Data.CONTENT_URI val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID,
CommonDataKinds.Event.START_DATE, CommonDataKinds.Event.START_DATE,
CommonDataKinds.Event.TYPE 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 var cursor: Cursor? = null
try { try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) { if (cursor?.moveToFirst() == true) {
do { 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) val type = cursor.getIntValue(CommonDataKinds.Event.TYPE)
if (events[contactId] == null) { if (events[id] == null) {
events.put(contactId, ArrayList()) events.put(id, ArrayList())
} }
events[contactId]!!.add(Event(startDate, type)) events[id]!!.add(Event(startDate, type))
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} catch (e: Exception) { } catch (e: Exception) {