properly handle limiting Recent items in the call log on Android O+

This commit is contained in:
tibbi 2021-07-25 12:10:54 +02:00
parent 6973f86399
commit 3d144dfc60

View File

@ -1,8 +1,12 @@
package com.simplemobiletools.dialer.helpers package com.simplemobiletools.dialer.helpers
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.os.Build
import android.os.Bundle
import android.provider.CallLog.Calls import android.provider.CallLog.Calls
import androidx.annotation.RequiresApi
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.SimpleContact import com.simplemobiletools.commons.models.SimpleContact
@ -34,6 +38,7 @@ class RecentsHelper(private val context: Context) {
} }
} }
@RequiresApi(Build.VERSION_CODES.O)
private fun getRecents(contacts: ArrayList<SimpleContact>, groupSubsequentCalls: Boolean, callback: (ArrayList<RecentCall>) -> Unit) { private fun getRecents(contacts: ArrayList<SimpleContact>, groupSubsequentCalls: Boolean, callback: (ArrayList<RecentCall>) -> Unit) {
var recentCalls = ArrayList<RecentCall>() var recentCalls = ArrayList<RecentCall>()
var previousRecentCallFrom = "" var previousRecentCallFrom = ""
@ -55,56 +60,70 @@ class RecentsHelper(private val context: Context) {
numberToSimIDMap[it.phoneNumber] = it.id numberToSimIDMap[it.phoneNumber] = it.id
} }
val sortOrder = "${Calls._ID} DESC LIMIT 100" val cursor = if (isRPlus()) {
context.queryCursor(uri, projection, sortOrder = sortOrder, showErrors = true) { cursor -> val bundle = Bundle().apply {
val id = cursor.getIntValue(Calls._ID) putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(Calls._ID))
val number = cursor.getStringValue(Calls.NUMBER) putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING)
var name = cursor.getStringValue(Calls.CACHED_NAME) putInt(ContentResolver.QUERY_ARG_LIMIT, 20)
if (name == null || name.isEmpty()) {
name = number
} }
if (name == number) { context.contentResolver.query(uri, projection, bundle, null)
if (contactsNumbersMap.containsKey(number)) { } else {
name = contactsNumbersMap[number]!! val sortOrder = "${Calls._ID} DESC LIMIT 100"
} else { context.contentResolver.query(uri, projection, null, null, sortOrder)
val normalizedNumber = number.normalizePhoneNumber() }
if (normalizedNumber!!.length >= COMPARABLE_PHONE_NUMBER_LENGTH) {
name = contacts.firstOrNull { contact -> if (cursor?.moveToFirst() == true) {
val curNumber = contact.phoneNumbers.first().normalizePhoneNumber() do {
if (curNumber!!.length >= COMPARABLE_PHONE_NUMBER_LENGTH) { val id = cursor.getIntValue(Calls._ID)
if (curNumber.substring(curNumber.length - COMPARABLE_PHONE_NUMBER_LENGTH) == normalizedNumber.substring(normalizedNumber.length - COMPARABLE_PHONE_NUMBER_LENGTH)) { val number = cursor.getStringValue(Calls.NUMBER)
contactsNumbersMap[number] = contact.name var name = cursor.getStringValue(Calls.CACHED_NAME)
return@firstOrNull true if (name == null || name.isEmpty()) {
name = number
}
if (name == number) {
if (contactsNumbersMap.containsKey(number)) {
name = contactsNumbersMap[number]!!
} else {
val normalizedNumber = number.normalizePhoneNumber()
if (normalizedNumber!!.length >= COMPARABLE_PHONE_NUMBER_LENGTH) {
name = contacts.firstOrNull { contact ->
val curNumber = contact.phoneNumbers.first().normalizePhoneNumber()
if (curNumber!!.length >= COMPARABLE_PHONE_NUMBER_LENGTH) {
if (curNumber.substring(curNumber.length - COMPARABLE_PHONE_NUMBER_LENGTH) == normalizedNumber.substring(normalizedNumber.length - COMPARABLE_PHONE_NUMBER_LENGTH)) {
contactsNumbersMap[number] = contact.name
return@firstOrNull true
}
} }
} false
false }?.name ?: number
}?.name ?: number }
} }
} }
}
if (name.isEmpty()) { if (name.isEmpty()) {
name = context.getString(R.string.unknown) name = context.getString(R.string.unknown)
} }
val photoUri = cursor.getStringValue(Calls.CACHED_PHOTO_URI) ?: "" val photoUri = cursor.getStringValue(Calls.CACHED_PHOTO_URI) ?: ""
val startTS = (cursor.getLongValue(Calls.DATE) / 1000L).toInt() val startTS = (cursor.getLongValue(Calls.DATE) / 1000L).toInt()
val duration = cursor.getIntValue(Calls.DURATION) val duration = cursor.getIntValue(Calls.DURATION)
val type = cursor.getIntValue(Calls.TYPE) val type = cursor.getIntValue(Calls.TYPE)
val accountAddress = cursor.getStringValue("phone_account_address") val accountAddress = cursor.getStringValue("phone_account_address")
val simID = numberToSimIDMap[accountAddress] ?: 1 val simID = numberToSimIDMap[accountAddress] ?: 1
val neighbourIDs = ArrayList<Int>() val neighbourIDs = ArrayList<Int>()
val recentCall = RecentCall(id, number, name, photoUri, startTS, duration, type, neighbourIDs, simID) val recentCall = RecentCall(id, number, name, photoUri, startTS, duration, type, neighbourIDs, simID)
// if we have multiple missed calls from the same number, show it just once // if we have multiple missed calls from the same number, show it just once
if (!groupSubsequentCalls || "$number$name" != previousRecentCallFrom) { if (!groupSubsequentCalls || "$number$name" != previousRecentCallFrom) {
recentCalls.add(recentCall) recentCalls.add(recentCall)
} else { } else {
recentCalls.lastOrNull()?.neighbourIDs?.add(id) recentCalls.lastOrNull()?.neighbourIDs?.add(id)
} }
previousRecentCallFrom = "$number$name" previousRecentCallFrom = "$number$name"
} while (cursor.moveToNext())
} }
val blockedNumbers = context.getBlockedNumbers() val blockedNumbers = context.getBlockedNumbers()