/frameworks/base/services/java_opt/com/android/server/SystemServer.java
private static final String USB_SERVICE_CLASS = “com.android.server.usb.UsbService$Lifecycle”;
mSystemServiceManager.startService(USB_SERVICE_CLASS);
开启UsbService的内部类LifeCycle,内部类中有两个方法:onStart优先执行,onBootPhase后执行???
原因在文件SystemServiceManager.java中
/frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
startService
mServices.add(service);
service.onStart();(Java的回调,看这个时候就调用了onStart方法)
startBootPhase
service.onBootPhase(mCurrentPhase);
(调用onBootPhase方法,这个startBootPhase在哪儿调用呢?)发现等系统某个Service启动完成后,就会调用
/frameworks/base/services/usb/java/com/android/server/usb/UsbService.java
UsbService构造方法:新建以下的类,其中最为重要的是UsbHostManager
UsbHostManager
UsbDeviceManager
UsbPortManager
frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java
systemReady(在UsbService里面执行mUsbService.systemReady中调用)
<font style="color:rgb(255, 0, 0);">monitorUsbHostBus(</font>) native方法,jni部分完成大部分功能
/frameworks/base/services/core/jni/com_android_server_UsbHostManager.cpp
android_server_UsbHostManager_monitorUsbHostBus
usb_host_init
**<font style="color:rgb(255, 0, 0);">inotify_init (linux inotify机制,这个是监听某个目录下文件的所有事件)</font>**
usb_host_run(context, <font style="color:rgb(255, 0, 0);">usb_device_added</font>, <font style="color:rgb(255, 0, 0);">usb_device_removed</font>, NULL, (void *)thiz); (这个是和内核交互的代码)
两个回调指针,设备添加时候调用usb_device_added,设备移除时候调用usb_device_removed
/system/core/libusbhost/usbhost.c
usb_host_run
usb_host_load
inotify_add_watch
watch_existing_subdirs
find_existing_devices
经过层层调用,最后在设备增加时候context->cb_added,设备移除之后context->cb_removed
usb_host_read_event
回到/frameworks/base/services/core/jni/com_android_server_UsbHostManager.cpp
usb_device_added (设备增加时的回调,和上面的context->cb_added对应)
method_beginUsbDeviceAdded
method_addUsbConfiguration
method_addUsbInterface
method_endUsbDeviceAdded 四个JNI回调系统办法,这个时候就跑到Java层了
usb_device_removed (设备移除时的回调,和上面的context->cb__removed对应)
method_usbDeviceRemoved
/frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java
beginUsbDeviceAdded new UsbDevice
addUsbConfiguration new UsbConfiguration
addUsbInterface new UsbInterface
addUsbEndpoint new UsbEndpoint
endUsbDeviceAdded mDevices.put(mNewDevice.getDeviceName(), mNewDevice)把上面的值都保存起来,供app调用
参考:
https://www.jianshu.com/p/51809085e9cc
https://blog.csdn.net/u014742281/article/details/89328518#androidusb__2