add samples, fix require()
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
package com.stardust.scriptdroid.external.floating_window;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.external.floating_window.menu.HoverMenuService;
|
||||
import com.stardust.util.IntentUtil;
|
||||
|
||||
@@ -15,9 +21,13 @@ import com.stardust.util.IntentUtil;
|
||||
|
||||
public class FloatingWindowManger {
|
||||
|
||||
private static final String KEY_FLOATING_WINDOW_PERMISSION = "May we go back..I...miss..you..Eating..17.5.9";
|
||||
private static final String TAG = "FloatingWindowManger";
|
||||
|
||||
public static void showHoverMenu() {
|
||||
if (!HoverMenuService.isServiceRunning()) {
|
||||
if (!hasFloatingWindowPermission()) {
|
||||
if (!hasFloatingWindowPermission(App.getApp())) {
|
||||
Toast.makeText(App.getApp(), R.string.text_no_floating_window_permission, Toast.LENGTH_SHORT).show();
|
||||
goToFloatingWindowPermissionSetting();
|
||||
} else {
|
||||
HoverMenuService.startService(App.getApp());
|
||||
@@ -26,33 +36,54 @@ public class FloatingWindowManger {
|
||||
}
|
||||
|
||||
public static void goToFloatingWindowPermissionSetting() {
|
||||
String packageName = App.getApp().getPackageName();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
try {
|
||||
App.getApp().startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
|
||||
Uri.parse("package:" + packageName))
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
||||
} catch (Exception e) {
|
||||
IntentUtil.goToAppDetailSettings(App.getApp());
|
||||
}
|
||||
} else {
|
||||
IntentUtil.goToAppDetailSettings(App.getApp());
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
private static void goToOverlayPermissionSettings(Context context, String packageName) {
|
||||
try {
|
||||
App.getApp().startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
|
||||
Uri.parse("package:" + context))
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
||||
} catch (Exception e) {
|
||||
IntentUtil.goToAppDetailSettings(App.getApp());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasFloatingWindowPermission() {
|
||||
public static void checkPermission() {
|
||||
final OverlayPermissionChecker checker = new OverlayPermissionChecker(App.getApp());
|
||||
checker.setCallback(new OverlayPermissionChecker.Callback() {
|
||||
@Override
|
||||
public void onCheckResult(boolean granted) {
|
||||
checker.setCallback(null);
|
||||
Log.d(TAG, "onCheckResult:" + granted);
|
||||
setHasFloatingWindowPermission(App.getApp(), granted);
|
||||
}
|
||||
});
|
||||
checker.check(1500);
|
||||
}
|
||||
|
||||
public static boolean hasFloatingWindowPermission(Context context) {
|
||||
return hasOverlayPermission();
|
||||
}
|
||||
|
||||
private static void setHasFloatingWindowPermission(Context context, boolean has) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(KEY_FLOATING_WINDOW_PERMISSION, has).apply();
|
||||
}
|
||||
|
||||
private static boolean hasOverlayPermission() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
return Settings.canDrawOverlays(App.getApp());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isFloatingWindowShowing() {
|
||||
public static boolean isHoverMenuShowing() {
|
||||
return HoverMenuService.isServiceRunning();
|
||||
}
|
||||
|
||||
|
||||
public static void hideFloatingWindow() {
|
||||
public static void hideHoverMenu() {
|
||||
if (HoverMenuService.isServiceRunning())
|
||||
App.getApp().stopService(new Intent(App.getApp(), HoverMenuService.class));
|
||||
}
|
||||
|
||||
@@ -1,9 +1,23 @@
|
||||
package com.stardust.scriptdroid.external.floating_window;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.IntDef;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.enhancedfloaty.FloatyWindow;
|
||||
import com.stardust.scriptdroid.App;
|
||||
import com.stardust.scriptdroid.autojs.api.VolatileBox;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/9.
|
||||
@@ -11,41 +25,99 @@ import com.stardust.enhancedfloaty.FloatyWindow;
|
||||
|
||||
public class OverlayPermissionChecker {
|
||||
|
||||
|
||||
public static void addFloaty() {
|
||||
FloatyService.addWindow(new FloatyWindow() {
|
||||
@Override
|
||||
public void onCreate(FloatyService floatyService, WindowManager windowManager) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDestroy(FloatyService floatyService) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
});
|
||||
public interface Callback {
|
||||
void onCheckResult(boolean granted);
|
||||
}
|
||||
|
||||
private static class OnePixelWindow implements FloatyWindow {
|
||||
private OnePixelWindow mOnePixelWindow = new OnePixelWindow();
|
||||
private Callback mCallback;
|
||||
private Context mContext;
|
||||
private Handler mHandler;
|
||||
private Boolean mCheckResult = null;
|
||||
|
||||
public OverlayPermissionChecker(Context context) {
|
||||
mContext = context;
|
||||
mHandler = new Handler(context.getMainLooper());
|
||||
}
|
||||
|
||||
public void check() {
|
||||
mCheckResult = null;
|
||||
try {
|
||||
FloatyService.addWindow(mOnePixelWindow);
|
||||
mContext.startService(new Intent(mContext, FloatyService.class));
|
||||
} catch (WindowManager.BadTokenException e) {
|
||||
onCheckResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void check(int timeOut) {
|
||||
check();
|
||||
mHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (mCheckResult == null) {
|
||||
onCheckResult(false);
|
||||
}
|
||||
}
|
||||
}, timeOut);
|
||||
}
|
||||
|
||||
public void setCallback(Callback callback) {
|
||||
mCallback = callback;
|
||||
}
|
||||
|
||||
private void onCheckResult(boolean b) {
|
||||
mCheckResult = b;
|
||||
if (mCallback != null) {
|
||||
mCallback.onCheckResult(b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class OnePixelWindow implements FloatyWindow {
|
||||
|
||||
private View mOnePixelView;
|
||||
private WindowManager mWindowManager;
|
||||
|
||||
@Override
|
||||
public void onCreate(FloatyService floatyService, WindowManager windowManager) {
|
||||
|
||||
mWindowManager = windowManager;
|
||||
mOnePixelView = new View(floatyService) {
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
notifyWindowVisible();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onWindowVisibilityChanged(int visibility) {
|
||||
super.onWindowVisibilityChanged(visibility);
|
||||
}
|
||||
};
|
||||
mOnePixelView.setWillNotDraw(false);
|
||||
mOnePixelView.setBackgroundColor(Color.RED);
|
||||
WindowManager.LayoutParams params = new WindowManager.LayoutParams(10, 10,
|
||||
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
|
||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
|
||||
PixelFormat.TRANSLUCENT);
|
||||
params.gravity = Gravity.TOP | Gravity.START;
|
||||
windowManager.addView(mOnePixelView, params);
|
||||
}
|
||||
|
||||
private void notifyWindowVisible() {
|
||||
onCheckResult(true);
|
||||
//close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDestroy(FloatyService floatyService) {
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
mWindowManager.removeView(mOnePixelView);
|
||||
FloatyService.removeWindow(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@ import com.stardust.scriptdroid.external.CommonUtils;
|
||||
import com.stardust.scriptdroid.R;
|
||||
import com.stardust.scriptdroid.script.Scripts;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/2/22.
|
||||
*/
|
||||
@@ -35,6 +37,7 @@ public class RunIntentActivity extends Activity {
|
||||
|
||||
private void handleIntent(Intent intent) {
|
||||
String path = getPath(intent);
|
||||
String directoryPath = null;
|
||||
String script = intent.getStringExtra(CommonUtils.EXTRA_KEY_PRE_EXECUTE_SCRIPT);
|
||||
ScriptSource source = null;
|
||||
if (path == null && script != null) {
|
||||
@@ -42,9 +45,13 @@ public class RunIntentActivity extends Activity {
|
||||
} else if (path != null && new PathChecker(this).checkAndToastError(path)) {
|
||||
ScriptSource fileScriptSource = new FileScriptSource(path);
|
||||
source = new SequenceScriptSource(fileScriptSource.getName(), new StringScriptSource(script), fileScriptSource);
|
||||
directoryPath = new File(path).getParent();
|
||||
}
|
||||
if (source != null) {
|
||||
Scripts.run(source);
|
||||
if (directoryPath == null)
|
||||
Scripts.run(source);
|
||||
else
|
||||
Scripts.run(source, directoryPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user