From 54023bee59312a9e8f487ae099aab647b189e2ff Mon Sep 17 00:00:00 2001 From: BinTianqi <1220958406@qq.com> Date: Thu, 8 Feb 2024 15:54:32 +0800 Subject: [PATCH] fix creating managed profile problem --- .../binbin/androidowner/ApplicationManage.kt | 2 +- .../com/binbin/androidowner/DeviceControl.kt | 35 +++++++++++-------- .../com/binbin/androidowner/MainActivity.kt | 18 ++++++++-- .../java/com/binbin/androidowner/Network.kt | 2 +- .../com/binbin/androidowner/Permissions.kt | 3 -- .../main/java/com/binbin/androidowner/User.kt | 33 +++++++++++------ 6 files changed, 59 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/binbin/androidowner/ApplicationManage.kt b/app/src/main/java/com/binbin/androidowner/ApplicationManage.kt index ae33926..83af48d 100644 --- a/app/src/main/java/com/binbin/androidowner/ApplicationManage.kt +++ b/app/src/main/java/com/binbin/androidowner/ApplicationManage.kt @@ -76,7 +76,7 @@ fun ApplicationManage(){ keyboardActions = KeyboardActions(onDone = {focusMgr.clearFocus()}) ) } - if(VERSION.SDK_INT>=24){ + if(VERSION.SDK_INT>=24&&isDeviceOwner(myDpm)){ val isSuspended: Boolean = try{ myDpm.isPackageSuspended(myComponent,pkgName) } catch(e:NameNotFoundException){ false } catch(w:NameNotFoundException){ false } diff --git a/app/src/main/java/com/binbin/androidowner/DeviceControl.kt b/app/src/main/java/com/binbin/androidowner/DeviceControl.kt index 442707b..9b225b5 100644 --- a/app/src/main/java/com/binbin/androidowner/DeviceControl.kt +++ b/app/src/main/java/com/binbin/androidowner/DeviceControl.kt @@ -58,15 +58,17 @@ fun DeviceControl(){ {myDpm.isStatusBarDisabled},{b -> myDpm.setStatusBarDisabled(myComponent,b) } ) } - if(VERSION.SDK_INT>=30&&isDeviceOwner(myDpm)){ - DeviceCtrlItem(R.string.auto_time,R.string.place_holder,R.drawable.schedule_fill0, - {myDpm.getAutoTimeEnabled(myComponent)},{b -> myDpm.setAutoTimeEnabled(myComponent,b) } - ) - DeviceCtrlItem(R.string.auto_timezone,R.string.place_holder,R.drawable.globe_fill0, - {myDpm.getAutoTimeZoneEnabled(myComponent)},{b -> myDpm.setAutoTimeZoneEnabled(myComponent,b) } - ) - }else{ - DeviceCtrlItem(R.string.auto_time,R.string.place_holder,R.drawable.schedule_fill0,{myDpm.autoTimeRequired},{b -> myDpm.setAutoTimeRequired(myComponent,b)}) + if(isDeviceOwner(myDpm)){ + if(VERSION.SDK_INT>=30){ + DeviceCtrlItem(R.string.auto_time,R.string.place_holder,R.drawable.schedule_fill0, + {myDpm.getAutoTimeEnabled(myComponent)},{b -> myDpm.setAutoTimeEnabled(myComponent,b) } + ) + DeviceCtrlItem(R.string.auto_timezone,R.string.place_holder,R.drawable.globe_fill0, + {myDpm.getAutoTimeZoneEnabled(myComponent)},{b -> myDpm.setAutoTimeZoneEnabled(myComponent,b) } + ) + }else{ + DeviceCtrlItem(R.string.auto_time,R.string.place_holder,R.drawable.schedule_fill0,{myDpm.autoTimeRequired},{b -> myDpm.setAutoTimeRequired(myComponent,b)}) + } } if(isDeviceOwner(myDpm)|| isProfileOwner(myDpm)){ DeviceCtrlItem(R.string.master_mute,R.string.place_holder,R.drawable.volume_up_fill0, @@ -155,8 +157,9 @@ fun DeviceControl(){ val result = myDpm.requestBugreport(myComponent) Toast.makeText(myContext, if(result){"成功"}else{"失败"}, Toast.LENGTH_SHORT).show() }, - modifier = Modifier.fillMaxWidth() - ) { + modifier = Modifier.fillMaxWidth(), + enabled = isDeviceOwner(myDpm) + ) { Text("请求错误报告") } Button( @@ -488,10 +491,12 @@ fun DeviceControl(){ var confirmed by remember{ mutableStateOf(false) } Text(text = "清除数据",style = typography.titleLarge,modifier = Modifier.padding(6.dp),color = colorScheme.onErrorContainer) RadioButtonItem("默认",{flag==0},{flag=0}, colorScheme.onErrorContainer) - RadioButtonItem("WIPE_EXTERNAL_STORAGE",{flag==0x0001},{flag=0x0001}, colorScheme.onErrorContainer) - RadioButtonItem("WIPE_RESET_PROTECTION_DATA",{flag==0x0002},{flag=0x0002}, colorScheme.onErrorContainer) - RadioButtonItem("WIPE_EUICC",{flag==0x0004},{flag=0x0004}, colorScheme.onErrorContainer) - RadioButtonItem("WIPE_SILENTLY",{flag==0x0008},{flag=0x0008}, colorScheme.onErrorContainer) + RadioButtonItem("WIPE_EXTERNAL_STORAGE",{flag==WIPE_EXTERNAL_STORAGE},{flag=WIPE_EXTERNAL_STORAGE}, colorScheme.onErrorContainer) + if(VERSION.SDK_INT>=22){ + RadioButtonItem("WIPE_RESET_PROTECTION_DATA",{flag==WIPE_RESET_PROTECTION_DATA},{flag=WIPE_RESET_PROTECTION_DATA}, colorScheme.onErrorContainer) + } + if(VERSION.SDK_INT>=28){ RadioButtonItem("WIPE_EUICC",{flag==WIPE_EUICC},{flag=WIPE_EUICC}, colorScheme.onErrorContainer) } + if(VERSION.SDK_INT>=29){ RadioButtonItem("WIPE_SILENTLY",{flag==WIPE_SILENTLY},{flag=WIPE_SILENTLY}, colorScheme.onErrorContainer) } Text(text = "清空数据的不能是系统用户",color = colorScheme.onErrorContainer, style = if(!sharedPref.getBoolean("isWear",false)){typography.bodyLarge}else{typography.bodyMedium}) Button( diff --git a/app/src/main/java/com/binbin/androidowner/MainActivity.kt b/app/src/main/java/com/binbin/androidowner/MainActivity.kt index f2e0007..05995e4 100644 --- a/app/src/main/java/com/binbin/androidowner/MainActivity.kt +++ b/app/src/main/java/com/binbin/androidowner/MainActivity.kt @@ -1,6 +1,7 @@ package com.binbin.androidowner import android.annotation.SuppressLint +import android.app.Activity import android.app.admin.DevicePolicyManager import android.content.ComponentName import android.content.Context @@ -45,6 +46,7 @@ import java.io.IOException lateinit var getCaCert: ActivityResultLauncher lateinit var createUser:ActivityResultLauncher +lateinit var createManagedProfile:ActivityResultLauncher var caCert = byteArrayOf() @ExperimentalMaterial3Api @@ -69,11 +71,17 @@ class MainActivity : ComponentActivity() { } createUser = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { when(it.resultCode){ - UserManager.USER_CREATION_FAILED_NO_MORE_USERS->Toast.makeText(applicationContext, "用户太多了", Toast.LENGTH_SHORT).show(); - UserManager.USER_CREATION_FAILED_NOT_PERMITTED->Toast.makeText(applicationContext, "不是管理员用户", Toast.LENGTH_SHORT).show(); + UserManager.USER_CREATION_FAILED_NO_MORE_USERS->Toast.makeText(applicationContext, "用户太多了", Toast.LENGTH_SHORT).show() + UserManager.USER_CREATION_FAILED_NOT_PERMITTED->Toast.makeText(applicationContext, "不是管理员用户", Toast.LENGTH_SHORT).show() else->Toast.makeText(applicationContext, "成功", Toast.LENGTH_SHORT).show() } } + createManagedProfile = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + when(it.resultCode){ + Activity.RESULT_OK->Toast.makeText(applicationContext, "创建成功", Toast.LENGTH_SHORT).show() + Activity.RESULT_CANCELED->Toast.makeText(applicationContext, "用户已取消", Toast.LENGTH_SHORT).show() + } + } setContent { AndroidOwnerTheme { MyScaffold() @@ -168,7 +176,11 @@ fun HomePage(navCtrl:NavHostController){ val myContext = LocalContext.current val myDpm = myContext.getSystemService(ComponentActivity.DEVICE_POLICY_SERVICE) as DevicePolicyManager val myComponent = ComponentName(myContext,MyDeviceAdminReceiver::class.java) - val activateType = if(isDeviceOwner(myDpm)){"Device Owner"}else if(isProfileOwner(myDpm)){"Profile Owner"}else if(myDpm.isAdminActive(myComponent)){"Device Admin"}else{""} + val activateType = + if(isDeviceOwner(myDpm)){"Device Owner"} + else if(isProfileOwner(myDpm)){if(VERSION.SDK_INT>=24&&myDpm.isManagedProfile(myComponent)){"工作资料"}else{"Profile Owner"}} + else if(myDpm.isAdminActive(myComponent)){"Device Admin"} + else{""} val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE) val isWear = sharedPref.getBoolean("isWear",false) caCert = byteArrayOf() diff --git a/app/src/main/java/com/binbin/androidowner/Network.kt b/app/src/main/java/com/binbin/androidowner/Network.kt index 763fa4c..3bd72e6 100644 --- a/app/src/main/java/com/binbin/androidowner/Network.kt +++ b/app/src/main/java/com/binbin/androidowner/Network.kt @@ -62,7 +62,7 @@ fun Network(){ {myDpm.isPreferentialNetworkServiceEnabled},{b -> myDpm.isPreferentialNetworkServiceEnabled = b} ) } - if(VERSION.SDK_INT>=30){ + if(VERSION.SDK_INT>=30&&isDeviceOwner(myDpm)){ DeviceCtrlItem(R.string.wifi_lockdown,R.string.place_holder,R.drawable.wifi_password_fill0, {myDpm.hasLockdownAdminConfiguredNetworks(myComponent)},{b -> myDpm.setConfiguredNetworksLockdownState(myComponent,b)} ) diff --git a/app/src/main/java/com/binbin/androidowner/Permissions.kt b/app/src/main/java/com/binbin/androidowner/Permissions.kt index de9187a..a849181 100644 --- a/app/src/main/java/com/binbin/androidowner/Permissions.kt +++ b/app/src/main/java/com/binbin/androidowner/Permissions.kt @@ -177,9 +177,6 @@ fun DpmPermissions(navCtrl:NavHostController){ Text(text = "设备信息", style = typography.titleLarge,color = titleColor) val orgDevice = myDpm.isOrganizationOwnedDeviceWithManagedProfile Text("由组织拥有的受管理资料设备:$orgDevice",style=bodyTextStyle) - if(isDeviceOwner(myDpm)|| isProfileOwner(myDpm)){ - Text("Managed profile: ${myDpm.isManagedProfile(myComponent)}",style=bodyTextStyle) - } if(VERSION.SDK_INT>=34&&(isDeviceOwner(myDpm)||(isProfileOwner(myDpm)&&myDpm.isOrganizationOwnedDeviceWithManagedProfile))){ val financed = myDpm.isDeviceFinanced Text("企业资产 : $financed",style=bodyTextStyle) diff --git a/app/src/main/java/com/binbin/androidowner/User.kt b/app/src/main/java/com/binbin/androidowner/User.kt index e315b57..f73c0a2 100644 --- a/app/src/main/java/com/binbin/androidowner/User.kt +++ b/app/src/main/java/com/binbin/androidowner/User.kt @@ -194,28 +194,39 @@ fun UserManage(navCtrl:NavHostController){ Column(modifier = sections()) { Text(text = "工作资料", style = typography.titleLarge) - Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween){ + if(VERSION.SDK_INT>=24){Text(text = "可以创建工作资料:${myDpm.isProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE)}", style = bodyTextStyle)} + if(isDeviceOwner(myDpm)){Text(text = "Device owner不能创建工作资料", style = bodyTextStyle)} + if(VERSION.SDK_INT<24||(VERSION.SDK_INT>=24&&myDpm.isProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE))){ + var skipEncrypt by remember{mutableStateOf(false)} + if(VERSION.SDK_INT>=24){CheckBoxItem("跳过加密",{skipEncrypt},{skipEncrypt=!skipEncrypt})} Button( - onClick = { createWorkProfile(myContext)}, - modifier = Modifier.fillMaxWidth(0.49F) + onClick = { + val intent = Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE) + if(VERSION.SDK_INT>=23){ + intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,myComponent) + }else{ + intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,"com.binbin.androidowner") + } + if(VERSION.SDK_INT>=24){intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_SKIP_ENCRYPTION,skipEncrypt)} + if(VERSION.SDK_INT>=33){intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_ALLOW_OFFLINE,true)} + createManagedProfile.launch(intent) + }, + modifier = Modifier.fillMaxWidth() ) { Text("创建") } + } + if(isProfileOwner(myDpm)){ Button( onClick = { - try{ - myDpm.setProfileEnabled(myComponent) - }catch(e:SecurityException){ - Toast.makeText(myContext, "失败", Toast.LENGTH_SHORT).show() - } + myDpm.setProfileEnabled(myComponent) + Toast.makeText(myContext, "成功", Toast.LENGTH_SHORT).show() }, - enabled = isProfileOwner(myDpm)||isDeviceOwner(myDpm), - modifier = Modifier.fillMaxWidth(0.96F) + modifier = Modifier.fillMaxWidth() ) { Text(text = "启用") } } - Text("可能无法创建工作资料",style = bodyTextStyle) } if(VERSION.SDK_INT>=24){