Magisk/app/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsItems.kt

343 lines
11 KiB
Kotlin
Raw Normal View History

2020-01-12 17:43:09 +01:00
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
2020-01-13 15:01:46 +01:00
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info
2020-07-24 07:43:25 +02:00
import com.topjohnwu.magisk.core.UpdateCheckService
2020-10-17 12:40:43 +02:00
import com.topjohnwu.magisk.core.tasks.HideAPK
2020-07-12 15:15:32 +02:00
import com.topjohnwu.magisk.core.utils.BiometricHelper
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
2020-07-12 15:15:32 +02:00
import com.topjohnwu.magisk.core.utils.availableLocales
import com.topjohnwu.magisk.core.utils.currentLocale
import com.topjohnwu.magisk.databinding.DialogSettingsAppNameBinding
import com.topjohnwu.magisk.databinding.DialogSettingsDownloadPathBinding
import com.topjohnwu.magisk.databinding.DialogSettingsUpdateChannelBinding
2020-07-11 14:36:31 +02:00
import com.topjohnwu.magisk.ktx.get
2020-08-18 15:31:15 +02:00
import com.topjohnwu.magisk.utils.Utils
2021-04-09 10:32:37 +02:00
import com.topjohnwu.magisk.utils.asText
2020-07-15 10:21:57 +02:00
import com.topjohnwu.magisk.utils.set
import com.topjohnwu.superuser.Shell
2020-07-10 13:19:18 +02:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
// --- Customization
object Customization : BaseSettingsItem.Section() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_customization.asText()
}
object Language : BaseSettingsItem.Selector() {
2020-07-15 10:21:57 +02:00
override var value = -1
2020-08-11 12:30:00 +02:00
set(value) = setV(value, field, { field = it }) {
2020-07-15 10:21:57 +02:00
Config.locale = entryValues[it]
}
2021-04-09 10:32:37 +02:00
override val title = R.string.language.asText()
private var entries = emptyArray<String>()
private var entryValues = emptyArray<String>()
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)
}
2020-07-10 13:19:18 +02:00
suspend fun loadLanguages(scope: CoroutineScope) {
scope.launch {
availableLocales().let { (names, values) ->
entries = names
entryValues = values
val selectedLocale = currentLocale.getDisplayName(currentLocale)
value = names.indexOfFirst { it == selectedLocale }.let { if (it == -1) 0 else it }
}
}
}
}
object Theme : BaseSettingsItem.Blank() {
override val icon = R.drawable.ic_paint
2021-04-09 10:32:37 +02:00
override val title = R.string.section_theme.asText()
}
2021-02-11 11:34:00 +01:00
// --- App
2021-02-11 11:34:00 +01:00
object AppSettings : BaseSettingsItem.Section() {
2021-04-09 10:32:37 +02:00
override val title = R.string.home_app_title.asText()
}
object ClearRepoCache : BaseSettingsItem.Blank() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_clear_cache_title.asText()
override val description = R.string.settings_clear_cache_summary.asText()
override fun refresh() {
isEnabled = Info.env.isActive
}
}
object Hide : BaseSettingsItem.Input() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_hide_app_title.asText()
override val description = R.string.settings_hide_app_summary.asText()
2020-08-11 09:54:19 +02:00
2020-08-23 13:24:06 +02:00
override var value = ""
set(value) = setV(value, field, { field = it })
override val inputResult
get() = if (isError) null else result
2020-01-17 18:34:46 +01:00
@get:Bindable
var result = "Settings"
set(value) = set(value, field, { field = it }, BR.result, BR.error)
val maxLength
2020-10-17 12:40:43 +02:00
get() = HideAPK.MAX_LABEL_LENGTH
@get:Bindable
val isError
get() = result.length > maxLength || result.isBlank()
override fun getView(context: Context) = DialogSettingsAppNameBinding
.inflate(LayoutInflater.from(context)).also { it.data = this }.root
override fun refresh() {
isEnabled = Info.remote.stub.versionCode > 0
}
}
object Restore : BaseSettingsItem.Blank() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_restore_app_title.asText()
override val description = R.string.settings_restore_app_summary.asText()
}
2020-08-21 12:36:12 +02:00
object AddShortcut : BaseSettingsItem.Blank() {
2021-04-09 10:32:37 +02:00
override val title = R.string.add_shortcut_title.asText()
override val description = R.string.setting_add_shortcut_summary.asText()
2020-08-21 12:36:12 +02:00
}
object DownloadPath : BaseSettingsItem.Input() {
override var value = Config.downloadDir
set(value) = setV(value, field, { field = it }) { Config.downloadDir = it }
2020-08-11 09:54:19 +02:00
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_download_path_title.asText()
override val description get() = path.asText()
2020-08-11 09:54:19 +02:00
2020-08-21 16:36:59 +02:00
override val inputResult: String get() = result
2020-01-17 18:34:46 +01:00
@get:Bindable
2020-07-15 10:21:57 +02:00
var result = value
set(value) = set(value, field, { field = it }, BR.result, BR.path)
2020-01-17 18:34:46 +01:00
@get:Bindable
val path
get() = MediaStoreUtils.fullPath(result)
override fun getView(context: Context) = DialogSettingsDownloadPathBinding
.inflate(LayoutInflater.from(context)).also { it.data = this }.root
}
object UpdateChannel : BaseSettingsItem.Selector() {
override var value = Config.updateChannel.let { if (it < 0) 0 else it }
2021-01-25 13:13:08 +01:00
set(value) = setV(value, field, { field = it }) {
Config.updateChannel = it
Info.remote = Info.EMPTY_REMOTE
}
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_update_channel_title.asText()
override val entryRes = R.array.update_channel
override fun entries(res: Resources): Array<String> {
return super.entries(res).let {
if (!BuildConfig.DEBUG)
it.copyOfRange(0, Config.Value.CANARY_CHANNEL)
else it
}
}
}
object UpdateChannelUrl : BaseSettingsItem.Input() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_update_custom.asText()
2020-07-15 10:21:57 +02:00
override var value = Config.customChannelUrl
2021-01-25 13:13:08 +01:00
set(value) = setV(value, field, { field = it }) {
Config.customChannelUrl = it
Info.remote = Info.EMPTY_REMOTE
}
2021-04-09 10:32:37 +02:00
override val description get() = value.asText()
2020-08-11 09:54:19 +02:00
override val inputResult get() = result
2020-01-17 18:34:46 +01:00
@get:Bindable
2020-07-15 10:21:57 +02:00
var result = value
set(value) = set(value, field, { field = it }, BR.result)
override fun refresh() {
isEnabled = UpdateChannel.value == Config.Value.CUSTOM_CHANNEL
}
override fun getView(context: Context) = DialogSettingsUpdateChannelBinding
.inflate(LayoutInflater.from(context)).also { it.data = this }.root
}
object UpdateChecker : BaseSettingsItem.Toggle() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_check_update_title.asText()
override val description = R.string.settings_check_update_summary.asText()
2020-07-15 10:21:57 +02:00
override var value = Config.checkUpdate
set(value) = setV(value, field, { field = it }) {
Config.checkUpdate = it
2020-07-24 07:43:25 +02:00
UpdateCheckService.schedule(get())
2020-07-15 10:21:57 +02:00
}
}
object DoHToggle : BaseSettingsItem.Toggle() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_doh_title.asText()
override val description = R.string.settings_doh_description.asText()
override var value = Config.doh
set(value) = setV(value, field, { field = it }) {
Config.doh = it
}
}
// check whether is module already installed beforehand?
object SystemlessHosts : BaseSettingsItem.Blank() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_hosts_title.asText()
override val description = R.string.settings_hosts_summary.asText()
}
2020-10-22 11:40:47 +02:00
object Tapjack : BaseSettingsItem.Toggle() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_su_tapjack_title.asText()
override var description = R.string.settings_su_tapjack_summary.asText()
2020-10-22 11:40:47 +02:00
override var value = Config.suTapjack
set(value) = setV(value, field, { field = it }) { Config.suTapjack = it }
}
object Biometrics : BaseSettingsItem.Toggle() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_su_biometric_title.asText()
2020-07-15 10:21:57 +02:00
override var value = Config.suBiometric
set(value) = setV(value, field, { field = it }) { Config.suBiometric = it }
2021-04-09 10:32:37 +02:00
override var description = R.string.settings_su_biometric_summary.asText()
override fun refresh() {
2020-01-17 20:06:33 +01:00
isEnabled = BiometricHelper.isSupported
if (!isEnabled) {
value = false
2021-04-09 10:32:37 +02:00
description = R.string.no_biometric.asText()
}
}
}
object Reauthenticate : BaseSettingsItem.Toggle() {
2021-04-09 10:32:37 +02:00
override val title = R.string.settings_su_reauth_title.asText()
override val description = R.string.settings_su_reauth_summary.asText()
2020-07-15 10:21:57 +02:00
override var value = Config.suReAuth
set(value) = setV(value, field, { field = it }) { Config.suReAuth = it }
override fun refresh() {
isEnabled = Build.VERSION.SDK_INT < Build.VERSION_CODES.O && Utils.showSuperUser()
}
}
// --- Magisk
object Magisk : BaseSettingsItem.Section() {
2021-04-09 10:32:37 +02:00
override val title = R.string.magisk.asText()
}
object MagiskHide : BaseSettingsItem.Toggle() {
2021-04-09 10:32:37 +02:00
override val title = R.string.magiskhide.asText()
override val description = R.string.settings_magiskhide_summary.asText()
2020-07-15 10:21:57 +02:00
override var value = Config.magiskHide
set(value) = setV(value, field, { field = it }) {
2020-09-23 22:50:10 +02:00
val cmd = if (it) "enable" else "disable"
2021-02-24 21:24:44 +01:00
Shell.su("magiskhide $cmd").submit { cb ->
2020-09-23 22:50:10 +02:00
if (cb.isSuccess) Config.magiskHide = it
else field = !it
2020-07-15 10:21:57 +02:00
}
}
}
// --- Superuser
object Superuser : BaseSettingsItem.Section() {
2021-04-09 10:32:37 +02:00
override val title = R.string.superuser.asText()
}
object AccessMode : BaseSettingsItem.Selector() {
2021-04-09 10:32:37 +02:00
override val title = R.string.superuser_access.asText()
2020-02-18 23:02:08 +01:00
override val entryRes = R.array.su_access
2020-07-15 10:21:57 +02:00
override var value = Config.rootMode
2020-08-11 12:30:00 +02:00
set(value) = setV(value, field, { field = it }) {
2020-11-30 18:26:58 +01:00
Config.rootMode = it
2020-07-15 10:21:57 +02:00
}
}
object MultiuserMode : BaseSettingsItem.Selector() {
2021-04-09 10:32:37 +02:00
override val title = R.string.multiuser_mode.asText()
2020-02-18 23:02:08 +01:00
override val entryRes = R.array.multiuser_mode
override val descriptionRes = R.array.multiuser_summary
2020-01-17 18:34:46 +01:00
2020-07-15 10:21:57 +02:00
override var value = Config.suMultiuserMode
2020-08-11 12:30:00 +02:00
set(value) = setV(value, field, { field = it }) {
2020-11-30 18:26:58 +01:00
Config.suMultiuserMode = it
2020-07-15 10:21:57 +02:00
}
2020-01-17 18:34:46 +01:00
override fun refresh() {
2020-01-17 20:06:33 +01:00
isEnabled = Const.USER_ID == 0
}
}
object MountNamespaceMode : BaseSettingsItem.Selector() {
2021-04-09 10:32:37 +02:00
override val title = R.string.mount_namespace_mode.asText()
2020-02-18 23:02:08 +01:00
override val entryRes = R.array.namespace
override val descriptionRes = R.array.namespace_summary
2020-01-17 18:34:46 +01:00
2020-07-15 10:21:57 +02:00
override var value = Config.suMntNamespaceMode
2020-08-11 12:30:00 +02:00
set(value) = setV(value, field, { field = it }) {
2020-11-30 18:26:58 +01:00
Config.suMntNamespaceMode = it
2020-07-15 10:21:57 +02:00
}
}
object AutomaticResponse : BaseSettingsItem.Selector() {
2021-04-09 10:32:37 +02:00
override val title = R.string.auto_response.asText()
2020-02-18 23:02:08 +01:00
override val entryRes = R.array.auto_response
2020-10-17 12:40:43 +02:00
override var value = Config.suAutoResponse
2020-08-11 12:30:00 +02:00
set(value) = setV(value, field, { field = it }) {
2020-11-30 18:26:58 +01:00
Config.suAutoResponse = it
2020-07-15 10:21:57 +02:00
}
}
object RequestTimeout : BaseSettingsItem.Selector() {
2021-04-09 10:32:37 +02:00
override val title = R.string.request_timeout.asText()
2020-02-18 23:02:08 +01:00
override val entryRes = R.array.request_timeout
private val entryValues = listOf(10, 15, 20, 30, 45, 60)
2020-07-15 10:21:57 +02:00
override var value = selected
2020-08-11 12:30:00 +02:00
set(value) = setV(value, field, { field = it }) {
Config.suDefaultTimeout = entryValues[it]
2020-07-15 10:21:57 +02:00
}
2020-01-17 18:34:46 +01:00
private val selected: Int
get() = entryValues.indexOfFirst { it == Config.suDefaultTimeout }
}
object SUNotification : BaseSettingsItem.Selector() {
2021-04-09 10:32:37 +02:00
override val title = R.string.superuser_notification.asText()
2020-02-18 23:02:08 +01:00
override val entryRes = R.array.su_notification
2020-07-15 10:21:57 +02:00
override var value = Config.suNotification
2020-08-11 12:30:00 +02:00
set(value) = setV(value, field, { field = it }) {
2020-11-30 18:26:58 +01:00
Config.suNotification = it
2020-07-15 10:21:57 +02:00
}
2020-01-12 08:00:49 +01:00
}