add contact Events

This commit is contained in:
tibbi 2017-12-27 16:57:43 +01:00
parent ca9b1cc6c4
commit 9f90b35750
5 changed files with 63 additions and 24 deletions

View File

@ -36,7 +36,7 @@ ext {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:3.4.5' implementation 'com.simplemobiletools:commons:3.4.11'
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion" debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion" releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion"

View File

@ -221,7 +221,7 @@ class ContactActivity : SimpleActivity() {
private fun setupNewContact() { private fun setupNewContact() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
supportActionBar?.title = resources.getString(R.string.new_contact) 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.text = config.lastUsedContactSource
contact_source.setOnClickListener { showAccountSourcePicker() } contact_source.setOnClickListener { showAccountSourcePicker() }
} }

View File

@ -22,6 +22,7 @@ import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.Email import com.simplemobiletools.contacts.models.Email
import com.simplemobiletools.contacts.models.Event
import com.simplemobiletools.contacts.models.PhoneNumber import com.simplemobiletools.contacts.models.PhoneNumber
import com.simplemobiletools.contacts.overloads.times import com.simplemobiletools.contacts.overloads.times
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@ -56,10 +57,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
continue continue
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI) ?: "" val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI) ?: ""
val number = ArrayList<PhoneNumber>() // proper value is obtained below val number = ArrayList<PhoneNumber>() // proper value is obtained below
val emails = ArrayList<Email>() // proper value is obtained below val emails = ArrayList<Email>() // proper value is obtained below
val events = ArrayList<Event>() // proper value is obtained below
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) 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) contacts.put(id, contact)
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
@ -90,6 +92,40 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}.start() }.start()
} }
private fun getPhoneNumbers(contactId: Int? = null): SparseArray<ArrayList<PhoneNumber>> {
val phoneNumbers = SparseArray<ArrayList<PhoneNumber>>()
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<ArrayList<Email>> { private fun getEmails(contactId: Int? = null): SparseArray<ArrayList<Email>> {
val emails = SparseArray<ArrayList<Email>>() val emails = SparseArray<ArrayList<Email>>()
val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI
@ -125,38 +161,37 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return emails return emails
} }
private fun getPhoneNumbers(contactId: Int? = null): SparseArray<ArrayList<PhoneNumber>> { private fun getEvents(contactId: Int): SparseArray<ArrayList<Event>> {
val phoneNumbers = SparseArray<ArrayList<PhoneNumber>>() val events = SparseArray<ArrayList<Event>>()
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID, ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Event.TYPE
ContactsContract.CommonDataKinds.Phone.TYPE
) )
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?"
val selection = if (contactId == null) null else "${ContactsContract.Data.RAW_CONTACT_ID} = ?" val selectionArgs = arrayOf(contactId.toString(), ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
val selectionArgs = if (contactId == null) null else arrayOf(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 id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID) val startDate = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE)
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.TYPE)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.TYPE)
if (phoneNumbers[id] == null) { if (events[contactId] == null) {
phoneNumbers.put(id, ArrayList()) events.put(contactId, ArrayList())
} }
phoneNumbers[id].add(PhoneNumber(number, type)) events[contactId]!!.add(Event(startDate, type))
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} catch (e: Exception) {
activity.showErrorToast(e)
} finally { } finally {
cursor?.close() cursor?.close()
} }
return phoneNumbers
return events
} }
fun getContactWithId(id: Int): Contact? { fun getContactWithId(id: Int): Contact? {
@ -178,8 +213,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: "" val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
val number = getPhoneNumbers(id)[id] ?: ArrayList() val number = getPhoneNumbers(id)[id] ?: ArrayList()
val emails = getEmails(id)[id] ?: ArrayList() val emails = getEmails(id)[id] ?: ArrayList()
val events = getEvents(id)[id] ?: ArrayList()
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) 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 { } finally {
cursor?.close() cursor?.close()

View File

@ -5,7 +5,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME
import com.simplemobiletools.commons.helpers.SORT_DESCENDING 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, data class Contact(val id: Int, var firstName: String, var middleName: String, var surname: String, var photoUri: String,
var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var source: String) : Comparable<Contact> { var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var events: ArrayList<Event>, var source: String) : Comparable<Contact> {
companion object { companion object {
var sorting: Int = 0 var sorting: Int = 0
} }

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.contacts.models
data class Event(var value: String, var type: Int)