mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-24 21:33:16 +08:00
新增 阻塞操作加上ui线程判断,在ui线程执行时会抛出异常:
waitForPackage, waitForActivity, http模块的方法没有加callback参数时 findOne(), untilFind()等 sleep()
This commit is contained in:
parent
c803bb6efc
commit
d3c073fa9d
@ -9,7 +9,12 @@ module.exports = function(runtime, global){
|
||||
global.log(text);
|
||||
}
|
||||
|
||||
global.sleep = runtime.sleep.bind(runtime);
|
||||
global.sleep = function(t) {
|
||||
if(ui.isUiThread()){
|
||||
throw new Error("不能在ui线程执行阻塞操作,请使用setTimeout代替");
|
||||
}
|
||||
runtime.sleep(t);
|
||||
}
|
||||
|
||||
global.isStopped = function(){
|
||||
return runtime.isStopped();
|
||||
@ -25,7 +30,6 @@ module.exports = function(runtime, global){
|
||||
|
||||
global.exit = runtime.exit.bind(runtime);
|
||||
|
||||
|
||||
global.stop = global.exit;
|
||||
|
||||
global.setClip = function(text){
|
||||
@ -47,6 +51,7 @@ module.exports = function(runtime, global){
|
||||
}
|
||||
|
||||
global.waitForActivity = function(activity, period){
|
||||
ensureNonUiThread();
|
||||
period = period || 200;
|
||||
while(global.currentActivity() != activity){
|
||||
sleep(period);
|
||||
@ -54,12 +59,19 @@ module.exports = function(runtime, global){
|
||||
}
|
||||
|
||||
global.waitForPackage = function(packageName, period){
|
||||
ensureNonUiThread();
|
||||
period = period || 200;
|
||||
while(global.currentPackage() != packageName){
|
||||
sleep(period);
|
||||
}
|
||||
}
|
||||
|
||||
function ensureNonUiThread() {
|
||||
if(ui.isUiThread()){
|
||||
throw new Error("不能在ui线程执行阻塞操作,请在子线程或子脚本执行,或者使用setInterval循环检测当前activity和package");
|
||||
}
|
||||
}
|
||||
|
||||
global.random = function(min, max){
|
||||
if(arguments.length == 0){
|
||||
return Math.random();
|
||||
|
||||
@ -40,6 +40,9 @@ module.exports = function(runtime, scope){
|
||||
}
|
||||
|
||||
http.request = function(url, options, callback){
|
||||
if(!callback && ui.isUiThread()){
|
||||
throw new Error("不能在ui线程执行网络操作,请加上回调函数的参数callback或在子线程执行");
|
||||
}
|
||||
var call = http.client().newCall(http.buildRequest(url, options));
|
||||
if(!callback){
|
||||
return wrapResponse(call.execute());
|
||||
|
||||
@ -140,6 +140,7 @@ public class UiSelector extends UiGlobalSelector {
|
||||
@ScriptInterface
|
||||
@NonNull
|
||||
public UiObjectCollection untilFind() {
|
||||
ensureNonUiThread();
|
||||
UiObjectCollection uiObjectCollection = find();
|
||||
while (uiObjectCollection.empty()) {
|
||||
if (Thread.currentThread().isInterrupted()) {
|
||||
@ -155,6 +156,13 @@ public class UiSelector extends UiGlobalSelector {
|
||||
return uiObjectCollection;
|
||||
}
|
||||
|
||||
private void ensureNonUiThread() {
|
||||
if(Looper.myLooper() == Looper.getMainLooper()){
|
||||
// TODO: 2018/11/1 配置字符串
|
||||
throw new IllegalThreadStateException("不能在ui线程执行阻塞操作, 请在子线程或子脚本执行findOne()或untilFind()");
|
||||
}
|
||||
}
|
||||
|
||||
@ScriptInterface
|
||||
public UiObject findOne(long timeout) {
|
||||
if (timeout == -1) {
|
||||
|
||||
@ -42,7 +42,6 @@ public class Dialogs {
|
||||
.showAndGet();
|
||||
}
|
||||
|
||||
|
||||
@ScriptInterface
|
||||
public Object alert(String title, String content, Object callback) {
|
||||
MaterialDialog.Builder builder = dialogBuilder(callback)
|
||||
|
||||
@ -9,7 +9,6 @@ import android.graphics.Matrix;
|
||||
import android.media.Image;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.SystemClock;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.util.Base64;
|
||||
import android.view.Display;
|
||||
@ -18,10 +17,11 @@ import android.view.WindowManager;
|
||||
import com.stardust.autojs.annotation.ScriptVariable;
|
||||
import com.stardust.autojs.core.image.ColorFinder;
|
||||
import com.stardust.autojs.core.image.ImageWrapper;
|
||||
import com.stardust.autojs.core.opencv.OpenCVHelper;
|
||||
import com.stardust.autojs.core.image.TemplateMatching;
|
||||
import com.stardust.autojs.core.image.capture.ScreenCaptureRequester;
|
||||
import com.stardust.autojs.core.image.capture.ScreenCapturer;
|
||||
import com.stardust.autojs.core.image.TemplateMatching;
|
||||
import com.stardust.autojs.core.opencv.Mat;
|
||||
import com.stardust.autojs.core.opencv.OpenCVHelper;
|
||||
import com.stardust.autojs.core.ui.inflater.util.Drawables;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
@ -29,8 +29,6 @@ import com.stardust.concurrent.VolatileDispose;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
import com.stardust.util.ScreenMetrics;
|
||||
|
||||
import com.stardust.autojs.core.opencv.Mat;
|
||||
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Rect;
|
||||
|
||||
@ -52,7 +50,6 @@ public class Images {
|
||||
private ScreenCaptureRequester mScreenCaptureRequester;
|
||||
private ScreenCapturer mScreenCapturer;
|
||||
private Context mContext;
|
||||
private Display mDisplay;
|
||||
private Image mPreCapture;
|
||||
private ImageWrapper mPreCaptureImage;
|
||||
private ScreenMetrics mScreenMetrics;
|
||||
@ -64,7 +61,6 @@ public class Images {
|
||||
mScriptRuntime = scriptRuntime;
|
||||
mScreenCaptureRequester = screenCaptureRequester;
|
||||
mContext = context;
|
||||
mDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
|
||||
mScreenMetrics = mScriptRuntime.getScreenMetrics();
|
||||
colorFinder = new ColorFinder(mScreenMetrics);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user