InputManagerService由SystemService创建并启动
InputManager中创建InputDispatcher和InputReader,InputDispatcher和InputReader分别是一个线程。InputDispatcher实现了InputListenerInterface,把InputDispatcher传给InputReader用于分发事件
EventHub监听/dev/input下的文件通过epoll方式监听设备输入,用Notify监听设备变化,并注册的epoll中,InputReader调用EventHub.getEvents时,如果没有输入会epoll_wait。
当有触摸事件时,epoll_wait会返回,把事件交给TouchInputMapper处理,TouchInputMapper调用notifyMotion唤起InputDispatcher,找到要发送事件的window,调用InputPublisher发送事件,然后调用InputChannel的sendMessage,调用socket的send发送事件
接收的时候是在ViewRootImpl的setView时,创建一个InputChannel用于接收事件,并调用addToDisplay将InputChannel注册。WindowInputEventReceiver中依赖了InputChannel,在native的InputEventReceiver(实现了LooperCallback)中往looper添加了InputChannel的fd监听,一旦有信号发过来就调用了LooperCallback的handleEvent方法,然后将InputChannel收到的消息回调到onInputEvent,如果不需要立即处理,通过handler发布一个异步消息,最后和需要立即处理的一样进入doProcessInputEvents,通过InputStag进行分发,InputStag采用责任链模式,在ViewRootImpl的setView中初始化的。
触摸事件是在ViewPostImeInputStage中处理的,ViewPostImeInputStage把事件发送给DecorView,DecorView发送给Activity,Activity发送给PhoneWindow,PhoneWindow又发送给DecorView,DecorView发送给ViewGroup,如果事件在PhoneWindow之后的流程消耗掉了,activity就不调用onTouchEvent,如果没有消耗掉就调用onTouchEvent。