From 35aafddc8ec1b37cb52eb732d6423f534a7e566a Mon Sep 17 00:00:00 2001 From: BinTianqi Date: Thu, 18 Jul 2024 09:32:47 +0800 Subject: [PATCH] use a dialog to confirm wipe data --- .../bintianqi/owndroid/dpm/SystemManager.kt | 125 +++++++++++------- app/src/main/res/values-tr/strings.xml | 7 +- app/src/main/res/values-zh-rCN/strings.xml | 7 +- app/src/main/res/values/strings.xml | 7 +- 4 files changed, 83 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/SystemManager.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/SystemManager.kt index d22562d..6a7d4dd 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/SystemManager.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/SystemManager.kt @@ -40,7 +40,6 @@ import android.content.ComponentName import android.content.Context import android.content.Intent import android.net.Uri -import android.os.Binder import android.os.Build.VERSION import android.os.UserManager import android.util.Log @@ -225,7 +224,7 @@ private fun Home(navCtrl: NavHostController, scrollState: ScrollState, rebootDia SubPageItem(R.string.frp_policy, "", R.drawable.device_reset_fill0) { navCtrl.navigate("FRP") } } if(dpm.isAdminActive(receiver)) { - SubPageItem(R.string.wipe_data, "", R.drawable.warning_fill0) { navCtrl.navigate("WipeData") } + SubPageItem(R.string.wipe_data, "", R.drawable.device_reset_fill0) { navCtrl.navigate("WipeData") } } Spacer(Modifier.padding(vertical = 30.dp)) LaunchedEffect(Unit) { fileUriFlow.value = Uri.parse("") } @@ -1064,6 +1063,7 @@ fun FactoryResetProtection() { } } +@SuppressLint("NewApi") @Composable private fun WipeData() { val context = LocalContext.current @@ -1071,14 +1071,14 @@ private fun WipeData() { val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager val receiver = ComponentName(context,Receiver::class.java) val focusMgr = LocalFocusManager.current + var warning by remember { mutableStateOf(false) } + var wipeDevice by remember { mutableStateOf(false) } + var externalStorage by remember { mutableStateOf(false) } + var protectionData by remember { mutableStateOf(false) } + var euicc by remember { mutableStateOf(false) } + var silent by remember { mutableStateOf(false) } + var reason by remember { mutableStateOf("") } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())) { - var flag by remember { mutableIntStateOf(0) } - var confirmed by remember { mutableStateOf(false) } - var externalStorage by remember { mutableStateOf(false) } - var protectionData by remember { mutableStateOf(false) } - var euicc by remember { mutableStateOf(false) } - var silent by remember { mutableStateOf(false) } - var reason by remember { mutableStateOf("") } Spacer(Modifier.padding(vertical = 10.dp)) Text( text = stringResource(R.string.wipe_data), @@ -1088,62 +1088,44 @@ private fun WipeData() { Spacer(Modifier.padding(vertical = 5.dp)) CheckBoxItem( stringResource(R.string.wipe_external_storage), - externalStorage, { externalStorage = it; confirmed = false } + externalStorage, { externalStorage = it } ) if(VERSION.SDK_INT >= 22 && isDeviceOwner(dpm)) { CheckBoxItem(stringResource(R.string.wipe_reset_protection_data), - protectionData, { protectionData = it; confirmed = false} + protectionData, { protectionData = it } ) } - if(VERSION.SDK_INT >= 28) { CheckBoxItem(stringResource(R.string.wipe_euicc), euicc, { euicc = it; confirmed = false }) } - if(VERSION.SDK_INT >= 29) { CheckBoxItem(stringResource(R.string.wipe_silently), silent, { silent = it; confirmed = false }) } + if(VERSION.SDK_INT >= 28) { CheckBoxItem(stringResource(R.string.wipe_euicc), euicc, { euicc = it }) } + if(VERSION.SDK_INT >= 29) { CheckBoxItem(stringResource(R.string.wipe_silently), silent, { silent = it }) } AnimatedVisibility(!silent && VERSION.SDK_INT >= 28) { OutlinedTextField( value = reason, onValueChange = { reason = it }, label = { Text(stringResource(R.string.reason)) }, - enabled = !confirmed, modifier = Modifier.fillMaxWidth().padding(vertical = 3.dp) ) } Spacer(Modifier.padding(vertical = 5.dp)) - Button( - onClick = { - focusMgr.clearFocus() - flag = 0 - if(externalStorage) { flag += WIPE_EXTERNAL_STORAGE } - if(protectionData && VERSION.SDK_INT >= 22) { flag += WIPE_RESET_PROTECTION_DATA } - if(euicc && VERSION.SDK_INT >= 28) { flag += WIPE_EUICC } - if(reason == "") { silent = true } - if(silent && VERSION.SDK_INT >= 29) { flag += WIPE_SILENTLY } - confirmed = !confirmed - }, - colors = ButtonDefaults.buttonColors( - containerColor = if(confirmed) colorScheme.primary else colorScheme.error , - contentColor = if(confirmed) colorScheme.onPrimary else colorScheme.onError - ), - modifier = Modifier.fillMaxWidth() - ) { - Text(text = stringResource(if(confirmed) R.string.cancel else R.string.confirm)) - } - Button( - onClick = { - if(VERSION.SDK_INT >= 28 && reason != "") { - dpm.wipeData(flag, reason) - }else{ - dpm.wipeData(flag) - } - }, - colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError), - enabled = confirmed && (VERSION.SDK_INT < 34 || (VERSION.SDK_INT >= 34 && !userManager.isSystemUser)), - modifier = Modifier.fillMaxWidth() - ) { - Text("WipeData") + if(VERSION.SDK_INT < 34 || (VERSION.SDK_INT >= 34 && !userManager.isSystemUser)) { + Button( + onClick = { + focusMgr.clearFocus() + wipeDevice = false + warning = true + }, + colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError), + modifier = Modifier.fillMaxWidth() + ) { + Text("WipeData") + } } if (VERSION.SDK_INT >= 34 && (isDeviceOwner(dpm) || dpm.isOrgProfile(receiver))) { Button( - onClick = { dpm.wipeDevice(flag) }, + onClick = { + focusMgr.clearFocus() + wipeDevice = true + warning = true + }, colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError), - enabled = confirmed, modifier = Modifier.fillMaxWidth() ) { Text("WipeDevice") @@ -1153,11 +1135,52 @@ private fun WipeData() { if(VERSION.SDK_INT >= 24 && isProfileOwner(dpm) && dpm.isManagedProfile(receiver)) { Information{ Text(text = stringResource(R.string.will_delete_work_profile)) } } - if(VERSION.SDK_INT >= 34 && Binder.getCallingUid()/100000 == 0) { - Information{ Text(text = stringResource(R.string.api34_or_above_wipedata_cannot_in_system_user)) } - } Spacer(Modifier.padding(vertical = 30.dp)) } + if(warning) { + LaunchedEffect(Unit) { silent = reason == "" } + AlertDialog( + title = { + Text(text = stringResource(R.string.warning), color = colorScheme.error) + }, + text = { + Text(text = stringResource( + if(VERSION.SDK_INT >= 24 && isProfileOwner(dpm) && dpm.isManagedProfile(receiver)) R.string.wipe_work_profile_warning + else R.string.wipe_data_warning), + color = colorScheme.error + ) + }, + onDismissRequest = { warning = false }, + confirmButton = { + TextButton( + onClick = { + var flag = 0 + if(externalStorage) { flag += WIPE_EXTERNAL_STORAGE } + if(protectionData && VERSION.SDK_INT >= 22) { flag += WIPE_RESET_PROTECTION_DATA } + if(euicc && VERSION.SDK_INT >= 28) { flag += WIPE_EUICC } + if(silent && VERSION.SDK_INT >= 29) { flag += WIPE_SILENTLY } + if(wipeDevice) { + dpm.wipeDevice(flag) + } else { + if(VERSION.SDK_INT >= 28 && reason != "") { + dpm.wipeData(flag, reason) + } else { + dpm.wipeData(flag) + } + } + }, + colors = ButtonDefaults.textButtonColors(contentColor = colorScheme.error) + ) { + Text(stringResource(R.string.confirm)) + } + }, + dismissButton = { + TextButton(onClick = { warning = false }) { + Text(stringResource(R.string.cancel)) + } + } + ) + } } @Composable diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index b7e0106..59b3829 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -51,10 +51,10 @@ Mevcut Durum: Başlat Stop - Bilinmeyen Hata Tümünü İzin Ver Politika Kullan Hesap + Warning Etkinleştirmek İçin Tıklayın @@ -167,7 +167,6 @@ Küresel eylemlere izin ver Ekran kilidine izin ver Görevde etkinlik başlatmayı engelle - Beyaz listeye alınan uygulama CA sertifikası Lütfen bir sertifika seçin Yüklenen sertifika: %1$s @@ -181,7 +180,8 @@ eUICC (eSIM) sil Sessizce sil Çalışma profili silinecek - System user\'da WipeData kullanamazsınız + All data on your device will be ERASED + Your work profile will be REMOVED Şifreleme durumu: FRP politikası Fabrika ayarlarına sıfırlama koruma politikası @@ -220,7 +220,6 @@ SSID listesi: Boş olamaz Zaten mevcut - Lütfen bir politika seçin Özel DNS Ana bilgisayar adı sağlayın Ana bilgisayar hizmet vermiyor diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index ac76577..ea3eab4 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -48,10 +48,10 @@ 当前状态: 开始 停止 - 未知错误 允许全部 使用策略 账户 + 警告 点击以激活 @@ -160,7 +160,6 @@ 允许全局行为(比如长按电源键对话框) 允许锁屏 阻止启动未允许的应用 - 白名单应用 包名 不存在 Ca证书 @@ -176,7 +175,8 @@ 清除eUICC(eSIM) 静默清除 将会删除工作资料 - API34或以上将不能在系统用户中使用WipeData + 你的设备上的所有数据将会被清除 + 你的工作资料将会被删除 加密状态: FRP策略 恢复出厂设置保护策略 @@ -215,7 +215,6 @@ SSID列表: 不能为空 已经存在 - 请选择策略 私人DNS 指定主机名 主机不支持 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d4e8a60..96e2e3a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -51,10 +51,10 @@ Current status: Start Stop - Unknown error Allow all Use policy Account + Warning Click to activate @@ -172,7 +172,6 @@ Allow global actions Allow keyguard Block activity start in task - Whitelisted app Ca certification Please select a certification Cert installed: %1$s @@ -186,7 +185,8 @@ Wipe eUICC(eSIM) Wipe silently Work profile will be deleted. - You cannot use WipeData in system user. + All data on your device will be ERASED + Your work profile will be REMOVED Encrypt status: FRP policy Factory reset protection policy @@ -226,7 +226,6 @@ SSID list: Cannot be empty Already exist - Please select a policy PrivateDNS Provide hostname Host not serving