Methods Summary |
---|
public static void | main(java.lang.String[] args)Command-line entry point.
(new Dpm()).run(args);
|
public void | onRun()
mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
if (mDevicePolicyManager == null) {
showError("Error: Could not access the Device Policy Manager. Is the system running?");
return;
}
String command = nextArgRequired();
switch (command) {
case COMMAND_SET_ACTIVE_ADMIN:
runSetActiveAdmin();
break;
case COMMAND_SET_DEVICE_OWNER:
runSetDeviceOwner();
break;
case COMMAND_SET_PROFILE_OWNER:
runSetProfileOwner();
break;
default:
throw new IllegalArgumentException ("unknown command '" + command + "'");
}
|
public void | onShowUsage(java.io.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" +
"dpm set-profile-owner: Sets the given component as active admin and profile" +
" owner for an existing user.\n");
|
private void | parseArgs(boolean canHaveUser)
String nextArg = nextArgRequired();
if (canHaveUser && "--user".equals(nextArg)) {
mUserId = parseInt(nextArgRequired());
nextArg = nextArgRequired();
}
mComponent = parseComponentName(nextArg);
|
private android.content.ComponentName | parseComponentName(java.lang.String component)
ComponentName cn = ComponentName.unflattenFromString(component);
if (cn == null) {
throw new IllegalArgumentException ("Invalid component " + component);
}
return cn;
|
private int | parseInt(java.lang.String argument)
try {
return Integer.parseInt(argument);
} catch (NumberFormatException e) {
throw new IllegalArgumentException ("Invalid integer argument '" + argument + "'", e);
}
|
private void | runSetActiveAdmin()
parseArgs(true);
mDevicePolicyManager.setActiveAdmin(mComponent, true /*refreshing*/, mUserId);
System.out.println("Success: Active admin set to component " + mComponent.toShortString());
|
private void | runSetDeviceOwner()
ComponentName component = parseComponentName(nextArgRequired());
mDevicePolicyManager.setActiveAdmin(component, true /*refreshing*/, UserHandle.USER_OWNER);
String packageName = component.getPackageName();
try {
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.
mDevicePolicyManager.removeActiveAdmin(component, UserHandle.USER_OWNER);
throw e;
}
System.out.println("Success: Device owner set to package " + packageName);
System.out.println("Active admin set to component " + component.toShortString());
|
private void | runSetProfileOwner()
// 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);
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("Success: Active admin and profile owner set to "
+ component.toShortString() + " for user " + userId);
|