android framework java层是如何拦截并分发底层传送来的按键事件

来源:互联网 发布:一览网络兼职是真的吗 编辑:程序博客网 时间:2024/05/19 21:59

android framework java层是如何拦截并分发底层传送来的按键事件


按键事件首先通过PhoneWindowManager的interceptKeyBeforeDispatching方法被拦截,然后分发到应用层,一些系统事件:HOME,MENU,SEARCH,会在这里做下预处理。那底层事件如何传到interceptKeyBeforeDispatching方法中呢?

通过查看谁调用了此方法(eclipse中右键单击此方法名,选择open call hierarchy),发现InputMonitor.java调用了此方法:

查看文本打印?
  1. public long interceptKeyBeforeDispatching(  
  2.             InputWindowHandle focus, KeyEvent event, int policyFlags) {  
  3.         WindowState windowState = focus != null ? (WindowState) focus.windowState : null;  
  4.         return mService.mPolicy.interceptKeyBeforeDispatching(windowState, event, policyFlags);  
  5.     }  

同样的方法我们发现InputManager.java中的Callbacks调用了InputMonitor的interceptKeyBeforeDispatching方法:
查看文本打印?
  1. @SuppressWarnings("unused")  
  2.         public long interceptKeyBeforeDispatching(InputWindowHandle focus,  
  3.                 KeyEvent event, int policyFlags) {  
  4.             return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching(  
  5.                     focus, event, policyFlags);  
  6.         }  

java层就是通过InputManager.java和C++层完成事件交互,通过上述我们知道是Callbacks调用了interceptKeyBeforeDispatching方法,而Callbacks是什么呢,顾名思义,Callbacks是C++层回调InputManager.java的一个接口。那Callbacks是如何初始化和注册的呢?InputManager.java的构造方法给出了答案:
查看文本打印?
  1. public InputManager(Context context, WindowManagerService windowManagerService) {  
  2.         this.mContext = context;  
  3.         this.mWindowManagerService = windowManagerService;  
  4.         this.mCallbacks = new Callbacks();  
  5.   
  6.         Looper looper = windowManagerService.mH.getLooper();  
  7.   
  8.         Slog.i(TAG, "Initializing input manager");  
  9.         nativeInit(mContext, mCallbacks, looper.getQueue());  
  10.   
  11.         // Add ourself to the Watchdog monitors.  
  12.         Watchdog.getInstance().addMonitor(this);  
  13.     }  

nativeInit(mContext, mCallbacks, looper.getQueue());这是一个native方法,java层通过jni调用C++层相对应的方法。C++层相对应的方法会去初始化C++层的InputManager和注册Callbacks。

那么如何找到C++层这个相对应的方法呢?其实很简单,我们看下InputManager.java的路径

\frameworks\base\services\java\com\android\server\wm

通常在和java(\frameworks\base\services\java)同目录下会有相对应的jni的目录结构(\frameworks\base\services\jni)

通过Source Insight 搜索对应目录(PS这样快)下的nativeInit字串,发现这样的一个数组:

查看文本打印?
  1. static JNINativeMethod gInputManagerMethods[] = {  
  2.     /* name, signature, funcPtr */  
  3.     { "nativeInit""(Landroid/content/Context;"  
  4.             "Lcom/android/server/wm/InputManager$Callbacks;Landroid/os/MessageQueue;)V",  
  5.             (void*) android_server_InputManager_nativeInit },  


android_server_InputManager_nativeInit就是C++层相对应的方法,在com_android_server_InputManager.cpp中:

查看文本打印?
  1. static void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz,  
  2.         jobject contextObj, jobject callbacksObj, jobject messageQueueObj) {  
  3.     if (gNativeInputManager == NULL) {  
  4.         sp<Looper> looper = android_os_MessageQueue_getLooper(env, messageQueueObj);  
  5.         gNativeInputManager = new NativeInputManager(contextObj, callbacksObj, looper);  
  6.     } else {  
  7.         LOGE("Input manager already initialized.");  
  8.         jniThrowRuntimeException(env, "Input manager already initialized.");  
  9.     }  
  10. }  


NativeInputManager的构造函数中:
查看文本打印?
  1. mInputManager = new InputManager(eventHub, thisthis);  


InputManager.cpp 存在于frameworks\base\services\input,构造函数中:
查看文本打印?
  1. InputManager::InputManager(  
  2.         const sp<EventHubInterface>& eventHub,  
  3.         const sp<InputReaderPolicyInterface>& readerPolicy,  
  4.         const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {  
  5.     mDispatcher = new InputDispatcher(dispatcherPolicy);  
  6.     mReader = new InputReader(eventHub, readerPolicy, mDispatcher);  
  7.     initialize();  
  8. }  


InputDispatcher中的doInterceptKeyBeforeDispatchingLockedInterruptible函数调用了mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle,
            &event, entry->policyFlags);传入事件,mPolicy就是上面代码中的dispatcherPolicy对象。

查看文本打印?
  1. void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible(  
  2.         CommandEntry* commandEntry) {  
  3.     KeyEntry* entry = commandEntry->keyEntry;  
  4.   
  5.     KeyEvent event;  
  6.     initializeKeyEvent(&event, entry);  
  7.   
  8.     mLock.unlock();  
  9.   
  10.     nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle,  
  11.             &event, entry->policyFlags);  
  12.   
  13.     mLock.lock();  
  14.   
  15.     if (delay < 0) {  
  16.         entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP;  
  17.     } else if (!delay) {  
  18.         entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE;  
  19.     } else {  
  20.         entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER;  
  21.         entry->interceptKeyWakeupTime = now() + delay;  
  22.     }  
  23.     entry->release();  
  24. }  

InputDispatcher负责分发c++层获得的linux设备的按键事件,交由dispatcherPolicy,也就是NativeInputManager处理,NativeInputManager,通过Callbacks然后上报java层。

对于c++层如何获得的linux设备的按键事件,以及如何通过InputDispatcher分发这些事件感兴趣的,可以查看frameworks\base\services\input下的代码,可以先从InputManager.cpp的构造函数入手,或者查看网上相关文章。这里暂时不描述了。


原创粉丝点击