14

A device owner can grant runtime permissions to a third-party app using DevicePolicyManager.setPermissionGrantState() to avoid user prompts.

But is there any way for a device owner to grant USB permissions as well, so that this app gets access to plugged USB devices without user prompt ?

I've tried to call UsbManager.grantPermission() (with reflection) but it raises a SecurityException since it requires the MANAGE_USB permission which is granted only to system apps (and not to device owner, obviously).

NB: I'm looking for a solution working on a non-root & non-custom Android system, the device owner app is set using Android Enterprise provisioning methods.

sdabet
  • 18,360
  • 11
  • 89
  • 158

2 Answers2

4

There is a special system config to disable USB permission dialogs: https://github.com/aosp-mirror/platform_frameworks_base/blob/8ff4a5a5b510e724b25ac00369696594bb0c9fdc/core/res/res/values/config.xml#L2283

Maybe you can also change it at runtime with root privileges using setprop.

Another way is to customize the UsbUserSettingsManager class, specifically this method: https://android.googlesource.com/platform/frameworks/base.git/+/master/services/usb/java/com/android/server/usb/UsbUserSettingsManager.java#178

I assumed that as a device owner you have full control over your ROM source code.

  • Unfortunately this is not a custom ROM and I don't have root access. The device owner app is set using standard [provisioning methods](https://developers.google.com/android/management/provision-device). Therefore I cannot use any of these solutions. – sdabet May 09 '19 at 18:48
  • Maybe your device owner app could declare the appropriate intent filter and react to plugging in of the USB device by calling grantDevicePermission() on UsbManager for a uid of the third party app which it can get in some other way? The idea is similar to what is done in onDestroy() here: https://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r1/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java – Wojciech Sadurski May 10 '19 at 08:55
  • I've just tried it, but unfortunately the [UsbManager.grantPermission()](https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/hardware/usb/UsbManager.java#601) methods require the [MANAGE_USB](https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/AndroidManifest.xml#L1700) permission which is only granted to system & privileged app, but not device owner apps. – sdabet May 10 '19 at 19:03
  • Did you manage to find any way to do it without OS source code ? – Андрій Пугач Oct 16 '19 at 19:02
  • Did anyone find an adb prop to toggle this config parameter? It looks like it is built in at build time and not exposed through ADB. – dazza5000 Nov 15 '19 at 00:45
0

It's a bit old, but hopefully it helps s.o. else. I've been using this for granting permission to apps

public boolean setPermissionGrantState (ComponentName admin, 
                String packageName, 
                String permission, 
                int grantState)

Link: https://developer.android.com/reference/android/app/admin/DevicePolicyManager#setPermissionGrantState(android.content.ComponentName,%20java.lang.String,%20java.lang.String,%20int)

Chinh Nguyen
  • 583
  • 1
  • 7
  • 14
  • 1
    AFAIK that doesn't work with USB permissions (I already mentioned this method in my question) – sdabet Dec 22 '20 at 14:37