Skip to content

Commit

Permalink
new feature in app manager: enable system app
Browse files Browse the repository at this point in the history
  • Loading branch information
BinTianqi committed May 30, 2024
1 parent ac4f034 commit ee1b6af
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
57 changes: 49 additions & 8 deletions app/src/main/java/com/bintianqi/owndroid/dpm/ApplicationManage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState<String>,
)
val clearAppDataDialog = remember { mutableStateOf(false) }
val defaultDialerAppDialog = remember { mutableStateOf(false) }
val enableSystemAppDialog = remember { mutableStateOf(false) }
Scaffold(
topBar = {
TopBar(backStackEntry, navCtrl, localNavCtrl) {
Expand Down Expand Up @@ -119,7 +120,9 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState<String>,
popEnterTransition = Animations.navHostPopEnterTransition,
popExitTransition = Animations.navHostPopExitTransition
) {
composable(route = "Home") { Home(localNavCtrl, pkgName.value, dialogStatus, clearAppDataDialog, defaultDialerAppDialog) }
composable(route = "Home") {
Home(localNavCtrl, pkgName.value, dialogStatus, clearAppDataDialog, defaultDialerAppDialog, enableSystemAppDialog)
}
composable(route = "UserControlDisabled") { UserCtrlDisabledPkg(pkgName.value) }
composable(route = "PermissionManage") { PermissionManage(pkgName.value, navCtrl) }
composable(route = "CrossProfilePackage") { CrossProfilePkg(pkgName.value) }
Expand All @@ -143,6 +146,9 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState<String>,
if(defaultDialerAppDialog.value) {
DefaultDialerAppDialog(defaultDialerAppDialog, pkgName.value)
}
if(enableSystemAppDialog.value) {
EnableSystemAppDialog(enableSystemAppDialog, pkgName.value)
}
}

@Composable
Expand All @@ -151,7 +157,8 @@ private fun Home(
pkgName: String,
dialogStatus: MutableIntState,
clearAppDataDialog: MutableState<Boolean>,
defaultDialerAppDialog: MutableState<Boolean>
defaultDialerAppDialog: MutableState<Boolean>,
enableSystemAppDialog: MutableState<Boolean>
) {
Column(
modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())
Expand Down Expand Up @@ -255,6 +262,9 @@ private fun Home(
if(isDeviceOwner(dpm)||isProfileOwner(dpm)) {
SubPageItem(R.string.permitted_ime, "", R.drawable.keyboard_fill0) { navCtrl.navigate("IME") }
}
if(isDeviceOwner(dpm) || isProfileOwner(dpm)) {
SubPageItem(R.string.enable_system_app, "", R.drawable.enable_fill0) { enableSystemAppDialog.value = true }
}
if(VERSION.SDK_INT>=28&&isDeviceOwner(dpm)) {
SubPageItem(R.string.keep_uninstalled_packages, "", R.drawable.delete_fill0) { navCtrl.navigate("KeepUninstalled") }
}
Expand Down Expand Up @@ -932,9 +942,7 @@ private fun ClearAppDataDialog(status: MutableState<Boolean>, pkgName: String) {
val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val receiver = ComponentName(context,Receiver::class.java)
AlertDialog(
title = {
Text(text = stringResource(R.string.clear_app_storage))
},
title = { Text(text = stringResource(R.string.clear_app_storage)) },
text = {
Text(stringResource(R.string.app_storage_will_be_cleared) + "\n" + pkgName)
},
Expand Down Expand Up @@ -977,9 +985,7 @@ private fun DefaultDialerAppDialog(status: MutableState<Boolean>, pkgName: Strin
val context = LocalContext.current
val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
AlertDialog(
title = {
Text(stringResource(R.string.set_default_dialer))
},
title = { Text(stringResource(R.string.set_default_dialer)) },
text = {
Text(stringResource(R.string.app_will_be_default_dialer) + "\n" + pkgName)
},
Expand Down Expand Up @@ -1008,6 +1014,41 @@ private fun DefaultDialerAppDialog(status: MutableState<Boolean>, pkgName: Strin
)
}

@Composable
private fun EnableSystemAppDialog(status: MutableState<Boolean>, pkgName: String) {
val context = LocalContext.current
val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val receiver = ComponentName(context,Receiver::class.java)
AlertDialog(
title = { Text(stringResource(R.string.enable_system_app)) },
text = {
Text(stringResource(R.string.enable_system_app_desc) + "\n" + pkgName)
},
onDismissRequest = { status.value = false },
dismissButton = {
TextButton(onClick = { status.value = false }) {
Text(stringResource(R.string.cancel))
}
},
confirmButton = {
TextButton(
onClick = {
try {
dpm.enableSystemApp(receiver, pkgName)
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
} catch(e: IllegalArgumentException) {
Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show()
}
status.value = false
}
) {
Text(stringResource(R.string.confirm))
}
},
modifier = Modifier.fillMaxWidth()
)
}

@Composable
private fun AppControlDialog(status: MutableIntState) {
val enabled = dialogGetStatus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import com.bintianqi.owndroid.ui.TopBar
import com.bintianqi.owndroid.uriToStream
import java.util.Date
import java.util.TimeZone
import java.util.concurrent.Executors

@Composable
fun SystemManage(navCtrl:NavHostController) {
Expand Down Expand Up @@ -1121,8 +1122,9 @@ fun InstallSystemUpdate() {
AnimatedVisibility(uri != Uri.parse("")) {
Button(
onClick = {
val executor = Executors.newCachedThreadPool()
try {
dpm.installSystemUpdate(receiver, uri, { it.run() }, callback)
dpm.installSystemUpdate(receiver, uri, executor, callback)
Toast.makeText(context, R.string.start_install_system_update, Toast.LENGTH_SHORT).show()
}catch(e: Exception) {
Toast.makeText(
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/enable_fill0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M480,880q-83,0 -156,-31.5T197,763q-54,-54 -85.5,-127T80,480q0,-132 77,-237.5T360,98v86q-91,37 -145.5,117.5T160,480q0,134 93,227t227,93q134,0 227,-93t93,-227q0,-98 -54.5,-178.5T600,184v-86q126,39 203,144.5T880,480q0,83 -31.5,156T763,763q-54,54 -127,85.5T480,880ZM480,640L280,440l56,-56 104,103v-407h80v407l104,-103 56,56 -200,200Z"
android:fillColor="#000000"/>
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@
<string name="select_apk" tools:ignore="TypographyEllipsis">选择APK...</string>
<string name="silent_install">静默安装</string>
<string name="request_install">请求安装</string>
<string name="enable_system_app">启用系统应用</string>
<string name="enable_system_app_desc">重新启用一个默认被禁用的系统应用</string>
<string name="app_installer_status">应用安装器:</string>
<string name="status_pending_action">等待用户操作</string>
<string name="status_fail_blocked">被阻止</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@
<string name="app_info">App info</string>
<string name="not_installed">Not installed</string>
<string name="block_uninstall">Block uninstall</string>
<string name="enable_system_app">Enable system app</string>
<string name="enable_system_app_desc">Re-enable a system app that was disabled by default</string>
<!--ucd: user control disabled-->
<string name="ucd">Disable user control</string>
<string name="ucd_desc">If you set this, you cannot clear these apps\' storage or force stop them. </string>
Expand Down

0 comments on commit ee1b6af

Please sign in to comment.