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 9386ca55..5b3298e1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt @@ -19,6 +19,7 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS import com.simplemobiletools.contacts.R +import com.simplemobiletools.contacts.R.string.email import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.sendEmailIntent import com.simplemobiletools.contacts.extensions.sendSMSIntent @@ -26,6 +27,7 @@ import com.simplemobiletools.contacts.extensions.startCallIntent import com.simplemobiletools.contacts.helpers.CONTACT_ID import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.models.Contact +import com.simplemobiletools.contacts.models.Emails import kotlinx.android.synthetic.main.activity_contact.* class ContactActivity : SimpleActivity() { @@ -73,7 +75,7 @@ class ContactActivity : SimpleActivity() { contact_send_sms.beVisibleIf(contact!!.number.isNotEmpty()) contact_start_call.beVisibleIf(contact!!.number.isNotEmpty()) - contact_send_email.beVisibleIf(contact!!.email.isNotEmpty()) + //contact_send_email.beVisibleIf(contact!!.email.isNotEmpty()) contact_photo.background = ColorDrawable(config.primaryColor) @@ -110,7 +112,7 @@ class ContactActivity : SimpleActivity() { contact_photo.setOnClickListener { } contact_send_sms.setOnClickListener { sendSMSIntent(contact!!.number) } contact_start_call.setOnClickListener { startCallIntent(contact!!.number) } - contact_send_email.setOnClickListener { sendEmailIntent(contact!!.email) } + //contact_send_email.setOnClickListener { sendEmailIntent(contact!!.email) } contact_source.setOnClickListener { showAccountSourcePicker() } updateTextColors(contact_scrollview) @@ -142,14 +144,14 @@ class ContactActivity : SimpleActivity() { contact_middle_name.setText(contact!!.middleName) contact_surname.setText(contact!!.surname) contact_number.setText(contact!!.number) - contact_email.setText(contact!!.email) + //contact_email.setText(contact!!.email) contact_source.text = contact!!.source } private fun setupNewContact() { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) supportActionBar?.title = resources.getString(R.string.new_contact) - contact = Contact(0, "", "", "", "", "", "", "") + contact = Contact(0, "", "", "", "", "", Emails(), "") } private fun applyPhotoPlaceholder() { @@ -165,7 +167,7 @@ class ContactActivity : SimpleActivity() { middleName = contact_middle_name.value surname = contact_surname.value number = contact_number.value - email = contact_email.value + //email = contact_email.value if (ContactsHelper(this@ContactActivity).updateContact(this)) { finish() 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 7e9a661a..d8fc6ae3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.contacts.helpers import android.content.ContentProviderOperation import android.database.Cursor import android.provider.ContactsContract +import android.util.SparseArray import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getStringValue @@ -13,6 +14,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.models.Contact +import com.simplemobiletools.contacts.models.Emails import com.simplemobiletools.contacts.overloads.times import java.util.* @@ -40,7 +42,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } fun getContacts(callback: (ArrayList) -> Unit) { - val contacts = HashMap() + val contacts = SparseArray() Thread { val sources = activity.config.displayContactSources val questionMarks = ("?," * sources.size).trimEnd(',') @@ -67,9 +69,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI) ?: "" val number = "" // proper value is obtained below - val email = "" // proper value is obtained below + val emails = Emails() // proper value is obtained below val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) - val contact = Contact(id, firstName, middleName, surname, photoUri, number, email, accountName) + val contact = Contact(id, firstName, middleName, surname, photoUri, number, emails, accountName) contacts.put(id, contact) } while (cursor.moveToNext()) } @@ -79,19 +81,21 @@ class ContactsHelper(val activity: BaseSimpleActivity) { cursor?.close() } - getEmails().forEach { - if (contacts.containsKey(it.first)) { - contacts[it.first]!!.email = it.second - } + val emails = getEmails() + val size = emails.size() + for (i in 0 until size) { + val key = emails.keyAt(i) + contacts[key]?.emails = emails.valueAt(i) } getNumbers().forEach { - if (contacts.containsKey(it.first)) { - contacts[it.first]!!.number = it.second - } + contacts[it.first]?.number = it.second } - callback(ArrayList(contacts.values)) + val contactsSize = contacts.size() + val resultContacts = ArrayList(contactsSize) + (0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) } + callback(resultContacts) }.start() } @@ -109,12 +113,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } } - private fun getEmails(): ArrayList> { - val pairs = ArrayList>() + private fun getEmails(): SparseArray { + val emails = SparseArray() val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI val projection = arrayOf( ContactsContract.CommonDataKinds.Phone.CONTACT_ID, - ContactsContract.CommonDataKinds.Email.DATA + ContactsContract.CommonDataKinds.Email.DATA, + ContactsContract.CommonDataKinds.Email.TYPE ) var cursor: Cursor? = null try { @@ -123,13 +128,20 @@ class ContactsHelper(val activity: BaseSimpleActivity) { do { val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID) val email = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.DATA) - pairs.add(Pair(id, email)) + val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Email.TYPE) + + if (emails[id] == null) { + emails.put(id, Emails()) + } + + emails[id]!!.fillEmail(email, type) } while (cursor.moveToNext()) } } finally { cursor?.close() } - return pairs + + return emails } private fun getNumbers(): ArrayList> { @@ -155,7 +167,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return pairs } - fun getContactEmail(id: Int): String { + fun getContactEmails(id: Int): String { val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI val projection = arrayOf(ContactsContract.CommonDataKinds.Email.DATA) val selection = "${ContactsContract.CommonDataKinds.Email.CONTACT_ID} = ?" @@ -209,9 +221,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) { val surname = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: "" val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: "" val number = getContactNumber(id) - val email = getContactEmail(id) + val emails = Emails() val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) - return Contact(id, firstName, middleName, surname, photoUri, number, email, accountName) + return Contact(id, firstName, middleName, surname, photoUri, number, emails, 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 77d5bbe5..7b0ba816 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -6,7 +6,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME 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 number: String, - var email: String, var source: String) : Comparable { + var emails: Emails, var source: String) : Comparable { companion object { var sorting: Int = 0 } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Emails.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Emails.kt new file mode 100644 index 00000000..403e592d --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Emails.kt @@ -0,0 +1,14 @@ +package com.simplemobiletools.contacts.models + +import android.provider.ContactsContract + +data class Emails(var home: String = "", var work: String = "", var mobile: String = "", var other: String = "") { + fun fillEmail(email: String, type: Int) { + when (type) { + ContactsContract.CommonDataKinds.Email.TYPE_HOME -> home = email + ContactsContract.CommonDataKinds.Email.TYPE_WORK -> work = email + ContactsContract.CommonDataKinds.Email.TYPE_MOBILE -> mobile = email + ContactsContract.CommonDataKinds.Email.TYPE_OTHER -> other = email + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/PhoneNumbers.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/PhoneNumbers.kt new file mode 100644 index 00000000..6c640337 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/PhoneNumbers.kt @@ -0,0 +1,4 @@ +package com.simplemobiletools.contacts.models + +data class PhoneNumbers(var home: String, var work: String, var mobile: String, var main: String, var work_fax: String, var home_fax: String, + var pager: String, var other: String)