mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
Migrate Superuser screen to Jetpack Compose with miuix
Replace Fragment data-binding UI with a Compose-based SuperuserScreen using miuix Card, Switch, Slider, and TextButton components. SuperuserViewModel now uses StateFlow<UiState> and Compose-friendly PolicyItem with mutableStateOf for reactive updates. Remove old PolicyRvItem, XML layouts, and stale navigation reference. Made-with: Cursor
This commit is contained in:
@@ -1,102 +0,0 @@
|
||||
package com.topjohnwu.magisk.ui.superuser
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
import androidx.databinding.Bindable
|
||||
import com.topjohnwu.magisk.BR
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.core.Config
|
||||
import com.topjohnwu.magisk.core.model.su.SuPolicy
|
||||
import com.topjohnwu.magisk.databinding.DiffItem
|
||||
import com.topjohnwu.magisk.databinding.ItemWrapper
|
||||
import com.topjohnwu.magisk.databinding.ObservableRvItem
|
||||
import com.topjohnwu.magisk.databinding.set
|
||||
import com.topjohnwu.magisk.core.R as CoreR
|
||||
|
||||
class PolicyRvItem(
|
||||
private val viewModel: SuperuserViewModel,
|
||||
override val item: SuPolicy,
|
||||
val packageName: String,
|
||||
private val isSharedUid: Boolean,
|
||||
val icon: Drawable,
|
||||
val appName: String
|
||||
) : ObservableRvItem(), DiffItem<PolicyRvItem>, ItemWrapper<SuPolicy> {
|
||||
|
||||
override val layoutRes = R.layout.item_policy_md2
|
||||
|
||||
val title get() = if (isSharedUid) "[SharedUID] $appName" else appName
|
||||
|
||||
private inline fun <reified T> setImpl(new: T, old: T, setter: (T) -> Unit) {
|
||||
if (old != new) {
|
||||
setter(new)
|
||||
}
|
||||
}
|
||||
|
||||
@get:Bindable
|
||||
var isExpanded = false
|
||||
set(value) = set(value, field, { field = it }, BR.expanded)
|
||||
|
||||
val showSlider = Config.suRestrict || item.policy == SuPolicy.RESTRICT
|
||||
|
||||
@get:Bindable
|
||||
var isEnabled
|
||||
get() = item.policy >= SuPolicy.ALLOW
|
||||
set(value) = setImpl(value, isEnabled) {
|
||||
notifyPropertyChanged(BR.enabled)
|
||||
viewModel.updatePolicy(this, if (it) SuPolicy.ALLOW else SuPolicy.DENY)
|
||||
}
|
||||
|
||||
@get:Bindable
|
||||
var sliderValue
|
||||
get() = item.policy
|
||||
set(value) = setImpl(value, sliderValue) {
|
||||
notifyPropertyChanged(BR.sliderValue)
|
||||
notifyPropertyChanged(BR.enabled)
|
||||
viewModel.updatePolicy(this, it)
|
||||
}
|
||||
|
||||
val sliderValueToPolicyString: (Float) -> Int = { value ->
|
||||
when (value.toInt()) {
|
||||
1 -> CoreR.string.deny
|
||||
2 -> CoreR.string.restrict
|
||||
3 -> CoreR.string.grant
|
||||
else -> CoreR.string.deny
|
||||
}
|
||||
}
|
||||
|
||||
@get:Bindable
|
||||
var shouldNotify
|
||||
get() = item.notification
|
||||
private set(value) = setImpl(value, shouldNotify) {
|
||||
item.notification = it
|
||||
viewModel.updateNotify(this)
|
||||
}
|
||||
|
||||
@get:Bindable
|
||||
var shouldLog
|
||||
get() = item.logging
|
||||
private set(value) = setImpl(value, shouldLog) {
|
||||
item.logging = it
|
||||
viewModel.updateLogging(this)
|
||||
}
|
||||
|
||||
fun toggleExpand() {
|
||||
isExpanded = !isExpanded
|
||||
}
|
||||
|
||||
fun toggleNotify() {
|
||||
shouldNotify = !shouldNotify
|
||||
}
|
||||
|
||||
fun toggleLog() {
|
||||
shouldLog = !shouldLog
|
||||
}
|
||||
|
||||
fun revoke() {
|
||||
viewModel.deletePressed(this)
|
||||
}
|
||||
|
||||
override fun itemSameAs(other: PolicyRvItem) = packageName == other.packageName
|
||||
|
||||
override fun contentSameAs(other: PolicyRvItem) = item.policy == other.item.policy
|
||||
|
||||
}
|
||||
@@ -1,36 +1,63 @@
|
||||
package com.topjohnwu.magisk.ui.superuser
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.arch.BaseFragment
|
||||
import com.topjohnwu.magisk.arch.viewModel
|
||||
import com.topjohnwu.magisk.databinding.FragmentSuperuserMd2Binding
|
||||
import rikka.recyclerview.addEdgeSpacing
|
||||
import rikka.recyclerview.addItemSpacing
|
||||
import rikka.recyclerview.fixEdgeEffect
|
||||
import android.view.ViewGroup
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.compose.ui.platform.ViewCompositionStrategy
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.topjohnwu.magisk.arch.ActivityExecutor
|
||||
import com.topjohnwu.magisk.arch.ContextExecutor
|
||||
import com.topjohnwu.magisk.arch.NavigationActivity
|
||||
import com.topjohnwu.magisk.arch.UIActivity
|
||||
import com.topjohnwu.magisk.arch.VMFactory
|
||||
import com.topjohnwu.magisk.arch.ViewEvent
|
||||
import com.topjohnwu.magisk.arch.ViewModelHolder
|
||||
import com.topjohnwu.magisk.ui.theme.MagiskTheme
|
||||
import com.topjohnwu.magisk.core.R as CoreR
|
||||
|
||||
class SuperuserFragment : BaseFragment<FragmentSuperuserMd2Binding>() {
|
||||
class SuperuserFragment : Fragment(), ViewModelHolder {
|
||||
|
||||
override val layoutRes = R.layout.fragment_superuser_md2
|
||||
override val viewModel by viewModel<SuperuserViewModel>()
|
||||
override val viewModel by lazy {
|
||||
ViewModelProvider(this, VMFactory)[SuperuserViewModel::class.java]
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
startObserveLiveData()
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
activity?.title = resources.getString(CoreR.string.superuser)
|
||||
(activity as? NavigationActivity<*>)?.setTitle(CoreR.string.superuser)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
binding.superuserList.apply {
|
||||
addEdgeSpacing(top = R.dimen.l_50, bottom = R.dimen.l1)
|
||||
addItemSpacing(R.dimen.l1, R.dimen.l_50, R.dimen.l1)
|
||||
fixEdgeEffect()
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
return ComposeView(requireContext()).apply {
|
||||
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
|
||||
setContent {
|
||||
MagiskTheme {
|
||||
SuperuserScreen(viewModel = viewModel as SuperuserViewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPreBind(binding: FragmentSuperuserMd2Binding) {}
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
(viewModel as SuperuserViewModel).startLoading()
|
||||
}
|
||||
|
||||
override fun onEventDispatched(event: ViewEvent) {
|
||||
when (event) {
|
||||
is ContextExecutor -> event(requireContext())
|
||||
is ActivityExecutor -> (activity as? UIActivity<*>)?.let { event(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
package com.topjohnwu.magisk.ui.superuser
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.drawable.Drawable
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.graphics.painter.BitmapPainter
|
||||
import androidx.compose.ui.graphics.painter.Painter
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.topjohnwu.magisk.core.model.su.SuPolicy
|
||||
import top.yukonga.miuix.kmp.basic.Card
|
||||
import top.yukonga.miuix.kmp.basic.CircularProgressIndicator
|
||||
import top.yukonga.miuix.kmp.basic.Slider
|
||||
import top.yukonga.miuix.kmp.basic.Switch
|
||||
import top.yukonga.miuix.kmp.basic.Text
|
||||
import top.yukonga.miuix.kmp.basic.TextButton
|
||||
import top.yukonga.miuix.kmp.theme.MiuixTheme
|
||||
import com.topjohnwu.magisk.core.R as CoreR
|
||||
|
||||
@Composable
|
||||
fun SuperuserScreen(viewModel: SuperuserViewModel) {
|
||||
val uiState by viewModel.uiState.collectAsState()
|
||||
|
||||
if (uiState.loading) {
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
CircularProgressIndicator()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (uiState.policies.isEmpty()) {
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(CoreR.string.superuser_policy_none),
|
||||
style = MiuixTheme.textStyles.body1,
|
||||
color = MiuixTheme.colorScheme.onSurfaceVariantSummary
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(horizontal = 12.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
item { Spacer(Modifier.height(4.dp)) }
|
||||
items(uiState.policies, key = { "${it.policy.uid}_${it.packageName}" }) { item ->
|
||||
PolicyCard(item = item, viewModel = viewModel)
|
||||
}
|
||||
item { Spacer(Modifier.height(4.dp)) }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PolicyCard(item: PolicyItem, viewModel: SuperuserViewModel) {
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.alpha(if (item.isEnabled) 1f else 0.5f)
|
||||
) {
|
||||
Column {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.clickable { item.isExpanded = !item.isExpanded }
|
||||
.padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Image(
|
||||
painter = rememberDrawablePainter(item.icon),
|
||||
contentDescription = item.appName,
|
||||
modifier = Modifier.size(40.dp)
|
||||
)
|
||||
Spacer(Modifier.width(12.dp))
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = item.title,
|
||||
style = MiuixTheme.textStyles.body1,
|
||||
)
|
||||
Text(
|
||||
text = item.packageName,
|
||||
style = MiuixTheme.textStyles.body2,
|
||||
color = MiuixTheme.colorScheme.onSurfaceVariantSummary
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.width(8.dp))
|
||||
if (item.showSlider) {
|
||||
PolicySlider(
|
||||
value = item.policyValue,
|
||||
onValueChange = { viewModel.updatePolicy(item, it) }
|
||||
)
|
||||
} else {
|
||||
Switch(
|
||||
checked = item.isEnabled,
|
||||
onCheckedChange = { viewModel.togglePolicy(item) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
AnimatedVisibility(visible = item.isExpanded) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 8.dp, vertical = 4.dp),
|
||||
horizontalArrangement = Arrangement.SpaceEvenly
|
||||
) {
|
||||
TextButton(
|
||||
text = stringResource(CoreR.string.superuser_toggle_notification),
|
||||
onClick = { viewModel.updateNotify(item) },
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
TextButton(
|
||||
text = stringResource(CoreR.string.logs),
|
||||
onClick = { viewModel.updateLogging(item) },
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
TextButton(
|
||||
text = stringResource(CoreR.string.superuser_toggle_revoke),
|
||||
onClick = { viewModel.deletePressed(item) },
|
||||
modifier = Modifier.weight(1f)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PolicySlider(value: Int, onValueChange: (Int) -> Unit) {
|
||||
val sliderValue = when (value) {
|
||||
SuPolicy.DENY -> 0f
|
||||
SuPolicy.RESTRICT -> 0.5f
|
||||
SuPolicy.ALLOW -> 1f
|
||||
else -> 0f
|
||||
}
|
||||
val label = when (value) {
|
||||
SuPolicy.DENY -> stringResource(CoreR.string.deny)
|
||||
SuPolicy.RESTRICT -> stringResource(CoreR.string.restrict)
|
||||
SuPolicy.ALLOW -> stringResource(CoreR.string.grant)
|
||||
else -> stringResource(CoreR.string.deny)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier.width(96.dp)
|
||||
) {
|
||||
Text(
|
||||
text = label,
|
||||
style = MiuixTheme.textStyles.body2,
|
||||
color = MiuixTheme.colorScheme.onSurfaceVariantSummary
|
||||
)
|
||||
Slider(
|
||||
value = sliderValue,
|
||||
onValueChange = { newVal ->
|
||||
val newPolicy = when {
|
||||
newVal < 0.25f -> SuPolicy.DENY
|
||||
newVal < 0.75f -> SuPolicy.RESTRICT
|
||||
else -> SuPolicy.ALLOW
|
||||
}
|
||||
if (newPolicy != value) onValueChange(newPolicy)
|
||||
},
|
||||
steps = 1,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun rememberDrawablePainter(drawable: Drawable): Painter {
|
||||
return remember(drawable) {
|
||||
val w = drawable.intrinsicWidth.coerceAtLeast(1)
|
||||
val h = drawable.intrinsicHeight.coerceAtLeast(1)
|
||||
val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
|
||||
val canvas = android.graphics.Canvas(bitmap)
|
||||
drawable.setBounds(0, 0, w, h)
|
||||
drawable.draw(canvas)
|
||||
BitmapPainter(bitmap.asImageBitmap())
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,13 @@ package com.topjohnwu.magisk.ui.superuser
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Process
|
||||
import androidx.databinding.Bindable
|
||||
import androidx.databinding.ObservableArrayList
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.topjohnwu.magisk.BR
|
||||
import com.topjohnwu.magisk.arch.AsyncLoadViewModel
|
||||
import com.topjohnwu.magisk.core.AppContext
|
||||
import com.topjohnwu.magisk.core.Config
|
||||
@@ -16,52 +18,60 @@ import com.topjohnwu.magisk.core.R
|
||||
import com.topjohnwu.magisk.core.data.magiskdb.PolicyDao
|
||||
import com.topjohnwu.magisk.core.ktx.getLabel
|
||||
import com.topjohnwu.magisk.core.model.su.SuPolicy
|
||||
import com.topjohnwu.magisk.databinding.MergeObservableList
|
||||
import com.topjohnwu.magisk.databinding.RvItem
|
||||
import com.topjohnwu.magisk.databinding.bindExtra
|
||||
import com.topjohnwu.magisk.databinding.diffList
|
||||
import com.topjohnwu.magisk.databinding.set
|
||||
import com.topjohnwu.magisk.dialog.SuperuserRevokeDialog
|
||||
import com.topjohnwu.magisk.events.AuthEvent
|
||||
import com.topjohnwu.magisk.events.SnackbarEvent
|
||||
import com.topjohnwu.magisk.utils.asText
|
||||
import com.topjohnwu.magisk.view.TextItem
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.Locale
|
||||
|
||||
class PolicyItem(
|
||||
val policy: SuPolicy,
|
||||
val packageName: String,
|
||||
val isSharedUid: Boolean,
|
||||
val icon: Drawable,
|
||||
val appName: String,
|
||||
) {
|
||||
val title get() = if (isSharedUid) "[SharedUID] $appName" else appName
|
||||
val showSlider = Config.suRestrict || policy.policy == SuPolicy.RESTRICT
|
||||
|
||||
var isExpanded by mutableStateOf(false)
|
||||
var policyValue by mutableIntStateOf(policy.policy)
|
||||
var notification by mutableStateOf(policy.notification)
|
||||
var logging by mutableStateOf(policy.logging)
|
||||
|
||||
val isEnabled get() = policyValue >= SuPolicy.ALLOW
|
||||
}
|
||||
|
||||
class SuperuserViewModel(
|
||||
private val db: PolicyDao
|
||||
) : AsyncLoadViewModel() {
|
||||
|
||||
private val itemNoData = TextItem(R.string.superuser_policy_none)
|
||||
data class UiState(
|
||||
val loading: Boolean = true,
|
||||
val policies: List<PolicyItem> = emptyList(),
|
||||
)
|
||||
|
||||
private val itemsHelpers = ObservableArrayList<TextItem>()
|
||||
private val itemsPolicies = diffList<PolicyRvItem>()
|
||||
|
||||
val items = MergeObservableList<RvItem>()
|
||||
.insertList(itemsHelpers)
|
||||
.insertList(itemsPolicies)
|
||||
val extraBindings = bindExtra {
|
||||
it.put(BR.listener, this)
|
||||
}
|
||||
|
||||
@get:Bindable
|
||||
var loading = true
|
||||
private set(value) = set(value, field, { field = it }, BR.loading)
|
||||
private val _uiState = MutableStateFlow(UiState())
|
||||
val uiState: StateFlow<UiState> = _uiState.asStateFlow()
|
||||
|
||||
@SuppressLint("InlinedApi")
|
||||
override suspend fun doLoadWork() {
|
||||
if (!Info.showSuperUser) {
|
||||
loading = false
|
||||
_uiState.update { it.copy(loading = false) }
|
||||
return
|
||||
}
|
||||
loading = true
|
||||
_uiState.update { it.copy(loading = true) }
|
||||
withContext(Dispatchers.IO) {
|
||||
db.deleteOutdated()
|
||||
db.delete(AppContext.applicationInfo.uid)
|
||||
val policies = ArrayList<PolicyRvItem>()
|
||||
val policies = ArrayList<PolicyItem>()
|
||||
val pm = AppContext.packageManager
|
||||
for (policy in db.fetchAll()) {
|
||||
val pkgs =
|
||||
@@ -74,14 +84,14 @@ class SuperuserViewModel(
|
||||
val map = pkgs.mapNotNull { pkg ->
|
||||
try {
|
||||
val info = pm.getPackageInfo(pkg, MATCH_UNINSTALLED_PACKAGES)
|
||||
PolicyRvItem(
|
||||
this@SuperuserViewModel, policy,
|
||||
info.packageName,
|
||||
info.sharedUserId != null,
|
||||
info.applicationInfo?.loadIcon(pm) ?: pm.defaultActivityIcon,
|
||||
info.applicationInfo?.getLabel(pm) ?: info.packageName
|
||||
PolicyItem(
|
||||
policy = policy,
|
||||
packageName = info.packageName,
|
||||
isSharedUid = info.sharedUserId != null,
|
||||
icon = info.applicationInfo?.loadIcon(pm) ?: pm.defaultActivityIcon,
|
||||
appName = info.applicationInfo?.getLabel(pm) ?: info.packageName
|
||||
)
|
||||
} catch (e: PackageManager.NameNotFoundException) {
|
||||
} catch (_: PackageManager.NameNotFoundException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -95,25 +105,15 @@ class SuperuserViewModel(
|
||||
{ it.appName.lowercase(Locale.ROOT) },
|
||||
{ it.packageName }
|
||||
))
|
||||
itemsPolicies.update(policies)
|
||||
_uiState.update { it.copy(loading = false, policies = policies) }
|
||||
}
|
||||
if (itemsPolicies.isNotEmpty())
|
||||
itemsHelpers.clear()
|
||||
else if (itemsHelpers.isEmpty())
|
||||
itemsHelpers.add(itemNoData)
|
||||
loading = false
|
||||
}
|
||||
|
||||
// ---
|
||||
|
||||
fun deletePressed(item: PolicyRvItem) {
|
||||
fun deletePressed(item: PolicyItem) {
|
||||
fun updateState() = viewModelScope.launch {
|
||||
db.delete(item.item.uid)
|
||||
val list = ArrayList(itemsPolicies)
|
||||
list.removeAll { it.item.uid == item.item.uid }
|
||||
itemsPolicies.update(list)
|
||||
if (list.isEmpty() && itemsHelpers.isEmpty()) {
|
||||
itemsHelpers.add(itemNoData)
|
||||
db.delete(item.policy.uid)
|
||||
_uiState.update { state ->
|
||||
state.copy(policies = state.policies.filter { it.policy.uid != item.policy.uid })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,49 +124,42 @@ class SuperuserViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun updateNotify(item: PolicyRvItem) {
|
||||
fun updateNotify(item: PolicyItem) {
|
||||
item.notification = !item.notification
|
||||
item.policy.notification = item.notification
|
||||
viewModelScope.launch {
|
||||
db.update(item.item)
|
||||
val res = when {
|
||||
item.item.notification -> R.string.su_snack_notif_on
|
||||
else -> R.string.su_snack_notif_off
|
||||
}
|
||||
itemsPolicies.forEach {
|
||||
if (it.item.uid == item.item.uid) {
|
||||
it.notifyPropertyChanged(BR.shouldNotify)
|
||||
}
|
||||
}
|
||||
db.update(item.policy)
|
||||
_uiState.value.policies
|
||||
.filter { it.policy.uid == item.policy.uid }
|
||||
.forEach { it.notification = item.notification }
|
||||
val res = if (item.notification) R.string.su_snack_notif_on else R.string.su_snack_notif_off
|
||||
SnackbarEvent(res.asText(item.appName)).publish()
|
||||
}
|
||||
}
|
||||
|
||||
fun updateLogging(item: PolicyRvItem) {
|
||||
fun updateLogging(item: PolicyItem) {
|
||||
item.logging = !item.logging
|
||||
item.policy.logging = item.logging
|
||||
viewModelScope.launch {
|
||||
db.update(item.item)
|
||||
val res = when {
|
||||
item.item.logging -> R.string.su_snack_log_on
|
||||
else -> R.string.su_snack_log_off
|
||||
}
|
||||
itemsPolicies.forEach {
|
||||
if (it.item.uid == item.item.uid) {
|
||||
it.notifyPropertyChanged(BR.shouldLog)
|
||||
}
|
||||
}
|
||||
db.update(item.policy)
|
||||
_uiState.value.policies
|
||||
.filter { it.policy.uid == item.policy.uid }
|
||||
.forEach { it.logging = item.logging }
|
||||
val res = if (item.logging) R.string.su_snack_log_on else R.string.su_snack_log_off
|
||||
SnackbarEvent(res.asText(item.appName)).publish()
|
||||
}
|
||||
}
|
||||
|
||||
fun updatePolicy(item: PolicyRvItem, policy: Int) {
|
||||
val items = itemsPolicies.filter { it.item.uid == item.item.uid }
|
||||
fun updatePolicy(item: PolicyItem, newPolicy: Int) {
|
||||
fun updateState() {
|
||||
viewModelScope.launch {
|
||||
val res = if (policy >= SuPolicy.ALLOW) R.string.su_snack_grant else R.string.su_snack_deny
|
||||
item.item.policy = policy
|
||||
db.update(item.item)
|
||||
items.forEach {
|
||||
it.notifyPropertyChanged(BR.enabled)
|
||||
it.notifyPropertyChanged(BR.sliderValue)
|
||||
}
|
||||
item.policy.policy = newPolicy
|
||||
item.policyValue = newPolicy
|
||||
db.update(item.policy)
|
||||
_uiState.value.policies
|
||||
.filter { it.policy.uid == item.policy.uid }
|
||||
.forEach { it.policyValue = newPolicy }
|
||||
val res = if (newPolicy >= SuPolicy.ALLOW) R.string.su_snack_grant else R.string.su_snack_deny
|
||||
SnackbarEvent(res.asText(item.appName)).publish()
|
||||
}
|
||||
}
|
||||
@@ -177,4 +170,9 @@ class SuperuserViewModel(
|
||||
updateState()
|
||||
}
|
||||
}
|
||||
|
||||
fun togglePolicy(item: PolicyItem) {
|
||||
val newPolicy = if (item.isEnabled) SuPolicy.DENY else SuPolicy.ALLOW
|
||||
updatePolicy(item, newPolicy)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
|
||||
<variable
|
||||
name="viewModel"
|
||||
type="com.topjohnwu.magisk.ui.superuser.SuperuserViewModel" />
|
||||
|
||||
</data>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/superuser_list"
|
||||
gone="@{viewModel.loading}"
|
||||
app:items="@{viewModel.items}"
|
||||
app:extraBindings="@{viewModel.extraBindings}"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:clipToPadding="false"
|
||||
android:orientation="vertical"
|
||||
android:paddingTop="@dimen/internal_action_bar_size"
|
||||
android:paddingBottom="56dp"
|
||||
app:fitsSystemWindowsInsets="top|bottom"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
||||
tools:listitem="@layout/item_policy_md2" />
|
||||
|
||||
<LinearLayout
|
||||
goneUnless="@{viewModel.loading}"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
tools:visibility="gone">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/loading"
|
||||
android:textAppearance="@style/AppearanceFoundation.Title"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<ProgressBar
|
||||
style="@style/WidgetFoundation.ProgressBar.Indeterminate"
|
||||
android:layout_marginTop="@dimen/l1" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
</layout>
|
||||
@@ -1,200 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
|
||||
<import type="com.topjohnwu.magisk.databinding.DataBindingAdaptersKt" />
|
||||
|
||||
<variable
|
||||
name="item"
|
||||
type="com.topjohnwu.magisk.ui.superuser.PolicyRvItem" />
|
||||
|
||||
</data>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center">
|
||||
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:id="@+id/policy"
|
||||
style="@style/WidgetFoundation.Card"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:alpha="@{item.enabled ? 1f : .5f}"
|
||||
android:focusable="true"
|
||||
android:nextFocusRight="@id/policy_indicator"
|
||||
android:onClick="@{() -> item.toggleExpand()}">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/policy_app_icon"
|
||||
style="@style/WidgetFoundation.Image"
|
||||
srcCompat="@{item.icon}"
|
||||
android:layout_marginStart="@dimen/l1"
|
||||
android:layout_marginTop="@dimen/l1"
|
||||
android:layout_marginBottom="@dimen/l1"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_bias="0"
|
||||
tools:srcCompat="@drawable/ic_logo" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/policy_app_name"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="@dimen/margin_generic"
|
||||
android:layout_marginRight="@dimen/margin_generic"
|
||||
android:ellipsize="middle"
|
||||
android:gravity="start"
|
||||
android:maxLines="2"
|
||||
android:text="@{item.title}"
|
||||
android:textAppearance="@style/AppearanceFoundation.Body"
|
||||
android:textIsSelectable="false"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toStartOf="@+id/policy_indicator"
|
||||
app:layout_constraintStart_toEndOf="@+id/policy_app_icon"
|
||||
app:layout_constraintTop_toTopOf="@+id/policy_app_icon"
|
||||
tools:text="@string/magisk" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/policy_package_name"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="@dimen/l1"
|
||||
android:ellipsize="middle"
|
||||
android:gravity="start"
|
||||
android:maxLines="2"
|
||||
android:text="@{item.packageName}"
|
||||
android:textAppearance="@style/AppearanceFoundation.Caption.Variant"
|
||||
android:textColor="@android:color/tertiary_text_dark"
|
||||
android:textIsSelectable="false"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="@id/policy_app_name"
|
||||
app:layout_constraintStart_toStartOf="@id/policy_app_name"
|
||||
app:layout_constraintTop_toBottomOf="@id/policy_app_name"
|
||||
app:layout_constraintVertical_bias="0"
|
||||
tools:text="com.topjohnwu.magisk" />
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/policy_indicator"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="@dimen/l1"
|
||||
android:nextFocusLeft="@id/policy"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<com.google.android.material.switchmaterial.SwitchMaterial
|
||||
gone="@{item.showSlider}"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:checked="@={item.enabled}" />
|
||||
|
||||
<com.google.android.material.slider.Slider
|
||||
goneUnless="@{item.showSlider}"
|
||||
labelFormatter="@{item.sliderValueToPolicyString}"
|
||||
android:layout_width="96dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:stepSize="1"
|
||||
android:value="@={DataBindingAdaptersKt.policyToSliderValue(item.sliderValue)}"
|
||||
android:valueFrom="1"
|
||||
android:valueTo="3" />
|
||||
</FrameLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/policy_expand_container"
|
||||
gone="@{!item.isExpanded}"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="?colorSurfaceVariant"
|
||||
android:orientation="horizontal"
|
||||
tools:visibility="visible">
|
||||
|
||||
<Button
|
||||
android:id="@+id/policy_notify"
|
||||
style="@style/WidgetFoundation.Button.Text"
|
||||
isSelected="@{item.shouldNotify}"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:minHeight="24dp"
|
||||
android:onClick="@{() -> item.toggleNotify()}"
|
||||
android:text="@string/superuser_toggle_notification"
|
||||
android:textAllCaps="false"
|
||||
android:textAppearance="@style/AppearanceFoundation.Tiny"
|
||||
android:textColor="@color/color_state_primary_transient"
|
||||
app:icon="@drawable/ic_notifications_md2"
|
||||
app:iconTint="@color/color_state_primary_transient"
|
||||
app:tint="@color/color_state_primary_transient" />
|
||||
|
||||
<View
|
||||
android:layout_width="1dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="@dimen/l_50"
|
||||
android:layout_marginBottom="@dimen/l_50"
|
||||
android:background="?colorSurfaceSurfaceVariant" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/policy_log"
|
||||
style="@style/WidgetFoundation.Button.Text"
|
||||
isSelected="@{item.shouldLog}"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:minHeight="24dp"
|
||||
android:onClick="@{() -> item.toggleLog()}"
|
||||
android:text="@string/logs"
|
||||
android:textAllCaps="false"
|
||||
android:textAppearance="@style/AppearanceFoundation.Tiny"
|
||||
android:textColor="@color/color_state_primary_transient"
|
||||
app:icon="@drawable/ic_bug_md2"
|
||||
app:iconTint="@color/color_state_primary_transient"
|
||||
app:tint="@color/color_state_primary_transient" />
|
||||
|
||||
<View
|
||||
android:layout_width="1dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginTop="@dimen/l_50"
|
||||
android:layout_marginBottom="@dimen/l_50"
|
||||
android:background="?colorSurfaceSurfaceVariant" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/policy_delete"
|
||||
style="@style/WidgetFoundation.Button.Text"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:minHeight="24dp"
|
||||
android:onClick="@{() -> item.revoke()}"
|
||||
android:text="@string/superuser_toggle_revoke"
|
||||
android:textAllCaps="false"
|
||||
android:textColor="?colorError"
|
||||
android:textSize="12sp"
|
||||
app:icon="@drawable/ic_delete_md2"
|
||||
app:iconTint="?colorError"
|
||||
app:rippleColor="?colorError" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
</layout>
|
||||
@@ -101,8 +101,7 @@
|
||||
<fragment
|
||||
android:id="@+id/superuserFragment"
|
||||
android:name="com.topjohnwu.magisk.ui.superuser.SuperuserFragment"
|
||||
android:label="SuperuserFragment"
|
||||
tools:layout="@layout/fragment_superuser_md2" />
|
||||
android:label="SuperuserFragment" />
|
||||
|
||||
<fragment
|
||||
android:id="@+id/themeFragment"
|
||||
|
||||
Reference in New Issue
Block a user