Skip to content

Commit

Permalink
fix creating managed profile problem
Browse files Browse the repository at this point in the history
  • Loading branch information
BinTianqi committed Feb 8, 2024
1 parent 62a4376 commit 54023be
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
35 changes: 20 additions & 15 deletions app/src/main/java/com/binbin/androidowner/DeviceControl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/com/binbin/androidowner/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,6 +46,7 @@ import java.io.IOException

lateinit var getCaCert: ActivityResultLauncher<Intent>
lateinit var createUser:ActivityResultLauncher<Intent>
lateinit var createManagedProfile:ActivityResultLauncher<Intent>
var caCert = byteArrayOf()

@ExperimentalMaterial3Api
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/binbin/androidowner/Network.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
)
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/binbin/androidowner/Permissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 22 additions & 11 deletions app/src/main/java/com/binbin/androidowner/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down

1 comment on commit 54023be

@BinTianqi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot_20240208-212635_工作设置

Please sign in to comment.