版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/u012439416/article/details/61925456
1. 概述
dpm主要是和DevicePolicyManage进行交互,通过用户授权应用设备管理权限后,可以在代码修改很多系统设置。
2. dpm命令
命令格式:dpm <command>
命令列表:
| 命令 | 功能 | 实现方法 |
|---|---|---|
| set-active-admin | 激活 | mDevicePolicyManager.setActiveAdmin |
| set-device-owner | 设置设备权限 | setDeviceOwner |
| set-profile-owner | 文件权限 | setProfileOwner |
3. 详细流程
所有adb命令的dpm方法最后都会通过Dpm.java执行,然后通过跨进程调用DevicePolicyManager的方法完成。
流程很简单,在此就不论述了,仅给出Dpm.java文件
public final class Dpm extends BaseCommand {/*** Command-line entry point.** @param args The command-line arguments*/public static void main(String[] args) {(new Dpm()).run(args);}private static final String COMMAND_SET_ACTIVE_ADMIN = "set-active-admin";private static final String COMMAND_SET_DEVICE_OWNER = "set-device-owner";private static final String COMMAND_SET_PROFILE_OWNER = "set-profile-owner";private IDevicePolicyManager mDevicePolicyManager;private int mUserId = UserHandle.USER_OWNER;private ComponentName mComponent = null;@Overridepublic void onShowUsage(PrintStream out) {out.println("usage: dpm [subcommand] [options]\n" +"usage: dpm set-active-admin [ --user <USER_ID> ] <COMPONENT>\n" +"usage: dpm set-device-owner <COMPONENT>\n" +"usage: dpm set-profile-owner [ --user <USER_ID> ] <COMPONENT>\n" +"\n" +"dpm set-active-admin: Sets the given component as active admin" +" for an existing user.\n" +"\n" +"dpm set-device-owner: Sets the given component as active admin, and its\n" +" package as device owner.\n" +"\n" +"dpm set-profile-owner: Sets the given component as active admin and profile" +" owner for an existing user.\n");}@Overridepublic void onRun() throws Exception {mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));if (mDevicePolicyManager == null) {showError("Error: Could not access the Device Policy Manager. Is the system running?");return;}String command = nextArgRequired();switch (command) {case COMMAND_SET_ACTIVE_ADMIN:runSetActiveAdmin();break;case COMMAND_SET_DEVICE_OWNER:runSetDeviceOwner();break;case COMMAND_SET_PROFILE_OWNER:runSetProfileOwner();break;default:throw new IllegalArgumentException ("unknown command '" + command + "'");}}private void parseArgs(boolean canHaveUser) {String nextArg = nextArgRequired();if (canHaveUser && "--user".equals(nextArg)) {mUserId = parseInt(nextArgRequired());nextArg = nextArgRequired();}mComponent = parseComponentName(nextArg);}private void runSetActiveAdmin() throws RemoteException {parseArgs(true);mDevicePolicyManager.setActiveAdmin(mComponent, true /*refreshing*/, mUserId);System.out.println("Success: Active admin set to component " + mComponent.toShortString());}private void runSetDeviceOwner() throws RemoteException {ComponentName component = parseComponentName(nextArgRequired());mDevicePolicyManager.setActiveAdmin(component, true /*refreshing*/, UserHandle.USER_OWNER);String packageName = component.getPackageName();try {if (!mDevicePolicyManager.setDeviceOwner(packageName, null /*ownerName*/)) {throw new RuntimeException("Can't set package " + packageName + " as device owner.");}} catch (Exception e) {// Need to remove the admin that we just added.mDevicePolicyManager.removeActiveAdmin(component, UserHandle.USER_OWNER);throw e;}System.out.println("Success: Device owner set to package " + packageName);System.out.println("Active admin set to component " + component.toShortString());}private void runSetProfileOwner() throws RemoteException {parseArgs(true);mDevicePolicyManager.setActiveAdmin(mComponent, true /*refreshing*/, mUserId);try {if (!mDevicePolicyManager.setProfileOwner(mComponent, "" /*ownerName*/, mUserId)) {throw new RuntimeException("Can't set component " + mComponent.toShortString() +" as profile owner for user " + mUserId);}} catch (Exception e) {// Need to remove the admin that we just added.mDevicePolicyManager.removeActiveAdmin(mComponent, mUserId);throw e;}System.out.println("Success: Active admin and profile owner set to "+ mComponent.toShortString() + " for user " + mUserId);}private ComponentName parseComponentName(String component) {ComponentName cn = ComponentName.unflattenFromString(component);if (cn == null) {throw new IllegalArgumentException ("Invalid component " + component);}return cn;}private int parseInt(String argument) {try {return Integer.parseInt(argument);} catch (NumberFormatException e) {throw new IllegalArgumentException ("Invalid integer argument '" + argument + "'", e);}}}
