为什么ACTION_OUTSIDE每次都在KitKat 4.4.2上返回0?

时间:2014-02-26 12:36:31

标签: android touch ontouchlistener motion motionevent

我已经实现了一个大小为1的窗口,并希望捕获ACTION_OUTSIDE事件。

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(1,1,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);       

我得到了触发器并且我收到ACTION_OUTSIDE事件,但在阅读event.getRawX()event.getRawY()时,它们每次都返回0。 我用 Android 2.3.6 测试了同样的东西,但它确实有效。而且我找不到任何被弃用的东西。

是Android问题还是有人知道解决方案? THX

1 个答案:

答案 0 :(得分:2)

Tniederm,我回答了一个类似的问题here以供参考,但我会在这里重复一些小编辑:

在搜索源代码后,我找到了问题的根源:

https://github.com/android/platform_frameworks_base/blob/79e0206ef3203a1842949242e58fa8f3c25eb129/services/input/InputDispatcher.cpp#L1417

// Check whether windows listening for outside touches are owned by the same UID. If it is
// set the policy flag that we will not reveal coordinate information to this window.
if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
    sp<InputWindowHandle> foregroundWindowHandle =
            mTempTouchState.getFirstForegroundWindowHandle();
    const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid;
    for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
        const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
        if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
            sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle;
            if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) {
                mTempTouchState.addOrUpdateWindow(inputWindowHandle,
                        InputTarget::FLAG_ZERO_COORDS, BitSet32(0));
            }
        }
    }
}

如果“外部触摸”落在视图中,该视图不与正在侦听外部触摸的视图共享其UID(read about it here),则事件调度程序将其坐标设置为0,0。这绝对是出于安全目的而做的,但我不确定我是否看到了旨在减轻威胁的全部范围。您可以尝试查找旧版本的InputDispatcher,以了解何时引入了此功能 - 我没有看过自己。

如果您想关注它,我会打开一张关于此的错误提示。至少,文档需要包含这些信息......我还想知道这个安全功能是否真的有必要。

Issue 72746: FLAG_WATCH_OUTSIDE_TOUCH doesn't return location for ACTION_OUTSIDE events on 4.2+