improving blocked number checking performance

This commit is contained in:
tibbi 2020-05-23 17:15:27 +02:00
parent b90110b108
commit b5cd6aafbc
4 changed files with 10 additions and 17 deletions

View File

@ -56,7 +56,7 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:5.28.14'
implementation 'com.simplemobiletools:commons:5.28.18'
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.klinkerapps:android-smsmms:5.2.6'
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'

View File

@ -48,6 +48,7 @@ fun Context.getMessages(threadId: Int): ArrayList<Message> {
val sortOrder = "${Sms._ID} DESC LIMIT 100"
val blockStatus = HashMap<String, Boolean>()
val blockedNumbers = getBlockedNumbers()
var messages = ArrayList<Message>()
queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor ->
val senderNumber = cursor.getStringValue(Sms.ADDRESS) ?: return@queryCursor
@ -55,7 +56,7 @@ fun Context.getMessages(threadId: Int): ArrayList<Message> {
val isNumberBlocked = if (blockStatus.containsKey(senderNumber)) {
blockStatus[senderNumber]!!
} else {
val isBlocked = isNumberBlocked(senderNumber)
val isBlocked = isNumberBlocked(senderNumber, blockedNumbers)
blockStatus[senderNumber] = isBlocked
isBlocked
}
@ -148,7 +149,7 @@ fun Context.getMMS(threadId: Int? = null, sortOrder: String? = null): ArrayList<
messages.add(message)
participants.forEach {
contactsMap.put(it.rawId, it)
contactsMap[it.rawId] = it
}
}
@ -189,6 +190,7 @@ fun Context.getConversations(): ArrayList<Conversation> {
val conversations = ArrayList<Conversation>()
val simpleContactHelper = SimpleContactsHelper(this)
val blockedNumbers = getBlockedNumbers()
queryCursor(uri, projection, selection, selectionArgs, sortOrder, true) { cursor ->
val id = cursor.getIntValue(Threads._ID)
var snippet = cursor.getStringValue(Threads.SNIPPET) ?: ""
@ -204,7 +206,7 @@ fun Context.getConversations(): ArrayList<Conversation> {
val rawIds = cursor.getStringValue(Threads.RECIPIENT_IDS)
val recipientIds = rawIds.split(" ").filter { it.areDigitsOnly() }.map { it.toInt() }.toMutableList()
val phoneNumbers = getThreadPhoneNumbers(recipientIds)
if (phoneNumbers.any { isNumberBlocked(it) }) {
if (phoneNumbers.any { isNumberBlocked(it, blockedNumbers) }) {
return@queryCursor
}
@ -378,13 +380,14 @@ fun Context.getSuggestedContacts(privateContacts: ArrayList<SimpleContact>): Arr
val selection = "1 == 1) GROUP BY (${Sms.ADDRESS}"
val selectionArgs = null
val sortOrder = "${Sms.DATE} DESC LIMIT 20"
val blockedNumbers = getBlockedNumbers()
queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor ->
val senderNumber = cursor.getStringValue(Sms.ADDRESS) ?: return@queryCursor
val namePhoto = getNameAndPhotoFromPhoneNumber(senderNumber)
var senderName = namePhoto?.name ?: ""
var photoUri = namePhoto?.photoUri ?: ""
if (namePhoto == null || isNumberBlocked(senderNumber)) {
if (namePhoto == null || isNumberBlocked(senderNumber, blockedNumbers)) {
return@queryCursor
} else if (namePhoto.name == senderNumber) {
if (privateContacts.isNotEmpty()) {
@ -499,16 +502,6 @@ fun Context.getThreadId(addresses: Set<String>): Long {
}
}
fun Context.isNumberBlocked(number: String): Boolean {
if (!isNougatPlus()) {
return false
}
val blockedNumbers = getBlockedNumbers()
val numberToCompare = number.trimToComparableNumber()
return blockedNumbers.map { it.numberToCompare }.contains(numberToCompare) || blockedNumbers.map { it.number }.contains(numberToCompare)
}
@SuppressLint("NewApi")
fun Context.showReceivedMessageNotification(address: String, body: String, threadID: Int, bitmap: Bitmap?, messageId: Int, isMMS: Boolean) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

View File

@ -3,10 +3,10 @@ package com.simplemobiletools.smsmessenger.receivers
import android.content.Context
import android.net.Uri
import com.bumptech.glide.Glide
import com.simplemobiletools.commons.extensions.isNumberBlocked
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.extensions.getLatestMMS
import com.simplemobiletools.smsmessenger.extensions.isNumberBlocked
import com.simplemobiletools.smsmessenger.extensions.showReceivedMessageNotification
// more info at https://github.com/klinker41/android-smsmms

View File

@ -4,9 +4,9 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.provider.Telephony
import com.simplemobiletools.commons.extensions.isNumberBlocked
import com.simplemobiletools.smsmessenger.extensions.getThreadId
import com.simplemobiletools.smsmessenger.extensions.insertNewSMS
import com.simplemobiletools.smsmessenger.extensions.isNumberBlocked
import com.simplemobiletools.smsmessenger.extensions.showReceivedMessageNotification
import com.simplemobiletools.smsmessenger.helpers.refreshMessages