filter out the blocked numbers from Recents

This commit is contained in:
tibbi 2018-11-30 11:10:56 +01:00
parent 665b5b32a9
commit 47a1ff142b
2 changed files with 47 additions and 2 deletions

View File

@ -1,14 +1,18 @@
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
@ -32,7 +36,9 @@ 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.*
@ -40,6 +46,7 @@ import kotlinx.android.synthetic.main.fragment_groups.*
import kotlinx.android.synthetic.main.fragment_recents.* import kotlinx.android.synthetic.main.fragment_recents.*
import java.io.FileOutputStream import java.io.FileOutputStream
class MainActivity : SimpleActivity(), RefreshContactsListener { class MainActivity : SimpleActivity(), RefreshContactsListener {
private var isSearchOpen = false private var isSearchOpen = false
private var searchMenuItem: MenuItem? = null private var searchMenuItem: MenuItem? = null
@ -540,8 +547,14 @@ 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()
it.filter { it.name == null }.forEach { recents.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) {
@ -550,12 +563,41 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
} }
runOnUiThread { runOnUiThread {
recents_fragment?.updateRecentCalls(it) recents_fragment?.updateRecentCalls(recents)
} }
} }
} }
} }
@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

@ -0,0 +1,3 @@
package com.simplemobiletools.contacts.pro.models
data class BlockedNumber(val id: Long, val number: String, val normalizedNumber: String)