reset all settings after deactivating device owner

close #56
This commit is contained in:
BinTianqi
2024-07-22 16:51:02 +08:00
parent e004fb0dbd
commit cacd9438e1
6 changed files with 102 additions and 9 deletions

View File

@@ -3,7 +3,10 @@ package com.bintianqi.owndroid.dpm
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.app.admin.DevicePolicyManager
import android.app.admin.FactoryResetProtectionPolicy
import android.app.admin.IDevicePolicyManager
import android.app.admin.SystemUpdatePolicy
import android.app.admin.WifiSsidPolicy
import android.content.ComponentName
import android.content.Context
import android.content.Intent
@@ -126,3 +129,68 @@ fun Context.getReceiver(): ComponentName {
}
val dhizukuErrorStatus = MutableStateFlow(0)
fun Context.resetDevicePolicy() {
val dpm = getDPM()
val receiver = getReceiver()
RestrictionData.getAllRestrictions(this).forEach {
dpm.clearUserRestriction(receiver, it)
}
dpm.accountTypesWithManagementDisabled?.forEach {
dpm.setAccountManagementDisabled(receiver, it, false)
}
if (VERSION.SDK_INT >= 30) {
dpm.setConfiguredNetworksLockdownState(receiver, false)
dpm.setAutoTimeZoneEnabled(receiver, true)
dpm.setAutoTimeEnabled(receiver, true)
dpm.setCommonCriteriaModeEnabled(receiver, false)
try {
val frp = FactoryResetProtectionPolicy.Builder().setFactoryResetProtectionEnabled(false).setFactoryResetProtectionAccounts(listOf())
dpm.setFactoryResetProtectionPolicy(receiver, frp.build())
} catch(_: Exception) {}
dpm.setUserControlDisabledPackages(receiver, listOf())
}
if (VERSION.SDK_INT >= 33) {
dpm.minimumRequiredWifiSecurityLevel = DevicePolicyManager.WIFI_SECURITY_OPEN
dpm.wifiSsidPolicy = null
}
if (VERSION.SDK_INT >= 28) {
dpm.getOverrideApns(receiver).forEach { dpm.removeOverrideApn(receiver, it.id) }
dpm.setKeepUninstalledPackages(receiver, listOf())
}
dpm.setCameraDisabled(receiver, false)
dpm.setScreenCaptureDisabled(receiver, false)
dpm.setMasterVolumeMuted(receiver, false)
try {
if(VERSION.SDK_INT >= 31) dpm.isUsbDataSignalingEnabled = true
} catch (_: Exception) { }
if (VERSION.SDK_INT >= 23) {
dpm.setPermissionPolicy(receiver, DevicePolicyManager.PERMISSION_POLICY_PROMPT)
dpm.setSystemUpdatePolicy(receiver, SystemUpdatePolicy.createAutomaticInstallPolicy())
}
if (VERSION.SDK_INT >= 24) {
dpm.setAlwaysOnVpnPackage(receiver, null, false)
dpm.setPackagesSuspended(receiver, arrayOf(), false)
}
dpm.setPermittedInputMethods(receiver, null)
dpm.setPermittedAccessibilityServices(receiver, null)
packageManager.getInstalledApplications(0).forEach {
if (dpm.isUninstallBlocked(receiver, it.packageName)) dpm.setUninstallBlocked(receiver, it.packageName, false)
}
if (VERSION.SDK_INT >= 26) {
dpm.setRequiredStrongAuthTimeout(receiver, 0)
dpm.clearResetPasswordToken(receiver)
}
if (VERSION.SDK_INT >= 31) {
dpm.requiredPasswordComplexity = DevicePolicyManager.PASSWORD_COMPLEXITY_NONE
}
dpm.setKeyguardDisabledFeatures(receiver, 0)
dpm.setMaximumTimeToLock(receiver, 0)
dpm.setPasswordExpirationTimeout(receiver, 0)
dpm.setMaximumFailedPasswordsForWipe(receiver, 0)
dpm.setPasswordHistoryLength(receiver, 0)
if (VERSION.SDK_INT < 31) {
dpm.setPasswordQuality(receiver, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED)
}
dpm.setRecommendedGlobalProxy(receiver, null)
}

View File

@@ -40,6 +40,9 @@ import com.bintianqi.owndroid.backToHomeStateFlow
import com.bintianqi.owndroid.ui.*
import com.rosan.dhizuku.api.Dhizuku
import com.rosan.dhizuku.api.DhizukuRequestPermissionListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable
fun DpmPermissions(navCtrl:NavHostController) {
@@ -350,9 +353,15 @@ private fun DeviceOwner() {
}
if(deactivateDialog) {
val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE)
val coroutine = rememberCoroutineScope()
AlertDialog(
title = { Text(stringResource(R.string.deactivate)) },
text = { if(sharedPref.getBoolean("dhizuku", false)) Text(stringResource(R.string.dhizuku_will_be_deactivated)) },
text = {
Column {
if(sharedPref.getBoolean("dhizuku", false)) Text(stringResource(R.string.dhizuku_will_be_deactivated))
Text(stringResource(R.string.will_reset_policy))
}
},
onDismissRequest = { deactivateDialog = false },
dismissButton = {
TextButton(
@@ -364,14 +373,17 @@ private fun DeviceOwner() {
confirmButton = {
TextButton(
onClick = {
dpm.clearDeviceOwnerApp(context.dpcPackageName)
if(sharedPref.getBoolean("dhizuku", false)) {
if (!Dhizuku.init(context)) {
sharedPref.edit().putBoolean("dhizuku", false).apply()
backToHomeStateFlow.value = true
coroutine.launch {
context.resetDevicePolicy()
dpm.clearDeviceOwnerApp(context.dpcPackageName)
if(sharedPref.getBoolean("dhizuku", false)) {
if (!Dhizuku.init(context)) {
sharedPref.edit().putBoolean("dhizuku", false).apply()
backToHomeStateFlow.value = true
}
}
deactivateDialog = false
}
deactivateDialog = false
}
) {
Text(stringResource(R.string.confirm))

View File

@@ -35,7 +35,7 @@ import com.bintianqi.owndroid.ui.SubPageItem
import com.bintianqi.owndroid.ui.SwitchItem
import com.bintianqi.owndroid.ui.TopBar
private data class Restriction(
data class Restriction(
val restriction:String,
@StringRes val name:Int,
val desc:String,
@@ -211,7 +211,7 @@ private fun UserRestrictionItem(
)
}
private object RestrictionData{
object RestrictionData {
fun internet(): List<Restriction>{
val list:MutableList<Restriction> = mutableListOf()
list += Restriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, R.string.config_mobile_network, "", R.drawable.signal_cellular_alt_fill0)
@@ -313,4 +313,14 @@ private object RestrictionData{
list += Restriction(UserManager.DISALLOW_DEBUGGING_FEATURES, R.string.debug_features, "", R.drawable.adb_fill0)
return list
}
fun getAllRestrictions(context: Context): List<String> {
val result = mutableListOf<String>()
internet().forEach { result.add(it.restriction) }
connectivity().forEach { result.add(it.restriction) }
media().forEach { result.add(it.restriction) }
application(context).forEach { result.add(it.restriction) }
user().forEach { result.add(it.restriction) }
other(context).forEach { result.add(it.restriction) }
return result
}
}