mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
allow storing multiple phone numbers per contact
This commit is contained in:
parent
592d4edc76
commit
b40be40bb4
@ -19,15 +19,12 @@ import com.simplemobiletools.commons.extensions.*
|
|||||||
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
|
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
|
||||||
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
|
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
|
||||||
import com.simplemobiletools.contacts.R
|
import com.simplemobiletools.contacts.R
|
||||||
import com.simplemobiletools.contacts.R.string.email
|
|
||||||
import com.simplemobiletools.contacts.extensions.config
|
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.CONTACT_ID
|
||||||
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
||||||
import com.simplemobiletools.contacts.models.Contact
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
import com.simplemobiletools.contacts.models.Emails
|
import com.simplemobiletools.contacts.models.Emails
|
||||||
|
import com.simplemobiletools.contacts.models.PhoneNumbers
|
||||||
import kotlinx.android.synthetic.main.activity_contact.*
|
import kotlinx.android.synthetic.main.activity_contact.*
|
||||||
|
|
||||||
class ContactActivity : SimpleActivity() {
|
class ContactActivity : SimpleActivity() {
|
||||||
@ -73,8 +70,8 @@ class ContactActivity : SimpleActivity() {
|
|||||||
setupEditContact()
|
setupEditContact()
|
||||||
}
|
}
|
||||||
|
|
||||||
contact_send_sms.beVisibleIf(contact!!.number.isNotEmpty())
|
//contact_send_sms.beVisibleIf(contact!!.number.isNotEmpty())
|
||||||
contact_start_call.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)
|
contact_photo.background = ColorDrawable(config.primaryColor)
|
||||||
@ -110,8 +107,8 @@ class ContactActivity : SimpleActivity() {
|
|||||||
contact_source_image.applyColorFilter(textColor)
|
contact_source_image.applyColorFilter(textColor)
|
||||||
|
|
||||||
contact_photo.setOnClickListener { }
|
contact_photo.setOnClickListener { }
|
||||||
contact_send_sms.setOnClickListener { sendSMSIntent(contact!!.number) }
|
//contact_send_sms.setOnClickListener { sendSMSIntent(contact!!.number) }
|
||||||
contact_start_call.setOnClickListener { startCallIntent(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() }
|
contact_source.setOnClickListener { showAccountSourcePicker() }
|
||||||
contact_number_type.setOnClickListener { }
|
contact_number_type.setOnClickListener { }
|
||||||
@ -145,7 +142,7 @@ class ContactActivity : SimpleActivity() {
|
|||||||
contact_first_name.setText(contact!!.firstName)
|
contact_first_name.setText(contact!!.firstName)
|
||||||
contact_middle_name.setText(contact!!.middleName)
|
contact_middle_name.setText(contact!!.middleName)
|
||||||
contact_surname.setText(contact!!.surname)
|
contact_surname.setText(contact!!.surname)
|
||||||
contact_number.setText(contact!!.number)
|
//contact_number.setText(contact!!.number)
|
||||||
//contact_email.setText(contact!!.email)
|
//contact_email.setText(contact!!.email)
|
||||||
contact_source.text = contact!!.source
|
contact_source.text = contact!!.source
|
||||||
}
|
}
|
||||||
@ -153,7 +150,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, "", "", "", "", "", Emails(), "")
|
contact = Contact(0, "", "", "", "", PhoneNumbers(), Emails(), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun applyPhotoPlaceholder() {
|
private fun applyPhotoPlaceholder() {
|
||||||
@ -168,7 +165,7 @@ class ContactActivity : SimpleActivity() {
|
|||||||
firstName = contact_first_name.value
|
firstName = contact_first_name.value
|
||||||
middleName = contact_middle_name.value
|
middleName = contact_middle_name.value
|
||||||
surname = contact_surname.value
|
surname = contact_surname.value
|
||||||
number = contact_number.value
|
//number = contact_number.value
|
||||||
//email = contact_email.value
|
//email = contact_email.value
|
||||||
|
|
||||||
if (ContactsHelper(this@ContactActivity).updateContact(this)) {
|
if (ContactsHelper(this@ContactActivity).updateContact(this)) {
|
||||||
|
@ -164,7 +164,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
|||||||
if (currAdapter == null) {
|
if (currAdapter == null) {
|
||||||
ContactsAdapter(this, contacts, this, contacts_list) {
|
ContactsAdapter(this, contacts, this, contacts_list) {
|
||||||
if (config.callContact) {
|
if (config.callContact) {
|
||||||
startCallIntent((it as Contact).number)
|
//startCallIntent((it as Contact).number)
|
||||||
} else {
|
} else {
|
||||||
openContact(it as Contact)
|
openContact(it as Contact)
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
|
|||||||
view.apply {
|
view.apply {
|
||||||
contact_first_name.text = contact.getFullName(startNameWithSurname)
|
contact_first_name.text = contact.getFullName(startNameWithSurname)
|
||||||
contact_first_name.setTextColor(textColor)
|
contact_first_name.setTextColor(textColor)
|
||||||
contact_number.text = contact.number
|
contact_number.text = contact.phoneNumbers.phoneNumbers.firstOrNull()?.value ?: ""
|
||||||
contact_number.setTextColor(textColor)
|
contact_number.setTextColor(textColor)
|
||||||
|
|
||||||
if (contact.photoUri.isNotEmpty()) {
|
if (contact.photoUri.isNotEmpty()) {
|
||||||
|
@ -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_BY_SURNAME
|
||||||
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
|
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
|
||||||
import com.simplemobiletools.contacts.extensions.config
|
import com.simplemobiletools.contacts.extensions.config
|
||||||
import com.simplemobiletools.contacts.models.Contact
|
import com.simplemobiletools.contacts.models.*
|
||||||
import com.simplemobiletools.contacts.models.Email
|
|
||||||
import com.simplemobiletools.contacts.models.Emails
|
|
||||||
import com.simplemobiletools.contacts.overloads.times
|
import com.simplemobiletools.contacts.overloads.times
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@ -69,7 +67,7 @@ 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 = "" // proper value is obtained below
|
val number = PhoneNumbers() // proper value is obtained below
|
||||||
val emails = Emails() // proper value is obtained below
|
val emails = Emails() // 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, accountName)
|
||||||
@ -83,14 +81,17 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val emails = getEmails()
|
val emails = getEmails()
|
||||||
val size = emails.size()
|
var size = emails.size()
|
||||||
for (i in 0 until size) {
|
for (i in 0 until size) {
|
||||||
val key = emails.keyAt(i)
|
val key = emails.keyAt(i)
|
||||||
contacts[key]?.emails = emails.valueAt(i)
|
contacts[key]?.emails = emails.valueAt(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
getNumbers().forEach {
|
val phoneNumbers = getPhoneNumbers()
|
||||||
contacts[it.first]?.number = it.second
|
size = phoneNumbers.size()
|
||||||
|
for (i in 0 until size) {
|
||||||
|
val key = phoneNumbers.keyAt(i)
|
||||||
|
contacts[key]?.phoneNumbers = phoneNumbers.valueAt(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
val contactsSize = contacts.size()
|
val contactsSize = contacts.size()
|
||||||
@ -149,27 +150,38 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
return emails
|
return emails
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getNumbers(): ArrayList<Pair<Int, String>> {
|
private fun getPhoneNumbers(contactId: Int? = null): SparseArray<PhoneNumbers> {
|
||||||
val pairs = ArrayList<Pair<Int, String>>()
|
val phoneNumbers = SparseArray<PhoneNumbers>()
|
||||||
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||||
val projection = arrayOf(
|
val projection = arrayOf(
|
||||||
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
|
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
|
var cursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
cursor = activity.contentResolver.query(uri, projection, null, null, 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.CommonDataKinds.Phone.CONTACT_ID)
|
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)
|
||||||
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
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())
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
}
|
}
|
||||||
return pairs
|
return phoneNumbers
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getContactNumber(id: Int): String {
|
fun getContactNumber(id: Int): String {
|
||||||
@ -207,7 +219,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
|
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
|
||||||
val surname = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
|
val surname = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
|
||||||
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
|
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 emails = getEmails(id)[id] ?: Emails()
|
||||||
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, accountName)
|
||||||
|
@ -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_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, 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> {
|
var emails: Emails, var source: String) : Comparable<Contact> {
|
||||||
companion object {
|
companion object {
|
||||||
var sorting: Int = 0
|
var sorting: Int = 0
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
package com.simplemobiletools.contacts.models
|
||||||
|
|
||||||
|
data class PhoneNumber(var value: String, var type: Int)
|
@ -1,4 +1,3 @@
|
|||||||
package com.simplemobiletools.contacts.models
|
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,
|
data class PhoneNumbers(var phoneNumbers: ArrayList<PhoneNumber> = ArrayList())
|
||||||
var pager: String, var other: String)
|
|
||||||
|
@ -44,14 +44,6 @@
|
|||||||
android:paddingTop="@dimen/medium_margin"
|
android:paddingTop="@dimen/medium_margin"
|
||||||
android:text="@string/surname"/>
|
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>
|
</RadioGroup>
|
||||||
|
|
||||||
<include
|
<include
|
||||||
|
Loading…
x
Reference in New Issue
Block a user