本文正在参加「金石计划 . 瓜分6万现金大奖」

🔥 **Hi,我是小余。本文已收录到 GitHub · Androider-Planet 中。这儿有 Android 进阶成长常识体系,重视大众号 [小余的自习室] ,在成功的路上不迷路! **

前言

关于为什么会有这“framework必会系列”文章?对,卷王太多了。。

关于现在应用开发现已饱和的大环境下,作为一个多年Android开发,强逼咱们Android开发往更深层次的framework层走,于是就有了这么个系列。

好了这都不谈了,咱们来进入正文。
上一篇文章咱们解说了Android输入体系事情读取,这儿再来回顾下:

  • 开机后SystemServer发动进程中创立了InputManagerService,InputManagerService结构办法中在native层创立了NativeInputManager目标。
  • NativeInputManager结构办法中又创立了一个InputManager目标,InputManager结构办法中又创立了InputDispatcher和InputReader目标以及他们两个工作对应的线程 InputDispatcherThread和InputReaderThread。
  • InputManagerService在实例化完成后,SystemServer发动进程会继续调用其start办法发动IMS,实践是发动InputDispatcherThread和InputReaderThread线程。
  • 在InputReaderThread线程中运用EventHub的getEvents办法去设备节点/dev/input下面获取节点数据。并在事情加工完成后并唤醒InputDispatcherThread线程去处理。

以上便是整个输入事情读取的进程,用一张总结关系:

本篇文章首要解说关于IMS的输入事情分发的进程

事情分发流程

为了让咱们不会迷失在源码中,笔者先抛出几个问题,然后带着问题去看源码

  • 问题1:窗口什么时分传入IMS输入体系的?IMS是怎么找到对应的Window窗口的?
  • 问题2:IMS和WMS是经过什么方式通讯将事情分发出去的?

这儿再提早放一张图让咱们对输入事情模型有个概念,然后再结合源码去剖析。

下面正式开始剖析源码:

事情分发进口确定

上篇文章咱们讲到事情读取流程将事情放入封装为KeyEntry放入到mInboundQueue行列的尾部tail,并唤醒InputDispatcherThread线程,就以这儿为进口

void InputDispatcher::notifyKey(const NotifyKeyArgs* args) {
    ...
    KeyEntry* newEntry = new KeyEntry(args->eventTime,args->deviceId, ...);//1
​
    needWake = enqueueInboundEventLocked(newEntry); //2  
    if (needWake) {
        mLooper->wake();//3
    }
​
}
bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) {
    ...
    mInboundQueue.enqueueAtTail(entry);
}

事情分发线程唤醒

进入InputDispatcherThread的threadLoop办法: 关于Thread的threadLoop办法解说现已在上篇文章讲过,这儿不再重复。

bool InputDispatcherThread::threadLoop() {
    mDispatcher->dispatchOnce();//mDispatcher 是InputDispatcher类型目标
    return true;
}
void InputDispatcher::dispatchOnce() {  
    ...
    if (!haveCommandsLocked()) {//1
        dispatchOnceInnerLocked(&nextWakeupTime);
    }
    ...
    if (runCommandsLockedInterruptible()) {//2
        nextWakeupTime = LONG_LONG_MIN;
    }
    mLooper->pollOnce(timeoutMillis);
}

注释1处判别是否有指令需求履行,假如没有,则调用dispatchOnceInnerLocked去处理输入事情,假如有,则优先调用runCommandsLockedInterruptible处理事情, 为了让线程能够当即进入事情处理,将nextWakeupTime 设置为LONG_LONG_MIN,这样线程在指令履行结束后能够当即被唤醒去处理输入事情。

从这儿能够看出dispatchOnce首要是做了两个功用:1.履行指令 2.处理输入事情且指令履行优先级高于输入事情处理。 这儿的指令例如:对waitQueue中的事情进行出栈,后面会讲到。

入注释2输入事情处理:dispatchOnceInnerLocked

void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
    switch (mPendingEvent->type) {
    ...
    case EventEntry::TYPE_KEY: {
        KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent);
        ...
        done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);
        break;
    }
​
    case EventEntry::TYPE_MOTION: {
        MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent);
        ...
        done = dispatchMotionLocked(currentTime, typedEntry,
                &dropReason, nextWakeupTime);
        break;
    }
    if (done) {
        ...
        releasePendingEventLocked();
        *nextWakeupTime = LONG_LONG_MIN;  // force next poll to wake up immediately
    }
​
}

dispatchOnceInnerLocked办法首要是依据事情的类型调用不同的处理办法。 咱们拿TYPE_KEY 按键事情来作为主线,关于TYPE_MOTION触摸事情处理逻辑都是差不过的,感兴趣的同学能够自行阅览源码。 在事情处理分发结束后会调用releasePendingEventLocked里边会释放对应的内存资源

mPendingEvent代表当时需求处理的输入事情,传递给dispatchKeyLocked去处理

bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry,
        DropReason* dropReason, nsecs_t* nextWakeupTime) {
    ...
    Vector<InputTarget> inputTargets;
    int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime,
            entry, inputTargets, nextWakeupTime); //1
    if (injectionResult == INPUT_EVENT_INJECTION_PENDING) {
        return false;
    }
    ...
    addMonitoringTargetsLocked(inputTargets);//2
​
    // Dispatch the key.
    dispatchEventLocked(currentTime, entry, inputTargets);  //3
​
}

dispatchKeyLocked在注释1处获取按键事情关于的Window窗口,在注释2处放入一个监听input通道注释3处实践处理事情处。

在解说注释1处获取窗口逻辑前咱们先来看下咱们窗口是怎么传入到InputDispatcher目标中的以及InputChannel概念。

事情分发通道注册

在之前一篇文章解说Window体系的文章中讲过,咱们Window是在ViewRootImpl的setView办法中传入WMS的。

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
    ...
    mInputChannel = new InputChannel();
    res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                            getHostVisibility(), mDisplay.getDisplayId(),
                            mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                            mAttachInfo.mOutsets, mInputChannel);
    if (mInputChannel != null) {
        mInputEventReceiver = new       WindowInputEventReceiver(mInputChannel,Looper.myLooper());
    }
}

mWindowSession是IWindowSession在app端的代理目标。实践履行的是Session类

frameworks\base\services\core\java\com\android\server\wm\Session.java
public int addToDisplay(IWindow window,...InputChannel outInputChannel) {
    return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
            outContentInsets, outStableInsets, outOutsets, outInputChannel);
}

这儿的mService是WMS目标,重点记住终究一个参数outInputChannel

WMS:
public int addWindow(Session session, IWindow client...InputChannel outInputChannel) {
    ...
    final WindowState win = new WindowState(this, session, client, token, parentWindow,
                    appOp[0], seq, attrs, viewVisibility, session.mUid,
                    session.mCanAddInternalSystemWindow);
    ...
    win.openInputChannel(outInputChannel);  //1
    if (focusChanged) {
        mInputMonitor.setInputFocusLw(mCurrentFocus, false /*updateInputWindows*/);//2
    }
}

addWindow的注释1处调用WindowState翻开InputChannel通道,什么是InputChannel通道呢?

进入WindowState的openInputChannel看看:

void openInputChannel(InputChannel outInputChannel) {
    ...
    String name = getName();
    InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);//1
    mInputChannel = inputChannels[0];
    mClientChannel = inputChannels[1];
    mInputWindowHandle.inputChannel = inputChannels[0];
    if (outInputChannel != null) {
        mClientChannel.transferTo(outInputChannel);
        mClientChannel.dispose();
        mClientChannel = null;
    }
    ...
    mService.mInputManager.registerInputChannel(mInputChannel, mInputWindowHandle);//2
}
InputChannel.java:
public static InputChannel[] openInputChannelPair(String name) {
    ...
    return nativeOpenInputChannelPair(name);
}
android_view_InputChannel.cpp:
static jobjectArray android_view_InputChannel_nativeOpenInputChannelPair(JNIEnv* env...) {
    ...
    sp<InputChannel> serverChannel;
    sp<InputChannel> clientChannel;
    status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);
