# 設備狀態
## 類圖

枚舉 : 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對于處于執行任務的設備來說,比較重要的一種狀態。
- 前言
- (1)-windows下cts配置
- (2)-cts調試環境的搭建
- (3)-基礎庫tradefederation配置
- (4)-任務的添加
- (5)-9大組件配置
- (6)-任務的執行
- (7)-任務執行的調度室
- (8)-IBuildProvider
- (9)-IDeviceRecovery
- (10)-TestDeviceOptions
- (11)-ICommandOptions
- (12)-ITargetPreparer
- (13)-任務執行過程
- (14)-任務執行過程
- (15)-任務執行完
- (16)-logcat信息收集系統
- (17)-fastboot狀態監聽器
- (18)-設備恢復
- (19)-設備狀態的分類以及恢復模式的分類
- (20)-cts自身log系統
- (21)-測試結果收集系統
- (22)-自動檢測設備
- (23)-設備分類
- (24)-case的組織