From 1e54e6cde01bde5bbc3e05bcb221eec5813796c7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 7 Apr 2020 23:26:43 +0200 Subject: [PATCH] fetch only the wanted MMS messages at threads --- .../smsmessenger/extensions/Context.kt | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) 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 8286d7da..e82e2648 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -29,7 +29,7 @@ import kotlin.collections.ArrayList val Context.config: Config get() = Config.newInstance(applicationContext) -fun Context.getMessages(threadID: Int? = null): ArrayList { +fun Context.getMessages(threadId: Int? = null): ArrayList { val hasContactsPermission = hasPermission(PERMISSION_READ_CONTACTS) val uri = Sms.CONTENT_URI val projection = arrayOf( @@ -44,16 +44,16 @@ fun Context.getMessages(threadID: Int? = null): ArrayList { Sms.THREAD_ID ) - val selection = if (threadID == null) { + val selection = if (threadId == null) { "1 == 1) GROUP BY (${Sms.THREAD_ID}" } else { "${Sms.THREAD_ID} = ?" } - val selectionArgs = if (threadID == null) { + val selectionArgs = if (threadId == null) { null } else { - arrayOf(threadID.toString()) + arrayOf(threadId.toString()) } var messages = ArrayList() @@ -84,16 +84,16 @@ fun Context.getMessages(threadID: Int? = null): ArrayList { messages.add(message) } - messages.addAll(getMMS()) + messages.addAll(getMMS(threadId)) messages = messages.sortedByDescending { it.date }.toMutableList() as ArrayList - if (threadID == null) { + if (threadId == null) { messages = messages.distinctBy { it.thread }.toMutableList() as ArrayList } return messages } -fun Context.getMMS(): ArrayList { +fun Context.getMMS(threadId: Int? = null): ArrayList { val hasContactsPermission = hasPermission(PERMISSION_READ_CONTACTS) val uri = Mms.CONTENT_URI val projection = arrayOf( @@ -105,8 +105,20 @@ fun Context.getMMS(): ArrayList { Mms.THREAD_ID ) + val selection = if (threadId == null) { + null + } else { + "${Mms.THREAD_ID} = ?" + } + + val selectionArgs = if (threadId == null) { + null + } else { + arrayOf(threadId.toString()) + } + val messages = ArrayList() - queryCursor(uri, projection, showErrors = true) { cursor -> + queryCursor(uri, projection, selection, selectionArgs, showErrors = true) { cursor -> val id = cursor.getIntValue(Mms._ID) val subject = cursor.getStringValue(Mms.SUBJECT) ?: "" val type = cursor.getIntValue(Mms.MESSAGE_BOX)