Don't unblock numbers not reported by SIA

This commit is contained in:
moezbhatti
2019-08-30 16:54:05 +02:00
parent 03af81336c
commit e1b1a6c8db
11 changed files with 69 additions and 66 deletions
@@ -34,9 +34,9 @@ class BlockingManager @Inject constructor(
override fun isAvailable(): Boolean = client.isAvailable() override fun isAvailable(): Boolean = client.isAvailable()
override fun getClientCapability(): BlockingClientCapability = client.getClientCapability() override fun getClientCapability(): BlockingClient.Capability = client.getClientCapability()
override fun isBlocked(address: String): Single<Boolean> = client.isBlocked(address) override fun getAction(address: String): Single<BlockingClient.Action> = client.getAction(address)
override fun block(addresses: List<String>): Completable = client.block(addresses) override fun block(addresses: List<String>): Completable = client.block(addresses)
@@ -46,16 +46,19 @@ class CallControlBlockingClient @Inject constructor(
override fun isAvailable(): Boolean = context.isInstalled("com.flexaspect.android.everycallcontrol") override fun isAvailable(): Boolean = context.isInstalled("com.flexaspect.android.everycallcontrol")
override fun getClientCapability() = BlockingClientCapability.BLOCK_WITH_PERMISSION override fun getClientCapability() = BlockingClient.Capability.BLOCK_WITH_PERMISSION
override fun isBlocked(address: String): Single<Boolean> { override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
val uri = Uri.withAppendedPath(CallControl.LOOKUP_TEXT_URI, address) val uri = Uri.withAppendedPath(CallControl.LOOKUP_TEXT_URI, address)
return Single.fromCallable { val blocked = tryOrNull {
tryOrNull { context.contentResolver.query(uri, projection, null, null, null) // Query URI
context.contentResolver.query(uri, projection, null, null, null) // Query URI ?.use { cursor -> cursor.map(::LookupResult) } // Map to Result object
?.use { cursor -> cursor.map(::LookupResult) } // Map to Result object ?.any { result -> result.blockReason != null } // Check if any are blocked
?.any { result -> result.blockReason != null } // Check if any are blocked } == true // If none are blocked or we errored at some point, return false
} == true // If none are blocked or we errored at some point, return false
when (blocked) {
true -> BlockingClient.Action.BLOCK
false -> BlockingClient.Action.UNBLOCK
} }
} }
@@ -29,10 +29,13 @@ class QksmsBlockingClient @Inject constructor(
override fun isAvailable(): Boolean = true override fun isAvailable(): Boolean = true
override fun getClientCapability() = BlockingClientCapability.BLOCK_WITHOUT_PERMISSION override fun getClientCapability() = BlockingClient.Capability.BLOCK_WITHOUT_PERMISSION
override fun isBlocked(address: String): Single<Boolean> = Single.fromCallable { override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
blockingRepo.isBlocked(address) when (blockingRepo.isBlocked(address)) {
true -> BlockingClient.Action.BLOCK
false -> BlockingClient.Action.UNBLOCK
}
} }
override fun block(addresses: List<String>): Completable = Completable.fromCallable { override fun block(addresses: List<String>): Completable = Completable.fromCallable {
@@ -53,13 +53,16 @@ class ShouldIAnswerBlockingClient @Inject constructor(
"org.mistergroup.muzutozvednout") "org.mistergroup.muzutozvednout")
.any(context::isInstalled) .any(context::isInstalled)
override fun getClientCapability() = BlockingClientCapability.CANT_BLOCK override fun getClientCapability() = BlockingClient.Capability.CANT_BLOCK
/** override fun getAction(address: String): Single<BlockingClient.Action> {
* Return a Single<Boolean> which emits whether or not the given [address] should be blocked
*/
override fun isBlocked(address: String): Single<Boolean> {
return Binder(context, address).isBlocked() return Binder(context, address).isBlocked()
.map { blocked ->
when (blocked) {
true -> BlockingClient.Action.BLOCK
false -> BlockingClient.Action.DO_NOTHING
}
}
} }
override fun block(addresses: List<String>): Completable = Completable.fromCallable { openSettings() } override fun block(addresses: List<String>): Completable = Completable.fromCallable { openSettings() }
@@ -355,7 +355,7 @@ class ConversationRepositoryImpl @Inject constructor(
} }
conversation.blocked = recipients.any { recipient -> conversation.blocked = recipients.any { recipient ->
blockingClient.isBlocked(recipient.address).blockingGet() blockingClient.getAction(recipient.address).blockingGet() == BlockingClient.Action.BLOCK
} }
conversation.recipients.clear() conversation.recipients.clear()
@@ -23,6 +23,22 @@ import io.reactivex.Single
interface BlockingClient { interface BlockingClient {
enum class Capability {
BLOCK_WITHOUT_PERMISSION,
BLOCK_WITH_PERMISSION ,
CANT_BLOCK
}
enum class Action {
BLOCK,
UNBLOCK,
// We only need these for Should I Answer, because they don't allow us to block numbers in their app directly.
// This means there's a good chance that if a number is blocked in QK, it won't be blocked there, so we
// shouldn't unblock the conversation in that case
DO_NOTHING
}
/** /**
* Returns true if the target blocking client is available for use, ie. it is installed * Returns true if the target blocking client is available for use, ie. it is installed
*/ */
@@ -31,12 +47,12 @@ interface BlockingClient {
/** /**
* Returns the level of access that the given blocking client provides to QKSMS * Returns the level of access that the given blocking client provides to QKSMS
*/ */
fun getClientCapability(): BlockingClientCapability fun getClientCapability(): Capability
/** /**
* Return a Single<Boolean> which emits whether or not the given [address] should be blocked * Returns the recommendation action to perform given a message from the [address]
*/ */
fun isBlocked(address: String): Single<Boolean> fun getAction(address: String): Single<Action>
/** /**
* Blocks the numbers or opens the manager * Blocks the numbers or opens the manager
@@ -1,25 +0,0 @@
/*
* Copyright (C) 2019 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.blocking
enum class BlockingClientCapability {
BLOCK_WITHOUT_PERMISSION,
BLOCK_WITH_PERMISSION ,
CANT_BLOCK
}
@@ -57,19 +57,19 @@ class ReceiveMms @Inject constructor(
// to check if it should be blocked after we've pulled it into realm. If it // 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 // turns out that it should be dropped, then delete it
// TODO Don't store blocked messages in the first place // TODO Don't store blocked messages in the first place
val shouldBlock = blockingClient.isBlocked(message.address).blockingGet() val action = blockingClient.getAction(message.address).blockingGet()
val shouldDrop = prefs.drop.get() val shouldDrop = prefs.drop.get()
Timber.v("block=$shouldBlock, drop=$shouldDrop") Timber.v("block=$action, drop=$shouldDrop")
if (shouldBlock && shouldDrop) { if (action == BlockingClient.Action.BLOCK && shouldDrop) {
messageRepo.deleteMessages(message.id) messageRepo.deleteMessages(message.id)
return@mapNotNull null return@mapNotNull null
} }
if (shouldBlock) { when (action) {
conversationRepo.markBlocked(message.threadId) BlockingClient.Action.BLOCK -> conversationRepo.markBlocked(message.threadId)
} else { BlockingClient.Action.UNBLOCK -> conversationRepo.markUnblocked(message.threadId)
conversationRepo.markUnblocked(message.threadId) else -> Unit
} }
message message
@@ -49,12 +49,12 @@ class ReceiveSms @Inject constructor(
// Don't continue if the sender is blocked // Don't continue if the sender is blocked
val messages = it.messages val messages = it.messages
val address = messages[0].displayOriginatingAddress val address = messages[0].displayOriginatingAddress
val shouldBlock = blockingClient.isBlocked(address).blockingGet() val action = blockingClient.getAction(address).blockingGet()
val shouldDrop = prefs.drop.get() val shouldDrop = prefs.drop.get()
Timber.v("block=$shouldBlock, drop=$shouldDrop") Timber.v("block=$action, drop=$shouldDrop")
// If we should drop the message, don't even save it // If we should drop the message, don't even save it
if (shouldBlock && shouldDrop) { if (action == BlockingClient.Action.BLOCK && shouldDrop) {
return@mapNotNull null return@mapNotNull null
} }
@@ -66,10 +66,10 @@ class ReceiveSms @Inject constructor(
// Add the message to the db // Add the message to the db
val message = messageRepo.insertReceivedSms(it.subId, address, body, time) val message = messageRepo.insertReceivedSms(it.subId, address, body, time)
if (shouldBlock) { when (action) {
conversationRepo.markBlocked(message.threadId) BlockingClient.Action.BLOCK -> conversationRepo.markBlocked(message.threadId)
} else { BlockingClient.Action.UNBLOCK -> conversationRepo.markUnblocked(message.threadId)
conversationRepo.markUnblocked(message.threadId) else -> Unit
} }
message message
@@ -23,7 +23,6 @@ import android.app.AlertDialog
import android.content.Context import android.content.Context
import com.moez.QKSMS.R import com.moez.QKSMS.R
import com.moez.QKSMS.blocking.BlockingClient import com.moez.QKSMS.blocking.BlockingClient
import com.moez.QKSMS.blocking.BlockingClientCapability
import com.moez.QKSMS.interactor.MarkBlocked import com.moez.QKSMS.interactor.MarkBlocked
import com.moez.QKSMS.interactor.MarkUnblocked import com.moez.QKSMS.interactor.MarkUnblocked
import com.moez.QKSMS.repository.ConversationRepository import com.moez.QKSMS.repository.ConversationRepository
@@ -55,7 +54,7 @@ class BlockingDialog @Inject constructor(
return@launch return@launch
} }
if (blockingManager.getClientCapability() == BlockingClientCapability.BLOCK_WITHOUT_PERMISSION) { if (blockingManager.getClientCapability() == BlockingClient.Capability.BLOCK_WITHOUT_PERMISSION) {
// If we can block/unblock in the external manager, then just fire that off and exit // If we can block/unblock in the external manager, then just fire that off and exit
if (block) { if (block) {
markBlocked.execute(conversationIds) markBlocked.execute(conversationIds)
@@ -64,7 +63,7 @@ class BlockingDialog @Inject constructor(
markUnblocked.execute(conversationIds) markUnblocked.execute(conversationIds)
blockingManager.unblock(addresses).subscribe() blockingManager.unblock(addresses).subscribe()
} }
} else if (addresses.all { address -> block == blockingManager.isBlocked(address).blockingGet() }) { } else if (block == allBlocked(addresses)) {
// If all of the addresses are already in their correct state in the blocking manager, just marked the // If all of the addresses are already in their correct state in the blocking manager, just marked the
// conversations blocked and exit // conversations blocked and exit
when (block) { when (block) {
@@ -77,6 +76,10 @@ class BlockingDialog @Inject constructor(
} }
} }
private fun allBlocked(addresses: List<String>): Boolean = addresses.all { address ->
blockingManager.getAction(address).blockingGet() == BlockingClient.Action.BLOCK
}
private suspend fun showDialog( private suspend fun showDialog(
activity: Activity, activity: Activity,
conversationIds: List<Long>, conversationIds: List<Long>,
@@ -99,7 +99,7 @@ class BlockingManagerPresenter @Inject constructor(
.switchMap { numbers -> callControl.block(numbers).andThen(Observable.just(Unit)) } // Hack .switchMap { numbers -> callControl.block(numbers).andThen(Observable.just(Unit)) } // Hack
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { .subscribe {
callControl.isBlocked("callcontrol").blockingGet() callControl.getAction("callcontrol").blockingGet()
analytics.setUserProperty("Blocking Manager", "Call Control") analytics.setUserProperty("Blocking Manager", "Call Control")
prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_CC) prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_CC)
} }
@@ -142,6 +142,6 @@ class BlockingManagerPresenter @Inject constructor(
private fun getAddressesToBlock(client: BlockingClient) = conversationRepo.getBlockedConversations() private fun getAddressesToBlock(client: BlockingClient) = conversationRepo.getBlockedConversations()
.fold(listOf<String>(), { numbers, conversation -> numbers + conversation.recipients.map { it.address } }) .fold(listOf<String>(), { numbers, conversation -> numbers + conversation.recipients.map { it.address } })
.filter { number -> !client.isBlocked(number).blockingGet() } .filter { number -> client.getAction(number).blockingGet() != BlockingClient.Action.BLOCK }
} }