I. Processing of Power, Home, Menu, back, Vol +, Vol-
We know that when WindowManagerService is created, an InputManager instance is declared. This InputManager adds mCallbacks and logoff to C ++ through NativeInit to create InputDispatcher and InputReader. Add the two instances to InputDispatcherThread and InputReaderThread to read and dispatch input events.
1. InputManager. java:
Constructor has the following implementation:
This. mCallbacks = new Callbacks ();
This is critical. It is the method for implementing callback in the C ++ layer. Then add it to C ++ through NativeInit.
Private final class Callbacks {
Public int interceptKeyBeforeQueueing (KeyEvent event, int policyFlags, boolean
IsScreenOn ){
Return mWindowManagerService. mInputMonitor. interceptKeyBeforeQueueing (
Event, policyFlags, isScreenOn );
}
Public int interceptMotionBeforeQueueingWhenScreenOff (int policyFlags ){
Return mWindowManagerService. mInputMonitor.
InterceptMotionBeforeQueueingWhenScreenOff (policyFlags );
}
......
}
All of these functions call mPolicy through WindowManagerService to implement specific functions.
2. com_android_server_InputManager.cpp
The following operations are performed in the android_server_InputManager_nativeInit function:
GNativeInputManager = new NativeInputManager (contextObj, callbacksObj, lobj );
Here, the callbacks implemented in InputManager. java is added to mCallbacksObj in NativeInputManager.
Build InputManager at the same time: mInputManager = new InputManager (eventHub, this, this );
3. InputManager. cpp
The constructors include:
MDispatcher = new InputDispatcher (dispatcherPolicy );
MReader = new InputReader (eventHub, readerPolicy, mDispatcher );
By
Class NativeInputManager: public virtual RefBase,
Public virtual InputReaderPolicyInterface,
Public virtual InputDispatcherPolicyInterface,
Public virtual PointerControllerPolicyInterface {
} We can know that the second parameter of the InputManager constructor is actually the implementation of InputDispatcherPolicyInterface, that is, the instance of com_android_server_InputManager.
We know from the InputDispatcher statement that the policy here is actually com_android_server_InputManager.
InputDispatcher: InputDispatcher (const sp <InputDispatcherPolicyInterface> & policy ):
MPolicy (policy ),
MPendingEvent (NULL), mAppSwitchSawKeyDown (false), mAppSwitchDueTime (LONG_LONG_MAX ),
MNextUnblockedEvent (NULL ),
MDispatchEnabled (true), mDispatchFrozen (false), mInputFilterEnabled (false ),
MCurrentInputTargetsValid (false ),
MInputTargetWaitCause (INPUT_TARGET_WAIT_CAUSE_NONE)
After InputReader reads the event, it will wake up the InputDiapatcher function and then call InputDispatcher: policykey. For detailed analysis, see:
Http://blog.csdn.net/luoshengyang/article/details/6882903
4. InputDispatcher. cpp
Void InputDispatcher: policykey (const policykeyargs * args ){
......
// The mPolicy here is InputManager
MPolicy-> interceptKeyBeforeQueueing (& event, policyFlags );
......
}
5. Return to com_android_server_InputManager:
Void NativeInputManager: interceptKeyBeforeQueueing (const KeyEvent * keyEvent,
Uint32_t & policyFlags ){
If (policyFlags & POLICY_FLAG_TRUSTED )){
If (keyEventObj ){
// Here, we can clearly see that mCallbacksObj is Callbacks in InputManager. java.
// Call its interceptKeyBeforeQueueing.
WmActions = env-> CallIntMethod (mCallbacksObj,
GCallbacksClassInfo. interceptKeyBeforeQueueing,
KeyEventObj, policyFlags, isScreenOn );
If (checkAndClearExceptionFromCallback (env, "interceptKeyBeforeQueueing ")){
WmActions = 0;
}
Android_view_KeyEvent_recycle (env, keyEventObj );
Env-> DeleteLocalRef (keyEventObj );
}
}
}
Let's go back to the Callbacks definition in Step 1:
Public int interceptKeyBeforeQueueing (KeyEvent event, int policyFlags, boolean
IsScreenOn ){
Return mWindowManagerService. mInputMonitor. interceptKeyBeforeQueueing (
Event, policyFlags, isScreenOn );
}
InputMonitor. java
Public int interceptKeyBeforeQueueing (
KeyEvent event, int policyFlags, boolean isScreenOn ){
Return mService. mPolicy. interceptKeyBeforeQueueing (event, policyFlags, isScreenOn );
}
Here mService is WindowManagerService, and mPolicy is defined as follows (in WindowManagerService)
Final WindowManagerPolicy mPolicy = PolicyManager. makeNewWindowManager ();
Yymanager. java
Public static WindowManagerPolicy makeNewWindowManager (){
// SPolicy is the Policy instantiation.
Return sPolicy. makeNewWindowManager ();
}
Policy. java
Public WindowManagerPolicy makeNewWindowManager (){
Return new PhoneWindowManager ();
}
Now, the PhoneWindowManager instance is officially called.
Therefore, the code for processing the hardkey is in PhoneWindowManager.
Author: kakaBack