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
@@ -191,6 +191,15 @@ class Navigator @Inject constructor(
}
}
/**
* Launch the Play Store and display the Call Blocker listing
*/
fun installCallBlocker() {
val url = "https://play.google.com/store/apps/details?id=com.cuiet.blockCalls"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivityExternal(intent)
}
/**
* Launch the Play Store and display the Call Control listing
*/
@@ -77,7 +77,7 @@ class BlockingDialog @Inject constructor(
}
private fun allBlocked(addresses: List<String>): Boolean = addresses.all { address ->
blockingManager.getAction(address).blockingGet() is BlockingClient.Action.Block
blockingManager.isBlacklisted(address).blockingGet() is BlockingClient.Action.Block
}
private suspend fun showDialog(
@@ -92,8 +92,9 @@ class BlockingDialog @Inject constructor(
}
val manager = context.getString(when (prefs.blockingManager.get()) {
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
Preferences.BLOCKING_MANAGER_CB -> R.string.blocking_manager_call_blocker_title
Preferences.BLOCKING_MANAGER_CC -> R.string.blocking_manager_call_control_title
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
else -> R.string.blocking_manager_qksms_title
})
@@ -38,8 +38,9 @@ class BlockingPresenter @Inject constructor(
disposables += prefs.blockingManager.asObservable()
.map { client ->
when (client) {
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
Preferences.BLOCKING_MANAGER_CB -> R.string.blocking_manager_call_blocker_title
Preferences.BLOCKING_MANAGER_CC -> R.string.blocking_manager_call_control_title
Preferences.BLOCKING_MANAGER_SIA -> R.string.blocking_manager_sia_title
else -> R.string.blocking_manager_qksms_title
}
}
@@ -2,11 +2,14 @@ package com.moez.QKSMS.feature.blocking.manager
import android.app.Activity
import android.app.AlertDialog
import android.content.res.ColorStateList
import android.view.View
import androidx.core.view.isVisible
import androidx.core.view.isInvisible
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkController
import com.moez.QKSMS.common.util.Colors
import com.moez.QKSMS.common.util.extensions.resolveThemeColor
import com.moez.QKSMS.injection.appComponent
import com.moez.QKSMS.util.Preferences
import io.reactivex.Observable
@@ -14,12 +17,12 @@ import io.reactivex.Single
import io.reactivex.subjects.PublishSubject
import kotlinx.android.synthetic.main.blocking_manager_controller.*
import kotlinx.android.synthetic.main.blocking_manager_list_option.view.*
import kotlinx.android.synthetic.main.radio_preference_view.view.*
import javax.inject.Inject
class BlockingManagerController : QkController<BlockingManagerView, BlockingManagerState, BlockingManagerPresenter>(),
BlockingManagerView {
@Inject lateinit var colors: Colors
@Inject override lateinit var presenter: BlockingManagerPresenter
private val activityResumedSubject: PublishSubject<Unit> = PublishSubject.create()
@@ -35,6 +38,18 @@ class BlockingManagerController : QkController<BlockingManagerView, BlockingMana
presenter.bindIntents(this)
setTitle(R.string.blocking_manager_title)
showBackButton(true)
val states = arrayOf(
intArrayOf(android.R.attr.state_activated),
intArrayOf(-android.R.attr.state_activated))
val textTertiary = view.context.resolveThemeColor(android.R.attr.textColorTertiary)
val imageTintList = ColorStateList(states, intArrayOf(colors.theme().theme, textTertiary))
qksms.action.imageTintList = imageTintList
callBlocker.action.imageTintList = imageTintList
callControl.action.imageTintList = imageTintList
shouldIAnswer.action.imageTintList = imageTintList
}
override fun onActivityResumed(activity: Activity) {
@@ -42,17 +57,34 @@ class BlockingManagerController : QkController<BlockingManagerView, BlockingMana
}
override fun render(state: BlockingManagerState) {
qksms.radioButton.isChecked = state.blockingManager == Preferences.BLOCKING_MANAGER_QKSMS
qksms.action.setImageResource(getActionIcon(true))
qksms.action.isActivated = true
qksms.action.isInvisible = state.blockingManager != Preferences.BLOCKING_MANAGER_QKSMS
callControl.radioButton.isChecked = state.blockingManager == Preferences.BLOCKING_MANAGER_CC
callControl.install.isVisible = !state.callControlInstalled
callBlocker.action.setImageResource(getActionIcon(state.callBlockerInstalled))
callBlocker.action.isActivated = state.callBlockerInstalled
callBlocker.action.isInvisible = state.blockingManager != Preferences.BLOCKING_MANAGER_CB
&& state.callBlockerInstalled
shouldIAnswer.radioButton.isChecked = state.blockingManager == Preferences.BLOCKING_MANAGER_SIA
shouldIAnswer.install.isVisible = !state.siaInstalled
callControl.action.setImageResource(getActionIcon(state.callControlInstalled))
callControl.action.isActivated = state.callControlInstalled
callControl.action.isInvisible = state.blockingManager != Preferences.BLOCKING_MANAGER_CC
&& state.callControlInstalled
shouldIAnswer.action.setImageResource(getActionIcon(state.siaInstalled))
shouldIAnswer.action.isActivated = state.siaInstalled
shouldIAnswer.action.isInvisible = state.blockingManager != Preferences.BLOCKING_MANAGER_SIA
&& state.siaInstalled
}
private fun getActionIcon(installed: Boolean): Int = when {
!installed -> R.drawable.ic_chevron_right_black_24dp
else -> R.drawable.ic_check_white_24dp
}
override fun activityResumed(): Observable<*> = activityResumedSubject
override fun qksmsClicked(): Observable<*> = qksms.clicks()
override fun callBlockerClicked(): Observable<*> = callBlocker.clicks()
override fun callControlClicked(): Observable<*> = callControl.clicks()
override fun siaClicked(): Observable<*> = shouldIAnswer.clicks()
@@ -0,0 +1,93 @@
/*
* 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.feature.blocking.manager
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.moez.QKSMS.R
import com.moez.QKSMS.common.util.extensions.animateLayoutChanges
import com.moez.QKSMS.common.util.extensions.resolveThemeAttribute
import com.moez.QKSMS.common.util.extensions.setVisible
import kotlinx.android.synthetic.main.blocking_manager_preference_view.view.*
class BlockingManagerPreferenceView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
var icon: Drawable? = null
set(value) {
field = value
if (isInEditMode) {
findViewById<ImageView>(R.id.iconView).setImageDrawable(value)
} else {
iconView.setImageDrawable(value)
}
}
var title: String? = null
set(value) {
field = value
if (isInEditMode) {
findViewById<TextView>(R.id.titleView).text = value
} else {
titleView.text = value
}
}
var summary: String? = null
set(value) {
field = value
if (isInEditMode) {
findViewById<TextView>(R.id.summaryView).run {
text = value
setVisible(value?.isNotEmpty() == true)
}
} else {
summaryView.text = value
summaryView.setVisible(value?.isNotEmpty() == true)
}
}
init {
View.inflate(context, R.layout.blocking_manager_preference_view, this)
setBackgroundResource(context.resolveThemeAttribute(R.attr.selectableItemBackground))
context.obtainStyledAttributes(attrs, R.styleable.BlockingManagerPreferenceView).run {
icon = getDrawable(R.styleable.BlockingManagerPreferenceView_icon)
title = getString(R.styleable.BlockingManagerPreferenceView_title)
summary = getString(R.styleable.BlockingManagerPreferenceView_summary)
// If there's a custom view used for the preference's widget, inflate it
getResourceId(R.styleable.BlockingManagerPreferenceView_widget, -1).takeIf { it != -1 }?.let { id ->
View.inflate(context, id, widgetFrame)
}
recycle()
}
}
}
@@ -3,6 +3,7 @@ package com.moez.QKSMS.feature.blocking.manager
import android.content.Context
import com.moez.QKSMS.R
import com.moez.QKSMS.blocking.BlockingClient
import com.moez.QKSMS.blocking.CallBlockerBlockingClient
import com.moez.QKSMS.blocking.CallControlBlockingClient
import com.moez.QKSMS.blocking.QksmsBlockingClient
import com.moez.QKSMS.blocking.ShouldIAnswerBlockingClient
@@ -21,6 +22,7 @@ import javax.inject.Inject
class BlockingManagerPresenter @Inject constructor(
private val analytics: AnalyticsManager,
private val callBlocker: CallBlockerBlockingClient,
private val callControl: CallControlBlockingClient,
private val context: Context,
private val conversationRepo: ConversationRepository,
@@ -30,6 +32,7 @@ class BlockingManagerPresenter @Inject constructor(
private val shouldIAnswer: ShouldIAnswerBlockingClient
) : QkPresenter<BlockingManagerView, BlockingManagerState>(BlockingManagerState(
blockingManager = prefs.blockingManager.get(),
callBlockerInstalled = callBlocker.isAvailable(),
callControlInstalled = callControl.isAvailable(),
siaInstalled = shouldIAnswer.isAvailable()
)) {
@@ -42,6 +45,12 @@ class BlockingManagerPresenter @Inject constructor(
override fun bindIntents(view: BlockingManagerView) {
super.bindIntents(view)
view.activityResumed()
.map { callBlocker.isAvailable() }
.distinctUntilChanged()
.autoDisposable(view.scope())
.subscribe { available -> newState { copy(callBlockerInstalled = available) } }
view.activityResumed()
.map { callControl.isAvailable() }
.distinctUntilChanged()
@@ -64,6 +73,23 @@ class BlockingManagerPresenter @Inject constructor(
prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_QKSMS)
}
view.callBlockerClicked()
.filter {
val installed = callBlocker.isAvailable()
if (!installed) {
analytics.track("Install Call Blocker")
navigator.installCallBlocker()
}
val enabled = prefs.blockingManager.get() == Preferences.BLOCKING_MANAGER_CB
installed && !enabled
}
.autoDisposable(view.scope())
.subscribe {
analytics.setUserProperty("Blocking Manager", "Call Blocker")
prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_CB)
}
view.callControlClicked()
.filter {
val installed = callControl.isAvailable()
@@ -92,7 +118,7 @@ class BlockingManagerPresenter @Inject constructor(
.switchMap { numbers -> callControl.block(numbers).andThen(Observable.just(Unit)) } // Hack
.autoDisposable(view.scope())
.subscribe {
callControl.getAction("callcontrol").blockingGet()
callControl.shouldBlock("callcontrol").blockingGet()
analytics.setUserProperty("Blocking Manager", "Call Control")
prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_CC)
}
@@ -117,6 +143,6 @@ class BlockingManagerPresenter @Inject constructor(
private fun getAddressesToBlock(client: BlockingClient) = conversationRepo.getBlockedConversations()
.fold(listOf<String>(), { numbers, conversation -> numbers + conversation.recipients.map { it.address } })
.filter { number -> client.getAction(number).blockingGet() !is BlockingClient.Action.Block }
.filter { number -> client.isBlacklisted(number).blockingGet() !is BlockingClient.Action.Block }
}
@@ -2,6 +2,7 @@ package com.moez.QKSMS.feature.blocking.manager
data class BlockingManagerState(
val blockingManager: Int = 0,
val callBlockerInstalled: Boolean = false,
val callControlInstalled: Boolean = false,
val siaInstalled: Boolean = false
)
@@ -8,6 +8,7 @@ interface BlockingManagerView : QkViewContract<BlockingManagerState> {
fun activityResumed(): Observable<*>
fun qksmsClicked(): Observable<*>
fun callBlockerClicked(): Observable<*>
fun callControlClicked(): Observable<*>
fun siaClicked(): Observable<*>