Input流程简述

  1. InputManagerService:system_server中的binder实体,其实只是native层的代理
  2. InputManger:native层真正处理事件的类,持有InputReader和InputDispatcher
  3. EventHub:硬件驱动读取到事件并将事件信息写入到设备节点,EventHub负责从设备节点读取事件
  4. InputReader:持有InputReaderThread线程,用来读取EventHub中的事件,并将事件转发给InputDispatcher。
  5. InputDispathcer:持有InputDispatcher线程,用来将事件转发给合适的窗口,并且收到窗口给的事件消费完成的回调,这个过程使用的是socket通讯

一个事件发生的过程:
1. 在Activity调用onResume时会创建一个ViewRootImpl和一个Window对象,并和WMS通讯,让WMS记录Windows相关信息,这时就会创建两个Socket对象,分别是客户端和服务端,服务端保存在WMS中,客户端保存在ViewRootImpl中,这样WMS和IMS在同一个进程中,这样IMS作为服务端就可以和UI进程进行通讯,同时InputDispatcher持有Window的一个Handler,可以通过这个Handler获取Window的所有信息,每当Window发生改变时也会通知InputDispatcher改变Handler。
InputDispatcher的native looper会注册handleReceiveCallback,UI线程的native looper会注册handleEvent
2. 当事件发生时,InputReader通过EventHub读取到事件信息,并将事件编码、封装放入到InputReader持有的一个Queue中,然后将Queue中的事件放入到InputDispatcher中的mInboundQueue
3. InputDispatcher首先在所有的Window的Handler中找到fcous的Window的Handler,然后将mInboundQueue中的事件放入这个Handler对应的mOutboundQueue(其实还有一个Connectiont对象来关联Handler和mOutboundQueue),再将事件放入waitQueue,这个waitQueue的作用是存放正在处理的事件。最后通过socket向窗口发送事件相关信息
4. 然后looper回调handleEvent,handleEvent层层调用会导致VootRootImp根据事件类型进行事件分发,然后事件被消费,最终会通过socket告诉InputDispatcher线程事件消费完成
5. InputDispatcher的native looper调用handleReceiveCallback,然后清空waitQueue

消息流程是:
EnentHub-InputRead-InputDispatcher-Window(WaitQueue)-UIThread-消费-InputDispatcher-Window清空WaitQueue

Input流程简述

图片来自http://gityuan.com/2016/12/31/input-ipc/