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 dispose = new VolatileDispose<>(); - mUiHandler.post(() -> { - ClipboardUtil.setClip(mUiHandler.getContext(), text); + uiHandler.post(() -> { + ClipboardUtil.setClip(uiHandler.getContext(), text); dispose.setAndNotify(text); }); dispose.blockedGet(); @@ -243,13 +245,13 @@ public class ScriptRuntime { public String getClip() { if (Looper.myLooper() == Looper.getMainLooper()) { - return ClipboardUtil.getClipOrEmpty(mUiHandler.getContext()).toString(); + return ClipboardUtil.getClipOrEmpty(uiHandler.getContext()).toString(); } final VolatileDispose clip = new VolatileDispose<>(); - mUiHandler.post(new Runnable() { + uiHandler.post(new Runnable() { @Override public void run() { - clip.setAndNotify(ClipboardUtil.getClipOrEmpty(mUiHandler.getContext()).toString()); + clip.setAndNotify(ClipboardUtil.getClipOrEmpty(uiHandler.getContext()).toString()); } }); return clip.blockedGetOrThrow(ScriptInterruptedException.class); @@ -282,7 +284,7 @@ public class ScriptRuntime { public void requiresApi(int i) { if (Build.VERSION.SDK_INT < i) { - throw new ScriptException(mUiHandler.getContext().getString(R.string.text_requires_sdk_version_to_run_the_script) + SdkVersionUtil.sdkIntToString(i)); + throw new ScriptException(uiHandler.getContext().getString(R.string.text_requires_sdk_version_to_run_the_script) + SdkVersionUtil.sdkIntToString(i)); } } diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/Device.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/Device.java index d1b7f9a3..924725f9 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/Device.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/Device.java @@ -2,6 +2,7 @@ package com.stardust.autojs.runtime.api; import android.Manifest; import android.annotation.SuppressLint; +import android.app.ActivityManager; import android.app.AlarmManager; import android.content.Context; import android.content.Intent; @@ -178,6 +179,22 @@ public class Device { return Math.round(battery * 10) / 10; } + public long getTotalMem(){ + ActivityManager activityManager = getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); + activityManager.getMemoryInfo(info); + return info.totalMem; + } + + public long getAvailMem(){ + ActivityManager activityManager = getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); + activityManager.getMemoryInfo(info); + return info.availMem; + } + + + public boolean isCharging() { Intent intent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); if (intent == null) { diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/Floaty.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/Floaty.java index 91f3030e..78057b83 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/Floaty.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/Floaty.java @@ -11,8 +11,11 @@ import com.stardust.autojs.core.floaty.FloatyWindow; import com.stardust.autojs.core.ui.JsLayoutInflater; import com.stardust.autojs.core.ui.JsViewHelper; import com.stardust.autojs.rhino.ProxyObject; +import com.stardust.autojs.runtime.exception.ScriptInterruptedException; +import com.stardust.concurrent.VolatileDispose; import com.stardust.enhancedfloaty.FloatyService; import com.stardust.util.UiHandler; +import com.stardust.util.ViewUtil; import org.mozilla.javascript.NativeJavaObject; import org.mozilla.javascript.Scriptable; @@ -53,7 +56,13 @@ public class Floaty { } private View inflate(String xml) { - return mJsLayoutInflater.inflate(mContext, xml); + if (Looper.getMainLooper() == Looper.myLooper()) { + return mJsLayoutInflater.inflate(mContext, xml); + } else { + VolatileDispose dispose = new VolatileDispose<>(); + mUiHandler.post(() -> dispose.setAndNotify(mJsLayoutInflater.inflate(mContext, xml))); + return dispose.blockedGetOrThrow(ScriptInterruptedException.class); + } } public void closeAll() { @@ -87,18 +96,9 @@ public class Floaty { } public View getView(String id) { - View v = mViewCache.get(id); - if (v != null) { - return v; - } - v = JsViewHelper.findViewByStringId(mView, id); - if (v != null) { - mViewCache.put(id, v); - return v; - } else { - return null; - } + return JsViewHelper.findViewByStringId(mView, id); } + public int getX() { return mWindow.getWindowBridge().getX(); } @@ -119,7 +119,10 @@ public class Floaty { if (Looper.myLooper() == Looper.getMainLooper()) { mWindow.getWindowBridge().updateMeasure(w, h); } else { - mUiHandler.post(() -> mWindow.getWindowBridge().updateMeasure(w, h)); + mUiHandler.post(() -> { + ViewUtil.setViewMeasure(mView, w, h); + mWindow.getWindowBridge().updateMeasure(w, h); + }); } }