modularize init script

This commit is contained in:
hyb1996
2017-05-09 15:47:31 +08:00
parent 08654b8f90
commit 956f064737
28 changed files with 451 additions and 667 deletions

View File

@@ -13,296 +13,25 @@ if(__engine_name__ == "rhino"){
}
}
var toast = function(text){
__runtime__.toast(text);
}
var app = require("app")(__runtime__);
var launchPackage = function(package){
app.launchPackage(package);
}
var launch = app.launch.bind(app);
var launchApp = function(appName){
app.launchApp(appName);
}
var getPackageName = function(appName){
return app.getPackageName(appName);
}
var openAppSetting = function(packageName){
return app.openAppSetting(packageName);
}
var sleep = function(millis){
__runtime__.sleep(millis);
}
var isStopped = function(){
return __runtime__.isStopped();
}
var notStopped = function(){
return !isStopped();
}
var stop = function(){
__runtime__.stop();
}
var console = __runtime__.console;
var log = function(str){
console.log(str);
}
var print = log;
var err = function(e){
console.e(e);
}
var openConsole = function(){
console.show();
}
var clearConsole = function(){
console.clear();
}
var shell = function(cmd, root){
root = root ? 1 : 0;
return __runtime__.shell(cmd, root);
}
var currentPackage = function(){
return __runtime__.info.getLatestPackage();
}
var currentActivity = function(){
return __runtime__.info.getLatestActivity();
}
var __this__ = this;
var back = function(){
return __runtime__.automator.back();
}
var home = function(){
return __runtime__.automator.home();
}
var powerDialog = function(){
return __runtime__.automator.powerDialog();
}
var notifications = function(){
return __runtime__.automator.notifications();
}
var quickSettings = function(){
return __runtime__.automator.quickSettings();
}
var recents = function(){
return __runtime__.automator.recents();
}
var splitScreen = function(){
return __runtime__.automator.splitScreen();
}
function performAction(action, args){
if(args.length == 4){
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]));
}else {
return action(__runtime__.automator.text(args[0], -1));
var __asGlobal__ = function(obj, functions){
__runtime__.console.log(functions);
var len = functions.length;
for(var i = 0; i < len; i++) {
var funcName = functions[i];
__runtime__.console.log(funcName);
this[funcName] = obj[funcName].bind(obj);
}
}
var click = function(){
return performAction(function(target){
return __runtime__.automator.click(target);
}, arguments);
}
var longClick = function(a, b, c, d){
return performAction(function(target){
return __runtime__.automator.longClick(target);
}, arguments);
}
require("__general__")(__runtime__, this);
var scrollDown = function(a, b, c, d){
if(arguments.length == 0)
return __runtime__.automator.scrollMaxForward();
if(arguments.length == 1 && typeof a === 'number')
return __runtime__.automator.scrollForward(a);
return performAction(function(target){
return __runtime__.automator.scrollForward(target);
}, arguments);
}
var scrollUp = function(a, b, c, d){
if(arguments.length == 0)
return __runtime__.automator.scrollMaxBackward();
if(arguments.length == 1 && typeof a === 'number')
return __runtime__.automator.scrollBackward(a);
return performAction(function(target){
return __runtime__.automator.scrollBackward(target);
}, arguments);
}
var input = function(a, b){
if(arguments.length == 1){
return __runtime__.automator.setText(__runtime__.automator.editable(-1), a);
}else{
return __runtime__.automator.setText(__runtime__.automator.editable(a), b);
(function(scope){
var modules = ['app', 'automator', 'console', 'io', 'selector', 'shell', 'web'];
var len = modules.length;
for(var i = 0; i < len; i++) {
var m = modules[i];
scope[m] = require('__' + m + '__')(scope.__runtime__, scope);
}
}
var setClip = function(text){
__runtime__.setClip(text);
}
var SetScreenMetrics = function(w, h){
__runtime__.SetScreenMetrics(w, h);
}
var Tap = function(x, y){
__runtime__.shellExecAsync("input tap " + x + " " + y);
}
var Swipe = function(x1, y1, x2, y2, duration){
if(arguments.length == 5){
__runtime__.shellExecAsync("input swipe " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + duration);
}else{
__runtime__.shellExecAsync("input swipe " + x1 + " " + y1 + " " + x2 + " " + y2);
}
}
var Screencap = function(path){
__runtime__.shellExecAsync("screencap -p " + path);
}
var KeyCode = function(keyCode){
__runtime__.shellExecAsync("input keyevent " + keyCode);
}
var Home = function(){
return KeyCode(3);
}
var Back = function(){
return KeyCode(4);
}
var Power = function(){
return KeyCode(26);
}
var Up = function(){
return KeyCode(19);
}
var Down = function(){
return KeyCode(20);
}
var Left = function(){
return KeyCode(21);
}
var Right = function(){
return KeyCode(22);
}
var OK = function(){
return KeyCode(23);
}
var VolumeUp = function(){
return KeyCode(24);
}
var VolumeDown = function(){
return KeyCode(25);
}
var Menu = function(){
return KeyCode(1);
}
var Camera = function(){
return KeyCode(27);
}
var Text = function(text){
__runtime__.shellExecAsync("input text " + text);
}
var selector = function(){
return __runtime__.selector(__engine__);
}
var __selector__ = selector();
var __obj__ = new java.lang.Object();
for(var x in __selector__){
if(!__obj__[x] && !this[x]){
this[x] = (function(method) {
return function(){
var s = selector();
//这里不知道怎么写。尴尬。只能写成这样。
if(arguments.length == 0){
return s[method]();
}else if(arguments.length == 1){
return s[method](arguments[0]);
}else if(arguments.length == 2){
return s[method](arguments[0], arguments[1]);
}else if(arguments.length == 3){
return s[method](arguments[0], arguments[1], arguments[2]);
}else if(arguments.length == 4){
return s[method](arguments[0], arguments[1], arguments[2], arguments[3]);
}else{
return s[method].call(s, Array.prototype.slice.call(arguments));
}
};
})(x);
}
}
var open = function(path, mode, encoding, bufferSize){
if(arguments.length == 1){
return com.stardust.pio.PFile.open(path);
}else if(arguments.length == 2){
return com.stardust.pio.PFile.open(path, mode);
}else if(arguments.length == 3){
return com.stardust.pio.PFile.open(path, mode, encoding);
}else if(arguments.length == 4){
return com.stardust.pio.PFile.open(path, mode, encoding, bufferSize);
}
}
var newInjectableWebClient = function(){
return new com.stardust.autojs.runtime.api.InjectableWebClient(org.mozilla.javascript.Context.getCurrentContext(), __this__);
}
var newInjectableWebView = function(activity){
return new com.stardust.autojs.runtime.api.InjectableWebView(activity, org.mozilla.javascript.Context.getCurrentContext(), __this__);
}
})(this);

View File

@@ -1,6 +1,7 @@
module.exports = function(__runtime__){
module.exports = function(__runtime__, scope){
var app = new Object(__runtime__.app);
var context = scope.context;
app.intent = function(i) {
var intent = new android.content.Intent();
@@ -30,7 +31,7 @@ module.exports = function(__runtime__){
}
app.startActivity = function(i){
context.startActivity(app.intent(i));
context.startActivity(app.intent(i).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK));
}
app.sendBroadcast = function(i){
@@ -39,5 +40,8 @@ module.exports = function(__runtime__){
app.launch = app.launchPackage;
scope.__asGlobal__(app, ['launchPackage', 'launch', 'launchApp', 'getPackageName', 'openAppSetting']);
return app;
}

View File

@@ -0,0 +1,62 @@
module.exports = function(__runtime__, scope){
var automator = {};
function performAction(action, args){
if(args.length == 4){
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]));
}else {
return action(__runtime__.automator.text(args[0], -1));
}
}
automator.click = function(){
return performAction(function(target){
return __runtime__.automator.click(target);
}, arguments);
}
automator.longClick = function(a, b, c, d){
return performAction(function(target){
return __runtime__.automator.longClick(target);
}, arguments);
}
automator.scrollDown = function(a, b, c, d){
if(arguments.length == 0)
return __runtime__.automator.scrollMaxForward();
if(arguments.length == 1 && typeof a === 'number')
return __runtime__.automator.scrollForward(a);
return performAction(function(target){
return __runtime__.automator.scrollForward(target);
}, arguments);
}
automator.scrollUp = function(a, b, c, d){
if(arguments.length == 0)
return __runtime__.automator.scrollMaxBackward();
if(arguments.length == 1 && typeof a === 'number')
return __runtime__.automator.scrollBackward(a);
return performAction(function(target){
return __runtime__.automator.scrollBackward(target);
}, arguments);
}
automator.input = function(a, b){
if(arguments.length == 1){
return __runtime__.automator.setText(__runtime__.automator.editable(-1), a);
}else{
return __runtime__.automator.setText(__runtime__.automator.editable(a), b);
}
}
scope.__asGlobal__(__runtime__.automator, ['back', 'home', 'powerDialog', 'notifications', 'quickSettings', 'recents', 'splitScreen']);
scope.__asGlobal__(automator, ['click', 'longClick', 'scrollDown', 'scrollUp', 'input']);
return automator;
}

View File

@@ -0,0 +1,22 @@
module.exports = function(__runtime__, scope){
var console = new Object(__runtime__.console);
console.assert = function(value, message){
message = message || "";
console.assertTrue(value, message);
}
scope.print = console.log.bind(console);
scope.log = scope.print;
scope.err = console.error.bind(console);
scope.openConsole = console.show.bind(console);
scope.clearConsole = console.clear.bind(console);
return console;
}

View File

@@ -0,0 +1,38 @@
module.exports = function(__runtime__, scope){
scope.toast = function(text){
__runtime__.toast(text);
}
scope.sleep = function(millis){
__runtime__.sleep(millis);
}
scope.isStopped = function(){
return __runtime__.isStopped();
}
scope.notStopped = function(){
return !isStopped();
}
scope.stop = function(){
__runtime__.stop();
}
scope.setClip = function(text){
__runtime__.setClip(text);
}
scope.getClip = function(text){
return __runtime__.getClip();
}
scope.currentPackage = function(){
return __runtime__.info.getLatestPackage();
}
scope.currentActivity = function(){
return __runtime__.info.getLatestActivity();
}
}

View File

@@ -0,0 +1,15 @@
module.exports = function(__runtime__, scope){
scope.open = function(path, mode, encoding, bufferSize){
if(arguments.length == 1){
return com.stardust.pio.PFile.open(path);
}else if(arguments.length == 2){
return com.stardust.pio.PFile.open(path, mode);
}else if(arguments.length == 3){
return com.stardust.pio.PFile.open(path, mode, encoding);
}else if(arguments.length == 4){
return com.stardust.pio.PFile.open(path, mode, encoding, bufferSize);
}
};
}

View File

@@ -0,0 +1,36 @@
module.exports = function(__runtime__, scope){
var __selector__ = __runtime__.selector(scope.__engine__);
var __obj__ = new java.lang.Object();
for(var method in __selector__){
if(!__obj__[method] && !scope[method]){
scope[method] = (function(method) {
return function(){
var s = selector();
//这里不知道怎么写。尴尬。只能写成这样。
if(arguments.length == 0){
return s[method]();
}else if(arguments.length == 1){
return s[method](arguments[0]);
}else if(arguments.length == 2){
return s[method](arguments[0], arguments[1]);
}else if(arguments.length == 3){
return s[method](arguments[0], arguments[1], arguments[2]);
}else if(arguments.length == 4){
return s[method](arguments[0], arguments[1], arguments[2], arguments[3]);
}else{
return s[method].call(s, Array.prototype.slice.call(arguments));
}
};
})(method);
}
}
return function(){
return __runtime__.selector(scope.__engine__);
};
}

View File

@@ -0,0 +1,87 @@
module.exports = function(__runtime__, scope){
scope.SetScreenMetrics = function(w, h){
__runtime__.getRootShell().SetScreenMetrics(x, y);
}
scope.Tap = function(x, y){
__runtime__.getRootShell().Tap(x, y);
}
scope.Swipe = function(x1, y1, x2, y2, duration){
if(arguments.length == 5){
__runtime__.getRootShell().Swipe(x1, y1, x2, y2, duration);
}else{
__runtime__.getRootShell().Swipe(x1, y1, x2, y2);
}
}
scope.Screencap = function(path){
__runtime__.getRootShell().Screencap(path);
}
scope.KeyCode = function(keyCode){
__runtime__.getRootShell().KeyCode(keyCode);
}
scope.Home = function(){
return KeyCode(3);
}
scope.Back = function(){
return KeyCode(4);
}
scope.Power = function(){
return KeyCode(26);
}
scope.Up = function(){
return KeyCode(19);
}
scope.Down = function(){
return KeyCode(20);
}
scope.Left = function(){
return KeyCode(21);
}
scope.Right = function(){
return KeyCode(22);
}
scope.OK = function(){
return KeyCode(23);
}
scope.VolumeUp = function(){
return KeyCode(24);
}
scope.VolumeDown = function(){
return KeyCode(25);
}
scope.Menu = function(){
return KeyCode(1);
}
scope.Camera = function(){
return KeyCode(27);
}
scope.Text = function(text){
__runtime__.getRootShell().Text(text);
}
scope.Input = scope.Text;
return function(cmd, root){
root = root ? 1 : 0;
return __runtime__.shell(cmd, root);
};
}

View File

@@ -0,0 +1,9 @@
assert = console.assert.bind(console);
function testApp(){
print('正在测试模块app');
assert("com.tencent.mm" == app.getPackageName("微信"));
assert("com.tencent.mobileqq" == app.getPackageName("QQ"));
}

View File

@@ -0,0 +1,13 @@
module.exports = function(__runtime__, scope){
scope.newInjectableWebClient = function(){
return new com.stardust.autojs.runtime.api.InjectableWebClient(org.mozilla.javascript.Context.getCurrentContext(), scope);
}
scope.newInjectableWebView = function(activity){
return new com.stardust.autojs.runtime.api.InjectableWebView(scope.activity, org.mozilla.javascript.Context.getCurrentContext(), scope);
}
}

View File

@@ -1,4 +1,4 @@
var __requireOld__ = require;
var __require__ = require;
var __nodejs_modules__ = {
'websocket' : com.iwebpp.wspp.WebSocket,
'websocketserver': com.iwebpp.wspp.WebSocketServer,
@@ -42,5 +42,5 @@ var require = function(module){
};
}
return __requireOld__(module);
return __require__(module);
};

View File

@@ -86,7 +86,7 @@ public class RhinoJavaScriptEngineManager extends AbstractScriptEngineManager {
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(getContext(), list);
new RequireBuilder()
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(provider))
.setSandboxed(true)
.setSandboxed(false)
.createRequire(context, scope)
.install(scope);
}

