From 41de9bb6fda113f7cfb8b7b8d64d07d3809f8f20 Mon Sep 17 00:00:00 2001 From: Esteban Talavera Date: Mon, 15 Sep 2014 19:05:48 +0100 Subject: Add 'adb dpm' subcommand to set profile owner Required for GTS tests. Needed to relax the restriction that only root user can run 'pm create-user' as GTS tests can't get root permissions. Bug: 17312478 Change-Id: I1841286ddf51756c73018c087a5f29afeb5b9f15 --- cmds/dpm/src/com/android/commands/dpm/Dpm.java | 61 +++++++++++++++++++++----- 1 file changed, 51 insertions(+), 10 deletions(-) (limited to 'cmds/dpm') diff --git a/cmds/dpm/src/com/android/commands/dpm/Dpm.java b/cmds/dpm/src/com/android/commands/dpm/Dpm.java index 6a5ecee..b8b2087 100644 --- a/cmds/dpm/src/com/android/commands/dpm/Dpm.java +++ b/cmds/dpm/src/com/android/commands/dpm/Dpm.java @@ -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 \n" + + "usage: dpm set-profile-owner \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 -- cgit v1.1