Android:输入系统(InputChannel)

http://blog.csdn.net/itleaks/article/details/27165657

前面的“锤子快捷键”相关文章已经分析了输入事件的读取,处理,分发。我们知道事件的传递是以window为单位传递的,即server只负责将事件传递给某一个或者多个window,window然后再将事件传递给某一个具体的view。一个activity或者dialog对应一个window,但是事件只传递给合适的window,比如对于按键事件,就必须是获得焦点的window,也就是说只能传递给一个window,通常是最上面的程序。找到了合适的window,然后就是将事件添加到window的Connection的事件队列上。其实,到这为止输入事件还只是在server端,即system_server这个进程里,要想让程序获取到事件,肯定必须将事件信息传递到程序端的进程里。这个就是Connection的实现问题了,这个connection的真正逻辑是InputChannel, InputChannel其实就是linux unix socket的一种封装, unixsocket是linux的一种跨进程通信方式。系统创建InputChannel对即unix socket对,系统server端和程序client各只有其中一个,这样通过unix socket就可以给对方发送消息,而这里的事件就是通过这种方式从系统进程传递到程序进程的。整个系统框架图如下:
Android:输入系统(InputChannel)_第1张图片

系统server端的InputChannel

系统InputChannel的整个处理逻辑如下:
Android:输入系统(InputChannel)_第2张图片

Server端 InputChannel的创建

Server端 InputChannel是在window被创建的时候创建的:

    //addWindow会创建一个channel对,其实上就是unix socket对,其中一个unix socket
    //通过传入参数outInputChannel被传递到程序端,
    //另外一个unix socket保存在server的window中并注册到native的InputManager
    public int addWindow(Session session, IWindow client, int seq,
            WindowManager.LayoutParams attrs, int viewVisibility, int displayId,
            Rect outContentInsets, InputChannel outInputChannel) {
            //创建window的数据对象WindowState
            win = new WindowState(this, session, client, token,
                    attachedWindow, appOp[0], seq, attrs, viewVisibility, displayContent);
            if (outInputChannel != null && (attrs.inputFeatures & 
                WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
                String name = win.makeInputChannelName();
                //创建channel对,即会返回两个InputChannel
                InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);
                //一个unix socket保存到window里
                win.setInputChannel(inputChannels[0]);
                //另外一个unix socket传递到程序端
                inputChannels[1].transferTo(outInputChannel);
                //这个函数很重要,这个会将server端的unix socket注册到native层
                //的InputManager,  win.mInputChannel就是上面的inputChannels[0]
                mInputManager.registerInputChannel(win.mInputChannel, 
                       win.mInputWindowHandle);
            }
        }
        return res;
}

public static InputChannel[] openInputChannelPair(String name) {
        return nativeOpenInputChannelPair(name);
}

static jobjectArray android_view_InputChannel_nativeOpenInputChannelPair(JNIEnv* env,
        jclass clazz, jstring nameObj) {
    sp serverChannel;
    sp clientChannel;
     //创建input channel对
    status_t result = InputChannel::openInputChannelPair(name, 
         serverChannel, clientChannel);
    jobjectArray channelPair = env->NewObjectArray(2, gInputChannelClassInfo.clazz, NULL);
    //创建inputChannel对应的java对象
    jobject serverChannelObj = android_view_InputChannel_createInputChannel(env,
            new NativeInputChannel(serverChannel));
    jobject clientChannelObj = android_view_InputChannel_createInputChannel(env,
            new NativeInputChannel(clientChannel));
    //将两个channel放到channel数组中
    env->SetObjectArrayElement(channelPair, 0, serverChannelObj);
    env->SetObjectArrayElement(channelPair, 1, clientChannelObj);
    return channelPair;
}
//InputTransport.cpp
status_t InputChannel::openInputChannelPair(const String8& name,
        sp& outServerChannel, sp& outClientChannel) {
    int sockets[2];
    //很早的android 版本是使用双向管道实现的,而是现在是使用unix socket双通道
    //来通信
    if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
        return result;
    }

    int bufferSize = SOCKET_BUFFER_SIZE;
    setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
    setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
    setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
    setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));

    String8 serverChannelName = name;
    serverChannelName.append(" (server)");
    //创建InputChannel,并把通信文件句柄传入
    outServerChannel = new InputChannel(serverChannelName, sockets[0]);

    String8 clientChannelName = name;
    clientChannelName.append(" (client)");
    //创建InputChannel,并把通信文件句柄传入
    outClientChannel = new InputChannel(clientChannelName, sockets[1]);
    return OK;
}

