From e8e73aac14a77c3460667809aebeb70edc7c6bca Mon Sep 17 00:00:00 2001
From: hyb1996 <946994919@qq.com>
Date: Fri, 29 Dec 2017 21:45:16 +0800
Subject: [PATCH] add: two samples for floaty
---
.../sample/悬浮窗/悬浮窗运行脚本按钮简单版.js | 31 ++++++++++
.../assets/sample/悬浮窗/悬浮运行脚本按钮.js | 62 +++++++++++++++++++
.../autojs/core/console/ConsoleFloaty.java | 1 +
3 files changed, 94 insertions(+)
create mode 100644 app/src/main/assets/sample/悬浮窗/悬浮窗运行脚本按钮简单版.js
create mode 100644 app/src/main/assets/sample/悬浮窗/悬浮运行脚本按钮.js
diff --git a/app/src/main/assets/sample/悬浮窗/悬浮窗运行脚本按钮简单版.js b/app/src/main/assets/sample/悬浮窗/悬浮窗运行脚本按钮简单版.js
new file mode 100644
index 00000000..a31d9448
--- /dev/null
+++ b/app/src/main/assets/sample/悬浮窗/悬浮窗运行脚本按钮简单版.js
@@ -0,0 +1,31 @@
+var path = "/sdcard/脚本/test.js";
+if(!files.exists(path)){
+ toast("脚本文件不存在: " + path);
+ exit();
+}
+var window = floaty.window(
+
+
+
+);
+
+var execution = null;
+
+window.action.click(()=>{
+ if(window.action.getText() == '开始运行'){
+ execution = engines.execScriptFile(path);
+ window.action.setText('停止运行');
+ }else{
+ if(execution){
+ execution.getEngine().forceStop();
+ }
+ window.action.setText('开始运行');
+ }
+});
+
+window.action.longClick(()=>{
+ window.setAdjustEnabled(!window.isAdjustEnabled());
+ return true;
+});
+
+setInterval(()=>{}, 1000);
diff --git a/app/src/main/assets/sample/悬浮窗/悬浮运行脚本按钮.js b/app/src/main/assets/sample/悬浮窗/悬浮运行脚本按钮.js
new file mode 100644
index 00000000..8fd9d2d0
--- /dev/null
+++ b/app/src/main/assets/sample/悬浮窗/悬浮运行脚本按钮.js
@@ -0,0 +1,62 @@
+var path = "/sdcard/脚本/test.js";
+if(!files.exists(path)){
+ toast("脚本文件不存在: " + path);
+ exit();
+}
+var window = floaty.window(
+
+
+
+);
+
+var execution = null;
+
+//记录按键被按下时的触摸坐标
+var x = 0, y = 0;
+//记录按键被按下时的悬浮窗位置
+var windowX, windowY;
+//记录按键被按下的时间以便判断长按等动作
+var downTime;
+
+window.action.setOnTouchListener(function(view, event){
+ switch(event.getAction()){
+ case event.ACTION_DOWN:
+ x = event.getRawX();
+ y = event.getRawY();
+ windowX = window.getX();
+ windowY = window.getY();
+ downTime = new Date().getTime();
+ return true;
+ case event.ACTION_MOVE:
+ //移动手指时调整悬浮窗位置
+ window.setPosition(windowX + (event.getRawX() - x),
+ windowY + (event.getRawY() - y));
+ //如果按下的时间超过1.5秒判断为长按,退出脚本
+ if(new Date().getTime() - downTime > 1500){
+ exit();
+ }
+ return true;
+ case event.ACTION_UP:
+ //手指弹起时如果偏移很小则判断为点击
+ if(Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5){
+ onClick();
+ }
+ return true;
+ }
+ return true;
+});
+
+function onClick(){
+ if(window.action.getText() == '开始运行'){
+ execution = engines.execScriptFile(path);
+ window.action.setText('停止运行');
+ }else{
+ if(execution){
+ execution.getEngine().forceStop();
+ }
+ window.action.setText('开始运行');
+ }
+}
+
+setInterval(()=>{}, 1000);
+
diff --git a/autojs/src/main/java/com/stardust/autojs/core/console/ConsoleFloaty.java b/autojs/src/main/java/com/stardust/autojs/core/console/ConsoleFloaty.java
index b5507162..a6349951 100644
--- a/autojs/src/main/java/com/stardust/autojs/core/console/ConsoleFloaty.java
+++ b/autojs/src/main/java/com/stardust/autojs/core/console/ConsoleFloaty.java
@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.ContextWrapper;
import android.support.annotation.Nullable;
import android.view.ContextThemeWrapper;
+import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;