optimize module floaty and add two samples

This commit is contained in:
hyb1996
2017-12-06 15:02:37 +08:00
parent c871b474f5
commit a2f0e8d63b
7 changed files with 106 additions and 35 deletions

View File

@@ -0,0 +1,32 @@
var window = floaty.window(
<frame gravity="center">
<text id="text" textSize="16sp" textColor="#f44336"/>
</frame>
);
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;
}

View File

@@ -0,0 +1,11 @@
var window = floaty.window(
<frame gravity="center">
<text id="text" text="点击可调整位置" textSize="16sp"/>
</frame>
);
window.text.click(()=>{
window.setAdjustEnabled(!window.isAdjustEnabled());
});
setInterval(()=>{}, 1000);

View File

@@ -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;

View File

@@ -26,7 +26,7 @@ module.exports = function(__runtime__, scope){
}
ui.run = function(action){
activity.runOnUiThread(action);
__runtime__.uiHandler.post(action);
}
ui.nonUi = function(action){

View File

@@ -158,11 +158,13 @@ public class ScriptRuntime {
@ScriptVariable
public final Floaty floaty;
@ScriptVariable
public UiHandler uiHandler;
private Images images;
private static WeakReference<Context> applicationContext;
private Map<String, Object> mProperties = new ConcurrentHashMap<>();
private UiHandler mUiHandler;
private AbstractShell mRootShell;
private Supplier<AbstractShell> 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<Object> 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<String> 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));
}
}

View File

@@ -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) {

View File

@@ -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<View> 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);
});
}
}