欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Input输入系统之四:KeyEvent事件中的InputChannel通信

程序员文章站 2022-06-05 16:05:42
...

Android Input输入系统之一:KeyEvent事件监听及事件分发流程
Android Input输入系统之二:KeyEvent注入事件及事件分发流程
Android Input输入系统之三:KeyEvent事件分发和上层应用层对事件的接收
Android Input输入系统之四:KeyEvent事件中的InputChannel通信

经过一系列的分析,key事件的分发,中间有一个InputChannel通信。

key事件发送端,InputDispatcher,属于system_server进程;
key事件接收端,ViewRootImpl,是应用程序进程。

这两者之间的跨进程通信就是通过InputChannel实现的。

重点还是看一下InputChannel的创建过程。

/** A window in the window manager. */
public class WindowState extends WindowContainer<WindowState> implements WindowManagerPolicy.WindowState {
    void openInputChannel(InputChannel outInputChannel) {
        if (mInputChannel != null) {
            throw new IllegalStateException("Window already has an input channel.");
        }
        String name = getName();
        InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);
        mInputChannel = inputChannels[0];
        mClientChannel = inputChannels[1];
        mInputWindowHandle.inputChannel = inputChannels[0];
        if (outInputChannel != null) {
            mClientChannel.transferTo(outInputChannel);
            mClientChannel.dispose();
            mClientChannel = null;
        } else {
            // If the window died visible, we setup a dummy input channel, so that taps
            // can still detected by input monitor channel, and we can relaunch the app.
            // Create dummy event receiver that simply reports all events as handled.
            mDeadWindowEventReceiver = new DeadWindowEventReceiver(mClientChannel);
        }
        mService.mInputManager.registerInputChannel(mInputChannel, mInputWindowHandle);
    }

    @Override
    String getName() {
        return Integer.toHexString(System.identityHashCode(this))
                + " " + getWindowTag();
    }

    CharSequence getWindowTag() {
        CharSequence tag = mAttrs.getTitle();
        if (tag == null || tag.length() <= 0) {
            tag = mAttrs.packageName;
        }
        return tag;
    }
}

name是window的title名或者包名。

InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);

openInputChannelPair(),创建了一对InputChannel分别是mInputChannel和mClientChannel,mInputChannel是服务端的channel,mClientChannel是客户端的channel。

通过mClientChannel.transferTo(outInputChannel);将mClientChannel传递给View应用端的outInputChannel中。即将fd传给客户端。

通过registerInputChannel将mInputChannel注册到InputDispatcher中。
mService.mInputManager.registerInputChannel(mInputChannel, mInputWindowHandle);

这样就分别创建了一对服务端和客户端的InputChannel,并分别把fd传给服务端和客户端。

建立起了InputDispatcher的View之间的联系。

相关标签: Android 系统