上一篇文章《UiAutomator源碼分析之UiAutomatorBridge框架》中我們把UiAutomatorBridge以及它相關的類進行的描述,往下我們會嘗試根據兩個實例將這些類給串聯起來,我準備做的是用如下兩個很有代表性的實例:
/* */ UiAutomatorBridge(UiAutomation uiAutomation) /* */ { /* 48 */ this.mUiAutomation = uiAutomation; /* 49 */ this.mInteractionController = new InteractionController(this); /* 50 */ this.mQueryController = new QueryController(this); /* */ }
public boolean pressHome() { 218 Tracer.trace(); 219 waitForIdle(); 220 return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent( 221 KeyEvent.KEYCODE_HOME, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED, 222 KEY_PRESS_EVENT_TIMEOUT); 223 }
/* */ public boolean sendKeyAndWaitForEvent(final int keyCode, final int metaState, int eventType, long timeout) /* */ { /* 188 */ Runnable command = new Runnable() /* */ { /* */ public void run() { /* 191 */ long eventTime = SystemClock.uptimeMillis(); /* 192 */ KeyEvent downEvent = new KeyEvent(eventTime, eventTime, 0, keyCode, 0, metaState, -1, 0, 0, 257); /* */ /* */ /* 195 */ if (InteractionController.this.injectEventSync(downEvent)) { /* 196 */ KeyEvent upEvent = new KeyEvent(eventTime, eventTime, 1, keyCode, 0, metaState, -1, 0, 0, 257); /* */ /* */ /* 199 */ InteractionController.this.injectEventSync(upEvent); /* */ } /* */ /* */ } /* 203 */ }; /* 204 */ return runAndWaitForEvents(command, new WaitForAnyEventPredicate(eventType), timeout) != null; /* */ }代碼中創建了一個Runnable的線程,線程里面run重寫方法要做的事情就是去做注入事件的事情,那么為什么我們不直接去調用事件而需要創建一個線程了,這是因為我們在注入完事件之后還要去等待我們上面定義的預期的eventType是否有出現來判斷我們的事件注入究竟是否成功,這個就是204行runAndWaitForEvents做的事情。但我們這里還是先看下線程中是如何注入事件的:
/* */ private boolean injectEventSync(InputEvent event) { /* 655 */ return this.mUiAutomatorBridge.injectInputEvent(event, true); /* */ }再跟蹤到UiAutomatorBridge對象:
/* */ public boolean injectInputEvent(InputEvent event, boolean sync) { /* 70 */ return this.mUiAutomation.injectInputEvent(event, sync); /* */ }可以看到最終還是通過UiAutomation來注入事件的,和我們的預期是一致的。
/* */ private AccessibilityEvent runAndWaitForEvents(Runnable command, UiAutomation.AccessibilityEventFilter filter, long timeout) /* */ { /* */ try /* */ { /* 161 */ return this.mUiAutomatorBridge.executeCommandAndWaitForAccessibilityEvent(command, filter, timeout); /* */ } /* */ catch (TimeoutException e) { /* 164 */ Log.w(LOG_TAG, "runAndwaitForEvent timedout waiting for events"); /* 165 */ return null; /* */ } catch (Exception e) { /* 167 */ Log.e(LOG_TAG, "exception from executeCommandAndWaitForAccessibilityEvent", e); } /* 168 */ return null; /* */ }代碼又跳到了UiAutomatorBridge這個類
/* */ public AccessibilityEvent executeCommandAndWaitForAccessibilityEvent(Runnable command, UiAutomation.AccessibilityEventFilter filter, long timeoutMillis) throws TimeoutException /* */ { /* 104 */ return this.mUiAutomation.executeAndWaitForEvent(command, filter, timeoutMillis); /* */ }最終把要執行的runnable執行注入事件的線程command和我們預期事件發生后返回來的窗口事件filter以及超時timeoutMillis傳進去,UiAutomation就會和AccessibilityService進行交互以注入事件并且等待預期AccessibilityEvent發生或者超時返回。至于UiAutomation是如何和AccessibilityService交互的,這就超出了這個系列文章的范疇了。也許今后有充裕的時間的話我們再來深入去了解分析它。
作者 | 自主博客 | 微信 | CSDN |
天地會珠海分舵 | http://techgogogo.com | 服務號:TechGoGoGo 掃描碼: | 向AI問一下細節 免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。 猜你喜歡最新資訊相關推薦相關標簽AI
助 手
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女
|