api(engines): engines.all(), ScriptEngine.emit()
api(events): events.broadcast
This commit is contained in:
@@ -33,7 +33,7 @@ runtime.init();
|
||||
}
|
||||
return func.apply(target, arr);
|
||||
},
|
||||
toArray: function(list){
|
||||
asArray: function(list){
|
||||
var arr = [];
|
||||
for(var i = 0; i < list.size(); i++){
|
||||
arr.push(list.get(i));
|
||||
@@ -50,6 +50,14 @@ runtime.init();
|
||||
}
|
||||
return arr;
|
||||
},
|
||||
toArray: function(iterable){
|
||||
var iterator = iterable.iterator();
|
||||
var arr = [];
|
||||
while(iterator.hasNext()){
|
||||
arr.push(iterator.next());
|
||||
}
|
||||
return arr;
|
||||
},
|
||||
toString: function(o){
|
||||
return String(o);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ module.exports = function(__runtime__, scope){
|
||||
return rtEngines.myEngine();
|
||||
}
|
||||
|
||||
engines.all = function(){
|
||||
return rtEngines.all();
|
||||
}
|
||||
|
||||
engines.stopAll = rtEngines.stopAll.bind(rtEngines);
|
||||
engines.stopAllAndToast = rtEngines.stopAllAndToast.bind(rtEngines);
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import android.view.accessibility.AccessibilityNodeInfo;
|
||||
|
||||
import com.stardust.autojs.BuildConfig;
|
||||
import com.stardust.autojs.annotation.ScriptInterface;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.automator.ActionArgument;
|
||||
@@ -73,7 +74,8 @@ public class UiSelector extends UiGlobalSelector {
|
||||
public UiObjectCollection find() {
|
||||
ensureAccessibilityServiceEnabled();
|
||||
AccessibilityNodeInfo root = mAccessibilityBridge.getRootInActiveWindow();
|
||||
Log.d(TAG, "find: root = " + root);
|
||||
if (BuildConfig.DEBUG)
|
||||
Log.d(TAG, "find: root = " + root);
|
||||
if (root == null) {
|
||||
return UiObjectCollection.EMPTY;
|
||||
}
|
||||
@@ -160,7 +162,6 @@ public class UiSelector extends UiGlobalSelector {
|
||||
}
|
||||
uiObjectCollection = find();
|
||||
}
|
||||
|
||||
return uiObjectCollection.get(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.stardust.autojs.core.boardcast;
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/4/1.
|
||||
*/
|
||||
|
||||
public class Broadcast {
|
||||
|
||||
private static CopyOnWriteArrayList<BroadcastEmitter> sEventEmitters = new CopyOnWriteArrayList<>();
|
||||
|
||||
public static void registerListener(BroadcastEmitter eventEmitter) {
|
||||
sEventEmitters.add(eventEmitter);
|
||||
}
|
||||
|
||||
public static boolean unregisterListener(BroadcastEmitter eventEmitter) {
|
||||
return sEventEmitters.remove(eventEmitter);
|
||||
}
|
||||
|
||||
public static void send(String eventName, Object[] args) {
|
||||
for (BroadcastEmitter emitter : sEventEmitters) {
|
||||
emitter.onBroadcast(eventName, args);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.stardust.autojs.core.boardcast;
|
||||
|
||||
import com.stardust.autojs.core.eventloop.EventEmitter;
|
||||
import com.stardust.autojs.core.looper.Timer;
|
||||
import com.stardust.autojs.runtime.ScriptBridges;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/4/1.
|
||||
*/
|
||||
|
||||
public class BroadcastEmitter extends EventEmitter {
|
||||
|
||||
public BroadcastEmitter(ScriptBridges bridges, Timer timer) {
|
||||
super(bridges, timer);
|
||||
Broadcast.registerListener(this);
|
||||
}
|
||||
|
||||
public boolean onBroadcast(String eventName, Object... args) {
|
||||
return super.emit(eventName, args);
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
Broadcast.unregisterListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean emit(String eventName, Object... args) {
|
||||
Broadcast.send(eventName, args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -117,7 +117,6 @@ public class EventEmitter {
|
||||
getListeners(eventName).add(listener, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Listeners getListeners(String eventName) {
|
||||
Listeners listeners = mListenersMap.get(eventName);
|
||||
|
||||
@@ -173,6 +173,8 @@ public class TemplateMatching {
|
||||
public static Mat matchTemplate(Mat img, Mat temp, int match_method) {
|
||||
int result_cols = img.cols() - temp.cols() + 1;
|
||||
int result_rows = img.rows() - temp.rows() + 1;
|
||||
Log.d(LOG_TAG, String.format("matchTemplate: rows = %d, cols = %d", result_rows, result_cols));
|
||||
Log.d(LOG_TAG, String.format("matchTemplate: img = %s, temp = %s", img.toString(), temp.toString()));
|
||||
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
|
||||
Imgproc.matchTemplate(img, temp, result, match_method);
|
||||
return result;
|
||||
|
||||
@@ -69,7 +69,7 @@ public class Timer {
|
||||
return id;
|
||||
}
|
||||
|
||||
private void postDelayed(Runnable r, long interval) {
|
||||
public void postDelayed(Runnable r, long interval) {
|
||||
long uptime = SystemClock.uptimeMillis() + interval;
|
||||
mHandler.postAtTime(r, uptime);
|
||||
mMaxCallbackUptimeMillis = Math.max(mMaxCallbackUptimeMillis, uptime);
|
||||
|
||||
@@ -36,5 +36,15 @@ public abstract class JavaScriptEngine extends ScriptEngine.AbstractScriptEngine
|
||||
put("runtime", runtime);
|
||||
}
|
||||
|
||||
public void emit(String eventName, Object... args) {
|
||||
mRuntime.timers.getMainTimer().postDelayed(() -> mRuntime.events.emit(eventName, args), 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ScriptEngine@" + Integer.toHexString(hashCode()) + "{" +
|
||||
"source='" + getTag(TAG_SOURCE) + "'," +
|
||||
"cwd='" + cwd() + "'" +
|
||||
"}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
return getRuntime().bridges.toString(obj.toString());
|
||||
}
|
||||
if (staticType == UiObjectCollection.class) {
|
||||
return getRuntime().bridges.toArray(obj);
|
||||
return getRuntime().bridges.asArray(obj);
|
||||
|
||||
}
|
||||
return super.wrap(cx, scope, obj, staticType);
|
||||
|
||||
@@ -17,7 +17,9 @@ public class ScriptBridges {
|
||||
|
||||
Object call(Object func, Object target, Object arg);
|
||||
|
||||
Object toArray(Object o);
|
||||
Object toArray(Iterable o);
|
||||
|
||||
Object asArray(Object o);
|
||||
|
||||
Object toString(Object obj);
|
||||
|
||||
@@ -40,11 +42,16 @@ public class ScriptBridges {
|
||||
}
|
||||
|
||||
|
||||
public Object toArray(Object c) {
|
||||
public Object toArray(Iterable c) {
|
||||
checkBridges();
|
||||
return mBridges.toArray(c);
|
||||
}
|
||||
|
||||
public Object asArray(Object o) {
|
||||
checkBridges();
|
||||
return mBridges.asArray(o);
|
||||
}
|
||||
|
||||
|
||||
public Object toString(Object obj) {
|
||||
checkBridges();
|
||||
|
||||
@@ -12,6 +12,9 @@ import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
import com.stardust.autojs.script.JavaScriptSource;
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/8/4.
|
||||
*/
|
||||
@@ -39,6 +42,10 @@ public class Engines {
|
||||
return mEngineService.execute(new AutoFileSource(mScriptRuntime.files.path(path)), config);
|
||||
}
|
||||
|
||||
public Object all() {
|
||||
return mScriptRuntime.bridges.toArray(mEngineService.getEngines());
|
||||
}
|
||||
|
||||
public int stopAll() {
|
||||
return mEngineService.stopAll();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import android.view.KeyEvent;
|
||||
|
||||
import com.stardust.autojs.R;
|
||||
import com.stardust.autojs.core.accessibility.AccessibilityBridge;
|
||||
import com.stardust.autojs.core.boardcast.Broadcast;
|
||||
import com.stardust.autojs.core.boardcast.BroadcastEmitter;
|
||||
import com.stardust.autojs.core.eventloop.EventEmitter;
|
||||
import com.stardust.autojs.core.looper.Loopers;
|
||||
import com.stardust.autojs.core.looper.MainThreadProxy;
|
||||
@@ -55,12 +57,15 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
||||
private KeyInterceptor mKeyInterceptor;
|
||||
private Set<String> mInterceptedKeys = new HashSet<>();
|
||||
|
||||
public final BroadcastEmitter broadcast;
|
||||
|
||||
public Events(Context context, AccessibilityBridge accessibilityBridge, ScriptRuntime runtime) {
|
||||
super(runtime.bridges);
|
||||
mAccessibilityBridge = accessibilityBridge;
|
||||
mContext = context;
|
||||
mLoopers = runtime.loopers;
|
||||
mScriptRuntime = runtime;
|
||||
broadcast = new BroadcastEmitter(runtime.bridges, runtime.timers.getMainTimer());
|
||||
}
|
||||
|
||||
public EventEmitter emitter() {
|
||||
@@ -230,6 +235,7 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
|
||||
|
||||
|
||||
public void recycle() {
|
||||
broadcast.unregister();
|
||||
if (mListeningKey) {
|
||||
AccessibilityService service = mAccessibilityBridge.getService();
|
||||
if (service != null) {
|
||||
|
||||
Reference in New Issue
Block a user