上一篇《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