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

Commit 63b328bd authored by Esteban Talavera's avatar Esteban Talavera Committed by Android (Google) Code Review
Browse files

Merge "Add 'adb dpm' subcommand to set profile owner" into lmp-dev

parents 9077cbc3 41de9bb6
Loading
Loading
Loading
Loading
+51 −10
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ public final class Dpm extends BaseCommand {
    }

    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;

@@ -47,9 +48,13 @@ public final class Dpm extends BaseCommand {
        out.println(
                "usage: dpm [subcommand] [options]\n" +
                "usage: dpm set-device-owner <COMPONENT>\n" +
                "usage: dpm set-profile-owner <COMPONENT> <USER_ID>\n" +
                "\n" +
                "dpm set-device-owner: Sets the given component as active admin, and its\n" +
                "  package as device owner.\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");
    }

    @Override
@@ -64,24 +69,25 @@ public final class Dpm extends BaseCommand {
        String command = nextArgRequired();
        switch (command) {
            case COMMAND_SET_DEVICE_OWNER:
                runSetDeviceOwner(nextArgRequired());
                runSetDeviceOwner();
                break;
            case COMMAND_SET_PROFILE_OWNER:
                runSetProfileOwner();
                break;
            default:
                throw new IllegalArgumentException ("unknown command '" + command + "'");
        }
    }

    private void runSetDeviceOwner(String argument) throws Exception {
        ComponentName component = ComponentName.unflattenFromString(argument);
        if (component == null) {
            throw new IllegalArgumentException ("Invalid component " + argument);
        }
        mDevicePolicyManager.setActiveAdmin(component, true, UserHandle.USER_OWNER);
    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)) {
                throw new Exception("Can't set package " + packageName + " as device owner.");
            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.
@@ -91,4 +97,39 @@ public final class Dpm extends BaseCommand {
        System.out.println("Device owner set to package " + packageName);
        System.out.println("Active admin set to component " + component.toShortString());
    }

    private void runSetProfileOwner() throws RemoteException {
        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("Active admin and profile owner set to " + component.toShortString() +
                " for user " + userId);
    }

    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);
        }
    }
}
 No newline at end of file
+0 −4
Original line number Diff line number Diff line
@@ -1190,10 +1190,6 @@ public final class Pm {
            if (userId < 0) {
                info = mUm.createUser(name, flags);
            } else {
                if (Process.myUid() != 0) {
                    System.err.println("Error: not running as root.");
                    return;
                }
                info = mUm.createProfileForUser(name, flags, userId);
            }
            if (info != null) {