/
SettingsItems.kt
330 lines (267 loc) · 11.1 KB
/
SettingsItems.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.UpdateCheckService
import com.topjohnwu.magisk.core.tasks.HideAPK
import com.topjohnwu.magisk.core.utils.BiometricHelper
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
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
import com.topjohnwu.magisk.databinding.set
import com.topjohnwu.magisk.di.AppContext
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.asText
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
// --- Customization
object Customization : BaseSettingsItem.Section() {
override val title = R.string.settings_customization.asText()
}
object Language : BaseSettingsItem.Selector() {
override var value = -1
set(value) = setV(value, field, { field = it }) {
Config.locale = entryValues[it]
}
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)
}
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
override val title = R.string.section_theme.asText()
}
// --- App
object AppSettings : BaseSettingsItem.Section() {
override val title = R.string.home_app_title.asText()
}
object Hide : BaseSettingsItem.Input() {
override val title = R.string.settings_hide_app_title.asText()
override val description = R.string.settings_hide_app_summary.asText()
override var value = ""
set(value) = setV(value, field, { field = it })
override val inputResult
get() = if (isError) null else result
@get:Bindable
var result = "Settings"
set(value) = set(value, field, { field = it }, BR.result, BR.error)
val maxLength
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() {
override val title = R.string.settings_restore_app_title.asText()
override val description = R.string.settings_restore_app_summary.asText()
}
object AddShortcut : BaseSettingsItem.Blank() {
override val title = R.string.add_shortcut_title.asText()
override val description = R.string.setting_add_shortcut_summary.asText()
}
object DownloadPath : BaseSettingsItem.Input() {
override var value = Config.downloadDir
set(value) = setV(value, field, { field = it }) { Config.downloadDir = it }
override val title = R.string.settings_download_path_title.asText()
override val description get() = path.asText()
override val inputResult: String get() = result
@get:Bindable
var result = value
set(value) = set(value, field, { field = it }, BR.result, BR.path)
@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 }
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 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() {
override val title = R.string.settings_update_custom.asText()
override var value = Config.customChannelUrl
set(value) = setV(value, field, { field = it }) {
Config.customChannelUrl = it
Info.remote = Info.EMPTY_REMOTE
}
override val description get() = value.asText()
override val inputResult get() = result
@get:Bindable
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() {
override val title = R.string.settings_check_update_title.asText()
override val description = R.string.settings_check_update_summary.asText()
override var value = Config.checkUpdate
set(value) = setV(value, field, { field = it }) {
Config.checkUpdate = it
UpdateCheckService.schedule(AppContext)
}
}
// check whether is module already installed beforehand?
object SystemlessHosts : BaseSettingsItem.Blank() {
override val title = R.string.settings_hosts_title.asText()
override val description = R.string.settings_hosts_summary.asText()
}
object Tapjack : BaseSettingsItem.Toggle() {
override val title = R.string.settings_su_tapjack_title.asText()
override var description = R.string.settings_su_tapjack_summary.asText()
override var value = Config.suTapjack
set(value) = setV(value, field, { field = it }) { Config.suTapjack = it }
}
object Biometrics : BaseSettingsItem.Toggle() {
override val title = R.string.settings_su_biometric_title.asText()
override var value = Config.suBiometric
set(value) = setV(value, field, { field = it }) { Config.suBiometric = it }
override var description = R.string.settings_su_biometric_summary.asText()
override fun refresh() {
isEnabled = BiometricHelper.isSupported
if (!isEnabled) {
value = false
description = R.string.no_biometric.asText()
}
}
}
object Reauthenticate : BaseSettingsItem.Toggle() {
override val title = R.string.settings_su_reauth_title.asText()
override val description = R.string.settings_su_reauth_summary.asText()
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() {
override val title = R.string.magisk.asText()
}
object DenyList : BaseSettingsItem.Toggle() {
override val title = R.string.settings_denylist_title.asText()
override val description = R.string.settings_denylist_summary.asText()
override var value = Info.env.denyListEnforced
set(value) = setV(value, field, { field = it }) {
val cmd = if (it) "enable" else "disable"
Shell.su("magisk --denylist $cmd").submit { cb ->
if (cb.isSuccess) Info.env.denyListEnforced = it
else field = !it
}
DenyListConfig.isEnabled = it
}
}
object DenyListConfig : BaseSettingsItem.Blank() {
override val title = R.string.settings_denylist_config_title.asText()
override val description = R.string.settings_denylist_config_summary.asText()
}
// --- Superuser
object Superuser : BaseSettingsItem.Section() {
override val title = R.string.superuser.asText()
}
object AccessMode : BaseSettingsItem.Selector() {
override val title = R.string.superuser_access.asText()
override val entryRes = R.array.su_access
override var value = Config.rootMode
set(value) = setV(value, field, { field = it }) {
Config.rootMode = it
}
}
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 fun refresh() {
isEnabled = Const.USER_ID == 0
}
}
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
}
}
object AutomaticResponse : BaseSettingsItem.Selector() {
override val title = R.string.auto_response.asText()
override val entryRes = R.array.auto_response
override var value = Config.suAutoResponse
set(value) = setV(value, field, { field = it }) {
Config.suAutoResponse = it
}
}
object RequestTimeout : BaseSettingsItem.Selector() {
override val title = R.string.request_timeout.asText()
override val entryRes = R.array.request_timeout
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]
}
private val selected: Int
get() = entryValues.indexOfFirst { it == Config.suDefaultTimeout }
}
object SUNotification : BaseSettingsItem.Selector() {
override val title = R.string.superuser_notification.asText()
override val entryRes = R.array.su_notification
override var value = Config.suNotification
set(value) = setV(value, field, { field = it }) {
Config.suNotification = it
}
}