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() {
Intent(Intent.ACTION_PICK).apply {
type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
type = ContactsContract.Contacts.CONTENT_TYPE
launchActivityForResult(this, PICK_CONTACT_INTENT)
}
}

View File

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