​
    jobjectArray channelPair = env->NewObjectArray(2, gInputChannelClassInfo.clazz, NULL);
    jobject serverChannelObj = android_view_InputChannel_createInputChannel(env,
            std::make_unique<NativeInputChannel>(serverChannel));
    jobject clientChannelObj = android_view_InputChannel_createInputChannel(env,
            std::make_unique<NativeInputChannel>(clientChannel));
    ...
    env->SetObjectArrayElement(channelPair, 0, serverChannelObj);
    env->SetObjectArrayElement(channelPair, 1, clientChannelObj);
    return channelPair;
​
}
​
status_t InputChannel::openInputChannelPair(const String8& name,
        sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) {
    int sockets[2];
    if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
        ...
        return result;
    }
    ...
    outServerChannel = new InputChannel(serverChannelName, sockets[0]);
    outClientChannel = new InputChannel(clientChannelName, sockets[1]);
    return OK;
}

经过以上代码能够看出InputChannel运用的是sockets通讯,且WindowState的openInputChannel中注释1处:InputChannel[] inputChannels = InputChannel.openInputChannelPair(name),返回的inputChannels是一个服务端和客户端的输入通道数组 其间: 下标0:表明服务端的InputChannel 下标1:表明客户端的InputChannel

有了这些基础咱们再来细细品味下这段代码:为什么registerInputChannel传递的是mInputChannel而不是mClientChannel

void openInputChannel(InputChannel outInputChannel) {
    ...
    String name = getName();
    InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);//1
    mInputChannel = inputChannels[0];
    mClientChannel = inputChannels[1];
    mInputWindowHandle.inputChannel = inputChannels[0];
    if (outInputChannel != null) {
        mClientChannel.transferTo(outInputChannel);
        mClientChannel.dispose();
        mClientChannel = null;
    }
    ...
    mService.mInputManager.registerInputChannel(mInputChannel, mInputWindowHandle);//2
}

经过前面剖析知:

  • mInputChannel:服务端的InputChannel
  • mClientChannel:客户端的InputChannel
  • outInputChannel:app层传递进来的InputChannel

Channel模型

  • 1.调用mService.mInputManager.registerInputChannel 将wms在服务端的InputChannel注册到IMS中。这样在IMS输入体系就能够给服务端的InputChannel写入数据,在WMS的客户端InputChannel就能够读取数据

  • 2.调用mClientChannel.transferTo(outInputChannel)

    将app端的InputChannel和wms的客户端InputChannel相关 这样就能够向客户端InputChannel中写入数据然后告诉app端的InputChannel,实践传递给ViewRootImpl目标处理,接着便是View层面的处理了。

这儿咱们继续看注释2处:mService.mInputManager.registerInputChannel 终究会进入native层:

frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp
static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
        jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
    ...
    status_t status = im->registerInputChannel(
            env, inputChannel, inputWindowHandle, monitor);//1
    
}
frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp
status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
        const sp<InputChannel>& inputChannel,
        const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
    return mInputManager->getDispatcher()->registerInputChannel(
            inputChannel, inputWindowHandle, monitor);
}
​
frameworks/native/services/inputflinger/InputDispatcher.cpp
status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel,
        const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
        ...
        sp<Connection> connection = new Connection(inputChannel, inputWindowHandle, monitor);
​
        int fd = inputChannel->getFd();
        mConnectionsByFd.add(fd, connection);
        ...
    
        // registerInputChannel里边传入的monitor是false --> nativeRegisterInputChannel(mPtr, inputChannel, inputWindowHandle, false);
        // 所以这个流程不会将窗口的channel放到mMonitoringChannels里边
        if (monitor) {
            mMonitoringChannels.push(inputChannel);
        }
        ...
