From c3ac30d47ee9e48baf1f24c60aa48dbda761c163 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 30 Nov 2018 18:00:32 +0100 Subject: [PATCH] improve the name detection at Recents tab --- .../contacts/pro/activities/MainActivity.kt | 32 +++++++++---------- .../contacts/pro/helpers/ContactsHelper.kt | 2 +- .../contacts/pro/models/Contact.kt | 9 +++++- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt index be011bc2..ec08ce56 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt @@ -519,39 +519,39 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { return@getContacts } + val contacts = it if (refreshTabsMask and CONTACTS_TAB_MASK != 0) { - contacts_fragment?.refreshContacts(it) + contacts_fragment?.refreshContacts(contacts) } if (refreshTabsMask and FAVORITES_TAB_MASK != 0) { - favorites_fragment?.refreshContacts(it) + favorites_fragment?.refreshContacts(contacts) } if (refreshTabsMask and RECENTS_TAB_MASK != 0) { - recents_fragment?.refreshContacts(it) + recents_fragment?.refreshContacts(contacts) } if (refreshTabsMask and GROUPS_TAB_MASK != 0) { if (refreshTabsMask == GROUPS_TAB_MASK) { groups_fragment.skipHashComparing = true } - groups_fragment?.refreshContacts(it) + groups_fragment?.refreshContacts(contacts) } - } - if (refreshTabsMask and RECENTS_TAB_MASK != 0) { - ContactsHelper(this).getRecents { - val localContacts = LocalContactsHelper(applicationContext).getAllContacts() - it.filter { it.name == null }.forEach { - val namelessCall = it - val localContact = localContacts.firstOrNull { it.doesContainPhoneNumber(namelessCall.number) } - if (localContact != null) { - it.name = localContact.getNameToDisplay() + if (refreshTabsMask and RECENTS_TAB_MASK != 0) { + ContactsHelper(this).getRecents { + it.filter { it.name == null }.forEach { + val namelessCall = it + val contact = contacts.firstOrNull { it.doesContainPhoneNumber(namelessCall.number) } + if (contact != null) { + it.name = contact.getNameToDisplay() + } } - } - runOnUiThread { - recents_fragment?.updateRecentCalls(it) + runOnUiThread { + recents_fragment?.updateRecentCalls(it) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index acc5127d..8a14f930 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -257,7 +257,7 @@ class ContactsHelper(val context: Context) { do { val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID) val number = cursor.getStringValue(CommonDataKinds.Phone.NUMBER) ?: continue - val normalizedNumber = cursor.getStringValue(CommonDataKinds.Phone.NORMALIZED_NUMBER) ?: "" + val normalizedNumber = cursor.getStringValue(CommonDataKinds.Phone.NORMALIZED_NUMBER) ?: number.normalizeNumber() val type = cursor.getIntValue(CommonDataKinds.Phone.TYPE) val label = cursor.getStringValue(CommonDataKinds.Phone.LABEL) ?: "" diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/Contact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/Contact.kt index 50c27d86..7eb0d57e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/Contact.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.contacts.pro.models import android.graphics.Bitmap +import android.telephony.PhoneNumberUtils import com.simplemobiletools.commons.extensions.normalizeString import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME @@ -128,7 +129,13 @@ data class Contact(var id: Int, var prefix: String, var firstName: String, var m fun doesContainPhoneNumber(text: String): Boolean { return if (text.isNotEmpty()) { - phoneNumbers.any { it.value.contains(text) || it.normalizedNumber?.contains(text.normalizeNumber()) == true || it.value.normalizeNumber().contains(text.normalizeNumber()) } + val normalizedText = text.normalizeNumber() + phoneNumbers.any { + PhoneNumberUtils.compare(it.normalizedNumber, normalizedText) || + it.value.contains(text) || + it.normalizedNumber?.contains(normalizedText) == true || + it.value.normalizeNumber().contains(normalizedText) + } } else { false }