fix #40, try harder at fetching contact name at Recents

This commit is contained in:
tibbi 2020-05-30 23:08:16 +02:00
parent 2246e47a9c
commit a7d36936d7
2 changed files with 86 additions and 54 deletions

View File

@ -56,6 +56,6 @@ android {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:5.28.21' implementation 'com.simplemobiletools:commons:5.28.25'
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a' implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'
} }

View File

@ -4,23 +4,36 @@ import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.provider.CallLog.Calls import android.provider.CallLog.Calls
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.commons.helpers.getQuestionMarks
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.extensions.getAvailableSIMCardLabels import com.simplemobiletools.dialer.extensions.getAvailableSIMCardLabels
import com.simplemobiletools.dialer.models.RecentCall import com.simplemobiletools.dialer.models.RecentCall
class RecentsHelper(private val context: Context) { class RecentsHelper(private val context: Context) {
private val COMPARABLE_PHONE_NUMBER_LENGTH = 9
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
fun getRecentCalls(callback: (ArrayList<RecentCall>) -> Unit) { fun getRecentCalls(callback: (ArrayList<RecentCall>) -> Unit) {
val privateCursor = context.getMyContactsContentProviderCursorLoader().loadInBackground()
ensureBackgroundThread { ensureBackgroundThread {
var recentCalls = ArrayList<RecentCall>()
if (!context.hasPermission(PERMISSION_READ_CALL_LOG)) { if (!context.hasPermission(PERMISSION_READ_CALL_LOG)) {
callback(recentCalls) callback(ArrayList())
return@ensureBackgroundThread return@ensureBackgroundThread
} }
SimpleContactsHelper(context).getAvailableContacts(false) { contacts ->
val privateContacts = MyContactsContentProvider.getSimpleContacts(context, privateCursor)
if (privateContacts.isNotEmpty()) {
contacts.addAll(privateContacts)
}
getRecents(contacts, callback)
}
}
}
private fun getRecents(contacts: ArrayList<SimpleContact>, callback: (ArrayList<RecentCall>) -> Unit) {
var recentCalls = ArrayList<RecentCall>()
val uri = Calls.CONTENT_URI val uri = Calls.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
Calls._ID, Calls._ID,
@ -39,8 +52,8 @@ class RecentsHelper(private val context: Context) {
} }
val sortOrder = "${Calls._ID} DESC LIMIT 100" val sortOrder = "${Calls._ID} DESC LIMIT 100"
var previousRecentCallFrom = "" var previousRecentCallFrom = ""
val contactsNumbersMap = HashMap<String, String>()
context.queryCursor(uri, projection, sortOrder = sortOrder, showErrors = true) { cursor -> context.queryCursor(uri, projection, sortOrder = sortOrder, showErrors = true) { cursor ->
val id = cursor.getIntValue(Calls._ID) val id = cursor.getIntValue(Calls._ID)
val number = cursor.getStringValue(Calls.NUMBER) val number = cursor.getStringValue(Calls.NUMBER)
@ -49,6 +62,26 @@ class RecentsHelper(private val context: Context) {
name = number 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.phoneNumber.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
}?.name ?: number
}
}
}
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)
@ -58,7 +91,7 @@ class RecentsHelper(private val context: Context) {
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 3 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 ("$number$name" != previousRecentCallFrom) { if ("$number$name" != previousRecentCallFrom) {
recentCalls.add(recentCall) recentCalls.add(recentCall)
} else { } else {
@ -72,7 +105,6 @@ class RecentsHelper(private val context: Context) {
recentCalls = recentCalls.filter { !context.isNumberBlocked(it.phoneNumber, blockedNumbers) }.toMutableList() as ArrayList<RecentCall> recentCalls = recentCalls.filter { !context.isNumberBlocked(it.phoneNumber, blockedNumbers) }.toMutableList() as ArrayList<RecentCall>
callback(recentCalls) callback(recentCalls)
} }
}
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
fun removeRecentCalls(ids: ArrayList<Int>, callback: () -> Unit) { fun removeRecentCalls(ids: ArrayList<Int>, callback: () -> Unit) {