​
}

im->registerInputChannel参数阐明:

  • inputChannel:WMS在服务端InputChannel
  • inputWindowHandle:WMS内的一个包括Window一切信息的实例。
  • monitor:值为false,表明不加入监控

InputChannel时序图如下:

终究阶段在InputDispatcher中创立一个Connection并加入到mConnectionsByFd行列中,key为当时inputChannel的fd获取的时分也是经过inputChannel的fd去获取

大约的通讯原理图如下:

经过上面的剖析咱们知道了,WMS和输入体系InputDispatcher运用的socket通讯,在View端,WMS端以及IMS端都有一个InputChannel。

哪个部分有数据需求分发便是将数据写入通道中。 那么接下来咱们看看IMS是怎么选取对应的通道的。

事情分发窗口承认

回到InputDispatcher::dispatchKeyLocked办法

bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry,
        DropReason* dropReason, nsecs_t* nextWakeupTime) {
    ..
    int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime,
            entry, inputTargets, nextWakeupTime); //1
    
​
    dispatchEventLocked(currentTime, entry, inputTargets);  //2
​
}

进入findFocusedWindowTargetsLocked:这个办法便是用来承认当时需求传递事情的窗口
重点看inputTargets的赋值操作

int32_t InputDispatcher::findFocusedWindowTargetsLocked(...inputTargets){
    ...
    addWindowTargetLocked(mFocusedWindowHandle,
            InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0),
            inputTargets);
    ...
}
​
void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
        int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets) {
    inputTargets.push();
​
    const InputWindowInfo* windowInfo = windowHandle->getInfo();
    InputTarget& target = inputTargets.editTop();
    target.inputChannel = windowInfo->inputChannel;
    target.flags = targetFlags;
    target.xOffset = - windowInfo->frameLeft;
    target.yOffset = - windowInfo->frameTop;
    target.scaleFactor = windowInfo->scaleFactor;
    target.pointerIds = pointerIds;
​
}

这儿能够看出findFocusedWindowTargetsLocked办法中对inputTargets的头部数据进行了赋值 其间将windowInfo->inputChannel通道赋值给了target.inputChannel。 那么这个windowInfo是个什么?怎么获取?

windowInfo是InputWindowHandle的属性,而InputWindowHandle传入的是一个mFocusedWindowHandle目标。 从名字也能够大约看出这是一个包括焦点Window信息的目标。

那么这个焦点Window是在哪里赋值的呢?

事情分发窗口注册

咱们回到WMS的addWindow进程。

// frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
@Override
public int addWindow(Session session, IWindow client, int seq,
            WindowManager.LayoutParams attrs, int viewVisibility, int displayId,
            Rect outContentInsets, Rect outStableInsets, Rect outOutsets,
            InputChannel outInputChannel) {
    ...
    if (focusChanged) {
        mInputMonitor.setInputFocusLw(mCurrentFocus, false /*updateInputWindows*/);
    }
    mInputMonitor.updateInputWindowsLw(false /*force*/);
    ...
}

焦点产生改动的时分会调用setInputFocusLw办法和updateInputWindowsLw updateInputWindowsLw经过层层调用终究会走到InputDispatcher::setInputWindows中

// frameworks/native/services/inputflinger/InputDispatcher.cpp
void InputDispatcher::setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) {
    ...
    mWindowHandles = inputWindowHandles;
​
    sp<InputWindowHandle> newFocusedWindowHandle;
    ...
    for (size_t i = 0; i < mWindowHandles.size(); i++) {
        const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
        ...
        if (windowHandle->getInfo()->hasFocus) {
            newFocusedWindowHandle = windowHandle;
        }
        ...
        mFocusedWindowHandle = newFocusedWindowHandle;
    }
    ...
​
}

