代码路径 :frameworks\base\services\usb\java\com\android\server\usb\UsbHostManager.java
1.添加usb设备
/* Called from JNI in monitorUsbHostBus() to report new USB devices Returns true if successful, i.e. the USB Audio device descriptors are correctly parsed and the unique device is added to the audio device list. */ @SuppressWarnings("unused") private boolean usbDeviceAdded(String deviceAddress, int deviceClass, int deviceSubclass, byte[] descriptors) { if (DEBUG) { Slog.d(TAG, "usbDeviceAdded(" + deviceAddress + ") - start"); } if (isDenyListed(deviceAddress)) { if (DEBUG) { Slog.d(TAG, "device address is Deny listed"); } return false; } if (isDenyListed(deviceClass, deviceSubclass)) { if (DEBUG) { Slog.d(TAG, "device class is deny listed"); } return false; } UsbDescriptorParser parser = new UsbDescriptorParser(deviceAddress, descriptors); if (deviceClass == UsbConstants.USB_CLASS_PER_INTERFACE && !checkUsbInterfacesDenyListed(parser)) { return false; } // Potentially can block as it may read data from the USB device. logUsbDevice(parser); synchronized (mLock) { if (mDevices.get(deviceAddress) != null) { Slog.w(TAG, "device already on mDevices list: " + deviceAddress); //TODO If this is the same peripheral as is being connected, replace // it with the new connection. return false; } UsbDevice.Builder newDeviceBuilder = parser.toAndroidUsbDeviceBuilder(); if (newDeviceBuilder == null) { Slog.e(TAG, "Couldn't create UsbDevice object."); // Tracking addConnectionRecord(deviceAddress, ConnectionRecord.CONNECT_BADDEVICE, parser.getRawDescriptors()); } else { UsbSerialReader serialNumberReader = new UsbSerialReader(mContext, mPermissionManager, newDeviceBuilder.serialNumber); UsbDevice newDevice = newDeviceBuilder.build(serialNumberReader); serialNumberReader.setDevice(newDevice); mDevices.put(deviceAddress, newDevice); Slog.d(TAG, "Added device " + newDevice); // It is fine to call this only for the current user as all broadcasts are // sent to all profiles of the user and the dialogs should only show once. ComponentName usbDeviceConnectionHandler = getUsbDeviceConnectionHandler(); if (usbDeviceConnectionHandler == null) { getCurrentUserSettings().deviceAttached(newDevice); } else { getCurrentUserSettings().deviceAttachedForFixedHandler(newDevice, usbDeviceConnectionHandler); } mUsbAlsaManager.usbDeviceAdded(deviceAddress, newDevice, parser); // Tracking addConnectionRecord(deviceAddress, ConnectionRecord.CONNECT, parser.getRawDescriptors()); // Stats collection FrameworkStatsLog.write(FrameworkStatsLog.USB_DEVICE_ATTACHED, newDevice.getVendorId(), newDevice.getProductId(), parser.hasAudioInterface(), parser.hasHIDInterface(), parser.hasStorageInterface(), FrameworkStatsLog.USB_DEVICE_ATTACHED__STATE__STATE_CONNECTED, 0); } } if (DEBUG) { Slog.d(TAG, "beginUsbDeviceAdded(" + deviceAddress + ") end"); } return true; }
2.拒绝添加的usb设备
private boolean isDenyListed(String deviceAddress) { int count = mHostDenyList.length; for (int i = 0; i < count; i++) { if (deviceAddress.startsWith(mHostDenyList[i])) { return true; } } return false; } /* returns true if the USB device should not be accessible by applications */ private boolean isDenyListed(int clazz, int subClass) { // deny hubs if (clazz == UsbConstants.USB_CLASS_HUB) return true; // deny HID boot devices (mouse and keyboard) return clazz == UsbConstants.USB_CLASS_HID && subClass == UsbConstants.USB_INTERFACE_SUBCLASS_BOOT; }
3.构造方法
/* * UsbHostManager */ public UsbHostManager(Context context, UsbAlsaManager alsaManager, UsbPermissionManager permissionManager) { mContext = context; mHostDenyList = context.getResources().getStringArray( com.android.internal.R.array.config_usbHostDenylist); mUsbAlsaManager = alsaManager; mPermissionManager = permissionManager; String deviceConnectionHandler = context.getResources().getString( com.android.internal.R.string.config_UsbDeviceConnectionHandling_component); if (!TextUtils.isEmpty(deviceConnectionHandler)) { setUsbDeviceConnectionHandler(ComponentName.unflattenFromString( deviceConnectionHandler)); } }
4.config_usbHostDenylist frameworks\base\core\res\res\values\config.xml
<!-- List of file paths for USB host busses to exclude from USB host support. For example, if the first USB bus on the device is used to communicate with the modem or some other restricted hardware, add "/dev/bus/usb/001/" to this list. If this is empty, no parts of the host USB bus will be excluded. --> <string-array name="config_usbHostDenylist" translatable="false"> </string-array>