Catch exception when setting user restriction (#161)

This commit is contained in:
BinTianqi
2025-08-27 14:13:00 +08:00
parent ed5b23edf6
commit 10ac570818
2 changed files with 29 additions and 15 deletions

View File

@@ -233,17 +233,14 @@ fun WorkModesScreen(
if(privilege.profile) Row( if(privilege.profile) Row(
Modifier Modifier
.fillMaxWidth() .fillMaxWidth()
.background(if (privilege.device) colorScheme.primaryContainer else Color.Transparent) .background(colorScheme.primaryContainer)
.padding(HorizontalPadding, 10.dp), .padding(HorizontalPadding, 10.dp),
Arrangement.SpaceBetween, Alignment.CenterVertically Arrangement.SpaceBetween, Alignment.CenterVertically
) { ) {
Column { Column {
Text(stringResource(R.string.profile_owner), style = typography.titleLarge) Text(stringResource(R.string.profile_owner), style = typography.titleLarge)
} }
Icon( Icon(Icons.Default.Check, null, tint = colorScheme.primary)
if(privilege.device) Icons.Default.Check else Icons.AutoMirrored.Default.KeyboardArrowRight, null,
tint = if(privilege.device) colorScheme.primary else colorScheme.onBackground
)
} }
if(privilege.dhizuku || !(privilege.device || privilege.profile)) Row( if(privilege.dhizuku || !(privilege.device || privilege.profile)) Row(
Modifier Modifier

View File

@@ -57,6 +57,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.bintianqi.owndroid.HorizontalPadding import com.bintianqi.owndroid.HorizontalPadding
import com.bintianqi.owndroid.R import com.bintianqi.owndroid.R
import com.bintianqi.owndroid.myPrivilege import com.bintianqi.owndroid.myPrivilege
import com.bintianqi.owndroid.showOperationResultToast
import com.bintianqi.owndroid.ui.FunctionItem import com.bintianqi.owndroid.ui.FunctionItem
import com.bintianqi.owndroid.ui.MyLazyScaffold import com.bintianqi.owndroid.ui.MyLazyScaffold
import com.bintianqi.owndroid.ui.NavIcon import com.bintianqi.owndroid.ui.NavIcon
@@ -149,12 +150,13 @@ fun UserRestrictionOptionsScreen(
val dpm = context.getDPM() val dpm = context.getDPM()
val receiver = context.getReceiver() val receiver = context.getReceiver()
val status = remember { mutableStateMapOf<String, Boolean>() } val status = remember { mutableStateMapOf<String, Boolean>() }
LaunchedEffect(Unit) { fun refresh() {
val restrictions = dpm.getUserRestrictions(receiver) val restrictions = dpm.getUserRestrictions(receiver)
data.items.forEach { data.items.forEach {
status.put(it.id, restrictions.getBoolean(it.id)) status.put(it.id, restrictions.getBoolean(it.id))
} }
} }
LaunchedEffect(Unit) { refresh() }
MyLazyScaffold(data.title, onNavigateUp) { MyLazyScaffold(data.title, onNavigateUp) {
items(data.items.filter { Build.VERSION.SDK_INT >= it.requiresApi }) { restriction -> items(data.items.filter { Build.VERSION.SDK_INT >= it.requiresApi }) { restriction ->
Row( Row(
@@ -174,12 +176,17 @@ fun UserRestrictionOptionsScreen(
Switch( Switch(
status[restriction.id] == true, status[restriction.id] == true,
{ {
try {
if (it) { if (it) {
dpm.addUserRestriction(receiver, restriction.id) dpm.addUserRestriction(receiver, restriction.id)
} else { } else {
dpm.clearUserRestriction(receiver, restriction.id) dpm.clearUserRestriction(receiver, restriction.id)
} }
status[restriction.id] = dpm.getUserRestrictions(receiver).getBoolean(restriction.id) } catch (e: Exception) {
e.printStackTrace()
context.showOperationResultToast(false)
}
refresh()
} }
) )
} }
@@ -272,12 +279,12 @@ object RestrictionData {
Restriction(UserManager.DISALLOW_SAFE_BOOT, R.string.safe_boot, R.drawable.security_fill0, 23), Restriction(UserManager.DISALLOW_SAFE_BOOT, R.string.safe_boot, R.drawable.security_fill0, 23),
Restriction(UserManager.DISALLOW_DEBUGGING_FEATURES, R.string.debug_features, R.drawable.adb_fill0) Restriction(UserManager.DISALLOW_DEBUGGING_FEATURES, R.string.debug_features, R.drawable.adb_fill0)
) )
fun getAllRestrictions() = internet + connectivity + media + applications + users + other
} }
@Serializable object UserRestrictionEditor @Serializable object UserRestrictionEditor
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@RequiresApi(24)
@Composable @Composable
fun UserRestrictionEditorScreen(onNavigateUp: () -> Unit) { fun UserRestrictionEditorScreen(onNavigateUp: () -> Unit) {
val context = LocalContext.current val context = LocalContext.current
@@ -307,7 +314,12 @@ fun UserRestrictionEditorScreen(onNavigateUp: () -> Unit) {
) { ) {
Text(it) Text(it)
IconButton({ IconButton({
try {
dpm.clearUserRestriction(receiver, it) dpm.clearUserRestriction(receiver, it)
} catch (e: Exception) {
e.printStackTrace()
context.showOperationResultToast(false)
}
refresh() refresh()
}) { }) {
Icon(Icons.Outlined.Delete, null) Icon(Icons.Outlined.Delete, null)
@@ -317,9 +329,14 @@ fun UserRestrictionEditorScreen(onNavigateUp: () -> Unit) {
item { item {
var input by remember { mutableStateOf("") } var input by remember { mutableStateOf("") }
fun add() { fun add() {
try {
dpm.addUserRestriction(receiver, input) dpm.addUserRestriction(receiver, input)
refresh()
input = "" input = ""
} catch (e: Exception) {
e.printStackTrace()
context.showOperationResultToast(false)
}
refresh()
} }
OutlinedTextField( OutlinedTextField(
input, { input = it }, Modifier.fillMaxWidth().padding(HorizontalPadding, 20.dp), input, { input = it }, Modifier.fillMaxWidth().padding(HorizontalPadding, 20.dp),