Server端 InputChannel事件监听器安装

InputDispatcher要能够发送事件数据,必须的要让其知道对应的window的InputChannel,这个通过注册实现的。

 public void registerInputChannel(InputChannel inputChannel,
            InputWindowHandle inputWindowHandle) {        
        nativeRegisterInputChannel(mPtr, inputChannel, inputWindowHandle, false);
}

status_t NativeInputManager::registerInputChannel(JNIEnv* env,
        const sp& inputChannel,
        const sp& inputWindowHandle, bool monitor) {
    //调用InputDispatcher的函数
    return mInputManager->getDispatcher()->registerInputChannel(
            inputChannel, inputWindowHandle, monitor);
}

status_t InputDispatcher::registerInputChannel(const sp& inputChannel,
        const sp& inputWindowHandle, bool monitor) {
    { // acquire lock
        AutoMutex _l(mLock);
        //这个将inputChannel封装为Connection
        sp connection = new Connection(inputChannel, 
inputWindowHandle, monitor);
            //这个就是unix socket文件句柄
        int fd = inputChannel->getFd();
        //将connection保存到映射表中
        mConnectionsByFd.add(fd, connection);
        //监听该unix socket文件,当unix socket有数据时即client发送消息过来了,
        //函数handleReceiveCallback就会被执行
        mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this);
    } // release lock

    // Wake the looper because some connections have changed.
    mLooper->wake();
    return OK;
}

Server端的InputChannel事件数据发送

在上几篇文章“锤子快捷键配置”中,已经讲到了事件分发,并最后将事件放到了connection的事件队列中,InputChannel事件发送就是从开始的

void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime,
        const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) {
    bool wasEmpty = connection->outboundQueue.isEmpty();
    // Enqueue dispatch entries for the requested modes.
    enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
            InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT);
    enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
            InputTarget::FLAG_DISPATCH_AS_OUTSIDE);
    enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
            InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER);
    enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
            InputTarget::FLAG_DISPATCH_AS_IS);
    enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
            InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT);
    enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
            InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER);

    // 原来是空,现在不空,则立刻分发事件
    if (wasEmpty && !connection->outboundQueue.isEmpty()) {
        startDispatchCycleLocked(currentTime, connection);
    }
} 

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);

            //真正发送事件.
            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;
        }

        // Check the result.
        if (status) {
            if (status == WOULD_BLOCK) {
                if (connection->waitQueue.isEmpty()) {
                } else {
                    connection->inputPublisherBlocked = true;
                }
            }
            //发送成功,返回执行下一次循环
            return;
        }

        // 事件发送失败,重新放进待发送队列
        connection->outboundQueue.dequeue(dispatchEntry);
        connection->waitQueue.enqueueAtTail(dispatchEntry);
    }
}

status_t InputPublisher::publishKeyEvent(
        uint32_t seq,
        int32_t deviceId,
        int32_t source,
        int32_t action,
        int32_t flags,
        int32_t keyCode,
        int32_t scanCode,
        int32_t metaState,
        int32_t repeatCount,
        nsecs_t downTime,
        nsecs_t eventTime) {
    InputMessage msg;
    //将输入事件转化为unix socket通信的格式
    msg.header.type = InputMessage::TYPE_KEY;
    msg.body.key.seq = seq;
    msg.body.key.deviceId = deviceId;
    msg.body.key.source = source;
    msg.body.key.action = action;
    msg.body.key.flags = flags;
    msg.body.key.keyCode = keyCode;
    msg.body.key.scanCode = scanCode;
    msg.body.key.metaState = metaState;
    msg.body.key.repeatCount = repeatCount;
    msg.body.key.downTime = downTime;
msg.body.key.eventTime = eventTime;
    //调用unix socket消息发送机制
    return mChannel->sendMessage(&msg);
}

