AccessibilityService 如何在其他应用程序之上进行绘制?

and*_*per 6 android accessibilityservice system-alert-window

背景

SAW(系统警报窗口)权限可用于在其他应用程序之上绘制内容。

很久以前我就被告知无障碍服务也可以做到这一点,但我从未找到任何教程、示例、文档,甚至一个应用程序可以做到这一点......直到最近:

https://play.google.com/store/apps/details?id=com.github.ericytsang.screenfilter.app.android

事实上,这个应用程序似乎能够在任何地方绘图,而不是 SAW 许可。它甚至在设置应用程序和系统对话框之上绘制,而 SAW 权限是不允许的。

问题

遗憾的是,由于可访问性是一个非常独特且很少使用的东西,正如我所写的,我无法找到这样的东西如何与其他应用程序之上的绘图一起使用。

我发现了什么

我唯一知道的是这个应用程序以某种方式做到了这一点,这就是它在要求授予它时显示的内容:

在此输入图像描述

但这还不够。我知道对于我使用辅助服务制作的一些旧 POC,它显示了相同的内容,并且检查它,我看不出是什么触发了它。很确定这是用户在任何类型的无障碍服务中都会看到的最小的东西,所以这没有帮助。

问题

  1. AccessibilityService 如何在其他应用程序之上进行绘制?

  2. 它与 SAW 许可的作用相同吗?例如,您可以处理其绘制内容的触摸事件吗?

  3. 如果有的话,使用它有哪些限制?

小智 10

从辅助服务中添加视图时使用TYPE_ACCESSIBILITY_OVERLAY似乎可以解决问题。我做了一个快速测试,即使在设置菜单中也显示了覆盖窗口。覆盖窗口也接收触摸事件。无需清单中的许可,也无需用户交互设置“在其他应用程序上显示”权限,这也可以工作。我使用目标 SDK 29 进行了测试。typeWindowManager.LayoutParamsSYSTEM_ALERT_WINDOW

抱歉,我无法回答您关于适用哪些具体限制的第三个问题。


编辑:通过查看 Google 的旧教程,这里有一个简短的示例:

GlobalActionBarService.java

public class GlobalActionBarService extends AccessibilityService {
    FrameLayout mLayout;

    @Override
    protected void onServiceConnected() {
        // Create an overlay and display the action bar
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        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_NOT_FOCUSABLE;
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.TOP;
        LayoutInflater inflater = LayoutInflater.from(this);
        inflater.inflate(R.layout.action_bar, mLayout);
        wm.addView(mLayout, lp);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
    }

    @Override
    public void onInterrupt() {
    }
}
Run Code Online (Sandbox Code Playgroud)

显现

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    package="com.lb.myapplication">

    <application
        android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication" tools:ignore="AllowBackup">
        <activity
            android:name=".MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".GlobalActionBarService" android:exported="false"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
            <meta-data
                android:name="android.accessibilityservice" android:resource="@xml/global_action_bar_service" />
        </service>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

global_action_bar_service.xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="flagDefault"
    android:canPerformGestures="true" android:canRetrieveWindowContent="true" />
Run Code Online (Sandbox Code Playgroud)

动作栏.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:orientation="horizontal">

    <Button
        android:id="@+id/power" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/power" />

    <Button
        android:id="@+id/volume_up" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/volume" />

    <Button
        android:id="@+id/scroll" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/scroll" />

    <Button
        android:id="@+id/swipe" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/swipe" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)