全屏TYPE_ACCESSIBILITY_OVERLAY

时间:2018-06-04 09:52:59

标签: android accessibility android-accessibility android-overlay

我是Android上的辅助功能新手。在浏览课程和文档时,我在TYPE_ACCESSIBILITY_OVERLAY课程中遇到了WindowManager

documentation说(只有相关文字)

  

例如,如果存在全屏辅助功能覆盖   可触摸,它下面的窗户将是一个内省   可访问性服务,即使它们被可触摸的覆盖   窗口。

所以我开始实现这一目标,全屏可访问性覆盖并尝试内省它下面的窗口

展开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;
        }
    });
}

现在,问题是,我如何反省或找到此叠加层下面的窗口?即使在onAccessibilityEvent回调中,我也会得到这个叠加窗口。 getWindows()始终有size of 1。它不反驳上面针对TYPE_ACCESSIBILITY_OVERLAY做出的断言吗?

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

android:canRequestTouchExplorationMode="false"

1 个答案:

答案 0 :(得分:2)

您的配置似乎缺少flagRetrieveInteractiveWindows。这些属性和窗口布局参数配置应该可以正常工作,无需您禁用canRequestTouchExplorationMode以获取事件并让getWindows返回您下面的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,因为不应包含与此属性关联的标记,因此无用。