Android13系统设置第三方app为设备所有者

最近遇到一个项目,客户要求内置一个客制app,并要求开机默认将这个app设置为系统的默认设备所有者app和设备管理器app,但是这个项目又是一个要求过GMS谷歌认证的项目,发现这个需求跟GMS过认证要求冲突了,实现了客户要求后会导致GMS认证测试过不了,主要原因是机器第一次开机进入到开机向导app处会自动跳过开机向导直接进入桌面,我们知道GMS测试是对开机向导app有测试内容和要求的,开机向导自动跳过直接进入桌面那肯定是不能送测3pl去过GMS的,如果是定制案子项目或者国内项目,没有过GMS的要求,那还是可以去实现的,具体的实现方法如下几步:

1. 拷贝设备所有者和设备管理器两个对应的xml文件到系统分区

        设备所有者对应的device_owner_2.xml文件内容如下:





        设备管理器对应的device_policies.xml文件内容如下:














        以上两个xml文件需要自己提前创建好,记得将包类名换成自己的哈,然后通过在mk文件添加拷贝代码将两个xml文件编译时拷贝到系统分区下,拷贝代码如下:

PRODUCT_COPY_FILES += device/rockchip/rk3562/device_policies.xml:system/device_policies.xml
PRODUCT_COPY_FILES += device/rockchip/rk3562/device_owner_2.xml:system/device_owner_2.xml

2. 将拷贝到系统分区下的两个xml文件继续通过init.rc服务拷贝到data/system/目录下并修改权限

        init.rc文件中的拷贝并修改权限的代码如下:       

diff --git a/rootdir/init.rc b/rootdir/init.rc
index 2a4817606..31b990dd0 100755
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -1027,6 +1027,15 @@ on post-fs-data
     # Enable FUSE by default
     setprop persist.sys.fuse true
 
+    # set device manager as device owner
+    copy /system/device_owner_2.xml /data/system/device_owner_2.xml
+    chmod 0600 /data/system/device_owner_2.xml
+    chown system system /data/system/device_owner_2.xml
+    copy /system/device_policies.xml /data/system/device_policies.xml
+    chmod 0600 /data/system/device_policies.xml
+    chown system system /data/system/device_policies.xml

 # It is recommended to put unnecessary data/ initialization from post-fs-data
 # to start-zygote in device's init.rc to unblock zygote start.
 on zygote-start && property:ro.crypto.state=unencrypted

3. 添加默认设置第三方app为系统默认的设备所有者和设备管理器代码,如下:

diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
old mode 100644
new mode 100755
index 4a26787bccd7..0c910918fea0
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -928,6 +928,19 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager {
                     Slogf.v(LOG_TAG, "Sending password expiration notifications for action "
                             + action + " for user " + userHandle);
                 }
+				
+				// set device owner
+                try {
+                    ComponentName adminComponent = new ComponentName("com.softi9.cuco", "com.softi9.cuco.android.LockDeviceAdminReceiver");
+                    Slogf.e("INCAR", "Initial adminComponent: " + adminComponent);
+                    if (!enableAdminAndSetProfileOwner(0, 0, adminComponent, "com.softi9.cuco")) {
+                        Slogf.e("INCAR", "Failed to enable Admin and set profile owner");
+                    }
+                } catch (Exception e) {
+                    Slogf.e("INCAR", "Failure provisioning managed profile, failed to "
+                        + "infer the device admin component name", e);
+                }
+
                 mHandler.post(new Runnable() {
                     @Override
                     public void run() {

DevicePolicyManagerService.java文件所在目录是frameworks/base,记得将包类名换成自己app的

你可能感兴趣的:(Android,安卓,平板,1024程序员节,android,平板,java)