看到了这儿对mWindowHandles和mFocusedWindowHandle做了赋值。

  • mWindowHandles:代表一切Window的Handler目标
  • mFocusedWindowHandle:表明焦点Window的Handler目标 经过这些代码就让咱们IMS中获取到了需求处理的焦点Window。

window窗口赋值时序图如下:

事情分发终究处理

继续回到回到InputDispatcher::dispatchKeyLocked办法的注释2

dispatchEventLocked(currentTime, entry, inputTargets); //2

void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,
        EventEntry* eventEntry, const Vector<InputTarget>& inputTargets) {
    ...
    for (size_t i = 0; i < inputTargets.size(); i++) {
        const InputTarget& inputTarget = inputTargets.itemAt(i);
​
        ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel);
        if (connectionIndex >= 0) {
            sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
            prepareDispatchCycleLocked(currentTime, connection, eventEntry, &inputTarget);
        }
        ...
    }
​
}
​
ssize_t InputDispatcher::getConnectionIndexLocked(const sp<InputChannel>& inputChannel) {
    ssize_t connectionIndex = mConnectionsByFd.indexOfKey(inputChannel->getFd());
    if (connectionIndex >= 0) {
        sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
        if (connection->inputChannel.get() == inputChannel.get()) {
            return connectionIndex;
        }
    }
​
    return -1;
​
}

dispatchEventLocked首要效果:轮询inputTargets,依据inputTarget.inputChannel获取其在mConnectionsByFd中的索引,依据索引获取Connection目标,并调用prepareDispatchCycleLocked进行处理。 prepareDispatchCycleLocked办法内部调用了enqueueDispatchEntriesLocked办法

void InputDispatcher::enqueueDispatchEntriesLocked(connection,..){
    // Enqueue dispatch entries for the requested modes.
    enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,...);//1
    ...
    // If the outbound queue was previously empty, start the dispatch cycle going.
    if (wasEmpty && !connection->outboundQueue.isEmpty()) {//2
        startDispatchCycleLocked(currentTime, connection);//3
    }
}
​
void InputDispatcher::enqueueDispatchEntryLocked(
        const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget,
        int32_t dispatchMode) {
    ...
    DispatchEntry* dispatchEntry = new DispatchEntry(eventEntry,
            inputTargetFlags, inputTarget->xOffset, inputTarget->yOffset,
            inputTarget->scaleFactor);
​
    switch (eventEntry->type) {
        case EventEntry::TYPE_KEY: {
            KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry);
            dispatchEntry->resolvedAction = keyEntry->action;
            dispatchEntry->resolvedFlags = keyEntry->flags;
            ...
            break;
        }
        ...
    }
    ...
    connection->outboundQueue.enqueueAtTail(dispatchEntry);
    ...
​
}

在注释1处enqueueDispatchEntryLocked办法中会将输入事情重新封装为一个DispatchEntry并压入connection的outboundQueue行列中。 然后在注释2处判别假如事情不为空,则调用startDispatchCycleLocked循环发送输入事情

void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
        const sp<Connection>& connection) {
    while (connection->status == Connection::STATUS_NORMAL
            && !connection->outboundQueue.isEmpty()) {
        DispatchEntry* dispatchEntry = connection->outboundQueue.head;
        ...
        EventEntry* eventEntry = dispatchEntry->eventEntry;
        switch (eventEntry->type) {
        case EventEntry::TYPE_KEY: {
            KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry);
​
            // Publish the key event.
            status = connection->inputPublisher.publishKeyEvent(dispatchEntry->seq,
                    keyEntry->deviceId, keyEntry->source,
                    dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags,
                    keyEntry->keyCode, keyEntry->scanCode,
                    keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime,
                    keyEntry->eventTime);
            break;
        }
        ...
        connection->outboundQueue.dequeue(dispatchEntry);
        connection->waitQueue.enqueueAtTail(dispatchEntry)
    }   
    ...
​
}

