Allow toggling of SIA integration, and direct user to play store if not installed

This commit is contained in:
Moez Bhatti
2018-04-06 02:32:39 -04:00
parent 956b8c2b8d
commit 137280403e
13 changed files with 119 additions and 44 deletions
@@ -30,10 +30,14 @@ import androidx.os.bundleOf
import io.reactivex.Single
import io.reactivex.subjects.SingleSubject
import timber.log.Timber
import util.Preferences
import util.tryOrNull
import javax.inject.Inject
class ExternalBlockingManagerImpl @Inject constructor(private val context: Context) : ExternalBlockingManager {
class ExternalBlockingManagerImpl @Inject constructor(
private val context: Context,
private val prefs: Preferences
) : ExternalBlockingManager {
companion object {
const val RATING_UNKNOWN = 0
@@ -48,23 +52,33 @@ class ExternalBlockingManagerImpl @Inject constructor(private val context: Conte
* 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, address).shouldBlock()
return Binder(context, prefs, address).shouldBlock()
}
private class Binder(private val context: Context, private val address: String) : ServiceConnection {
private class Binder(
private val context: Context,
private val prefs: Preferences,
private val address: String
) : ServiceConnection {
private val subject: SingleSubject<Boolean> = SingleSubject.create()
private var serviceMessenger: Messenger? = null
private var isBound: Boolean = false
fun shouldBlock(): Single<Boolean> {
// If either version of Should I Answer? is installed, build the intent to request a rating
val intent = tryOrNull {
context.packageManager.getApplicationInfo("org.mistergroup.shouldianswerpersonal", 0).enabled
Intent("org.mistergroup.shouldianswerpersonal.PublicService").setPackage("org.mistergroup.shouldianswerpersonal")
} ?: tryOrNull {
context.packageManager.getApplicationInfo("org.mistergroup.muzutozvednout", 0).enabled
Intent("org.mistergroup.muzutozvednout.PublicService").setPackage("org.mistergroup.muzutozvednout")
var intent: Intent? = null
// If either version of Should I Answer? is installed and SIA is enabled, build the
// intent to request a rating
if (prefs.sia.get()) {
intent = tryOrNull {
context.packageManager.getApplicationInfo("org.mistergroup.shouldianswerpersonal", 0).enabled
Intent("org.mistergroup.shouldianswerpersonal.PublicService").setPackage("org.mistergroup.shouldianswerpersonal")
} ?: tryOrNull {
context.packageManager.getApplicationInfo("org.mistergroup.muzutozvednout", 0).enabled
Intent("org.mistergroup.muzutozvednout.PublicService").setPackage("org.mistergroup.muzutozvednout")
}
}
// If the intent isn't null, bind the service and wait for a result. Otherwise, don't block
@@ -91,14 +91,11 @@ class MessageRepositoryImpl @Inject constructor(
.findAll())
}
override fun getBlockedConversations(): Flowable<List<Conversation>> {
override fun getBlockedConversations(): RealmResults<Conversation> {
return Realm.getDefaultInstance()
.where(Conversation::class.java)
.equalTo("blocked", true)
.findAllAsync()
.asFlowable()
.filter { it.isLoaded }
.map { it.toList() }
}
override fun getConversationAsync(threadId: Long): Conversation {
+1
View File
@@ -45,6 +45,7 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
val nightStart = rxPrefs.getString("nightStart", "6:00 PM")
val nightEnd = rxPrefs.getString("nightEnd", "6:00 AM")
val black = rxPrefs.getBoolean("black", false)
val sia = rxPrefs.getBoolean("sia", false)
val autoEmoji = rxPrefs.getBoolean("autoEmoji", true)
val delivery = rxPrefs.getBoolean("delivery", false)
val textSize = rxPrefs.getInteger("textSize", TEXT_SIZE_NORMAL)
@@ -31,7 +31,7 @@ interface MessageRepository {
fun getConversationsSnapshot(): List<Conversation>
fun getBlockedConversations(): Flowable<List<Conversation>>
fun getBlockedConversations(): RealmResults<Conversation>
fun getConversationAsync(threadId: Long): Conversation
@@ -145,6 +145,15 @@ class Navigator @Inject constructor(private val context: Context, private val no
startActivityExternal(intent)
}
/**
* Launch the Play Store and display the Should I Answer? listing
*/
fun showSia() {
val url = "https://play.google.com/store/apps/details?id=org.mistergroup.shouldianswerpersonal"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
}
fun showSupport() {
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:")
@@ -20,15 +20,16 @@ package feature.blocked
import android.app.AlertDialog
import android.os.Bundle
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.base.QkThemedActivity
import injection.appComponent
import common.util.extensions.setVisible
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.blocked_activity.*
import common.base.QkThemedActivity
import kotlinx.android.synthetic.main.settings_switch_widget.view.*
import javax.inject.Inject
class BlockedActivity : QkThemedActivity<BlockedViewModel>(), BlockedView {
@@ -36,6 +37,7 @@ class BlockedActivity : QkThemedActivity<BlockedViewModel>(), BlockedView {
@Inject lateinit var blockedAdapter: BlockedAdapter
override val viewModelClass = BlockedViewModel::class
override val siaClickedIntent by lazy { shouldIAnswer.clicks() }
override val unblockIntent by lazy { blockedAdapter.unblock }
override val confirmUnblockIntent: Subject<Unit> = PublishSubject.create()
@@ -54,12 +56,14 @@ class BlockedActivity : QkThemedActivity<BlockedViewModel>(), BlockedView {
.autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) }
blockedAdapter.emptyView = empty
conversations.adapter = blockedAdapter
}
override fun render(state: BlockedState) {
blockedAdapter.flowable = state.data
empty.setVisible(state.empty)
shouldIAnswer.checkbox.isChecked = state.siaEnabled
blockedAdapter.updateData(state.data)
}
override fun showUnblockDialog() {
@@ -22,16 +22,16 @@ import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import com.moez.QKSMS.R
import common.base.QkRealmAdapter
import common.base.QkViewHolder
import io.reactivex.subjects.PublishSubject
import kotlinx.android.synthetic.main.blocked_list_item.view.*
import model.Contact
import model.Conversation
import model.PhoneNumber
import common.base.FlowableAdapter
import common.base.QkViewHolder
import javax.inject.Inject
class BlockedAdapter @Inject constructor(private val context: Context) : FlowableAdapter<Conversation>() {
class BlockedAdapter @Inject constructor(private val context: Context) : QkRealmAdapter<Conversation>() {
val unblock: PublishSubject<Long> = PublishSubject.create()
@@ -40,7 +40,7 @@ class BlockedAdapter @Inject constructor(private val context: Context) : Flowabl
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val conversation = getItem(position)
val conversation = getItem(position)!!
val view = holder.itemView
view.setOnClickListener { unblock.onNext(conversation.id) }
@@ -18,10 +18,10 @@
*/
package feature.blocked
import io.reactivex.Flowable
import io.realm.RealmResults
import model.Conversation
data class BlockedState(
val data: Flowable<List<Conversation>>? = null,
val empty: Boolean = false
val siaEnabled: Boolean = false,
val data: RealmResults<Conversation>? = null
)
@@ -18,13 +18,14 @@
*/
package feature.blocked
import io.reactivex.Observable
import common.base.QkView
import io.reactivex.Observable
interface BlockedView : QkView<BlockedState> {
val siaClickedIntent: Observable<*>
val unblockIntent: Observable<Long>
val confirmUnblockIntent: Observable<Unit>
val confirmUnblockIntent: Observable<*>
fun showUnblockDialog()
@@ -18,32 +18,51 @@
*/
package feature.blocked
import android.content.Context
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import common.Navigator
import common.base.QkViewModel
import injection.appComponent
import interactor.MarkUnblocked
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import common.base.QkViewModel
import repository.MessageRepository
import util.Preferences
import util.tryOrNull
import javax.inject.Inject
class BlockedViewModel : QkViewModel<BlockedView, BlockedState>(BlockedState()) {
@Inject lateinit var context: Context
@Inject lateinit var markUnblocked: MarkUnblocked
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var navigator: Navigator
@Inject lateinit var prefs: Preferences
init {
appComponent.inject(this)
val conversations = messageRepo.getBlockedConversations()
.doOnNext { conversations -> newState { it.copy(empty = conversations.isEmpty()) } }
newState { it.copy(data = messageRepo.getBlockedConversations()) }
newState { it.copy(data = conversations) }
disposables += prefs.sia.asObservable()
.subscribe { enabled -> newState { it.copy(siaEnabled = enabled) } }
}
override fun bindView(view: BlockedView) {
super.bindView(view)
view.siaClickedIntent
.map {
tryOrNull { context.packageManager.getApplicationInfo("org.mistergroup.shouldianswerpersonal", 0).enabled }
?: tryOrNull { context.packageManager.getApplicationInfo("org.mistergroup.muzutozvednout", 0).enabled }
?: false
}
.doOnNext { installed -> if (!installed) navigator.showSia() }
.withLatestFrom(prefs.sia.asObservable(), { installed, enabled -> installed && !enabled })
.autoDisposable(view.scope())
.subscribe { shouldEnable -> prefs.sia.set(shouldEnable) }
// Show confirm unblock conversation dialog
view.unblockIntent
.autoDisposable(view.scope())
@@ -43,18 +43,44 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<android.support.v7.widget.RecyclerView
android:id="@+id/conversations"
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
tools:listitem="@layout/blocked_list_item" />
app:layout_constraintTop_toBottomOf="@id/toolbar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<common.widget.PreferenceView
android:id="@+id/shouldIAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingTop="8dp"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:summary="@string/settings_should_i_answer_summary"
app:title="@string/settings_should_i_answer_title"
app:widget="@layout/settings_switch_widget" />
<common.widget.Separator
android:layout_width="match_parent"
android:layout_height="1dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/conversations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingTop="8dp"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="@layout/blocked_list_item" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<View
android:layout_width="match_parent"
+2 -1
View File
@@ -20,7 +20,8 @@
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
style="@style/Toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools"
tools:background="@color/white">
<common.widget.QkTextView
android:id="@+id/toolbarTitle"
+5 -2
View File
@@ -92,7 +92,10 @@
<string name="settings_notifications_title">Notifications</string>
<string name="settings_vibration_title">Vibration</string>
<string name="settings_ringtone_title">Sound</string>
<string name="settings_blocking_title">Blocked</string>
<string name="settings_blocking_title">Blocking</string>
<!-- Do not translate -->
<string name="settings_should_i_answer_title">Should I Answer?</string>
<string name="settings_should_i_answer_summary">Automatically filter messages from unsolicited numbers by using the \"Should I Answer\" app</string>
<string name="settings_delivery_title">Delivery confirmations</string>
<string name="settings_delivery_summary">Confirm that messages were sent successfully</string>
<string name="settings_qkreply_title">QK Reply</string>
@@ -105,7 +108,7 @@
<string name="settings_sync_title">Sync messages</string>
<string name="settings_sync_summary">Re-sync your messages with the native Android SMS database</string>
<string name="blocked_title">Blocked</string>
<string name="blocked_title">Blocking</string>
<string name="blocked_empty">Your blocked conversations will appear here</string>
<string name="blocked_unblock_dialog_title">Unblock</string>
<string name="blocked_unblock_dialog_message">Would you like to unblock this conversation?</string>