From a2f0e8d63b61876066ded1c743f7831caada46c6 Mon Sep 17 00:00:00 2001
From: hyb1996 <946994919@qq.com>
Date: Wed, 6 Dec 2017 15:02:37 +0800
Subject: [PATCH] optimize module floaty and add two samples
---
.../main/assets/sample/悬浮窗/动态悬浮文字.js | 32 +++++++++++++++++
app/src/main/assets/sample/悬浮窗/悬浮文字.js | 11 ++++++
autojs/src/main/assets/modules/__floaty__.js | 14 +++++---
autojs/src/main/assets/modules/__ui__.js | 2 +-
.../autojs/runtime/ScriptRuntime.java | 36 ++++++++++---------
.../stardust/autojs/runtime/api/Device.java | 17 +++++++++
.../stardust/autojs/runtime/api/Floaty.java | 29 ++++++++-------
7 files changed, 106 insertions(+), 35 deletions(-)
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..57c0a943
--- /dev/null
+++ b/app/src/main/assets/sample/悬浮窗/动态悬浮文字.js
@@ -0,0 +1,32 @@
+var window = floaty.window(
+
+
+
+);
+
+window.text.click(()=>{
+ window.setAdjustEnabled(!window.isAdjustEnabled());
+});
+
+setInterval(()=>{
+ //对控件的操作需要在UI线程中执行
+ ui.run(function(){
+ window.text.setText(dynamicText());
+ });
+}, 1000);
+
+function dynamicText(){
+ var date = new Date();
+ var str = util.format("时间: %d:%d:%d\n", date.getHours(), date.getMinutes(), date.getSeconds());
+ str += util.format("内存使用量: %d%%\n", getMemoryUsage());
+ str += "当前活动: " + currentActivity() + "\n";
+ str += "当前包名: " + currentPackage();
+ return str;
+}
+
+//获取内存使用率
+function getMemoryUsage(){
+ var usage = (100 * device.getAvailMem() / device.getTotalMem());
+ //保留一位小数
+ return Math.round(usage * 10) / 10;
+}
\ No newline at end of file
diff --git a/app/src/main/assets/sample/悬浮窗/悬浮文字.js b/app/src/main/assets/sample/悬浮窗/悬浮文字.js
new file mode 100644
index 00000000..26f6134d
--- /dev/null
+++ b/app/src/main/assets/sample/悬浮窗/悬浮文字.js
@@ -0,0 +1,11 @@
+var window = floaty.window(
+
+
+
+);
+
+window.text.click(()=>{
+ window.setAdjustEnabled(!window.isAdjustEnabled());
+});
+
+setInterval(()=>{}, 1000);
\ No newline at end of file
diff --git a/autojs/src/main/assets/modules/__floaty__.js b/autojs/src/main/assets/modules/__floaty__.js
index 73db3382..87d72152 100644
--- a/autojs/src/main/assets/modules/__floaty__.js
+++ b/autojs/src/main/assets/modules/__floaty__.js
@@ -9,6 +9,8 @@ module.exports = function(__runtime__, scope){
return wrap(__runtime__.floaty.window(layout));
}
+ floaty.__view_cache__ = {};
+
function wrap(window){
var proxyObject = new com.stardust.autojs.rhino.ProxyJavaObject(scope, window, window.getClass());
proxyObject.__proxy__ = {
@@ -18,12 +20,16 @@ module.exports = function(__runtime__, scope){
get: function(name) {
var value = window[name];
if(typeof(value) == 'undefined'){
- value = window.getView(name);
+ value = floaty.__view_cache__[name];
+ if(!value){
+ value = window.getView(name);
+ if(value){
+ value = ui.__decorate__(value);
+ floaty.__view_cache__[name] = value;
+ }
+ }
if(!value){
value = undefined;
- }else{
- value = scope.ui.__decorate__(value);
-
}
}
return value;
diff --git a/autojs/src/main/assets/modules/__ui__.js b/autojs/src/main/assets/modules/__ui__.js
index 522b7864..80475138 100644
--- a/autojs/src/main/assets/modules/__ui__.js
+++ b/autojs/src/main/assets/modules/__ui__.js
@@ -26,7 +26,7 @@ module.exports = function(__runtime__, scope){
}
ui.run = function(action){
- activity.runOnUiThread(action);
+ __runtime__.uiHandler.post(action);
}
ui.nonUi = function(action){
diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java b/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java
index bcc91e5e..06a3a3b5 100644
--- a/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java
+++ b/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java
@@ -158,11 +158,13 @@ public class ScriptRuntime {
@ScriptVariable
public final Floaty floaty;
+ @ScriptVariable
+ public UiHandler uiHandler;
+
private Images images;
private static WeakReference applicationContext;
private Map mProperties = new ConcurrentHashMap<>();
- private UiHandler mUiHandler;
private AbstractShell mRootShell;
private Supplier mShellSupplier;
private ScreenMetrics mScreenMetrics = new ScreenMetrics();
@@ -170,23 +172,23 @@ public class ScriptRuntime {
protected ScriptRuntime(Builder builder) {
app = builder.mAppUtils;
- mUiHandler = builder.mUiHandler;
+ uiHandler = builder.mUiHandler;
console = builder.mConsole;
accessibilityBridge = builder.mAccessibilityBridge;
mShellSupplier = builder.mShellSupplier;
- ui = new UI(mUiHandler.getContext());
+ ui = new UI(uiHandler.getContext());
this.automator = new SimpleActionAutomator(accessibilityBridge, this);
automator.setScreenMetrics(mScreenMetrics);
this.info = accessibilityBridge.getInfoProvider();
- Context context = mUiHandler.getContext();
+ Context context = uiHandler.getContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
images = new Images(context, this, builder.mScreenCaptureRequester);
}
engines = new Engines(builder.mEngineService);
- dialogs = new Dialogs(app, mUiHandler, bridges);
+ dialogs = new Dialogs(app, uiHandler, bridges);
threads = new Threads(this);
- device = new Device(mUiHandler.getContext());
- floaty = new Floaty(mUiHandler, ui);
+ device = new Device(uiHandler.getContext());
+ floaty = new Floaty(uiHandler, ui);
}
public void init() {
@@ -194,7 +196,7 @@ public class ScriptRuntime {
throw new IllegalStateException("already initialized");
timers = new Timers(bridges);
loopers = new Loopers(this);
- events = new Events(mUiHandler.getContext(), accessibilityBridge, bridges, loopers);
+ events = new Events(uiHandler.getContext(), accessibilityBridge, bridges, loopers);
}
public static void setApplicationContext(Context context) {
@@ -209,7 +211,7 @@ public class ScriptRuntime {
}
public UiHandler getUiHandler() {
- return mUiHandler;
+ return uiHandler;
}
public AccessibilityBridge getAccessibilityBridge() {
@@ -217,7 +219,7 @@ public class ScriptRuntime {
}
public void toast(final String text) {
- mUiHandler.toast(text);
+ uiHandler.toast(text);
}
public void sleep(long millis) {
@@ -230,12 +232,12 @@ public class ScriptRuntime {
public void setClip(final String text) {
if (Looper.myLooper() == Looper.getMainLooper()) {
- ClipboardUtil.setClip(mUiHandler.getContext(), text);
+ ClipboardUtil.setClip(uiHandler.getContext(), text);
return;
}
VolatileDispose