Android 4.0 用户输入子系统

1.Android 4.0 用户输入子系统代码模块
frameworks/base/services/java/com/android/server/SystemServer.java
frameworks/base/services/java/com/android/server/wm/WindowManagerService.java
frameworks/base/services/java/com/android/server/wm/InputManager.java
frameworks/base/services/jni/com_android_server_InputManager.cpp
frameworks/base/services/input/Android.mk
frameworks/base/services/input/EventHub.cpp
frameworks/base/services/input/EventHub.h
frameworks/base/services/input/InputApplication.cpp
frameworks/base/services/input/InputDispatcher.cpp
frameworks/base/services/input/InputListener.cpp
frameworks/base/services/input/InputDispatcher.h
frameworks/base/services/input/InputListener.h
frameworks/base/services/input/InputManager.cpp
frameworks/base/services/input/InputManager.h
frameworks/base/services/input/InputReader.cpp
frameworks/base/services/input/InputReader.h
frameworks/base/services/input/InputWindow.cpp
frameworks/base/services/input/InputWindow.h
frameworks/base/services/input/PointerController.cpp
frameworks/base/services/input/PointerController.h
frameworks/base/services/input/SpriteController.cpp

frameworks/base/services/input/SpriteController.h


2.Service and WindowManagerService


[java] view plain copy
  1. Source code:frameworks/base/services/java/com/android/server/SystemServer.java  
  2.     class ServerThread extends Thread {  
  3.           private static final String TAG = "SystemServer";  
  4.            
  5.           /*。。。。。。。。。。处略过若干行。。。。。。。。。*/  
  6.                   Slog.i(TAG, "Window Manager");  
  7.                   wm = WindowManagerService.main(context, power,  
  8.                           factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,  
  9.                           !firstBoot);  
  10.                   ServiceManager.addService(Context.WINDOW_SERVICE, wm);  
  11.   
  12.                   ActivityManagerService.self().setWindowManager(wm);  
  13.   
  14.     }  
功能描述:系统启动过程通过SystemServer添加WindowManagerService到服务列表

--WindowManagerService的构造方法

[java] view plain copy
  1. Source code:frameworks/base/services/java/com/android/server/wm/WindowManagerService.java  
  2.     private WindowManagerService(Context context, PowerManagerService pm,  
  3.                   boolean haveInputMethods, boolean showBootMsgs) {  
  4.   
  5.               mInputManager = new InputManager(context, this);/*构造InputManager*/  
  6.   
  7.               PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);  
  8.               thr.start();  
  9.                     /*debug by Joyce*/  
  10.               synchronized (thr) {  
  11.                   while (!thr.mRunning) {  
  12.                       try {  
  13.                           thr.wait();  
  14.                       } catch (InterruptedException e) {  
  15.                       }  
  16.                   }  
  17.               }  
  18.               mInputManager.start();/*启动InputManager*/  
  19.           }  

功能描述:1)初始化并构造一个InputManager实例mInputManager
                    2)开启InputManager(由InputManager本身start()方法实现)

--InputManager构造方法以及start()方法的实现

[java] view plain copy
  1. Source code:frameworks/base/services/java/com/android/server/wm/InputManager.java  
  2.     public InputManager(Context context, WindowManagerService windowManagerService) {  
  3.               this.mContext = context;  
  4.               this.mWindowManagerService = windowManagerService;  
  5.               this.mCallbacks = new Callbacks();  
  6.                     /*现在知道为何与WindowManagerService有关系了吧 
  7.                      *因为InputManager的各种需求 
  8.                         */  
  9.                     /*获取Looper循环队列?说实话我已经忘记了,只不过还记得那一副经典的图*/  
  10.               Looper looper = windowManagerService.mH.getLooper();  
  11.               Slog.i(TAG, "Initializing input manager");  
  12.   
  13.               nativeInit(mContext, mCallbacks, looper.getQueue());/*初始化InputManager*/  
  14.           }  
  15.             /*debug by Joyce*/  
  16.           public void start() {  
  17.               Slog.i(TAG, "Hey Joyce Starting input manager");  
  18.   
  19.               nativeStart();/*启动InputManager的本地实现*/  
  20.   
  21.           }  
  22.     }  
