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