docs: events, automator

This commit is contained in:
hyb1996
2018-02-27 17:59:12 +08:00
parent ce515e58e0
commit 3de96662c6
7 changed files with 87 additions and 16 deletions

View File

@@ -111,13 +111,8 @@ module.exports = function(runtime, global){
global.auto = function(mode){
if(mode){
if(typeof(mode) !== "string"){
throw new TypeError("mode should be a string");
}
mode = modes[mode.toLowerCase()];
global.auto.setMode(mode);
}
mode = mode || 0;
runtime.accessibilityBridge.setMode(mode);
runtime.accessibilityBridge.ensureServiceEnabled();
}
@@ -125,6 +120,14 @@ module.exports = function(runtime, global){
runtime.accessibilityBridge.waitForServiceEnabled();
}
global.auto.setMode = function(mode){
if(typeof(mode) !== "string"){
throw new TypeError("mode should be a string");
}
mode = modes[mode.toLowerCase()] || 0;
runtime.accessibilityBridge.setMode(mode);
}
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']);

View File

@@ -9,8 +9,6 @@ import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import com.stardust.autojs.R;
import com.stardust.autojs.core.accessibility.AccessibilityBridge;
@@ -30,6 +28,9 @@ import com.stardust.view.accessibility.KeyInterceptor;
import com.stardust.view.accessibility.NotificationListener;
import com.stardust.view.accessibility.OnKeyListener;
import java.util.HashSet;
import java.util.Set;
/**
* Created by Stardust on 2017/7/18.
*/
@@ -50,8 +51,9 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
private boolean mListeningNotification = false;
private boolean mListeningToast = false;
private ScriptRuntime mScriptRuntime;
private volatile boolean mInterceptsKey = false;
private volatile boolean mInterceptsAllKey = false;
private KeyInterceptor mKeyInterceptor;
private Set<String> mInterceptedKeys = new HashSet<>();
public Events(Context context, AccessibilityBridge accessibilityBridge, ScriptRuntime runtime) {
super(runtime.bridges);
@@ -105,13 +107,33 @@ public class Events extends EventEmitter implements OnKeyListener, TouchObserver
}
public void setKeyInterceptionEnabled(boolean enabled) {
mInterceptsKey = enabled;
if (mInterceptsKey && mKeyInterceptor == null) {
mKeyInterceptor = event -> mInterceptsKey;
getAccessibilityService().getKeyInterrupterObserver().addKeyInterrupter(mKeyInterceptor);
mInterceptsAllKey = enabled;
if (mInterceptsAllKey) {
ensureKeyInterceptor();
}
}
public void setKeyInterceptionEnabled(String key, boolean enabled) {
if (enabled) {
mInterceptedKeys.add(key);
ensureKeyInterceptor();
} else {
mInterceptedKeys.remove(key);
}
}
private void ensureKeyInterceptor() {
if (mKeyInterceptor != null)
return;
mKeyInterceptor = event -> {
if (mInterceptsAllKey)
return true;
String keyName = KeyEvent.keyCodeToString(event.getKeyCode()).substring(8).toLowerCase();
return mInterceptedKeys.contains(keyName);
};
getAccessibilityService().getKeyInterrupterObserver().addKeyInterrupter(mKeyInterceptor);
}
private AccessibilityService getAccessibilityService() {
mScriptRuntime.ensureAccessibilityServiceEnabled();
AccessibilityService service = mAccessibilityBridge.getService();