diff --git a/autojs/src/main/assets/modules/__globals__.js b/autojs/src/main/assets/modules/__globals__.js index 794b7025..d285b362 100644 --- a/autojs/src/main/assets/modules/__globals__.js +++ b/autojs/src/main/assets/modules/__globals__.js @@ -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(); diff --git a/autojs/src/main/assets/modules/__http__.js b/autojs/src/main/assets/modules/__http__.js index 4332e8c9..4a7e1bf7 100644 --- a/autojs/src/main/assets/modules/__http__.js +++ b/autojs/src/main/assets/modules/__http__.js @@ -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()); diff --git a/autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java b/autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java index c48192f1..3e215aa0 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java +++ b/autojs/src/main/java/com/stardust/autojs/core/accessibility/UiSelector.java @@ -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) { diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/Dialogs.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/Dialogs.java index ccc68af8..166e701e 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/Dialogs.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/Dialogs.java @@ -42,7 +42,6 @@ public class Dialogs { .showAndGet(); } - @ScriptInterface public Object alert(String title, String content, Object callback) { MaterialDialog.Builder builder = dialogBuilder(callback) diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java index 4f28a30c..47099ad7 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java @@ -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); }