fix #103, make sure private contact names are shown at group conversations

This commit is contained in:
tibbi 2020-12-06 16:36:31 +01:00
parent 8b17b8a13e
commit 75fb84f9d4
2 changed files with 16 additions and 18 deletions

View File

@ -185,20 +185,8 @@ class MainActivity : SimpleActivity() {
private fun getNewConversations(cachedConversations: ArrayList<Conversation>) {
val privateCursor = getMyContactsCursor().loadInBackground()
ensureBackgroundThread {
val conversations = getConversations()
// check if no message came from a privately stored contact in Simple Contacts
val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor)
if (privateContacts.isNotEmpty()) {
conversations.filter { it.title == it.phoneNumber }.forEach { conversation ->
privateContacts.forEach { contact ->
if (contact.doesContainPhoneNumber(conversation.phoneNumber)) {
conversation.title = contact.name
conversation.photoUri = contact.photoUri
}
}
}
}
val conversations = getConversations(privateContacts = privateContacts)
runOnUiThread {
setupConversations(conversations)

View File

@ -185,7 +185,7 @@ fun Context.getMMSSender(msgId: Int): String {
return ""
}
fun Context.getConversations(threadId: Long? = null): ArrayList<Conversation> {
fun Context.getConversations(threadId: Long? = null, privateContacts: ArrayList<SimpleContact> = ArrayList()): ArrayList<Conversation> {
val uri = Uri.parse("${Threads.CONTENT_URI}?simple=true")
val projection = arrayOf(
Threads._ID,
@ -226,7 +226,7 @@ fun Context.getConversations(threadId: Long? = null): ArrayList<Conversation> {
return@queryCursor
}
val names = getThreadContactNames(phoneNumbers)
val names = getThreadContactNames(phoneNumbers, privateContacts)
val title = TextUtils.join(", ", names.toTypedArray())
val photoUri = if (phoneNumbers.size == 1) simpleContactHelper.getPhotoUriFromPhoneNumber(phoneNumbers.first()) else ""
val isGroupConversation = phoneNumbers.size > 1
@ -356,10 +356,20 @@ fun Context.getThreadPhoneNumbers(recipientIds: List<Int>): ArrayList<String> {
return numbers
}
fun Context.getThreadContactNames(phoneNumbers: List<String>): ArrayList<String> {
fun Context.getThreadContactNames(phoneNumbers: List<String>, privateContacts: ArrayList<SimpleContact>): ArrayList<String> {
val names = ArrayList<String>()
phoneNumbers.forEach {
names.add(SimpleContactsHelper(this).getNameFromPhoneNumber(it))
phoneNumbers.forEach { number ->
val name = SimpleContactsHelper(this).getNameFromPhoneNumber(number)
if (name != number) {
names.add(name)
} else {
val privateContact = privateContacts.firstOrNull { it.doesContainPhoneNumber(number) }
if (privateContact == null) {
names.add(name)
} else {
names.add(privateContact.name)
}
}
}
return names
}