View File

@@ -46,7 +46,7 @@ public abstract class AbstractScriptRuntime {
public abstract void setClip(final String text);
@ScriptInterface
public abstract void shellExecAsync(String cmd);
public abstract AbstractShell getRootShell();
@ScriptInterface
public abstract AbstractShell.Result shell(String cmd, int root);
@@ -66,9 +66,6 @@ public abstract class AbstractScriptRuntime {
@ScriptInterface
public abstract void stop();
@ScriptInterface
public abstract void SetScreenMetrics(int width, int height);
public abstract void ensureAccessibilityServiceEnabled();
public abstract void onStop();

View File

@@ -117,9 +117,10 @@ public class ScriptRuntime extends AbstractScriptRuntime {
});
}
public void shellExecAsync(String cmd) {
@Override
public AbstractShell getRootShell() {
ensureRootShell();
mRootShell.exec(cmd);
return mRootShell;
}
private void ensureRootShell() {
@@ -137,7 +138,6 @@ public class ScriptRuntime extends AbstractScriptRuntime {
}
public UiSelector selector(ScriptEngine engine) {
Intent intent;
return new UiSelector(mAccessibilityBridge);
}
@@ -163,12 +163,6 @@ public class ScriptRuntime extends AbstractScriptRuntime {
Thread.interrupted();
}
@Override
public void SetScreenMetrics(int width, int height) {
ensureRootShell();
mRootShell.SetScreenMetrics(width, height);
}
public void ensureAccessibilityServiceEnabled() {
mAccessibilityBridge.ensureServiceEnabled();
}

View File

@@ -45,7 +45,7 @@ public abstract class AbstractShell {
public AbstractShell(boolean root) {
mRoot = root;
init(root ? COMMAND_SU: COMMAND_SH);
init(root ? COMMAND_SU : COMMAND_SH);
}
public boolean isRoot() {
@@ -62,20 +62,20 @@ public abstract class AbstractShell {
mTouchDevice = touchDevice;
}
public void SendEvent(int type, int code, int value){
public void SendEvent(int type, int code, int value) {
SendEvent(mTouchDevice, type, code, value);
}
public void SendEvent(int device, int type, int code, int value){
public void SendEvent(int device, int type, int code, int value) {
exec(TextUtils.join("", new Object[]{"sendevent /dev/input/event", device, " ", type, " ", code, " ", value}));
}
public void SetScreenMetrics(int width, int height){
public void SetScreenMetrics(int width, int height) {
mScreenWidth = width;
mScreenHeight = height;
}
public void Touch(int x, int y){
public void Touch(int x, int y) {
TouchX(x);
TouchY(y);
}
@@ -85,8 +85,10 @@ public abstract class AbstractShell {
}
private int scaleX(int x) {
if (mScreenWidth == 0)
return x;
int screenWidth = ScreenMetrics.getScreenWidth();
if(screenWidth == mScreenWidth){
if (screenWidth == mScreenWidth) {
return x;
}
return x * screenWidth / mScreenWidth;
@@ -97,13 +99,26 @@ public abstract class AbstractShell {
}
private int scaleY(int y) {
if (mScreenHeight == 0)
return y;
int screenHeight = ScreenMetrics.getScreenHeight();
if(screenHeight == mScreenHeight){
if (screenHeight == mScreenHeight) {
return y;
}
return y * screenHeight / mScreenHeight;
}
public void Tap(int x, int y) {
exec("input tap " + scaleX(x) + " " + scaleY(y));
}
public void Swipe(int x1, int y1, int x2, int y2) {
exec(com.stardust.util.TextUtils.join(" ", "input", "tap", scaleX(x1), scaleY(y1), scaleX(x2), scaleY(y2)));
}
public void Swipe(int x1, int y1, int x2, int y2, int time) {
exec(com.stardust.util.TextUtils.join(" ", "input", "tap", scaleX(x1), scaleY(y1), scaleX(x2), scaleY(y2), time));
}
public void KeyCode(int keyCode) {
exec("input keyevent " + keyCode);
@@ -161,9 +176,17 @@ public abstract class AbstractShell {
KeyCode(27);
}
public void Text(String text) {
public void Input(String text) {
exec("input text " + text);
}
public void Screencap(String path) {
exec("screencap -p " + path);
}
public void Text(String text) {
Input(text);
}
public abstract void exitAndWaitFor();
}