基础设置,动态更新本地常量数据比如访问路径等
This commit is contained in:
parent
60f77c0b66
commit
e55b71ef4b
|
|
@ -23,6 +23,8 @@ import android.content.Context;
|
|||
import androidx.multidex.BuildConfig;
|
||||
import androidx.multidex.MultiDex;
|
||||
|
||||
import com.bonus.canteen.db.AppDatabase;
|
||||
import com.bonus.canteen.db.ConfigRepository;
|
||||
import com.bonus.canteen.utils.sdkinit.ANRWatchDogInit;
|
||||
import com.bonus.canteen.utils.sdkinit.UMengInit;
|
||||
import com.bonus.canteen.utils.sdkinit.XBasicLibInit;
|
||||
|
|
@ -32,6 +34,8 @@ import com.bonus.canteen.utils.sdkinit.XBasicLibInit;
|
|||
* @since 2018/11/7 下午1:12
|
||||
*/
|
||||
public class SmartCanteenApp extends Application {
|
||||
private static ConfigRepository configRepository;
|
||||
private static SmartCanteenApp instance;
|
||||
|
||||
@Override
|
||||
protected void attachBaseContext(Context base) {
|
||||
|
|
@ -43,9 +47,26 @@ public class SmartCanteenApp extends Application {
|
|||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
instance = this;
|
||||
initLibs();
|
||||
initConfigRepository();
|
||||
}
|
||||
/**
|
||||
* 初始化配置仓库
|
||||
*/
|
||||
private void initConfigRepository() {
|
||||
configRepository = new ConfigRepository(AppDatabase.getDatabase(this).parameterInfoDao());
|
||||
}
|
||||
/**
|
||||
* 获取全局 Application 实例
|
||||
*/
|
||||
public static Context getAppContext() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static ConfigRepository getConfigRepository() {
|
||||
return configRepository;
|
||||
}
|
||||
/**
|
||||
* 初始化基础库
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.bonus.canteen.activity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.ActivityManager;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
|
|
@ -1015,11 +1016,23 @@ public class OperationActivity extends BaseActivity<ActivityCanteenOperationBind
|
|||
"是否确认重启APP?",
|
||||
getString(R.string.lab_yes),
|
||||
(dialog, which) -> {
|
||||
// 获取应用的包名
|
||||
String packageName = getBaseContext().getPackageName();
|
||||
// 获取ActivityManager服务
|
||||
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
// 终止所有后台进程
|
||||
if (activityManager != null) {
|
||||
activityManager.killBackgroundProcesses(packageName);
|
||||
}
|
||||
// 重启应用程序
|
||||
Intent intent = getBaseContext().getPackageManager()
|
||||
.getLaunchIntentForPackage(getBaseContext().getPackageName());
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
startActivity(intent);
|
||||
.getLaunchIntentForPackage(packageName);
|
||||
if (intent != null) {
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
startActivity(intent);
|
||||
}
|
||||
// 终止当前进程
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
System.exit(0);
|
||||
dialog.dismiss();
|
||||
},
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package com.bonus.canteen.activity.setting;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
|
|
@ -25,8 +27,10 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.bonus.canteen.SmartCanteenApp;
|
||||
import com.bonus.canteen.core.BaseActivity;
|
||||
import com.bonus.canteen.db.AppDatabase;
|
||||
import com.bonus.canteen.db.ConfigRepository;
|
||||
import com.bonus.canteen.db.entity.base.ParamSettingInfo;
|
||||
import com.bonus.canteen.utils.ThreadPoolManager;
|
||||
import com.bonus.canteen.utils.WorkConfig;
|
||||
|
|
@ -88,15 +92,30 @@ public class ParamSettingsActivity extends BaseActivity<ActivityCanteenParamSett
|
|||
getString(R.string.lab_yes),
|
||||
(dialog, which) -> {
|
||||
//保存数据库
|
||||
ConfigRepository configRepository = SmartCanteenApp.getConfigRepository();
|
||||
configRepository.reloadCache(paramSettingInfo);
|
||||
ThreadPoolManager.getExecutor().execute(() -> {
|
||||
AppDatabase.getDatabase(this).parameterInfoDao().update(paramSettingInfo);
|
||||
runOnUiThread(() -> {
|
||||
// 获取应用的包名
|
||||
String packageName = getBaseContext().getPackageName();
|
||||
// 获取ActivityManager服务
|
||||
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
// 终止所有后台进程
|
||||
if (activityManager != null) {
|
||||
activityManager.killBackgroundProcesses(packageName);
|
||||
}
|
||||
// 重启应用程序
|
||||
Intent intent = getBaseContext().getPackageManager()
|
||||
.getLaunchIntentForPackage(getBaseContext().getPackageName());
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
startActivity(intent);
|
||||
.getLaunchIntentForPackage(packageName);
|
||||
if (intent != null) {
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
startActivity(intent);
|
||||
}
|
||||
// 终止当前进程
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
System.exit(0);
|
||||
|
||||
dialog.dismiss();
|
||||
});
|
||||
});
|
||||
|
|
@ -121,6 +140,7 @@ public class ParamSettingsActivity extends BaseActivity<ActivityCanteenParamSett
|
|||
paramSettingInfo.setAppKey(WorkConfig.getAppKey());
|
||||
paramSettingInfo.setFacePassRate(WorkConfig.getFacePassRate());
|
||||
paramSettingInfo.setPhotoPrefixes(WorkConfig.getFileUrl());
|
||||
paramSettingInfo.setVersion("1");
|
||||
AppDatabase.getDatabase(this).parameterInfoDao().insert(paramSettingInfo);
|
||||
}
|
||||
paramSettingInfo = AppDatabase.getDatabase(this).parameterInfoDao().getOneInfo();
|
||||
|
|
@ -136,12 +156,12 @@ public class ParamSettingsActivity extends BaseActivity<ActivityCanteenParamSett
|
|||
}
|
||||
|
||||
private void initListeners() {
|
||||
binding.ipAddress.setOnClickListener(v -> openDialog("IP地址", "ipAddress",paramSettingInfo.getIpAddress(), 1));
|
||||
binding.ipAddress.setOnClickListener(v -> openDialog("IP地址", "ipAddress", paramSettingInfo.getIpAddress(), 1));
|
||||
binding.mqttAddress.setOnClickListener(v -> openDialog("MQTT地址", "mqttAddress", paramSettingInfo.getMqttAddress(), 1));
|
||||
binding.mqttUserName.setOnClickListener(v -> openDialog("MQTT用户名", "mqttUserName",paramSettingInfo.getMqttUserName(), 1));
|
||||
binding.mqttUserName.setOnClickListener(v -> openDialog("MQTT用户名", "mqttUserName", paramSettingInfo.getMqttUserName(), 1));
|
||||
binding.mqttPassWord.setOnClickListener(v -> openDialog("MQTT密码", "mqttPassWord", paramSettingInfo.getMqttPassword(), 1));
|
||||
binding.appId.setOnClickListener(v -> openDialog("AppId", "appId",paramSettingInfo.getAppId(), 1));
|
||||
binding.appKey.setOnClickListener(v -> openDialog("AppKey", "appKey",paramSettingInfo.getAppKey(), 1));
|
||||
binding.appId.setOnClickListener(v -> openDialog("AppId", "appId", paramSettingInfo.getAppId(), 1));
|
||||
binding.appKey.setOnClickListener(v -> openDialog("AppKey", "appKey", paramSettingInfo.getAppKey(), 1));
|
||||
binding.facePassRate.setOnClickListener(v -> openDialog("人脸识别通过率\n提示:请输入 0.8 - 1 之间的数字", "facePassRate", paramSettingInfo.getFacePassRate(), 2));
|
||||
binding.photoPrefix.setOnClickListener(v -> openDialog("图片前缀", "photoPrefix", paramSettingInfo.getPhotoPrefixes(), 1));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (C) 2025 xuexiangjys(xuexiangjys@163.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.bonus.canteen.db;
|
||||
|
||||
import com.bonus.canteen.db.dao.base.ParamSettingInfoDao;
|
||||
import com.bonus.canteen.db.entity.base.ParamSettingInfo;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ConfigRepository {
|
||||
private final ParamSettingInfoDao paramSettingDao;
|
||||
private final Map<String, String> configCache = new HashMap<>();
|
||||
|
||||
public ConfigRepository(ParamSettingInfoDao paramSettingDao) {
|
||||
this.paramSettingDao = paramSettingDao;
|
||||
// 异步加载配置到缓存
|
||||
new Thread(() -> {
|
||||
ParamSettingInfo paramSettingInfo = paramSettingDao.getOneInfo();
|
||||
if (paramSettingInfo != null) {
|
||||
configCache.put("baseUrl", paramSettingInfo.getIpAddress() + "/smart-canteen");
|
||||
configCache.put("prefixesUrl", paramSettingInfo.getIpAddress());
|
||||
configCache.put("serverUri", paramSettingInfo.getMqttAddress());
|
||||
configCache.put("fileUrl", paramSettingInfo.getPhotoPrefixes());
|
||||
configCache.put("APP_ID", paramSettingInfo.getAppId());
|
||||
configCache.put("APP_KEY", paramSettingInfo.getAppKey());
|
||||
configCache.put("MqttUserName", paramSettingInfo.getMqttUserName());
|
||||
configCache.put("MqttPassWord", paramSettingInfo.getMqttPassword());
|
||||
configCache.put("FACE_PASS_RATE", paramSettingInfo.getFacePassRate());
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void reloadCache(ParamSettingInfo paramSettingInfo) {
|
||||
configCache.clear();
|
||||
configCache.put("baseUrl", paramSettingInfo.getIpAddress() + "/smart-canteen");
|
||||
configCache.put("prefixesUrl", paramSettingInfo.getIpAddress());
|
||||
configCache.put("serverUri", paramSettingInfo.getMqttAddress());
|
||||
configCache.put("fileUrl", paramSettingInfo.getPhotoPrefixes());
|
||||
configCache.put("APP_ID", paramSettingInfo.getAppId());
|
||||
configCache.put("APP_KEY", paramSettingInfo.getAppKey());
|
||||
configCache.put("MqttUserName", paramSettingInfo.getMqttUserName());
|
||||
configCache.put("MqttPassWord", paramSettingInfo.getMqttPassword());
|
||||
configCache.put("FACE_PASS_RATE", paramSettingInfo.getFacePassRate());
|
||||
}
|
||||
|
||||
public String getValue(String key) {
|
||||
return configCache.get(key);
|
||||
}
|
||||
|
||||
public void updateConfig() {
|
||||
configCache.clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -17,12 +17,8 @@
|
|||
|
||||
package com.bonus.canteen.utils;
|
||||
|
||||
import static com.ccb.facedevice.sdk.utils.AppUtil.getApplication;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.bonus.canteen.db.AppDatabase;
|
||||
import com.bonus.canteen.db.entity.base.ParamSettingInfo;
|
||||
import com.bonus.canteen.SmartCanteenApp;
|
||||
import com.bonus.canteen.db.ConfigRepository;
|
||||
|
||||
public class WorkConfig {
|
||||
//本地
|
||||
|
|
@ -30,9 +26,7 @@ public class WorkConfig {
|
|||
// protected static String prefixesUrl = "http://192.168.0.34:48380";
|
||||
protected static String baseUrl = "http://36.33.26.201:48380/smart-canteen";
|
||||
protected static String prefixesUrl = "http://36.33.26.201:48380";
|
||||
|
||||
protected static String fileUrl = "http://36.33.26.201:48380/lnyst/";
|
||||
|
||||
private static String updateUrl = "https://www.baidu.com";
|
||||
private static String serverUri = "tcp://36.33.26.201:51883";
|
||||
private static String MqttUserName = "admin";
|
||||
|
|
@ -41,57 +35,84 @@ public class WorkConfig {
|
|||
private static String APP_KEY = "9YFPa6eiuNQAFnzJUadn4LaR8w1bcw3a5ZWYZB6FB57Y";
|
||||
private static String FACE_PASS_RATE = "0.8";
|
||||
|
||||
static {
|
||||
ThreadPoolManager.getExecutor().execute(()->{
|
||||
ParamSettingInfo paramSettingInfo = AppDatabase.getDatabase(getApplication()).parameterInfoDao().getOneInfo();
|
||||
if (paramSettingInfo != null){
|
||||
baseUrl = paramSettingInfo.getIpAddress() + "/smart-canteen";
|
||||
prefixesUrl = paramSettingInfo.getIpAddress();
|
||||
serverUri = paramSettingInfo.getMqttAddress();
|
||||
fileUrl = paramSettingInfo.getPhotoPrefixes();
|
||||
APP_ID = paramSettingInfo.getAppId();
|
||||
APP_KEY = paramSettingInfo.getAppKey();
|
||||
MqttUserName = paramSettingInfo.getMqttUserName();
|
||||
MqttPassWord = paramSettingInfo.getMqttPassword();
|
||||
FACE_PASS_RATE = paramSettingInfo.getFacePassRate();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static String getBaseUrl() {
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("baseUrl") != null) {
|
||||
return repo.getValue("baseUrl");
|
||||
}
|
||||
return baseUrl;
|
||||
}
|
||||
public static String getPrefixesUrl() {
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("prefixesUrl") != null) {
|
||||
return repo.getValue("prefixesUrl");
|
||||
}
|
||||
return prefixesUrl;
|
||||
}
|
||||
public static String getUpdateUrl() {
|
||||
return updateUrl;
|
||||
}
|
||||
public static String getMqttUrl() {
|
||||
Log.d("mqttUrl",serverUri);
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("serverUri") != null) {
|
||||
return repo.getValue("serverUri");
|
||||
}
|
||||
return serverUri;
|
||||
}
|
||||
public static String getFileUrl() {
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("fileUrl") != null) {
|
||||
return repo.getValue("fileUrl");
|
||||
}
|
||||
return fileUrl;
|
||||
}
|
||||
|
||||
public static String getMqttUserName() {
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("MqttUserName") != null) {
|
||||
return repo.getValue("MqttUserName");
|
||||
}
|
||||
return MqttUserName;
|
||||
}
|
||||
|
||||
public static String getMqttPassWord() {
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("MqttPassWord") != null) {
|
||||
return repo.getValue("MqttPassWord");
|
||||
}
|
||||
return MqttPassWord;
|
||||
}
|
||||
|
||||
public static String getAppId() {
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("APP_ID") != null) {
|
||||
return repo.getValue("APP_ID");
|
||||
}
|
||||
return APP_ID;
|
||||
}
|
||||
|
||||
public static String getAppKey() {
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("APP_KEY") != null) {
|
||||
return repo.getValue("APP_KEY");
|
||||
}
|
||||
return APP_KEY;
|
||||
}
|
||||
|
||||
public static String getFacePassRate() {
|
||||
ConfigRepository repo = SmartCanteenApp.getConfigRepository();
|
||||
//判断是否存在某个配置
|
||||
if (repo != null && repo.getValue("FACE_PASS_RATE") != null) {
|
||||
return repo.getValue("FACE_PASS_RATE");
|
||||
}
|
||||
return FACE_PASS_RATE;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue