Skip to content

Commit 7835364

Browse files
committed
feat(Window): 支持android12以上非无障碍绘制
feat(LuaFunction): 新增函数强制禁用android12以上无障碍申请
1 parent 72903d9 commit 7835364

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ https://wwt.lanzoul.com/b02vn048j
2121
# 测试脚本
2222

2323
```lua
24+
--强制禁用android12以上的无障碍申请
25+
disDrawAcc()
2426
--创建画笔
2527
paint = newPaint()
2628
--画笔红色

app/src/main/java/Thousand_Dust/MyWindowManager.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
public class MyWindowManager {
1616

1717
private static MyWindowManager mWm;
18+
private static float alpha = 1f;
19+
20+
public static final float MAXIMUM_OBSCURING_OPACITY = 0.8f;
1821

1922
public static void newInstance(Context context) {
2023
if (mWm == null) {
@@ -28,7 +31,7 @@ public static boolean isInstanceEmpty() {
2831

2932
public static MyWindowManager getInstance() {
3033
if (mWm == null) {
31-
throw new o("无障碍功能可能未开启");
34+
throw new o("悬浮窗未初始化,可能是无障碍功能可能未开启");
3235
}
3336
return mWm;
3437
}
@@ -40,6 +43,21 @@ public static void destInstance() {
4043
System.gc();
4144
}
4245

46+
public static void setAlpha(float alpha) {
47+
MyWindowManager.alpha = alpha;
48+
if (!isInstanceEmpty()) {
49+
//悬浮窗已实例化,更新悬浮窗透明度
50+
mWm.lp.alpha = alpha;
51+
mWm.handler.post(() -> {
52+
mWm.wm.updateViewLayout(mWm.viewGroup, mWm.lp);
53+
});
54+
}
55+
}
56+
57+
public static float getAlpha() {
58+
return alpha;
59+
}
60+
4361
private Handler handler = new Handler(Looper.getMainLooper());
4462
private WindowManager wm;
4563
private WindowManager.LayoutParams lp;
@@ -59,7 +77,7 @@ private void initWindow(Context context) {
5977
int type;
6078
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
6179
type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
62-
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
80+
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && alpha > MAXIMUM_OBSCURING_OPACITY) {
6381
type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
6482
} else {
6583
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
@@ -70,9 +88,13 @@ private void initWindow(Context context) {
7088
ViewGroup.LayoutParams.MATCH_PARENT,
7189
ViewGroup.LayoutParams.MATCH_PARENT,
7290
type,
73-
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW,
91+
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
92+
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
93+
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
94+
WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW,
7495
PixelFormat.RGBA_8888
7596
);
97+
lp.alpha = alpha;
7698
lp.gravity = Gravity.TOP | Gravity.LEFT;
7799

78100
wm.addView(viewGroup, lp);

app/src/main/java/Thousand_Dust/canvas/ViewLib.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package Thousand_Dust.canvas;
22

3+
import static Thousand_Dust.MyWindowManager.MAXIMUM_OBSCURING_OPACITY;
4+
35
import android.app.AlertDialog;
46
import android.content.Intent;
57
import android.graphics.Color;
@@ -32,16 +34,12 @@ public class ViewLib extends TwoArgFunction {
3234

3335
private Globals globals;
3436

35-
public ViewLib() {
36-
//android版本小于12直接显示悬浮窗
37-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
38-
MyWindowManager.newInstance(Tools.getContext());
39-
}
40-
}
41-
4237
@Override
4338
public LuaValue a(LuaValue arg1, LuaValue env) {
4439
globals = env.c();
40+
//强制关闭绘图无障碍
41+
env.a("disDrawAcc", new disDrawAcc());
42+
4543
env.a("newView", new newView());
4644
env.a("removeAllView", new removeAllView());
4745

@@ -65,21 +63,26 @@ public LuaValue a(LuaValue arg1, LuaValue env) {
6563
return env;
6664
}
6765

66+
class disDrawAcc extends VarArgFunction {
67+
@Override
68+
public ap a_(ap args) {
69+
MyWindowManager.setAlpha(MAXIMUM_OBSCURING_OPACITY);
70+
return LuaValue.x;
71+
}
72+
}
73+
6874
class newView extends VarArgFunction {
69-
private boolean isShowAlert = false;
7075
private boolean isPrint = false;
7176
@Override
7277
public ap a_(ap args) {
73-
//android版本12及以上需要开启无障碍
74-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
75-
if (MyWindowManager.isInstanceEmpty()) {
78+
if (MyWindowManager.isInstanceEmpty()) {
79+
//android版本12及以上需要开启无障碍
80+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && MyWindowManager.getAlpha() > MAXIMUM_OBSCURING_OPACITY) {
7681
showPermissionAlert();
82+
} else {
83+
showOrdinaryAlert();
7784
}
78-
} else if (!isShowAlert) {
79-
showOrdinaryAlert();
80-
isShowAlert = true;
8185
}
82-
8386
DrawView drawView = new DrawView(Tools.getContext(), globals);
8487
MyWindowManager.getInstance().addView(drawView);
8588
return LuaView.valueOf(drawView);
@@ -108,6 +111,7 @@ public void showOrdinaryAlert() {
108111
ab.setView(devText);
109112
ab.setCancelable(false);
110113
ab.setPositiveButton("同意", (p1, p2) -> {
114+
MyWindowManager.newInstance(Tools.getContext());
111115
result[1] = true;
112116
});
113117
ab.setNegativeButton("拒绝", (p1, p2) -> {
@@ -153,7 +157,7 @@ public void showPermissionAlert() {
153157
final AlertDialog.Builder ab = new AlertDialog.Builder(Tools.getContext());
154158
ab.setTitle("权限请求");
155159
ab.setMessage("脚本即将在屏幕上绘图\n" +
156-
"但因为您的Android设备版本为12或以上,需要开启无障碍\n" +
160+
"但因为您的Android设备版本为12或以上,需要开启无障碍(或调用函数\"disDrawAcc()\"\n" +
157161
"同意跳转权限页面,拒绝结束脚本。");
158162
ab.setView(devText);
159163
ab.setCancelable(false);

0 commit comments

Comments
 (0)