Skip to content

Commit

Permalink
Merge pull request #126 from lx32056127/v4
Browse files Browse the repository at this point in the history
refactor: 资产管理页面添加记录时无正则模式
refactor: App识别&通知识别未安装对于APP时不再显示该App规则
refactor: 规则运行超过2秒将提示错误
deps: rhino依赖改为gradle管理
  • Loading branch information
dreamncn authored Jun 10, 2022
2 parents 4479ab8 + 149e031 commit 8d348f6
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ dependencies {
//内存泄漏监测leak,就需要加上这个依赖
// debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
//rhino
implementation project(path: ':rhino-1.7.13')
// implementation project(path: ':rhino-1.7.13')
implementation 'org.mozilla:rhino:1.7.13'
//多状态组件
implementation 'com.github.SheHuan:StatusView:1.0.5'
//material-dialogs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cn.dreamn.qianji_auto.data.data;

import android.annotation.SuppressLint;

import com.alibaba.fastjson.JSONObject;
import com.hjq.toast.ToastUtils;

import org.apache.commons.lang3.time.StopWatch;

import java.util.Arrays;

Expand Down Expand Up @@ -79,6 +84,7 @@ private void setType(String type) {

}

@SuppressLint("DefaultLocale")
public void run(BillInfo billInfo, String addJs, TaskThread.TaskResult taskResult) {
//执行分类
TaskThread.onThread(() -> {
Expand Down Expand Up @@ -108,8 +114,19 @@ public void run(BillInfo billInfo, String addJs, TaskThread.TaskResult taskResul
String testJs = String.format(js, jsInner, dataList.toString(), billInfo.getShopAccount(), billInfo.getShopRemark(), BillInfo.getTypeName(billInfo.getType()), hour, minute, billInfo.getMoney());

try {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
String result = JsEngine.run(testJs);
Log.i("自动分类结果:" + result);
stopWatch.split();
long elapsedTime = stopWatch.getSplitTime();
stopWatch.stop();
Log.i(String.format("自动分类结果:%s.耗时: %dms", result, elapsedTime));
if (elapsedTime >= 2000) {
Log.i("自动分类执行时间超过2秒,请检查规则.");
ToastUtils.show("自动分类执行时间超过2秒,请检查规则.");
// 添加超时 为了充分展示Toast
Thread.sleep(3000);
}
if (result.contains("Undefined")) {
taskResult.onEnd("NotFound");
} else taskResult.onEnd(result);
Expand All @@ -122,6 +139,7 @@ public void run(BillInfo billInfo, String addJs, TaskThread.TaskResult taskResul
});
}

@SuppressLint("DefaultLocale")
public void run(String app, String data, String addJs, TaskThread.TaskResult taskResult) {
data = data.replace("'", "\\'");
String finalData = data;
Expand Down Expand Up @@ -247,7 +265,19 @@ function retData(array, ret) {
//获得所有Js
String result = "";
try {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
result = JsEngine.run(testJs);
stopWatch.split();
long elapsedTime = stopWatch.getSplitTime();
stopWatch.stop();
Log.i(String.format("%s 解析结果:%s.耗时: %dms", app, result, elapsedTime));
if (elapsedTime >= 2000) {
Log.i(app + " App解析执行时间超过2秒,请检查规则.");
ToastUtils.show(app + " App解析执行时间超过2秒,请检查规则.");
// 添加超时 为了充分展示Toast
Thread.sleep(3000);
}
} catch (Throwable ex) {
Log.i("错误:" + ex.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static boolean findPackages(Set<String> packages, String targets) {
/*
* check the app is installed
*/
private static boolean isAppInstalled(Context context, String packagename) {
public static boolean isAppInstalled(Context context, String packagename) {
PackageInfo packageInfo;
try {
packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ protected void initListeners() {
refreshlayout.finishRefresh(0);
});
floatingActionButton.setOnClickListener(v -> {
BottomArea.input(getContext(), getString(R.string.assert_input), "", getString(R.string.set_sure), getString(R.string.set_cancle), new BottomArea.InputCallback() {
BottomArea.inputWithCheckBox(getContext(), getString(R.string.assert_input), "", getString(R.string.set_sure), getString(R.string.set_cancle), "正则模式", false, new BottomArea.InputWithCheckBoxCallback() {
@Override
public void input(String data) {
public void inputWithCheck(String data, boolean checked) {
Assets.showAssetSelect(getContext(), getString(R.string.assert_choose), false, obj -> {
Asset asset2s = (Asset) obj;
TaskThread.onThread(() -> {
Db.db.AssetMapDao().add(data, asset2s.name);
Db.db.AssetMapDao().add(checked ? "reg:" + data : data, asset2s.name);
HandlerUtil.send(mHandler, getString(R.string.add_success), HANDLE_REFRESH);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void handleMessage(@NonNull Message msg) {
return;
}
RegularManager.restoreFromData(getContext(), getName(), getLastType(), (String) msg.obj, code -> {
HandlerUtil.send(mHandler, HANDLE_REFRESH);
HandlerUtil.send(mHandler, HANDLE_REFRESH);
});
}
};
Expand Down Expand Up @@ -520,9 +520,8 @@ public void sure() {
cAdapter.setOnMoreClick(item -> {
BottomArea.msg(getContext(), item.getString("name"), item.getString("remark"));
});


} else {//不是数据部分
} else {
// 不是数据部分
rAdapter = new RemoteListAdapter(getContext(), isWeb);
recyclerView.setAdapter(rAdapter);
rAdapter.setOnItemClickListener((itemView, position) -> {
Expand Down Expand Up @@ -593,6 +592,10 @@ public void onSuccessful(String data) {
if (type.equals("app") || type.equals("notice") || type.equals("sms") || type.equals("helper")) {
for (Map.Entry<String, Object> stringObjectEntry : jsonObject1.entrySet()) {
String key = stringObjectEntry.getKey();
// 未安装app不再显示远程规则
if (!AppInfo.isAppInstalled(getContext(), key) && (type.equals("app") || type.equals("notice"))) {
continue;
}
JSONObject value = (JSONObject) stringObjectEntry.getValue();
Bundle bundle = new Bundle();
bundle.putString("identify", getLastType());
Expand Down Expand Up @@ -658,11 +661,20 @@ public void onSuccessful(String data) {
List<Bundle> bundleList = new ArrayList<>();
int index = 0;
for (Regular regular : regulars) {
// 未安装app不再显示本地规则
if (!AppInfo.isAppInstalled(getContext(), regular.app) && (type.equals("app") || type.equals("notice"))) {
continue;
}
Bundle bundle = Tool.class2Bundle(regular);
Db.db.RegularDao().setSort(regular.id, ++index);
bundle.putInt("sort", ++index);
bundleList.add(bundle);
}
if (bundleList.size() == 0) {
Log.d("已安装App数量为0");
HandlerUtil.send(mHandler, HANDLE_ERR);
return;
}
list = bundleList;
HandlerUtil.send(mHandler, HANDLE_OK);
});
Expand Down
2 changes: 0 additions & 2 deletions rhino-1.7.13/build.gradle

This file was deleted.

Binary file removed rhino-1.7.13/rhino-1.7.13.jar
Binary file not shown.

0 comments on commit 8d348f6

Please sign in to comment.