Tuesday, September 17, 2019

usb on the go - How to disable USB OTG on Android 6+?


I need to disable the OTG (USB) on Android 8. I already did it on Android 6 by disabling a permission on the file android.hardware.usb.host.xml but it is not working anymore on Android 8.1.




Answer



android.hardware.usb.host.xml file - located under /vendor/etc/permissions/ or /system/etc/permissions/ before Treble - contains feature name="android.hardware.usb.host" which indicates that the device can communicate with USB devices as the USB host. This feature is evaluated by PackageManager (along with other software / hardware features) and used by Google Play to filter (in)compatible apps based on the value in AndroidManifest.xml. See details here.


Also if the device doesn't support USB host mode, UsbHostManager service is not started, so apps like this which use USB host APIs cannot connect to USB devices. But any connected device is still detected by kernel and appears under /sys and /dev.


To confirm if the device supports USB host mode:


~$ pm list features | grep usb.host
feature:android.hardware.usb.host

~$ dumpsys usb | grep host_manager
host_manager={


These lines shouldn't appear after you delete android.hardware.usb.host.xml file.




However since Android 6 USB storage devices such as a USB stick are handled by vold, which doesn't depend on the feature discussed above. vold listens to kernel through a netlink socket for block device uevent (1) and matches that with some entry in fstab (2). Then it creates disk and partition nodes in /dev/block/vold/ (3), checks filesystem for errors and mounts in /mnt/media_rw/ (also emulates SD cards in /mnt/runtime/) (4). After that ExternalStorageProvider (a content provider) is triggered which grants apps URI access to OTG storage through SAF or Scoped Directory Access.


So in order to disable auto-mounting of USB storage devices by vold, you need to remove the corresponding entry from fstab as referenced here:


/devices/*/xhci-hcd.0.auto/usb*    auto    auto    defaults    voldmanaged=usb:auto

fstab is found under /odm/etc/ or /vendor/etc/. Before Treble it was in rootfs (/). See details in this answer.


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...