startDispatchCycleLocked办法中调用publishKeyEvent,其内部会将事情写入到WMS传递下来的InputChannel通道中。这样WMS端的InputChannel就能够经过socket获取到事情信息。在发送结束后会将事情移出connection->outboundQueue行列,并放入到waitQueue等候行列中,等候事情处理结束后再移出

waitQueue用来监听当时分发给WMS的输入事情是否现已被处理结束。什么时分知道事情处理结束呢?

在InputDispatcher::registerInputChannel办法里边注册了handleReceiveCallback回调:

status_t InputDispatcher::registerInputChannel(...) {
        ...
        mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this);
        ...
}

当app层的事情处理结束之后就会回调handleReceiveCallback

int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) {
    InputDispatcher* d = static_cast<InputDispatcher*>(data);
    ...
    d->finishDispatchCycleLocked(currentTime, connection, seq, handled);
    ...
    d->runCommandsLockedInterruptible();
    ...
}

这儿会先调用InputDispatcher::finishDispatchCycleLocked去往mCommandQueue里边加入一个履行InputDispatcher:: doDispatchCycleFinishedLockedInterruptible的Command:

​
void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(
        CommandEntry* commandEntry) {
    sp<Connection> connection = commandEntry->connection;
    ...
    DispatchEntry* dispatchEntry = connection->findWaitQueueEntry(seq);
    ...
    if (dispatchEntry == connection->findWaitQueueEntry(seq)) {
        connection->waitQueue.dequeue(dispatchEntry);
        ...
    }
}

doDispatchCycleFinishedLockedInterruptible中会将connection->waitQueue出栈,这样整个输入体系的分发进程就闭环

事情分发流程小结

下面对流程做一个总结:

  • 1.ViewRootImpl在setView办法会创立一个InputChannel通道,并在将Window增加给WMS的进程时,以参数传递给WMS。
  • 2.WMS在增加Window的进程中会调用updateInputWindows,这个办法终究会调用到InputDispatcher::setInputWindows中, 并给InputDispatcher的Window行列以及焦点Window赋值,这样IMS就能够找到对应的Window了
  • 3.在WMS在增加Window的进程中还会创立一个socketpair通道的InputChannel,其间客户端的socket与app层的InputChannel相关,用于WMS与app通讯 服务端的socket传递给IMS,用于IMS和WMS通讯。
  • 4.客户端在接收到输入事情后,会依据当时焦点Window的的InputChannel找到对应的Connection衔接,这个Connection用于与WMS进行通讯,内部其实便是运用前面的socket通讯。
  • 5.事情分发后将输入事情放入到waitQueue中,等候事情处理结束后,将事情移出waitQueue

问题复盘

那么关于最初的两个问题:

  • 问题1窗口什么时分传入IMS输入体系的?IMS又是怎么找到对应的Window窗口的? ViewRootImpl在setView办法中,调用addToDisplay将Window传递给WMS的时分,这个时分会调用InputMonitor.updateInputWindowsLw办法,终究会调用到InputDispatcher::setInputWindows,这儿面会对IMS的WIndow属性进行赋值。IMS依据前面赋值的Window属性,就能够找到对应的焦点Window

  • 问题2:IMS和WMS是经过什么方式通讯将事情分发出去的?

    IMS和WMS是经过InputChannel通道进行通讯的,WMS在Window增加进程中会创立一个socket通道,将server端通道传递给IMS,而client端通道用于WMS中接收server端事情,server端依据对应的Window,找到对应的Connection,然后运用Connection进行通讯,而Connection内部便是经过socket进行通讯的

总结

因为篇幅问题,且大部分都是源码性质的,所以关于Android输入事情传递的进程运用了两篇文章来总结。

“framework必会”系列:Android Input体系(一)事情读取机制

“framework必会”系列:Android Input体系(二)事情分发机制

关于事情在View的传递进程相信咱们都比较清楚,这儿不在描绘了。

假如这篇文章对你有协助,请帮助点个赞,重视下,你的点赞和重视是我继续发明的最大动力

笔者大众号:小余的自习室