From 1418bc454d7521d3546209c6def6ad03f12d36d6 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 18 Apr 2021 02:12:53 -0700 Subject: [PATCH] Don't hold resources in ViewModels --- .../topjohnwu/magisk/di/ViewModelsModule.kt | 2 +- .../magisk/ui/flash/FlashViewModel.kt | 2 +- .../magisk/ui/superuser/SpinnerRvItem.kt | 13 ------ .../magisk/ui/surequest/SuRequestViewModel.kt | 43 +++++++++++-------- .../com/topjohnwu/magisk/utils/TextHolder.kt | 2 +- app/src/main/res/layout/activity_request.xml | 5 ++- app/src/main/res/layout/item_spinner.xml | 7 +-- 7 files changed, 33 insertions(+), 41 deletions(-) delete mode 100644 app/src/main/java/com/topjohnwu/magisk/ui/superuser/SpinnerRvItem.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt index aa2722f92..4a8b6b1e8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt @@ -28,5 +28,5 @@ val viewModelModules = module { viewModel { InstallViewModel(get()) } viewModel { MainViewModel() } viewModel { (args: FlashFragmentArgs) -> FlashViewModel(args) } - viewModel { SuRequestViewModel(get(), get(), get(SUTimeout), get()) } + viewModel { SuRequestViewModel(get(), get(SUTimeout)) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt index da86e6dc3..a6b55a5d2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt @@ -26,7 +26,7 @@ import com.topjohnwu.superuser.Shell import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -class FlashViewModel(private val args: FlashFragmentArgs) : BaseViewModel() { +class FlashViewModel(var args: FlashFragmentArgs) : BaseViewModel() { @get:Bindable var showReboot = Shell.rootAccess() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SpinnerRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SpinnerRvItem.kt deleted file mode 100644 index 0b078905d..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SpinnerRvItem.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.topjohnwu.magisk.ui.superuser - -import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.databinding.ComparableRvItem - -class SpinnerRvItem(val item: String) : ComparableRvItem() { - - override val layoutRes: Int = R.layout.item_spinner - - override fun contentSameAs(other: SpinnerRvItem) = itemSameAs(other) - override fun itemSameAs(other: SpinnerRvItem) = item == other.item - -} diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt index e5b75947e..d2f7c5377 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt @@ -1,9 +1,9 @@ package com.topjohnwu.magisk.ui.surequest import android.annotation.SuppressLint +import android.content.Context import android.content.Intent import android.content.SharedPreferences -import android.content.pm.PackageManager import android.content.res.Resources import android.graphics.drawable.Drawable import android.os.Bundle @@ -30,19 +30,17 @@ import com.topjohnwu.magisk.core.utils.BiometricHelper import com.topjohnwu.magisk.events.DieEvent import com.topjohnwu.magisk.events.ShowUIEvent import com.topjohnwu.magisk.events.dialog.BiometricEvent -import com.topjohnwu.magisk.ui.superuser.SpinnerRvItem +import com.topjohnwu.magisk.ktx.get +import com.topjohnwu.magisk.utils.TextHolder import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.set import kotlinx.coroutines.launch -import me.tatarka.bindingcollectionadapter2.BindingListViewAdapter import me.tatarka.bindingcollectionadapter2.ItemBinding import java.util.concurrent.TimeUnit.SECONDS class SuRequestViewModel( - pm: PackageManager, policyDB: PolicyDao, - private val timeoutPrefs: SharedPreferences, - private val res: Resources + private val timeoutPrefs: SharedPreferences ) : BaseViewModel() { lateinit var icon: Drawable @@ -50,8 +48,7 @@ class SuRequestViewModel( lateinit var packageName: String @get:Bindable - var denyText = res.getString(R.string.deny) - set(value) = set(value, field, { field = it }, BR.denyText) + val denyText = DenyText() @get:Bindable var selectedItemPosition = 0 @@ -74,15 +71,9 @@ class SuRequestViewModel( false } - private val items = res.getStringArray(R.array.allow_timeout).map { SpinnerRvItem(it) } - val adapter = BindingListViewAdapter(1).apply { - itemBinding = ItemBinding.of { binding, _, item -> - item.bind(binding) - } - setItems(items) - } + val itemBinding = ItemBinding.of(BR.item, R.layout.item_spinner) - private val handler = SuRequestHandler(pm, policyDB) + private val handler = SuRequestHandler(get().packageManager, policyDB) private lateinit var timer: CountDownTimer fun grantPressed() { @@ -143,7 +134,7 @@ class SuRequestViewModel( private fun cancelTimer() { timer.cancel() - denyText = res.getString(R.string.deny) + denyText.seconds = 0 } private inner class SuTimer( @@ -155,16 +146,30 @@ class SuRequestViewModel( if (!grantEnabled && remains <= millis - 1000) { grantEnabled = true } - denyText = "${res.getString(R.string.deny)} (${(remains / 1000) + 1})" + denyText.seconds = (remains / 1000).toInt() + 1 } override fun onFinish() { - denyText = res.getString(R.string.deny) + denyText.seconds = 0 respond(DENY) } } + inner class DenyText : TextHolder() { + var seconds = 0 + set(value) = set(value, field, { field = it }, BR.denyText) + + override val isEmpty get() = false + + override fun getText(resources: Resources): CharSequence { + return if (seconds != 0) + "${resources.getString(R.string.deny)} ($seconds)" + else + resources.getString(R.string.deny) + } + } + // Invisible for accessibility services object EmptyAccessibilityDelegate : View.AccessibilityDelegate() { override fun sendAccessibilityEvent(host: View?, eventType: Int) {} diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/TextHolder.kt b/app/src/main/java/com/topjohnwu/magisk/utils/TextHolder.kt index a64bea0d3..06446247c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/TextHolder.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/TextHolder.kt @@ -4,7 +4,7 @@ import android.content.res.Resources import android.widget.TextView import androidx.databinding.BindingAdapter -sealed class TextHolder { +abstract class TextHolder { abstract val isEmpty: Boolean abstract fun getText(resources: Resources): CharSequence diff --git a/app/src/main/res/layout/activity_request.xml b/app/src/main/res/layout/activity_request.xml index 6183c2298..709516897 100644 --- a/app/src/main/res/layout/activity_request.xml +++ b/app/src/main/res/layout/activity_request.xml @@ -5,6 +5,8 @@ + + @@ -99,7 +101,8 @@ android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:enabled="@{viewModel.grantEnabled}" - android:adapter="@{viewModel.adapter}" + app:items="@{Arrays.asList(@stringArray/allow_timeout)}" + app:itemBinding="@{viewModel.itemBinding}" android:selection="@={viewModel.selectedItemPosition}" /> - +