refactor(module autojs): package com.stardust.autojs.runtime.inputevent

add: module engines
This commit is contained in:
hyb1996
2017-08-04 15:19:57 +08:00
parent f2c1015470
commit 19dac12614
37 changed files with 904 additions and 562 deletions

View File

@@ -32,6 +32,11 @@ launchApp("微信");
获取应用的包名。例如getPackageName("QQ")为"com.tencent.mobileqq"。如果有相同名称的应用,只返回其中某一个的包名。如果不存在这个名称的应用,会返回空字符串。
### getAppName(packageName)
* packageName \<String\> 应用包名
返回对应包名的应用的名称。如果应用不存在返回null。
### openAppSetting(packageName)
* packageName \<String\> 应用包名

View File

@@ -104,7 +104,9 @@ events.loop();
删除所有事件监听函数。
### key事件
### 事件: 'key'
* keyCode \<Number\> 键值
* event \<KeyEvent\> 事件
当有按键被按下或弹起时会触发该事件。
```
@@ -122,7 +124,9 @@ events.loop();
* KeyEvent.KEYCODE_VOLUME_UP 音量上键
* KeyEvent.KEYCODE_VOLUME_DOWN 音量下键
### key_down事件
### 事件: 'key_down'
* keyCode \<Number\> 键值
* event \<KeyEvent\> 事件
当有按键被按下时会触发该事件。
```
@@ -134,7 +138,9 @@ events.on("key_down", function(keyCode, event){
events.loop();
```
### key_up事件
### 事件: 'key_up'
* keyCode \<Number\> 键值
* event \<KeyEvent\> 事件
当有按键弹起时会触发该事件。
```
@@ -148,27 +154,34 @@ events.loop();
### obverseNotification()
开启通知(包括Toast)监听。
```
events.obverseNotification();
events.onNotification(function(notification){
log(notification.getText());
});
events.onToast(function(toast){
log(toast.getText());
});
```
### toast事件
### 事件: 'toast'
* toast \<Object\>
* getText() 获取Toast的文本内容
* getPackageName() 获取发出Toast的应用包名
### 事件: 'notification'
* notification \<Notification\> 通知
* notification \<Object\> 通知
```
events.observeNotification();
events.on("notification", function(notification){
log(notification.
log(notification);
});
```
# EventEmitter
### EventEmitter.defaultMaxListeners

View File

@@ -244,6 +244,7 @@
"emitter",
"observeKey",
"observeTouch",
"observeNotification",
"onKeyDown",
"onKeyUp",
"onceKeyDown",

View File

@@ -0,0 +1,9 @@
auto();
events.observeNotification();
events.onToast(function(toast){
var pkg = toast.getPackageName();
log("Toast内容: " + toast.getText() +
" 来自: " + getAppName(pkg) +
" 包名: " + pkg);
});
toast("监听中请在日志中查看记录的Toast及其内容");

View File

@@ -0,0 +1,21 @@
auto();
events.observeNotification();
events.onNotification(function(info, notification){
printNotification(info, notification);
});
toast("监听中,请在日志中查看记录的通知及其内容");
function printNotification(info, notification){
log("应用包名: " + info.getPackageName());
log("通知文本: " + info.getTexts());
log("通知优先级: " + notification.priority);
log("通知目录: " + notification.category);
log("通知创建时间: " + new Date(notification.creationTime));
log("通知时间: " + new Date(notification.when));
log("通知数: " + notification.number);
log("通知摘要: " + notification.tickerText);
for(var i = 0; i < notification.actions.length; i++){
var action = notification.actions[i];
log("通知" + (i + 1) + ": " + action.title);
}
}

View File

@@ -0,0 +1 @@
engines.stopAllAndToast();

View File

@@ -0,0 +1,14 @@
var scriptsPath = "/sdcard/脚本/";
if(!files.exists(scriptsPath)){
scriptsPath = "/sdcard/Scripts/";
}
var scriptFiles = files.listDir(scriptsPath, function(name){
return name.endsWith(".auto");
});
var i = dialogs.singleChoice("请选择要运行的脚本", scriptFiles);
if(i < 0){
exit();
}
var path = files.join(scriptsPath, scriptFiles[i]);
engines.execAutoFile(path);

View File

@@ -0,0 +1,6 @@
var script = "toast('Hello, Auto.js');" +
"sleep(3000);" +
"toast('略略略');";
var execution = engines.execScript(script);
sleep(1000);
execution.getEngine().forceStop();

View File

@@ -0,0 +1,14 @@
var scriptsPath = "/sdcard/脚本/";
if(!files.exists(scriptsPath)){
scriptsPath = "/sdcard/Scripts/";
}
var scriptFiles = files.listDir(scriptsPath, function(name){
return name.endsWith(".js");
});
var i = dialogs.singleChoice("请选择要运行的脚本", scriptFiles);
if(i < 0){
exit();
}
var path = files.join(scriptsPath, scriptFiles[i]);
engines.execScriptFile(path);

View File

@@ -23,6 +23,7 @@ import com.stardust.autojs.runtime.api.AbstractShell;
import com.stardust.autojs.runtime.api.AppUtils;
import com.stardust.autojs.runtime.api.Console;
import com.stardust.autojs.runtime.api.image.ScreenCaptureRequester;
import com.stardust.autojs.runtime.record.inputevent.InputEventObserver;
import com.stardust.autojs.script.AutoFileSource;
import com.stardust.autojs.script.JavaScriptSource;
import com.stardust.scriptdroid.App;
@@ -84,6 +85,7 @@ public class AutoJs {
addAccessibilityServiceDelegates();
mScriptEngineService.registerGlobalScriptExecutionListener(new ScriptExecutionGlobalListener());
registerActivityLifecycleCallbacks();
InputEventObserver.initGlobal(context);
}
private ScriptEngineService buildScriptEngineService() {
@@ -107,6 +109,7 @@ public class AutoJs {
.setAccessibilityBridge(new AccessibilityBridgeImpl())
.setUiHandler(mUiHandler)
.setAppUtils(mAppUtils)
.setEngineService(mScriptEngineService)
.setShellSupplier(new Supplier<AbstractShell>() {
@Override
public AbstractShell get() {

View File

@@ -12,6 +12,7 @@ import android.widget.Toast;
import com.stardust.autojs.runtime.record.Recorder;
import com.stardust.autojs.runtime.record.accessibility.AccessibilityActionRecorder;
import com.stardust.autojs.runtime.record.inputevent.InputEventObserver;
import com.stardust.autojs.runtime.record.inputevent.KeyObserver;
import com.stardust.autojs.runtime.record.inputevent.TouchRecorder;
import com.stardust.scriptdroid.App;
@@ -61,9 +62,11 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
private Recorder mRecorder;
private TouchRecorder mTouchRecorder;
private Context mContext;
private boolean mDiscard = false;
private KeyObserver mKeyObserver;
private InputEventObserver mInputEventObserver = InputEventObserver.getGlobal();
private OnKeyListener mVolumeKeyListener = new OnKeyListener() {
@Override
public void onKeyEvent(int keyCode, KeyEvent event) {
@@ -72,7 +75,7 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
&& Pref.isRecordVolumeControlEnable()) {
if (mRecorder == null) {
startRecord();
} else if (alreadyStartedRecord()) {
} else if (alreadyStartRecord()) {
stopRecord();
}
}
@@ -85,9 +88,10 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
ButterKnife.bind(this, mView);
HoverMenuService.getEventBus().register(this);
AccessibilityService.getStickOnKeyObserver().addListener(mVolumeKeyListener);
mTouchRecorder = TouchRecorder.getGlobal(context);
if (Pref.hasRecordTrigger()) {
mKeyObserver = new KeyObserver(mContext);
mKeyObserver.startListening();
mKeyObserver = new KeyObserver();
mInputEventObserver.addListener(mKeyObserver);
mKeyObserver.setKeyListener(this);
}
}
@@ -148,7 +152,12 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
private void startRecord() {
mDiscard = false;
mRecorder = mRecordedByRootSwitch.isChecked() ? new TouchRecorder(mContext) : AutoJs.getInstance().getAccessibilityActionRecorder();
if (mRecordedByRootSwitch.isChecked()) {
mTouchRecorder.reset();
mRecorder = mTouchRecorder;
} else {
mRecorder = AutoJs.getInstance().getAccessibilityActionRecorder();
}
mRecorder.setOnStateChangedListener(this);
mRecorder.start();
setState(Recorder.STATE_RECORDING);
@@ -187,9 +196,8 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
public void onMenuExit() {
HoverMenuService.getEventBus().unregister(this);
AccessibilityService.getStickOnKeyObserver().addListener(mVolumeKeyListener);
if (mKeyObserver != null) {
mKeyObserver.stopListening();
}
mInputEventObserver.recycle();
mInputEventObserver.removeListener(mKeyObserver);
}
@Subscribe
@@ -228,7 +236,7 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
@Override
public void onKeyDown(String keyName) {
if (keyName.equals(Pref.getStopRecordTrigger())) {
if (alreadyStartedRecord())
if (alreadyStartRecord())
stopRecord();
} else if (keyName.equals(Pref.getStartRecordTrigger())) {
if (mRecorder == null)
@@ -236,7 +244,7 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
}
}
private boolean alreadyStartedRecord() {
private boolean alreadyStartRecord() {
return mRecorder != null && mRecorder.getState() == Recorder.STATE_RECORDING && mRecorder.getState() == Recorder.STATE_PAUSED;
}

View File

@@ -9,7 +9,6 @@ import com.stardust.autojs.execution.ScriptExecution;
import com.stardust.autojs.execution.ScriptExecutionListener;
import com.stardust.autojs.execution.SimpleScriptExecutionListener;
import com.stardust.autojs.runtime.ScriptInterruptedException;
import com.stardust.autojs.script.JavaScriptFileSource;
import com.stardust.autojs.script.ScriptSource;
import com.stardust.autojs.script.StringScriptSource;
import com.stardust.scriptdroid.App;
@@ -85,17 +84,17 @@ public class Scripts {
public static ScriptExecution run(ScriptSource source, String directoryPath) {
return AutoJs.getInstance().getScriptEngineService().execute(source, new ExecutionConfig()
.requirePath(directoryPath, StorageScriptProvider.DEFAULT_DIRECTORY_PATH));
.path(directoryPath, StorageScriptProvider.DEFAULT_DIRECTORY_PATH));
}
public static ScriptExecution run(ScriptSource source) {
return AutoJs.getInstance().getScriptEngineService().execute(source, new ExecutionConfig()
.requirePath(StorageScriptProvider.DEFAULT_DIRECTORY_PATH));
.path(StorageScriptProvider.DEFAULT_DIRECTORY_PATH));
}
public static ScriptExecution runWithBroadcastSender(ScriptSource scriptSource, String directoryPath) {
return AutoJs.getInstance().getScriptEngineService().execute(scriptSource, BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER,
new ExecutionConfig().requirePath(directoryPath, StorageScriptProvider.DEFAULT_DIRECTORY_PATH));
new ExecutionConfig().path(directoryPath, StorageScriptProvider.DEFAULT_DIRECTORY_PATH));
}
public static ScriptExecution run(Context context, Sample file) {
@@ -105,14 +104,14 @@ public class Scripts {
public static ScriptExecution runWithBroadcastSender(ScriptSource source) {
return AutoJs.getInstance().getScriptEngineService().execute(source, BROADCAST_SENDER_SCRIPT_EXECUTION_LISTENER,
new ExecutionConfig().requirePath(StorageScriptProvider.DEFAULT_DIRECTORY_PATH));
new ExecutionConfig().path(StorageScriptProvider.DEFAULT_DIRECTORY_PATH));
}
public static ScriptExecution runRepeatedly(ScriptFile scriptFile, int loopTimes, long delay, long interval) {
ScriptSource source = scriptFile.toSource();
String directoryPath = scriptFile.getParent();
return AutoJs.getInstance().getScriptEngineService().execute(source, new ExecutionConfig()
.requirePath(directoryPath, StorageScriptProvider.DEFAULT_DIRECTORY_PATH)
.path(directoryPath, StorageScriptProvider.DEFAULT_DIRECTORY_PATH)
.loop(delay, loopTimes, interval));
}
}