#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.callcontrol.datashare.CallControl
import com.moez.QKSMS.extensions.map import com.moez.QKSMS.extensions.map
import com.moez.QKSMS.util.tryOrNull import com.moez.QKSMS.util.tryOrNull
import io.reactivex.Completable
import io.reactivex.Single import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
@@ -41,7 +42,7 @@ class CallControlBlockingClient @Inject constructor(
val blockReason: String? = cursor.getStringOrNull(0) 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) val uri = Uri.withAppendedPath(CallControl.LOOKUP_TEXT_URI, address)
return Single.fromCallable { return Single.fromCallable {
tryOrNull { 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 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.reactivex.Single
import io.realm.Realm
import javax.inject.Inject 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> { override fun isBlocked(address: String): Single<Boolean> = Single.fromCallable {
return Single.just(false) // TODO: This will be implemented once we're able to reject these messages entirely 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 androidx.core.os.bundleOf
import com.moez.QKSMS.util.Preferences import com.moez.QKSMS.util.Preferences
import com.moez.QKSMS.util.tryOrNull import com.moez.QKSMS.util.tryOrNull
import io.reactivex.Completable
import io.reactivex.Single import io.reactivex.Single
import io.reactivex.subjects.SingleSubject import io.reactivex.subjects.SingleSubject
import javax.inject.Inject 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 * Return a Single<Boolean> which emits whether or not the given [address] should be blocked
*/ */
override fun shouldBlock(address: String): Single<Boolean> { override fun isBlocked(address: String): Single<Boolean> {
return Binder(context, prefs, address).shouldBlock() 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( private class Binder(
@@ -65,8 +74,7 @@ class ShouldIAnswerBlockingClient @Inject constructor(
private var serviceMessenger: Messenger? = null private var serviceMessenger: Messenger? = null
private var isBound: Boolean = false private var isBound: Boolean = false
fun shouldBlock(): Single<Boolean> { fun isBlocked(): Single<Boolean> {
var intent: Intent? = null var intent: Intent? = null
// If either version of Should I Answer? is installed and SIA is enabled, build the // 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 -> conversation.blocked = recipients.any { recipient ->
blockingClient.shouldBlock(recipient.address).blockingGet() blockingClient.isBlocked(recipient.address).blockingGet()
} }
conversation.recipients.clear() conversation.recipients.clear()
@@ -18,6 +18,7 @@
*/ */
package com.moez.QKSMS.blocking package com.moez.QKSMS.blocking
import io.reactivex.Completable
import io.reactivex.Single import io.reactivex.Single
interface BlockingClient { interface BlockingClient {
@@ -25,6 +26,28 @@ interface BlockingClient {
/** /**
* Return a Single<Boolean> which emits whether or not the given [address] should be blocked * Return a Single<Boolean> which emits whether or not the given [address] should be blocked
*/ */
fun shouldBlock(address: String): Single<Boolean> fun isBlocked(address: String): Single<Boolean>
/**
* Returns true if the client is capable of blocking the address in the manager without any
* further user input
*/
fun canBlock(): Boolean = false
/**
* Blocks the number or opens the manager
*/
fun block(address: String): Completable
/**
* Returns true if the client is capable of unblocking the address in the manager without any
* further user input
*/
fun canUnblock(): Boolean = false
/**
* Unblocks the number or opens the manager
*/
fun unblock(address: String): Completable
} }
@@ -57,7 +57,7 @@ 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.shouldBlock(message.address).blockingGet() val shouldBlock = blockingClient.isBlocked(message.address).blockingGet()
val shouldDrop = prefs.drop.get() val shouldDrop = prefs.drop.get()
Timber.v("block=$shouldBlock, drop=$shouldDrop") Timber.v("block=$shouldBlock, drop=$shouldDrop")
@@ -49,7 +49,7 @@ 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.shouldBlock(address).blockingGet() val shouldBlock = blockingClient.isBlocked(address).blockingGet()
val shouldDrop = prefs.drop.get() val shouldDrop = prefs.drop.get()
Timber.v("block=$shouldBlock, drop=$shouldDrop") Timber.v("block=$shouldBlock, drop=$shouldDrop")
@@ -0,0 +1,10 @@
package com.moez.QKSMS.model
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class BlockedNumber(
@PrimaryKey var id: Long = 0,
var address: String = ""
) : RealmObject()
@@ -223,6 +223,8 @@
<string name="settings_blocking_title">Blocking</string> <string name="settings_blocking_title">Blocking</string>
<string name="settings_blocking_manager_title">Blocking Manager</string> <string name="settings_blocking_manager_title">Blocking Manager</string>
<string name="settings_blocking_qksms_summary">Built-in blocking functionality in QKSMS</string> <string name="settings_blocking_qksms_summary">Built-in blocking functionality in QKSMS</string>
<string name="settings_blocking_block_external">Would you also like to block this number in %s?</string>
<string name="settings_blocking_unblock_external">Would you also like to block this number in %s?</string>
<!-- Do not translate --> <!-- Do not translate -->
<string name="settings_call_control_title">Call Control</string> <string name="settings_call_control_title">Call Control</string>
<string name="settings_call_control_summary">Automatically filter your calls and messages in one convenient place! Community IQ™ allows you to prevent unwanted messages from community known spammers</string> <string name="settings_call_control_summary">Automatically filter your calls and messages in one convenient place! Community IQ™ allows you to prevent unwanted messages from community known spammers</string>