diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewConversationActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewConversationActivity.kt index dcc5f165..4f5b1fea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewConversationActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/NewConversationActivity.kt @@ -26,6 +26,7 @@ import kotlin.collections.ArrayList class NewConversationActivity : SimpleActivity() { private var allContacts = ArrayList() + private var privateContacts = ArrayList() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -104,11 +105,9 @@ class NewConversationActivity : SimpleActivity() { private fun fetchContacts() { fillSuggestedContacts { - val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() SimpleContactsHelper(this).getAvailableContacts(false) { allContacts = it - val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) if (privateContacts.isNotEmpty()) { allContacts.addAll(privateContacts) allContacts.sort() @@ -143,8 +142,10 @@ class NewConversationActivity : SimpleActivity() { } private fun fillSuggestedContacts(callback: () -> Unit) { + val privateCursor = getMyContactsContentProviderCursorLoader().loadInBackground() ensureBackgroundThread { - val suggestions = getSuggestedContacts() + privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) + val suggestions = getSuggestedContacts(privateContacts) runOnUiThread { suggestions_holder.removeAllViews() if (suggestions.isEmpty()) { diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 31606e9d..d7f0ea8c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -367,7 +367,7 @@ fun Context.getPhoneNumberFromAddressId(canonicalAddressId: Int): String { return "" } -fun Context.getSuggestedContacts(): ArrayList { +fun Context.getSuggestedContacts(privateContacts: ArrayList): ArrayList { val contacts = ArrayList() val uri = Sms.CONTENT_URI val projection = arrayOf( @@ -381,11 +381,22 @@ fun Context.getSuggestedContacts(): ArrayList { queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor -> val senderNumber = cursor.getStringValue(Sms.ADDRESS) ?: return@queryCursor val namePhoto = getNameAndPhotoFromPhoneNumber(senderNumber) - if (namePhoto == null || namePhoto.name == senderNumber || isNumberBlocked(senderNumber)) { + var senderName = namePhoto?.name ?: "" + if (namePhoto == null || isNumberBlocked(senderNumber)) { return@queryCursor + } else if (namePhoto.name == senderNumber) { + if (privateContacts.isNotEmpty()) { + val privateContact = privateContacts.firstOrNull { it.phoneNumber == senderNumber } + if (privateContact != null) { + senderName = privateContact.name + } else { + return@queryCursor + } + } else { + return@queryCursor + } } - val senderName = namePhoto.name val photoUri = namePhoto.photoUri ?: "" val contact = SimpleContact(0, 0, senderName, photoUri, senderNumber) if (!contacts.map { it.phoneNumber.trimToComparableNumber() }.contains(senderNumber.trimToComparableNumber())) {