mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-21 21:01:43 +08:00
fix: Permission for drawing overlays on MIUI 10
This commit is contained in:
parent
de77cd7509
commit
220473c109
@ -54,7 +54,7 @@ public class FloatyWindowManger {
|
||||
}
|
||||
|
||||
public static boolean showCircularMenu() {
|
||||
if (!SettingsCompat.canDrawOverlays(GlobalAppContext.get())) {
|
||||
if (!FloatingPermission.canDrawOverlays(GlobalAppContext.get())) {
|
||||
Toast.makeText(GlobalAppContext.get(), R.string.text_no_floating_window_permission, Toast.LENGTH_SHORT).show();
|
||||
manageDrawOverlays(GlobalAppContext.get());
|
||||
return false;
|
||||
|
||||
@ -10,6 +10,7 @@ import com.stardust.autojs.annotation.ScriptInterface;
|
||||
import com.stardust.autojs.runtime.api.AbstractConsole;
|
||||
import com.stardust.autojs.runtime.api.Console;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.util.FloatingPermission;
|
||||
import com.stardust.concurrent.ConcurrentArrayList;
|
||||
import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.enhancedfloaty.ResizableExpandableFloatyWindow;
|
||||
@ -151,8 +152,8 @@ public class StardustConsole extends AbstractConsole {
|
||||
if (mShown) {
|
||||
return;
|
||||
}
|
||||
if (!SettingsCompat.canDrawOverlays(mUiHandler.getContext())) {
|
||||
SettingsCompat.manageDrawOverlays(mUiHandler.getContext());
|
||||
if (!FloatingPermission.canDrawOverlays(mUiHandler.getContext())) {
|
||||
FloatingPermission.manageDrawOverlays(mUiHandler.getContext());
|
||||
mUiHandler.toast(R.string.text_no_floating_window_permission);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -1,8 +1,14 @@
|
||||
package com.stardust.autojs.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.R;
|
||||
@ -10,6 +16,11 @@ import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.enhancedfloaty.util.FloatingWindowPermissionUtil;
|
||||
import com.stardust.lang.ThreadCompat;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Set;
|
||||
|
||||
import ezy.assist.compat.RomUtil;
|
||||
import ezy.assist.compat.SettingsCompat;
|
||||
|
||||
/**
|
||||
@ -19,8 +30,20 @@ import ezy.assist.compat.SettingsCompat;
|
||||
public class FloatingPermission {
|
||||
|
||||
|
||||
private static final int OP_SYSTEM_ALERT_WINDOW = 24;
|
||||
private static Method sCheckOp;
|
||||
|
||||
static {
|
||||
try {
|
||||
sCheckOp = SettingsCompat.class.getDeclaredMethod("checkOp", Context.class, int.class);
|
||||
sCheckOp.setAccessible(true);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void ensurePermissionGranted(Context context) {
|
||||
if (!SettingsCompat.canDrawOverlays(context)) {
|
||||
if (!canDrawOverlays(context)) {
|
||||
Toast.makeText(context, R.string.text_no_floating_window_permission, Toast.LENGTH_SHORT).show();
|
||||
manageDrawOverlays(context);
|
||||
return;
|
||||
@ -28,7 +51,7 @@ public class FloatingPermission {
|
||||
}
|
||||
|
||||
public static void waitForPermissionGranted(Context context) throws InterruptedException {
|
||||
if (SettingsCompat.canDrawOverlays(context)) {
|
||||
if (canDrawOverlays(context)) {
|
||||
return;
|
||||
}
|
||||
Runnable r = () -> {
|
||||
@ -41,7 +64,7 @@ public class FloatingPermission {
|
||||
r.run();
|
||||
}
|
||||
while (true) {
|
||||
if (SettingsCompat.canDrawOverlays(context))
|
||||
if (canDrawOverlays(context))
|
||||
return;
|
||||
Thread.sleep(200);
|
||||
}
|
||||
@ -51,11 +74,42 @@ public class FloatingPermission {
|
||||
|
||||
public static void manageDrawOverlays(Context context) {
|
||||
try {
|
||||
SettingsCompat.manageDrawOverlays(context);
|
||||
if (RomUtil.isMiui() && TextUtils.equals("V10", RomUtil.getVersion())
|
||||
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
manageDrawOverlaysForAndroidM(context);
|
||||
} else {
|
||||
SettingsCompat.manageDrawOverlays(context);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
FloatingWindowPermissionUtil.goToAppDetailSettings(context, context.getPackageName());
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
manageDrawOverlaysForAndroidM(context);
|
||||
} else {
|
||||
FloatingWindowPermissionUtil.goToAppDetailSettings(context, context.getPackageName());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
public static void manageDrawOverlaysForAndroidM(Context context) {
|
||||
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
|
||||
intent.setData(Uri.parse("package:" + context.getPackageName()));
|
||||
context.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
||||
}
|
||||
|
||||
public static boolean canDrawOverlays(Context context) {
|
||||
return SettingsCompat.canDrawOverlays(context);
|
||||
}
|
||||
|
||||
private static boolean checkOp(Context context, int op) {
|
||||
if (sCheckOp == null) {
|
||||
return SettingsCompat.canDrawOverlays(context);
|
||||
}
|
||||
try {
|
||||
return (boolean) sCheckOp.invoke(null, context, op);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user