api(app, globals): app.autojs, app.versionCode, app.versionName, requiresApi, requiresAutojsVersion

This commit is contained in:
hyb1996
2018-04-02 14:30:03 +08:00
parent 737f1a901f
commit b381f28470
7 changed files with 81 additions and 55 deletions

View File

@@ -1,8 +1,8 @@
module.exports = function(__runtime__, scope){
module.exports = function(runtime, global){
importClass(android.content.Intent);
var app = Object.create(__runtime__.app);
var context = scope.context;
var app = Object.create(runtime.app);
var context = global.context;
app.intent = function(i) {
var intent = new android.content.Intent();
@@ -43,8 +43,8 @@ module.exports = function(__runtime__, scope){
app.startActivity = function(i){
if(typeof(i) == "string"){
if(__runtime__.getProperty("class." + i)){
context.startActivity(new Intent(context, __runtime__.getProperty("class." + i))
if(runtime.getProperty("class." + i)){
context.startActivity(new Intent(context, runtime.getProperty("class." + i))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
return;
}else{
@@ -91,7 +91,15 @@ module.exports = function(__runtime__, scope){
app.launch = app.launchPackage;
scope.__asGlobal__(app, ['launchPackage', 'launch', 'launchApp', 'getPackageName', 'getAppName', 'openAppSetting']);
app.versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
app.versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
app.autojs = {
versionCode: com.stardust.scriptdroid.BuildConfig.VERSION_CODE,
versionName: com.stardust.scriptdroid.BuildConfig.VERSION_NAME
};
global.__asGlobal__(app, ['launchPackage', 'launch', 'launchApp', 'getPackageName', 'getAppName', 'openAppSetting']);
return app;
}

View File

@@ -69,4 +69,65 @@ module.exports = function(runtime, global){
global.setScreenMetrics = runtime.setScreenMetrics.bind(runtime);
global.requiresApi = runtime.requiresApi.bind(runtime);
global.requiresAutojsVersion = function(version){
if(typeof(version) == 'number'){
if(compare(version, app.autojs.versionCode) > 0){
throw new Error("需要Auto.js版本号" + version + "以上才能运行");
}
}else{
if(compareVersion(version, app.autojs.versionName) > 0){
throw new Error("需要Auto.js版本" + version + "以上才能运行");
}
}
}
var buildTypes = {
release: 100,
beta: 50,
alpha: 0
}
function compareVersion(v1, v2){
v1 = parseVersion(v1);
v2 = parseVersion(v2);
log(v1, v2);
return v1.major != v2.major ? compare(v1.major, v2.major) :
v1.minor != v2.minor ? compare(v1.minor, v2.minor) :
v1.revision != v2.revision ? compare(v1.revision, v2.revision) :
v1.buildType != v2.buildType ? compare(v1.buildType, v2.buildType) :
compare(v1.build, v2.build);
}
function compare(a, b){
return a > b ? 1 :
a < b ? -1:
0;
}
function parseVersion(v){
var m = /(\d+)\.(\d+)\.(\d+)[ ]?(Alpha|Beta)?(\d*)/.exec(v);
if(!m){
throw new Error("版本格式不合法: " + v);
}
return {
major: parseInt(m[1]),
minor: parseInt(m[2]),
revision: parseInt(m[3]),
buildType: buildType(m[4]),
build: m[5] ? parseInt(m[5]) : 1
};
}
function buildType(str){
if(str == 'Alpha'){
return buildTypes.alpha;
}
if(str == 'Beta'){
return buildTypes.beta;
}
return buildTypes.release;
}
}