现在的位置: 首页 > 综合 > 正文

Android 4.0 用户输入子系统

2014年02月24日 ⁄ 综合 ⁄ 共 5614字 ⁄ 字号 评论关闭

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

Source code:frameworks/base/services/java/com/android/server/SystemServer.java
	class ServerThread extends Thread {
		  private static final String TAG = "SystemServer";
		 
		  /*。。。。。。。。。。处略过若干行。。。。。。。。。*/
		          Slog.i(TAG, "Window Manager");
		          wm = WindowManagerService.main(context, power,
		                  factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,
		                  !firstBoot);
		          ServiceManager.addService(Context.WINDOW_SERVICE, wm);

		          ActivityManagerService.self().setWindowManager(wm);

	}

功能描述:系统启动过程通过SystemServer添加WindowManagerService到服务列表

--WindowManagerService的构造方法

Source code:frameworks/base/services/java/com/android/server/wm/WindowManagerService.java
	private WindowManagerService(Context context, PowerManagerService pm,
		          boolean haveInputMethods, boolean showBootMsgs) {

		      mInputManager = new InputManager(context, this);/*构造InputManager*/

		      PolicyThread thr = new PolicyThread(mPolicy, this, context, pm);
		      thr.start();
					/*debug by Joyce*/
		      synchronized (thr) {
		          while (!thr.mRunning) {
		              try {
		                  thr.wait();
		              } catch (InterruptedException e) {
		              }
		          }
		      }
		      mInputManager.start();/*启动InputManager*/
		  }

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

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

Source code:frameworks/base/services/java/com/android/server/wm/InputManager.java
	public InputManager(Context context, WindowManagerService windowManagerService) {
		      this.mContext = context;
		      this.mWindowManagerService = windowManagerService;
		      this.mCallbacks = new Callbacks();
					/*现在知道为何与WindowManagerService有关系了吧
					 *因为InputManager的各种需求
						*/
					/*获取Looper循环队列?说实话我已经忘记了,只不过还记得那一副经典的图*/
		      Looper looper = windowManagerService.mH.getLooper();
		      Slog.i(TAG, "Initializing input manager");

		      nativeInit(mContext, mCallbacks, looper.getQueue());/*初始化InputManager*/
		  }
			/*debug by Joyce*/
		  public void start() {
		      Slog.i(TAG, "Hey Joyce Starting input manager");

		      nativeStart();/*启动InputManager的本地实现*/

		  }
	}

功能描述:1)调用JNI本地框架中的nativeInit方法来初始化InputManager
                    2)调用JNI本地框架中的nativeStart方法来开启InputManager

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

------------------>A部分

Source code:frameworks/base/services/jni/com_android_server_InputManager.cpp
	/*方法映射表*/
	static JNINativeMethod gInputManagerMethods[] = {
		  /* name, signature, funcPtr */
		  { "nativeInit", "(Landroid/content/Context;"
		          "Lcom/android/server/wm/InputManager$Callbacks;Landroid/os/MessageQueue;)V",
		          (void*) android_server_InputManager_nativeInit },
		  { "nativeStart", "()V",
		          (void*) android_server_InputManager_nativeStart },
	}

	static void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz,
		      jobject contextObj, jobject callbacksObj, jobject messageQueueObj) {
		  if (gNativeInputManager == NULL) {/*首次调用肯定为NULL*/
		      sp<Looper> looper = android_os_MessageQueue_getLooper(env, messageQueueObj);
		      gNativeInputManager = new NativeInputManager(contextObj, callbacksObj, looper);
		  } else {
		      LOGE("Input manager already initialized.");
		      jniThrowRuntimeException(env, "Input manager already initialized.");
		  }
	}

--NativeInputManager本地类的构造函数
	NativeInputManager::NativeInputManager(jobject contextObj,
		      jobject callbacksObj, const sp<Looper>& looper) :
		      mLooper(looper) {
		  JNIEnv* env = jniEnv();
			/*程序到这里就已经为我们创建一个inputdevice HAL 的实例了*/
		  sp<EventHub> eventHub = new EventHub();/*creat the EventHub 一系列的memset*/
			/*并以eventHub为参数为我们创建了一个InputManager本地实例,注意这个InputManager
				是上层InputManager的本地实现定义在InputManager.cpp中
				*/
		  mInputManager = new InputManager(eventHub, this, this);
	}
--InputManager构造函数的实现
Source code:frameworks/base/services/input/InputManager.cpp
InputManager::InputManager(
        const sp<EventHubInterface>& eventHub,
        const sp<InputReaderPolicyInterface>& readerPolicy,
        const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
    mDispatcher = new InputDispatcher(dispatcherPolicy);
    mReader = new InputReader(eventHub, readerPolicy, mDispatcher);
    initialize();
}

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

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

	static void android_server_InputManager_nativeStart(JNIEnv* env, jclass clazz) {
		  if (checkInputManagerUnitialized(env)) {
		      return;
		  }

		  status_t result = gNativeInputManager->getInputManager()->start();
		  if (result) {
		      jniThrowRuntimeException(env, "Input manager could not be started.");
		  }
	}

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

抱歉!评论已关闭.