Skip to content

Commit

Permalink
use a dialog to confirm before clear app storage
Browse files Browse the repository at this point in the history
  • Loading branch information
BinTianqi committed May 27, 2024
1 parent cb434e6 commit fb6fd00
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: OwnDroid-CI-${{ env.SHORT_SHA }}-release-signed.apk
path: app/build/outputs/apk/debug/app-release.apk
path: app/build/outputs/apk/release/app-release.apk
78 changes: 49 additions & 29 deletions app/src/main/java/com/bintianqi/owndroid/dpm/ApplicationManage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState<String>,
"KeepUninstalled" to R.string.keep_uninstalled_pkgs,
"InstallApp" to R.string.install_app,
"UninstallApp" to R.string.uninstall_app,
"ClearAppData" to R.string.clear_app_data,
"ClearAppData" to R.string.clear_app_storage,
"DefaultDialer" to R.string.set_default_dialer,
)
val clearAppDataDialog = remember { mutableStateOf(false) }
Scaffold(
topBar = {
TopBar(backStackEntry, navCtrl, localNavCtrl) {
Expand Down Expand Up @@ -121,7 +122,7 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState<String>,
popEnterTransition = Animations.navHostPopEnterTransition,
popExitTransition = Animations.navHostPopExitTransition
) {
composable(route = "Home") { Home(localNavCtrl, pkgName.value, dialogStatus) }
composable(route = "Home") { Home(localNavCtrl, pkgName.value, dialogStatus, clearAppDataDialog) }
composable(route = "UserControlDisabled") { UserCtrlDisabledPkg(pkgName.value) }
composable(route = "PermissionManage") { PermissionManage(pkgName.value, navCtrl) }
composable(route = "CrossProfilePackage") { CrossProfilePkg(pkgName.value) }
Expand All @@ -132,7 +133,6 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState<String>,
composable(route = "KeepUninstalled") { KeepUninstalledApp(pkgName.value) }
composable(route = "InstallApp") { InstallApp() }
composable(route = "UninstallApp") { UninstallApp(pkgName.value) }
composable(route = "ClearAppData") { ClearAppData(pkgName.value) }
composable(route = "DefaultDialer") { DefaultDialerApp(pkgName.value) }
}
}
Expand All @@ -141,10 +141,13 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState<String>,
LocalFocusManager.current.clearFocus()
AppControlDialog(dialogStatus)
}
if(clearAppDataDialog.value) {
ClearAppDataDialog(clearAppDataDialog, pkgName.value)
}
}

@Composable
private fun Home(navCtrl:NavHostController, pkgName: String, dialogStatus: MutableIntState) {
private fun Home(navCtrl:NavHostController, pkgName: String, dialogStatus: MutableIntState, clearAppDataDialog: MutableState<Boolean>) {
Column(
modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())
) {
Expand Down Expand Up @@ -251,7 +254,9 @@ private fun Home(navCtrl:NavHostController, pkgName: String, dialogStatus: Mutab
SubPageItem(R.string.keep_uninstalled_pkgs, "", R.drawable.delete_fill0) { navCtrl.navigate("KeepUninstalled") }
}
if(VERSION.SDK_INT>=28) {
SubPageItem(R.string.clear_app_data, "", R.drawable.mop_fill0) { navCtrl.navigate("ClearAppData") }
SubPageItem(R.string.clear_app_storage, "", R.drawable.mop_fill0) {
if(pkgName != "") { clearAppDataDialog.value = true }
}
}
SubPageItem(R.string.install_app, "", R.drawable.install_mobile_fill0) { navCtrl.navigate("InstallApp") }
SubPageItem(R.string.uninstall_app, "", R.drawable.delete_fill0) { navCtrl.navigate("UninstallApp") }
Expand Down Expand Up @@ -843,34 +848,49 @@ private fun InstallApp() {

@SuppressLint("NewApi")
@Composable
private fun ClearAppData(pkgName: String) {
fun ClearAppDataDialog(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)
val focusMgr = LocalFocusManager.current
Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())) {
Spacer(Modifier.padding(vertical = 10.dp))
Button(
onClick = {
val executor = Executors.newCachedThreadPool()
val onClear = DevicePolicyManager.OnClearApplicationUserDataListener { pkg: String, succeed: Boolean ->
Looper.prepare()
focusMgr.clearFocus()
val toastText =
if(pkg!="") { "$pkg\n" }else{ "" } +
context.getString(R.string.clear_data) +
context.getString(if(succeed) { R.string.success } else { R.string.fail })
Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show()
Looper.loop()
AlertDialog(
title = {
Text(text = stringResource(R.string.clear_app_storage))
},
text = {
Text(text = stringResource(R.string.following_app_storage_will_clear) + "\n" + pkgName)
},
confirmButton = {
TextButton(
onClick = {
val executor = Executors.newCachedThreadPool()
val onClear = DevicePolicyManager.OnClearApplicationUserDataListener { pkg: String, succeed: Boolean ->
Looper.prepare()
val toastText =
if(pkg!="") { "$pkg\n" }else{ "" } +
context.getString(R.string.clear_data) +
context.getString(if(succeed) R.string.success else R.string.fail )
Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show()
Looper.loop()
}
dpm.clearApplicationUserData(receiver, pkgName, executor, onClear)
status.value = false
}
dpm.clearApplicationUserData(receiver, pkgName, executor, onClear)
},
enabled = isDeviceOwner(dpm) || isProfileOwner(dpm),
modifier = Modifier.fillMaxWidth().padding(horizontal = 10.dp)
) {
Text(stringResource(R.string.clear_app_data))
}
}
) {
Text(text = stringResource(R.string.clear))
}
},
dismissButton = {
TextButton(
onClick = { status.value = false }
) {
Text(text = stringResource(R.string.cancel))
}
},
onDismissRequest = {
status.value = false
},
modifier = Modifier.fillMaxWidth()
)
}

@SuppressLint("NewApi")
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@
<string name="permitted_ime">许可的输入法</string>
<string name="keep_uninstalled_pkgs">保持卸载的应用</string>
<string name="clear_data">数据清除</string>
<string name="clear_app_data">清除应用存储</string>
<string name="clear_app_storage">清除应用存储</string>
<string name="following_app_storage_will_clear">以下应用的存储空间将被清空</string>
<string name="set_default_dialer">设为默认拨号应用</string>
<string name="uninstall_app">卸载应用</string>
<string name="silent_uninstall">静默卸载</string>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@
<string name="permitted_ime">Permitted IME</string>
<string name="keep_uninstalled_pkgs">Keep uninstalled packages</string>
<string name="clear_data">Clear data</string>
<string name="clear_app_data">Clear app data</string>
<string name="clear_app_storage">Clear app storage</string>
<string name="following_app_storage_will_clear">The following app\'s storage will be cleared</string>
<string name="set_default_dialer">Set default dialer</string>
<string name="uninstall_app">Uninstall app</string>
<string name="silent_uninstall">Silent uninstall</string>
Expand Down

0 comments on commit fb6fd00

Please sign in to comment.