Running dpm with Runtime.exec(...)
Asked Answered
R

2

5

This answer suggests that an Android app can run dpm like this:

Runtime.getRuntime().exec("dpm set-device-owner com.test.my_device_owner_app");

This fails silently on my Nexus 4 running 5.1.1. The shell returns an error code of 0 (success) and there is no console output. Despite the apparent success, my app does not become the device owner. The device is fresh from a factory reset, with no user account configured.

As a control, I tried running a garbage command instead of dpm. It fails as expected.

Did this ever work? Was it intentionally nerfed?

Rafferty answered 5/6, 2015 at 20:5 Comment(4)
In your linked answer - there are conditions listed in the comments - do they apply to your setup?Pallaten
@MorrisonChang I've done all those things, else I could not have installed the app that's trying to run this command.Rafferty
I have the same problem, though from the commandline using "adb shell ..." it works fine, but for some reason using Runtime.getRuntime().exec() doesn't workEigenfunction
this was my problem: #26696940Eigenfunction
R
2

dpm incorrectly exits with a status code of 0 when you get the command syntax wrong. The correct syntax is dpm set-device-owner package/.ComponentName. When you get the syntax right, exec(...) throws a SecurityException:

java.lang.SecurityException: Neither user 10086 nor current process has android.permission.MANAGE_DEVICE_ADMINS.
  at android.os.Parcel.readException(Parcel.java:1546)
  at android.os.Parcel.readException(Parcel.java:1499)
  at android.app.admin.IDevicePolicyManager$Stub$Proxy.setActiveAdmin(IDevicePolicyManager.java:2993)
  at com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:110)
  at com.android.commands.dpm.Dpm.onRun(Dpm.java:82)
  at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
  at com.android.commands.dpm.Dpm.main(Dpm.java:38)
  at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
  at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:249)

Adding this permission to the manifest does not help, so maybe it's a system-only permission.

It's already a pain in the butt to deploy a kiosk mode app on a device without NFC, since you have to enable developer mode and install the app via adb. I guess the provisioner will just have to run dpm manually.

Rafferty answered 5/6, 2015 at 20:37 Comment(3)
When I call from the commandline "adb shell dpm set-device-owner BLAH" things work fine (my app becomes device owner - I don't get a security exception because my app is a system app), but when I call from my program "Runtime.getRuntime().exec("dpm set-device-owner BLAH");" I get a silent failure (not a SecurityException, though I wish I got some feedback). Any idea why?Eigenfunction
@Eigenfunction I'd guess that Google considers it a security risk.Rafferty
this was the answer: #26696940Eigenfunction
E
0

As some extra info, I was able to capture and log the output (stdout and stderr) to logcat

DevicePolicyManager dpm = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Runtime rt = Runtime.getRuntime();
Process proc = null;
try {
    proc = rt.exec("dpm set-device-owner com.myapp/.DeviceOwnerReceiver");
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));

    BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

    // Read the output from the command
    System.out.println("Here is the standard output of the command:\n");
    String s = null;
    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);
    }

    // Read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):\n");
    while ((s = stdError.readLine()) != null) {
        System.out.println(s);
    }
} catch (IOException e) {
    e.printStackTrace();
}


Elissaelita answered 11/1, 2021 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.