diff options
author | Esteban Talavera <etalavera@google.com> | 2014-09-12 15:48:55 +0100 |
---|---|---|
committer | Esteban Talavera <etalavera@google.com> | 2014-09-15 11:42:34 +0100 |
commit | 9c17388cff98fba9679ce6457ac5003a660c6013 (patch) | |
tree | 4908c6381906e7298eb2a6297e7a2ba09b8570ef /cmds/dpm | |
parent | 81e0c8491f22c64300182c652ac2add96888dd2e (diff) | |
download | frameworks_base-9c17388cff98fba9679ce6457ac5003a660c6013.zip frameworks_base-9c17388cff98fba9679ce6457ac5003a660c6013.tar.gz frameworks_base-9c17388cff98fba9679ce6457ac5003a660c6013.tar.bz2 |
Set device admin on dpm set-device-owner command
Bug: 17312478
Change-Id: I4ef730a97d603352eda77f04a2b00fa97c2be662
Diffstat (limited to 'cmds/dpm')
-rw-r--r-- | cmds/dpm/src/com/android/commands/dpm/Dpm.java | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/cmds/dpm/src/com/android/commands/dpm/Dpm.java b/cmds/dpm/src/com/android/commands/dpm/Dpm.java index 724b312..6a5ecee 100644 --- a/cmds/dpm/src/com/android/commands/dpm/Dpm.java +++ b/cmds/dpm/src/com/android/commands/dpm/Dpm.java @@ -17,9 +17,11 @@ package com.android.commands.dpm; import android.app.admin.IDevicePolicyManager; +import android.content.ComponentName; import android.content.Context; import android.os.RemoteException; import android.os.ServiceManager; +import android.os.UserHandle; import com.android.internal.os.BaseCommand; @@ -42,10 +44,12 @@ public final class Dpm extends BaseCommand { @Override public void onShowUsage(PrintStream out) { - out.println("usage: adb shell dpm [subcommand] [options]\n" + + out.println( + "usage: dpm [subcommand] [options]\n" + + "usage: dpm set-device-owner <COMPONENT>\n" + "\n" + - "usage: adb shell dpm set-device-owner <PACKAGE>\n" + - " <PACKAGE> an Android package name.\n"); + "dpm set-device-owner: Sets the given component as active admin, and its\n" + + " package as device owner.\n"); } @Override @@ -63,15 +67,28 @@ public final class Dpm extends BaseCommand { runSetDeviceOwner(nextArgRequired()); break; default: - showError("Error: unknown command '" + command + "'"); + throw new IllegalArgumentException ("unknown command '" + command + "'"); } } - private void runSetDeviceOwner(String packageName) throws RemoteException { - if (mDevicePolicyManager.setDeviceOwner(packageName, null)) { - System.out.println("Device owner set to package " + packageName); - } else { - showError("Error: Can't set package " + packageName + " as device owner."); + 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); + + String packageName = component.getPackageName(); + try { + if (!mDevicePolicyManager.setDeviceOwner(packageName, null)) { + throw new Exception("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("Device owner set to package " + packageName); + System.out.println("Active admin set to component " + component.toShortString()); } }
\ No newline at end of file |