From 9f90b357509ca1afe27fc12980d3d496e1fc60b9 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 27 Dec 2017 16:57:43 +0100 Subject: [PATCH] add contact Events --- app/build.gradle | 2 +- .../contacts/activities/ContactActivity.kt | 2 +- .../contacts/helpers/ContactsHelper.kt | 78 ++++++++++++++----- .../contacts/models/Contact.kt | 2 +- .../contacts/models/Event.kt | 3 + 5 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/models/Event.kt diff --git a/app/build.gradle b/app/build.gradle index a494e962..100b6105 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,7 +36,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.4.5' + implementation 'com.simplemobiletools:commons:3.4.11' debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion" releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion" 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 382fd6b6..2e9eee19 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt @@ -221,7 +221,7 @@ class ContactActivity : SimpleActivity() { private fun setupNewContact() { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) supportActionBar?.title = resources.getString(R.string.new_contact) - contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), "") + contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), "") contact_source.text = config.lastUsedContactSource contact_source.setOnClickListener { showAccountSourcePicker() } } 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 333c3ce5..61c203b3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -22,6 +22,7 @@ import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Email +import com.simplemobiletools.contacts.models.Event import com.simplemobiletools.contacts.models.PhoneNumber import com.simplemobiletools.contacts.overloads.times import java.io.ByteArrayOutputStream @@ -56,10 +57,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) { continue val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI) ?: "" - val number = ArrayList() // proper value is obtained below - val emails = ArrayList() // proper value is obtained below + val number = ArrayList() // proper value is obtained below + val emails = ArrayList() // proper value is obtained below + val events = ArrayList() // proper value is obtained below val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) - val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, accountName) + val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, events, accountName) contacts.put(id, contact) } while (cursor.moveToNext()) } @@ -90,6 +92,40 @@ class ContactsHelper(val activity: BaseSimpleActivity) { }.start() } + private fun getPhoneNumbers(contactId: Int? = null): SparseArray> { + val phoneNumbers = SparseArray>() + val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI + val projection = arrayOf( + ContactsContract.Data.RAW_CONTACT_ID, + ContactsContract.CommonDataKinds.Phone.NUMBER, + ContactsContract.CommonDataKinds.Phone.TYPE + ) + + val selection = if (contactId == null) null else "${ContactsContract.Data.RAW_CONTACT_ID} = ?" + val selectionArgs = if (contactId == null) null else arrayOf(contactId.toString()) + + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) + if (cursor?.moveToFirst() == true) { + do { + val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID) + val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) + val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.TYPE) + + if (phoneNumbers[id] == null) { + phoneNumbers.put(id, ArrayList()) + } + + phoneNumbers[id].add(PhoneNumber(number, type)) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + return phoneNumbers + } + private fun getEmails(contactId: Int? = null): SparseArray> { val emails = SparseArray>() val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI @@ -125,38 +161,37 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return emails } - private fun getPhoneNumbers(contactId: Int? = null): SparseArray> { - val phoneNumbers = SparseArray>() - val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI + private fun getEvents(contactId: Int): SparseArray> { + val events = SparseArray>() + val uri = ContactsContract.Data.CONTENT_URI val projection = arrayOf( - ContactsContract.Data.RAW_CONTACT_ID, - ContactsContract.CommonDataKinds.Phone.NUMBER, - ContactsContract.CommonDataKinds.Phone.TYPE + ContactsContract.CommonDataKinds.Event.START_DATE, + ContactsContract.CommonDataKinds.Event.TYPE ) - - val selection = if (contactId == null) null else "${ContactsContract.Data.RAW_CONTACT_ID} = ?" - val selectionArgs = if (contactId == null) null else arrayOf(contactId.toString()) - + val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?" + val selectionArgs = arrayOf(contactId.toString(), ContactsContract.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 id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID) - val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) - val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.TYPE) + val startDate = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE) + val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.TYPE) - if (phoneNumbers[id] == null) { - phoneNumbers.put(id, ArrayList()) + if (events[contactId] == null) { + events.put(contactId, ArrayList()) } - phoneNumbers[id].add(PhoneNumber(number, type)) + events[contactId]!!.add(Event(startDate, type)) } while (cursor.moveToNext()) } + } catch (e: Exception) { + activity.showErrorToast(e) } finally { cursor?.close() } - return phoneNumbers + + return events } fun getContactWithId(id: Int): Contact? { @@ -178,8 +213,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: "" val number = getPhoneNumbers(id)[id] ?: ArrayList() val emails = getEmails(id)[id] ?: ArrayList() + val events = getEvents(id)[id] ?: ArrayList() val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) - return Contact(id, firstName, middleName, surname, photoUri, number, emails, accountName) + return Contact(id, firstName, middleName, surname, photoUri, number, emails, events, accountName) } } finally { cursor?.close() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt index 49b4c549..6fd790d7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -5,7 +5,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME import com.simplemobiletools.commons.helpers.SORT_DESCENDING data class Contact(val id: Int, var firstName: String, var middleName: String, var surname: String, var photoUri: String, - var phoneNumbers: ArrayList, var emails: ArrayList, var source: String) : Comparable { + var phoneNumbers: ArrayList, var emails: ArrayList, var events: ArrayList, var source: String) : Comparable { companion object { var sorting: Int = 0 } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Event.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Event.kt new file mode 100644 index 00000000..c28a8eb9 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Event.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.contacts.models + +data class Event(var value: String, var type: Int)