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

《Android按键Input KeyEvent》
《Android Input输入系统之一:KeyEvent事件监听及事件分发流程》
《Android Input输入系统之二:KeyEvent注入事件及事件分发流程》
《Android Input输入系统之三:KeyEvent事件分发和上层应用层对事件的接收》
《Android Input输入系统之四:KeyEvent事件中的InputChannel通信》
《Android Input输入系统之五:按键调节音量加减》

经过一系列的分析,key事件的分发,中间有一个InputChannel通信。此章节对《Android Input输入系统之三:KeyEvent事件分发和上层应用层对事件的接收》中的socket通信进行一个详细的分析。

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之间的联系。

再理一理View和InputDispatcher之间的联系。

InputDispatcher,事件发送者,system_server进程;
View,事件接收者,app进程;

整个流程见下图:
从/dev/input/event*中读取到发送给View的整个流程t

图片来源于以下:

参考资料:
https://www.jianshu.com/p/f05d6b05ba17

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaolin2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值