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 b8ac0662..db12d518 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt @@ -135,7 +135,7 @@ class ContactActivity : SimpleActivity() { private fun setupEditContact() { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN) supportActionBar?.title = resources.getString(R.string.edit_contact) - contact_name.setText(contact!!.name) + contact_name.setText(contact!!.firstName) contact_number.setText(contact!!.number) contact_email.setText(contact!!.email) } @@ -143,7 +143,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, "", "", "", "", "") + contact = Contact(0, "", "", "", "", "", "", "") } private fun applyPhotoPlaceholder() { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt index c1cc06d9..6a50b0aa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt @@ -100,7 +100,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList> { val pairs = ArrayList>() val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI - val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Email.DATA) + val projection = arrayOf( + ContactsContract.CommonDataKinds.Phone.CONTACT_ID, + ContactsContract.CommonDataKinds.Email.DATA + ) var cursor: Cursor? = null try { cursor = activity.contentResolver.query(uri, projection, null, null, null) @@ -111,6 +129,29 @@ class ContactsHelper(val activity: SimpleActivity) { return pairs } + private fun getNumbers(): ArrayList> { + val pairs = ArrayList>() + val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI + val projection = arrayOf( + ContactsContract.CommonDataKinds.Phone.CONTACT_ID, + ContactsContract.CommonDataKinds.Phone.NUMBER + ) + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, null, null, 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)) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + return pairs + } + fun getContactEmail(id: Int): String { val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI val projection = arrayOf(ContactsContract.CommonDataKinds.Email.DATA) @@ -129,24 +170,45 @@ class ContactsHelper(val activity: SimpleActivity) { return "" } - fun getContactWithId(id: Int): Contact? { - if (id == 0) { - return null - } - + fun getContactNumber(id: Int): String { val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI - val projection = getContactProjection() + val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER) val selection = "${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = ?" val selectionArgs = arrayOf(id.toString()) var cursor: Cursor? = null try { cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) if (cursor?.moveToFirst() == true) { - val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: return null - val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: "" + return cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) + } + } finally { + cursor?.close() + } + + return "" + } + + fun getContactWithId(id: Int): Contact? { + if (id == 0) { + return null + } + + val uri = ContactsContract.Data.CONTENT_URI + val projection = getContactProjection() + val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID} = ?" + val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, id.toString()) + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) + if (cursor?.moveToFirst() == true) { + val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME) ?: "" + val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: "" + val familyName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: "" val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: "" + val number = getContactNumber(id) val email = getContactEmail(id) - return Contact(id, name, number, photoUri, email, "") + val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) + return Contact(id, firstName, middleName, familyName, photoUri, number, email, accountName) } } finally { cursor?.close() @@ -156,10 +218,11 @@ class ContactsHelper(val activity: SimpleActivity) { } private fun getContactProjection() = arrayOf( - ContactsContract.CommonDataKinds.Phone.CONTACT_ID, - ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, - ContactsContract.CommonDataKinds.Phone.NUMBER, - ContactsContract.CommonDataKinds.Phone.PHOTO_URI, + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID, + ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, + ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, + ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, + ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI, ContactsContract.RawContacts.ACCOUNT_NAME ) 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 50264391..24926acb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -3,7 +3,8 @@ package com.simplemobiletools.contacts.models import com.simplemobiletools.commons.helpers.SORT_BY_NUMBER import com.simplemobiletools.commons.helpers.SORT_DESCENDING -data class Contact(val id: Int, var name: String, var number: String, var photoUri: String, var email: String, var source: String) : Comparable { +data class Contact(val id: Int, var firstName: String, var middleName: String, var familyName: String, var photoUri: String, var number: String, + var email: String, var source: String) : Comparable { companion object { var sorting: Int = 0 } @@ -11,12 +12,12 @@ data class Contact(val id: Int, var name: String, var number: String, var photoU override fun compareTo(other: Contact): Int { var result = when { (sorting and SORT_BY_NUMBER != 0) -> number.toLowerCase().compareTo(other.number.toLowerCase()) - else -> if (name.first().isLetter() && !other.name.first().isLetter()) { + else -> if (firstName.firstOrNull()?.isLetter() == true && other.firstName.firstOrNull()?.isLetter() == false) { -1 - } else if (!name.first().isLetter() && other.name.first().isLetter()) { + } else if (firstName.firstOrNull()?.isLetter() == false && other.firstName.firstOrNull()?.isLetter() == true) { 1 } else { - name.toLowerCase().compareTo(other.name.toLowerCase()) + firstName.toLowerCase().compareTo(other.firstName.toLowerCase()) } } @@ -29,6 +30,14 @@ data class Contact(val id: Int, var name: String, var number: String, var photoU fun getBubbleText() = when { sorting and SORT_BY_NUMBER != 0 -> number - else -> name + else -> firstName + } + + fun getFullName(): String { + var name = firstName + if (middleName.isNotEmpty()) { + name += " $middleName" + } + return "$name $familyName".trim() } }