Created
November 2, 2019 18:15
-
-
Save Kpeved/9ac0630379e38a7973910f55f5517dab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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