From deec462b9c96201fc53f9baf1ccda3b135dcb295 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 21 Jun 2022 01:50:48 +0530 Subject: [PATCH 1/5] Properly limit recent calls using `LIMIT_PARAM_KEY` --- .../dialer/helpers/RecentsHelper.kt | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt index 272623a9..46866a52 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt @@ -15,6 +15,7 @@ import com.simplemobiletools.dialer.models.RecentCall class RecentsHelper(private val context: Context) { private val COMPARABLE_PHONE_NUMBER_LENGTH = 9 + private val QUERY_LIMIT = "100" @SuppressLint("MissingPermission") fun getRecentCalls(groupSubsequentCalls: Boolean, callback: (ArrayList) -> Unit) { @@ -44,7 +45,10 @@ class RecentsHelper(private val context: Context) { val contactsNumbersMap = HashMap() val contactPhotosMap = HashMap() - val uri = Calls.CONTENT_URI + val uri = Calls.CONTENT_URI.buildUpon() + .appendQueryParameter(Calls.LIMIT_PARAM_KEY, QUERY_LIMIT) + .build() + val projection = arrayOf( Calls._ID, Calls.NUMBER, @@ -61,18 +65,11 @@ class RecentsHelper(private val context: Context) { accountIdToSimIDMap[it.handle.id] = it.id } - val cursor = if (isRPlus()) { - val bundle = Bundle().apply { - putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(Calls._ID)) - putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING) - putInt(ContentResolver.QUERY_ARG_LIMIT, 100) - } - - context.contentResolver.query(uri, projection, bundle, null) - } else { - val sortOrder = "${Calls._ID} DESC LIMIT 100" - context.contentResolver.query(uri, projection, null, null, sortOrder) + val bundle = Bundle().apply { + putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(Calls._ID)) + putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING) } + val cursor = context.contentResolver.query(uri, projection, bundle, null) val contactsWithMultipleNumbers = contacts.filter { it.phoneNumbers.size > 1 } val numbersToContactIDMap = HashMap() From b70ccfa18376ab686149595741bd5d564fc83634 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 21 Jun 2022 01:52:12 +0530 Subject: [PATCH 2/5] Close the cursor after use --- .../kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt index 46866a52..5179ee4a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt @@ -163,6 +163,7 @@ class RecentsHelper(private val context: Context) { previousRecentCallFrom = "$number$name$simID" } while (cursor.moveToNext()) } + cursor?.close() val blockedNumbers = context.getBlockedNumbers() recentCalls = recentCalls.filter { !context.isNumberBlocked(it.phoneNumber, blockedNumbers) }.toMutableList() as ArrayList From 91225ba802384d85a987681cb668897607ede0e4 Mon Sep 17 00:00:00 2001 From: Naveen Date: Wed, 22 Jun 2022 00:53:29 +0530 Subject: [PATCH 3/5] Revert to backward compatible query --- .../dialer/helpers/RecentsHelper.kt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt index 5179ee4a..4897096c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt @@ -65,11 +65,17 @@ class RecentsHelper(private val context: Context) { accountIdToSimIDMap[it.handle.id] = it.id } - val bundle = Bundle().apply { - putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(Calls._ID)) - putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING) + val cursor = if (isRPlus()) { + val bundle = Bundle().apply { + putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(Calls._ID)) + putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING) + } + + context.contentResolver.query(uri, projection, bundle, null) + } else { + val sortOrder = "${Calls._ID} DESC" + context.contentResolver.query(uri, projection, null, null, sortOrder) } - val cursor = context.contentResolver.query(uri, projection, bundle, null) val contactsWithMultipleNumbers = contacts.filter { it.phoneNumbers.size > 1 } val numbersToContactIDMap = HashMap() From d6025e59624641f016412e08bdbb6c7a64b11512 Mon Sep 17 00:00:00 2001 From: Naveen Date: Wed, 22 Jun 2022 01:26:22 +0530 Subject: [PATCH 4/5] Use SQL style `LIMIT` query before nougat --- .../dialer/helpers/RecentsHelper.kt | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt index 4897096c..46a0fd87 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt @@ -1,9 +1,7 @@ package com.simplemobiletools.dialer.helpers import android.annotation.SuppressLint -import android.content.ContentResolver import android.content.Context -import android.os.Bundle import android.provider.CallLog.Calls import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* @@ -45,10 +43,7 @@ class RecentsHelper(private val context: Context) { val contactsNumbersMap = HashMap() val contactPhotosMap = HashMap() - val uri = Calls.CONTENT_URI.buildUpon() - .appendQueryParameter(Calls.LIMIT_PARAM_KEY, QUERY_LIMIT) - .build() - + val uri = Calls.CONTENT_URI val projection = arrayOf( Calls._ID, Calls.NUMBER, @@ -65,15 +60,15 @@ class RecentsHelper(private val context: Context) { accountIdToSimIDMap[it.handle.id] = it.id } - val cursor = if (isRPlus()) { - val bundle = Bundle().apply { - putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(Calls._ID)) - putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING) - } - - context.contentResolver.query(uri, projection, bundle, null) - } else { + val cursor = if (isNougatPlus()) { + // https://issuetracker.google.com/issues/175198972?pli=1#comment6 + val limitedUri = uri.buildUpon() + .appendQueryParameter(Calls.LIMIT_PARAM_KEY, QUERY_LIMIT) + .build() val sortOrder = "${Calls._ID} DESC" + context.contentResolver.query(limitedUri, projection, null, null, sortOrder) + } else { + val sortOrder = "${Calls._ID} DESC LIMIT $QUERY_LIMIT" context.contentResolver.query(uri, projection, null, null, sortOrder) } From d31bdd92a1bf566dde8191435109e3cae0db5184 Mon Sep 17 00:00:00 2001 From: Tibor Kaputa Date: Tue, 21 Jun 2022 23:06:08 +0200 Subject: [PATCH 5/5] lets change the fetched item count to 200 --- .../com/simplemobiletools/dialer/helpers/RecentsHelper.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt index 46a0fd87..f6e2a347 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt @@ -13,7 +13,7 @@ import com.simplemobiletools.dialer.models.RecentCall class RecentsHelper(private val context: Context) { private val COMPARABLE_PHONE_NUMBER_LENGTH = 9 - private val QUERY_LIMIT = "100" + private val QUERY_LIMIT = "200" @SuppressLint("MissingPermission") fun getRecentCalls(groupSubsequentCalls: Boolean, callback: (ArrayList) -> Unit) {