Make settings page more reasonable

This commit is contained in:
topjohnwu 2020-01-18 03:06:33 +08:00
parent cae43b26f4
commit e4bcdbd0c4
3 changed files with 60 additions and 52 deletions

View File

@ -10,10 +10,14 @@ import me.tatarka.bindingcollectionadapter2.OnItemBind
inline fun <T : ComparableRvItem<*>> diffListOf(
vararg newItems: T
) = diffListOf(newItems.toList())
inline fun <T : ComparableRvItem<*>> diffListOf(
newItems: List<T>
) = DiffObservableList(object : DiffObservableList.Callback<T> {
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 <T : ComparableRvItem<*>> filterableListOf(
vararg newItems: T

View File

@ -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()
}
}

View File

@ -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<SettingsItem>()
val itemBinding = itemBindingOf<SettingsItem> { 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<SettingsItem> {
// 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()