Add ability to check blacklist in spite of blacklist schedule

This commit is contained in:
Moez Bhatti
2021-02-10 21:27:06 -05:00
parent 178cccd93f
commit 2bcb05945b
11 changed files with 45 additions and 15 deletions
@@ -30,7 +30,9 @@ class BlockingManager @Inject constructor(
override fun getClientCapability(): BlockingClient.Capability = client.getClientCapability()
override fun getAction(address: String): Single<BlockingClient.Action> = client.getAction(address)
override fun shouldBlock(address: String): Single<BlockingClient.Action> = client.shouldBlock(address)
override fun isBlacklisted(address: String): Single<BlockingClient.Action> = client.isBlacklisted(address)
override fun block(addresses: List<String>): Completable = client.block(addresses)
@@ -43,11 +43,24 @@ class CallBlockerBlockingClient @Inject constructor(
override fun getClientCapability() = BlockingClient.Capability.BLOCK_WITH_PERMISSION
override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
override fun shouldBlock(address: String): Single<BlockingClient.Action> = Single.fromCallable {
val uri = Uri.parse("content://com.cuiet.blockCalls.ContProvBlockCalls/lookup/is.blocked.lookup")
val blockReason = tryOrNull {
context.contentResolver.query(uri, arrayOf("result"), "incomingNumber",
arrayOf(address), null)
context.contentResolver.query(uri, arrayOf("result"), "incomingNumber", arrayOf(address), null)
?.use { cursor -> cursor.map(::LookupResult) }
?.find { result -> result.blockReason != null }
}?.blockReason
when (blockReason) {
"true" -> BlockingClient.Action.Block()
else -> BlockingClient.Action.Unblock
}
}
override fun isBlacklisted(address: String): Single<BlockingClient.Action> = Single.fromCallable {
val uri = Uri.parse("content://com.cuiet.blockCalls.ContProvBlockCalls/lookup/is.blocked.lookup")
val blockReason = tryOrNull {
context.contentResolver.query(uri, arrayOf("result"), "blacklistLookup", arrayOf(address), null)
?.use { cursor -> cursor.map(::LookupResult) }
?.find { result -> result.blockReason != null }
}?.blockReason
@@ -48,7 +48,9 @@ class CallControlBlockingClient @Inject constructor(
override fun getClientCapability() = BlockingClient.Capability.BLOCK_WITH_PERMISSION
override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
override fun shouldBlock(address: String): Single<BlockingClient.Action> = isBlacklisted(address)
override fun isBlacklisted(address: String): Single<BlockingClient.Action> = Single.fromCallable {
val uri = Uri.withAppendedPath(CallControl.LOOKUP_TEXT_URI, address)
val blockReason = tryOrNull {
context.contentResolver.query(uri, projection, null, null, null) // Query URI
@@ -31,7 +31,9 @@ class QksmsBlockingClient @Inject constructor(
override fun getClientCapability() = BlockingClient.Capability.BLOCK_WITHOUT_PERMISSION
override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
override fun shouldBlock(address: String): Single<BlockingClient.Action> = isBlacklisted(address)
override fun isBlacklisted(address: String): Single<BlockingClient.Action> = Single.fromCallable {
when (blockingRepo.isBlocked(address)) {
true -> BlockingClient.Action.Block()
false -> BlockingClient.Action.Unblock
@@ -55,7 +55,9 @@ class ShouldIAnswerBlockingClient @Inject constructor(
override fun getClientCapability() = BlockingClient.Capability.CANT_BLOCK
override fun getAction(address: String): Single<BlockingClient.Action> {
override fun shouldBlock(address: String): Single<BlockingClient.Action> = isBlacklisted(address)
override fun isBlacklisted(address: String): Single<BlockingClient.Action> {
return Binder(context, address).isBlocked()
.map { blocked ->
when (blocked) {
@@ -52,7 +52,14 @@ interface BlockingClient {
/**
* Returns the recommendation action to perform given a message from the [address]
*/
fun getAction(address: String): Single<Action>
fun shouldBlock(address: String): Single<Action>
/**
* Returns whether or not the [address] is in the blocking manager's blacklist
* In most cases this will return the same result as [shouldBlock], but it's possible for an app's blacklist
* to be temporarily deactivated, in which case the results will differ
*/
fun isBlacklisted(address: String): Single<Action>
/**
* Blocks the numbers or opens the manager
@@ -57,7 +57,7 @@ class ReceiveMms @Inject constructor(
// to check if it should be blocked after we've pulled it into realm. If it
// turns out that it should be dropped, then delete it
// TODO Don't store blocked messages in the first place
val action = blockingClient.getAction(message.address).blockingGet()
val action = blockingClient.shouldBlock(message.address).blockingGet()
val shouldDrop = prefs.drop.get()
Timber.v("block=$action, drop=$shouldDrop")
@@ -49,7 +49,7 @@ class ReceiveSms @Inject constructor(
// Don't continue if the sender is blocked
val messages = it.messages
val address = messages[0].displayOriginatingAddress
val action = blockingClient.getAction(address).blockingGet()
val action = blockingClient.shouldBlock(address).blockingGet()
val shouldDrop = prefs.drop.get()
Timber.v("block=$action, drop=$shouldDrop")
@@ -77,7 +77,7 @@ class BlockingDialog @Inject constructor(
}
private fun allBlocked(addresses: List<String>): Boolean = addresses.all { address ->
blockingManager.getAction(address).blockingGet() is BlockingClient.Action.Block
blockingManager.isBlacklisted(address).blockingGet() is BlockingClient.Action.Block
}
private suspend fun showDialog(
@@ -92,8 +92,9 @@ class BlockingDialog @Inject constructor(
}
val manager = context.getString(when (prefs.blockingManager.get()) {
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
Preferences.BLOCKING_MANAGER_CB -> R.string.blocking_manager_call_blocker_title
Preferences.BLOCKING_MANAGER_CC -> R.string.blocking_manager_call_control_title
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
else -> R.string.blocking_manager_qksms_title
})
@@ -38,8 +38,9 @@ class BlockingPresenter @Inject constructor(
disposables += prefs.blockingManager.asObservable()
.map { client ->
when (client) {
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
Preferences.BLOCKING_MANAGER_CB -> R.string.blocking_manager_call_blocker_title
Preferences.BLOCKING_MANAGER_CC -> R.string.blocking_manager_call_control_title
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
else -> R.string.blocking_manager_qksms_title
}
}
@@ -118,7 +118,7 @@ class BlockingManagerPresenter @Inject constructor(
.switchMap { numbers -> callControl.block(numbers).andThen(Observable.just(Unit)) } // Hack
.autoDisposable(view.scope())
.subscribe {
callControl.getAction("callcontrol").blockingGet()
callControl.shouldBlock("callcontrol").blockingGet()
analytics.setUserProperty("Blocking Manager", "Call Control")
prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_CC)
}
@@ -143,6 +143,6 @@ class BlockingManagerPresenter @Inject constructor(
private fun getAddressesToBlock(client: BlockingClient) = conversationRepo.getBlockedConversations()
.fold(listOf<String>(), { numbers, conversation -> numbers + conversation.recipients.map { it.address } })
.filter { number -> client.getAction(number).blockingGet() !is BlockingClient.Action.Block }
.filter { number -> client.isBlacklisted(number).blockingGet() !is BlockingClient.Action.Block }
}