api(automator): auto.waitFor()

This commit is contained in:
hyb1996
2018-02-27 17:28:50 +08:00
parent 528171b77d
commit ce515e58e0
8 changed files with 134 additions and 62 deletions

View File

@@ -1,49 +1,49 @@
module.exports = function(__runtime__, scope){
module.exports = function(runtime, global){
var automator = {};
function performAction(action, args){
if(args.length == 4){
return action(__runtime__.automator.bounds(args[0], args[1], args[2], args[3]));
return action(runtime.automator.bounds(args[0], args[1], args[2], args[3]));
}else if(args.length == 2){
return action(__runtime__.automator.text(args[0], args[1]));
return action(runtime.automator.text(args[0], args[1]));
}else {
return action(__runtime__.automator.text(args[0], -1));
return action(runtime.automator.text(args[0], -1));
}
}
automator.click = function(){
if(arguments.length == 2 && typeof(arguments[0]) == 'number' && typeof(arguments[1]) == 'number'){
return __runtime__.automator.click(arguments[0], arguments[1]);
return runtime.automator.click(arguments[0], arguments[1]);
}
return performAction(function(target){
return __runtime__.automator.click(target);
return runtime.automator.click(target);
}, arguments);
}
automator.longClick = function(a, b, c, d){
if(arguments.length == 2 && typeof(arguments[0]) == 'number' && typeof(arguments[1]) == 'number'){
return __runtime__.automator.longClick(arguments[0], arguments[1]);
return runtime.automator.longClick(arguments[0], arguments[1]);
}
return performAction(function(target){
return __runtime__.automator.longClick(target);
return runtime.automator.longClick(target);
}, arguments);
}
automator.press = __runtime__.automator.press.bind(__runtime__.automator);
automator.gesture = __runtime__.automator.gesture.bind(__runtime__.automator, 0);
automator.gestureAsync = __runtime__.automator.gestureAsync.bind(__runtime__.automator, 0);
automator.swipe = __runtime__.automator.swipe.bind(__runtime__.automator);
automator.press = runtime.automator.press.bind(runtime.automator);
automator.gesture = runtime.automator.gesture.bind(runtime.automator, 0);
automator.gestureAsync = runtime.automator.gestureAsync.bind(runtime.automator, 0);
automator.swipe = runtime.automator.swipe.bind(runtime.automator);
automator.gestures = function(){
return __runtime__.automator.gestures(toStrokes(arguments));
return runtime.automator.gestures(toStrokes(arguments));
}
automator.gesturesAsync = function(){
__runtime__.automator.gesturesAsync(toStrokes(arguments));
runtime.automator.gesturesAsync(toStrokes(arguments));
}
function toStrokes(args){
var screenMetrics = __runtime__.getScreenMetrics();
var screenMetrics = runtime.getScreenMetrics();
var len = args.length;
var strokes = java.lang.reflect.Array.newInstance(android.accessibilityservice.GestureDescription.StrokeDescription, len);
for(var i = 0; i < len; i++){
@@ -70,37 +70,37 @@ module.exports = function(__runtime__, scope){
automator.scrollDown = function(a, b, c, d){
if(arguments.length == 0)
return __runtime__.automator.scrollMaxForward();
return runtime.automator.scrollMaxForward();
if(arguments.length == 1 && typeof a === 'number')
return __runtime__.automator.scrollForward(a);
return runtime.automator.scrollForward(a);
return performAction(function(target){
return __runtime__.automator.scrollForward(target);
return runtime.automator.scrollForward(target);
}, arguments);
}
automator.scrollUp = function(a, b, c, d){
if(arguments.length == 0)
return __runtime__.automator.scrollMaxBackward();
return runtime.automator.scrollMaxBackward();
if(arguments.length == 1 && typeof a === 'number')
return __runtime__.automator.scrollBackward(a);
return runtime.automator.scrollBackward(a);
return performAction(function(target){
return __runtime__.automator.scrollBackward(target);
return runtime.automator.scrollBackward(target);
}, arguments);
}
automator.setText = function(a, b){
if(arguments.length == 1){
return __runtime__.automator.setText(__runtime__.automator.editable(-1), a);
return runtime.automator.setText(runtime.automator.editable(-1), a);
}else{
return __runtime__.automator.setText(__runtime__.automator.editable(a), b);
return runtime.automator.setText(runtime.automator.editable(a), b);
}
}
automator.input = function(a, b){
if(arguments.length == 1){
return __runtime__.automator.appendText(__runtime__.automator.editable(-1), a);
return runtime.automator.appendText(runtime.automator.editable(-1), a);
}else{
return __runtime__.automator.appendText(__runtime__.automator.editable(a), b);
return runtime.automator.appendText(runtime.automator.editable(a), b);
}
}
@@ -109,7 +109,7 @@ module.exports = function(__runtime__, scope){
"fast": 1
}
scope.auto = function(mode){
global.auto = function(mode){
if(mode){
if(typeof(mode) !== "string"){
throw new TypeError("mode should be a string");
@@ -117,12 +117,17 @@ module.exports = function(__runtime__, scope){
mode = modes[mode.toLowerCase()];
}
mode = mode || 0;
__runtime__.accessibilityBridge.setMode(mode);
__runtime__.accessibilityBridge.ensureServiceEnabled();
runtime.accessibilityBridge.setMode(mode);
runtime.accessibilityBridge.ensureServiceEnabled();
}
scope.__asGlobal__(__runtime__.automator, ['back', 'home', 'powerDialog', 'notifications', 'quickSettings', 'recents', 'splitScreen']);
scope.__asGlobal__(automator, ['click', 'longClick', 'press', 'swipe', 'gesture', 'gestures', 'gestureAsync', 'gesturesAsync', 'scrollDown', 'scrollUp', 'input', 'setText']);
global.auto.waitFor = function(){
runtime.accessibilityBridge.waitForServiceEnabled();
}
global.__asGlobal__(runtime.automator, ['back', 'home', 'powerDialog', 'notifications', 'quickSettings', 'recents', 'splitScreen']);
global.__asGlobal__(automator, ['click', 'longClick', 'press', 'swipe', 'gesture', 'gestures', 'gestureAsync', 'gesturesAsync', 'scrollDown', 'scrollUp', 'input', 'setText']);
return automator;
}

View File

@@ -1,72 +1,72 @@
module.exports = function(__runtime__, scope){
scope.toast = function(text){
__runtime__.toast(text);
module.exports = function(runtime, global){
global.toast = function(text){
runtime.toast(text);
}
scope.toastLog = function(text){
__runtime__.toast(text);
scope.log(text);
global.toastLog = function(text){
runtime.toast(text);
global.log(text);
}
scope.sleep = __runtime__.sleep.bind(__runtime__);
global.sleep = runtime.sleep.bind(runtime);
scope.isStopped = function(){
return __runtime__.isStopped();
global.isStopped = function(){
return runtime.isStopped();
}
scope.isShuttingDown = scope.isShopped;
global.isShuttingDown = global.isShopped;
scope.notStopped = function(){
global.notStopped = function(){
return !isStopped();
}
scope.isRunning = scope.notStopped;
global.isRunning = global.notStopped;
scope.exit = __runtime__.exit.bind(__runtime__);
global.exit = runtime.exit.bind(runtime);
scope.stop = scope.exit;
global.stop = global.exit;
scope.setClip = function(text){
__runtime__.setClip(text);
global.setClip = function(text){
runtime.setClip(text);
}
scope.getClip = function(text){
return __runtime__.getClip();
global.getClip = function(text){
return runtime.getClip();
}
scope.currentPackage = function(){
scope.auto();
return __runtime__.info.getLatestPackage();
global.currentPackage = function(){
global.auto();
return runtime.info.getLatestPackage();
}
scope.currentActivity = function(){
scope.auto();
return __runtime__.info.getLatestActivity();
global.currentActivity = function(){
global.auto();
return runtime.info.getLatestActivity();
}
scope.waitForActivity = function(activity, period){
global.waitForActivity = function(activity, period){
period = period || 200;
while(scope.currentActivity() != activity){
while(global.currentActivity() != activity){
sleep(period);
}
}
scope.waitForPackage = function(packageName, period){
global.waitForPackage = function(packageName, period){
period = period || 200;
while(scope.currentPackage() != packageName){
while(global.currentPackage() != packageName){
sleep(period);
}
}
scope.random = function(min, max){
global.random = function(min, max){
if(arguments.length == 0){
return Math.random();
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
scope.setScreenMetrics = __runtime__.setScreenMetrics.bind(__runtime__);
global.setScreenMetrics = runtime.setScreenMetrics.bind(runtime);
}

View File

@@ -174,6 +174,8 @@ public abstract class AutoJs {
}
public abstract void waitForAccessibilityServiceEnabled();
protected AccessibilityConfig createAccessibilityConfig() {
return new AccessibilityConfig();
}
@@ -189,6 +191,11 @@ public abstract class AutoJs {
AutoJs.this.ensureAccessibilityServiceEnabled();
}
@Override
public void waitForServiceEnabled() {
AutoJs.this.waitForAccessibilityServiceEnabled();
}
@Nullable
@Override
public AccessibilityService getService() {

View File

@@ -29,6 +29,8 @@ public abstract class AccessibilityBridge {
public abstract void ensureServiceEnabled();
public abstract void waitForServiceEnabled();
@Nullable
public abstract AccessibilityService getService();