Android5.0 输入系统(一)————InputManagerService服务及相关对象的创建与启动

frameworks\base\services\java\com\android\server\SystemServer.java

public static void main(String[] args) {
        new SystemServer().run();
}
frameworks\base\services\java\com\android\server\SystemServer.java

private void run() {
	......
	
	try {
	        startBootstrapServices();
	        startCoreServices();
	        startOtherServices(); 
	    } catch (Throwable ex) {
	        Slog.e("System", "******************************************");
	        Slog.e("System", "************ Failure starting system services", ex);
	        throw ex;
	}
	......
}

frameworks\base\services\java\com\android\server\SystemServer.java

startOtherServices(){
		
	......
	/*Step1: 新建IMS*/
	inputManager = new InputManagerService(context);

	wm = WindowManagerService.main(context, inputManager,
	        mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
	        !mFirstBoot, mOnlyCore);
	ServiceManager.addService(Context.WINDOW_SERVICE, wm);
	/*Step2: 注册*/
	ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
	
	mActivityManagerService.setWindowManager(wm);
	/*Step3: 设置向WMS发起回调的callback对象*/
	inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
	/*Step4: 正式启动*/
	inputManager.start();
	......
}
Step1: 新建IMS

frameworks\base\services\core\java\com\android\server\input\InputManagerService.java

public InputManagerService(Context context) {
    this.mContext = context;
    this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());

    mUseDevInputEventForAudioJack =
            context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
    Slog.i(TAG, "Initializing input manager, mUseDevInputEventForAudioJack="
            + mUseDevInputEventForAudioJack);
	/*每一个分为Java和Native两部分的对象在创建时都有一个nativeInput函数,
	mPtr保存native层的InputMangerService*/
    mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());

    LocalServices.addService(InputManagerInternal.class, new LocalService());
}

frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp

static jlong nativeInit(JNIEnv* env, jclass clazz,
        jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
    /*java层转换为C++层*/
    sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
    if (messageQueue == NULL) {
        jniThrowRuntimeException(env, "MessageQueue is not initialized.");
        return 0;
    }

	/*NativeInputManager是Native层组件与java层IMS进行通信的桥梁*/
    NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
            messageQueue->getLooper());
    im->incStrong(0);
	/*返回NativeInputManager对象的指针给Java层的IMS,IMS将其
	*保存在mPtr成员变量中*/
    return reinterpret_cast<jlong>(im);
}
frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp

NativeInputManager::NativeInputManager(jobject contextObj,
        jobject serviceObj, const sp<Looper>& looper) :
        mLooper(looper), mInteractive(true) {
    JNIEnv* env = jniEnv();
   	......
	/*创建一个EventHub*/
    sp<EventHub> eventHub = new EventHub();
	/*创建一个native层的InputManager*/
    mInputManager = new InputManager(eventHub, this, this);
}
frameworks\native\services\inputflinger\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();
}
frameworks\native\services\inputflinger\InputDispatcher.cpp
InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) :
    mPolicy(policy),
    mPendingEvent(NULL), mAppSwitchSawKeyDown(false), mAppSwitchDueTime(LONG_LONG_MAX),
    mNextUnblockedEvent(NULL),
    mDispatchEnabled(false), mDispatchFrozen(false), mInputFilterEnabled(false),
    mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) {
    mLooper = new Looper(false);

    mKeyRepeatState.lastKeyEntry = NULL;
	/*配置相关*/
    policy->getDispatcherConfiguration(&mConfig);
}
      注意InputDispatcher类和InputReader类的成员变量mPolicy的赋值都为为NativeInputManager对象,请看博文最后的类图。
frameworks\native\services\inputflinger\InputReader.cpp
InputReader::InputReader(const sp<EventHubInterface>& eventHub,
        const sp<InputReaderPolicyInterface>& policy,
        const sp<InputListenerInterface>& listener) :
        mContext(this), mEventHub(eventHub), mPolicy(policy),
        mGlobalMetaState(0), mGeneration(1),
        mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
        mConfigurationChangesToRefresh(0) {
    mQueuedListener = new QueuedInputListener(listener);

    { // acquire lock
        AutoMutex _l(mLock);

        refreshConfigurationLocked(0);
        updateGlobalMetaStateLocked();
    } // release lock
}
      注意InputReader对象的构造函数的参数,第一个参数是前面创建的EventHub对象的引用,第二个参数是NativeInputManager对象的引用,第三个参数是InputDispatcher对象的引用。InputRead对象的构造函数创建一个队列监听器mQueuedListener,传入的参数是InputDispatcher对象的引用。
QueuedInputListener::QueuedInputListener(const sp<InputListenerInterface>& innerListener) :
        mInnerListener(innerListener) {
}
      传入的参数是InputDispatcher对象的引用。
frameworks\native\services\inputflinger\InputManager.cpp
void InputManager::initialize() {
    mReaderThread = new InputReaderThread(mReader);
    mDispatcherThread = new InputDispatcherThread(mDispatcher);
}
Step2: 正式启动
frameworks\base\services\core\java\com\android\server\input\InputManagerService.java

public void start() {
    Slog.i(TAG, "Starting input manager");
    nativeStart(mPtr);

    // Add ourself to the Watchdog monitors.
    Watchdog.getInstance().addMonitor(this);

    registerPointerSpeedSettingObserver();
    registerShowTouchesSettingObserver();

    mContext.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updatePointerSpeedFromSettings();
            updateShowTouchesFromSettings();
        }
    }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mHandler);

    updatePointerSpeedFromSettings();
    updateShowTouchesFromSettings();
}
frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp
static void nativeStart(JNIEnv* env, jclass clazz, jlong ptr) {
    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);

    status_t result = im->getInputManager()->start();
    if (result) {
        jniThrowRuntimeException(env, "Input manager could not be started.");
    }
}
      Java层传递过来的参数ptr实际上是Native层创建的NativeInputManager的指针,故可以转换。调用NativeInputManager对象的getInputManager()方法,得到InputManager对象,并调用其start()方法。

frameworks\native\services\inputflinger\InputManager.cpp

status_t InputManager::start() {
    status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
    result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);
    return OK;
}

      这幅图形象的描述了上面一系列代码所创建的对象






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值