InputManagerService的初始化
1.简介
Android系统的输入事件是由InputManagerService服务来监控的,而InputManagerService是由窗口管理服务WindowManagerService来启动的。WindowManagerService服务是在system_server进程中启动关键服务时启动的。
2.初始化流程
InputManagerService也是一个系统服务,在SystemServer中初始化,其初始化流程如下:
2.1 SystemServer.startOtherServices()
private void startOtherServices() {
....
inputManager = new InputManagerService(context);//构造InputManagerService服务,见2.2
wm = WindowManagerService.main(context, inputManager,
mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
!mFirstBoot, mOnlyCore);//将InputManagerService设置到WindowManagerService中,见2.12
ServiceManager.addService(Context.WINDOW_SERVICE, wm);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
inputManager.setWindowManagerCallbacks(wm.getInputMonitor());// 将InputMonitor对象保存在InputManagerService对象中
inputManager.start();//启动InputManagerService服务,见2.13
....
}
可以看到,在SystemServer的startOtherService方法中,不但构造了InputManagerService服务,而且在启动InputManagerService之前,将其自身注入到WindowMangerService中,进行关联。
2.2 InputManagerService()
public InputManagerService(Context context) {
this.mContext = context;
this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());//初始化Handler,运行在“android.display”线程
.....
mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());// 调用本地方法初始化,见2.3
......
LocalServices.addService(InputManagerInternal.class, new LocalService());
}
nativeInit方法在com_android_server_input_InputManagerService.cpp文件中实现
2.3 nativeInit()
static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
//获取InputManagerService对应的native消息队列
sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
if (messageQueue == NULL) {
jniThrowRuntimeException(env, "MessageQueue is not initialized.");
return 0;
}
// 构造NativeInputManager,见2.4
NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
messageQueue->getLooper());
im->incStrong(0);
return reinterpret_cast<jlong>(im);//返回native指针,保存在InputManagerService的mPtr变量中。
}
android_os_MessageQueue_getMessageQueue方法主要是获取Java层消息队列对应的native消息队列。
sp<MessageQueue> android_os_MessageQueue_getMessageQueue(JNIEnv* env, jobject messageQueueObj) {
jlong ptr = env->GetLongField(messageQueueObj, gMessageQueueClassInfo.mPtr);//获取InputManagerService中mHanlder对应的消息队列
return reinterpret_cast<NativeMessageQueue*>(ptr);
}
2.4 NativeInputManager()
NativeInputManager::NativeInputManager(jobject contextObj,
jobject serviceObj, const sp<Looper>& looper) :
mLooper(looper), mInteractive(true) {
JNIEnv* env = jniEnv();
mContextObj = env->NewGlobalRef(contextObj);//保存InputManagerService中的Context
mServiceObj = env->NewGlobalRef(serviceObj);// 保存InputManagerService对象
{
AutoMutex _l(mLock);
mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
mLocked.pointerSpeed = 0;
mLocked.pointerGesturesEnabled = true;
mLocked.showTouches = false;
}
mInteractive = true;
sp<EventHub> eventHub = new EventHub();//构建EventHub,见2.5
mInputManager =