From e4bcdbd0c4e693d476ec0f5bea4508269995d95c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 18 Jan 2020 03:06:33 +0800 Subject: [PATCH] Make settings page more reasonable --- .../topjohnwu/magisk/ui/base/CompatHelpers.kt | 6 +- .../magisk/ui/settings/SettingsItems.kt | 40 +---------- .../magisk/ui/settings/SettingsViewModel.kt | 66 +++++++++++++++---- 3 files changed, 60 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/base/CompatHelpers.kt b/app/src/main/java/com/topjohnwu/magisk/ui/base/CompatHelpers.kt index 27fd8c3dc..62ed84dcd 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/base/CompatHelpers.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/base/CompatHelpers.kt @@ -10,10 +10,14 @@ import me.tatarka.bindingcollectionadapter2.OnItemBind inline fun > diffListOf( vararg newItems: T +) = diffListOf(newItems.toList()) + +inline fun > diffListOf( + newItems: List ) = DiffObservableList(object : DiffObservableList.Callback { override fun areItemsTheSame(oldItem: T, newItem: T) = oldItem.genericItemSameAs(newItem) override fun areContentsTheSame(oldItem: T, newItem: T) = oldItem.genericContentSameAs(newItem) -}).also { it.update(newItems.toList()) } +}).also { it.update(newItems) } inline fun > filterableListOf( vararg newItems: T 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 c80f92d71..87b6b73b1 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 @@ -91,10 +91,6 @@ object Hide : SettingsItem.Input() { override fun getView(context: Context) = DialogSettingsAppNameBinding .inflate(LayoutInflater.from(context)).also { it.data = this }.root - - override fun refresh() { - isEnabled = Info.env.isActive - } } object Restore : SettingsItem.Blank() { @@ -183,10 +179,6 @@ object UpdateChecker : SettingsItem.Toggle() { object SystemlessHosts : SettingsItem.Blank() { override val title = R.string.settings_hosts_title.asTransitive() override val description = R.string.settings_hosts_summary.asTransitive() - - override fun refresh() { - isEnabled = Info.env.isActive - } } object Biometrics : SettingsItem.Toggle() { @@ -195,7 +187,7 @@ object Biometrics : SettingsItem.Toggle() { override var description = R.string.settings_su_biometric_summary.asTransitive() override fun refresh() { - isEnabled = BiometricHelper.isSupported && Utils.showSuperUser() + isEnabled = BiometricHelper.isSupported if (!isEnabled) { value = false description = R.string.no_biometric.asTransitive() @@ -232,10 +224,6 @@ object SafeMode : SettingsItem.Toggle() { } Utils.toast(R.string.settings_reboot_toast, Toast.LENGTH_LONG) } - - override fun refresh() { - isEnabled = Info.env.isActive - } } object MagiskHide : SettingsItem.Toggle() { @@ -248,10 +236,6 @@ object MagiskHide : SettingsItem.Toggle() { else -> Shell.su("magiskhide --disable").submit() } } - - override fun refresh() { - isEnabled = Info.env.isActive - } } // --- Superuser @@ -268,10 +252,6 @@ object AccessMode : SettingsItem.Selector() { override var value by bindableValue(Config.rootMode) { Config.rootMode = entryValues[it].toInt() } - - override fun refresh() { - isEnabled = Utils.showSuperUser() - } } object MultiuserMode : SettingsItem.Selector() { @@ -288,7 +268,7 @@ object MultiuserMode : SettingsItem.Selector() { get() = descArray[value].asTransitive() override fun refresh() { - isEnabled = Const.USER_ID <= 0 && Utils.showSuperUser() + isEnabled = Const.USER_ID == 0 } } @@ -304,10 +284,6 @@ object MountNamespaceMode : SettingsItem.Selector() { override val description get() = descArray[value].asTransitive() - - override fun refresh() { - isEnabled = Utils.showSuperUser() - } } object AutomaticResponse : SettingsItem.Selector() { @@ -318,10 +294,6 @@ object AutomaticResponse : SettingsItem.Selector() { override var value by bindableValue(Config.suAutoReponse) { Config.suAutoReponse = entryValues[it].toInt() } - - override fun refresh() { - isEnabled = Utils.showSuperUser() - } } object RequestTimeout : SettingsItem.Selector() { @@ -335,10 +307,6 @@ object RequestTimeout : SettingsItem.Selector() { private val selected: Int get() = entryValues.indexOfFirst { it.toInt() == Config.suDefaultTimeout } - - override fun refresh() { - isEnabled = Utils.showSuperUser() - } } object SUNotification : SettingsItem.Selector() { @@ -349,8 +317,4 @@ object SUNotification : SettingsItem.Selector() { override var value by bindableValue(Config.suNotification) { Config.suNotification = entryValues[it].toInt() } - - override fun refresh() { - isEnabled = Utils.showSuperUser() - } } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt index 4eeaeafb0..805e04089 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt @@ -1,12 +1,16 @@ package com.topjohnwu.magisk.ui.settings import android.Manifest +import android.os.Build import android.view.View import android.widget.Toast import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R +import com.topjohnwu.magisk.core.Const +import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.download.DownloadService import com.topjohnwu.magisk.core.utils.PatchAPK +import com.topjohnwu.magisk.core.utils.Utils import com.topjohnwu.magisk.data.database.RepoDao import com.topjohnwu.magisk.extensions.subscribeK import com.topjohnwu.magisk.model.entity.internal.Configuration @@ -20,7 +24,6 @@ import com.topjohnwu.magisk.ui.base.BaseViewModel import com.topjohnwu.magisk.ui.base.adapterOf import com.topjohnwu.magisk.ui.base.diffListOf import com.topjohnwu.magisk.ui.base.itemBindingOf -import com.topjohnwu.magisk.core.utils.Utils import com.topjohnwu.superuser.Shell import io.reactivex.Completable import io.reactivex.subjects.PublishSubject @@ -32,21 +35,58 @@ class SettingsViewModel( val adapter = adapterOf() val itemBinding = itemBindingOf { it.bindExtra(BR.callback, this) } - val items = diffListOf( - Customization, - Theme, Language, DownloadPath, GridSize, + val items = diffListOf(createItems()) - Manager, - UpdateChannel, UpdateChannelUrl, ClearRepoCache, HideOrRestore(), UpdateChecker, - Biometrics, Reauthenticate, + private fun createItems(): List { + // Customization + val list = mutableListOf( + Customization, + Theme, Language, GridSize + ) + if (Build.VERSION.SDK_INT < 21) { + // Pre 5.0 does not support getting colors from attributes, + // making theming a pain in the ass. Just forget about it + list.remove(Theme) + } - Magisk, - SafeMode, MagiskHide, SystemlessHosts, + // Manager + list.addAll(listOf( + Manager, + UpdateChannel, UpdateChannelUrl, UpdateChecker, DownloadPath + )) + if (Info.env.isActive) { + list.add(ClearRepoCache) + if (Const.USER_ID == 0 && Info.isConnected.value) + list.add(HideOrRestore()) + } - Superuser, - AccessMode, MultiuserMode, MountNamespaceMode, AutomaticResponse, RequestTimeout, - SUNotification - ) + // Magisk + if (Info.env.isActive) { + list.addAll(listOf( + Magisk, + MagiskHide, SystemlessHosts, SafeMode + )) + } + + // Superuser + if (Utils.showSuperUser()) { + list.addAll(listOf( + Superuser, + Biometrics, AccessMode, MultiuserMode, MountNamespaceMode, + AutomaticResponse, RequestTimeout, SUNotification + )) + if (Build.VERSION.SDK_INT < 23) { + // Biometric is only available on 6.0+ + list.remove(Biometrics) + } + if (Build.VERSION.SDK_INT < 26) { + // Re-authenticate is not feasible on 8.0+ + list.add(Reauthenticate) + } + } + + return list + } override fun onItemPressed(view: View, item: SettingsItem) = when (item) { is DownloadPath -> requireRWPermission()