cache participant fetching at the mainscreen to speed it up

This commit is contained in:
tibbi
2020-04-09 18:35:01 +02:00
parent 0984449a76
commit 5185a98ca9
2 changed files with 17 additions and 5 deletions

View File

@@ -61,7 +61,7 @@ class ThreadActivity : SimpleActivity() {
ensureBackgroundThread { ensureBackgroundThread {
messages = getMessages(threadId) messages = getMessages(threadId)
participants = if (messages.isEmpty()) { participants = if (messages.isEmpty()) {
getThreadParticipants(threadId) getThreadParticipants(threadId, null)
} else { } else {
messages.first().participants messages.first().participants
} }

View File

@@ -98,16 +98,21 @@ fun Context.getMMS(threadId: Int? = null): ArrayList<Message> {
} }
val messages = ArrayList<Message>() val messages = ArrayList<Message>()
val contactsMap = HashMap<Int, Contact>()
queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor -> queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor ->
val id = cursor.getIntValue(Mms._ID) val id = cursor.getIntValue(Mms._ID)
val type = cursor.getIntValue(Mms.MESSAGE_BOX) val type = cursor.getIntValue(Mms.MESSAGE_BOX)
val date = cursor.getLongValue(Mms.DATE).toInt() val date = cursor.getLongValue(Mms.DATE).toInt()
val read = cursor.getIntValue(Mms.READ) == 1 val read = cursor.getIntValue(Mms.READ) == 1
val thread = cursor.getIntValue(Mms.THREAD_ID) val thread = cursor.getIntValue(Mms.THREAD_ID)
val participants = getThreadParticipants(thread) val participants = getThreadParticipants(thread, contactsMap)
val attachment = getMmsAttachment(id) val attachment = getMmsAttachment(id)
val message = Message(id, attachment?.text ?: "", type, participants, date, read, thread, attachment) val message = Message(id, attachment?.text ?: "", type, participants, date, read, thread, attachment)
messages.add(message) messages.add(message)
participants.forEach {
contactsMap.put(it.id, it)
}
} }
return messages return messages
} }
@@ -144,7 +149,7 @@ fun Context.getMmsAttachment(id: Int): MessageAttachment? {
return attachment return attachment
} }
fun Context.getThreadParticipants(threadId: Int): ArrayList<Contact> { fun Context.getThreadParticipants(threadId: Int, contactsMap: HashMap<Int, Contact>?): ArrayList<Contact> {
val uri = Uri.parse("${MmsSms.CONTENT_CONVERSATIONS_URI}?simple=true") val uri = Uri.parse("${MmsSms.CONTENT_CONVERSATIONS_URI}?simple=true")
val projection = arrayOf( val projection = arrayOf(
ThreadsColumns.RECIPIENT_IDS ThreadsColumns.RECIPIENT_IDS
@@ -158,9 +163,15 @@ fun Context.getThreadParticipants(threadId: Int): ArrayList<Contact> {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
val address = cursor.getStringValue(ThreadsColumns.RECIPIENT_IDS) val address = cursor.getStringValue(ThreadsColumns.RECIPIENT_IDS)
address.split(" ").filter { it.areDigitsOnly() }.forEach { address.split(" ").filter { it.areDigitsOnly() }.forEach {
val phoneNumber = getPhoneNumberFromAddressId(it.toInt()) val addressId = it.toInt()
if (contactsMap?.containsKey(addressId) == true) {
participants.add(contactsMap[addressId]!!)
return@forEach
}
val phoneNumber = getPhoneNumberFromAddressId(addressId)
val name = getNameFromPhoneNumber(phoneNumber) val name = getNameFromPhoneNumber(phoneNumber)
val contact = Contact(0, name, "", phoneNumber, false) val contact = Contact(addressId, name, "", phoneNumber, false)
participants.add(contact) participants.add(contact)
} }
} }
@@ -176,6 +187,7 @@ fun Context.getPhoneNumberFromAddressId(canonicalAddressId: Int): String {
val projection = arrayOf( val projection = arrayOf(
Mms.Addr.ADDRESS Mms.Addr.ADDRESS
) )
val selection = "${Mms._ID} = ?" val selection = "${Mms._ID} = ?"
val selectionArgs = arrayOf(canonicalAddressId.toString()) val selectionArgs = arrayOf(canonicalAddressId.toString())
try { try {