Don't show blocking dialog if not necessary

This commit is contained in:
moezbhatti
2019-08-23 01:30:50 +02:00
parent 1439e3f126
commit c5fa1ce0f5
11 changed files with 103 additions and 51 deletions
+1
View File
@@ -10,6 +10,7 @@ buildscript {
ext.androidx_viewpager_version = '1.0.0-alpha01'
ext.autodispose_version = '1.3.0'
ext.conductor_version = '2.1.5'
ext.coroutines_version = '1.2.2'
ext.dagger_version = "2.16"
ext.espresso_version = '3.1.0-alpha3'
ext.exoplayer_version = "2.8.1"
@@ -34,14 +34,12 @@ class BlockingManager @Inject constructor(
override fun isAvailable(): Boolean = client.isAvailable()
override fun getClientCapability(): BlockingClientCapability = client.getClientCapability()
override fun isBlocked(address: String): Single<Boolean> = client.isBlocked(address)
override fun canBlock(): Boolean = client.canBlock()
override fun block(addresses: List<String>): Completable = client.block(addresses)
override fun canUnblock(): Boolean = client.canUnblock()
override fun unblock(addresses: List<String>): Completable = client.unblock(addresses)
override fun openSettings() = client.openSettings()
@@ -46,6 +46,8 @@ class CallControlBlockingClient @Inject constructor(
override fun isAvailable(): Boolean = context.isInstalled("com.flexaspect.android.everycallcontrol")
override fun getClientCapability() = BlockingClientCapability.BLOCK_WITH_PERMISSION
override fun isBlocked(address: String): Single<Boolean> {
val uri = Uri.withAppendedPath(CallControl.LOOKUP_TEXT_URI, address)
return Single.fromCallable {
@@ -29,18 +29,16 @@ class QksmsBlockingClient @Inject constructor(
override fun isAvailable(): Boolean = true
override fun getClientCapability() = BlockingClientCapability.BLOCK_WITHOUT_PERMISSION
override fun isBlocked(address: String): Single<Boolean> = Single.fromCallable {
blockingRepo.isBlocked(address)
}
override fun canBlock(): Boolean = true
override fun block(addresses: List<String>): Completable = Completable.fromCallable {
blockingRepo.blockNumber(*addresses.toTypedArray())
}
override fun canUnblock(): Boolean = true
override fun unblock(addresses: List<String>): Completable = Completable.fromCallable {
blockingRepo.unblockNumbers(*addresses.toTypedArray())
}
@@ -53,6 +53,8 @@ class ShouldIAnswerBlockingClient @Inject constructor(
"org.mistergroup.muzutozvednout")
.any(context::isInstalled)
override fun getClientCapability() = BlockingClientCapability.CANT_BLOCK
/**
* Return a Single<Boolean> which emits whether or not the given [address] should be blocked
*/
@@ -28,28 +28,21 @@ interface BlockingClient {
*/
fun isAvailable(): Boolean
/**
* Returns the level of access that the given blocking client provides to QKSMS
*/
fun getClientCapability(): BlockingClientCapability
/**
* Return a Single<Boolean> which emits whether or not the given [address] should be blocked
*/
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 numbers or opens the manager
*/
fun block(addresses: List<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 numbers or opens the manager
*/
@@ -60,4 +53,4 @@ interface BlockingClient {
*/
fun openSettings()
}
}
@@ -0,0 +1,25 @@
/*
* 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
}
+2
View File
@@ -169,6 +169,8 @@ dependencies {
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
implementation "me.leolin:ShortcutBadger:1.1.21"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation project(":android-smsmms")
implementation project(":common")
implementation project(':data')
@@ -50,7 +50,7 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
private val selection = mutableListOf<Long>()
private var selection = listOf<Long>()
/**
* Toggles the selected state for a particular view
@@ -61,9 +61,9 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
protected fun toggleSelection(id: Long, force: Boolean = true): Boolean {
if (!force && selection.isEmpty()) return false
when (selection.contains(id)) {
true -> selection.remove(id)
false -> selection.add(id)
selection = when (selection.contains(id)) {
true -> selection - id
false -> selection + id
}
selectionChanges.onNext(selection)
@@ -75,7 +75,7 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
}
fun clearSelection() {
selection.clear()
selection = listOf()
selectionChanges.onNext(selection)
notifyDataSetChanged()
}
@@ -23,10 +23,15 @@ import android.app.AlertDialog
import android.content.Context
import com.moez.QKSMS.R
import com.moez.QKSMS.blocking.BlockingClient
import com.moez.QKSMS.blocking.BlockingClientCapability
import com.moez.QKSMS.interactor.MarkBlocked
import com.moez.QKSMS.interactor.MarkUnblocked
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.util.Preferences
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
// TODO: Once we have a custom dialog based on conductor, turn this into a controller
@@ -39,28 +44,45 @@ class BlockingDialog @Inject constructor(
private val markUnblocked: MarkUnblocked
) {
fun show(activity: Activity, conversationIds: List<Long>, block: Boolean) {
fun show(activity: Activity, conversationIds: List<Long>, block: Boolean) = GlobalScope.launch {
val addresses = conversationIds.toLongArray()
.let(conversationRepo::getConversations)
.let { conversationRepo.getConversations(*it) }
.flatMap { conversation -> conversation.recipients }
.map { it.address }
.distinct()
// If we can block/unblock in the external manager, then just fire that off and exit
if (block) {
markBlocked.execute(conversationIds)
if (blockingManager.canBlock()) {
blockingManager.block(addresses).subscribe()
return
}
} else {
markUnblocked.execute(conversationIds)
if (blockingManager.canUnblock()) {
blockingManager.unblock(addresses).subscribe()
return
}
if (addresses.isEmpty()) {
return@launch
}
if (blockingManager.getClientCapability() == BlockingClientCapability.BLOCK_WITHOUT_PERMISSION) {
// If we can block/unblock in the external manager, then just fire that off and exit
if (block) {
markBlocked.execute(conversationIds)
blockingManager.block(addresses).subscribe()
} else {
markUnblocked.execute(conversationIds)
blockingManager.unblock(addresses).subscribe()
}
} else if (addresses.all { address -> block == blockingManager.isBlocked(address).blockingGet() }) {
// If all of the addresses are already in their correct state in the blocking manager, just marked the
// conversations blocked and exit
when (block) {
true -> markBlocked.execute(conversationIds)
false -> markUnblocked.execute(conversationIds)
}
} else {
// Otherwise, show the UI that lets the users know they need to mark the number as blocked in the client
showDialog(activity, conversationIds, addresses, block)
}
}
private suspend fun showDialog(
activity: Activity,
conversationIds: List<Long>,
addresses: List<String>,
block: Boolean
) = withContext(MainScope().coroutineContext) {
val res = when (block) {
true -> R.plurals.blocking_block_external
false -> R.plurals.blocking_unblock_external
@@ -77,13 +99,19 @@ class BlockingDialog @Inject constructor(
// Otherwise, show a dialog asking the user if they want to be directed to the external
// blocking manager
AlertDialog.Builder(activity)
.setTitle(R.string.main_menu_block)
.setTitle(when (block) {
true -> R.string.blocking_block_title
false -> R.string.blocking_unblock_title
})
.setMessage(message)
.setPositiveButton(R.string.button_yes) { _, _ ->
when (block) {
true -> blockingManager.block(addresses)
false -> blockingManager.unblock(addresses)
}.subscribe()
.setPositiveButton(R.string.button_continue) { _, _ ->
if (block) {
markBlocked.execute(conversationIds)
blockingManager.block(addresses).subscribe()
} else {
markUnblocked.execute(conversationIds)
blockingManager.unblock(addresses).subscribe()
}
}
.setNegativeButton(R.string.button_cancel) { _, _ -> }
.create()
+7 -4
View File
@@ -274,13 +274,15 @@
<string name="blocked_numbers_dialog_hint">Phone number</string>
<string name="blocked_numbers_dialog_block">Block</string>
<string name="blocking_block_title">Block</string>
<string name="blocking_unblock_title">Unblock</string>
<plurals name="blocking_block_external">
<item quantity="one">Continue to %s to block this number</item>
<item quantity="other">Continue to %s to block these numbers</item>
<item quantity="one">Continue to %s and block this number</item>
<item quantity="other">Continue to %s and block these numbers</item>
</plurals>
<plurals name="blocking_unblock_external">
<item quantity="one">Continue to %s to unblock this number</item>
<item quantity="other">Continue to %s to unblock these numbers</item>
<item quantity="one">Continue to %s and allow this number</item>
<item quantity="other">Continue to %s and allow these numbers</item>
</plurals>
<string name="about_title">About</string>
@@ -346,6 +348,7 @@
</string-array>
<string name="button_yes">Yes</string>
<string name="button_continue">Continue</string>
<string name="button_cancel">Cancel</string>
<string name="button_delete">Delete</string>
<string name="button_save">Save</string>