Skip to content

Instantly share code, notes, and snippets.

@Kpeved
Created November 2, 2019 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kpeved/9ac0630379e38a7973910f55f5517dab to your computer and use it in GitHub Desktop.
Save Kpeved/9ac0630379e38a7973910f55f5517dab to your computer and use it in GitHub Desktop.
private fun loadAllContacts(): Map<Long, ContactModel> {
val uri = ContactsContract.Data.CONTENT_URI
val selection = "${ContactsContract.Data.MIMETYPE} = ? "
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
val itemsMap = mutableMapOf<Long, ContactModel>()
contentResolver.query(
uri,
getContactProjection(),
selection,
selectionArgs,
getSortString()
).use { cursor ->
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getLongValue(ContactsContract.Data.RAW_CONTACT_ID)
val contactId = cursor.getLongValue(ContactsContract.Data.CONTACT_ID)
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)
val surname = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)
itemsMap[id] = ContactModel(
id = id,
contactId = contactId,
photoUri = photoUri,
firstName = firstName,
surname = surname,
fullName = name
)
} while (cursor.moveToNext())
}
}
return itemsMap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment