allow storing multiple phone numbers per contact

This commit is contained in:
tibbi 2017-12-17 23:48:49 +01:00
parent 592d4edc76
commit b40be40bb4
8 changed files with 42 additions and 39 deletions

View file

@ -19,15 +19,12 @@ 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
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 com.simplemobiletools.contacts.models.PhoneNumbers
import kotlinx.android.synthetic.main.activity_contact.*
class ContactActivity : SimpleActivity() {
@ -73,8 +70,8 @@ class ContactActivity : SimpleActivity() {
setupEditContact()
}
contact_send_sms.beVisibleIf(contact!!.number.isNotEmpty())
contact_start_call.beVisibleIf(contact!!.number.isNotEmpty())
//contact_send_sms.beVisibleIf(contact!!.number.isNotEmpty())
//contact_start_call.beVisibleIf(contact!!.number.isNotEmpty())
//contact_send_email.beVisibleIf(contact!!.email.isNotEmpty())
contact_photo.background = ColorDrawable(config.primaryColor)
@ -110,8 +107,8 @@ class ContactActivity : SimpleActivity() {
contact_source_image.applyColorFilter(textColor)
contact_photo.setOnClickListener { }
contact_send_sms.setOnClickListener { sendSMSIntent(contact!!.number) }
contact_start_call.setOnClickListener { startCallIntent(contact!!.number) }
//contact_send_sms.setOnClickListener { sendSMSIntent(contact!!.number) }
//contact_start_call.setOnClickListener { startCallIntent(contact!!.number) }
//contact_send_email.setOnClickListener { sendEmailIntent(contact!!.email) }
contact_source.setOnClickListener { showAccountSourcePicker() }
contact_number_type.setOnClickListener { }
@ -145,7 +142,7 @@ class ContactActivity : SimpleActivity() {
contact_first_name.setText(contact!!.firstName)
contact_middle_name.setText(contact!!.middleName)
contact_surname.setText(contact!!.surname)
contact_number.setText(contact!!.number)
//contact_number.setText(contact!!.number)
//contact_email.setText(contact!!.email)
contact_source.text = contact!!.source
}
@ -153,7 +150,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, "", "", "", "", "", Emails(), "")
contact = Contact(0, "", "", "", "", PhoneNumbers(), Emails(), "")
}
private fun applyPhotoPlaceholder() {
@ -168,7 +165,7 @@ class ContactActivity : SimpleActivity() {
firstName = contact_first_name.value
middleName = contact_middle_name.value
surname = contact_surname.value
number = contact_number.value
//number = contact_number.value
//email = contact_email.value
if (ContactsHelper(this@ContactActivity).updateContact(this)) {

View file

@ -164,7 +164,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
if (currAdapter == null) {
ContactsAdapter(this, contacts, this, contacts_list) {
if (config.callContact) {
startCallIntent((it as Contact).number)
//startCallIntent((it as Contact).number)
} else {
openContact(it as Contact)
}

View file

@ -113,7 +113,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
view.apply {
contact_first_name.text = contact.getFullName(startNameWithSurname)
contact_first_name.setTextColor(textColor)
contact_number.text = contact.number
contact_number.text = contact.phoneNumbers.phoneNumbers.firstOrNull()?.value ?: ""
contact_number.setTextColor(textColor)
if (contact.photoUri.isNotEmpty()) {

View file

@ -13,9 +13,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME
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.Email
import com.simplemobiletools.contacts.models.Emails
import com.simplemobiletools.contacts.models.*
import com.simplemobiletools.contacts.overloads.times
import java.util.*
@ -69,8 +67,8 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
continue
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI) ?: ""
val number = "" // proper value is obtained below
val emails = Emails() // proper value is obtained below
val number = PhoneNumbers() // 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, emails, accountName)
contacts.put(id, contact)
@ -83,14 +81,17 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
val emails = getEmails()
val size = emails.size()
var size = emails.size()
for (i in 0 until size) {
val key = emails.keyAt(i)
contacts[key]?.emails = emails.valueAt(i)
}
getNumbers().forEach {
contacts[it.first]?.number = it.second
val phoneNumbers = getPhoneNumbers()
size = phoneNumbers.size()
for (i in 0 until size) {
val key = phoneNumbers.keyAt(i)
contacts[key]?.phoneNumbers = phoneNumbers.valueAt(i)
}
val contactsSize = contacts.size()
@ -149,27 +150,38 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return emails
}
private fun getNumbers(): ArrayList<Pair<Int, String>> {
val pairs = ArrayList<Pair<Int, String>>()
private fun getPhoneNumbers(contactId: Int? = null): SparseArray<PhoneNumbers> {
val phoneNumbers = SparseArray<PhoneNumbers>()
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val projection = arrayOf(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE
)
val selection = if (contactId == null) null else "${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = ?"
val selectionArgs = if (contactId == null) null else arrayOf(contactId.toString())
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, null, null, null)
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER)
pairs.add(Pair(id, number))
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.TYPE)
if (phoneNumbers[id] == null) {
phoneNumbers.put(id, PhoneNumbers())
}
phoneNumbers[id].phoneNumbers.add(PhoneNumber(number, type))
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
return pairs
return phoneNumbers
}
fun getContactNumber(id: Int): String {
@ -207,7 +219,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
val surname = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
val number = getContactNumber(id)
val number = getPhoneNumbers(id)[id] ?: PhoneNumbers()
val emails = getEmails(id)[id] ?: Emails()
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
return Contact(id, firstName, middleName, surname, photoUri, number, emails, accountName)

View file

@ -4,7 +4,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
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 number: String,
data class Contact(val id: Int, var firstName: String, var middleName: String, var surname: String, var photoUri: String, var phoneNumbers: PhoneNumbers,
var emails: Emails, var source: String) : Comparable<Contact> {
companion object {
var sorting: Int = 0

View file

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

View file

@ -1,4 +1,3 @@
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)
data class PhoneNumbers(var phoneNumbers: ArrayList<PhoneNumber> = ArrayList())

View file

@ -44,14 +44,6 @@
android:paddingTop="@dimen/medium_margin"
android:text="@string/surname"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/medium_margin"
android:paddingTop="@dimen/medium_margin"
android:text="@string/number"/>
</RadioGroup>
<include