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