FileDocCategorySizeDatePackage
Dpm.javaAPI DocAndroid 5.1 API6368Thu Mar 12 22:22:08 GMT 2015com.android.commands.dpm

Dpm

public final class Dpm extends com.android.internal.os.BaseCommand

Fields Summary
private static final String
COMMAND_SET_ACTIVE_ADMIN
private static final String
COMMAND_SET_DEVICE_OWNER
private static final String
COMMAND_SET_PROFILE_OWNER
private android.app.admin.IDevicePolicyManager
mDevicePolicyManager
private int
mUserId
private android.content.ComponentName
mComponent
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
Command-line entry point.

param
args The command-line arguments

      (new Dpm()).run(args);
    
public voidonRun()

        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 + "'");
        }
    
public voidonShowUsage(java.io.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 <COMPONENT> <USER_ID>\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");
    
private voidparseArgs(boolean canHaveUser)

        String nextArg = nextArgRequired();
        if (canHaveUser && "--user".equals(nextArg)) {
            mUserId = parseInt(nextArgRequired());
            nextArg = nextArgRequired();
        }
        mComponent = parseComponentName(nextArg);
    
private android.content.ComponentNameparseComponentName(java.lang.String component)

        ComponentName cn = ComponentName.unflattenFromString(component);
        if (cn == null) {
            throw new IllegalArgumentException ("Invalid component " + component);
        }
        return cn;
    
private intparseInt(java.lang.String argument)

        try {
            return Integer.parseInt(argument);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException ("Invalid integer argument '" + argument + "'", e);
        }
    
private voidrunSetActiveAdmin()

        parseArgs(true);
        mDevicePolicyManager.setActiveAdmin(mComponent, true /*refreshing*/, mUserId);

        System.out.println("Success: Active admin set to component " + mComponent.toShortString());
    
private voidrunSetDeviceOwner()

        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 voidrunSetProfileOwner()

        // To be refactored later to use parseArgs(boolean). Currently in use by existing tests.
        ComponentName component = parseComponentName(nextArgRequired());
        int userId = parseInt(nextArgRequired());
        mDevicePolicyManager.setActiveAdmin(component, true /*refreshing*/, userId);

        try {
            if (!mDevicePolicyManager.setProfileOwner(component, "" /*ownerName*/, userId)) {
                throw new RuntimeException("Can't set component " + component.toShortString() +
                        " as profile owner for user " + userId);
            }
        } catch (Exception e) {
            // Need to remove the admin that we just added.
            mDevicePolicyManager.removeActiveAdmin(component, userId);
            throw e;
        }
        System.out.println("Success: Active admin and profile owner set to "
                + component.toShortString() + " for user " + userId);