Add support for Call Blocker

This commit is contained in:
Moez Bhatti
2021-02-09 23:09:05 -05:00
parent 3d84ccceea
commit 178cccd93f
10 changed files with 138 additions and 0 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
@@ -0,0 +1,84 @@
/*
* 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 com.moez.QKSMS.util.tryOrNull
import io.reactivex.Completable
import io.reactivex.Single
import java.util.ArrayList
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 getAction(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)
?.use { cursor -> cursor.map(::LookupResult) }
?.find { result -> result.blockReason != null }
}?.blockReason
when (blockReason) {
"true" -> BlockingClient.Action.Block()
else -> BlockingClient.Action.Unblock
}
}
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)
}
}