status_t InputChannel::sendMessage(const InputMessage* msg) {
    size_t msgLength = msg->size();
    ssize_t nWrite;
do {
    //通过unix socket将事件数据发送到程序端
        nWrite = ::send(mFd, msg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
    } while (nWrite == -1 && errno == EINTR);
    return OK;
}

程序client端的InputChannel

client的InputChannel相关的处理逻辑如下:
Android:输入系统(InputChannel)_第3张图片

Client 端的InputChannel创建

Client接受事件,肯定必须先获得inputChannel,这个是在addWindow时系统返回回来的。

public void setView(View view, WindowManager.LayoutParams attrs, 
View panelParentView) {
        synchronized (this) {
                if ((mWindowAttributes.inputFeatures&
                   WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
                    mInputChannel = new InputChannel();
                }
                try {
                    //该函数会返回一个InputChannel
                    res = mWindowSession.addToDisplay(mWindow, mSeq,
                            mWindowAttributes,
                            getHostVisibility(), mDisplay.getDisplayId(),
                            mAttachInfo.mContentInsets, mInputChannel);
                }
                if (mInputChannel != null) {
                    if (mInputQueueCallback != null) {
                        mInputQueue = new InputQueue();
                        mInputQueueCallback.onInputQueueCreated(mInputQueue);
                    }
                    //为InputChannel注册监听器
                    mInputEventReceiver = new WindowInputEventReceiver(
                         mInputChannel,
                         Looper.myLooper());
                }
"code" class="java" style="font-size: 14px;">          }

Client端的 InputChannel监听器安装

InputChannel监听器安装在WindowInputEventReceiver初始化的时候

final class WindowInputEventReceiver extends InputEventReceiver {
        public WindowInputEventReceiver(InputChannel inputChannel, Looper looper) {
            super(inputChannel, looper);
        }
}
public InputEventReceiver(InputChannel inputChannel, Looper looper) {
        mInputChannel = inputChannel;
        mMessageQueue = looper.getQueue();
        mReceiverPtr = nativeInit(new WeakReference(this),
                inputChannel, mMessageQueue);

        mCloseGuard.open("dispose");
 }

static jint nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak,
        jobject inputChannelObj, jobject messageQueueObj) {
    //获取native层的InputChannel
    sp inputChannel = android_view_InputChannel_getInputChannel(env,
            inputChannelObj);
    //获取java层InputEventReceiver对象的native层的消息队列
    sp messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
        //创建native对应的InputEventReceiver对象
    sp receiver = new NativeInputEventReceiver(env,
            receiverWeak, inputChannel, messageQueue);
    //这个是真正安装监听的函数
    status_t status = receiver->initialize();
    return reinterpret_cast(receiver.get());
}


status_t NativeInputEventReceiver::initialize() {
    //安装监听器
    setFdEvents(ALOOPER_EVENT_INPUT);
    return OK;
}

void NativeInputEventReceiver::setFdEvents(int events) {
    if (mFdEvents != events) {
        mFdEvents = events;
        int fd = mInputConsumer.getChannel()->getFd();
        if (events) {
           //用looper监听inputChannel对应的unix socket文件句柄
            mMessageQueue->getLooper()->addFd(fd, 0, events, this, NULL);
        }
    }
}

int Looper::addFd(int fd, int ident, int events, ALooper_callbackFunc callback, void* data) {
    return addFd(fd, ident, events, callback ? new SimpleLooperCallback(callback) : NULL, data);
}

int Looper::addFd(int fd, int ident, int events, const sp& callback, void* data) {
    { // acquire lock
        AutoMutex _l(mLock);
        //将监听参数封装
        Request request;
        request.fd = fd;
        request.ident = ident;
        //这个很重要,当被监听的文件发生变化时就会调用该callback函数
        request.callback = callback;
        request.data = data;

        ssize_t requestIndex = mRequests.indexOfKey(fd);
        if (requestIndex < 0) {
            //epoll该文件,也就是讲unix socket文件添加到监听文件列表中
            int epollResult = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, fd, & eventItem);
            mRequests.add(fd, request);
        }
    } // release lock
    return 1;
}

Client端的InputChannel中的事件接收

从上面可以看出,Java的InputEventReceiver层的native层的NativeInputEventReceiver负责监听事件,当有事件时,就会调用它。

int Looper::pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData) {
    int result = 0;
    for (;;) {
        while (mResponseIndex < mResponses.size()) {
            const Response& response = mResponses.itemAt(mResponseIndex++);
            int ident = response.request.ident;
            if (ident >= 0) {
                int fd = response.request.fd;
                int events = response.events;
                void* data = response.request.data;
                if (outFd != NULL) *outFd = fd;
                if (outEvents != NULL) *outEvents = events;
                if (outData != NULL) *outData = data;
                return ident;
            }
        }
        result = pollInner(timeoutMillis);
    }
}

