#1304 - Create blocking client for built-in android blocking

This commit is contained in:
moezbhatti
2019-06-20 01:27:15 -04:00
parent 60f41d9996
commit afa6199897
9 changed files with 125 additions and 12 deletions
@@ -25,6 +25,7 @@ import androidx.core.database.getStringOrNull
import com.callcontrol.datashare.CallControl
import com.moez.QKSMS.extensions.map
import com.moez.QKSMS.util.tryOrNull
import io.reactivex.Completable
import io.reactivex.Single
import javax.inject.Inject
@@ -41,7 +42,7 @@ class CallControlBlockingClient @Inject constructor(
val blockReason: String? = cursor.getStringOrNull(0)
}
override fun shouldBlock(address: String): Single<Boolean> {
override fun isBlocked(address: String): Single<Boolean> {
val uri = Uri.withAppendedPath(CallControl.LOOKUP_TEXT_URI, address)
return Single.fromCallable {
tryOrNull {
@@ -52,4 +53,14 @@ class CallControlBlockingClient @Inject constructor(
}
}
override fun canBlock(): Boolean = true
override fun block(address: String): Completable = Completable.fromCallable {
CallControl.report(context, CallControl.Report(address))
}
override fun unblock(address: String): Completable = Completable.fromCallable {
CallControl.openBlockedList(context, address)
}
}
@@ -18,13 +18,72 @@
*/
package com.moez.QKSMS.blocking
import android.content.Context
import android.os.Build
import android.provider.BlockedNumberContract
import android.telecom.TelecomManager
import android.telephony.PhoneNumberUtils
import com.moez.QKSMS.extensions.anyOf
import com.moez.QKSMS.model.BlockedNumber
import io.reactivex.Completable
import io.reactivex.Single
import io.realm.Realm
import javax.inject.Inject
class QkBlockingClient @Inject constructor() : BlockingClient {
class QkBlockingClient @Inject constructor(private val context: Context) : BlockingClient {
override fun shouldBlock(address: String): Single<Boolean> {
return Single.just(false) // TODO: This will be implemented once we're able to reject these messages entirely
override fun isBlocked(address: String): Single<Boolean> = Single.fromCallable {
when {
Build.VERSION.SDK_INT >= 24 -> BlockedNumberContract.isBlocked(context, address)
else -> Realm.getDefaultInstance()
.where(BlockedNumber::class.java)
.findAll()
.any { number -> PhoneNumberUtils.compare(number.address, address) }
}
}
override fun canBlock(): Boolean = Build.VERSION.SDK_INT < 24
override fun block(address: String): Completable = Completable.fromCallable {
when {
Build.VERSION.SDK_INT >= 24 -> {
val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
val intent = telecomManager.createManageBlockedNumbersIntent()
context.startActivity(intent)
}
else -> Realm.getDefaultInstance().use { realm ->
val maxId = realm.where(BlockedNumber::class.java)
.max("id")?.toLong() ?: -1
realm.executeTransaction {
realm.insert(BlockedNumber(maxId + 1, address))
}
}
}
}
override fun canUnblock(): Boolean = true
override fun unblock(address: String): Completable = Completable.fromCallable {
when {
Build.VERSION.SDK_INT >= 24 -> BlockedNumberContract.unblock(context, address)
else -> Realm.getDefaultInstance().use { realm ->
val ids = realm
.where(BlockedNumber::class.java)
.findAll()
.filter { number -> PhoneNumberUtils.compare(number.address, address) }
.map { number -> number.id }
.toLongArray()
realm.executeTransaction {
realm.where(BlockedNumber::class.java)
.anyOf("id", ids)
}
}
}
}
}
@@ -30,6 +30,7 @@ import android.os.Messenger
import androidx.core.os.bundleOf
import com.moez.QKSMS.util.Preferences
import com.moez.QKSMS.util.tryOrNull
import io.reactivex.Completable
import io.reactivex.Single
import io.reactivex.subjects.SingleSubject
import javax.inject.Inject
@@ -51,8 +52,16 @@ class ShouldIAnswerBlockingClient @Inject constructor(
/**
* Return a Single<Boolean> which emits whether or not the given [address] should be blocked
*/
override fun shouldBlock(address: String): Single<Boolean> {
return Binder(context, prefs, address).shouldBlock()
override fun isBlocked(address: String): Single<Boolean> {
return Binder(context, prefs, address).isBlocked()
}
override fun block(address: String): Completable = Completable.fromCallable { showSia() }
override fun unblock(address: String): Completable = Completable.fromCallable { showSia() }
private fun showSia() {
// TODO
}
private class Binder(
@@ -65,8 +74,7 @@ class ShouldIAnswerBlockingClient @Inject constructor(
private var serviceMessenger: Messenger? = null
private var isBound: Boolean = false
fun shouldBlock(): Single<Boolean> {
fun isBlocked(): Single<Boolean> {
var intent: Intent? = null
// If either version of Should I Answer? is installed and SIA is enabled, build the
@@ -341,7 +341,7 @@ class ConversationRepositoryImpl @Inject constructor(
}
conversation.blocked = recipients.any { recipient ->
blockingClient.shouldBlock(recipient.address).blockingGet()
blockingClient.isBlocked(recipient.address).blockingGet()
}
conversation.recipients.clear()