2020-01-12 17:43:09 +01:00
|
|
|
package com.topjohnwu.magisk.ui.settings
|
2019-10-17 18:57:00 +02:00
|
|
|
|
2020-01-17 20:06:33 +01:00
|
|
|
import android.os.Build
|
2019-11-27 19:47:20 +01:00
|
|
|
import android.view.View
|
2019-12-02 18:35:48 +01:00
|
|
|
import android.widget.Toast
|
2020-08-21 12:36:12 +02:00
|
|
|
import androidx.core.content.pm.ShortcutManagerCompat
|
2020-07-10 13:19:18 +02:00
|
|
|
import androidx.lifecycle.viewModelScope
|
2019-11-27 19:47:20 +01:00
|
|
|
import com.topjohnwu.magisk.BR
|
2020-08-21 12:36:12 +02:00
|
|
|
import com.topjohnwu.magisk.BuildConfig
|
2019-11-27 19:47:20 +01:00
|
|
|
import com.topjohnwu.magisk.R
|
2020-08-18 15:31:15 +02:00
|
|
|
import com.topjohnwu.magisk.arch.BaseViewModel
|
|
|
|
import com.topjohnwu.magisk.arch.adapterOf
|
|
|
|
import com.topjohnwu.magisk.arch.diffListOf
|
|
|
|
import com.topjohnwu.magisk.arch.itemBindingOf
|
2020-01-17 20:06:33 +01:00
|
|
|
import com.topjohnwu.magisk.core.Const
|
|
|
|
import com.topjohnwu.magisk.core.Info
|
2020-08-22 19:51:32 +02:00
|
|
|
import com.topjohnwu.magisk.core.isRunningAsStub
|
2020-10-17 12:40:43 +02:00
|
|
|
import com.topjohnwu.magisk.core.tasks.HideAPK
|
2019-12-02 18:35:48 +01:00
|
|
|
import com.topjohnwu.magisk.data.database.RepoDao
|
2021-04-18 13:46:11 +02:00
|
|
|
import com.topjohnwu.magisk.di.AppContext
|
2020-08-21 12:36:12 +02:00
|
|
|
import com.topjohnwu.magisk.events.AddHomeIconEvent
|
2020-08-19 11:05:23 +02:00
|
|
|
import com.topjohnwu.magisk.events.RecreateEvent
|
2020-09-11 12:09:01 +02:00
|
|
|
import com.topjohnwu.magisk.events.dialog.BiometricEvent
|
2021-02-21 05:12:35 +01:00
|
|
|
import com.topjohnwu.magisk.ktx.activity
|
2020-08-18 15:31:15 +02:00
|
|
|
import com.topjohnwu.magisk.utils.Utils
|
2019-12-02 18:35:48 +01:00
|
|
|
import com.topjohnwu.superuser.Shell
|
2020-07-10 13:19:18 +02:00
|
|
|
import kotlinx.coroutines.launch
|
2019-10-17 18:57:00 +02:00
|
|
|
|
2019-12-02 18:35:48 +01:00
|
|
|
class SettingsViewModel(
|
|
|
|
private val repositoryDao: RepoDao
|
2020-08-19 11:05:23 +02:00
|
|
|
) : BaseViewModel(), BaseSettingsItem.Callback {
|
2019-10-17 19:26:35 +02:00
|
|
|
|
2020-08-19 11:05:23 +02:00
|
|
|
val adapter = adapterOf<BaseSettingsItem>()
|
|
|
|
val itemBinding = itemBindingOf<BaseSettingsItem> { it.bindExtra(BR.callback, this) }
|
2020-01-17 20:06:33 +01:00
|
|
|
val items = diffListOf(createItems())
|
|
|
|
|
2020-07-17 12:02:58 +02:00
|
|
|
init {
|
|
|
|
viewModelScope.launch {
|
|
|
|
Language.loadLanguages(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 11:05:23 +02:00
|
|
|
private fun createItems(): List<BaseSettingsItem> {
|
2021-04-18 13:46:11 +02:00
|
|
|
val context = AppContext
|
2020-08-21 12:36:12 +02:00
|
|
|
val hidden = context.packageName != BuildConfig.APPLICATION_ID
|
|
|
|
|
2020-01-17 20:06:33 +01:00
|
|
|
// Customization
|
|
|
|
val list = mutableListOf(
|
|
|
|
Customization,
|
2020-07-04 12:28:10 +02:00
|
|
|
Theme, Language
|
2020-01-17 20:06:33 +01:00
|
|
|
)
|
2020-08-22 19:51:32 +02:00
|
|
|
if (isRunningAsStub && ShortcutManagerCompat.isRequestPinShortcutSupported(context))
|
2020-08-21 12:36:12 +02:00
|
|
|
list.add(AddShortcut)
|
2019-11-28 21:53:31 +01:00
|
|
|
|
2020-01-17 20:06:33 +01:00
|
|
|
// Manager
|
|
|
|
list.addAll(listOf(
|
2021-02-11 11:34:00 +01:00
|
|
|
AppSettings,
|
2020-09-13 13:34:00 +02:00
|
|
|
UpdateChannel, UpdateChannelUrl, DoHToggle, UpdateChecker, DownloadPath
|
2020-01-17 20:06:33 +01:00
|
|
|
))
|
|
|
|
if (Info.env.isActive) {
|
|
|
|
list.add(ClearRepoCache)
|
2021-04-21 18:41:42 +02:00
|
|
|
if (Const.USER_ID == 0) {
|
2021-01-26 13:17:37 +01:00
|
|
|
if (hidden)
|
|
|
|
list.add(Restore)
|
|
|
|
else if (Info.isConnected.get())
|
|
|
|
list.add(Hide)
|
|
|
|
}
|
2020-01-17 20:06:33 +01:00
|
|
|
}
|
2019-11-28 21:53:31 +01:00
|
|
|
|
2020-01-17 20:06:33 +01:00
|
|
|
// Magisk
|
|
|
|
if (Info.env.isActive) {
|
|
|
|
list.addAll(listOf(
|
|
|
|
Magisk,
|
2020-06-22 00:59:06 +02:00
|
|
|
MagiskHide, SystemlessHosts
|
2020-01-17 20:06:33 +01:00
|
|
|
))
|
|
|
|
}
|
2019-11-28 21:53:31 +01:00
|
|
|
|
2020-01-17 20:06:33 +01:00
|
|
|
// Superuser
|
|
|
|
if (Utils.showSuperUser()) {
|
|
|
|
list.addAll(listOf(
|
|
|
|
Superuser,
|
2020-10-22 11:40:47 +02:00
|
|
|
Tapjack, Biometrics, AccessMode, MultiuserMode, MountNamespaceMode,
|
2020-01-17 20:06:33 +01:00
|
|
|
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
|
|
|
|
}
|
2019-10-17 19:26:35 +02:00
|
|
|
|
2020-08-19 11:05:23 +02:00
|
|
|
override fun onItemPressed(view: View, item: BaseSettingsItem, callback: () -> Unit) = when (item) {
|
2020-07-17 12:02:58 +02:00
|
|
|
is DownloadPath -> withExternalRW(callback)
|
|
|
|
is Biometrics -> authenticate(callback)
|
2021-03-16 12:58:02 +01:00
|
|
|
is Theme -> SettingsFragmentDirections.actionSettingsFragmentToThemeFragment().navigate()
|
2020-07-17 12:02:58 +02:00
|
|
|
is ClearRepoCache -> clearRepoCache()
|
|
|
|
is SystemlessHosts -> createHosts()
|
2021-02-21 05:12:35 +01:00
|
|
|
is Restore -> HideAPK.restore(view.activity)
|
2020-08-21 12:36:12 +02:00
|
|
|
is AddShortcut -> AddHomeIconEvent().publish()
|
2020-07-17 12:02:58 +02:00
|
|
|
else -> callback()
|
2019-12-02 18:35:48 +01:00
|
|
|
}
|
|
|
|
|
2021-02-21 05:12:35 +01:00
|
|
|
override fun onItemChanged(view: View, item: BaseSettingsItem) {
|
|
|
|
when (item) {
|
|
|
|
is Language -> RecreateEvent().publish()
|
|
|
|
is UpdateChannel -> openUrlIfNecessary(view)
|
|
|
|
is Hide -> viewModelScope.launch { HideAPK.hide(view.activity, item.value) }
|
|
|
|
else -> Unit
|
|
|
|
}
|
2019-11-27 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
2019-11-29 21:04:47 +01:00
|
|
|
private fun openUrlIfNecessary(view: View) {
|
2019-12-02 18:35:48 +01:00
|
|
|
UpdateChannelUrl.refresh()
|
2020-01-17 20:59:02 +01:00
|
|
|
if (UpdateChannelUrl.isEnabled && UpdateChannelUrl.value.isBlank()) {
|
2020-07-17 12:02:58 +02:00
|
|
|
UpdateChannelUrl.onPressed(view, this)
|
2019-11-29 20:07:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 12:02:58 +02:00
|
|
|
private fun authenticate(callback: () -> Unit) {
|
2020-09-11 12:09:01 +02:00
|
|
|
BiometricEvent {
|
2019-12-02 18:35:48 +01:00
|
|
|
// allow the change on success
|
2020-07-17 12:02:58 +02:00
|
|
|
onSuccess { callback() }
|
2019-12-02 18:35:48 +01:00
|
|
|
}.publish()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun clearRepoCache() {
|
2020-07-10 13:19:18 +02:00
|
|
|
viewModelScope.launch {
|
|
|
|
repositoryDao.clear()
|
|
|
|
Utils.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT)
|
|
|
|
}
|
2019-12-02 18:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun createHosts() {
|
|
|
|
Shell.su("add_hosts_module").submit {
|
|
|
|
Utils.toast(R.string.settings_hosts_toast, Toast.LENGTH_SHORT)
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 08:00:49 +01:00
|
|
|
}
|