int Looper::pollInner(int timeoutMillis) {
    struct epoll_event eventItems[EPOLL_MAX_EVENTS];
    //等待消息
    int eventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);
    for (int i = 0; i < eventCount; i++) {
        int fd = eventItems[i].data.fd;
        uint32_t epollEvents = eventItems[i].events;
        if (fd == mWakeReadPipeFd) {
            if (epollEvents & EPOLLIN) {
                awoken();
            }
        } else {
            ssize_t requestIndex = mRequests.indexOfKey(fd);
            if (requestIndex >= 0) {
                int events = 0;
                if (epollEvents & EPOLLIN) events |= ALOOPER_EVENT_INPUT;
                if (epollEvents & EPOLLOUT) events |= ALOOPER_EVENT_OUTPUT;
                if (epollEvents & EPOLLERR) events |= ALOOPER_EVENT_ERROR;
                if (epollEvents & EPOLLHUP) events |= ALOOPER_EVENT_HANGUP;
                //将事件放到事件队列上
                pushResponse(events, mRequests.valueAt(requestIndex));
            }
        }
    }
Done: ;
    //处理前面加入的response事件
    for (size_t i = 0; i < mResponses.size(); i++) {
        Response& response = mResponses.editItemAt(i);
        if (response.request.ident == ALOOPER_POLL_CALLBACK) {
            int fd = response.request.fd;
            int events = response.events;
            void* data = response.request.data;
            // 下面的callback就是 NativeInputEventRecieverd
            int callbackResult = response.request.callback->handleEvent(fd, events, data);
            if (callbackResult == 0) {
                removeFd(fd);
            }
            response.request.callback.clear();
            result = ALOOPER_POLL_CALLBACK;
        }
    }
    return result;
}


int NativeInputEventReceiver::handleEvent(int receiveFd, int events, void* data) {

if (events & ALOOPER_EVENT_INPUT) {
        JNIEnv* env = AndroidRuntime::getJNIEnv();
        //处理事件
        status_t status = consumeEvents(env, false /*consumeBatches*/, -1, NULL);
        mMessageQueue->raiseAndClearException(env, "handleReceiveCallback");
        return status == OK || status == NO_MEMORY ? 1 : 0;
}
    return 1;
}

status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env,
        bool consumeBatches, nsecs_t frameTime, bool* outConsumedBatch) {
    for (;;) {
        uint32_t seq;
        InputEvent* inputEvent;
        //从buffer中还原出事件
        status_t status = mInputConsumer.consume(&mInputEventFactory,
                consumeBatches, frameTime, &seq, &inputEvent);
        if (!skipCallbacks) {
            jobject inputEventObj;
            switch (inputEvent->getType()) {
            case AINPUT_EVENT_TYPE_KEY:
                //转换为java层的InputEvent
                inputEventObj = android_view_KeyEvent_fromNative(env,
                        static_cast(inputEvent));
                break;
            }
            if (inputEventObj) {
                //这个就会调用到java层的函数InputEventReceiver->dispatchInputEvent
                env->CallVoidMethod(receiverObj.get(),
                        gInputEventReceiverClassInfo.dispatchInputEvent, seq,
 inputEventObj);
            }
        }
    }
}

