add both RawID and ContactID into SimpleContact model
This commit is contained in:
parent
198c0c1fb3
commit
2f29f9510e
3 changed files with 13 additions and 10 deletions
|
@ -7,7 +7,7 @@ buildscript {
|
|||
propMinSdkVersion = 21
|
||||
propTargetSdkVersion = propCompileSdkVersion
|
||||
propVersionCode = 1
|
||||
propVersionName = '5.27.13'
|
||||
propVersionName = '5.27.14'
|
||||
kotlin_version = '1.3.72'
|
||||
}
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ class ContactsHelper(val context: Context) {
|
|||
val names = getContactNames()
|
||||
var allContacts = getContactPhoneNumbers()
|
||||
allContacts.forEach {
|
||||
val contactId = it.id
|
||||
val contact = names.firstOrNull { it.id == contactId }
|
||||
val contactId = it.rawId
|
||||
val contact = names.firstOrNull { it.rawId == contactId }
|
||||
val name = contact?.name
|
||||
if (name != null) {
|
||||
it.name = name
|
||||
|
@ -57,6 +57,7 @@ class ContactsHelper(val context: Context) {
|
|||
val contacts = ArrayList<SimpleContact>()
|
||||
val uri = ContactsContract.Data.CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
ContactsContract.Data.RAW_CONTACT_ID,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
StructuredName.PREFIX,
|
||||
StructuredName.GIVEN_NAME,
|
||||
|
@ -76,7 +77,8 @@ class ContactsHelper(val context: Context) {
|
|||
)
|
||||
|
||||
context.queryCursor(uri, projection, selection, selectionArgs) { cursor ->
|
||||
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||
val rawId = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID)
|
||||
val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||
val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE)
|
||||
val photoUri = cursor.getStringValue(StructuredName.PHOTO_THUMBNAIL_URI) ?: ""
|
||||
val isPerson = mimetype == StructuredName.CONTENT_ITEM_TYPE
|
||||
|
@ -89,7 +91,7 @@ class ContactsHelper(val context: Context) {
|
|||
if (firstName.isNotEmpty() || middleName.isNotEmpty() || familyName.isNotEmpty()) {
|
||||
val names = arrayOf(prefix, firstName, middleName, familyName, suffix).filter { it.isNotEmpty() }
|
||||
val fullName = TextUtils.join(" ", names)
|
||||
val contact = SimpleContact(id, fullName, photoUri, "")
|
||||
val contact = SimpleContact(rawId, contactId, fullName, photoUri, "")
|
||||
contacts.add(contact)
|
||||
}
|
||||
}
|
||||
|
@ -100,7 +102,7 @@ class ContactsHelper(val context: Context) {
|
|||
val jobTitle = cursor.getStringValue(Organization.TITLE) ?: ""
|
||||
if (company.isNotEmpty() || jobTitle.isNotEmpty()) {
|
||||
val fullName = "$company $jobTitle".trim()
|
||||
val contact = SimpleContact(id, fullName, photoUri, "")
|
||||
val contact = SimpleContact(rawId, contactId, fullName, photoUri, "")
|
||||
contacts.add(contact)
|
||||
}
|
||||
}
|
||||
|
@ -112,22 +114,23 @@ class ContactsHelper(val context: Context) {
|
|||
val contacts = ArrayList<SimpleContact>()
|
||||
val uri = CommonDataKinds.Phone.CONTENT_URI
|
||||
val projection = arrayOf(
|
||||
ContactsContract.Data.RAW_CONTACT_ID,
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
CommonDataKinds.Phone.NORMALIZED_NUMBER
|
||||
)
|
||||
|
||||
context.queryCursor(uri, projection) { cursor ->
|
||||
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||
val rawId = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID)
|
||||
val contactId = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
|
||||
val phoneNumber = cursor.getStringValue(CommonDataKinds.Phone.NORMALIZED_NUMBER)
|
||||
if (phoneNumber != null) {
|
||||
val contact = SimpleContact(id, "", "", phoneNumber)
|
||||
val contact = SimpleContact(rawId, contactId, "", "", phoneNumber)
|
||||
contacts.add(contact)
|
||||
}
|
||||
}
|
||||
return contacts
|
||||
}
|
||||
|
||||
|
||||
fun getNameFromPhoneNumber(number: String): String {
|
||||
if (!context.hasPermission(PERMISSION_READ_CONTACTS)) {
|
||||
return number
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.simplemobiletools.commons.models
|
|||
|
||||
import com.simplemobiletools.commons.extensions.normalizeString
|
||||
|
||||
data class SimpleContact(val id: Int, var name: String, var photoUri: String, var phoneNumber: String) : Comparable<SimpleContact> {
|
||||
data class SimpleContact(val rawId: Int, val contactId: Int, var name: String, var photoUri: String, var phoneNumber: String) : Comparable<SimpleContact> {
|
||||
override fun compareTo(other: SimpleContact): Int {
|
||||
val firstString = name.normalizeString()
|
||||
val secondString = other.name.normalizeString()
|
||||
|
|
Loading…
Reference in a new issue