Injection event for uiautomator source code analysis

Source: Internet
Author: User

In the previous article "uiautomator source code analysis-uiautomatorbridge framework", we described uiautomatorbridge and its related classes. Next we will try to concatenate these classes based on two instances, I am going to use the following two representative instances:

    • Injection event
    • Get control
In this article, we will analyze the presshome method of uidevice to analyze how uiautomator injects events. The next article will describe how to obtain controls, so stay tuned.
1. uiobject. first, let's take a look at the nonstandard sequence chart we have drawn. We can see how many classes the presshome action needs to interact with and how they interact.

2. when are these classes initialized? When we compile the test case script, we will not initialize all the above classes, including the uiobject object is obtained by directly calling the getuidevice () method of the parent class uiautomationtestcase in the script. In fact, these are all initialized by the START () method of the runtestcommand class during the uiautomator operation, for details, see section 3.6 "initialize uidevice and uiautomationbridge" in "start and run of uiautomator source code analysis. Here we will see how qunerycontrooler and interactioncontroller are initialized during uiautomatorbridge initialization. For details, see the constructor of uiautomatorbridge:
/*     */   UiAutomatorBridge(UiAutomation uiAutomation)/*     */   {/*  48 */     this.mUiAutomation = uiAutomation;/*  49 */     this.mInteractionController = new InteractionController(this);/*  50 */     this.mQueryController = new QueryController(this);/*     */   }

3. For code tracking, first check the presshome method of uidevice:
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    }
Row 3:
    • Obtain the uiautomatorbridge object saved by the uidevice object. Both objects are initialized at runtime. If you are not clear about them, please refer to the above article.
    • Use the uiautomatorbridge object to obtain the interactioncontroller object initialized in the previous section.
    • Call the sendkeyandwaitforevent method of the interactioncontroller object. The key parameters are the first keycode and the second eventtype.
      • Keycode: Specifies the key-press event to be injected. For example, here we use keycode_home.
      • Eventtype: indicates which accessibilityevent type is expected to be returned by the window after the event is injected. For example, we use type_window_content_change
Enter sendkeyandwaitforevent of the interactioncontroller class:
/*     */   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;/*     */   }
A runnable thread is created in the Code. The run rewrite method in the thread is used to inject events. Why do we need to create a thread instead of calling the event directly, this is because after the event is injected, we have to wait for the expected eventtype we defined above to determine whether the event injection is successful. This is what the 204 line runandwaitforevents does. But here we should first look at how events are injected into the deprecation process:
/*     */   private boolean injectEventSync(InputEvent event) {/* 655 */     return this.mUiAutomatorBridge.injectInputEvent(event, true);/*     */   }
Then we track the uiautomatorbridge object:
/*     */   public boolean injectInputEvent(InputEvent event, boolean sync) {/*  70 */     return this.mUiAutomation.injectInputEvent(event, sync);/*     */   }
We can see that we still inject events through uiautomation, which is consistent with our expectation. Let's continue to look at the runandwaitforevents method that actually executes the injection event thread in interactioncontroller:
/*     */   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;/*     */   }
The Code jumps to the uiautomatorbridge class.
/*     */   public AccessibilityEvent executeCommandAndWaitForAccessibilityEvent(Runnable command, UiAutomation.AccessibilityEventFilter filter, long timeoutMillis) throws TimeoutException/*     */   {/* 104 */     return this.mUiAutomation.executeAndWaitForEvent(command, filter, timeoutMillis);/*     */   }
Finally, the thread command of the runnable execution injection event to be executed and the window Event Filter and timeout timeoutmillis returned after the expected event are passed in, uiautomation interacts with the accessibilityservice to inject events and wait for the expected accessibilityevent to occur or return timeout. How uiautomation interacts with accessibilityservice is beyond the scope of this series of articles. We may have plenty of time to analyze it.




Injection event for uiautomator source code analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.