filter out blocked numbers earlier, right at fetching

This commit is contained in:
tibbi
2018-11-30 11:34:39 +01:00
parent 47a1ff142b
commit 6fae60381f
2 changed files with 39 additions and 43 deletions

View File

@ -1,18 +1,14 @@
package com.simplemobiletools.contacts.pro.activities package com.simplemobiletools.contacts.pro.activities
import android.annotation.TargetApi
import android.app.SearchManager import android.app.SearchManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.database.Cursor
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.provider.BlockedNumberContract.BlockedNumbers
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.widget.SearchView 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.fragments.MyViewPagerFragment
import com.simplemobiletools.contacts.pro.helpers.* import com.simplemobiletools.contacts.pro.helpers.*
import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener 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.Contact
import com.simplemobiletools.contacts.pro.models.RecentCall
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_contacts.* import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.fragment_favorites.* import kotlinx.android.synthetic.main.fragment_favorites.*
@ -547,14 +541,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
if (refreshTabsMask and RECENTS_TAB_MASK != 0) { if (refreshTabsMask and RECENTS_TAB_MASK != 0) {
ContactsHelper(this).getRecents { 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<RecentCall>
val localContacts = LocalContactsHelper(applicationContext).getAllContacts() val localContacts = LocalContactsHelper(applicationContext).getAllContacts()
recents.filter { it.name == null }.forEach { it.filter { it.name == null }.forEach {
val namelessCall = it val namelessCall = it
val localContact = localContacts.firstOrNull { it.doesContainPhoneNumber(namelessCall.number) } val localContact = localContacts.firstOrNull { it.doesContainPhoneNumber(namelessCall.number) }
if (localContact != null) { if (localContact != null) {
@ -563,41 +551,12 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
} }
runOnUiThread { runOnUiThread {
recents_fragment?.updateRecentCalls(recents) recents_fragment?.updateRecentCalls(it)
} }
} }
} }
} }
@TargetApi(Build.VERSION_CODES.N)
private fun getBlockedNumbers(): ArrayList<BlockedNumber> {
val blockedNumbers = ArrayList<BlockedNumber>()
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 getAllFragments() = arrayListOf(contacts_fragment, favorites_fragment, recents_fragment, groups_fragment)
private fun getRecentsTabIndex(): Int { private fun getRecentsTabIndex(): Int {

View File

@ -3,12 +3,15 @@ package com.simplemobiletools.contacts.pro.helpers
import android.accounts.Account import android.accounts.Account
import android.accounts.AccountManager import android.accounts.AccountManager
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.* import android.content.*
import android.database.Cursor import android.database.Cursor
import android.graphics.Bitmap import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.provider.BlockedNumberContract
import android.provider.CallLog import android.provider.CallLog
import android.provider.ContactsContract import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds import android.provider.ContactsContract.CommonDataKinds
@ -1547,6 +1550,7 @@ class ContactsHelper(val context: Context) {
return@Thread return@Thread
} }
val blockedNumbers = getBlockedNumbers()
val uri = CallLog.Calls.CONTENT_URI val uri = CallLog.Calls.CONTENT_URI
val projection = arrayOf( val projection = arrayOf(
CallLog.Calls._ID, CallLog.Calls._ID,
@ -1577,6 +1581,10 @@ class ContactsHelper(val context: Context) {
continue continue
} }
if (blockedNumbers.any { it.number == number || it.normalizedNumber == number }) {
continue
}
var formattedDate = SimpleDateFormat("dd MMM yyyy, $timeFormat", Locale.getDefault()).format(Date(date)) var formattedDate = SimpleDateFormat("dd MMM yyyy, $timeFormat", Locale.getDefault()).format(Date(date))
val datePart = formattedDate.substring(0, 11) val datePart = formattedDate.substring(0, 11)
when { when {
@ -1621,4 +1629,33 @@ class ContactsHelper(val context: Context) {
} }
}.start() }.start()
} }
@TargetApi(Build.VERSION_CODES.N)
private fun getBlockedNumbers(): ArrayList<BlockedNumber> {
val blockedNumbers = ArrayList<BlockedNumber>()
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
}
} }