Client端对输入事件的处理

输入事件,比如按键事件并不是全部被window的view处理了,比如Back键,如果此时系统输入法是显示的,其实该键首先会去关闭输入法,而window的view是接收不到这个键的,这个就是事件处理器链实现的,这个链上又各种处理器,它们按照处理的优先顺序添加咋链表上

输入事件处理链

public abstract class InputEventReceiver {
    //native收到输入事件是最终会回调到该函数
    private void dispatchInputEvent(int seq, InputEvent event) {
        mSeqMap.put(event.getSequenceNumber(), seq);
        onInputEvent(event);
    }
}

   final class WindowInputEventReceiver extends InputEventReceiver {
        @Override
        public void onInputEvent(InputEvent event) {
            enqueueInputEvent(event, this, 0, true);
        }
    }

    void enqueueInputEvent(InputEvent event) {
        enqueueInputEvent(event, null, 0, false);
    }

    void enqueueInputEvent(InputEvent event,
            InputEventReceiver receiver, int flags, boolean processImmediately) {
        QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);
        QueuedInputEvent last = mPendingInputEventTail;
        if (last == null) {
            mPendingInputEventHead = q;
            mPendingInputEventTail = q;
        } else {
            last.mNext = q;
            mPendingInputEventTail = q;
        }
        mPendingInputEventCount += 1;
        if (processImmediately) {
            //处理事件
            doProcessInputEvents();
        } else {
            scheduleProcessInputEvents();
        }
    }

    void doProcessInputEvents() {
        // 遍历所有的输入事件
        while (mPendingInputEventHead != null) {
            QueuedInputEvent q = mPendingInputEventHead;
            mPendingInputEventHead = q.mNext;
            if (mPendingInputEventHead == null) {
                mPendingInputEventTail = null;
            }
            q.mNext = null;

            mPendingInputEventCount -= 1;
            //处理事件
            deliverInputEvent(q);
        }
    }

    private void deliverInputEvent(QueuedInputEvent q) {
        try {
            //检测ime相关module是否需要处理该输入事件,比如back键,是需要先
            //让IME处理,这个时候需要先交给mFirstPostImeInputStage处理
            InputStage stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
            if (stage != null) {
                stage.deliver(q);
            } else {
                //
                finishInputEvent(q);
            }
        }
    }

       //大部分时候stage= mFirstInputStage,这个变量在最开始的时候赋值
       InputStage syntheticInputStage = new SyntheticInputStage();
       InputStage viewPostImeStage = new ViewPostImeInputStage(syntheticInputStage);
       InputStage nativePostImeStage = new NativePostImeInputStage(viewPostImeStage,
                  "aq:native-post-ime:" + counterSuffix);
       InputStage earlyPostImeStage = new EarlyPostImeInputStage(nativePostImeStage);
       InputStage imeStage = new ImeInputStage(earlyPostImeStage,
                        "aq:ime:" + counterSuffix);
       InputStage viewPreImeStage = new ViewPreImeInputStage(imeStage);
       InputStage nativePreImeStage = new NativePreImeInputStage(viewPreImeStage,
                        "aq:native-pre-ime:" + counterSuffix);
       mFirstInputStage = nativePreImeStage;
       mFirstPostImeInputStage = earlyPostImeStage;<div>
       abstract class InputStage {
        public final void deliver(QueuedInputEvent q) {
            if ((q.mFlags & QueuedInputEvent.FLAG_FINISHED) != 0) {
                //已经被处理了,则让后面的处理
                forward(q);
            } else if (shouldDropInputEvent(q)) {
                finish(q, false);
            } else {
                //没有处理,自己开始处理该事件
                apply(q, onProcess(q));
            }
        }

        protected void apply(QueuedInputEvent q, int result) {
            if (result == FORWARD) {
                forward(q);
            } else if (result == FINISH_HANDLED) {
                finish(q, true);
            }
        }

        protected void forward(QueuedInputEvent q) {
            onDeliverToNext(q);
        }

        protected void onDeliverToNext(QueuedInputEvent q) {
            //如果下一个事件处理器不为空,则让下一个事件处理器处理
            if (mNext != null) {
                mNext.deliver(q);
            } else {
               //所有的都处理器都完成了处理,调用finish告知server端事件已经被处理
                finishInputEvent(q);
            }
        }
}

    //将事件发送给view的事件处理器是ViewPostImeInputStage
    final class ViewPostImeInputStage extends InputStage {
        @Override
        protected int onProcess(QueuedInputEvent q) {
            if (q.mEvent instanceof KeyEvent) {
                return processKeyEvent(q);
            }
        }

        private int processKeyEvent(QueuedInputEvent q) {
            final KeyEvent event = (KeyEvent)q.mEvent;

            if (event.getAction() != KeyEvent.ACTION_UP) {
                // If delivering a new key event, make sure the window is
                // now allowed to start updating.
                handleDispatchDoneAnimating();
            }

            // 向view发送按键事件
            if (mView.dispatchKeyEvent(event)) {
                return FINISH_HANDLED;
            }

            // 系统默认按键处理,比如CAMERA快捷键处理
            if (mFallbackEventHandler.dispatchKeyEvent(event)) {
                return FINISH_HANDLED;
            }
            return FORWARD;
        }
    } 

