1. WindowManagerService
frameworks/base/services/java/com/android/server/WindowManagerService.java
依赖 InputManager mInputManager;
在初始化的时候生成,并mInputManager.start() 启动
2. InputManager
frameworks/base/services/java/com/android/server/InputManager.java
a. new InputManager()
--->nativeInit(mcallbak) //初始化InputManager,并注册输入事件的回调函数
---> android_server_InputManager_nativeInit () // frameworks/base/services/jni/com_android_server_InputManager.cpp
---> NativeInputManager() // frameworks/base/services/jni/com_android_server_InputManager.cpp
---> InputManager() // frameworks/base/libs/ui/InputManager.cpp
----> 初始化DispatcherThread 和 ReaderThread 两个线程
mReaderThread = new InputReaderThread(mReader); // InputReader 循环loop EventHub
mDispatcherThread = new InputDispatcherThread(mDispatcher);
b. InputManager.start()
--- > android_server_InputManager_nativeStart //frameworks/base/services/jni/com_android_server_InputManager.cpp
-->启动DispatcherThread 和 ReaderThread 两个线程
3. InputReader //获取一次event事件
frameworks/base/libs/ui/InputReader.cpp
InputReader::loopOnce()
---> EventHub.getEvent()
4. InputManager.java 的mCallbacks //输入事件的回调处理函数InputManager.Callbacks内部类
InputDispatcher::notifyKey 回调NativeInputManager方法并通过jni 的方式回调InputManager.java 的mCallbacks 进行事件的处理
如:InputDispatcher::notifyKey ----> InputManager.CallbacksinterceptKeyBeforeQueueing()
//frameworks/base/services/jni/com_android_server_InputManager.cpp
5. InputManager.Callbacks内部类
a. 回调WindowManagerService的WindowManagerService.InputMonitor类的方法进行事件的处理.
b. InputMonitor 调用 WindowManagerService的成员mPolicy
c. mPolicy 是WindowManagerPolicy的对象,实际是com.android.internal.policy.impl.Policy 创建出来的PhoneWindowManager
// PhoneWindowManager.java
6. PhoneWindowManager.java
a. interceptKeyBeforeDispatching() home事件的截取
b. interceptKeyBeforeQueueing() power等按键的截取
7. InputReader.cpp
InputReader::process()
---> InputReader::consumeEvent()
---> InputDevice::process()
---> KeyboardInputMapper::process() //或者 TrackballInputMapper::process() 或者 TouchInputMapper::process()
--->KeyboardInputMapper::processKey()
----> InputDispatcher::notifyKey()
--->WindowManagerPolicy::interceptKeyBeforeQueueing() //即对应6中的,前期对事件的处理
InputDispatcher::enqueueInboundEventLocked()
---> mInboundQueue.enqueueAtTail(entry); // 将按键事件添加到InputDispatcher的消息队列中
如果队列原来是空的,则唤醒InputDispatcherThread进程
8. InputDispatcherThread
-->InputDispatcher::dispatchOnce()
--> InputDispatcher::dispatchOnceInnerLocked()
--> InputDispatcher::dispatchKeyLocked()
--> InputDispatcher::findFocusedWindowTargetsLocked()
InputDispatcher::dispatchEventToCurrentInputTargetsLocked()
--> InputDispatcher::prepareDispatchCycleLocked()
-->InputDispatcher::startDispatchCycleLocked()
--> InputPublisher::publishKeyEvent()
--> NativeInputQueue::handleReceiveCallback
--> java 层的InputQueue的dispatchKeyEvent() 方法
--> ViewRoot的dispatchKeyEvent() 方法
参考:
http://blog.csdn.net/yuleslie/article/details/7079448