Bypass android usb host permission confirmation dialog on Android 9

一曲冷凌霜 提交于 2019-11-26 21:30:29

问题


Before Android 9 I could bypass android usb host permission confirmation dialog by using root, systemizing my app and using next usb classes https://stackoverflow.com/a/15378118/7767664 https://stackoverflow.com/a/19681849/7767664

But it doesn't work for newest Android version - 9

It throws java.lang.NoSuchMethodError: No interface method grantDevicePermission(Landroid/hardware/usb/UsbDevice;I)V in class Landroid/hardware/usb/IUsbManager; or its super classes (declaration of 'android.hardware.usb.IUsbManager' appears in /system/framework/framework.jar)

fun setUsbPermissionRoot(device: UsbDevice) : Boolean {
    if (BuildConfig.DEBUG) Log.i(TAG, "trying set permission")
    try {
        val pm = App.context.packageManager
        val ai = pm.getApplicationInfo(App.context.packageName, 0)
        ai?.let {
            val b = ServiceManager.getService(Context.USB_SERVICE)
            val service = IUsbManager.Stub.asInterface(b)
            service.grantDevicePermission(device, it.uid)
            try {
                service.setDevicePackage(device, App.context.packageName, it.uid)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            if (BuildConfig.DEBUG) Log.i(TAG, "permission was set usb device")
            return true
        }
    } catch (e: PackageManager.NameNotFoundException) {
        if (BuildConfig.DEBUG) e.printStackTrace()
    } catch (e: RemoteException) {
        if (BuildConfig.DEBUG) e.printStackTrace()
    }
    return false
}

Is there any way to make it work on Android 9?


回答1:


We can solve it by entering:

adb shell
su
settings put global hidden_api_policy_pre_p_apps  1
settings put global hidden_api_policy_p_apps 1

Restrictions on non-SDK interfaces (Android 9): https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces

And then grantDevicePermission method will be available again through reflection on Android 9:

public static boolean grantAutomaticUsbPermissionRoot(Context context, UsbDevice usbDevice) {
    try {
        PackageManager pkgManager = context.getPackageManager();
        ApplicationInfo appInfo = pkgManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

        Class serviceManagerClass = Class.forName("android.os.ServiceManager");
        Method getServiceMethod = serviceManagerClass.getDeclaredMethod("getService", String.class);
        getServiceMethod.setAccessible(true);
        android.os.IBinder binder = (android.os.IBinder)getServiceMethod.invoke(null, Context.USB_SERVICE);

        Class iUsbManagerClass = Class.forName("android.hardware.usb.IUsbManager");
        Class stubClass = Class.forName("android.hardware.usb.IUsbManager$Stub");
        Method asInterfaceMethod = stubClass.getDeclaredMethod("asInterface", android.os.IBinder.class);
        asInterfaceMethod.setAccessible(true);
        Object iUsbManager=asInterfaceMethod.invoke(null, binder);

        final Method grantDevicePermissionMethod = iUsbManagerClass.getDeclaredMethod("grantDevicePermission", UsbDevice.class, int.class);
        grantDevicePermissionMethod.setAccessible(true);
        grantDevicePermissionMethod.invoke(iUsbManager, usbDevice,appInfo.uid);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

p.s. you need root of course and systemize your app (move to /system/priv-app/)



来源:https://stackoverflow.com/questions/57390558/bypass-android-usb-host-permission-confirmation-dialog-on-android-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!