Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 604aec95 authored by Zoltan Szatmary-Ban's avatar Zoltan Szatmary-Ban Committed by Android (Google) Code Review
Browse files

Merge "Add new subcommand 'set-active-admin' to the dpm command." into lmp-mr1-dev

parents 962b06f9 e9119876
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -38,18 +38,25 @@ public final class Dpm extends BaseCommand {
      (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;

    @Override
    public 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 <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" +
@@ -68,6 +75,9 @@ public final class Dpm extends BaseCommand {

        String command = nextArgRequired();
        switch (command) {
            case COMMAND_SET_ACTIVE_ADMIN:
                runSetActiveAdmin();
                break;
            case COMMAND_SET_DEVICE_OWNER:
                runSetDeviceOwner();
                break;
@@ -79,6 +89,22 @@ public final class Dpm extends BaseCommand {
        }
    }

    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);
@@ -99,6 +125,7 @@ public final class Dpm extends BaseCommand {
    }

    private void runSetProfileOwner() throws RemoteException {
        // 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);