全屏 TYPE_ACCESSIBILITY_OVERLAY

Full screen TYPE_ACCESSIBILITY_OVERLAY

我不熟悉 Android 上的辅助功能。在浏览 classes 和文档时,我在 WindowManager class.

中遇到了 TYPE_ACCESSIBILITY_OVERLAY

documentation 说(只有相关文字)

For example, if there is a full screen accessibility overlay that is touchable, the windows below it will be introspectable by an accessibility service even though they are covered by a touchable window.

所以我开始着手实现这一点,一个全屏辅助功能叠加层,并尝试反省其下方的 windows

扩展AccessibilityService并在调用onServiceConnected时添加我的全屏覆盖(添加覆盖的灵感来自here

@Override
protected void onServiceConnected() {
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    FrameLayout mLayout = new FrameLayout(this);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
    lp.format = PixelFormat.TRANSLUCENT;
    lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    lp.gravity = Gravity.TOP;
    wm.addView(mLayout, lp);

    mLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Here I'm getting the touch events on the overlay I added
            return false;
        }
    });
}

现在,问题是,我如何反省或找到此叠加层下方的 windows?即使在 onAccessibilityEvent 回调中,我也只得到这个覆盖层 window。 getWindows() 总是有一个 size of 1。这不是反驳了上面对TYPE_ACCESSIBILITY_OVERLAY的断言吗?

相关信息:为了接收叠加层上的触摸事件,我在服务设置中禁用了 touchExplorationMode

android:canRequestTouchExplorationMode="false"

您的配置似乎缺少 flagRetrieveInteractiveWindows。这些属性和 window 布局参数配置应该可以工作,不需要您禁用 canRequestTouchExplorationMode 来获取事件并让 getWindows return AccessibilityWindowInfo 实例在您的下面:

    <?xml version="1.0" encoding="utf-8"?>
    <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
        android:packageNames="test.demo.com.tests"
        android:accessibilityEventTypes="typeAllMask"
        android:accessibilityFlags="flagRetrieveInteractiveWindows|flagReportViewIds|flagIncludeNotImportantViews"
        android:accessibilityFeedbackType="feedbackAllMask"
        android:notificationTimeout="100"
        android:canRetrieveWindowContent="true"
        />

并已连接服务:

    @Override
    protected void onServiceConnected() {
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        FrameLayout layout = new FrameLayout(this);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
                WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS| 
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP;

        windowManager.addView(layout, params);
        layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //You can either get the information here or on onAccessibilityEvent                 
                return false;
            }
        });
    }

编辑:

添加了 FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 以实现全屏并删除了 canRequestTouchExplorationMode,因为不应包含与此 属性 关联的标志,因此没有用。