From 5172075e0159f819e433a0a8a47f06e4c9093e24 Mon Sep 17 00:00:00 2001 From: BinTianqi Date: Thu, 16 May 2024 18:17:02 +0800 Subject: [PATCH] rename variables --- .../com/bintianqi/owndroid/MainActivity.kt | 46 +-- .../java/com/bintianqi/owndroid/Receiver.kt | 6 +- .../java/com/bintianqi/owndroid/Setting.kt | 8 +- .../main/java/com/bintianqi/owndroid/Utils.kt | 4 +- .../owndroid/dpm/ApplicationManage.kt | 246 +++++++-------- .../bintianqi/owndroid/dpm/ManagedProfile.kt | 78 ++--- .../com/bintianqi/owndroid/dpm/Network.kt | 152 ++++----- .../com/bintianqi/owndroid/dpm/Password.kt | 188 ++++++------ .../com/bintianqi/owndroid/dpm/Permissions.kt | 176 +++++------ .../bintianqi/owndroid/dpm/ShizukuActivate.kt | 34 +- .../bintianqi/owndroid/dpm/SystemManager.kt | 290 +++++++++--------- .../com/bintianqi/owndroid/dpm/UserManager.kt | 176 +++++------ .../bintianqi/owndroid/dpm/UserRestriction.kt | 46 +-- 13 files changed, 727 insertions(+), 723 deletions(-) diff --git a/app/src/main/java/com/bintianqi/owndroid/MainActivity.kt b/app/src/main/java/com/bintianqi/owndroid/MainActivity.kt index 1cb458c..b2aa449 100644 --- a/app/src/main/java/com/bintianqi/owndroid/MainActivity.kt +++ b/app/src/main/java/com/bintianqi/owndroid/MainActivity.kt @@ -59,7 +59,7 @@ class MainActivity : ComponentActivity() { val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true)) val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false)) OwnDroidTheme(materialYou.value, blackTheme.value){ - MyScaffold(materialYou, blackTheme) + Home(materialYou, blackTheme) } } } @@ -68,11 +68,11 @@ class MainActivity : ComponentActivity() { @SuppressLint("UnrememberedMutableState") @ExperimentalMaterial3Api @Composable -fun MyScaffold(materialYou:MutableState, blackTheme:MutableState){ +fun Home(materialYou:MutableState, blackTheme:MutableState){ val navCtrl = rememberNavController() - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE) val focusMgr = LocalFocusManager.current val pkgName = mutableStateOf("") @@ -111,26 +111,26 @@ fun MyScaffold(materialYou:MutableState, blackTheme:MutableState=24&&myDpm.isManagedProfile(myComponent))) + val profileNotActivated = !profileInited&&isProfileOwner(dpm)&&(VERSION.SDK_INT<24||(VERSION.SDK_INT>=24&&dpm.isManagedProfile(receiver))) if(profileNotActivated){ - myDpm.setProfileEnabled(myComponent) + dpm.setProfileEnabled(receiver) sharedPref.edit().putBoolean("ManagedProfileActivated",true).apply() - Toast.makeText(myContext, R.string.work_profile_activated, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.work_profile_activated, Toast.LENGTH_SHORT).show() } } } @Composable private fun HomePage(navCtrl:NavHostController, pkgName: MutableState){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) val activateType = stringResource( - if(isDeviceOwner(myDpm)){R.string.device_owner} - else if(isProfileOwner(myDpm)){ - if(VERSION.SDK_INT>=24&&myDpm.isManagedProfile(myComponent)){R.string.work_profile_owner}else{R.string.profile_owner} + if(isDeviceOwner(dpm)){R.string.device_owner} + else if(isProfileOwner(dpm)){ + if(VERSION.SDK_INT>=24&&dpm.isManagedProfile(receiver)){R.string.work_profile_owner}else{R.string.profile_owner} } - else if(myDpm.isAdminActive(myComponent)){R.string.device_admin}else{R.string.click_to_activate} + else if(dpm.isAdminActive(receiver)){R.string.device_admin}else{R.string.click_to_activate} ) LaunchedEffect(Unit){ pkgName.value = "" } Column(modifier = Modifier.background(colorScheme.background).statusBarsPadding().verticalScroll(rememberScrollState())) { @@ -152,14 +152,14 @@ private fun HomePage(navCtrl:NavHostController, pkgName: MutableState){ ) { Spacer(modifier = Modifier.padding(start = 22.dp)) Icon( - painter = painterResource(if(myDpm.isAdminActive(myComponent)){ R.drawable.check_circle_fill1 }else{ R.drawable.block_fill0 }), + painter = painterResource(if(dpm.isAdminActive(receiver)){ R.drawable.check_circle_fill1 }else{ R.drawable.block_fill0 }), contentDescription = null, tint = colorScheme.onPrimary ) Spacer(modifier = Modifier.padding(start = 10.dp)) Column { Text( - text = stringResource(if(myDpm.isAdminActive(myComponent)){R.string.activated}else{R.string.deactivated}), + text = stringResource(if(dpm.isAdminActive(receiver)){R.string.activated}else{R.string.deactivated}), style = typography.headlineSmall, color = colorScheme.onPrimary, modifier = Modifier.padding(bottom = 2.dp) @@ -168,11 +168,11 @@ private fun HomePage(navCtrl:NavHostController, pkgName: MutableState){ } } HomePageItem(R.string.system_manage, R.drawable.mobile_phone_fill0, "SystemManage", navCtrl) - if(VERSION.SDK_INT>=24&&(isDeviceOwner(myDpm))||isProfileOwner(myDpm)){ HomePageItem(R.string.network, R.drawable.wifi_fill0, "Network",navCtrl) } + if(VERSION.SDK_INT>=24&&(isDeviceOwner(dpm))||isProfileOwner(dpm)){ HomePageItem(R.string.network, R.drawable.wifi_fill0, "Network",navCtrl) } if( - (VERSION.SDK_INT<24&&!isDeviceOwner(myDpm))||( - VERSION.SDK_INT>=24&&(myDpm.isProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE)|| - (isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent))) + (VERSION.SDK_INT<24&&!isDeviceOwner(dpm))||( + VERSION.SDK_INT>=24&&(dpm.isProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE)|| + (isProfileOwner(dpm)&&dpm.isManagedProfile(receiver))) ) ){ HomePageItem(R.string.work_profile, R.drawable.work_fill0, "ManagedProfile",navCtrl) @@ -189,12 +189,12 @@ private fun HomePage(navCtrl:NavHostController, pkgName: MutableState){ } @Composable -fun HomePageItem(name:Int, imgVector:Int, navTo:String, myNav:NavHostController){ +fun HomePageItem(name:Int, imgVector:Int, navTo:String, navCtrl:NavHostController){ Row( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(25)) - .clickable(onClick = {myNav.navigate(navTo)}) + .clickable(onClick = {navCtrl.navigate(navTo)}) .padding(vertical = 13.dp), verticalAlignment = Alignment.CenterVertically ) { diff --git a/app/src/main/java/com/bintianqi/owndroid/Receiver.kt b/app/src/main/java/com/bintianqi/owndroid/Receiver.kt index 8e50434..47d92cf 100644 --- a/app/src/main/java/com/bintianqi/owndroid/Receiver.kt +++ b/app/src/main/java/com/bintianqi/owndroid/Receiver.kt @@ -16,9 +16,9 @@ import com.bintianqi.owndroid.dpm.isProfileOwner class Receiver : DeviceAdminReceiver() { override fun onEnabled(context: Context, intent: Intent) { super.onEnabled(context, intent) - val myDpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(context, this::class.java) - if(myDpm.isAdminActive(myComponent)||isProfileOwner(myDpm)||isDeviceOwner(myDpm)){ + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, this::class.java) + if(dpm.isAdminActive(receiver)||isProfileOwner(dpm)||isDeviceOwner(dpm)){ Toast.makeText(context, context.getString(R.string.onEnabled), Toast.LENGTH_SHORT).show() } } diff --git a/app/src/main/java/com/bintianqi/owndroid/Setting.kt b/app/src/main/java/com/bintianqi/owndroid/Setting.kt index eb59a9a..c6efe35 100644 --- a/app/src/main/java/com/bintianqi/owndroid/Setting.kt +++ b/app/src/main/java/com/bintianqi/owndroid/Setting.kt @@ -100,8 +100,8 @@ private fun Settings(materialYou:MutableState, blackTheme:MutableState< @Composable private fun About(){ - val myContext = LocalContext.current - val pkgInfo = myContext.packageManager.getPackageInfo(myContext.packageName,0) + val context = LocalContext.current + val pkgInfo = context.packageManager.getPackageInfo(context.packageName,0) val verCode = pkgInfo.versionCode val verName = pkgInfo.versionName Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())){ @@ -113,8 +113,8 @@ private fun About(){ Text(text = stringResource(R.string.about_desc)) Spacer(Modifier.padding(vertical = 5.dp)) } - SubPageItem(R.string.user_guide,"",R.drawable.open_in_new){shareLink(myContext, "https://github.com/BinTianqi/AndroidOwner/blob/master/Guide.md")} - SubPageItem(R.string.source_code,"",R.drawable.open_in_new){shareLink(myContext, "https://github.com/BinTianqi/AndroidOwner")} + SubPageItem(R.string.user_guide,"",R.drawable.open_in_new){shareLink(context, "https://github.com/BinTianqi/AndroidOwner/blob/master/Guide.md")} + SubPageItem(R.string.source_code,"",R.drawable.open_in_new){shareLink(context, "https://github.com/BinTianqi/AndroidOwner")} } } diff --git a/app/src/main/java/com/bintianqi/owndroid/Utils.kt b/app/src/main/java/com/bintianqi/owndroid/Utils.kt index f8f6d27..d6d215d 100644 --- a/app/src/main/java/com/bintianqi/owndroid/Utils.kt +++ b/app/src/main/java/com/bintianqi/owndroid/Utils.kt @@ -77,8 +77,8 @@ fun registerActivityResult(context: ComponentActivity){ } createManagedProfile = context.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {} addDeviceAdmin = context.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { - val myDpm = context.applicationContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - if(myDpm.isAdminActive(ComponentName(context.applicationContext, Receiver::class.java))){ + val dpm = context.applicationContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + if(dpm.isAdminActive(ComponentName(context.applicationContext, Receiver::class.java))){ backToHome = true } } diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/ApplicationManage.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/ApplicationManage.kt index 1b4076f..aeb6e48 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/ApplicationManage.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/ApplicationManage.kt @@ -97,7 +97,9 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState, TopBar(backStackEntry, navCtrl, localNavCtrl){Text(text = stringResource(titleMap[backStackEntry?.destination?.route] ?: R.string.app_manager))} } ){ paddingValues-> - Column(modifier = Modifier.fillMaxSize().padding(top = paddingValues.calculateTopPadding())){ + Column( + modifier = Modifier.fillMaxSize().padding(top = paddingValues.calculateTopPadding()) + ) { if(backStackEntry?.destination?.route!="InstallApp"){ TextField( value = pkgName.value, @@ -147,107 +149,109 @@ fun ApplicationManage(navCtrl:NavHostController, pkgName: MutableState, @Composable private fun Home(navCtrl:NavHostController, pkgName: String, dialogStatus: MutableIntState){ - Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) + Column( + modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()) + ) { + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) Spacer(Modifier.padding(vertical = 5.dp)) - if(VERSION.SDK_INT>=24&&isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent)){ + if(VERSION.SDK_INT>=24&&isProfileOwner(dpm)&&dpm.isManagedProfile(receiver)){ Text(text = stringResource(R.string.scope_is_work_profile), textAlign = TextAlign.Center,modifier = Modifier.fillMaxWidth()) } SubPageItem(R.string.app_info,"",R.drawable.open_in_new){ val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) intent.setData(Uri.parse("package:$pkgName")) - startActivity(myContext,intent,null) + startActivity(context, intent, null) } - if(VERSION.SDK_INT>=24&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=24&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ val getSuspendStatus = { - try{ myDpm.isPackageSuspended(myComponent, pkgName) } + try{ dpm.isPackageSuspended(receiver, pkgName) } catch(e:NameNotFoundException){ false } catch(e:IllegalArgumentException){ false } } SwitchItem( title = R.string.suspend, desc = "", icon = R.drawable.block_fill0, getState = getSuspendStatus, - onCheckedChange = { myDpm.setPackagesSuspended(myComponent, arrayOf(pkgName), it) }, + onCheckedChange = { dpm.setPackagesSuspended(receiver, arrayOf(pkgName), it) }, onClickBlank = { dialogGetStatus = getSuspendStatus - dialogConfirmButtonAction = { myDpm.setPackagesSuspended(myComponent, arrayOf(pkgName), true) } - dialogDismissButtonAction = { myDpm.setPackagesSuspended(myComponent, arrayOf(pkgName), false) } + dialogConfirmButtonAction = { dpm.setPackagesSuspended(receiver, arrayOf(pkgName), true) } + dialogDismissButtonAction = { dpm.setPackagesSuspended(receiver, arrayOf(pkgName), false) } dialogStatus.intValue = 1 } ) } - if(isDeviceOwner(myDpm)||isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm) || isProfileOwner(dpm)){ SwitchItem( title = R.string.hide, desc = stringResource(R.string.isapphidden_desc), icon = R.drawable.visibility_off_fill0, - getState = { myDpm.isApplicationHidden(myComponent,pkgName) }, - onCheckedChange = { myDpm.setApplicationHidden(myComponent, pkgName, it) }, + getState = { dpm.isApplicationHidden(receiver,pkgName) }, + onCheckedChange = { dpm.setApplicationHidden(receiver, pkgName, it) }, onClickBlank = { - dialogGetStatus = { myDpm.isApplicationHidden(myComponent,pkgName) } - dialogConfirmButtonAction = { myDpm.setApplicationHidden(myComponent, pkgName, true) } - dialogDismissButtonAction = { myDpm.setApplicationHidden(myComponent, pkgName, false) } + dialogGetStatus = { dpm.isApplicationHidden(receiver,pkgName) } + dialogConfirmButtonAction = { dpm.setApplicationHidden(receiver, pkgName, true) } + dialogDismissButtonAction = { dpm.setApplicationHidden(receiver, pkgName, false) } dialogStatus.intValue = 2 } ) } - if(isDeviceOwner(myDpm)||isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm) || isProfileOwner(dpm)) { SwitchItem( title = R.string.block_uninstall, desc = "", icon = R.drawable.delete_forever_fill0, - getState = { myDpm.isUninstallBlocked(myComponent,pkgName) }, - onCheckedChange = { myDpm.setUninstallBlocked(myComponent,pkgName,it) }, + getState = { dpm.isUninstallBlocked(receiver,pkgName) }, + onCheckedChange = { dpm.setUninstallBlocked(receiver,pkgName,it) }, onClickBlank = { - dialogGetStatus = { myDpm.isUninstallBlocked(myComponent,pkgName) } - dialogConfirmButtonAction = { myDpm.setUninstallBlocked(myComponent,pkgName,true) } - dialogDismissButtonAction = { myDpm.setUninstallBlocked(myComponent,pkgName,false) } + dialogGetStatus = { dpm.isUninstallBlocked(receiver,pkgName) } + dialogConfirmButtonAction = { dpm.setUninstallBlocked(receiver,pkgName,true) } + dialogDismissButtonAction = { dpm.setUninstallBlocked(receiver,pkgName,false) } dialogStatus.intValue = 3 } ) } - if(VERSION.SDK_INT>=24&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=24&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ val setAlwaysOnVpn: (Boolean)->Unit = { try { - myDpm.setAlwaysOnVpnPackage(myComponent, pkgName, it) + dpm.setAlwaysOnVpnPackage(receiver, pkgName, it) } catch(e: UnsupportedOperationException) { - Toast.makeText(myContext, R.string.unsupported, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.unsupported, Toast.LENGTH_SHORT).show() } catch(e: NameNotFoundException) { - Toast.makeText(myContext, R.string.not_installed, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.not_installed, Toast.LENGTH_SHORT).show() } } SwitchItem( title = R.string.always_on_vpn, desc = "", icon = R.drawable.vpn_key_fill0, - getState = {pkgName == myDpm.getAlwaysOnVpnPackage(myComponent)}, + getState = {pkgName == dpm.getAlwaysOnVpnPackage(receiver)}, onCheckedChange = setAlwaysOnVpn, onClickBlank = { - dialogGetStatus = { pkgName == myDpm.getAlwaysOnVpnPackage(myComponent) } + dialogGetStatus = { pkgName == dpm.getAlwaysOnVpnPackage(receiver) } dialogConfirmButtonAction = { setAlwaysOnVpn(true) } dialogDismissButtonAction = { setAlwaysOnVpn(false) } dialogStatus.intValue = 4 } ) } - if((VERSION.SDK_INT>=33&&isProfileOwner(myDpm))||(VERSION.SDK_INT>=30&&isDeviceOwner(myDpm))){ + if((VERSION.SDK_INT>=33&&isProfileOwner(dpm))||(VERSION.SDK_INT>=30&&isDeviceOwner(dpm))){ SubPageItem(R.string.ucd,"",R.drawable.do_not_touch_fill0){navCtrl.navigate("UserControlDisabled")} } - if(VERSION.SDK_INT>=23&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=23&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ SubPageItem(R.string.permission_manage,"",R.drawable.key_fill0){navCtrl.navigate("PermissionManage")} } - if(VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent)){ + if(VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isManagedProfile(receiver)){ SubPageItem(R.string.cross_profile_package,"",R.drawable.work_fill0){navCtrl.navigate("CrossProfilePackage")} } - if(isProfileOwner(myDpm)){ + if(isProfileOwner(dpm)){ SubPageItem(R.string.cross_profile_widget,"",R.drawable.widgets_fill0){navCtrl.navigate("CrossProfileWidget")} } - if(VERSION.SDK_INT>=34&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=34&&isDeviceOwner(dpm)){ SubPageItem(R.string.credential_manage_policy,"",R.drawable.license_fill0){navCtrl.navigate("CredentialManagePolicy")} } - if(isProfileOwner(myDpm)||isDeviceOwner(myDpm)){ + if(isProfileOwner(dpm)||isDeviceOwner(dpm)){ SubPageItem(R.string.permitted_accessibility_app,"",R.drawable.settings_accessibility_fill0){navCtrl.navigate("Accessibility")} } - if(isDeviceOwner(myDpm)||isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm)||isProfileOwner(dpm)){ SubPageItem(R.string.permitted_ime,"",R.drawable.keyboard_fill0){navCtrl.navigate("IME")} } - if(VERSION.SDK_INT>=28&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=28&&isDeviceOwner(dpm)){ SubPageItem(R.string.keep_uninstalled_pkgs,"",R.drawable.delete_fill0){navCtrl.navigate("KeepUninstalled")} } if(VERSION.SDK_INT>=28){ @@ -268,14 +272,14 @@ private fun Home(navCtrl:NavHostController, pkgName: String, dialogStatus: Mutab @SuppressLint("NewApi") @Composable private fun UserCtrlDisabledPkg(pkgName:String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ - var pkgList = myDpm.getUserControlDisabledPackages(myComponent) + var pkgList = dpm.getUserControlDisabledPackages(receiver) var listText by remember{mutableStateOf("")} val refresh = { - pkgList = myDpm.getUserControlDisabledPackages(myComponent) + pkgList = dpm.getUserControlDisabledPackages(receiver) listText = pkgList.toText() } var inited by remember{mutableStateOf(false)} @@ -295,10 +299,10 @@ private fun UserCtrlDisabledPkg(pkgName:String){ onClick = { if(pkgName!=""){ pkgList.add(pkgName) - myDpm.setUserControlDisabledPackages(myComponent,pkgList) + dpm.setUserControlDisabledPackages(receiver,pkgList) refresh() }else{ - Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth(0.49F) @@ -309,10 +313,10 @@ private fun UserCtrlDisabledPkg(pkgName:String){ onClick = { val result = if(pkgName!=""){pkgList.remove(pkgName)}else{false} if(result){ - myDpm.setUserControlDisabledPackages(myComponent,pkgList) + dpm.setUserControlDisabledPackages(receiver,pkgList) refresh() }else{ - Toast.makeText(myContext, R.string.not_exist, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.not_exist, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth(0.96F) @@ -321,7 +325,7 @@ private fun UserCtrlDisabledPkg(pkgName:String){ } } Button( - onClick = { myDpm.setUserControlDisabledPackages(myComponent, listOf()); refresh() }, + onClick = { dpm.setUserControlDisabledPackages(receiver, listOf()); refresh() }, modifier = Modifier.fillMaxWidth() ){ Text(stringResource(R.string.clear_list)) @@ -333,12 +337,12 @@ private fun UserCtrlDisabledPkg(pkgName:String){ @SuppressLint("NewApi") @Composable private fun PermissionManage(pkgName: String, navCtrl: NavHostController){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 var inputPermission by remember{mutableStateOf(selectedPermission)} - var currentState by remember{mutableStateOf(myContext.getString(R.string.unknown))} + var currentState by remember{mutableStateOf(context.getString(R.string.unknown))} val grantState = mapOf( PERMISSION_GRANT_STATE_DEFAULT to stringResource(R.string.decide_by_user), PERMISSION_GRANT_STATE_GRANTED to stringResource(R.string.granted), @@ -351,7 +355,7 @@ private fun PermissionManage(pkgName: String, navCtrl: NavHostController){ } } LaunchedEffect(pkgName) { - if(pkgName!=""){currentState = grantState[myDpm.getPermissionGrantState(myComponent,pkgName,inputPermission)]!!} + if(pkgName!=""){currentState = grantState[dpm.getPermissionGrantState(receiver,pkgName,inputPermission)]!!} } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) @@ -362,7 +366,7 @@ private fun PermissionManage(pkgName: String, navCtrl: NavHostController){ label = { Text(stringResource(R.string.permission))}, onValueChange = { inputPermission = it; selectedPermission = inputPermission - currentState = grantState[myDpm.getPermissionGrantState(myComponent,pkgName,inputPermission)]!! + currentState = grantState[dpm.getPermissionGrantState(receiver,pkgName,inputPermission)]!! }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Ascii, imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), @@ -381,8 +385,8 @@ private fun PermissionManage(pkgName: String, navCtrl: NavHostController){ Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween){ Button( onClick = { - myDpm.setPermissionGrantState(myComponent,pkgName,inputPermission, PERMISSION_GRANT_STATE_GRANTED) - currentState = grantState[myDpm.getPermissionGrantState(myComponent,pkgName,inputPermission)]!! + dpm.setPermissionGrantState(receiver,pkgName,inputPermission, PERMISSION_GRANT_STATE_GRANTED) + currentState = grantState[dpm.getPermissionGrantState(receiver,pkgName,inputPermission)]!! }, modifier = Modifier.fillMaxWidth(0.49F) ) { @@ -390,8 +394,8 @@ private fun PermissionManage(pkgName: String, navCtrl: NavHostController){ } Button( onClick = { - myDpm.setPermissionGrantState(myComponent,pkgName,inputPermission, PERMISSION_GRANT_STATE_DENIED) - currentState = grantState[myDpm.getPermissionGrantState(myComponent,pkgName,inputPermission)]!! + dpm.setPermissionGrantState(receiver,pkgName,inputPermission, PERMISSION_GRANT_STATE_DENIED) + currentState = grantState[dpm.getPermissionGrantState(receiver,pkgName,inputPermission)]!! }, Modifier.fillMaxWidth(0.96F) ) { @@ -400,8 +404,8 @@ private fun PermissionManage(pkgName: String, navCtrl: NavHostController){ } Button( onClick = { - myDpm.setPermissionGrantState(myComponent,pkgName,inputPermission, PERMISSION_GRANT_STATE_DEFAULT) - currentState = grantState[myDpm.getPermissionGrantState(myComponent,pkgName,inputPermission)]!! + dpm.setPermissionGrantState(receiver,pkgName,inputPermission, PERMISSION_GRANT_STATE_DEFAULT) + currentState = grantState[dpm.getPermissionGrantState(receiver,pkgName,inputPermission)]!! }, modifier = Modifier.fillMaxWidth() ) { @@ -414,15 +418,15 @@ private fun PermissionManage(pkgName: String, navCtrl: NavHostController){ @SuppressLint("NewApi") @Composable private fun CrossProfilePkg(pkgName: String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.cross_profile_package), style = typography.headlineLarge) var list by remember{mutableStateOf("")} val refresh = { - crossProfilePkg = myDpm.getCrossProfilePackages(myComponent) + crossProfilePkg = dpm.getCrossProfilePackages(receiver) list = crossProfilePkg.toText() } LaunchedEffect(Unit){refresh()} @@ -435,7 +439,7 @@ private fun CrossProfilePkg(pkgName: String){ onClick = { if(pkgName!=""){ crossProfilePkg.add(pkgName)} - myDpm.setCrossProfilePackages(myComponent, crossProfilePkg) + dpm.setCrossProfilePackages(receiver, crossProfilePkg) refresh() }, modifier = Modifier.fillMaxWidth(0.49F) @@ -446,7 +450,7 @@ private fun CrossProfilePkg(pkgName: String){ onClick = { if(pkgName!=""){ crossProfilePkg.remove(pkgName)} - myDpm.setCrossProfilePackages(myComponent, crossProfilePkg) + dpm.setCrossProfilePackages(receiver, crossProfilePkg) refresh() }, modifier = Modifier.fillMaxWidth(0.96F) @@ -460,14 +464,14 @@ private fun CrossProfilePkg(pkgName: String){ @Composable private fun CrossProfileWidget(pkgName: String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ var pkgList: MutableList var list by remember{mutableStateOf("")} val refresh = { - pkgList = myDpm.getCrossProfileWidgetProviders(myComponent) + pkgList = dpm.getCrossProfileWidgetProviders(receiver) list = pkgList.toText() } LaunchedEffect(Unit){refresh()} @@ -482,7 +486,7 @@ private fun CrossProfileWidget(pkgName: String){ Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween){ Button( onClick = { - if(pkgName!=""){myDpm.addCrossProfileWidgetProvider(myComponent,pkgName)} + if(pkgName!=""){dpm.addCrossProfileWidgetProvider(receiver,pkgName)} refresh() }, modifier = Modifier.fillMaxWidth(0.49F) @@ -491,7 +495,7 @@ private fun CrossProfileWidget(pkgName: String){ } Button( onClick = { - if(pkgName!=""){myDpm.removeCrossProfileWidgetProvider(myComponent,pkgName)} + if(pkgName!=""){dpm.removeCrossProfileWidgetProvider(receiver,pkgName)} refresh() }, modifier = Modifier.fillMaxWidth(0.96F) @@ -506,14 +510,14 @@ private fun CrossProfileWidget(pkgName: String){ @SuppressLint("NewApi") @Composable private fun CredentialManagePolicy(pkgName: String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager val focusMgr = LocalFocusManager.current var policy:PackagePolicy? var policyType by remember{mutableIntStateOf(-1)} var credentialListText by remember{mutableStateOf("")} val refreshPolicy = { - policy = myDpm.credentialManagerPolicy + policy = dpm.credentialManagerPolicy policyType = policy?.policyType ?: -1 credentialList = policy?.packageNames ?: mutableSetOf() credentialList = credentialList.toMutableSet() @@ -564,13 +568,13 @@ private fun CredentialManagePolicy(pkgName: String){ focusMgr.clearFocus() try{ if(policyType!=-1&&credentialList.isNotEmpty()){ - myDpm.credentialManagerPolicy = PackagePolicy(policyType, credentialList) + dpm.credentialManagerPolicy = PackagePolicy(policyType, credentialList) }else{ - myDpm.credentialManagerPolicy = null + dpm.credentialManagerPolicy = null } - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }catch(e:java.lang.IllegalArgumentException){ - Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() }finally { refreshPolicy() credentialListText = credentialList.toText() @@ -586,16 +590,16 @@ private fun CredentialManagePolicy(pkgName: String){ @Composable private fun PermittedAccessibility(pkgName: String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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)) Text(text = stringResource(R.string.permitted_accessibility_app), style = typography.headlineLarge) var listText by remember{ mutableStateOf("") } LaunchedEffect(Unit){ - val getList = myDpm.getPermittedAccessibilityServices(myComponent) + val getList = dpm.getPermittedAccessibilityServices(receiver) if(getList!=null){ permittedAccessibility = getList } listText = permittedAccessibility.toText() } @@ -623,11 +627,11 @@ private fun PermittedAccessibility(pkgName: String){ onClick = { focusMgr.clearFocus() Toast.makeText( - myContext, - if(myDpm.setPermittedAccessibilityServices(myComponent, permittedAccessibility)){R.string.success}else{R.string.fail}, + context, + if(dpm.setPermittedAccessibilityServices(receiver, permittedAccessibility)){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT ).show() - val getList = myDpm.getPermittedAccessibilityServices(myComponent) + val getList = dpm.getPermittedAccessibilityServices(receiver) if(getList!=null){ permittedAccessibility = getList } listText = permittedAccessibility.toText() }, @@ -641,16 +645,16 @@ private fun PermittedAccessibility(pkgName: String){ @Composable private fun PermittedIME(pkgName: String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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)) Text(text = stringResource(R.string.permitted_ime), style = typography.headlineLarge) var imeListText by remember{ mutableStateOf("") } LaunchedEffect(Unit){ - val getList = myDpm.getPermittedInputMethods(myComponent) + val getList = dpm.getPermittedInputMethods(receiver) if(getList!=null){ permittedIme = getList } imeListText = permittedIme.toText() } @@ -678,11 +682,11 @@ private fun PermittedIME(pkgName: String){ onClick = { focusMgr.clearFocus() Toast.makeText( - myContext, - if(myDpm.setPermittedInputMethods(myComponent, permittedIme)){R.string.success}else{R.string.fail}, + context, + if(dpm.setPermittedInputMethods(receiver, permittedIme)){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT ).show() - val getList = myDpm.getPermittedInputMethods(myComponent) + val getList = dpm.getPermittedInputMethods(receiver) if(getList!=null){ permittedIme = getList } imeListText = permittedIme.toText() }, @@ -697,16 +701,16 @@ private fun PermittedIME(pkgName: String){ @SuppressLint("NewApi") @Composable private fun KeepUninstalledApp(pkgName: String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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)) Text(text = stringResource(R.string.keep_uninstalled_pkgs), style = typography.headlineLarge) var listText by remember{mutableStateOf("")} LaunchedEffect(Unit){ - val getList = myDpm.getKeepUninstalledPackages(myComponent) + val getList = dpm.getKeepUninstalledPackages(receiver) if(getList!=null){ keepUninstallPkg = getList } listText = keepUninstallPkg.toText() } @@ -739,11 +743,11 @@ private fun KeepUninstalledApp(pkgName: String){ Button( onClick = { focusMgr.clearFocus() - myDpm.setKeepUninstalledPackages(myComponent, keepUninstallPkg) - val getList = myDpm.getKeepUninstalledPackages(myComponent) + dpm.setKeepUninstalledPackages(receiver, keepUninstallPkg) + val getList = dpm.getKeepUninstalledPackages(receiver) if(getList!=null){ keepUninstallPkg = getList } listText = keepUninstallPkg.toText() - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ){ @@ -755,7 +759,7 @@ private fun KeepUninstalledApp(pkgName: String){ @Composable private fun UninstallApp(pkgName: String){ - val myContext = LocalContext.current + val context = LocalContext.current Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.uninstall_app), style = typography.headlineLarge) @@ -763,9 +767,9 @@ private fun UninstallApp(pkgName: String){ Column(modifier = Modifier.fillMaxWidth()){ Button( onClick = { - val intent = Intent(myContext, PackageInstallerReceiver::class.java) - val intentSender = PendingIntent.getBroadcast(myContext, 8, intent, PendingIntent.FLAG_IMMUTABLE).intentSender - val pkgInstaller = myContext.packageManager.packageInstaller + val intent = Intent(context, PackageInstallerReceiver::class.java) + val intentSender = PendingIntent.getBroadcast(context, 8, intent, PendingIntent.FLAG_IMMUTABLE).intentSender + val pkgInstaller = context.packageManager.packageInstaller pkgInstaller.uninstall(pkgName, intentSender) }, modifier = Modifier.fillMaxWidth() @@ -776,7 +780,7 @@ private fun UninstallApp(pkgName: String){ onClick = { val intent = Intent(Intent.ACTION_UNINSTALL_PACKAGE) intent.setData(Uri.parse("package:$pkgName")) - myContext.startActivity(intent) + context.startActivity(intent) }, modifier = Modifier.fillMaxWidth() ) { @@ -788,7 +792,7 @@ private fun UninstallApp(pkgName: String){ @Composable private fun InstallApp(){ - val myContext = LocalContext.current + val context = LocalContext.current val focusMgr = LocalFocusManager.current Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) @@ -812,7 +816,7 @@ private fun InstallApp(){ Spacer(Modifier.padding(vertical = 3.dp)) Column(modifier = Modifier.fillMaxWidth()){ Button( - onClick = { uriToStream(myContext, fileUri){stream -> installPackage(myContext,stream)} }, + onClick = { uriToStream(context, fileUri){stream -> installPackage(context,stream)} }, modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.silent_install)) @@ -822,7 +826,7 @@ private fun InstallApp(){ val intent = Intent(Intent.ACTION_INSTALL_PACKAGE) intent.setData(fileUri) intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - myContext.startActivity(intent) + context.startActivity(intent) }, modifier = Modifier.fillMaxWidth() ) { @@ -836,9 +840,9 @@ private fun InstallApp(){ @SuppressLint("NewApi") @Composable private fun ClearAppData(pkgName: String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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)) @@ -848,13 +852,13 @@ private fun ClearAppData(pkgName: String){ val onClear = DevicePolicyManager.OnClearApplicationUserDataListener { pkg: String, succeed: Boolean -> Looper.prepare() focusMgr.clearFocus() - val toastText = if(pkg!=""){"$pkg\n"}else{""} + myContext.getString(R.string.clear_data) + myContext.getString(if(succeed){R.string.success}else{R.string.fail}) - Toast.makeText(myContext, toastText, Toast.LENGTH_SHORT).show() + 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() } - myDpm.clearApplicationUserData(myComponent,pkgName,executor,onClear) + dpm.clearApplicationUserData(receiver, pkgName, executor, onClear) }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth().padding(horizontal = 10.dp) ) { Text(stringResource(R.string.clear_app_data)) @@ -865,20 +869,20 @@ private fun ClearAppData(pkgName: String){ @SuppressLint("NewApi") @Composable private fun DefaultDialerApp(pkgName: String){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Button( onClick = { try{ - myDpm.setDefaultDialerApplication(pkgName) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setDefaultDialerApplication(pkgName) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }catch(e:IllegalArgumentException){ - Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth().padding(horizontal = 10.dp) ) { Text(stringResource(R.string.set_default_dialer)) diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/ManagedProfile.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/ManagedProfile.kt index 1173036..3ef994a 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/ManagedProfile.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/ManagedProfile.kt @@ -80,24 +80,24 @@ fun ManagedProfile(navCtrl: NavHostController) { @Composable private fun Home(navCtrl: NavHostController){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())){ Text(text = stringResource(R.string.work_profile), style = typography.headlineLarge, modifier = Modifier.padding(top = 8.dp, bottom = 5.dp, start = 15.dp)) - if(VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent)){ + if(VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isManagedProfile(receiver)){ SubPageItem(R.string.org_owned_work_profile,"",R.drawable.corporate_fare_fill0){navCtrl.navigate("OrgOwnedWorkProfile")} } - if(VERSION.SDK_INT<24||(VERSION.SDK_INT>=24&&myDpm.isProvisioningAllowed(ACTION_PROVISION_MANAGED_PROFILE))){ + if(VERSION.SDK_INT<24||(VERSION.SDK_INT>=24&&dpm.isProvisioningAllowed(ACTION_PROVISION_MANAGED_PROFILE))){ SubPageItem(R.string.create_work_profile,"",R.drawable.work_fill0){navCtrl.navigate("CreateWorkProfile")} } - if(VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile){ + if(VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isManagedProfile(receiver)&&dpm.isOrganizationOwnedDeviceWithManagedProfile){ SubPageItem(R.string.suspend_personal_app,"",R.drawable.block_fill0){navCtrl.navigate("SuspendPersonalApp")} } - if(isProfileOwner(myDpm)&&(VERSION.SDK_INT<24||(VERSION.SDK_INT>=24&&myDpm.isManagedProfile(myComponent)))){ + if(isProfileOwner(dpm)&&(VERSION.SDK_INT<24||(VERSION.SDK_INT>=24&&dpm.isManagedProfile(receiver)))){ SubPageItem(R.string.intent_filter,"",R.drawable.filter_alt_fill0){navCtrl.navigate("IntentFilter")} } - if(VERSION.SDK_INT>=31&&(isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent))){ + if(VERSION.SDK_INT>=31&&(isProfileOwner(dpm)&&dpm.isManagedProfile(receiver))){ SubPageItem(R.string.org_id,"",R.drawable.corporate_fare_fill0){navCtrl.navigate("OrgID")} } Spacer(Modifier.padding(vertical = 30.dp)) @@ -106,8 +106,8 @@ private fun Home(navCtrl: NavHostController){ @Composable private fun CreateWorkProfile(){ - val myContext = LocalContext.current - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.create_work_profile), style = typography.headlineLarge) @@ -122,15 +122,15 @@ private fun CreateWorkProfile(){ try { val intent = Intent(ACTION_PROVISION_MANAGED_PROFILE) if(VERSION.SDK_INT>=23){ - intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,myComponent) + intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,receiver) }else{ - intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, myContext.packageName) + intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, context.packageName) } if(VERSION.SDK_INT>=24){intent.putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION,skipEncrypt)} if(VERSION.SDK_INT>=33){intent.putExtra(EXTRA_PROVISIONING_ALLOW_OFFLINE,true)} createManagedProfile.launch(intent) }catch(e:ActivityNotFoundException){ - Toast.makeText(myContext, R.string.unsupported, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.unsupported, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth() @@ -143,15 +143,15 @@ private fun CreateWorkProfile(){ @SuppressLint("NewApi") @Composable private fun OrgOwnedProfile(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.org_owned_work_profile), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) - Text(text = stringResource(R.string.is_org_owned_profile,myDpm.isOrganizationOwnedDeviceWithManagedProfile)) + Text(text = stringResource(R.string.is_org_owned_profile,dpm.isOrganizationOwnedDeviceWithManagedProfile)) Spacer(Modifier.padding(vertical = 5.dp)) - if(!myDpm.isOrganizationOwnedDeviceWithManagedProfile){ + if(!dpm.isOrganizationOwnedDeviceWithManagedProfile){ SelectionContainer { Text( text = stringResource(R.string.activate_org_profile_command, Binder.getCallingUid()/100000), @@ -166,8 +166,8 @@ private fun OrgOwnedProfile(){ @SuppressLint("NewApi") @Composable private fun OrgID(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager val focusMgr = LocalFocusManager.current Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ var orgId by remember{mutableStateOf("")} @@ -188,8 +188,8 @@ private fun OrgID(){ Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - myDpm.setOrganizationId(orgId) - Toast.makeText(myContext, R.string.success,Toast.LENGTH_SHORT).show() + dpm.setOrganizationId(orgId) + Toast.makeText(context, R.string.success,Toast.LENGTH_SHORT).show() }, enabled = orgId.length in 6..64, modifier = Modifier.fillMaxWidth() @@ -204,22 +204,22 @@ private fun OrgID(){ @SuppressLint("NewApi") @Composable private fun SuspendPersonalApp(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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)) SwitchItem( - R.string.suspend_personal_app,"",null,{myDpm.getPersonalAppsSuspendedReasons(myComponent)!=PERSONAL_APPS_NOT_SUSPENDED}, - {myDpm.setPersonalAppsSuspended(myComponent,it)} + R.string.suspend_personal_app,"",null,{dpm.getPersonalAppsSuspendedReasons(receiver)!=PERSONAL_APPS_NOT_SUSPENDED}, + {dpm.setPersonalAppsSuspended(receiver,it)} ) var time by remember{mutableStateOf("")} - time = myDpm.getManagedProfileMaximumTimeOff(myComponent).toString() + time = dpm.getManagedProfileMaximumTimeOff(receiver).toString() Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.profile_max_time_off), style = typography.titleLarge) Text(text = stringResource(R.string.profile_max_time_out_desc)) - Text(text = stringResource(R.string.personal_app_suspended_because_timeout, myDpm.getPersonalAppsSuspendedReasons(myComponent)==PERSONAL_APPS_SUSPENDED_PROFILE_TIMEOUT)) + Text(text = stringResource(R.string.personal_app_suspended_because_timeout, dpm.getPersonalAppsSuspendedReasons(receiver)==PERSONAL_APPS_SUSPENDED_PROFILE_TIMEOUT)) OutlinedTextField( value = time, onValueChange = {time=it}, modifier = Modifier.focusable().fillMaxWidth().padding(vertical = 2.dp), label = {Text(stringResource(R.string.time_unit_ms))}, @@ -229,8 +229,8 @@ private fun SuspendPersonalApp(){ Text(text = stringResource(R.string.cannot_less_than_72_hours)) Button( onClick = { - myDpm.setManagedProfileMaximumTimeOff(myComponent,time.toLong()) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setManagedProfileMaximumTimeOff(receiver,time.toLong()) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -241,9 +241,9 @@ private fun SuspendPersonalApp(){ @Composable private fun IntentFilter(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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())){ var action by remember{mutableStateOf("")} @@ -260,8 +260,8 @@ private fun IntentFilter(){ Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - myDpm.addCrossProfileIntentFilter(myComponent, IntentFilter(action), FLAG_PARENT_CAN_ACCESS_MANAGED) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.addCrossProfileIntentFilter(receiver, IntentFilter(action), FLAG_PARENT_CAN_ACCESS_MANAGED) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -269,8 +269,8 @@ private fun IntentFilter(){ } Button( onClick = { - myDpm.addCrossProfileIntentFilter(myComponent, IntentFilter(action), FLAG_MANAGED_CAN_ACCESS_PARENT) - Toast.makeText(myContext, R.string.success,Toast.LENGTH_SHORT).show() + dpm.addCrossProfileIntentFilter(receiver, IntentFilter(action), FLAG_MANAGED_CAN_ACCESS_PARENT) + Toast.makeText(context, R.string.success,Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -279,8 +279,8 @@ private fun IntentFilter(){ Spacer(Modifier.padding(vertical = 2.dp)) Button( onClick = { - myDpm.clearCrossProfileIntentFilters(myComponent) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.clearCrossProfileIntentFilters(receiver) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ){ diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/Network.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/Network.kt index bd4e6ca..7a206f2 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/Network.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/Network.kt @@ -100,13 +100,13 @@ fun Network(navCtrl: NavHostController){ @Composable private fun Home(navCtrl:NavHostController,scrollState: ScrollState){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) Column(modifier = Modifier.fillMaxSize().verticalScroll(scrollState)){ Text(text = stringResource(R.string.network), style = typography.headlineLarge, modifier = Modifier.padding(top = 8.dp, bottom = 5.dp, start = 15.dp)) - if(VERSION.SDK_INT>=24&&isDeviceOwner(myDpm)){ - val wifimac = myDpm.getWifiMacAddress(myComponent) + if(VERSION.SDK_INT>=24&&isDeviceOwner(dpm)){ + val wifimac = dpm.getWifiMacAddress(receiver) Text(text = "WiFi MAC: $wifimac", modifier = Modifier.padding(start = 15.dp)) } Spacer(Modifier.padding(vertical = 3.dp)) @@ -116,19 +116,19 @@ private fun Home(navCtrl:NavHostController,scrollState: ScrollState){ if(VERSION.SDK_INT>=33){ SubPageItem(R.string.min_wifi_security_level,"",R.drawable.wifi_password_fill0){navCtrl.navigate("MinWifiSecurityLevel")} } - if(VERSION.SDK_INT>=33&&(isDeviceOwner(myDpm)||(isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile))){ + if(VERSION.SDK_INT>=33&&(isDeviceOwner(dpm)||(isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile))){ SubPageItem(R.string.wifi_ssid_policy,"",R.drawable.wifi_fill0){navCtrl.navigate("WifiSsidPolicy")} } - if(VERSION.SDK_INT>=29&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=29&&isDeviceOwner(dpm)){ SubPageItem(R.string.private_dns,"",R.drawable.dns_fill0){navCtrl.navigate("PrivateDNS")} } - if(VERSION.SDK_INT>=26&&(isDeviceOwner(myDpm)||(isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent)))){ + if(VERSION.SDK_INT>=26&&(isDeviceOwner(dpm)||(isProfileOwner(dpm)&&dpm.isManagedProfile(receiver)))){ SubPageItem(R.string.retrieve_net_logs,"",R.drawable.description_fill0){navCtrl.navigate("NetLog")} } - if(VERSION.SDK_INT>=31&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=31&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ SubPageItem(R.string.wifi_keypair,"",R.drawable.key_fill0){navCtrl.navigate("WifiKeypair")} } - if(VERSION.SDK_INT>=28&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=28&&isDeviceOwner(dpm)){ SubPageItem(R.string.apn_settings,"",R.drawable.cell_tower_fill0){navCtrl.navigate("APN")} } Spacer(Modifier.padding(vertical = 30.dp)) @@ -137,20 +137,20 @@ private fun Home(navCtrl:NavHostController,scrollState: ScrollState){ @Composable private fun Switches(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize()){ Spacer(Modifier.padding(vertical = 5.dp)) - if(VERSION.SDK_INT>=33&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=33&&isDeviceOwner(dpm)){ SwitchItem( R.string.preferential_network_service, stringResource(R.string.developing),R.drawable.globe_fill0, - {myDpm.isPreferentialNetworkServiceEnabled},{myDpm.isPreferentialNetworkServiceEnabled = it} + {dpm.isPreferentialNetworkServiceEnabled},{dpm.isPreferentialNetworkServiceEnabled = it} ) } - if(VERSION.SDK_INT>=30&&(isDeviceOwner(myDpm)||(isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile))){ + if(VERSION.SDK_INT>=30&&(isDeviceOwner(dpm)||(isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile))){ SwitchItem(R.string.wifi_lockdown,"",R.drawable.wifi_password_fill0, - {myDpm.hasLockdownAdminConfiguredNetworks(myComponent)},{myDpm.setConfiguredNetworksLockdownState(myComponent,it)} + {dpm.hasLockdownAdminConfiguredNetworks(receiver)},{dpm.setConfiguredNetworksLockdownState(receiver,it)} ) } } @@ -159,10 +159,10 @@ private fun Switches(){ @SuppressLint("NewApi") @Composable private fun WifiSecLevel(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ - var selectedWifiSecLevel by remember{mutableIntStateOf(myDpm.minimumRequiredWifiSecurityLevel)} + var selectedWifiSecLevel by remember{mutableIntStateOf(dpm.minimumRequiredWifiSecurityLevel)} Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.min_wifi_security_level), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) @@ -172,10 +172,10 @@ private fun WifiSecLevel(){ RadioButtonItem("WPA3-192bit", {selectedWifiSecLevel==WIFI_SECURITY_ENTERPRISE_192}, {selectedWifiSecLevel= WIFI_SECURITY_ENTERPRISE_192}) Spacer(Modifier.padding(vertical = 5.dp)) Button( - enabled = isDeviceOwner(myDpm)||(isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile), + enabled = isDeviceOwner(dpm)||(isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile), onClick = { - myDpm.minimumRequiredWifiSecurityLevel=selectedWifiSecLevel - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.minimumRequiredWifiSecurityLevel=selectedWifiSecLevel + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ){ @@ -187,16 +187,16 @@ private fun WifiSecLevel(){ @SuppressLint("NewApi") @Composable private fun WifiSsidPolicy(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager val focusMgr = LocalFocusManager.current Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ - var policy = myDpm.wifiSsidPolicy + var policy = dpm.wifiSsidPolicy var selectedPolicyType by remember{mutableIntStateOf(policy?.policyType ?: -1)} var inputSsid by remember{mutableStateOf("")} var ssidList by remember{mutableStateOf("")} val refreshPolicy = { - policy = myDpm.wifiSsidPolicy + policy = dpm.wifiSsidPolicy selectedPolicyType = policy?.policyType ?: -1 ssidSet = policy?.ssids ?: mutableSetOf() } @@ -230,9 +230,9 @@ private fun WifiSsidPolicy(){ Button( onClick = { if(inputSsid==""){ - Toast.makeText(myContext, R.string.cannot_be_empty, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.cannot_be_empty, Toast.LENGTH_SHORT).show() }else if(WifiSsid.fromBytes(inputSsid.toByteArray()) in ssidSet){ - Toast.makeText(myContext, R.string.already_exist, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.already_exist, Toast.LENGTH_SHORT).show() }else{ ssidSet.add(WifiSsid.fromBytes(inputSsid.toByteArray())) ssidList = ssidSet.toText() @@ -246,13 +246,13 @@ private fun WifiSsidPolicy(){ Button( onClick = { if(inputSsid==""){ - Toast.makeText(myContext, R.string.cannot_be_empty, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.cannot_be_empty, Toast.LENGTH_SHORT).show() }else if(WifiSsid.fromBytes(inputSsid.toByteArray()) in ssidSet){ ssidSet.remove(WifiSsid.fromBytes(inputSsid.toByteArray())) inputSsid = "" ssidList = ssidSet.toText() }else{ - Toast.makeText(myContext, R.string.not_exist, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.not_exist, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth(0.96F) @@ -265,16 +265,16 @@ private fun WifiSsidPolicy(){ focusMgr.clearFocus() if(selectedPolicyType==-1){ if(policy==null&&ssidSet.isNotEmpty()){ - Toast.makeText(myContext, R.string.please_select_a_policy, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.please_select_a_policy, Toast.LENGTH_SHORT).show() }else{ - myDpm.wifiSsidPolicy = null + dpm.wifiSsidPolicy = null refreshPolicy() - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() } }else{ - myDpm.wifiSsidPolicy = if(ssidSet.size==0){ null }else{ WifiSsidPolicy(selectedPolicyType, ssidSet) } + dpm.wifiSsidPolicy = if(ssidSet.size==0){ null }else{ WifiSsidPolicy(selectedPolicyType, ssidSet) } refreshPolicy() - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth() @@ -288,9 +288,9 @@ private fun WifiSsidPolicy(){ @SuppressLint("NewApi") @Composable private fun PrivateDNS(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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)) @@ -306,16 +306,16 @@ private fun PrivateDNS(){ PRIVATE_DNS_SET_ERROR_HOST_NOT_SERVING to stringResource(R.string.host_not_serving_dns_tls), PRIVATE_DNS_SET_ERROR_FAILURE_SETTING to stringResource(R.string.fail) ) - var status by remember{mutableStateOf(dnsStatus[myDpm.getGlobalPrivateDnsMode(myComponent)])} + var status by remember{mutableStateOf(dnsStatus[dpm.getGlobalPrivateDnsMode(receiver)])} Spacer(Modifier.padding(vertical = 5.dp)) Text(text = stringResource(R.string.current_state, status?:stringResource(R.string.unknown))) - AnimatedVisibility(visible = myDpm.getGlobalPrivateDnsMode(myComponent)!=PRIVATE_DNS_MODE_OPPORTUNISTIC) { + AnimatedVisibility(visible = dpm.getGlobalPrivateDnsMode(receiver)!=PRIVATE_DNS_MODE_OPPORTUNISTIC) { Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - val result = myDpm.setGlobalPrivateDnsModeOpportunistic(myComponent) - Toast.makeText(myContext, operationResult[result], Toast.LENGTH_SHORT).show() - status = dnsStatus[myDpm.getGlobalPrivateDnsMode(myComponent)] + val result = dpm.setGlobalPrivateDnsModeOpportunistic(receiver) + Toast.makeText(context, operationResult[result], Toast.LENGTH_SHORT).show() + status = dnsStatus[dpm.getGlobalPrivateDnsMode(receiver)] }, modifier = Modifier.fillMaxWidth() ) { @@ -323,7 +323,7 @@ private fun PrivateDNS(){ } } Spacer(Modifier.padding(vertical = 10.dp)) - var inputHost by remember{mutableStateOf(myDpm.getGlobalPrivateDnsHost(myComponent) ?: "")} + var inputHost by remember{mutableStateOf(dpm.getGlobalPrivateDnsHost(receiver) ?: "")} OutlinedTextField( value = inputHost, onValueChange = {inputHost=it}, @@ -338,14 +338,14 @@ private fun PrivateDNS(){ focusMgr.clearFocus() val result: Int try{ - result = myDpm.setGlobalPrivateDnsModeSpecifiedHost(myComponent,inputHost) - Toast.makeText(myContext, operationResult[result], Toast.LENGTH_SHORT).show() + result = dpm.setGlobalPrivateDnsModeSpecifiedHost(receiver,inputHost) + Toast.makeText(context, operationResult[result], Toast.LENGTH_SHORT).show() }catch(e:IllegalArgumentException){ - Toast.makeText(myContext, R.string.invalid_hostname, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.invalid_hostname, Toast.LENGTH_SHORT).show() }catch(e:SecurityException){ - Toast.makeText(myContext, R.string.security_exception, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.security_exception, Toast.LENGTH_SHORT).show() }finally { - status = dnsStatus[myDpm.getGlobalPrivateDnsMode(myComponent)] + status = dnsStatus[dpm.getGlobalPrivateDnsMode(receiver)] } }, modifier = Modifier.fillMaxWidth() @@ -358,26 +358,26 @@ private fun PrivateDNS(){ @SuppressLint("NewApi") @Composable private fun NetLog(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.retrieve_net_logs), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) Text(text = stringResource(R.string.developing)) Spacer(Modifier.padding(vertical = 5.dp)) - SwitchItem(R.string.enable,"",null,{myDpm.isNetworkLoggingEnabled(myComponent)},{myDpm.setNetworkLoggingEnabled(myComponent,it)}) + SwitchItem(R.string.enable,"",null,{dpm.isNetworkLoggingEnabled(receiver)},{dpm.setNetworkLoggingEnabled(receiver,it)}) Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - val log = myDpm.retrieveNetworkLogs(myComponent,1234567890) + val log = dpm.retrieveNetworkLogs(receiver,1234567890) if(log!=null){ for(i in log){ Log.d("NetLog",i.toString()) } - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }else{ - Log.d("NetLog",myContext.getString(R.string.none)) - Toast.makeText(myContext, R.string.none, Toast.LENGTH_SHORT).show() + Log.d("NetLog",context.getString(R.string.none)) + Toast.makeText(context, R.string.none, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth() @@ -390,8 +390,8 @@ private fun NetLog(){ @SuppressLint("NewApi") @Composable private fun WifiKeypair(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager val focusMgr = LocalFocusManager.current Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ var keyPair by remember{mutableStateOf("")} @@ -407,14 +407,14 @@ private fun WifiKeypair(){ modifier = Modifier.focusable().fillMaxWidth() ) Spacer(Modifier.padding(vertical = 5.dp)) - val isExist = try{myDpm.isKeyPairGrantedToWifiAuth(keyPair)}catch(e:java.lang.IllegalArgumentException){false} + val isExist = try{dpm.isKeyPairGrantedToWifiAuth(keyPair)}catch(e:java.lang.IllegalArgumentException){false} Text(stringResource(R.string.already_exist)+":$isExist") Spacer(Modifier.padding(vertical = 5.dp)) Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween){ Button( onClick = { - val result = myDpm.grantKeyPairToWifiAuth(keyPair) - Toast.makeText(myContext, if(result){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + val result = dpm.grantKeyPairToWifiAuth(keyPair) + Toast.makeText(context, if(result){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth(0.49F) ) { @@ -422,8 +422,8 @@ private fun WifiKeypair(){ } Button( onClick = { - val result = myDpm.revokeKeyPairFromWifiAuth(keyPair) - Toast.makeText(myContext, if(result){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + val result = dpm.revokeKeyPairFromWifiAuth(keyPair) + Toast.makeText(context, if(result){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth(0.96F) ) { @@ -436,12 +436,12 @@ private fun WifiKeypair(){ @SuppressLint("NewApi") @Composable private fun APN(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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())){ - val setting = myDpm.getOverrideApns(myComponent) + val setting = dpm.getOverrideApns(receiver) var inputNum by remember{mutableStateOf("0")} var nextStep by remember{mutableStateOf(false)} val builder = Builder() @@ -450,7 +450,7 @@ private fun APN(){ Spacer(Modifier.padding(vertical = 5.dp)) Text(text = stringResource(id = R.string.developing)) Spacer(Modifier.padding(vertical = 5.dp)) - SwitchItem(R.string.enable,"",null,{myDpm.isOverrideApnEnabled(myComponent)},{myDpm.setOverrideApnsEnabled(myComponent,it)}) + SwitchItem(R.string.enable,"",null,{dpm.isOverrideApnEnabled(receiver)},{dpm.setOverrideApnsEnabled(receiver,it)}) Text(text = stringResource(R.string.total_apn_amount, setting.size)) if(setting.size>0){ Text(text = stringResource(R.string.select_a_apn_or_create, setting.size)) @@ -569,7 +569,7 @@ private fun APN(){ RadioButtonItem("PAP/CHAP",{selectedAuthType==AUTH_TYPE_PAP_OR_CHAP},{selectedAuthType=AUTH_TYPE_PAP_OR_CHAP}) if(VERSION.SDK_INT>=29){ - val ts = myContext.getSystemService(ComponentActivity.TELEPHONY_SERVICE) as TelephonyManager + val ts = context.getSystemService(ComponentActivity.TELEPHONY_SERVICE) as TelephonyManager carrierId = ts.simCarrierId.toString() Text(text = "CarrierID", style = typography.titleLarge) TextField( @@ -772,7 +772,7 @@ private fun APN(){ AnimatedVisibility(finalStep) { if(inputNum=="0"){ Button( - onClick = {myDpm.addOverrideApn(myComponent,result)}, + onClick = {dpm.addOverrideApn(receiver,result)}, modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.create)) @@ -781,8 +781,8 @@ private fun APN(){ Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween){ Button( onClick = { - val success = myDpm.updateOverrideApn(myComponent,id,result) - Toast.makeText(myContext, if(success){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + val success = dpm.updateOverrideApn(receiver,id,result) + Toast.makeText(context, if(success){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() }, Modifier.fillMaxWidth(0.49F) ){ @@ -790,8 +790,8 @@ private fun APN(){ } Button( onClick = { - val success = myDpm.removeOverrideApn(myComponent,id) - Toast.makeText(myContext, if(success){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + val success = dpm.removeOverrideApn(receiver,id) + Toast.makeText(context, if(success){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() }, Modifier.fillMaxWidth(0.96F) ){ diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/Password.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/Password.kt index dbb879a..fe45c64 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/Password.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/Password.kt @@ -118,9 +118,9 @@ private fun Home(navCtrl:NavHostController,scrollState: ScrollState){ @Composable private fun PasswordInfo(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.password_info), style = typography.headlineLarge) @@ -132,18 +132,18 @@ private fun PasswordInfo(){ PASSWORD_COMPLEXITY_MEDIUM to stringResource(R.string.password_complexity_medium), PASSWORD_COMPLEXITY_HIGH to stringResource(R.string.password_complexity_high) ) - val pwdComplex = passwordComplexity[myDpm.passwordComplexity] + val pwdComplex = passwordComplexity[dpm.passwordComplexity] Text(text = stringResource(R.string.current_password_complexity_is, pwdComplex?:stringResource(R.string.unknown))) } - if(isDeviceOwner(myDpm)|| isProfileOwner(myDpm)){ - Text(stringResource(R.string.is_password_sufficient, myDpm.isActivePasswordSufficient)) + if(isDeviceOwner(dpm)|| isProfileOwner(dpm)){ + Text(stringResource(R.string.is_password_sufficient, dpm.isActivePasswordSufficient)) } - if(myDpm.isAdminActive(myComponent)){ - val pwdFailedAttempts = myDpm.currentFailedPasswordAttempts + if(dpm.isAdminActive(receiver)){ + val pwdFailedAttempts = dpm.currentFailedPasswordAttempts Text(text = stringResource(R.string.password_failed_attempts_is, pwdFailedAttempts)) } - if(VERSION.SDK_INT>=28&&isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent)){ - val unifiedPwd = myDpm.isUsingUnifiedPassword(myComponent) + if(VERSION.SDK_INT>=28&&isProfileOwner(dpm)&&dpm.isManagedProfile(receiver)){ + val unifiedPwd = dpm.isUsingUnifiedPassword(receiver) Text(stringResource(R.string.is_using_unified_password, unifiedPwd)) } } @@ -152,49 +152,49 @@ private fun PasswordInfo(){ @SuppressLint("NewApi") @Composable private fun ResetPasswordToken(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) - val myByteArray by remember{ mutableStateOf(byteArrayOf(1,1,4,5,1,4,1,9,1,9,8,1,0,1,1,4,5,1,4,1,9,1,9,8,1,0,1,1,4,5,1,4,1,9,1,9,8,1,0)) } + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) + val tokenByteArray by remember{ mutableStateOf(byteArrayOf(1,1,4,5,1,4,1,9,1,9,8,1,0,1,1,4,5,1,4,1,9,1,9,8,1,0,1,1,4,5,1,4,1,9,1,9,8,1,0)) } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.reset_password_token), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - if(myDpm.clearResetPasswordToken(myComponent)){ Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() - }else{ Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() } + if(dpm.clearResetPasswordToken(receiver)){ Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() + }else{ Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth(), - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm) + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm) ) { Text(stringResource(R.string.clear)) } Button( onClick = { try { - if(myDpm.setResetPasswordToken(myComponent, myByteArray)){ - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + if(dpm.setResetPasswordToken(receiver, tokenByteArray)){ + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }else{ - Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } }catch(e:SecurityException){ - Toast.makeText(myContext, R.string.security_exception, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.security_exception, Toast.LENGTH_SHORT).show() } }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.set)) } Button( onClick = { - if(!myDpm.isResetPasswordTokenActive(myComponent)){ - try{ activateToken(myContext) } - catch(e:NullPointerException){ Toast.makeText(myContext, R.string.please_set_a_token, Toast.LENGTH_SHORT).show() } - }else{ Toast.makeText(myContext, R.string.token_already_activated, Toast.LENGTH_SHORT).show() } + if(!dpm.isResetPasswordTokenActive(receiver)){ + try{ activateToken(context) } + catch(e:NullPointerException){ Toast.makeText(context, R.string.please_set_a_token, Toast.LENGTH_SHORT).show() } + }else{ Toast.makeText(context, R.string.token_already_activated, Toast.LENGTH_SHORT).show() } }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.activate)) @@ -206,12 +206,12 @@ private fun ResetPasswordToken(){ @Composable private fun ResetPassword(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 var newPwd by remember{ mutableStateOf("") } - val myByteArray by remember{ mutableStateOf(byteArrayOf(1,1,4,5,1,4,1,9,1,9,8,1,0,1,1,4,5,1,4,1,9,1,9,8,1,0,1,1,4,5,1,4,1,9,1,9,8,1,0)) } + val tokenByteArray by remember{ mutableStateOf(byteArrayOf(1,1,4,5,1,4,1,9,1,9,8,1,0,1,1,4,5,1,4,1,9,1,9,8,1,0,1,1,4,5,1,4,1,9,1,9,8,1,0)) } var confirmed by remember{ mutableStateOf(false) } var resetPwdFlag by remember{ mutableIntStateOf(0) } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ @@ -221,7 +221,7 @@ private fun ResetPassword(){ OutlinedTextField( value = newPwd, onValueChange = {newPwd=it}, - enabled = !confirmed&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm)||myDpm.isAdminActive(myComponent)), + enabled = !confirmed&&(isDeviceOwner(dpm)||isProfileOwner(dpm)||dpm.isAdminActive(receiver)), label = { Text(stringResource(R.string.password))}, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), @@ -242,9 +242,9 @@ private fun ResetPassword(){ Button( onClick = { if(newPwd.length>=4||newPwd.isEmpty()){ confirmed=!confirmed - }else{ Toast.makeText(myContext, R.string.require_4_digit_password, Toast.LENGTH_SHORT).show() } + }else{ Toast.makeText(context, R.string.require_4_digit_password, Toast.LENGTH_SHORT).show() } }, - enabled = isDeviceOwner(myDpm) || isProfileOwner(myDpm) || myDpm.isAdminActive(myComponent), + enabled = isDeviceOwner(dpm) || isProfileOwner(dpm) || dpm.isAdminActive(receiver), modifier = Modifier.fillMaxWidth(), colors = ButtonDefaults.buttonColors( containerColor = if(confirmed){ colorScheme.primary }else{ colorScheme.error }, @@ -257,13 +257,13 @@ private fun ResetPassword(){ if(VERSION.SDK_INT>=26){ Button( onClick = { - val resetSuccess = myDpm.resetPasswordWithToken(myComponent,newPwd,myByteArray,resetPwdFlag) - if(resetSuccess){ Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show();newPwd=""} - else{ Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() } + val resetSuccess = dpm.resetPasswordWithToken(receiver,newPwd,tokenByteArray,resetPwdFlag) + if(resetSuccess){ Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show();newPwd=""} + else{ Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } confirmed=false }, colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError), - enabled = confirmed&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm)), + enabled = confirmed&&(isDeviceOwner(dpm)||isProfileOwner(dpm)), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.reset_password_with_token)) @@ -271,9 +271,9 @@ private fun ResetPassword(){ } Button( onClick = { - val resetSuccess = myDpm.resetPassword(newPwd,resetPwdFlag) - if(resetSuccess){ Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show(); newPwd=""} - else{ Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() } + val resetSuccess = dpm.resetPassword(newPwd,resetPwdFlag) + if(resetSuccess){ Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show(); newPwd=""} + else{ Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } confirmed=false }, enabled = confirmed, @@ -289,8 +289,8 @@ private fun ResetPassword(){ @SuppressLint("NewApi") @Composable private fun PasswordComplexity(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager val passwordComplexity = mapOf( PASSWORD_COMPLEXITY_NONE to stringResource(R.string.password_complexity_none), PASSWORD_COMPLEXITY_LOW to stringResource(R.string.password_complexity_low), @@ -298,8 +298,8 @@ private fun PasswordComplexity(){ PASSWORD_COMPLEXITY_HIGH to stringResource(R.string.password_complexity_high) ).toList() var selectedItem by remember{ mutableIntStateOf(passwordComplexity[0].first) } - if(isDeviceOwner(myDpm) || isProfileOwner(myDpm)){ - selectedItem=myDpm.requiredPasswordComplexity + if(isDeviceOwner(dpm) || isProfileOwner(dpm)){ + selectedItem=dpm.requiredPasswordComplexity } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) @@ -313,17 +313,17 @@ private fun PasswordComplexity(){ Button( onClick = { - myDpm.requiredPasswordComplexity = selectedItem - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.requiredPasswordComplexity = selectedItem + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm)|| isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)|| isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(text = stringResource(R.string.apply)) } Spacer(Modifier.padding(vertical = 5.dp)) Button( - onClick = {myContext.startActivity(Intent(ACTION_SET_NEW_PASSWORD))}, + onClick = {context.startActivity(Intent(ACTION_SET_NEW_PASSWORD))}, modifier = Modifier.fillMaxWidth() ){ Text(stringResource(R.string.require_set_new_password)) @@ -334,11 +334,11 @@ private fun PasswordComplexity(){ @Composable private fun ScreenTimeout(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 - var inputContent by remember{ mutableStateOf(if(isDeviceOwner(myDpm)){myDpm.getMaximumTimeToLock(myComponent).toString()}else{""}) } + var inputContent by remember{ mutableStateOf(if(isDeviceOwner(dpm)){dpm.getMaximumTimeToLock(receiver).toString()}else{""}) } var ableToApply by remember{ mutableStateOf(inputContent!="") } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) @@ -355,12 +355,12 @@ private fun ScreenTimeout(){ }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.focusable().fillMaxWidth() ) Spacer(Modifier.padding(vertical = 5.dp)) Button( - onClick = {focusMgr.clearFocus() ; myDpm.setMaximumTimeToLock(myComponent,inputContent.toLong())}, + onClick = {focusMgr.clearFocus() ; dpm.setMaximumTimeToLock(receiver,inputContent.toLong())}, modifier = Modifier.fillMaxWidth() ){ Text(stringResource(R.string.apply)) @@ -370,11 +370,11 @@ private fun ScreenTimeout(){ @Composable private fun MaxFailedPasswordForWipe(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 - var inputContent by remember{ mutableStateOf(if(isDeviceOwner(myDpm)){myDpm.getMaximumFailedPasswordsForWipe(myComponent).toString()}else{""}) } + var inputContent by remember{ mutableStateOf(if(isDeviceOwner(dpm)){dpm.getMaximumFailedPasswordsForWipe(receiver).toString()}else{""}) } var ableToApply by remember{ mutableStateOf(inputContent!=""&&inputContent!="0") } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) @@ -391,12 +391,12 @@ private fun MaxFailedPasswordForWipe(){ }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.focusable().fillMaxWidth() ) Spacer(Modifier.padding(vertical = 5.dp)) Button( - onClick = {focusMgr.clearFocus() ; myDpm.setMaximumFailedPasswordsForWipe(myComponent,inputContent.toInt())}, + onClick = {focusMgr.clearFocus() ; dpm.setMaximumFailedPasswordsForWipe(receiver,inputContent.toInt())}, modifier = Modifier.fillMaxWidth() ){ Text(stringResource(R.string.apply)) @@ -406,11 +406,11 @@ private fun MaxFailedPasswordForWipe(){ @Composable private fun PasswordExpiration(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 - var inputContent by remember{ mutableStateOf(if(isDeviceOwner(myDpm)){myDpm.getPasswordExpirationTimeout(myComponent).toString()}else{""}) } + var inputContent by remember{ mutableStateOf(if(isDeviceOwner(dpm)){dpm.getPasswordExpirationTimeout(receiver).toString()}else{""}) } var ableToApply by remember{ mutableStateOf(inputContent!="") } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) @@ -427,12 +427,12 @@ private fun PasswordExpiration(){ }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.focusable().fillMaxWidth() ) Spacer(Modifier.padding(vertical = 5.dp)) Button( - onClick = {focusMgr.clearFocus() ; myDpm.setPasswordExpirationTimeout(myComponent,inputContent.toLong())}, + onClick = {focusMgr.clearFocus() ; dpm.setPasswordExpirationTimeout(receiver,inputContent.toLong())}, modifier = Modifier.fillMaxWidth() ){ Text(stringResource(R.string.apply)) @@ -442,11 +442,11 @@ private fun PasswordExpiration(){ @Composable private fun PasswordHistoryLength(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 - var inputContent by remember{ mutableStateOf(if(isDeviceOwner(myDpm)){myDpm.getPasswordHistoryLength(myComponent).toString()}else{""}) } + var inputContent by remember{ mutableStateOf(if(isDeviceOwner(dpm)){dpm.getPasswordHistoryLength(receiver).toString()}else{""}) } var ableToApply by remember{ mutableStateOf(inputContent!="") } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) @@ -463,12 +463,12 @@ private fun PasswordHistoryLength(){ }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.focusable().fillMaxWidth() ) Spacer(Modifier.padding(vertical = 5.dp)) Button( - onClick = {focusMgr.clearFocus() ; myDpm.setPasswordHistoryLength(myComponent,inputContent.toInt())}, + onClick = {focusMgr.clearFocus() ; dpm.setPasswordHistoryLength(receiver,inputContent.toInt())}, modifier = Modifier.fillMaxWidth() ){ Text(stringResource(R.string.apply)) @@ -478,9 +478,9 @@ private fun PasswordHistoryLength(){ @Composable private fun KeyguardDisabledFeatures(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) var state by remember{mutableIntStateOf(-1)} var shortcuts by remember{mutableStateOf(false)} var biometrics by remember{mutableStateOf(false)} @@ -494,7 +494,7 @@ private fun KeyguardDisabledFeatures(){ var camera by remember{mutableStateOf(false)} var widgets by remember{mutableStateOf(false)} val calculateCustomFeature = { - var calculate = myDpm.getKeyguardDisabledFeatures(myComponent) + var calculate = dpm.getKeyguardDisabledFeatures(receiver) if(calculate==0){state=0} else{ if(calculate-KEYGUARD_DISABLE_SHORTCUTS_ALL>=0 && VERSION.SDK_INT>=34){shortcuts=true;calculate-= KEYGUARD_DISABLE_SHORTCUTS_ALL } @@ -511,7 +511,7 @@ private fun KeyguardDisabledFeatures(){ } } if(state==-1){ - state = when(myDpm.getKeyguardDisabledFeatures(myComponent)){ + state = when(dpm.getKeyguardDisabledFeatures(receiver)){ KEYGUARD_DISABLE_FEATURES_NONE->0 KEYGUARD_DISABLE_FEATURES_ALL->1 else->2 @@ -561,11 +561,11 @@ private fun KeyguardDisabledFeatures(){ if(camera){result+=KEYGUARD_DISABLE_SECURE_CAMERA} if(widgets){result+=KEYGUARD_DISABLE_WIDGETS_ALL} } - myDpm.setKeyguardDisabledFeatures(myComponent,result) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setKeyguardDisabledFeatures(receiver,result) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() calculateCustomFeature() }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(text = stringResource(R.string.apply)) @@ -576,9 +576,9 @@ private fun KeyguardDisabledFeatures(){ @Composable private fun PasswordQuality(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) val passwordQuality = mapOf( PASSWORD_QUALITY_UNSPECIFIED to stringResource(R.string.password_quality_unspecified), PASSWORD_QUALITY_SOMETHING to stringResource(R.string.password_quality_something), @@ -590,7 +590,7 @@ private fun PasswordQuality(){ PASSWORD_QUALITY_COMPLEX to stringResource(R.string.custom)+"(${stringResource(R.string.unsupported)})", ).toList() var selectedItem by remember{ mutableIntStateOf(passwordQuality[0].first) } - if(isDeviceOwner(myDpm) || isProfileOwner(myDpm)){ selectedItem=myDpm.getPasswordQuality(myComponent) } + if(isDeviceOwner(dpm) || isProfileOwner(dpm)){ selectedItem=dpm.getPasswordQuality(receiver) } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.required_password_quality), style = typography.headlineLarge) @@ -609,10 +609,10 @@ private fun PasswordQuality(){ Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - myDpm.setPasswordQuality(myComponent,selectedItem) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setPasswordQuality(receiver,selectedItem) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm) || isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm) || isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.apply)) @@ -621,13 +621,13 @@ private fun PasswordQuality(){ } } -private fun activateToken(myContext: Context){ - val desc = myContext.getString(R.string.activate_reset_password_token_here) - val keyguardManager = myContext.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager - val confirmIntent = keyguardManager.createConfirmDeviceCredentialIntent(myContext.getString(R.string.app_name), desc) +private fun activateToken(context: Context){ + val desc = context.getString(R.string.activate_reset_password_token_here) + val keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager + val confirmIntent = keyguardManager.createConfirmDeviceCredentialIntent(context.getString(R.string.app_name), desc) if (confirmIntent != null) { - startActivity(myContext,confirmIntent, null) + startActivity(context,confirmIntent, null) } else { - Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } } diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/Permissions.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/Permissions.kt index 2b00ba9..ba859d4 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/Permissions.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/Permissions.kt @@ -101,45 +101,45 @@ fun DpmPermissions(navCtrl:NavHostController){ @Composable private fun Home(localNavCtrl:NavHostController,listScrollState:ScrollState){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) Column(modifier = Modifier.fillMaxSize().verticalScroll(listScrollState)) { Text(text = stringResource(R.string.permission), style = typography.headlineLarge, modifier = Modifier.padding(top = 8.dp, bottom = 5.dp, start = 15.dp)) SubPageItem( - R.string.device_admin, stringResource(if(myDpm.isAdminActive(myComponent)){R.string.activated}else{R.string.deactivated}), + R.string.device_admin, stringResource(if(dpm.isAdminActive(receiver)){R.string.activated}else{R.string.deactivated}), operation = {localNavCtrl.navigate("DeviceAdmin")} ) - if(!isDeviceOwner(myDpm)){ + if(!isDeviceOwner(dpm)){ SubPageItem( - R.string.profile_owner, stringResource(if(isProfileOwner(myDpm)){R.string.activated}else{R.string.deactivated}), + R.string.profile_owner, stringResource(if(isProfileOwner(dpm)){R.string.activated}else{R.string.deactivated}), operation = {localNavCtrl.navigate("ProfileOwner")} ) } - if(!isProfileOwner(myDpm)){ + if(!isProfileOwner(dpm)){ SubPageItem( - R.string.device_owner, stringResource(if(isDeviceOwner(myDpm)){R.string.activated}else{R.string.deactivated}), + R.string.device_owner, stringResource(if(isDeviceOwner(dpm)){R.string.activated}else{R.string.deactivated}), operation = {localNavCtrl.navigate("DeviceOwner")} ) } SubPageItem(R.string.shizuku,""){localNavCtrl.navigate("Shizuku")} SubPageItem(R.string.device_info,"",R.drawable.perm_device_information_fill0){localNavCtrl.navigate("DeviceInfo")} - if(VERSION.SDK_INT>=31&&(isProfileOwner(myDpm)|| isDeviceOwner(myDpm))){ + if(VERSION.SDK_INT>=31&&(isProfileOwner(dpm)|| isDeviceOwner(dpm))){ SubPageItem(R.string.enrollment_specific_id,"",R.drawable.id_card_fill0){localNavCtrl.navigate("SpecificID")} } - if((VERSION.SDK_INT>=26&&isDeviceOwner(myDpm))||(VERSION.SDK_INT>=24&&isProfileOwner(myDpm))){ + if((VERSION.SDK_INT>=26&&isDeviceOwner(dpm))||(VERSION.SDK_INT>=24&&isProfileOwner(dpm))){ SubPageItem(R.string.org_name,"",R.drawable.corporate_fare_fill0){localNavCtrl.navigate("OrgName")} } - if(isDeviceOwner(myDpm) || isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm) || isProfileOwner(dpm)){ SubPageItem(R.string.account_types_management_disabled,"",R.drawable.account_circle_fill0){localNavCtrl.navigate("NoManagementAccount")} } - if(VERSION.SDK_INT>=24&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=24&&isDeviceOwner(dpm)){ SubPageItem(R.string.device_owner_lock_screen_info,"",R.drawable.screen_lock_portrait_fill0){localNavCtrl.navigate("LockScreenInfo")} } - if(VERSION.SDK_INT>=24&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=24&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ SubPageItem(R.string.support_msg,"",R.drawable.chat_fill0){localNavCtrl.navigate("SupportMsg")} } - if(VERSION.SDK_INT>=28&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=28&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ SubPageItem(R.string.transform_ownership,"",R.drawable.admin_panel_settings_fill0){localNavCtrl.navigate("TransformOwnership")} } Spacer(Modifier.padding(vertical = 30.dp)) @@ -149,11 +149,11 @@ private fun Home(localNavCtrl:NavHostController,listScrollState:ScrollState){ @SuppressLint("NewApi") @Composable private fun LockScreenInfo(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 - var infoText by remember{mutableStateOf(myDpm.deviceOwnerLockScreenInfo?.toString() ?: "")} + var infoText by remember{mutableStateOf(dpm.deviceOwnerLockScreenInfo?.toString() ?: "")} Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ Text(text = stringResource(R.string.device_owner_lock_screen_info), style = typography.headlineLarge) OutlinedTextField( @@ -165,8 +165,8 @@ private fun LockScreenInfo(){ Button( onClick = { focusMgr.clearFocus() - myDpm.setDeviceOwnerLockScreenInfo(myComponent,infoText) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setDeviceOwnerLockScreenInfo(receiver,infoText) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -175,8 +175,8 @@ private fun LockScreenInfo(){ Button( onClick = { focusMgr.clearFocus() - myDpm.setDeviceOwnerLockScreenInfo(myComponent,null) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setDeviceOwnerLockScreenInfo(receiver,null) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -187,23 +187,23 @@ private fun LockScreenInfo(){ @Composable private fun DeviceAdmin(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) val co = rememberCoroutineScope() - var showDeactivateButton by remember{mutableStateOf(myDpm.isAdminActive(myComponent))} + var showDeactivateButton by remember{mutableStateOf(dpm.isAdminActive(receiver))} Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.device_admin), style = typography.headlineLarge) - Text(text = stringResource(if(myDpm.isAdminActive(myComponent)) { R.string.activated } else { R.string.deactivated }), style = typography.titleLarge) + Text(text = stringResource(if(dpm.isAdminActive(receiver)) { R.string.activated } else { R.string.deactivated }), style = typography.titleLarge) Spacer(Modifier.padding(vertical = 5.dp)) AnimatedVisibility(showDeactivateButton) { Button( onClick = { - myDpm.removeActiveAdmin(myComponent) - co.launch{ delay(400); showDeactivateButton=myDpm.isAdminActive(myComponent) } + dpm.removeActiveAdmin(receiver) + co.launch{ delay(400); showDeactivateButton=dpm.isAdminActive(receiver) } }, - enabled = !isProfileOwner(myDpm)&&!isDeviceOwner(myDpm), + enabled = !isProfileOwner(dpm)&&!isDeviceOwner(dpm), colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError) ) { Text(stringResource(R.string.deactivate)) @@ -211,7 +211,7 @@ private fun DeviceAdmin(){ } AnimatedVisibility(!showDeactivateButton) { Column { - Button(onClick = {activateDeviceAdmin(myContext, myComponent)}, modifier = Modifier.fillMaxWidth()) { + Button(onClick = {activateDeviceAdmin(context, receiver)}, modifier = Modifier.fillMaxWidth()) { Text(stringResource(R.string.activate_jump)) } Spacer(Modifier.padding(vertical = 5.dp)) @@ -226,24 +226,24 @@ private fun DeviceAdmin(){ @Composable private fun ProfileOwner(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) - var showDeactivateButton by remember{mutableStateOf(isProfileOwner(myDpm))} + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) + var showDeactivateButton by remember{mutableStateOf(isProfileOwner(dpm))} Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.profile_owner), style = typography.headlineLarge) - Text(stringResource(if(isProfileOwner(myDpm)){R.string.activated}else{R.string.deactivated}), style = typography.titleLarge) + Text(stringResource(if(isProfileOwner(dpm)){R.string.activated}else{R.string.deactivated}), style = typography.titleLarge) Spacer(Modifier.padding(vertical = 5.dp)) if(VERSION.SDK_INT>=24){ AnimatedVisibility(showDeactivateButton) { val co = rememberCoroutineScope() Button( onClick = { - myDpm.clearProfileOwner(myComponent) - co.launch { delay(400); showDeactivateButton=isProfileOwner(myDpm) } + dpm.clearProfileOwner(receiver) + co.launch { delay(400); showDeactivateButton=isProfileOwner(dpm) } }, - enabled = !myDpm.isManagedProfile(myComponent), + enabled = !dpm.isManagedProfile(receiver), colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError) ) { Text(stringResource(R.string.deactivate)) @@ -263,20 +263,20 @@ private fun ProfileOwner(){ @Composable private fun DeviceOwner(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager val co = rememberCoroutineScope() - var showDeactivateButton by remember{mutableStateOf(isDeviceOwner(myDpm))} + var showDeactivateButton by remember{mutableStateOf(isDeviceOwner(dpm))} Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.device_owner), style = typography.headlineLarge) - Text(text = stringResource(if(isDeviceOwner(myDpm)){R.string.activated}else{R.string.deactivated}), style = typography.titleLarge) + Text(text = stringResource(if(isDeviceOwner(dpm)){R.string.activated}else{R.string.deactivated}), style = typography.titleLarge) Spacer(Modifier.padding(vertical = 5.dp)) AnimatedVisibility(showDeactivateButton) { Button( onClick = { - myDpm.clearDeviceOwnerApp(myContext.packageName) - co.launch{ delay(400); showDeactivateButton=isDeviceOwner(myDpm) } + dpm.clearDeviceOwnerApp(context.packageName) + co.launch{ delay(400); showDeactivateButton=isDeviceOwner(dpm) } }, colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError) ) { @@ -296,19 +296,19 @@ private fun DeviceOwner(){ @Composable fun DeviceInfo(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.device_info), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) - if(VERSION.SDK_INT>=34&&(isDeviceOwner(myDpm)||(isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile))){ - val financed = myDpm.isDeviceFinanced + if(VERSION.SDK_INT>=34&&(isDeviceOwner(dpm)||(isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile))){ + val financed = dpm.isDeviceFinanced Text(stringResource(R.string.is_device_financed, financed)) } Spacer(Modifier.padding(vertical = 2.dp)) if(VERSION.SDK_INT>=33){ - val dpmRole = myDpm.devicePolicyManagementRoleHolderPackage + val dpmRole = dpm.devicePolicyManagementRoleHolderPackage Text(stringResource(R.string.dpmrh, if(dpmRole==null) { stringResource(R.string.none) } else { "" })) if(dpmRole!=null){ SelectionContainer(modifier = Modifier.horizontalScroll(rememberScrollState())){ @@ -324,9 +324,9 @@ fun DeviceInfo(){ ) if(VERSION.SDK_INT>=23){ encryptionStatus[DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_DEFAULT_KEY] = stringResource(R.string.es_active_default_key) } if(VERSION.SDK_INT>=24){ encryptionStatus[DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER] = stringResource(R.string.es_active_per_user) } - Text(stringResource(R.string.encrypt_status_is)+encryptionStatus[myDpm.storageEncryptionStatus]) + Text(stringResource(R.string.encrypt_status_is)+encryptionStatus[dpm.storageEncryptionStatus]) Spacer(Modifier.padding(vertical = 2.dp)) - val adminList = myDpm.activeAdmins + val adminList = dpm.activeAdmins if(adminList!=null){ var adminListText = "" Text(text = stringResource(R.string.activated_device_admin, adminList.size)) @@ -346,10 +346,10 @@ fun DeviceInfo(){ @SuppressLint("NewApi") @Composable private fun SpecificID(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ - val specificId = myDpm.enrollmentSpecificId + val specificId = dpm.enrollmentSpecificId Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.enrollment_specific_id), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) @@ -365,12 +365,12 @@ private fun SpecificID(){ @SuppressLint("NewApi") @Composable private fun OrgName(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ - var orgName by remember{mutableStateOf(try{myDpm.getOrganizationName(myComponent).toString()}catch(e:SecurityException){""})} + var orgName by remember{mutableStateOf(try{dpm.getOrganizationName(receiver).toString()}catch(e:SecurityException){""})} Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.org_name), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) @@ -384,8 +384,8 @@ private fun OrgName(){ Button( onClick = { focusMgr.clearFocus() - myDpm.setOrganizationName(myComponent,orgName) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setOrganizationName(receiver,orgName) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ){ @@ -397,12 +397,12 @@ private fun OrgName(){ @SuppressLint("NewApi") @Composable private fun SupportMsg(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 - var shortMsg by remember{mutableStateOf(myDpm.getShortSupportMessage(myComponent)?.toString() ?: "")} - var longMsg by remember{mutableStateOf(myDpm.getLongSupportMessage(myComponent)?.toString() ?: "")} + var shortMsg by remember{mutableStateOf(dpm.getShortSupportMessage(receiver)?.toString() ?: "")} + var longMsg by remember{mutableStateOf(dpm.getLongSupportMessage(receiver)?.toString() ?: "")} Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.support_msg), style = typography.headlineLarge) @@ -424,11 +424,11 @@ private fun SupportMsg(){ Button( onClick = { focusMgr.clearFocus() - myDpm.setShortSupportMessage(myComponent, shortMsg) - myDpm.setLongSupportMessage(myComponent, longMsg) - shortMsg = myDpm.getShortSupportMessage(myComponent)?.toString() ?: "" - longMsg = myDpm.getLongSupportMessage(myComponent)?.toString() ?: "" - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setShortSupportMessage(receiver, shortMsg) + dpm.setLongSupportMessage(receiver, longMsg) + shortMsg = dpm.getShortSupportMessage(receiver)?.toString() ?: "" + longMsg = dpm.getLongSupportMessage(receiver)?.toString() ?: "" + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -438,11 +438,11 @@ private fun SupportMsg(){ Button( onClick = { focusMgr.clearFocus() - myDpm.setShortSupportMessage(myComponent, null) - myDpm.setLongSupportMessage(myComponent, null) - shortMsg = myDpm.getShortSupportMessage(myComponent)?.toString() ?: "" - longMsg = myDpm.getLongSupportMessage(myComponent)?.toString() ?: "" - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setShortSupportMessage(receiver, null) + dpm.setLongSupportMessage(receiver, null) + shortMsg = dpm.getShortSupportMessage(receiver)?.toString() ?: "" + longMsg = dpm.getLongSupportMessage(receiver)?.toString() ?: "" + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -456,9 +456,9 @@ private fun SupportMsg(){ @Composable private fun NoManageAccount(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ Spacer(Modifier.padding(vertical = 10.dp)) @@ -466,7 +466,7 @@ private fun NoManageAccount(){ Text(stringResource(R.string.unknown_effect)) var accountList by remember{ mutableStateOf("") } val refreshList = { - val noManageAccount = myDpm.accountTypesWithManagementDisabled + val noManageAccount = dpm.accountTypesWithManagementDisabled accountList = "" if (noManageAccount != null) { var count = noManageAccount.size @@ -488,7 +488,7 @@ private fun NoManageAccount(){ ) Button( onClick={ - myDpm.setAccountManagementDisabled(myComponent,inputText,true) + dpm.setAccountManagementDisabled(receiver,inputText,true) refreshList() }, modifier = Modifier.fillMaxWidth() @@ -497,7 +497,7 @@ private fun NoManageAccount(){ } Button( onClick={ - myDpm.setAccountManagementDisabled(myComponent,inputText,false) + dpm.setAccountManagementDisabled(receiver,inputText,false) refreshList() }, modifier = Modifier.fillMaxWidth() @@ -510,9 +510,9 @@ private fun NoManageAccount(){ @SuppressLint("NewApi") @Composable private fun TransformOwnership(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 val focusRequester = FocusRequester() Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 8.dp)){ @@ -540,10 +540,10 @@ private fun TransformOwnership(){ Button( onClick = { try { - myDpm.transferOwnership(myComponent,ComponentName(pkg, cls),null) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.transferOwnership(receiver,ComponentName(pkg, cls),null) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }catch(e:IllegalArgumentException){ - Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth() diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/ShizukuActivate.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/ShizukuActivate.kt index 3c501fb..e05adf2 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/ShizukuActivate.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/ShizukuActivate.kt @@ -40,20 +40,20 @@ private var waitGrantPermission = false @Composable fun ShizukuActivate(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) val coScope = rememberCoroutineScope() val outputTextScrollState = rememberScrollState() var enabled by remember{ mutableStateOf(false) } var bindShizuku by remember{ mutableStateOf(false) } var outputText by remember{mutableStateOf("")} - var showDeviceAdminButton by remember{mutableStateOf(!myDpm.isAdminActive(myComponent))} - var showProfileOwnerButton by remember{mutableStateOf(!isProfileOwner(myDpm))} - var showDeviceOwnerButton by remember{mutableStateOf(!isDeviceOwner(myDpm))} + var showDeviceAdminButton by remember{mutableStateOf(!dpm.isAdminActive(receiver))} + var showProfileOwnerButton by remember{mutableStateOf(!isProfileOwner(dpm))} + var showDeviceOwnerButton by remember{mutableStateOf(!isDeviceOwner(dpm))} var showOrgProfileOwnerButton by remember{mutableStateOf(true)} LaunchedEffect(Unit){ - if(service==null){userServiceControl(myContext, true)} + if(service==null){userServiceControl(context, true)} while(true){ if(service==null){ enabled = false @@ -75,7 +75,7 @@ fun ShizukuActivate(){ AnimatedVisibility(bindShizuku) { Button( onClick = { - userServiceControl(myContext, true) + userServiceControl(context, true) outputText = "" } ){ @@ -85,7 +85,7 @@ fun ShizukuActivate(){ Button( onClick = { - outputText = checkPermission(myContext) + outputText = checkPermission(context) coScope.launch { outputTextScrollState.animateScrollTo(0, scrollAnim()) } @@ -111,10 +111,10 @@ fun ShizukuActivate(){ Button( onClick = { coScope.launch{ - outputText = service!!.execute(myContext.getString(R.string.dpm_activate_da_command)) + outputText = service!!.execute(context.getString(R.string.dpm_activate_da_command)) outputTextScrollState.animateScrollTo(0, scrollAnim()) delay(500) - showDeviceAdminButton = !myDpm.isAdminActive(myComponent) + showDeviceAdminButton = !dpm.isAdminActive(receiver) } }, enabled = enabled @@ -127,10 +127,10 @@ fun ShizukuActivate(){ Button( onClick = { coScope.launch{ - outputText = service!!.execute(myContext.getString(R.string.dpm_activate_po_command)) + outputText = service!!.execute(context.getString(R.string.dpm_activate_po_command)) outputTextScrollState.animateScrollTo(0, scrollAnim()) delay(600) - showProfileOwnerButton = !isProfileOwner(myDpm) + showProfileOwnerButton = !isProfileOwner(dpm) } }, enabled = enabled @@ -143,10 +143,10 @@ fun ShizukuActivate(){ Button( onClick = { coScope.launch{ - outputText = service!!.execute(myContext.getString(R.string.dpm_activate_do_command)) + outputText = service!!.execute(context.getString(R.string.dpm_activate_do_command)) outputTextScrollState.animateScrollTo(0, scrollAnim()) delay(500) - showDeviceOwnerButton = !isDeviceOwner(myDpm) + showDeviceOwnerButton = !isDeviceOwner(dpm) } }, enabled = enabled @@ -156,7 +156,7 @@ fun ShizukuActivate(){ } if( - VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent)&&!myDpm.isOrganizationOwnedDeviceWithManagedProfile + VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isManagedProfile(receiver)&&!dpm.isOrganizationOwnedDeviceWithManagedProfile ){ AnimatedVisibility(showOrgProfileOwnerButton) { Button( @@ -168,7 +168,7 @@ fun ShizukuActivate(){ ) outputTextScrollState.animateScrollTo(0, scrollAnim()) delay(500) - showOrgProfileOwnerButton = !myDpm.isOrganizationOwnedDeviceWithManagedProfile + showOrgProfileOwnerButton = !dpm.isOrganizationOwnedDeviceWithManagedProfile } }, enabled = enabled 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 43bcfe6..5e32b38 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/SystemManager.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/SystemManager.kt @@ -112,11 +112,11 @@ fun SystemManage(navCtrl:NavHostController){ @Composable private fun Home(navCtrl: NavHostController,scrollState: ScrollState){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().verticalScroll(scrollState)){ Text(text = stringResource(R.string.system_manage), style = typography.headlineLarge, modifier = Modifier.padding(top = 8.dp, bottom = 5.dp, start = 15.dp)) - if(isDeviceOwner(myDpm)||isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm)||isProfileOwner(dpm)){ SubPageItem(R.string.options,"",R.drawable.tune_fill0){navCtrl.navigate("Switches")} } SubPageItem(R.string.keyguard,"",R.drawable.screen_lock_portrait_fill0){navCtrl.navigate("Keyguard")} @@ -127,25 +127,25 @@ private fun Home(navCtrl: NavHostController,scrollState: ScrollState){ if(VERSION.SDK_INT>=28){ SubPageItem(R.string.edit_time,"",R.drawable.schedule_fill0){navCtrl.navigate("EditTime")} } - if(VERSION.SDK_INT>=23&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=23&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ SubPageItem(R.string.permission_policy,"",R.drawable.key_fill0){navCtrl.navigate("PermissionPolicy")} } - if(VERSION.SDK_INT>=34&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=34&&isDeviceOwner(dpm)){ SubPageItem(R.string.mte_policy,"",R.drawable.memory_fill0){navCtrl.navigate("MTEPolicy")} } - if(VERSION.SDK_INT>=31&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=31&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ SubPageItem(R.string.nearby_streaming_policy,"",R.drawable.share_fill0){navCtrl.navigate("NearbyStreamingPolicy")} } - if(VERSION.SDK_INT>=28&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=28&&isDeviceOwner(dpm)){ SubPageItem(R.string.lock_task_feature,"",R.drawable.lock_fill0){navCtrl.navigate("LockTaskFeatures")} } - if(isDeviceOwner(myDpm)||isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm)||isProfileOwner(dpm)){ SubPageItem(R.string.ca_cert,"",R.drawable.license_fill0){navCtrl.navigate("CaCert")} } - if(VERSION.SDK_INT>=26&&(isDeviceOwner(myDpm)||(VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile))){ + if(VERSION.SDK_INT>=26&&(isDeviceOwner(dpm)||(VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile))){ SubPageItem(R.string.security_logs,"",R.drawable.description_fill0){navCtrl.navigate("SecurityLogs")} } - if(VERSION.SDK_INT>=23&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=23&&isDeviceOwner(dpm)){ SubPageItem(R.string.system_update_policy,"",R.drawable.system_update_fill0){navCtrl.navigate("SystemUpdatePolicy")} } SubPageItem(R.string.wipe_data,"",R.drawable.warning_fill0){navCtrl.navigate("WipeData")} @@ -156,66 +156,66 @@ private fun Home(navCtrl: NavHostController,scrollState: ScrollState){ @Composable private fun Switches(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) - if(isDeviceOwner(myDpm)||isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm)||isProfileOwner(dpm)){ SwitchItem(R.string.disable_cam,"", R.drawable.photo_camera_fill0, - {myDpm.getCameraDisabled(null)},{myDpm.setCameraDisabled(myComponent,it)} + {dpm.getCameraDisabled(null)},{dpm.setCameraDisabled(receiver,it)} ) } - if(isDeviceOwner(myDpm)||isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm)||isProfileOwner(dpm)){ SwitchItem(R.string.disable_screenshot, stringResource(R.string.also_disable_aosp_screen_record),R.drawable.screenshot_fill0, - {myDpm.getScreenCaptureDisabled(null)},{myDpm.setScreenCaptureDisabled(myComponent,it) } + {dpm.getScreenCaptureDisabled(null)},{dpm.setScreenCaptureDisabled(receiver,it) } ) } - if(VERSION.SDK_INT>=34&&(isDeviceOwner(myDpm)|| (isProfileOwner(myDpm)&&myDpm.isAffiliatedUser))){ + if(VERSION.SDK_INT>=34&&(isDeviceOwner(dpm)|| (isProfileOwner(dpm)&&dpm.isAffiliatedUser))){ SwitchItem(R.string.disable_status_bar,"",R.drawable.notifications_fill0, - {myDpm.isStatusBarDisabled},{myDpm.setStatusBarDisabled(myComponent,it) } + {dpm.isStatusBarDisabled},{dpm.setStatusBarDisabled(receiver,it) } ) } - if(isDeviceOwner(myDpm)||(VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile)){ + if(isDeviceOwner(dpm)||(VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile)){ if(VERSION.SDK_INT>=30){ SwitchItem(R.string.auto_time,"",R.drawable.schedule_fill0, - {myDpm.getAutoTimeEnabled(myComponent)},{myDpm.setAutoTimeEnabled(myComponent,it) } + {dpm.getAutoTimeEnabled(receiver)},{dpm.setAutoTimeEnabled(receiver,it) } ) SwitchItem(R.string.auto_timezone,"",R.drawable.globe_fill0, - {myDpm.getAutoTimeZoneEnabled(myComponent)},{myDpm.setAutoTimeZoneEnabled(myComponent,it) } + {dpm.getAutoTimeZoneEnabled(receiver)},{dpm.setAutoTimeZoneEnabled(receiver,it) } ) }else{ - SwitchItem(R.string.auto_time,"",R.drawable.schedule_fill0,{myDpm.autoTimeRequired},{myDpm.setAutoTimeRequired(myComponent,it)}) + SwitchItem(R.string.auto_time,"",R.drawable.schedule_fill0,{dpm.autoTimeRequired},{dpm.setAutoTimeRequired(receiver,it)}) } } - if(isDeviceOwner(myDpm)|| isProfileOwner(myDpm)){ + if(isDeviceOwner(dpm)|| isProfileOwner(dpm)){ SwitchItem(R.string.master_mute,"",R.drawable.volume_up_fill0, - {myDpm.isMasterVolumeMuted(myComponent)},{myDpm.setMasterVolumeMuted(myComponent,it) } + {dpm.isMasterVolumeMuted(receiver)},{dpm.setMasterVolumeMuted(receiver,it) } ) } - if(VERSION.SDK_INT>=26&&(isDeviceOwner(myDpm)|| isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=26&&(isDeviceOwner(dpm)|| isProfileOwner(dpm))){ SwitchItem(R.string.backup_service,"",R.drawable.backup_fill0, - {myDpm.isBackupServiceEnabled(myComponent)},{myDpm.setBackupServiceEnabled(myComponent,it) } + {dpm.isBackupServiceEnabled(receiver)},{dpm.setBackupServiceEnabled(receiver,it) } ) } - if(VERSION.SDK_INT>=23&&(isDeviceOwner(myDpm)|| isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=23&&(isDeviceOwner(dpm)|| isProfileOwner(dpm))){ SwitchItem(R.string.disable_bt_contact_share,"",R.drawable.account_circle_fill0, - {myDpm.getBluetoothContactSharingDisabled(myComponent)},{myDpm.setBluetoothContactSharingDisabled(myComponent,it)} + {dpm.getBluetoothContactSharingDisabled(receiver)},{dpm.setBluetoothContactSharingDisabled(receiver,it)} ) } - if(VERSION.SDK_INT>=30&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=30&&isDeviceOwner(dpm)){ SwitchItem(R.string.common_criteria_mode, stringResource(R.string.common_criteria_mode_desc),R.drawable.security_fill0, - {myDpm.isCommonCriteriaModeEnabled(myComponent)},{myDpm.setCommonCriteriaModeEnabled(myComponent,it)} + {dpm.isCommonCriteriaModeEnabled(receiver)},{dpm.setCommonCriteriaModeEnabled(receiver,it)} ) } - if(VERSION.SDK_INT>=31&&(isDeviceOwner(myDpm)||(VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile))){ + if(VERSION.SDK_INT>=31&&(isDeviceOwner(dpm)||(VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile))){ SwitchItem( - R.string.usb_signal,"",R.drawable.usb_fill0, {myDpm.isUsbDataSignalingEnabled}, + R.string.usb_signal,"",R.drawable.usb_fill0, {dpm.isUsbDataSignalingEnabled}, { - if(myDpm.canUsbDataSignalingBeDisabled()){ - myDpm.isUsbDataSignalingEnabled = it + if(dpm.canUsbDataSignalingBeDisabled()){ + dpm.isUsbDataSignalingEnabled = it }else{ - Toast.makeText(myContext, R.string.unsupported, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.unsupported, Toast.LENGTH_SHORT).show() } } ) @@ -226,9 +226,9 @@ private fun Switches(){ @Composable private fun Keyguard(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.keyguard), style = typography.headlineLarge) @@ -236,18 +236,18 @@ private fun Keyguard(){ if(VERSION.SDK_INT>=23){ Button( onClick = { - Toast.makeText(myContext, if(myDpm.setKeyguardDisabled(myComponent,true)){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + Toast.makeText(context, if(dpm.setKeyguardDisabled(receiver,true)){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm)|| (VERSION.SDK_INT>=28&&isProfileOwner(myDpm)&&myDpm.isAffiliatedUser), + enabled = isDeviceOwner(dpm)|| (VERSION.SDK_INT>=28&&isProfileOwner(dpm)&&dpm.isAffiliatedUser), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.disable)) } Button( onClick = { - Toast.makeText(myContext, if(myDpm.setKeyguardDisabled(myComponent,false)){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + Toast.makeText(context, if(dpm.setKeyguardDisabled(receiver,false)){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm)|| (VERSION.SDK_INT>=28&&isProfileOwner(myDpm)&&myDpm.isAffiliatedUser), + enabled = isDeviceOwner(dpm)|| (VERSION.SDK_INT>=28&&isProfileOwner(dpm)&&dpm.isAffiliatedUser), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.enable)) @@ -258,8 +258,8 @@ private fun Keyguard(){ } var flag by remember{mutableIntStateOf(0)} Button( - onClick = {myDpm.lockNow()}, - enabled = myDpm.isAdminActive(myComponent), + onClick = {dpm.lockNow()}, + enabled = dpm.isAdminActive(receiver), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.lock_now)) @@ -272,18 +272,18 @@ private fun Keyguard(){ @SuppressLint("NewApi") @Composable private fun BugReport(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp)){ Spacer(Modifier.padding(vertical = 10.dp)) Button( onClick = { - val result = myDpm.requestBugreport(myComponent) - Toast.makeText(myContext, if(result){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + val result = dpm.requestBugreport(receiver) + Toast.makeText(context, if(result){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth(), - enabled = isDeviceOwner(myDpm) + enabled = isDeviceOwner(dpm) ) { Text(stringResource(R.string.request_bug_report)) } @@ -293,14 +293,14 @@ private fun BugReport(){ @SuppressLint("NewApi") @Composable private fun Reboot(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp)){ Spacer(Modifier.padding(vertical = 10.dp)) Button( - onClick = {myDpm.reboot(myComponent)}, - enabled = isDeviceOwner(myDpm), + onClick = {dpm.reboot(receiver)}, + enabled = isDeviceOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.reboot)) @@ -311,9 +311,9 @@ private fun Reboot(){ @SuppressLint("NewApi") @Composable private fun EditTime(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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)) @@ -328,14 +328,14 @@ private fun EditTime(){ onValueChange = {inputTime = it}, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), - enabled = isDeviceOwner(myDpm)||(VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile), + enabled = isDeviceOwner(dpm)||(VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile), modifier = Modifier.focusable().fillMaxWidth() ) Spacer(Modifier.padding(vertical = 5.dp)) Button( - onClick = {myDpm.setTime(myComponent,inputTime.toLong())}, + onClick = {dpm.setTime(receiver,inputTime.toLong())}, modifier = Modifier.fillMaxWidth(), - enabled = inputTime!=""&&(isDeviceOwner(myDpm)||(VERSION.SDK_INT>=30&&isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile)) + enabled = inputTime!=""&&(isDeviceOwner(dpm)||(VERSION.SDK_INT>=30&&isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile)) ) { Text("应用") } @@ -351,11 +351,11 @@ private fun EditTime(){ @SuppressLint("NewApi") @Composable private fun PermissionPolicy(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ - var selectedPolicy by remember{mutableIntStateOf(myDpm.getPermissionPolicy(myComponent))} + var selectedPolicy by remember{mutableIntStateOf(dpm.getPermissionPolicy(receiver))} Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.permission_policy), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) @@ -365,8 +365,8 @@ private fun PermissionPolicy(){ Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - myDpm.setPermissionPolicy(myComponent,selectedPolicy) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setPermissionPolicy(receiver,selectedPolicy) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -378,25 +378,25 @@ private fun PermissionPolicy(){ @SuppressLint("NewApi") @Composable private fun MTEPolicy(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.mte_policy), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) - var selectedMtePolicy by remember{mutableIntStateOf(myDpm.mtePolicy)} + var selectedMtePolicy by remember{mutableIntStateOf(dpm.mtePolicy)} RadioButtonItem(stringResource(R.string.decide_by_user), {selectedMtePolicy==MTE_NOT_CONTROLLED_BY_POLICY}, {selectedMtePolicy= MTE_NOT_CONTROLLED_BY_POLICY}) RadioButtonItem(stringResource(R.string.enabled), {selectedMtePolicy==MTE_ENABLED}, {selectedMtePolicy=MTE_ENABLED}) RadioButtonItem(stringResource(R.string.disabled), {selectedMtePolicy==MTE_DISABLED}, {selectedMtePolicy=MTE_DISABLED}) Button( onClick = { try { - myDpm.mtePolicy = selectedMtePolicy - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.mtePolicy = selectedMtePolicy + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }catch(e:java.lang.UnsupportedOperationException){ - Toast.makeText(myContext, R.string.unsupported, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.unsupported, Toast.LENGTH_SHORT).show() } - selectedMtePolicy = myDpm.mtePolicy + selectedMtePolicy = dpm.mtePolicy }, modifier = Modifier.fillMaxWidth() ) { @@ -411,10 +411,10 @@ private fun MTEPolicy(){ @SuppressLint("NewApi") @Composable private fun NearbyStreamingPolicy(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ - var appPolicy by remember{mutableIntStateOf(myDpm.nearbyAppStreamingPolicy)} + var appPolicy by remember{mutableIntStateOf(dpm.nearbyAppStreamingPolicy)} Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.nearby_app_streaming), style = typography.titleLarge) Spacer(Modifier.padding(vertical = 3.dp)) @@ -425,14 +425,14 @@ private fun NearbyStreamingPolicy(){ Spacer(Modifier.padding(vertical = 3.dp)) Button( onClick = { - myDpm.nearbyAppStreamingPolicy = appPolicy - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.nearbyAppStreamingPolicy = appPolicy + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { Text("应用") } - var notificationPolicy by remember{mutableIntStateOf(myDpm.nearbyNotificationStreamingPolicy)} + var notificationPolicy by remember{mutableIntStateOf(dpm.nearbyNotificationStreamingPolicy)} Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.nearby_notification_streaming), style = typography.titleLarge) Spacer(Modifier.padding(vertical = 3.dp)) @@ -443,8 +443,8 @@ private fun NearbyStreamingPolicy(){ Spacer(Modifier.padding(vertical = 3.dp)) Button( onClick = { - myDpm.nearbyNotificationStreamingPolicy = notificationPolicy - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.nearbyNotificationStreamingPolicy = notificationPolicy + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ) { @@ -457,9 +457,9 @@ private fun NearbyStreamingPolicy(){ @SuppressLint("NewApi") @Composable private fun LockTaskFeatures(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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())){ val lockTaskPolicyList = mutableListOf( @@ -482,7 +482,7 @@ private fun LockTaskFeatures(){ var inited by remember{mutableStateOf(false)} var custom by remember{mutableStateOf(false)} val refreshFeature = { - var calculate = myDpm.getLockTaskFeatures(myComponent) + var calculate = dpm.getLockTaskFeatures(receiver) if(calculate!=0){ if(VERSION.SDK_INT>=30&&calculate-lockTaskPolicyList[7]>=0){blockAct=true;calculate-=lockTaskPolicyList[7]} if(calculate-lockTaskPolicyList[6]>=0){keyGuard=true;calculate-=lockTaskPolicyList[6]} @@ -498,7 +498,7 @@ private fun LockTaskFeatures(){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.lock_task_feature), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) - if(!inited){ refreshFeature();custom=myDpm.getLockTaskFeatures(myComponent)!=0;inited=true } + if(!inited){ refreshFeature();custom=dpm.getLockTaskFeatures(receiver)!=0;inited=true } RadioButtonItem(stringResource(R.string.disable_all),{!custom},{custom=false}) RadioButtonItem(stringResource(R.string.custom),{custom},{custom=true}) AnimatedVisibility(custom) { @@ -525,15 +525,15 @@ private fun LockTaskFeatures(){ if(notifications){result+=lockTaskPolicyList[2]} if(sysInfo){result+=lockTaskPolicyList[1]} } - myDpm.setLockTaskFeatures(myComponent,result) + dpm.setLockTaskFeatures(receiver,result) refreshFeature() - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() } ) { Text(stringResource(R.string.apply)) } Spacer(Modifier.padding(vertical = 5.dp)) - val whitelist = myDpm.getLockTaskPackages(myComponent).toMutableList() + val whitelist = dpm.getLockTaskPackages(receiver).toMutableList() var listText by remember{mutableStateOf("")} var inputPkg by remember{mutableStateOf("")} val refreshWhitelist = { @@ -559,8 +559,8 @@ private fun LockTaskFeatures(){ onClick = { focusMgr.clearFocus() whitelist.add(inputPkg) - myDpm.setLockTaskPackages(myComponent,whitelist.toTypedArray()) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setLockTaskPackages(receiver,whitelist.toTypedArray()) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() refreshWhitelist() }, modifier = Modifier.fillMaxWidth(0.49F) @@ -572,10 +572,10 @@ private fun LockTaskFeatures(){ focusMgr.clearFocus() if(inputPkg in whitelist){ whitelist.remove(inputPkg) - myDpm.setLockTaskPackages(myComponent,whitelist.toTypedArray()) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setLockTaskPackages(receiver,whitelist.toTypedArray()) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }else{ - Toast.makeText(myContext, R.string.not_exist, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.not_exist, Toast.LENGTH_SHORT).show() } refreshWhitelist() }, @@ -590,16 +590,16 @@ private fun LockTaskFeatures(){ @Composable private fun CaCert(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) var exist by remember{mutableStateOf(false)} var uriPath by remember{mutableStateOf("")} var caCertByteArray = byteArrayOf() val refresh = { if(uriPath!=fileUri?.path){ if(caCertByteArray.isEmpty()){ - uriToStream(myContext, fileUri){ + uriToStream(context, fileUri){ val array = it.readBytes() caCertByteArray = if(array.size<10000){ array @@ -607,7 +607,7 @@ private fun CaCert(){ byteArrayOf() } } - exist = myDpm.hasCaCertInstalled(myComponent, caCertByteArray) + exist = dpm.hasCaCertInstalled(receiver, caCertByteArray) } uriPath = fileUri?.path?:"" } @@ -640,8 +640,8 @@ private fun CaCert(){ Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween){ Button( onClick = { - val result = myDpm.installCaCert(myComponent, caCertByteArray) - Toast.makeText(myContext, if(result){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + val result = dpm.installCaCert(receiver, caCertByteArray) + Toast.makeText(context, if(result){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() refresh() }, modifier = Modifier.fillMaxWidth(0.49F) @@ -651,10 +651,10 @@ private fun CaCert(){ Button( onClick = { if(exist){ - myDpm.uninstallCaCert(myComponent, caCertByteArray) - exist = myDpm.hasCaCertInstalled(myComponent, caCertByteArray) - Toast.makeText(myContext, if(exist){R.string.fail}else{R.string.success}, Toast.LENGTH_SHORT).show() - }else{ Toast.makeText(myContext, R.string.not_exist, Toast.LENGTH_SHORT).show() } + dpm.uninstallCaCert(receiver, caCertByteArray) + exist = dpm.hasCaCertInstalled(receiver, caCertByteArray) + Toast.makeText(context, if(exist){R.string.fail}else{R.string.success}, Toast.LENGTH_SHORT).show() + }else{ Toast.makeText(context, R.string.not_exist, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth(0.96F) ) { @@ -664,8 +664,8 @@ private fun CaCert(){ } Button( onClick = { - myDpm.uninstallAllUserCaCerts(myComponent) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.uninstallAllUserCaCerts(receiver) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, modifier = Modifier.fillMaxWidth() ){ @@ -677,24 +677,24 @@ private fun CaCert(){ @SuppressLint("NewApi") @Composable private fun SecurityLogs(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.security_logs), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) Text(text = stringResource(R.string.developing)) - SwitchItem(R.string.enable,"",null,{myDpm.isSecurityLoggingEnabled(myComponent)},{myDpm.setSecurityLoggingEnabled(myComponent,it)}) + SwitchItem(R.string.enable,"",null,{dpm.isSecurityLoggingEnabled(receiver)},{dpm.setSecurityLoggingEnabled(receiver,it)}) Button( onClick = { - val log = myDpm.retrieveSecurityLogs(myComponent) + val log = dpm.retrieveSecurityLogs(receiver) if(log!=null){ for(i in log){ Log.d("SecureLog",i.toString()) } - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }else{ - Log.d("SecureLog",myContext.getString(R.string.none)) - Toast.makeText(myContext, R.string.no_logs, Toast.LENGTH_SHORT).show() + Log.d("SecureLog",context.getString(R.string.none)) + Toast.makeText(context, R.string.no_logs, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth() @@ -703,13 +703,13 @@ private fun SecurityLogs(){ } Button( onClick = { - val log = myDpm.retrievePreRebootSecurityLogs(myComponent) + val log = dpm.retrievePreRebootSecurityLogs(receiver) if(log!=null){ for(i in log){ Log.d("SecureLog",i.toString()) } - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }else{ - Log.d("SecureLog",myContext.getString(R.string.none)) - Toast.makeText(myContext, R.string.no_logs, Toast.LENGTH_SHORT).show() + Log.d("SecureLog",context.getString(R.string.none)) + Toast.makeText(context, R.string.no_logs, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth() @@ -721,10 +721,10 @@ private fun SecurityLogs(){ @Composable private fun WipeData(){ - val myContext = LocalContext.current - val userManager = myContext.getSystemService(Context.USER_SERVICE) as UserManager - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager + 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())){ var flag by remember{ mutableIntStateOf(0) } @@ -738,7 +738,7 @@ private fun WipeData(){ Text(text = stringResource(R.string.wipe_data),style = typography.headlineLarge,modifier = Modifier.padding(6.dp),color = colorScheme.error) Spacer(Modifier.padding(vertical = 5.dp)) CheckBoxItem(stringResource(R.string.wipe_external_storage),{externalStorage},{externalStorage=!externalStorage;confirmed=false}) - if(VERSION.SDK_INT>=22&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=22&&isDeviceOwner(dpm)){ CheckBoxItem(stringResource(R.string.wipe_reset_protection_data),{protectionData},{protectionData=!protectionData;confirmed=false}) } if(VERSION.SDK_INT>=28){ CheckBoxItem(stringResource(R.string.wipe_euicc),{euicc},{euicc=!euicc;confirmed=false}) } @@ -767,7 +767,7 @@ private fun WipeData(){ containerColor = if(confirmed){ colorScheme.primary }else{ colorScheme.error }, contentColor = if(confirmed){ colorScheme.onPrimary }else{ colorScheme.onError } ), - enabled = myDpm.isAdminActive(myComponent), + enabled = dpm.isAdminActive(receiver), modifier = Modifier.fillMaxWidth() ) { Text(text = stringResource(if(confirmed){ R.string.cancel }else{ R.string.confirm })) @@ -775,9 +775,9 @@ private fun WipeData(){ Button( onClick = { if(VERSION.SDK_INT>=28&&reason!=""){ - myDpm.wipeData(flag,reason) + dpm.wipeData(flag,reason) }else{ - myDpm.wipeData(flag) + dpm.wipeData(flag) } }, colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError), @@ -786,9 +786,9 @@ private fun WipeData(){ ) { Text("WipeData") } - if (VERSION.SDK_INT >= 34&&(isDeviceOwner(myDpm)||(isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile))) { + if (VERSION.SDK_INT >= 34&&(isDeviceOwner(dpm)||(isProfileOwner(dpm)&&dpm.isOrganizationOwnedDeviceWithManagedProfile))) { Button( - onClick = {myDpm.wipeDevice(flag)}, + onClick = {dpm.wipeDevice(flag)}, colors = ButtonDefaults.buttonColors(containerColor = colorScheme.error, contentColor = colorScheme.onError), enabled = confirmed, modifier = Modifier.fillMaxWidth() @@ -797,7 +797,7 @@ private fun WipeData(){ } } Spacer(Modifier.padding(vertical = 5.dp)) - if(VERSION.SDK_INT>=24&&isProfileOwner(myDpm)&&myDpm.isManagedProfile(myComponent)){ + 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){ @@ -809,18 +809,18 @@ private fun WipeData(){ @Composable private fun SysUpdatePolicy(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 - val sharedPref = myContext.getSharedPreferences("data", Context.MODE_PRIVATE) + val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE) val isWear = sharedPref.getBoolean("isWear",false) val bodyTextStyle = if(isWear){ typography.bodyMedium}else{typography.bodyLarge} Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) if(VERSION.SDK_INT>=23){ Column { - var selectedPolicy by remember{ mutableStateOf(myDpm.systemUpdatePolicy?.policyType) } + var selectedPolicy by remember{ mutableStateOf(dpm.systemUpdatePolicy?.policyType) } Text(text = stringResource(R.string.system_update_policy), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) RadioButtonItem(stringResource(R.string.system_update_policy_automatic),{selectedPolicy==TYPE_INSTALL_AUTOMATIC},{selectedPolicy= TYPE_INSTALL_AUTOMATIC}) @@ -860,10 +860,10 @@ private fun SysUpdatePolicy(){ TYPE_POSTPONE-> SystemUpdatePolicy.createPostponeInstallPolicy() else->null } - myDpm.setSystemUpdatePolicy(myComponent,policy) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setSystemUpdatePolicy(receiver,policy) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.apply)) @@ -872,7 +872,7 @@ private fun SysUpdatePolicy(){ } if(VERSION.SDK_INT>=26){ Spacer(Modifier.padding(vertical = 10.dp)) - val sysUpdateInfo = myDpm.getPendingSystemUpdate(myComponent) + val sysUpdateInfo = dpm.getPendingSystemUpdate(receiver) Column { if(sysUpdateInfo!=null){ Text(text = stringResource(R.string.update_received_time, Date(sysUpdateInfo.receivedTime)), style = bodyTextStyle) @@ -900,14 +900,14 @@ private fun SysUpdatePolicy(){ getOtaPackage.launch(getUri) }, modifier = Modifier.fillMaxWidth(), - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm) + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm) ) { Text("选择OTA包") } Button( onClick = {resultUri = otaUri}, modifier = Modifier.fillMaxWidth(), - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm) + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm) ) { Text("查看OTA包详情") } @@ -917,9 +917,9 @@ private fun SysUpdatePolicy(){ onClick = { val sysUpdateExecutor = Executors.newCachedThreadPool() val sysUpdateCallback:InstallSystemUpdateCallback = InstallSystemUpdateCallback - myDpm.installSystemUpdate(myComponent,resultUri,sysUpdateExecutor,sysUpdateCallback) + dpm.installSystemUpdate(receiver,resultUri,sysUpdateExecutor,sysUpdateCallback) }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm) + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm) ){ Text("安装") } diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/UserManager.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/UserManager.kt index 4b33d84..9c63f01 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/UserManager.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/UserManager.kt @@ -99,23 +99,23 @@ fun UserManage(navCtrl:NavHostController) { @Composable private fun Home(navCtrl: NavHostController,scrollState: ScrollState){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager Column(modifier = Modifier.fillMaxSize().verticalScroll(scrollState)){ Text(text = stringResource(R.string.user_manager), style = typography.headlineLarge, modifier = Modifier.padding(top = 8.dp, bottom = 5.dp, start = 15.dp)) SubPageItem(R.string.user_info,"",R.drawable.person_fill0){navCtrl.navigate("UserInfo")} SubPageItem(R.string.user_operation,"",R.drawable.sync_alt_fill0){navCtrl.navigate("UserOperation")} - if(VERSION.SDK_INT>=24&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=24&&isDeviceOwner(dpm)){ SubPageItem(R.string.create_user,"",R.drawable.person_add_fill0){navCtrl.navigate("CreateUser")} } SubPageItem(R.string.edit_username,"",R.drawable.edit_fill0){navCtrl.navigate("EditUsername")} - if(VERSION.SDK_INT>=23&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=23&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ SubPageItem(R.string.change_user_icon,"",R.drawable.account_circle_fill0){navCtrl.navigate("ChangeUserIcon")} } - if(VERSION.SDK_INT>=28&&isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=28&&isDeviceOwner(dpm)){ SubPageItem(R.string.user_session_msg,"",R.drawable.notifications_fill0){navCtrl.navigate("UserSessionMessage")} } - if(VERSION.SDK_INT>=26&&(isDeviceOwner(myDpm)||isProfileOwner(myDpm))){ + if(VERSION.SDK_INT>=26&&(isDeviceOwner(dpm)||isProfileOwner(dpm))){ SubPageItem(R.string.affiliation_id,"",R.drawable.id_card_fill0){navCtrl.navigate("AffiliationID")} } Spacer(Modifier.padding(vertical = 30.dp)) @@ -127,28 +127,28 @@ private fun Home(navCtrl: NavHostController,scrollState: ScrollState){ @Composable private fun CurrentUserInfo(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) - val userManager = myContext.getSystemService(Context.USER_SERVICE) as UserManager + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) + val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ Spacer(Modifier.padding(vertical = 10.dp)) Text(text = stringResource(R.string.user_info), style = typography.headlineLarge) Spacer(Modifier.padding(vertical = 5.dp)) - Text(stringResource(R.string.is_user_unlocked, UserManagerCompat.isUserUnlocked(myContext))) + Text(stringResource(R.string.is_user_unlocked, UserManagerCompat.isUserUnlocked(context))) if(VERSION.SDK_INT>=24){ Text(stringResource(R.string.is_support_multi_user, UserManager.supportsMultipleUsers())) } if(VERSION.SDK_INT>=23){ Text(text = stringResource(R.string.is_system_user, userManager.isSystemUser)) } if(VERSION.SDK_INT>=34){ Text(text = stringResource(R.string.is_admin_user, userManager.isAdminUser)) } if(VERSION.SDK_INT>=31){ Text(text = stringResource(R.string.is_headless_system_user, UserManager.isHeadlessSystemUserMode())) } Spacer(Modifier.padding(vertical = 5.dp)) if (VERSION.SDK_INT >= 28) { - val logoutable = myDpm.isLogoutEnabled + val logoutable = dpm.isLogoutEnabled Text(text = stringResource(R.string.user_can_logout, logoutable)) - if(isDeviceOwner(myDpm)|| isProfileOwner(myDpm)){ - val ephemeralUser = myDpm.isEphemeralUser(myComponent) + if(isDeviceOwner(dpm)|| isProfileOwner(dpm)){ + val ephemeralUser = dpm.isEphemeralUser(receiver) Text(text = stringResource(R.string.is_ephemeral_user, ephemeralUser)) } - Text(text = stringResource(R.string.is_affiliated_user, myDpm.isAffiliatedUser)) + Text(text = stringResource(R.string.is_affiliated_user, dpm.isAffiliatedUser)) } Spacer(Modifier.padding(vertical = 5.dp)) Text(text = stringResource(R.string.user_id_is, Binder.getCallingUid()/100000)) @@ -158,10 +158,10 @@ private fun CurrentUserInfo(){ @Composable private fun UserOperation(){ - val myContext = LocalContext.current - val userManager = myContext.getSystemService(Context.USER_SERVICE) as UserManager - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager + 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)) @@ -184,7 +184,7 @@ private fun UserOperation(){ } }, label = {Text(if(useUid){"UID"}else{ stringResource(R.string.serial_number) })}, - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.focusable().fillMaxWidth(), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}) @@ -195,13 +195,13 @@ private fun UserOperation(){ } Spacer(Modifier.padding(vertical = 5.dp)) if(VERSION.SDK_INT>28){ - if(isProfileOwner(myDpm)&&myDpm.isAffiliatedUser){ + if(isProfileOwner(dpm)&&dpm.isAffiliatedUser){ Button( onClick = { - val result = myDpm.logoutUser(myComponent) - Toast.makeText(myContext, userOperationResultCode(result,myContext), Toast.LENGTH_SHORT).show() + val result = dpm.logoutUser(receiver) + Toast.makeText(context, userOperationResultCode(result,context), Toast.LENGTH_SHORT).show() }, - enabled = isProfileOwner(myDpm), + enabled = isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.logout_current_user)) @@ -212,10 +212,10 @@ private fun UserOperation(){ Button( onClick = { focusMgr.clearFocus() - val result = myDpm.startUserInBackground(myComponent,userHandleById) - Toast.makeText(myContext, userOperationResultCode(result,myContext), Toast.LENGTH_SHORT).show() + val result = dpm.startUserInBackground(receiver,userHandleById) + Toast.makeText(context, userOperationResultCode(result,context), Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.fillMaxWidth() ){ Text(stringResource(R.string.start_in_background)) @@ -224,9 +224,9 @@ private fun UserOperation(){ Button( onClick = { focusMgr.clearFocus() - Toast.makeText(myContext, if(myDpm.switchUser(myComponent,userHandleById)) { R.string.success }else{ R.string.fail }, Toast.LENGTH_SHORT).show() + Toast.makeText(context, if(dpm.switchUser(receiver,userHandleById)) { R.string.success }else{ R.string.fail }, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.user_operation_switch)) @@ -236,13 +236,13 @@ private fun UserOperation(){ onClick = { focusMgr.clearFocus() try{ - val result = myDpm.stopUser(myComponent,userHandleById) - Toast.makeText(myContext, userOperationResultCode(result,myContext), Toast.LENGTH_SHORT).show() + val result = dpm.stopUser(receiver,userHandleById) + Toast.makeText(context, userOperationResultCode(result,context), Toast.LENGTH_SHORT).show() }catch(e:IllegalArgumentException){ - Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } }, - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.user_operation_stop)) @@ -251,14 +251,14 @@ private fun UserOperation(){ Button( onClick = { focusMgr.clearFocus() - if(myDpm.removeUser(myComponent,userHandleById)){ - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + if(dpm.removeUser(receiver,userHandleById)){ + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() idInput="" }else{ - Toast.makeText(myContext, R.string.fail, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.fail, Toast.LENGTH_SHORT).show() } }, - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.user_operation_remove)) @@ -270,10 +270,10 @@ private fun UserOperation(){ @SuppressLint("NewApi") @Composable private fun CreateUser(){ - val myContext = LocalContext.current - val userManager = myContext.getSystemService(Context.USER_SERVICE) as UserManager - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) val focusMgr = LocalFocusManager.current var userName by remember{ mutableStateOf("") } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ @@ -300,11 +300,11 @@ private fun CreateUser(){ Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - newUserHandle=myDpm.createAndManageUser(myComponent,userName,myComponent,null,selectedFlag) + newUserHandle=dpm.createAndManageUser(receiver,userName,receiver,null,selectedFlag) focusMgr.clearFocus() - Toast.makeText(myContext, if(newUserHandle!=null){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() + Toast.makeText(context, if(newUserHandle!=null){R.string.success}else{R.string.fail}, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm), + enabled = isDeviceOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.create)) @@ -318,14 +318,14 @@ private fun CreateUser(){ @SuppressLint("NewApi") @Composable private fun AffiliationID(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 var input by remember{mutableStateOf("")} var list by remember{mutableStateOf("")} LaunchedEffect(Unit){ - affiliationID = myDpm.getAffiliationIds(myComponent) + affiliationID = dpm.getAffiliationIds(receiver) list = affiliationID.toText() } Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ @@ -366,14 +366,14 @@ private fun AffiliationID(){ Button( onClick = { if("" in affiliationID) { - Toast.makeText(myContext, R.string.include_empty_string, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.include_empty_string, Toast.LENGTH_SHORT).show() }else if(affiliationID.isEmpty()){ - Toast.makeText(myContext, R.string.cannot_be_empty, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.cannot_be_empty, Toast.LENGTH_SHORT).show() }else{ - myDpm.setAffiliationIds(myComponent, affiliationID) - affiliationID = myDpm.getAffiliationIds(myComponent) + dpm.setAffiliationIds(receiver, affiliationID) + affiliationID = dpm.getAffiliationIds(receiver) list = affiliationID.toText() - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth() @@ -386,9 +386,9 @@ private fun AffiliationID(){ @Composable private fun Username(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 var inputUsername by remember{mutableStateOf("")} Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ @@ -402,24 +402,24 @@ private fun Username(){ keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), modifier = Modifier.focusable().fillMaxWidth(), - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm) + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm) ) Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - myDpm.setProfileName(myComponent,inputUsername) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setProfileName(receiver,inputUsername) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.apply)) } Button( onClick = { - myDpm.setProfileName(myComponent,null) + dpm.setProfileName(receiver,null) }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.reset)) @@ -430,12 +430,12 @@ private fun Username(){ @SuppressLint("NewApi") @Composable private fun UserSessionMessage(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + 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 - val getStart = myDpm.getStartUserSessionMessage(myComponent)?:"" - val getEnd = myDpm.getEndUserSessionMessage(myComponent)?:"" + val getStart = dpm.getStartUserSessionMessage(receiver)?:"" + val getEnd = dpm.getEndUserSessionMessage(receiver)?:"" var start by remember{mutableStateOf(getStart.toString())} var end by remember{mutableStateOf(getEnd.toString())} Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ @@ -449,7 +449,7 @@ private fun UserSessionMessage(){ keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), modifier = Modifier.focusable().fillMaxWidth(), - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm) + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm) ) Spacer(Modifier.padding(vertical = 2.dp)) OutlinedTextField( @@ -459,27 +459,27 @@ private fun UserSessionMessage(){ keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}), modifier = Modifier.focusable().fillMaxWidth(), - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm) + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm) ) Spacer(Modifier.padding(vertical = 5.dp)) Button( onClick = { - myDpm.setStartUserSessionMessage(myComponent,start) - myDpm.setEndUserSessionMessage(myComponent,end) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setStartUserSessionMessage(receiver,start) + dpm.setEndUserSessionMessage(receiver,end) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.apply)) } Button( onClick = { - myDpm.setStartUserSessionMessage(myComponent,null) - myDpm.setEndUserSessionMessage(myComponent,null) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setStartUserSessionMessage(receiver,null) + dpm.setEndUserSessionMessage(receiver,null) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() }, - enabled = isDeviceOwner(myDpm)||isProfileOwner(myDpm), + enabled = isDeviceOwner(dpm)||isProfileOwner(dpm), modifier = Modifier.fillMaxWidth() ) { Text(stringResource(R.string.reset)) @@ -491,9 +491,9 @@ private fun UserSessionMessage(){ @SuppressLint("NewApi") @Composable private fun UserIcon(){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) var getContent by remember{mutableStateOf(false)} var canApply by remember{mutableStateOf(false)} Column(modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp).verticalScroll(rememberScrollState())){ @@ -519,10 +519,10 @@ private fun UserIcon(){ AnimatedVisibility(canApply) { Button( onClick = { - uriToStream(myContext, fileUri){stream -> + uriToStream(context, fileUri){stream -> val bitmap = BitmapFactory.decodeStream(stream) - myDpm.setUserIcon(myComponent,bitmap) - Toast.makeText(myContext, R.string.success, Toast.LENGTH_SHORT).show() + dpm.setUserIcon(receiver,bitmap) + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() } }, modifier = Modifier.fillMaxWidth() @@ -533,12 +533,12 @@ private fun UserIcon(){ } } -private fun userOperationResultCode(result:Int, myContext: Context): String { +private fun userOperationResultCode(result:Int, context: Context): String { return when(result){ - UserManager.USER_OPERATION_SUCCESS->myContext.getString(R.string.success) - UserManager.USER_OPERATION_ERROR_UNKNOWN-> myContext.getString(R.string.unknown_result) - UserManager.USER_OPERATION_ERROR_MANAGED_PROFILE-> myContext.getString(R.string.fail_managed_profile) - UserManager.USER_OPERATION_ERROR_CURRENT_USER-> myContext.getString(R.string.fail_current_user) + UserManager.USER_OPERATION_SUCCESS->context.getString(R.string.success) + UserManager.USER_OPERATION_ERROR_UNKNOWN-> context.getString(R.string.unknown_result) + UserManager.USER_OPERATION_ERROR_MANAGED_PROFILE-> context.getString(R.string.fail_managed_profile) + UserManager.USER_OPERATION_ERROR_CURRENT_USER-> context.getString(R.string.fail_current_user) else->"未知" } } diff --git a/app/src/main/java/com/bintianqi/owndroid/dpm/UserRestriction.kt b/app/src/main/java/com/bintianqi/owndroid/dpm/UserRestriction.kt index 67095d4..0bdc8aa 100644 --- a/app/src/main/java/com/bintianqi/owndroid/dpm/UserRestriction.kt +++ b/app/src/main/java/com/bintianqi/owndroid/dpm/UserRestriction.kt @@ -97,14 +97,14 @@ fun UserRestriction(navCtrl: NavHostController){ @Composable private fun Home(navCtrl:NavHostController,scrollState: ScrollState){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext, Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context, Receiver::class.java) Column(modifier = Modifier.fillMaxSize().verticalScroll(scrollState)){ Text(text = stringResource(R.string.user_restrict), style = typography.headlineLarge, modifier = Modifier.padding(top = 8.dp, bottom = 7.dp, start = 15.dp)) Text(text = stringResource(R.string.switch_to_disable_feature), modifier = Modifier.padding(start = 15.dp)) - if(isProfileOwner(myDpm)){ Text(text = stringResource(R.string.profile_owner_is_restricted), modifier = Modifier.padding(start = 15.dp)) } - if(isProfileOwner(myDpm)&&(VERSION.SDK_INT<24||(VERSION.SDK_INT>=24&&myDpm.isManagedProfile(myComponent)))){ + if(isProfileOwner(dpm)){ Text(text = stringResource(R.string.profile_owner_is_restricted), modifier = Modifier.padding(start = 15.dp)) } + if(isProfileOwner(dpm)&&(VERSION.SDK_INT<24||(VERSION.SDK_INT>=24&&dpm.isManagedProfile(receiver)))){ Text(text = stringResource(R.string.some_features_invalid_in_work_profile), modifier = Modifier.padding(start = 15.dp)) } Spacer(Modifier.padding(vertical = 2.dp)) @@ -141,9 +141,9 @@ private fun Connectivity(){ @Composable fun Application(){ - val myContext = LocalContext.current + val context = LocalContext.current Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())){ - for(applicationItem in RestrictionData.application(myContext)){ + for(applicationItem in RestrictionData.application(context)){ UserRestrictionItem(applicationItem.restriction,applicationItem.name,applicationItem.desc,applicationItem.ico) } Spacer(Modifier.padding(vertical = 30.dp)) @@ -172,9 +172,9 @@ private fun Media(){ @Composable private fun Other(){ - val myContext = LocalContext.current + val context = LocalContext.current Column(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())){ - for(otherItem in RestrictionData.other(myContext)){ + for(otherItem in RestrictionData.other(context)){ UserRestrictionItem(otherItem.restriction,otherItem.name,otherItem.desc,otherItem.ico) } Spacer(Modifier.padding(vertical = 30.dp)) @@ -188,26 +188,26 @@ private fun UserRestrictionItem( restrictionDescription:String, leadIcon:Int ){ - val myContext = LocalContext.current - val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager - val myComponent = ComponentName(myContext,Receiver::class.java) + val context = LocalContext.current + val dpm = context.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager + val receiver = ComponentName(context,Receiver::class.java) SwitchItem( itemName,restrictionDescription,leadIcon, - { if(isDeviceOwner(myDpm)||isProfileOwner(myDpm)){ myDpm.getUserRestrictions(myComponent).getBoolean(restriction) }else{ false } }, + { if(isDeviceOwner(dpm)||isProfileOwner(dpm)){ dpm.getUserRestrictions(receiver).getBoolean(restriction) }else{ false } }, { try{ if(it){ - myDpm.addUserRestriction(myComponent,restriction) + dpm.addUserRestriction(receiver,restriction) }else{ - myDpm.clearUserRestriction(myComponent,restriction) + dpm.clearUserRestriction(receiver,restriction) } }catch(e:SecurityException){ - if(isProfileOwner(myDpm)){ - Toast.makeText(myContext, R.string.require_device_owner, Toast.LENGTH_SHORT).show() + if(isProfileOwner(dpm)){ + Toast.makeText(context, R.string.require_device_owner, Toast.LENGTH_SHORT).show() } } }, - isDeviceOwner(myDpm)||isProfileOwner(myDpm) + isDeviceOwner(dpm)||isProfileOwner(dpm) ) } @@ -252,13 +252,13 @@ private object RestrictionData{ if(VERSION.SDK_INT>=28){list += Restriction(UserManager.DISALLOW_PRINTING,R.string.printing,"",R.drawable.print_fill0)} return list } - fun application(myContext: Context):List{ + fun application(context: Context):List{ val list:MutableList = mutableListOf() list += Restriction(UserManager.DISALLOW_INSTALL_APPS,R.string.install_app,"",R.drawable.android_fill0) if(VERSION.SDK_INT>=29){list += Restriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY,R.string.install_unknown_src_globally,"",R.drawable.android_fill0)} list += Restriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,R.string.inst_unknown_src,"",R.drawable.android_fill0) list += Restriction(UserManager.DISALLOW_UNINSTALL_APPS,R.string.uninstall_app,"",R.drawable.delete_fill0) - list += Restriction(UserManager.DISALLOW_APPS_CONTROL,R.string.apps_ctrl, myContext.getString(R.string.apps_control_desc),R.drawable.apps_fill0) + list += Restriction(UserManager.DISALLOW_APPS_CONTROL,R.string.apps_ctrl, context.getString(R.string.apps_control_desc),R.drawable.apps_fill0) if(VERSION.SDK_INT>=34){ list += Restriction(UserManager.DISALLOW_CONFIG_DEFAULT_APPS,R.string.config_default_apps,"",R.drawable.apps_fill0) } return list } @@ -290,7 +290,7 @@ private object RestrictionData{ } return list } - fun other(myContext: Context):List{ + fun other(context: Context):List{ val list:MutableList = mutableListOf() if(VERSION.SDK_INT>=26){ list += Restriction(UserManager.DISALLOW_AUTOFILL,R.string.autofill, "",R.drawable.password_fill0) } list += Restriction(UserManager.DISALLOW_CONFIG_CREDENTIALS,R.string.config_credentials,"",R.drawable.android_fill0) @@ -298,10 +298,10 @@ private object RestrictionData{ list += Restriction(UserManager.DISALLOW_CONTENT_CAPTURE,R.string.content_capture,"",R.drawable.screenshot_fill0) list += Restriction(UserManager.DISALLOW_CONTENT_SUGGESTIONS,R.string.content_suggestions,"",R.drawable.sms_fill0) } - list += Restriction(UserManager.DISALLOW_CREATE_WINDOWS,R.string.create_windows, myContext.getString(R.string.create_windows_desc),R.drawable.web_asset) + list += Restriction(UserManager.DISALLOW_CREATE_WINDOWS,R.string.create_windows, context.getString(R.string.create_windows_desc),R.drawable.web_asset) if(VERSION.SDK_INT>=24){list += Restriction(UserManager.DISALLOW_SET_WALLPAPER,R.string.set_wallpaper,"",R.drawable.wallpaper_fill0)} if(VERSION.SDK_INT>=34){ list += Restriction(UserManager.DISALLOW_GRANT_ADMIN,R.string.grant_admin,"",R.drawable.security_fill0) } - if(VERSION.SDK_INT>=23){ list += Restriction(UserManager.DISALLOW_FUN,R.string.`fun`, myContext.getString(R.string.fun_desc),R.drawable.stadia_controller_fill0) } + if(VERSION.SDK_INT>=23){ list += Restriction(UserManager.DISALLOW_FUN,R.string.`fun`, context.getString(R.string.fun_desc),R.drawable.stadia_controller_fill0) } list += Restriction(UserManager.DISALLOW_MODIFY_ACCOUNTS,R.string.modify_accounts,"",R.drawable.manage_accounts_fill0) if(VERSION.SDK_INT>=28){ list += Restriction(UserManager.DISALLOW_CONFIG_LOCALE,R.string.config_locale,"",R.drawable.language_fill0)