Use lookup key to load contacts

This commit is contained in:
Naveen 2022-11-06 21:00:35 +05:30
parent 6f58903cf2
commit b6407dc49b
2 changed files with 12 additions and 12 deletions

View File

@ -853,7 +853,7 @@ class ThreadActivity : SimpleActivity() {
private fun launchPickContactIntent() { private fun launchPickContactIntent() {
Intent(Intent.ACTION_PICK).apply { Intent(Intent.ACTION_PICK).apply {
type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE type = ContactsContract.Contacts.CONTENT_TYPE
launchActivityForResult(this, PICK_CONTACT_INTENT) launchActivityForResult(this, PICK_CONTACT_INTENT)
} }
} }

View File

@ -20,24 +20,24 @@ class ContactsHelper(val context: Context) {
private var displayContactSources = ArrayList<String>() private var displayContactSources = ArrayList<String>()
fun getContactFromUri(uri: Uri): Contact? { fun getContactFromUri(uri: Uri): Contact? {
val projection = arrayOf(Data.RAW_CONTACT_ID) val key = getLookupKeyFromUri(uri) ?: return null
val cursor = context.contentResolver.query(uri, projection, null, null, null) return getContactWithLookupKey(key)
}
private fun getLookupKeyFromUri(lookupUri: Uri): String? {
val projection = arrayOf(ContactsContract.Contacts.LOOKUP_KEY)
val cursor = context.contentResolver.query(lookupUri, projection, null, null, null)
cursor?.use { cursor?.use {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
val id = cursor.getIntValue(Data.RAW_CONTACT_ID) return cursor.getStringValue(ContactsContract.Contacts.LOOKUP_KEY)
return getContactWithId(id)
} }
} }
return null return null
} }
private fun getContactWithId(id: Int): Contact? { private fun getContactWithLookupKey(key: String): Contact? {
if (id == 0) { val selection = "(${Data.MIMETYPE} = ? OR ${Data.MIMETYPE} = ?) AND ${Data.LOOKUP_KEY} = ?"
return null val selectionArgs = arrayOf(StructuredName.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE, key)
}
val selection = "(${Data.MIMETYPE} = ? OR ${Data.MIMETYPE} = ?) AND ${Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(StructuredName.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE, id.toString())
return parseContactCursor(selection, selectionArgs) return parseContactCursor(selection, selectionArgs)
} }