USB 拔插广播
在开发中发现USB 在连接情况下,app 侦测 断开,因此需要添加log,查看系统是否有广播
目前 先使用host manager run 监控方式
1- 设备插入: 广播ACTION_USB_DEVICE_ATTACHED
2- 设备拔出: 广播ACTION_USB_DEVICE_DETACHED
/frameworks/base/core/java/android/hardware/usb/UsbManager.java
public static final String ACTION_USB_DEVICE_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
public static final String ACTION_USB_DEVICE_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java
流程:
UsbHostManager 启动监控线程
---》注册了endUsbDeviceAdded,usbDeviceRemoved 函数
---》 监控USB 插入后,调用endUsbDeviceAdded (拔出:usbDeviceRemoved)
---》 发送ACTION_USB_DEVICE_ATTACHED 广播 (拔出:ACTION_USB_DEVICE_DETACHED)
UsbHostManager 启动监控线程:
systemReady -> UsbHostManager.java
monitorUsbHostBus ->
android_server_UsbHostManager_monitorUsbHostBus -> android_server_UsbHostManager.cpp
usb_host_run -> Usbhost.c
usb_host_read_event ->
context->cb_added -> //有usb设备添加, 删除是cb_removed, 都是UsbHostManager 注册下来的
usb_device_added ->
env->CallVoidMethod -> //对方方法method_endUsbDeviceAdded --> endUsbDeviceAdded
endUsbDeviceAdded -> UsbHostManager.java
最后走到了:UsbHostManager :endUsbDeviceAdded
endUsbDeviceAdded -->
deviceAttached-->UsbProfileGroupSettingsManager
mContext.sendBroadcastAsUser-->发送ACTION_USB_DEVICE_ATTACHED
UsbHostManager.java: usbDeviceRemoved:
/* Called from JNI in monitorUsbHostBus to report USB device removal */
@SuppressWarnings("unused")
private void usbDeviceRemoved(String deviceName) {
synchronized (mLock) {
UsbDevice device = mDevices.remove(deviceName);
if (device != null) {
Slog.d(TAG, "usbDeviceRemoved:"+device.getProductName());
mUsbAlsaManager.usbDeviceRemoved(device);
mSettingsManager.usbDeviceRemoved(device);//-->发送ACTION_USB_DEVICE_DETACHED
getCurrentUserSettings().usbDeviceRemoved(device);
}
}
}
备注:UsbHostManager:systemReady 什么时候被调用
调用流程:
SystemServer (SystemServer.java)
--->UsbService (SystemServer.java)
--->startBootPhase (SystemServer.java)
--->startBootPhase (UsbService.java)
--->UsbHostManager :systemReady (UsbHostManager.java)
frameworks/base/services/java/com/android/server/SystemServer.java
private static final String USB_SERVICE_CLASS = "com.android.server.usb.UsbService$Lifecycle";
startOtherServices
-->startService(USB_SERVICE_CLASS);--->启动UsbService
--> startBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
-->service.onBootPhase(mCurrentPhase); --》启动所有service **onBootPhase** 函数
SystemServer.java:startBootPhase:
public void startBootPhase(final int phase) {
if (phase <= mCurrentPhase) {
throw new IllegalArgumentException("Next phase must be larger than previous");
}
mCurrentPhase = phase;
Slog.i(TAG, "Starting phase " + mCurrentPhase);
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "OnBootPhase " + phase);
final int serviceLen = mServices.size();
for (int i = 0; i < serviceLen; i++) {
final SystemService service = mServices.get(i);
long time = SystemClock.elapsedRealtime();
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, service.getClass().getName());
try {
service.onBootPhase(mCurrentPhase);
} catch (Exception ex) {
throw new RuntimeException("Failed to boot service "
+ service.getClass().getName()
+ ": onBootPhase threw an exception during phase "
+ mCurrentPhase, ex);
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onBootPhase");
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
frameworks/base/services/usb/java/com/android/server/usb/UsbService.java
UsbService.java:onBootPhase
-->mUsbService.systemReady();
-->mHostManager.systemReady();
UsbService.java:onBootPhase:
@Override
public void onBootPhase(int phase) {
if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
mUsbService.systemReady();
} else if (phase == SystemService.PHASE_BOOT_COMPLETED) {
mUsbService.bootCompleted();
}
}
UsbService.java:systemReady:
public void systemReady() {
mAlsaManager.systemReady();
if (mDeviceManager != null) {
mDeviceManager.systemReady();
}
if (mHostManager != null) {
mHostManager.systemReady();
}
if (mPortManager != null) {
mPortManager.systemReady();
}
}