From 6fae60381fecc34af9eca7285e7a72e26b7ce862 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 30 Nov 2018 11:34:39 +0100 Subject: [PATCH] filter out blocked numbers earlier, right at fetching --- .../contacts/pro/activities/MainActivity.kt | 45 +------------------ .../contacts/pro/helpers/ContactsHelper.kt | 37 +++++++++++++++ 2 files changed, 39 insertions(+), 43 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt index 484a37cd..be011bc2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt @@ -1,18 +1,14 @@ package com.simplemobiletools.contacts.pro.activities -import android.annotation.TargetApi import android.app.SearchManager import android.content.Context import android.content.Intent -import android.database.Cursor import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.net.Uri -import android.os.Build import android.os.Bundle import android.os.Handler -import android.provider.BlockedNumberContract.BlockedNumbers import android.view.Menu import android.view.MenuItem import androidx.appcompat.widget.SearchView @@ -36,9 +32,7 @@ import com.simplemobiletools.contacts.pro.extensions.getTempFile import com.simplemobiletools.contacts.pro.fragments.MyViewPagerFragment import com.simplemobiletools.contacts.pro.helpers.* import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener -import com.simplemobiletools.contacts.pro.models.BlockedNumber import com.simplemobiletools.contacts.pro.models.Contact -import com.simplemobiletools.contacts.pro.models.RecentCall import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.fragment_contacts.* import kotlinx.android.synthetic.main.fragment_favorites.* @@ -547,14 +541,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { if (refreshTabsMask and RECENTS_TAB_MASK != 0) { ContactsHelper(this).getRecents { - val numbers = getBlockedNumbers() - val recents = it.filter { - val recentCall = it - numbers.none { it.number == recentCall.number || it.normalizedNumber == recentCall.number } - }.toMutableList() as ArrayList - val localContacts = LocalContactsHelper(applicationContext).getAllContacts() - recents.filter { it.name == null }.forEach { + it.filter { it.name == null }.forEach { val namelessCall = it val localContact = localContacts.firstOrNull { it.doesContainPhoneNumber(namelessCall.number) } if (localContact != null) { @@ -563,41 +551,12 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } runOnUiThread { - recents_fragment?.updateRecentCalls(recents) + recents_fragment?.updateRecentCalls(it) } } } } - @TargetApi(Build.VERSION_CODES.N) - private fun getBlockedNumbers(): ArrayList { - val blockedNumbers = ArrayList() - if (!isNougatPlus()) { - return blockedNumbers - } - - val uri = BlockedNumbers.CONTENT_URI - val projection = arrayOf(BlockedNumbers.COLUMN_ID, BlockedNumbers.COLUMN_ORIGINAL_NUMBER, BlockedNumbers.COLUMN_E164_NUMBER) - - var cursor: Cursor? = null - try { - cursor = contentResolver.query(uri, projection, null, null, null) - if (cursor?.moveToFirst() == true) { - do { - val id = cursor.getLongValue(BlockedNumbers.COLUMN_ID) - val number = cursor.getStringValue(BlockedNumbers.COLUMN_ORIGINAL_NUMBER) ?: "" - val normalizedNumber = cursor.getStringValue(BlockedNumbers.COLUMN_E164_NUMBER) ?: "" - val blockedNumber = BlockedNumber(id, number, normalizedNumber) - blockedNumbers.add(blockedNumber) - } while (cursor.moveToNext()) - } - } finally { - cursor?.close() - } - - return blockedNumbers - } - private fun getAllFragments() = arrayListOf(contacts_fragment, favorites_fragment, recents_fragment, groups_fragment) private fun getRecentsTabIndex(): Int { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index 9aa917ba..10abc5bc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -3,12 +3,15 @@ package com.simplemobiletools.contacts.pro.helpers import android.accounts.Account import android.accounts.AccountManager import android.annotation.SuppressLint +import android.annotation.TargetApi import android.content.* import android.database.Cursor import android.graphics.Bitmap import android.net.Uri +import android.os.Build import android.os.Handler import android.os.Looper +import android.provider.BlockedNumberContract import android.provider.CallLog import android.provider.ContactsContract import android.provider.ContactsContract.CommonDataKinds @@ -1547,6 +1550,7 @@ class ContactsHelper(val context: Context) { return@Thread } + val blockedNumbers = getBlockedNumbers() val uri = CallLog.Calls.CONTENT_URI val projection = arrayOf( CallLog.Calls._ID, @@ -1577,6 +1581,10 @@ class ContactsHelper(val context: Context) { continue } + if (blockedNumbers.any { it.number == number || it.normalizedNumber == number }) { + continue + } + var formattedDate = SimpleDateFormat("dd MMM yyyy, $timeFormat", Locale.getDefault()).format(Date(date)) val datePart = formattedDate.substring(0, 11) when { @@ -1621,4 +1629,33 @@ class ContactsHelper(val context: Context) { } }.start() } + + @TargetApi(Build.VERSION_CODES.N) + private fun getBlockedNumbers(): ArrayList { + val blockedNumbers = ArrayList() + if (!isNougatPlus()) { + return blockedNumbers + } + + val uri = BlockedNumberContract.BlockedNumbers.CONTENT_URI + val projection = arrayOf(BlockedNumberContract.BlockedNumbers.COLUMN_ID, BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER) + + var cursor: Cursor? = null + try { + cursor = context.contentResolver.query(uri, projection, null, null, null) + if (cursor?.moveToFirst() == true) { + do { + val id = cursor.getLongValue(BlockedNumberContract.BlockedNumbers.COLUMN_ID) + val number = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER) ?: "" + val normalizedNumber = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER) ?: "" + val blockedNumber = BlockedNumber(id, number, normalizedNumber) + blockedNumbers.add(blockedNumber) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + + return blockedNumbers + } }