Android 源码分析VR返回键无法用Accessibility拦截的问题

本文深入分析了Android系统中AccessibilityService无法拦截VR眼镜返回键(VBACK)的问题。从InputReader.cpp到DecorView.java的事件传递过程揭示,VBACK确实到达了View层。文章探讨了VBACK与BACK键的区别,并着重研究了AccessibilityManagerService、KeyEventDispatcher和AccessibilityInputFilter在处理Key事件中的作用,指出VBACK可能在过滤过程中被忽略,揭示了Google对VR支持的不足。
摘要由CSDN通过智能技术生成

上一篇《Android 源码分析AccessibilityService拦截VR眼镜Key事件以及key事件在View体系的传递》我们分析到了系统对虚拟按键的BACK键和VR眼镜的BACK键处理是不同的。AccessibilityService很容易就拦截到了虚拟按键的BACK键(以下简称BACK),但始终拦截不到VR眼镜的BACK键(以下简称VBACK)。经过从源头InputReader.cpp到DecorView.java事件传递整个流程的追踪,我们发现VBACK确确实实传递到了View。下面我们从VBACK和BACK的区别来继续分析。(ps:VR设备一般被认为是鼠标类型设备)

我们从AccessibilityManagerService(以下简称ACMS)进行分析。


    boolean notifyKeyEvent(KeyEvent event, int policyFlags) {
        synchronized (mLock) {
            List<Service> boundServices = getCurrentUserStateLocked().mBoundServices;
            if (boundServices.isEmpty()) {
                return false;
            }
            return getKeyEventDispatcher().notifyKeyEventLocked(event, policyFlags, boundServices);
        }
    }

ACMS对于 Key事件的处理并不是走AccessibilityEvent这条路,AccessibilityEvent主要是跟View焦点,点击事件,窗口变化有关,这是我们之前分析得到的结论。我们看onKeyEvent这条线。

ACMS对key事件的处理包含两个比较重要的类KeyEventDispatcher和AccessibilityInputFilter,KeyEventDispatcher发送收到KeyEvent的消息,notifyKeyEvent之后调用KeyEventDispatcher的notifyKeyEventLocked。


    public boolean notifyKeyEventLocked(
            KeyEvent event, int policyFlags, List<Service> boundServices) {
        PendingKeyEvent pendingKeyEvent = null;
        KeyEvent localClone = KeyEvent.obtain(event);
        for (int i = 0; i < boundServices.size(); i++) {
            Service service = boundServices.get(i);
            // Key events are handled only by services that declared
            // this capability and requested to filter key events.
            if (!service.mRequestFilterKeyEvents || (service.mServiceInterface == null)) {
                continue;
            }
            int filterKeyEventBit = service.mAccessibilityServiceInfo.getCapabilities()
                    & AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_FILTER_KEY_EVENTS;
            if (filterKeyEventBit == 0) {
                continue;
            }

            try {
                // The event will be cloned in the IPC call, so it doesn't need to be here.
                service.mServiceInterface.onKeyEvent(localClone, localClone.getSequenceNumber());
            } catch (RemoteException re) {
                continue;
            }

            if (pendingKeyEvent == null) {
                pendingKeyEvent 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值