summaryrefslogtreecommitdiffstats
path: root/cmds/dpm
diff options
context:
space:
mode:
authorEsteban Talavera <etalavera@google.com>2014-09-15 19:05:48 +0100
committerEsteban Talavera <etalavera@google.com>2014-09-16 10:24:58 +0100
commit41de9bb6fda113f7cfb8b7b8d64d07d3809f8f20 (patch)
tree5076956deff1cfc717e3d205c71f3b93f90a7a02 /cmds/dpm
parent41f766394d07d09113d2e64a6917d069d41026c6 (diff)
downloadframeworks_base-41de9bb6fda113f7cfb8b7b8d64d07d3809f8f20.zip
frameworks_base-41de9bb6fda113f7cfb8b7b8d64d07d3809f8f20.tar.gz
frameworks_base-41de9bb6fda113f7cfb8b7b8d64d07d3809f8f20.tar.bz2
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
Diffstat (limited to 'cmds/dpm')
-rw-r--r--cmds/dpm/src/com/android/commands/dpm/Dpm.java61
1 files changed, 51 insertions, 10 deletions
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 <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