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(
Modifier
.fillMaxWidth()
.background(if (privilege.device) colorScheme.primaryContainer else Color.Transparent)
.background(colorScheme.primaryContainer)
.padding(HorizontalPadding, 10.dp),
Arrangement.SpaceBetween, Alignment.CenterVertically
) {
Column {
Text(stringResource(R.string.profile_owner), style = typography.titleLarge)
}
Icon(
if(privilege.device) Icons.Default.Check else Icons.AutoMirrored.Default.KeyboardArrowRight, null,
tint = if(privilege.device) colorScheme.primary else colorScheme.onBackground
)
Icon(Icons.Default.Check, null, tint = colorScheme.primary)
}
if(privilege.dhizuku || !(privilege.device || privilege.profile)) Row(
Modifier

View File

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