feat(floaty): wait for floating permission granted when creating a window
This commit is contained in:
@@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.autojs.util.FloatingPermission;
|
||||
import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.enhancedfloaty.FloatyWindow;
|
||||
import com.stardust.enhancedfloaty.util.FloatingWindowPermissionUtil;
|
||||
@@ -16,6 +17,8 @@ import java.lang.ref.WeakReference;
|
||||
|
||||
import ezy.assist.compat.SettingsCompat;
|
||||
|
||||
import static com.stardust.autojs.util.FloatingPermission.manageDrawOverlays;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/9/30.
|
||||
*/
|
||||
@@ -26,11 +29,7 @@ public class FloatyWindowManger {
|
||||
|
||||
public static void addWindow(Context context, FloatyWindow window) {
|
||||
context.startService(new Intent(context, FloatyService.class));
|
||||
if (!SettingsCompat.canDrawOverlays(context)) {
|
||||
Toast.makeText(context, R.string.text_no_floating_window_permission, Toast.LENGTH_SHORT).show();
|
||||
manageDrawOverlays(context);
|
||||
return;
|
||||
}
|
||||
FloatingPermission.ensurePermissionGranted(context);
|
||||
try {
|
||||
FloatyService.addWindow(window);
|
||||
// SecurityException: https://github.com/hyb1996-guest/AutoJsIssueReport/issues/4781
|
||||
@@ -43,14 +42,6 @@ public class FloatyWindowManger {
|
||||
}
|
||||
|
||||
|
||||
public static void manageDrawOverlays(Context context) {
|
||||
try {
|
||||
SettingsCompat.manageDrawOverlays(context);
|
||||
} catch (Exception ex) {
|
||||
FloatingWindowPermissionUtil.goToAppDetailSettings(context, context.getPackageName());
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeWindow(FloatyWindow window) {
|
||||
window.close();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.content.Intent;
|
||||
import android.os.Looper;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.autojs.R;
|
||||
import com.stardust.autojs.core.floaty.FloatyWindow;
|
||||
@@ -12,6 +13,7 @@ import com.stardust.autojs.core.ui.JsLayoutInflater;
|
||||
import com.stardust.autojs.core.ui.JsViewHelper;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.util.FloatingPermission;
|
||||
import com.stardust.concurrent.VolatileDispose;
|
||||
import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.util.UiHandler;
|
||||
@@ -20,6 +22,8 @@ import com.stardust.util.ViewUtil;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import ezy.assist.compat.SettingsCompat;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/12/5.
|
||||
*/
|
||||
@@ -44,6 +48,11 @@ public class Floaty {
|
||||
}
|
||||
|
||||
public JsFloatyWindow window(View view) {
|
||||
try {
|
||||
FloatingPermission.waitForPermissionGranted(view.getContext());
|
||||
} catch (InterruptedException e) {
|
||||
throw new ScriptInterruptedException();
|
||||
}
|
||||
JsFloatyWindow window = new JsFloatyWindow(view);
|
||||
mWindows.add(window);
|
||||
return window;
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.stardust.autojs.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.R;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.enhancedfloaty.util.FloatingWindowPermissionUtil;
|
||||
import com.stardust.lang.ThreadCompat;
|
||||
|
||||
import ezy.assist.compat.SettingsCompat;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/1/30.
|
||||
*/
|
||||
|
||||
public class FloatingPermission {
|
||||
|
||||
|
||||
public static void ensurePermissionGranted(Context context) {
|
||||
if (!SettingsCompat.canDrawOverlays(context)) {
|
||||
Toast.makeText(context, R.string.text_no_floating_window_permission, Toast.LENGTH_SHORT).show();
|
||||
manageDrawOverlays(context);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static void waitForPermissionGranted(Context context) throws InterruptedException {
|
||||
if (SettingsCompat.canDrawOverlays(context)) {
|
||||
return;
|
||||
}
|
||||
Runnable r = () -> {
|
||||
manageDrawOverlays(context);
|
||||
Toast.makeText(context, R.string.text_no_floating_window_permission, Toast.LENGTH_SHORT).show();
|
||||
};
|
||||
if (Looper.myLooper() != Looper.getMainLooper()) {
|
||||
new Handler(Looper.getMainLooper()).post(r);
|
||||
} else {
|
||||
r.run();
|
||||
}
|
||||
while (true) {
|
||||
if (SettingsCompat.canDrawOverlays(context))
|
||||
return;
|
||||
Thread.sleep(200);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void manageDrawOverlays(Context context) {
|
||||
try {
|
||||
SettingsCompat.manageDrawOverlays(context);
|
||||
} catch (Exception ex) {
|
||||
FloatingWindowPermissionUtil.goToAppDetailSettings(context, context.getPackageName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
<resources>
|
||||
<string name="app_name">common</string>
|
||||
<string name="text_no_floating_window_permission">没有悬浮窗权限</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user