From 2f29f9510e4e80d37c8436dc42965ee27e13b7cb Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 8 May 2020 21:43:23 +0200 Subject: [PATCH] add both RawID and ContactID into SimpleContact model --- build.gradle | 2 +- .../commons/helpers/ContactsHelper.kt | 19 +++++++++++-------- .../commons/models/SimpleContact.kt | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 4576b999e..28f2bff20 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { propMinSdkVersion = 21 propTargetSdkVersion = propCompileSdkVersion propVersionCode = 1 - propVersionName = '5.27.13' + propVersionName = '5.27.14' kotlin_version = '1.3.72' } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/ContactsHelper.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/ContactsHelper.kt index a8fa6595d..c6252a835 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/ContactsHelper.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/ContactsHelper.kt @@ -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() 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() 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 diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleContact.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleContact.kt index 5147f1312..b80025c9e 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleContact.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleContact.kt @@ -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 { +data class SimpleContact(val rawId: Int, val contactId: Int, var name: String, var photoUri: String, var phoneNumber: String) : Comparable { override fun compareTo(other: SimpleContact): Int { val firstString = name.normalizeString() val secondString = other.name.normalizeString()