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

@@ -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);
}
}