功能描述:1)调用JNI本地框架中的nativeInit方法来初始化InputManager
                    2)调用JNI本地框架中的nativeStart方法来开启InputManager


3.JNI本地框架的实现
--com_android_server_InputManager中给上层InputManager所提供的两个本地AP的实现

------------------>A部分
[java] view plain copy
  1. Source code:frameworks/base/services/jni/com_android_server_InputManager.cpp  
  2.     /*方法映射表*/  
  3.     static JNINativeMethod gInputManagerMethods[] = {  
  4.           /* name, signature, funcPtr */  
  5.           { "nativeInit""(Landroid/content/Context;"  
  6.                   "Lcom/android/server/wm/InputManager$Callbacks;Landroid/os/MessageQueue;)V",  
  7.                   (void*) android_server_InputManager_nativeInit },  
  8.           { "nativeStart""()V",  
  9.                   (void*) android_server_InputManager_nativeStart },  
  10.     }  
  11.   
  12.     static void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz,  
  13.               jobject contextObj, jobject callbacksObj, jobject messageQueueObj) {  
  14.           if (gNativeInputManager == NULL) {/*首次调用肯定为NULL*/  
  15.               sp<Looper> looper = android_os_MessageQueue_getLooper(env, messageQueueObj);  
  16.               gNativeInputManager = new NativeInputManager(contextObj, callbacksObj, looper);  
  17.           } else {  
  18.               LOGE("Input manager already initialized.");  
  19.               jniThrowRuntimeException(env, "Input manager already initialized.");  
  20.           }  
  21.     }  
  22.   
  23. --NativeInputManager本地类的构造函数  
  24.     NativeInputManager::NativeInputManager(jobject contextObj,  
  25.               jobject callbacksObj, const sp<Looper>& looper) :  
  26.               mLooper(looper) {  
  27.           JNIEnv* env = jniEnv();  
  28.             /*程序到这里就已经为我们创建一个inputdevice HAL 的实例了*/  
  29.           sp<EventHub> eventHub = new EventHub();/*creat the EventHub 一系列的memset*/  
  30.             /*并以eventHub为参数为我们创建了一个InputManager本地实例,注意这个InputManager 
  31.                 是上层InputManager的本地实现定义在InputManager.cpp中 
  32.                 */  
  33.           mInputManager = new InputManager(eventHub, thisthis);  
  34.     }  
  35. --InputManager构造函数的实现  
  36. Source code:frameworks/base/services/input/InputManager.cpp  
  37. InputManager::InputManager(  
  38.         const sp<EventHubInterface>& eventHub,  
  39.         const sp<InputReaderPolicyInterface>& readerPolicy,  
  40.         const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {  
  41.     mDispatcher = new InputDispatcher(dispatcherPolicy);  
  42.     mReader = new InputReader(eventHub, readerPolicy, mDispatcher);  
  43.     initialize();  
  44. }  


功能描述:1)创建NativeInputManager对象并保存成gNativeInputManager,该变量马上就会被用到
                    2)创建NativeInputManager对象的同时创建EventHub对象,并将创建的EventHub对象mEventHub作为参数传递给InputManager
                       的构造函数创建InputManager对象mInputManager
                    3)构建InputManager的同时会为我们构建InputDispatcher和InputReader对象
                    4)好了其实到这里就已经进入了本地C++框架层了,先到此打住,把下面这个JNI方法先分析一番

------------------>B部分

[java] view plain copy
  1. static void android_server_InputManager_nativeStart(JNIEnv* env, jclass clazz) {  
  2.       if (checkInputManagerUnitialized(env)) {  
  3.           return;  
  4.       }  
  5.   
  6.       status_t result = gNativeInputManager->getInputManager()->start();  
  7.       if (result) {  
  8.           jniThrowRuntimeException(env, "Input manager could not be started.");  
  9.       }  
  10. }  

功能描述:1)很简单直接调用上面初始化过程中创建的NativeInputManager对象mNativeInputManager去调用getInputManager()函数,该函数又刚好返回InputManager对象mInputManager了
                    2)再由mInputManager去调用它本身的start()方法来启动InputManager
                    3)从上面的这些方法可以得知在上层我们开启InputManager实质是开启了本地C++框架中的InputManager
4.C++Input框架的实现

你可能感兴趣的:(java,android,框架,server,jni,null)