InputManagerService服务的初始化

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 =
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值