从上面的逻辑可以看出处理器的处理有限顺序是:
NativePreImeInputStage->ViewPreImeInputStage-> ImeInputStage->
EarlyPostImeInputStage-> NativePostImeInputStage->ViewPostImeInputStage->
SyntheticInputStage

Back按键如何结束Activity

刚刚前面说到,view获得输入事件是由ViewPostImeInputStage传递过来的。ViewPostImeInputStage会将事件传递给activity的根View —DecorView

   private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (!isDestroyed()) {
                //首先让callback处理,然后调用super的接口
                final Callback cb = getCallback() && mFeatureId < 0 ? 
cb.dispatchKeyEvent(event)
                        : super.dispatchKeyEvent(event);
                if (handled) {
                    return true;
                }
            }

            return isDown ? PhoneWindow.this.onKeyDown(mFeatureId, event.getKeyCode(), event)
                    : PhoneWindow.this.onKeyUp(mFeatureId, event.getKeyCode(), event);
        }
   }

   //上面的getCallback的返回值就是Activity,故其有很高的优先级获取并处理这些按键。
   public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (getApplicationInfo().targetSdkVersion
                    >= Build.VERSION_CODES.ECLAIR) {
            } else {
                //这个就是结束activity的函数
                onBackPressed();
            }
            return true;
        }
    }

如果非back按键,则会调用super即View.dispatchKeyEvent的接口,view的事件接收及处理就是从这开始的。

View如何获取按键

由于DecorView继承FrameLayout,它自然是一个ViewGroup,所以我们来看下ViewGroup的dispatchKeyEvent。

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onKeyEvent(event, 1);
        }

        if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
                == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
            //调用view的接口
            if (super.dispatchKeyEvent(event)) {
                return true;
            }
        } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
                == PFLAG_HAS_BOUNDS) {
            //向获得焦点的view传递事件
            if (mFocused.dispatchKeyEvent(event)) {
                return true;
            }
        }
        return false;
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.dispatch(this, mAttachInfo != null
                ? mAttachInfo.mKeyDispatchState : null, this)) {
            return true;
        }
        return false;
    }

    public final boolean dispatch(Callback receiver, DispatcherState state,
            Object target) {
        switch (mAction) {
            case ACTION_DOWN: {
                mFlags &= ~FLAG_START_TRACKING;
                //这个就是我们的常见的onKeyDown,onKeyUp接口的调用
                boolean res = receiver.onKeyDown(mKeyCode, this);
                return res;
            }
        }
        return false;
    }

