<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Fastboot狀態監聽器 ## 類圖 涉及1個監聽器接口,2個實現類,1個執行線程。 ![](https://box.kancloud.cn/2016-01-09_56911ddc404eb.jpg) ## 解釋 這些類實際運用的是觀察者模式,所有的Listener都會被添加到Set中,然后執行線程FastbootMonitor每隔5秒鐘運行一次,來檢索目前處于fastboot狀態下的設備,然后更新設備狀態后,通知所有的listener,逐個調用listener的stateUpdated方法通知到各個listener。 ## 代碼 監聽器接口IFastbootListener: ~~~ public static interface IFastbootListener { /** * Callback when fastboot state has been updated for all devices. */ public void stateUpdated(); } ~~~ 2個實現類,都是DeviceStateMonitor類的私有類。 NotifyFastbootListener:通過Object類的notify通知在該對象上擁有鎖的且處于wait狀態的對象,你可以往下執行了。 ~~~ private static class NotifyFastbootListener implements IFastbootListener { @Override public void stateUpdated() { synchronized (this) { notify(); } } } ~~~ StubFastbootListener:不做任何事 ~~~ private static class StubFastbootListener implements IFastbootListener { @Override public void stateUpdated() { // ignore } } ~~~ 線程類FastbootMonitor,DeviceManager的私有類。 在該線程run方法中: 1.首先會通過cmd命令“fastboot devices”的執行結果,解析出處于fastboot的設備的SN號。 2.然后判斷可分配的設備中是否含有這些設備,如果有,需要更新這些設備的狀態至FASTBOOT。 3.接著要判斷已分配設備中是否含有這些設備,如果有,需要更新這些設備的狀態值NOT_AVAILABLE。(至于為什么和第2項中更新的不一樣,以后會講)。 4.通知所有監聽器,我已更新完當次的fastboot狀態。 ~~~ private class FastbootMonitor extends Thread { private boolean mQuit = false; FastbootMonitor() { super("FastbootMonitor"); } public void terminate() { mQuit = true; interrupt(); } @Override public void run() { while (!mQuit) { // only poll fastboot devices if there are listeners, as polling // it // indiscriminately can cause fastboot commands to hang if (!mFastbootListeners.isEmpty()) { Set<String> serials = getDevicesOnFastboot(); if (serials != null) { for (String serial : serials) { IManagedTestDevice testDevice = mAllocatedDeviceMap.get(serial); if (testDevice != null && !testDevice.getDeviceState().equals(TestDeviceState.FASTBOOT)) { testDevice.setDeviceState(TestDeviceState.FASTBOOT); } } // now update devices that are no longer on fastboot synchronized (mAllocatedDeviceMap) { for (IManagedTestDevice testDevice : mAllocatedDeviceMap.values()) { if (!serials.contains(testDevice.getSerialNumber()) && testDevice.getDeviceState().equals(TestDeviceState.FASTBOOT)) { testDevice.setDeviceState(TestDeviceState.NOT_AVAILABLE); } } } // create a copy of listeners for notification to // prevent deadlocks Collection<IFastbootListener> listenersCopy = new ArrayList<IFastbootListener>(mFastbootListeners.size()); listenersCopy.addAll(mFastbootListeners); for (IFastbootListener listener : listenersCopy) { listener.stateUpdated(); } } } getRunUtil().sleep(FASTBOOT_POLL_WAIT_TIME); } } } private Set<String> getDevicesOnFastboot() { CommandResult fastbootResult = getRunUtil().runTimedCmd(FASTBOOT_CMD_TIMEOUT, "fastboot", "devices"); if (fastbootResult.getStatus().equals(CommandStatus.SUCCESS)) { CLog.v("fastboot devices returned\n %s", fastbootResult.getStdout()); return parseDevicesOnFastboot(fastbootResult.getStdout()); } else { CLog.w("'fastboot devices' failed. Result: %s, stderr: %s", fastbootResult.getStatus(), fastbootResult.getStderr()); } return null; } static Set<String> parseDevicesOnFastboot(String fastbootOutput) { Set<String> serials = new HashSet<String>(); Pattern fastbootPattern = Pattern.compile("([\\w\\d]+)\\s+fastboot\\s*"); Matcher fastbootMatcher = fastbootPattern.matcher(fastbootOutput); while (fastbootMatcher.find()) { serials.add(fastbootMatcher.group(1)); } return serials; } } ~~~ ## 觀察者 哪些地方用到了這個狀態監聽器呢。也就是那些地方注冊成為了觀察者。很簡單,在DeviceManager類中找到addFastbootListener方法: ![](https://box.kancloud.cn/2016-01-09_56911ddc54166.jpg) 右鍵彈出對話框,點擊 ![](https://box.kancloud.cn/2016-01-09_56911ddc8971e.jpg) 就會找到有哪些地方用到了這個監聽器。 ![](https://box.kancloud.cn/2016-01-09_56911ddcec428.jpg) 都在DeviceStateMonitor,其實想想也對,上面監聽器的2個實現類都是DeviceStateMonitor的私有類,別人也不能用。 ~~~ /** * {@inheritDoc} */ @Override public boolean waitForDeviceBootloader(long time) { if (!mFastbootEnabled) { return false; } long startTime = System.currentTimeMillis(); // ensure fastboot state is updated at least once waitForDeviceBootloaderStateUpdate(); long elapsedTime = System.currentTimeMillis() - startTime; IFastbootListener listener = new StubFastbootListener(); mMgr.addFastbootListener(listener); long waitTime = time - elapsedTime; if (waitTime < 0) { // wait at least 200ms waitTime = 200; } boolean result = waitForDeviceState(TestDeviceState.FASTBOOT, waitTime); mMgr.removeFastbootListener(listener); return result; } ~~~ ~~~ @Override public void waitForDeviceBootloaderStateUpdate() { if (!mFastbootEnabled) { return; } IFastbootListener listener = new NotifyFastbootListener(); synchronized (listener) { mMgr.addFastbootListener(listener); try { listener.wait(); } catch (InterruptedException e) { Log.w(LOG_TAG, "wait for device bootloader state update interrupted"); } } mMgr.removeFastbootListener(listener); } ~~~ ~~~ @Override public boolean waitForDeviceNotAvailable(long waitTime) { IFastbootListener listener = new StubFastbootListener(); if (mFastbootEnabled) { mMgr.addFastbootListener(listener); } boolean result = waitForDeviceState(TestDeviceState.NOT_AVAILABLE, waitTime); if (mFastbootEnabled) { mMgr.removeFastbootListener(listener); } return result; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看