Grant USB permissions as device owner
Asked Answered
B

2

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.

Bywoods answered 2/5, 2019 at 10:35 Comment(9)
Take a look here, if it helps: #13648047Hexagonal
@GauravMall Unfortunately all the suggested answers require to be a system appBywoods
Yeah, sorry, that detail just passed over my head. I don't know how. Did it help you, however?Hexagonal
@GauravMall Not really. Surprinsingly it feels like an impossible thing to do.Bywoods
Did you find a solution for it? Even UsbManager.grantPermission() (with reflection) deosn't work anymore on Android 9 with root system appBrownley
found solution for Android 9 https://mcmap.net/q/340966/-bypass-android-usb-host-permission-confirmation-dialog-on-android-9Brownley
@Brownley this solution requires root accessBywoods
@fiddler yes, I know. But it's the only possible solutionBrownley
@Brownley gives me an error: "W/System.err: java.lang.reflect.InvocationTargetException" in "grantDevicePermissionMethod.invoke(iUsbManager, usbDevice,appInfo.uid); "Yorgo
D
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.

Dogger answered 8/5, 2019 at 11:34 Comment(6)
Unfortunately this is not a custom ROM and I don't have root access. The device owner app is set using standard provisioning methods. Therefore I cannot use any of these solutions.Bywoods
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: android.googlesource.com/platform/frameworks/base/+/…Dogger
I've just tried it, but unfortunately the UsbManager.grantPermission() methods require the MANAGE_USB permission which is only granted to system & privileged app, but not device owner apps.Bywoods
Did you manage to find any way to do it without OS source code ?Eyesight
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.Corrody
One could use adb shell setprop persist.sys.usb.config_disableUsbPermissionDialogs true, but on my device it does not work....Kerplunk
B
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)

Blatt answered 22/12, 2020 at 9:40 Comment(1)
AFAIK that doesn't work with USB permissions (I already mentioned this method in my question)Bywoods

© 2022 - 2024 — McMap. All rights reserved.