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

@ -10,6 +10,7 @@ import android.view.MenuItem;
import com.stardust.app.OnActivityResultDelegate;
import com.stardust.pio.PFiles;
import com.stardust.scriptdroid.BuildConfig;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.storage.file.TmpScriptFiles;
import com.stardust.scriptdroid.ui.BaseActivity;

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

View File

@ -63,7 +63,6 @@ public class TemplateMatching {
if (!isFirstMatching && !shouldContinueMatching(level, maxLevel)) {
break;
}
// FIXME: 2018/3/31 此处的matchResult.release()某些情况下会导致currentTemplate被释放
OpenCVHelper.release(matchResult);
matchResult = matchTemplate(src, currentTemplate, matchMethod);
Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold);
@ -171,9 +170,7 @@ public class TemplateMatching {
public static Mat matchTemplate(Mat img, Mat temp, int match_method) {
int result_cols = img.cols() - temp.cols() + 1;
int result_rows = img.rows() - temp.rows() + 1;
Log.d(LOG_TAG, String.format("matchTemplate: rows = %d, cols = %d", result_rows, result_cols));
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
Log.d(LOG_TAG, String.format("matchTemplate: img = %s, temp = %s, result = %s", img.toString(), temp.toString(), result.toString()));
Imgproc.matchTemplate(img, temp, result, match_method);
return result;
}

View File

@ -1,12 +1,14 @@
package com.stardust.autojs.runtime;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Looper;
import android.view.MotionEvent;
import android.view.View;
import com.stardust.app.GlobalAppContext;
import com.stardust.autojs.BuildConfig;
import com.stardust.autojs.R;
import com.stardust.autojs.ScriptEngineService;
import com.stardust.autojs.annotation.ScriptVariable;
@ -57,6 +59,8 @@ import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.microedition.khronos.opengles.GL;
/**
* Created by Stardust on 2017/1/27.

View File

@ -17,6 +17,7 @@
<string name="text_should_enable_key_observing">按键监听未启用,请在软件设置中开启</string>
<string name="no_write_settings_permissin">沒有修改系統设置权限</string>
<string name="exception_notification_service_disabled">通知服务未运行,请重新启用通知权限</string>
<string name="text_requires_app_version_to_run_the_script" formatted="true">本脚本需要Auto.js版本号%d以上才能运行</string>
</resources>

View File

@ -1,46 +0,0 @@
package com.stardust.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Stardust on 2017/4/4.
*/
public class VolumeChangeObserver extends BroadcastReceiver {
public interface OnVolumeChangeListener {
void onVolumeChange();
}
public static final String ACTION_VOLUME_CHANGE = "android.media.VOLUME_CHANGED_ACTION";
private long mLastChangeMillis;
private List<OnVolumeChangeListener> mOnVolumeChangeListenerList = new ArrayList<>();
public void addOnVolumeChangeListener(OnVolumeChangeListener listener) {
mOnVolumeChangeListenerList.add(listener);
}
public void removeOnVolumeChangeListener(OnVolumeChangeListener listener) {
mOnVolumeChangeListenerList.remove(listener);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_VOLUME_CHANGE)) {
if (System.currentTimeMillis() - mLastChangeMillis < 400) {
return;
}
mLastChangeMillis = System.currentTimeMillis();
for (OnVolumeChangeListener listener : mOnVolumeChangeListenerList) {
listener.onVolumeChange();
}
}
}
}