prepare for storing multiple emails per contact

This commit is contained in:
tibbi 2017-12-14 00:15:24 +01:00
parent 17fd1dc084
commit 210ec1328d
5 changed files with 57 additions and 25 deletions

View File

@ -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()

View File

@ -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<Contact>) -> Unit) {
val contacts = HashMap<Int, Contact>()
val contacts = SparseArray<Contact>()
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<Contact>(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<Pair<Int, String>> {
val pairs = ArrayList<Pair<Int, String>>()
private fun getEmails(): SparseArray<Emails> {
val emails = SparseArray<Emails>()
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<Pair<Int, String>> {
@ -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()

View File

@ -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<Contact> {
var emails: Emails, var source: String) : Comparable<Contact> {
companion object {
var sorting: Int = 0
}

View File

@ -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
}
}
}

View File

@ -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)