monkey源码分析之事件注入方法变化

时间:2024-09-20 16:33:56

在上一篇文章《Monkey源码分析之事件注入》中,我们看到了monkey在注入事件的时候用到了《Monkey源码分析番外篇之Android注入事件的三种方法比较》中的第一种方法,通过Internal API的WindowManager的injectKeyEvent之类的方法注入事件。这种方法在android api level 16也就是android4.1.2之后已经发生了变化:

  • 在此之后注入事件的方式变成了使用InputManager的injectInputEvent方法了
  • 而InputManager的getInstance和injectInputEvent等方法后来又变成了隐藏方法,具体哪个版本我没有去查,但起码我现在在看的Android 4.4.2是这样的
  • 同样,uiautomator使用的注入事件方法用的也是InputManager的injectInputEvent的方法,这我想就是为什么UIAutomator只支持api level 16以后的android版本了
这里我们看下monkey在最新的版本API Level 19(android 4.4.2)的注入事件代码。
/*     */   public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose)
/* */ {
/* 101 */ if (verbose > 1) { String note;
/* */ String note;
/* 103 */ if (this.mAction == 1) {
/* 104 */ note = "ACTION_UP";
/* */ } else {
/* 106 */ note = "ACTION_DOWN";
/* */ }
/* */ try
/* */ {
/* 110 */ System.out.println(":Sending Key (" + note + "): " + this.mKeyCode + " // " + MonkeySourceRandom.getKeyName(this.mKeyCode));
/* */ }
/* */ catch (ArrayIndexOutOfBoundsException e)
/* */ {
/* 114 */ System.out.println(":Sending Key (" + note + "): " + this.mKeyCode + " // Unknown key event");
/* */ }
/* */ }
/* */
/* */
/* 119 */ KeyEvent keyEvent = this.mKeyEvent;
/* 120 */ if (keyEvent == null) {
/* 121 */ long eventTime = this.mEventTime;
/* 122 */ if (eventTime <= 0L) {
/* 123 */ eventTime = SystemClock.uptimeMillis();
/* */ }
/* 125 */ long downTime = this.mDownTime;
/* 126 */ if (downTime <= 0L) {
/* 127 */ downTime = eventTime;
/* */ }
/* 129 */ keyEvent = new KeyEvent(downTime, eventTime, this.mAction, this.mKeyCode, this.mRepeatCount, this.mMetaState, this.mDeviceId, this.mScanCode, 8, 257);
/* */ }
/* */
/* */
/* 133 */ if (!InputManager.getInstance().injectInputEvent(keyEvent, 1))
/* */ {
/* 135 */ return 0;
/* */ }
/* 137 */ return 1;
/* */ }
/* */ }

可以看到最后的注入事件方法从原来的iwm.injectKeyEvent变成了现在的Inputmanager.getInstance().injectInputEvent方法了。

作者 自主博客 微信服务号及扫描码 ****
天地会珠海分舵 http://techgogogo.com 服务号:TechGoGoGo扫描码:monkey源码分析之事件注入方法变化 http://blog.****.net/zhubaitian