Bypass Android Usb Host Permission Confirmation Dialog For Android 5.1
Solution 1:
It's been a while since you asked this... but in case it can help someone here is my answer.
The accepted answer in the initial question states:
So if you have another version of Android and this code doesn't work you will have to check the source code for your particular version of OS.
So you should get the files you need directly from the android source code. You can download the source code relative to your version or browse directly from the repo.
The IUsbManager interface you are searching for is normally under: /frameworks/base/android-branch-name/core/java/android/hardware/usb. As for the Service Manager it can be found under: /frameworks/base/android-branch-name/core/java/android/os/
I didn't post the code since I suppose you're not searching for it anymore after 2 years+ :)
=== EDIT ===
As asked, here is the code. I made it work for version 6.0.0, but I think functions calls are the same as 5.1. To be verified.
First, here is the android project structure you will get:
Create the interface IUsbManager.java in android.harware.usb:
package android.hardware.usb;
publicinterfaceIUsbManagerextendsandroid.os.IInterface
{
/** Local-side IPC implementation stub class. */publicstaticabstractclassStubextendsandroid.os.Binder implementsandroid.hardware.usb.IUsbManager
{
/** Construct the stub at attach it to the interface. */publicStub()
{
thrownewRuntimeException( "Stub!" );
}
/**
* Cast an IBinder object into an android.hardware.usb.IUsbManager interface,
* generating a proxy if needed.
*/publicstatic android.hardware.usb.IUsbManager asInterface( android.os.IBinder obj )
{
thrownewRuntimeException( "Stub!" );
}
public android.os.IBinder asBinder()
{
thrownewRuntimeException( "Stub!" );
}
publicbooleanonTransact( int code, android.os.Parcel data, android.os.Parcel reply, int flags )throws android.os.RemoteException
{
thrownewRuntimeException( "Stub!" );
}
staticfinalintTRANSACTION_getDeviceList= (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
staticfinalintTRANSACTION_openDevice= (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
staticfinalintTRANSACTION_getCurrentAccessory= (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
staticfinalintTRANSACTION_openAccessory= (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
staticfinalintTRANSACTION_setDevicePackage= (android.os.IBinder.FIRST_CALL_TRANSACTION + 4);
staticfinalintTRANSACTION_setAccessoryPackage= (android.os.IBinder.FIRST_CALL_TRANSACTION + 5);
staticfinalintTRANSACTION_hasDevicePermission= (android.os.IBinder.FIRST_CALL_TRANSACTION + 6);
staticfinalintTRANSACTION_hasAccessoryPermission= (android.os.IBinder.FIRST_CALL_TRANSACTION + 7);
staticfinalintTRANSACTION_requestDevicePermission= (android.os.IBinder.FIRST_CALL_TRANSACTION + 8);
staticfinalintTRANSACTION_requestAccessoryPermission= (android.os.IBinder.FIRST_CALL_TRANSACTION + 9);
staticfinalintTRANSACTION_grantDevicePermission= (android.os.IBinder.FIRST_CALL_TRANSACTION + 10);
staticfinalintTRANSACTION_grantAccessoryPermission= (android.os.IBinder.FIRST_CALL_TRANSACTION + 11);
staticfinalintTRANSACTION_hasDefaults= (android.os.IBinder.FIRST_CALL_TRANSACTION + 12);
staticfinalintTRANSACTION_clearDefaults= (android.os.IBinder.FIRST_CALL_TRANSACTION + 13);
staticfinalintTRANSACTION_setCurrentFunction= (android.os.IBinder.FIRST_CALL_TRANSACTION + 14);
staticfinalintTRANSACTION_setMassStorageBackingFile= (android.os.IBinder.FIRST_CALL_TRANSACTION + 15);
}
/* Returns a list of all currently attached USB devices */publicvoidgetDeviceList( android.os.Bundle devices )throws android.os.RemoteException;
/* Returns a file descriptor for communicating with the USB device.
* The native fd can be passed to usb_device_new() in libusbhost.
*/public android.os.ParcelFileDescriptor openDevice( java.lang.String deviceName )throws android.os.RemoteException;
/* Returns the currently attached USB accessory */public android.hardware.usb.UsbAccessory getCurrentAccessory()throws android.os.RemoteException;
/* Returns a file descriptor for communicating with the USB accessory.
* This file descriptor can be used with standard Java file operations.
*/public android.os.ParcelFileDescriptor openAccessory( android.hardware.usb.UsbAccessory accessory )throws android.os.RemoteException;
/* Sets the default package for a USB device
* (or clears it if the package name is null)
*/publicvoidsetDevicePackage(android.hardware.usb.UsbDevice device, java.lang.String packageName, int userId)throws android.os.RemoteException;
/* Sets the default package for a USB accessory
* (or clears it if the package name is null)
*/publicvoidsetAccessoryPackage( android.hardware.usb.UsbAccessory accessory, java.lang.String packageName )throws android.os.RemoteException;
/* Returns true if the caller has permission to access the device. */publicbooleanhasDevicePermission(android.hardware.usb.UsbDevice device)throws android.os.RemoteException;
/* Returns true if the caller has permission to access the accessory. */publicbooleanhasAccessoryPermission( android.hardware.usb.UsbAccessory accessory )throws android.os.RemoteException;
/* Requests permission for the given package to access the device.
* Will display a system dialog to query the user if permission
* had not already been given.
*/publicvoidrequestDevicePermission( android.hardware.usb.UsbDevice device, java.lang.String packageName, android.app.PendingIntent pi )throws android.os.RemoteException;
/* Requests permission for the given package to access the accessory.
* Will display a system dialog to query the user if permission
* had not already been given. Result is returned via pi.
*/publicvoidrequestAccessoryPermission( android.hardware.usb.UsbAccessory accessory, java.lang.String packageName, android.app.PendingIntent pi )throws android.os.RemoteException;
/* Grants permission for the given UID to access the device */publicvoidgrantDevicePermission( android.hardware.usb.UsbDevice device, int uid )throws android.os.RemoteException;
/* Grants permission for the given UID to access the accessory */publicvoidgrantAccessoryPermission( android.hardware.usb.UsbAccessory accessory, int uid )throws android.os.RemoteException;
/* Returns true if the USB manager has default preferences or permissions for the package */publicbooleanhasDefaults( java.lang.String packageName )throws android.os.RemoteException;
/* Clears default preferences and permissions for the package */publicvoidclearDefaults( java.lang.String packageName )throws android.os.RemoteException;
/* Sets the current USB function. */publicvoidsetCurrentFunction( java.lang.String function, boolean makeDefault )throws android.os.RemoteException;
/* Sets the file path for USB mass storage backing file. */publicvoidsetMassStorageBackingFile( java.lang.String path )throws android.os.RemoteException;
}
Then create the java class ServiceManager.java in android.os:
package android.os;
import java.util.Map;
publicfinalclassServiceManager{
publicstatic IBinder getService( String name )
{
thrownewRuntimeException( "Stub!" );
}
/**
* Place a new @a service called @a name into the service
* manager.
*
* @param name the name of the new service
* @param service the service object
*/publicstaticvoid addService( String name, IBinder service )
{
thrownewRuntimeException( "Stub!" );
}
/**
* Retrieve an existing service called @a name from the
* service manager. Non-blocking.
*/publicstatic IBinder checkService( String name )
{
thrownewRuntimeException( "Stub!" );
}
publicstaticString[] listServices() throws RemoteException
{
thrownewRuntimeException( "Stub!" );
}
/**
* This is only intended to be called when the process is first being brought
* up and bound by the activity manager. There is only one thread in the process
* at that time, so no locking is done.
*
* @param cache the cache of service references
* @hide
*/publicstaticvoid initServiceCache( Map<String, IBinder> cache )
{
thrownewRuntimeException( "Stub!" );
}
}
Once this is done, don't forget to add the android.permission.MANAGE_USB in you AndroidManifest.
Then you can use those function calls:
/**
* Verify if the application is a system app and has MANAGE_USB permission
* before granting the USB permission for you specific USB devices
*/privatevoidmanageUSBPermissions() {
if ((this.getApplicationInfo().flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Log.i(TAG,"This is a system application");
if (getApplicationContext().checkCallingOrSelfPermission("android.permission.MANAGE_USB") == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG,"I have android.permission.MANAGE_USB");
grantUsbPermissions();
} else {
Log.i(TAG,"I do not have android.permission.MANAGE_USB");
}
} else {
Log.i(TAG,"This is not a system application");
}
}
/**
* This is to avoid the android usb host permission confirmation dialog
* The application need to be a system app and have MANAGE_USB permission for it to work
*/privatevoidgrantUsbPermissions() {
try {
PackageManagerpm= getPackageManager();
ApplicationInfoai= pm.getApplicationInfo( "com.your.package", 0 );
if( ai != null ) {
UsbManagermanager= (UsbManager) getSystemService( Context.USB_SERVICE );
IBinderb= ServiceManager.getService( Context.USB_SERVICE );
IUsbManagerservice= IUsbManager.Stub.asInterface( b );
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while( deviceIterator.hasNext() ) {
UsbDevicedevice= deviceIterator.next();
if ( device.getVendorId() == 0x1234 ) {
service.grantDevicePermission( device, ai.uid );
service.setDevicePackage( device, "com.your.package", ai.uid );
}
}
}
}
catch ( Exception e ) {
Log.e(TAG, "Error granting USB permissions: " + e);
}
}
There's a check on whether your application is a system application and if it has the correct permission, otherwise it would not work.
Also be aware that your vendor id is not in hexadecimal but in decimal.
Solution 2:
Have you tried using an intent-filter in the Manifest as described here: https://developer.android.com/guide/topics/connectivity/usb/host.html#using-intents
I had a similar issue - it seems like if you programmatically request a USB permission, it will ignore the ticked checkbox and ask you every time again.
Edit: If you are having issues because of the service, you might want to read this too: https://stackoverflow.com/a/15151075/3540885
Post a Comment for "Bypass Android Usb Host Permission Confirmation Dialog For Android 5.1"