Camera等快捷键是如何传递处理的

如果view没有处理按键,则最后会给mFallbackEventHandler一个机会处理按键,Camera等快捷键就是由这个handler处理的,下面来看看。

 public ViewRootImpl(Context context, Display display) {       
mFallbackEventHandler= PolicyManager.makeNewFallbackEventHandler(context);
}

public class Policy implements IPolicy {
public FallbackEventHandler makeNewFallbackEventHandler(Context context) {
   return new PhoneFallbackEventHandler(context);
}
}

public class PhoneFallbackEventHandler implements FallbackEventHandler {
    public boolean dispatchKeyEvent(KeyEvent event) {

        final int action = event.getAction();
        final int keyCode = event.getKeyCode();

        if (action == KeyEvent.ACTION_DOWN) {
            return onKeyDown(keyCode, event);
        } else {
            return onKeyUp(keyCode, event);
        }
    }

    boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_CAMERA: {
                if (event.getRepeatCount() == 0) {
                } else if (event.isLongPress() && dispatcher.isTracking(event)) {
                    //启动拍照程序
                    Intent intent = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
                    intent.putExtra(Intent.EXTRA_KEY_EVENT, event);
                    mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF,
                            null, null, null, 0, null, null);
                }
                return true;
            }
        }
        return false;
    }

输入事件处理完成通知

client将事件处理完了,必须通知server已经完成对该事件的处理,否则server一直在等待事件完成而不能发送后面的事件。

private void finishInputEvent(QueuedInputEvent q) {
        if (q.mReceiver != null) {
            boolean handled = (q.mFlags & QueuedInputEvent.FLAG_FINISHED_HANDLED) != 0;              
            // mReceiver是InputEventReciever
            q.mReceiver.finishInputEvent(q.mEvent, handled);
        }
        recycleQueuedInputEvent(q);
}

public abstract class InputEventReceiver {
    public final void finishInputEvent(InputEvent event, boolean handled) {
        {
            int index = mSeqMap.indexOfKey(event.getSequenceNumber());
            if (index < 0) {
            } else {
                //又调回native层
                nativeFinishInputEvent(mReceiverPtr, seq, handled);
            }
        }
        event.recycleIfNeededAfterDispatch();
    }
}


status_t NativeInputEventReceiver::finishInputEvent(uint32_t seq, bool handled) {
    //告知server端client已经处理完成inputEvent
    status_t status = mInputConsumer.sendFinishedSignal(seq, handled);
    return status;
}


status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
    size_t seqChainCount = mSeqChains.size();
    if (seqChainCount) {
        uint32_t currentSeq = seq;
        uint32_t chainSeqs[seqChainCount];
        size_t chainIndex = 0;
        for (size_t i = seqChainCount; i-- > 0; ) {
             const SeqChain& seqChain = mSeqChains.itemAt(i);
             if (seqChain.seq == currentSeq) {
                 currentSeq = seqChain.chain;
                 chainSeqs[chainIndex++] = currentSeq;
                 mSeqChains.removeAt(i);
             }
        }
        status_t status = OK;
        while (!status && chainIndex-- > 0) {
            status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
        }
    }

    // Send finished signal for the last message in the batch.
    return sendUnchainedFinishedSignal(seq, handled);
}

status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
    InputMessage msg;
    msg.header.type = InputMessage::TYPE_FINISHED;
    msg.body.finished.seq = seq;
    msg.body.finished.handled = handled;
    return mChannel->sendMessage(&msg);
}

//这个和server端发送事件过来一样的,只不过这次是client发送消息给server
status_t InputChannel::sendMessage(const InputMessage* msg) {
    size_t msgLength = msg->size();
    ssize_t nWrite;
    do {
        nWrite = ::send(mFd, msg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
    } while (nWrite == -1 && errno == EINTR);
    return OK;
}

你可能感兴趣的:(android,framework)