1 InputFramework概述
Android输入系统在整个图形系统框架中扮演了很重要的角色,主要负责用户消息的管理,具体职责包括以下几个方面:
1、
2、
3、
Android系统使用InputManager类来管理消息,而具体的功能则是通过InputReaderThread和InputDispatcherThread两个线程来实现。其中InputReaderThread线程负责消息的读取,而
InputDispatcherThread则负责消息的预处理和分发到各个应用进程中。输入系统的整体框架如下图所示:
从框图中可以看出,Android输入系统通过EventHub收集输入设备的原始数据,InputReader调用接口读取EventHub中获取的数据,然后通知InputDispatcher数据已经准备好,InputDispatcher获得数据回调InputManager的接口间接回调WMS 中的InputMonitor对输入消息进行处理,如果WMS没有消耗掉该消息,则InputDispatcher会将该消息通过管道的方式,直接发送到应用进程中,当前焦点应用的ViewrootImpl会收到该消息,并对消息进行分发处理,最终将其发送到对应的View对象中进行界面响应。
1 NativeInputManager初始化
在WindowManagerService构造函数中,经过JNI调用完成了Native层InputManager的初始化,初始化工作有如下几点。
1.1 调用时序图
1.2 类图对象关系
1.3在Native层注册java层的CallBacks回调接口
在InputManagerService类中定义了一个Callback接口:
|
InputMonitor实现了InputManagerService.Callbacks接口,在WindowManagerService的构造函数中创建了InputMonitor对象,并以mInputMonitor作为参数创建InputManagerService的对象,在InputManagerService构造函数中,将mInputMonitor作为参数调用了JNI函数nativeInit(),将回调接口传到JNI层,在需要的时候,JNI在回调mInputMonitor中的函数,实现数据才传递。
class InputMonitorimplements InputManagerService.Callbacks{ } privateWindowManagerService(Context context, PowerManagerServicepm, final InputMonitormInputMonitor = new InputMonitor(this); mInputManager = newInputManagerService(context, mInputMonitor); } |
1.4 创建InputDispatcher和InputReader线程
在InputManagerService的构造函数中,调用了
<