api(automator): auto.waitFor()
This commit is contained in:
@@ -8,6 +8,7 @@ import com.stardust.autojs.runtime.ScriptRuntime;
|
|||||||
import com.stardust.autojs.runtime.accessibility.AccessibilityConfig;
|
import com.stardust.autojs.runtime.accessibility.AccessibilityConfig;
|
||||||
import com.stardust.autojs.runtime.exception.ScriptException;
|
import com.stardust.autojs.runtime.exception.ScriptException;
|
||||||
import com.stardust.autojs.runtime.api.Console;
|
import com.stardust.autojs.runtime.api.Console;
|
||||||
|
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||||
import com.stardust.scriptdroid.App;
|
import com.stardust.scriptdroid.App;
|
||||||
import com.stardust.scriptdroid.BuildConfig;
|
import com.stardust.scriptdroid.BuildConfig;
|
||||||
import com.stardust.scriptdroid.Pref;
|
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
|
@Override
|
||||||
protected AccessibilityConfig createAccessibilityConfig() {
|
protected AccessibilityConfig createAccessibilityConfig() {
|
||||||
AccessibilityConfig config = super.createAccessibilityConfig();
|
AccessibilityConfig config = super.createAccessibilityConfig();
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
<string name="text_path_is_empty">路径为空</string>
|
<string name="text_path_is_empty">路径为空</string>
|
||||||
<string name="text_file_not_exists">文件不存在</string>
|
<string name="text_file_not_exists">文件不存在</string>
|
||||||
<string name="text_no_file_rw_permission">无文件读写权限</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_close">关闭侧拉菜单</string>
|
||||||
<string name="text_drawer_open">打开侧拉菜单</string>
|
<string name="text_drawer_open">打开侧拉菜单</string>
|
||||||
<string name="text_undo">撤销</string>
|
<string name="text_undo">撤销</string>
|
||||||
@@ -81,7 +81,7 @@
|
|||||||
<string name="text_file_exists">文件已存在</string>
|
<string name="text_file_exists">文件已存在</string>
|
||||||
<string name="text_accessibility_service">无障碍服务</string>
|
<string name="text_accessibility_service">无障碍服务</string>
|
||||||
<string name="text_enable_accessibility_service_by_root">通过Root权限自动启用服务</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="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="text_enable_accessibility_service_by_root_timeout">使用Root权限启动无障碍服务超时</string>
|
||||||
<string name="summary_enable_accessibility_service_by_root">开启后运行脚本会使用Root权限自动开启无障碍服务</string>
|
<string name="summary_enable_accessibility_service_by_root">开启后运行脚本会使用Root权限自动开启无障碍服务</string>
|
||||||
|
|||||||
@@ -1,49 +1,49 @@
|
|||||||
|
|
||||||
module.exports = function(__runtime__, scope){
|
module.exports = function(runtime, global){
|
||||||
var automator = {};
|
var automator = {};
|
||||||
|
|
||||||
function performAction(action, args){
|
function performAction(action, args){
|
||||||
if(args.length == 4){
|
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){
|
}else if(args.length == 2){
|
||||||
return action(__runtime__.automator.text(args[0], args[1]));
|
return action(runtime.automator.text(args[0], args[1]));
|
||||||
}else {
|
}else {
|
||||||
return action(__runtime__.automator.text(args[0], -1));
|
return action(runtime.automator.text(args[0], -1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
automator.click = function(){
|
automator.click = function(){
|
||||||
if(arguments.length == 2 && typeof(arguments[0]) == 'number' && typeof(arguments[1]) == 'number'){
|
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 performAction(function(target){
|
||||||
return __runtime__.automator.click(target);
|
return runtime.automator.click(target);
|
||||||
}, arguments);
|
}, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
automator.longClick = function(a, b, c, d){
|
automator.longClick = function(a, b, c, d){
|
||||||
if(arguments.length == 2 && typeof(arguments[0]) == 'number' && typeof(arguments[1]) == 'number'){
|
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 performAction(function(target){
|
||||||
return __runtime__.automator.longClick(target);
|
return runtime.automator.longClick(target);
|
||||||
}, arguments);
|
}, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
automator.press = __runtime__.automator.press.bind(__runtime__.automator);
|
automator.press = runtime.automator.press.bind(runtime.automator);
|
||||||
automator.gesture = __runtime__.automator.gesture.bind(__runtime__.automator, 0);
|
automator.gesture = runtime.automator.gesture.bind(runtime.automator, 0);
|
||||||
automator.gestureAsync = __runtime__.automator.gestureAsync.bind(__runtime__.automator, 0);
|
automator.gestureAsync = runtime.automator.gestureAsync.bind(runtime.automator, 0);
|
||||||
automator.swipe = __runtime__.automator.swipe.bind(__runtime__.automator);
|
automator.swipe = runtime.automator.swipe.bind(runtime.automator);
|
||||||
automator.gestures = function(){
|
automator.gestures = function(){
|
||||||
return __runtime__.automator.gestures(toStrokes(arguments));
|
return runtime.automator.gestures(toStrokes(arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
automator.gesturesAsync = function(){
|
automator.gesturesAsync = function(){
|
||||||
__runtime__.automator.gesturesAsync(toStrokes(arguments));
|
runtime.automator.gesturesAsync(toStrokes(arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
function toStrokes(args){
|
function toStrokes(args){
|
||||||
var screenMetrics = __runtime__.getScreenMetrics();
|
var screenMetrics = runtime.getScreenMetrics();
|
||||||
var len = args.length;
|
var len = args.length;
|
||||||
var strokes = java.lang.reflect.Array.newInstance(android.accessibilityservice.GestureDescription.StrokeDescription, len);
|
var strokes = java.lang.reflect.Array.newInstance(android.accessibilityservice.GestureDescription.StrokeDescription, len);
|
||||||
for(var i = 0; i < len; i++){
|
for(var i = 0; i < len; i++){
|
||||||
@@ -70,37 +70,37 @@ module.exports = function(__runtime__, scope){
|
|||||||
|
|
||||||
automator.scrollDown = function(a, b, c, d){
|
automator.scrollDown = function(a, b, c, d){
|
||||||
if(arguments.length == 0)
|
if(arguments.length == 0)
|
||||||
return __runtime__.automator.scrollMaxForward();
|
return runtime.automator.scrollMaxForward();
|
||||||
if(arguments.length == 1 && typeof a === 'number')
|
if(arguments.length == 1 && typeof a === 'number')
|
||||||
return __runtime__.automator.scrollForward(a);
|
return runtime.automator.scrollForward(a);
|
||||||
return performAction(function(target){
|
return performAction(function(target){
|
||||||
return __runtime__.automator.scrollForward(target);
|
return runtime.automator.scrollForward(target);
|
||||||
}, arguments);
|
}, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
automator.scrollUp = function(a, b, c, d){
|
automator.scrollUp = function(a, b, c, d){
|
||||||
if(arguments.length == 0)
|
if(arguments.length == 0)
|
||||||
return __runtime__.automator.scrollMaxBackward();
|
return runtime.automator.scrollMaxBackward();
|
||||||
if(arguments.length == 1 && typeof a === 'number')
|
if(arguments.length == 1 && typeof a === 'number')
|
||||||
return __runtime__.automator.scrollBackward(a);
|
return runtime.automator.scrollBackward(a);
|
||||||
return performAction(function(target){
|
return performAction(function(target){
|
||||||
return __runtime__.automator.scrollBackward(target);
|
return runtime.automator.scrollBackward(target);
|
||||||
}, arguments);
|
}, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
automator.setText = function(a, b){
|
automator.setText = function(a, b){
|
||||||
if(arguments.length == 1){
|
if(arguments.length == 1){
|
||||||
return __runtime__.automator.setText(__runtime__.automator.editable(-1), a);
|
return runtime.automator.setText(runtime.automator.editable(-1), a);
|
||||||
}else{
|
}else{
|
||||||
return __runtime__.automator.setText(__runtime__.automator.editable(a), b);
|
return runtime.automator.setText(runtime.automator.editable(a), b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
automator.input = function(a, b){
|
automator.input = function(a, b){
|
||||||
if(arguments.length == 1){
|
if(arguments.length == 1){
|
||||||
return __runtime__.automator.appendText(__runtime__.automator.editable(-1), a);
|
return runtime.automator.appendText(runtime.automator.editable(-1), a);
|
||||||
}else{
|
}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
|
"fast": 1
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.auto = function(mode){
|
global.auto = function(mode){
|
||||||
if(mode){
|
if(mode){
|
||||||
if(typeof(mode) !== "string"){
|
if(typeof(mode) !== "string"){
|
||||||
throw new TypeError("mode should be a string");
|
throw new TypeError("mode should be a string");
|
||||||
@@ -117,12 +117,17 @@ module.exports = function(__runtime__, scope){
|
|||||||
mode = modes[mode.toLowerCase()];
|
mode = modes[mode.toLowerCase()];
|
||||||
}
|
}
|
||||||
mode = mode || 0;
|
mode = mode || 0;
|
||||||
__runtime__.accessibilityBridge.setMode(mode);
|
runtime.accessibilityBridge.setMode(mode);
|
||||||
__runtime__.accessibilityBridge.ensureServiceEnabled();
|
runtime.accessibilityBridge.ensureServiceEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.__asGlobal__(__runtime__.automator, ['back', 'home', 'powerDialog', 'notifications', 'quickSettings', 'recents', 'splitScreen']);
|
global.auto.waitFor = function(){
|
||||||
scope.__asGlobal__(automator, ['click', 'longClick', 'press', 'swipe', 'gesture', 'gestures', 'gestureAsync', 'gesturesAsync', 'scrollDown', 'scrollUp', 'input', 'setText']);
|
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;
|
return automator;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,72 +1,72 @@
|
|||||||
|
|
||||||
module.exports = function(__runtime__, scope){
|
module.exports = function(runtime, global){
|
||||||
scope.toast = function(text){
|
global.toast = function(text){
|
||||||
__runtime__.toast(text);
|
runtime.toast(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.toastLog = function(text){
|
global.toastLog = function(text){
|
||||||
__runtime__.toast(text);
|
runtime.toast(text);
|
||||||
scope.log(text);
|
global.log(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.sleep = __runtime__.sleep.bind(__runtime__);
|
global.sleep = runtime.sleep.bind(runtime);
|
||||||
|
|
||||||
scope.isStopped = function(){
|
global.isStopped = function(){
|
||||||
return __runtime__.isStopped();
|
return runtime.isStopped();
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.isShuttingDown = scope.isShopped;
|
global.isShuttingDown = global.isShopped;
|
||||||
|
|
||||||
scope.notStopped = function(){
|
global.notStopped = function(){
|
||||||
return !isStopped();
|
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){
|
global.setClip = function(text){
|
||||||
__runtime__.setClip(text);
|
runtime.setClip(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.getClip = function(text){
|
global.getClip = function(text){
|
||||||
return __runtime__.getClip();
|
return runtime.getClip();
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.currentPackage = function(){
|
global.currentPackage = function(){
|
||||||
scope.auto();
|
global.auto();
|
||||||
return __runtime__.info.getLatestPackage();
|
return runtime.info.getLatestPackage();
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.currentActivity = function(){
|
global.currentActivity = function(){
|
||||||
scope.auto();
|
global.auto();
|
||||||
return __runtime__.info.getLatestActivity();
|
return runtime.info.getLatestActivity();
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.waitForActivity = function(activity, period){
|
global.waitForActivity = function(activity, period){
|
||||||
period = period || 200;
|
period = period || 200;
|
||||||
while(scope.currentActivity() != activity){
|
while(global.currentActivity() != activity){
|
||||||
sleep(period);
|
sleep(period);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.waitForPackage = function(packageName, period){
|
global.waitForPackage = function(packageName, period){
|
||||||
period = period || 200;
|
period = period || 200;
|
||||||
while(scope.currentPackage() != packageName){
|
while(global.currentPackage() != packageName){
|
||||||
sleep(period);
|
sleep(period);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.random = function(min, max){
|
global.random = function(min, max){
|
||||||
if(arguments.length == 0){
|
if(arguments.length == 0){
|
||||||
return Math.random();
|
return Math.random();
|
||||||
}
|
}
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.setScreenMetrics = __runtime__.setScreenMetrics.bind(__runtime__);
|
global.setScreenMetrics = runtime.setScreenMetrics.bind(runtime);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -174,6 +174,8 @@ public abstract class AutoJs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public abstract void waitForAccessibilityServiceEnabled();
|
||||||
|
|
||||||
protected AccessibilityConfig createAccessibilityConfig() {
|
protected AccessibilityConfig createAccessibilityConfig() {
|
||||||
return new AccessibilityConfig();
|
return new AccessibilityConfig();
|
||||||
}
|
}
|
||||||
@@ -189,6 +191,11 @@ public abstract class AutoJs {
|
|||||||
AutoJs.this.ensureAccessibilityServiceEnabled();
|
AutoJs.this.ensureAccessibilityServiceEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void waitForServiceEnabled() {
|
||||||
|
AutoJs.this.waitForAccessibilityServiceEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public AccessibilityService getService() {
|
public AccessibilityService getService() {
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ public abstract class AccessibilityBridge {
|
|||||||
|
|
||||||
public abstract void ensureServiceEnabled();
|
public abstract void ensureServiceEnabled();
|
||||||
|
|
||||||
|
public abstract void waitForServiceEnabled();
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public abstract AccessibilityService getService();
|
public abstract AccessibilityService getService();
|
||||||
|
|
||||||
|
|||||||
@@ -152,6 +152,12 @@ public class AccessibilityService extends android.accessibilityservice.Accessibi
|
|||||||
return true;
|
return true;
|
||||||
LOCK.lock();
|
LOCK.lock();
|
||||||
try {
|
try {
|
||||||
|
if (instance != null)
|
||||||
|
return true;
|
||||||
|
if (timeOut == -1) {
|
||||||
|
ENABLED.await();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return ENABLED.await(timeOut, TimeUnit.MILLISECONDS);
|
return ENABLED.await(timeOut, TimeUnit.MILLISECONDS);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.stardust.auojs.inrt.R;
|
|||||||
import com.stardust.auojs.inrt.SettingsActivity;
|
import com.stardust.auojs.inrt.SettingsActivity;
|
||||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||||
import com.stardust.autojs.runtime.exception.ScriptException;
|
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.AccessibilityService;
|
||||||
import com.stardust.view.accessibility.AccessibilityServiceUtils;
|
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
|
@Override
|
||||||
protected Application getApplication() {
|
protected Application getApplication() {
|
||||||
return App.getApp();
|
return App.getApp();
|
||||||
|
|||||||
Reference in New Issue
Block a user