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

@ -8,6 +8,7 @@ import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.autojs.runtime.accessibility.AccessibilityConfig;
import com.stardust.autojs.runtime.exception.ScriptException;
import com.stardust.autojs.runtime.api.Console;
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.BuildConfig;
import com.stardust.scriptdroid.Pref;
@ -76,6 +77,31 @@ public class AutoJs extends com.stardust.autojs.AutoJs {
}
}
@Override
public void waitForAccessibilityServiceEnabled() {
if (AccessibilityService.getInstance() != null) {
return;
}
String errorMessage = null;
if (AccessibilityServiceTool.isAccessibilityServiceEnabled(App.getApp())) {
errorMessage = App.getApp().getString(R.string.text_auto_operate_service_enabled_but_not_running);
} else {
if (Pref.shouldEnableAccessibilityServiceByRoot()) {
if (!AccessibilityServiceTool.enableAccessibilityServiceByRootAndWaitFor(2000)) {
errorMessage = App.getApp().getString(R.string.text_enable_accessibility_service_by_root_timeout);
}
} else {
errorMessage = App.getApp().getString(R.string.text_no_accessibility_permission);
}
}
if (errorMessage != null) {
AccessibilityServiceTool.goToAccessibilitySetting();
if (!com.stardust.scriptdroid.accessibility.AccessibilityService.waitForEnabled(-1)) {
throw new ScriptInterruptedException();
}
}
}
@Override
protected AccessibilityConfig createAccessibilityConfig() {
AccessibilityConfig config = super.createAccessibilityConfig();

View File

@ -10,7 +10,7 @@
<string name="text_path_is_empty">路径为空</string>
<string name="text_file_not_exists">文件不存在</string>
<string name="text_no_file_rw_permission">无文件读写权限</string>
<string name="text_no_accessibility_permission">无障碍服务未启动,脚本已停止运行</string>
<string name="text_no_accessibility_permission">无障碍服务未启动</string>
<string name="text_drawer_close">关闭侧拉菜单</string>
<string name="text_drawer_open">打开侧拉菜单</string>
<string name="text_undo">撤销</string>
@ -81,7 +81,7 @@
<string name="text_file_exists">文件已存在</string>
<string name="text_accessibility_service">无障碍服务</string>
<string name="text_enable_accessibility_service_by_root">通过Root权限自动启用服务</string>
<string name="text_auto_operate_service_enabled_but_not_running">无障碍服务已启用但并未运行这可能是安卓的BUG您可能需要重启手机</string>
<string name="text_auto_operate_service_enabled_but_not_running">无障碍服务已启用但并未运行这可能是安卓的BUG您可能需要重启手机或重启无障碍服务</string>
<string name="key_enable_accessibility_service_by_root">key_enable_accessibility_service_by_root</string>
<string name="text_enable_accessibility_service_by_root_timeout">使用Root权限启动无障碍服务超时</string>
<string name="summary_enable_accessibility_service_by_root">开启后运行脚本会使用Root权限自动开启无障碍服务</string>

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();

View File

@ -152,6 +152,12 @@ public class AccessibilityService extends android.accessibilityservice.Accessibi
return true;
LOCK.lock();
try {
if (instance != null)
return true;
if (timeOut == -1) {
ENABLED.await();
return true;
}
return ENABLED.await(timeOut, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();

View File

@ -10,6 +10,7 @@ import com.stardust.auojs.inrt.R;
import com.stardust.auojs.inrt.SettingsActivity;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.autojs.runtime.exception.ScriptException;
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
import com.stardust.view.accessibility.AccessibilityService;
import com.stardust.view.accessibility.AccessibilityServiceUtils;
@ -58,6 +59,31 @@ public class AutoJs extends com.stardust.autojs.AutoJs {
}
}
@Override
public void waitForAccessibilityServiceEnabled() {
if (AccessibilityService.getInstance() != null) {
return;
}
String errorMessage = null;
if (AccessibilityServiceUtils.isAccessibilityServiceEnabled(getApplication(), AccessibilityService.class)) {
errorMessage = App.getApp().getString(R.string.text_auto_operate_service_enabled_but_not_running);
} else {
if (Pref.shouldEnableAccessibilityServiceByRoot()) {
if (!AccessibilityServiceTool.enableAccessibilityServiceByRootAndWaitFor(getApplication(), 2000)) {
errorMessage = App.getApp().getString(R.string.text_enable_accessibility_service_by_root_timeout);
}
} else {
errorMessage = App.getApp().getString(R.string.text_no_accessibility_permission);
}
}
if (errorMessage != null) {
AccessibilityServiceTool.goToAccessibilitySetting();
if (!AccessibilityService.waitForEnabled(-1)) {
throw new ScriptInterruptedException();
}
}
}
@Override
protected Application getApplication() {
return App.getApp();