🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 设备状态 ## 类图 ![](https://box.kancloud.cn/2016-01-09_56911ddd3d7a4.jpg) 枚举 : TestDeviceState,其实是adb中DeviceState扩展而来。 1.FASTBOOT:线刷状态(根据fastboot监听器获得经过设置) 2.ONLINE:在线状态(根据DeviceState值转化而来) 3.OFFLINE:离线状态(根据DeviceState值转化而来) 4.RECOVERY:卡刷状态(根据DeviceState值转化而来) 5.NOT_AVAILABLE:不可用状态(根据情况不同手动设置) 枚举:RecoveryMode,恢复模式。在进行设备恢复的时候,会先判断该设备的恢复模式。 1.NONE:该设备不进行设备恢复 2.ONLINE:该设备需要恢复到online状态 3.AVAILABLE:该设备需要恢复到可用状态 ## 理解 1.最需要理解是TestDeviceState.NOT_AVAILABLE状态: 一般情况下用adb devices没有获得该设备的任何状态,但是程序知道设备肯定是存在的。这个时候就可以判断该设备是处于NOT_AVAILABLE状态。 2.TestDeviceState.OFFLINE的状态和TestDeviceState.NOT_AVAILABLE的区别: OFFLINE是离线状态,但是这种离线adb devices是可以检测到的,这个时候设备是有反馈的。 但是NOT_AVAILABLE是adb devices无法得到的,这个时候压根就不理睬你。 比如QQ中的离线提醒和下线的区别,大家一下子就明白了。离线状态好比TestDeviceState.OFFLINE,有时候可能会给你恢复,提示该用户暂时不在线。 下线就好比TestDeviceState.NOT_AVAILABLE。 3.TestDeviceState.ONLINE和RecoveryMode.ONLINE区别: TestDeviceState.ONLINE是一种状态的分类,而RecoveryMode.ONLINE是在设备离线后,设备恢复要达到的一种目标的分类。当设备处于TestDeviceState.OFFLINE的时候或者TestDeviceState.NOT_AVAILABLE的时候,它就要调用ITestRecovery来恢复设备,那么RecoveryMode就定义了,该设备恢复的目标。ITestRecovery中的方法执行的时候,会先判断要恢复到什么状态。然后才会做相应的工作。 ## 代码 ### TestDeviceState ~~~ public enum TestDeviceState { FASTBOOT, ONLINE, OFFLINE, RECOVERY, NOT_AVAILABLE; /** * Converts from {@link TestDeviceState} to {@link DeviceState} * @return the {@link DeviceState} or <code>null</code> */ DeviceState getDdmsState() { switch (this) { case ONLINE: return DeviceState.ONLINE; case OFFLINE: return DeviceState.OFFLINE; case RECOVERY: return DeviceState.RECOVERY; default: return null; } } /** * Returns the {@link TestDeviceState} corresponding to the {@link DeviceState}. */ static TestDeviceState getStateByDdms(DeviceState ddmsState) { if (ddmsState == null) { return TestDeviceState.NOT_AVAILABLE; } switch (ddmsState) { case ONLINE: return TestDeviceState.ONLINE; case OFFLINE: return TestDeviceState.OFFLINE; case RECOVERY: return TestDeviceState.RECOVERY; } return TestDeviceState.NOT_AVAILABLE; } } ~~~ ### RecoveryMode ~~~ public enum RecoveryMode { /** don't attempt to recover device. */ NONE, /** recover device to online state only */ ONLINE, /** * Recover device into fully testable state - framework is up, and external storage is * mounted. */ AVAILABLE } ~~~ ## 重点理解 上面说了TestDeviceState.NOT_AVAILABLE很特殊,那么下面就来看看哪些场景下设备状态被设置成了NOT_AVAILABLE 1.DeviceManager.createTestDevice() ~~~ IManagedTestDevice createTestDevice(IDevice allocatedDevice, IDeviceStateMonitor monitor) { IManagedTestDevice testDevice = new TestDevice(allocatedDevice, monitor); testDevice.setFastbootEnabled(mFastbootEnabled); if (allocatedDevice instanceof FastbootDevice) { testDevice.setDeviceState(TestDeviceState.FASTBOOT); } else if (allocatedDevice instanceof StubDevice) { testDevice.setDeviceState(TestDeviceState.NOT_AVAILABLE); } return testDevice; } ~~~ 当设备属于虚拟设备的时候,也设置该设备为NOT_AVAILABLE状态。 2.DeviceManager的私有类ManagedDeviceListener.deviceDisconnected() ~~~ public void deviceDisconnected(IDevice disconnectedDevice) { if (mAvailableDeviceQueue.remove(disconnectedDevice)) { CLog.i("Removed disconnected device %s from available queue", disconnectedDevice.getSerialNumber()); } IManagedTestDevice testDevice = mAllocatedDeviceMap.get(disconnectedDevice.getSerialNumber()); if (testDevice != null) { testDevice.setDeviceState(TestDeviceState.NOT_AVAILABLE); } else if (mCheckDeviceMap.containsKey(disconnectedDevice.getSerialNumber())) { IDeviceStateMonitor monitor = mCheckDeviceMap.get(disconnectedDevice.getSerialNumber()); monitor.setState(TestDeviceState.NOT_AVAILABLE); } updateDeviceMonitor(); } ~~~ 当adb监听到有设备断线的时候,会判断该设备是否处于已分配或者已检测的设备列表中,则设置其状态为NOT_AVAILABLE。 3.DeviceManager的私有类FastbootMonitor.run() ~~~ synchronized (mAllocatedDeviceMap) { for (IManagedTestDevice testDevice : mAllocatedDeviceMap.values()) { if (!serials.contains(testDevice.getSerialNumber()) && testDevice.getDeviceState().equals(TestDeviceState.FASTBOOT)) { testDevice.setDeviceState(TestDeviceState.NOT_AVAILABLE); } } } ~~~ 当处于已分配(就是正在执行任务)的任务列表中的设备被检测出来处于fastboot状态,这个时候就要将设备状态设置成NOT_AVAILABLE。 所以说,NOT_AVAILABLE对于处于执行任务的设备来说,比较重要的一种状态。