From 649b49ff4547a76aa417a8e7b4d997b7f6457e42 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 18 Apr 2021 04:14:43 -0700 Subject: [PATCH] Don't hold resources in Settings objects --- .../magisk/ui/settings/BaseSettingsItem.kt | 33 +++++-------- .../magisk/ui/settings/SettingsItems.kt | 46 +++++++++++-------- app/src/main/res/values/arrays.xml | 9 ---- 3 files changed, 39 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/settings/BaseSettingsItem.kt b/app/src/main/java/com/topjohnwu/magisk/ui/settings/BaseSettingsItem.kt index 285f7ddf4..54c1268ee 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/settings/BaseSettingsItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/settings/BaseSettingsItem.kt @@ -3,18 +3,14 @@ package com.topjohnwu.magisk.ui.settings import android.content.Context import android.content.res.Resources import android.view.View -import androidx.annotation.ArrayRes import androidx.annotation.CallSuper import androidx.databinding.Bindable import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R import com.topjohnwu.magisk.databinding.ObservableItem -import com.topjohnwu.magisk.ktx.get import com.topjohnwu.magisk.utils.TextHolder -import com.topjohnwu.magisk.utils.asText import com.topjohnwu.magisk.utils.set import com.topjohnwu.magisk.view.MagiskDialog -import org.koin.core.component.KoinComponent sealed class BaseSettingsItem : ObservableItem() { @@ -141,34 +137,29 @@ sealed class BaseSettingsItem : ObservableItem() { abstract fun getView(context: Context): View } - abstract class Selector : Value(), KoinComponent { + abstract class Selector : Value() { - protected val resources get() = get() + open val entryRes get() = -1 + open val descriptionRes get() = entryRes + open fun entries(res: Resources) = res.getArrayOrEmpty(entryRes) + open fun descriptions(res: Resources) = res.getArrayOrEmpty(descriptionRes) - @ArrayRes open val entryRes = -1 - @ArrayRes open val entryValRes = -1 - - open val entries get() = resources.getArrayOrEmpty(entryRes) - open val entryValues get() = resources.getArrayOrEmpty(entryValRes) - - override val description: TextHolder - get() = entries.getOrNull(value)?.asText() ?: TextHolder.EMPTY + override val description = object : TextHolder() { + override fun getText(resources: Resources): CharSequence { + return descriptions(resources).getOrElse(value) { "" } + } + } private fun Resources.getArrayOrEmpty(id: Int): Array = runCatching { getStringArray(id) }.getOrDefault(emptyArray()) - override fun onPressed(view: View, callback: Callback) { - if (entries.isEmpty()) return - super.onPressed(view, callback) - } - override fun onPressed(view: View) { MagiskDialog(view.context) - .applyTitle(title.getText(resources)) + .applyTitle(title.getText(view.resources)) .applyButton(MagiskDialog.ButtonType.NEGATIVE) { titleRes = android.R.string.cancel } - .applyAdapter(entries) { + .applyAdapter(entries(view.resources)) { value = it } .reveal() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt index d25d504c8..1b532e479 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt @@ -1,8 +1,10 @@ package com.topjohnwu.magisk.ui.settings import android.content.Context +import android.content.res.Resources import android.os.Build import android.view.LayoutInflater +import android.view.View import androidx.databinding.Bindable import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BuildConfig @@ -20,7 +22,6 @@ import com.topjohnwu.magisk.databinding.DialogSettingsAppNameBinding import com.topjohnwu.magisk.databinding.DialogSettingsDownloadPathBinding import com.topjohnwu.magisk.databinding.DialogSettingsUpdateChannelBinding import com.topjohnwu.magisk.ktx.get -import com.topjohnwu.magisk.utils.TextHolder import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.asText import com.topjohnwu.magisk.utils.set @@ -41,8 +42,17 @@ object Language : BaseSettingsItem.Selector() { } override val title = R.string.language.asText() - override var entries = emptyArray() - override var entryValues = emptyArray() + + private var entries = emptyArray() + private var entryValues = emptyArray() + + override fun entries(res: Resources) = entries + override fun descriptions(res: Resources) = entries + + override fun onPressed(view: View, callback: Callback) { + if (entries.isEmpty()) return + super.onPressed(view, callback) + } suspend fun loadLanguages(scope: CoroutineScope) { scope.launch { @@ -137,20 +147,22 @@ object DownloadPath : BaseSettingsItem.Input() { } object UpdateChannel : BaseSettingsItem.Selector() { - override var value = Config.updateChannel + override var value = Config.updateChannel.let { if (it < 0) 0 else it } set(value) = setV(value, field, { field = it }) { Config.updateChannel = it Info.remote = Info.EMPTY_REMOTE } override val title = R.string.settings_update_channel_title.asText() - override val entries: Array = resources.getStringArray(R.array.update_channel).let { - if (BuildConfig.VERSION_CODE % 100 == 0) - it.toMutableList().apply { removeAt(Config.Value.CANARY_CHANNEL) }.toTypedArray() - else it + + override val entryRes = R.array.update_channel + override fun entries(res: Resources): Array { + return super.entries(res).let { + if (!BuildConfig.DEBUG) + it.copyOfRange(0, Config.Value.CANARY_CHANNEL) + else it + } } - override val description - get() = entries.getOrNull(value)?.asText() ?: TextHolder.String(entries[0]) } object UpdateChannelUrl : BaseSettingsItem.Input() { @@ -272,15 +284,13 @@ object AccessMode : BaseSettingsItem.Selector() { object MultiuserMode : BaseSettingsItem.Selector() { override val title = R.string.multiuser_mode.asText() override val entryRes = R.array.multiuser_mode + override val descriptionRes = R.array.multiuser_summary override var value = Config.suMultiuserMode set(value) = setV(value, field, { field = it }) { Config.suMultiuserMode = it } - override val description - get() = resources.getStringArray(R.array.multiuser_summary)[value].asText() - override fun refresh() { isEnabled = Const.USER_ID == 0 } @@ -289,14 +299,12 @@ object MultiuserMode : BaseSettingsItem.Selector() { object MountNamespaceMode : BaseSettingsItem.Selector() { override val title = R.string.mount_namespace_mode.asText() override val entryRes = R.array.namespace + override val descriptionRes = R.array.namespace_summary override var value = Config.suMntNamespaceMode set(value) = setV(value, field, { field = it }) { Config.suMntNamespaceMode = it } - - override val description - get() = resources.getStringArray(R.array.namespace_summary)[value].asText() } object AutomaticResponse : BaseSettingsItem.Selector() { @@ -312,15 +320,15 @@ object AutomaticResponse : BaseSettingsItem.Selector() { object RequestTimeout : BaseSettingsItem.Selector() { override val title = R.string.request_timeout.asText() override val entryRes = R.array.request_timeout - override val entryValRes = R.array.request_timeout_value + private val entryValues = listOf(10, 15, 20, 30, 45, 60) override var value = selected set(value) = setV(value, field, { field = it }) { - Config.suDefaultTimeout = entryValues[it].toInt() + Config.suDefaultTimeout = entryValues[it] } private val selected: Int - get() = entryValues.indexOfFirst { it.toInt() == Config.suDefaultTimeout } + get() = entryValues.indexOfFirst { it == Config.suDefaultTimeout } } object SUNotification : BaseSettingsItem.Selector() { diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 73f6c4ac2..999411173 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -25,15 +25,6 @@ @string/settings_su_request_60 - - 10 - 15 - 20 - 30 - 45 - 60 - - @string/prompt @string/deny