Merge branch 'call-blocker'

This commit is contained in:
Moez Bhatti
2021-06-23 16:04:10 -07:00
66 changed files with 985 additions and 531 deletions
@@ -12,6 +12,7 @@ import javax.inject.Singleton
@Singleton
class BlockingManager @Inject constructor(
private val prefs: Preferences,
private val callBlockerBlockingClient: CallBlockerBlockingClient,
private val callControlBlockingClient: CallControlBlockingClient,
private val qksmsBlockingClient: QksmsBlockingClient,
private val shouldIAnswerBlockingClient: ShouldIAnswerBlockingClient
@@ -19,6 +20,7 @@ class BlockingManager @Inject constructor(
private val client: BlockingClient
get() = when (prefs.blockingManager.get()) {
Preferences.BLOCKING_MANAGER_CB -> callBlockerBlockingClient
Preferences.BLOCKING_MANAGER_SIA -> shouldIAnswerBlockingClient
Preferences.BLOCKING_MANAGER_CC -> callControlBlockingClient
else -> qksmsBlockingClient
@@ -28,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)
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2020 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
import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.net.Uri
import androidx.core.database.getStringOrNull
import com.moez.QKSMS.common.util.extensions.isInstalled
import com.moez.QKSMS.extensions.map
import io.reactivex.Completable
import io.reactivex.Single
import timber.log.Timber
import java.util.*
import javax.inject.Inject
class CallBlockerBlockingClient @Inject constructor(
private val context: Context
) : BlockingClient {
class LookupResult(cursor: Cursor) {
val blockReason: String? = cursor.getStringOrNull(0)
}
override fun isAvailable(): Boolean = context.isInstalled("com.cuiet.blockCalls")
override fun getClientCapability() = BlockingClient.Capability.BLOCK_WITH_PERMISSION
override fun shouldBlock(address: String): Single<BlockingClient.Action> = lookup(address, "incomingNumber")
override fun isBlacklisted(address: String): Single<BlockingClient.Action> = lookup(address, "blacklistLookup")
private fun lookup(address: String, reason: String): Single<BlockingClient.Action> = Single.fromCallable {
val uri = Uri.parse("content://com.cuiet.blockCalls.ContProvBlockCalls/lookup/is.blocked.lookup")
return@fromCallable try {
val blockReason = context.contentResolver.query(uri, arrayOf("result"), reason, arrayOf(address), null)
?.use { cursor -> cursor.map(::LookupResult) }
?.find { result -> result.blockReason != null }
?.blockReason
when (blockReason) {
"true" -> BlockingClient.Action.Block()
else -> BlockingClient.Action.Unblock
}
} catch (e: Exception) {
Timber.w(e)
BlockingClient.Action.DoNothing
}
}
override fun block(addresses: List<String>): Completable = Completable.fromCallable {
val arrayList = ArrayList<String>()
arrayList.addAll(addresses)
val intent = Intent("com.cuiet.blockCalls.ADD_NUMBERS")
intent.putStringArrayListExtra("addresses", arrayList)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
override fun unblock(addresses: List<String>): Completable = Completable.fromCallable {
val arrayList = ArrayList<String>()
arrayList.addAll(addresses)
val intent = Intent("com.cuiet.blockCalls.REMOVE_NUMBERS")
intent.putStringArrayListExtra("addresses", arrayList)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
override fun openSettings() {
val intent = Intent("com.cuiet.blockCalls.OPEN_SETTINGS")
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
}
@@ -26,9 +26,9 @@ import androidx.core.database.getStringOrNull
import com.callcontrol.datashare.CallControl
import com.moez.QKSMS.common.util.extensions.isInstalled
import com.moez.QKSMS.extensions.map
import com.moez.QKSMS.util.tryOrNull
import io.reactivex.Completable
import io.reactivex.Single
import timber.log.Timber
import javax.inject.Inject
class CallControlBlockingClient @Inject constructor(
@@ -48,17 +48,23 @@ 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
return@fromCallable try {
val blockReason = context.contentResolver.query(uri, projection, null, null, null) // Query URI
?.use { cursor -> cursor.map(::LookupResult) } // Map to Result object
?.find { result -> result.blockReason != null } // Check if any are blocked
}?.blockReason // If none are blocked or we errored at some point, return false
?.blockReason // If none are blocked or we errored at some point, return false
when (blockReason) {
null -> BlockingClient.Action.Unblock
else -> BlockingClient.Action.Block(blockReason)
when (blockReason) {
null -> BlockingClient.Action.Unblock
else -> BlockingClient.Action.Block(blockReason)
}
} catch (e: Exception) {
Timber.w(e)
BlockingClient.Action.DoNothing
}
}
@@ -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) {