Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grant USB permissions as device owner

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.

like image 494
sdabet Avatar asked May 02 '19 10:05

sdabet


2 Answers

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)

like image 189
Chinh Nguyen Avatar answered Oct 04 '22 20:10

Chinh Nguyen


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.

like image 25
Wojciech Sadurski Avatar answered Oct 04 '22 19:10

Wojciech Sadurski