翻看UsbManager源码,发现其构造方法有两个参数,一个是Context对象,一个是IUSBManger对象,IUsbManager是aidl接口,具体实现类是UsbService,UsbService的构造方法中会初始化UsbHostManger和UsbDeviceManager。
以UsbHostManager为例。UsbManager中getDeviceList()方法,实际是调用UsbHostManager中getDeviceList(Bundle b),这里的Bundle对象是UsbManager中getDeviceList()方法创建的,在UsbHostManager中对该Bundle对象赋值。
源码如下:
public void getDeviceList(Bundle devices) {
synchronized (mLock) {
for (String name : mDevices.keySet()) {
devices.putParcelable(name, mDevices.get(name));
}
}
}
HashMap, UsbDevice> mDevices; 该对象用来存储获取的USB设备。具体是如何给mDevices赋值,接着翻看源码。
系统启动后,会调用JNI 中monitorUsbHostBus()方法,来反馈usb设备,具体源码在frameworks\base\services\core\jni\com_android_server_UsbHostManager.cpp该类会调用UsbHostManager中的几个方法,以添加usb设备为例。
private UsbDevice mNewDevice;
private UsbConfiguration mNewConfiguration;
private UsbInterface mNewInterface;
private ArrayList mNewConfigurations;
private ArrayList mNewInterfaces;
private ArrayList mNewEndpoints;
private boolean beginUsbDeviceAdded(String deviceName, int vendorID, int productID,
int deviceClass, int deviceSubclass, int deviceProtocol,