From 1581663e7ae6f97c1369cbac58a9b63e97041d17 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 10 Aug 2024 17:46:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/common/JSONUtils.java | 36 +++++++++ .../java/com/xypower/mpmaster/AppMaster.java | 74 ++++++------------- 2 files changed, 57 insertions(+), 53 deletions(-) diff --git a/common/src/main/java/com/xypower/common/JSONUtils.java b/common/src/main/java/com/xypower/common/JSONUtils.java index 3179e68e..ec71b146 100644 --- a/common/src/main/java/com/xypower/common/JSONUtils.java +++ b/common/src/main/java/com/xypower/common/JSONUtils.java @@ -167,6 +167,42 @@ public class JSONUtils { return false; } + public static boolean updateConfigFile(String path, String fileName, String name, int fieldType, Object val) { + + if (name == null) { + return false; + } + + File configFile = new File(Environment.getExternalStorageDirectory(), path); + if (!configFile.exists()) { + if (val == null) { + // Should delete the config field + return true; + } + + configFile.mkdirs(); + } + + configFile = new File(configFile, fileName); + if (!configFile.exists() && val == null) { + return true; + } + + JSONObject jsonObject = JSONUtils.loadJson(configFile.getAbsolutePath()); + if (jsonObject == null) { + if (val == null) { + return true; + } + jsonObject = new JSONObject(); + } + + if (!JSONUtils.updateJsonProperty(jsonObject, name, fieldType, val)) { + return false; + } + + return JSONUtils.saveJson(configFile.getAbsolutePath(), jsonObject); + } + public static boolean updateJsonProperty(JSONObject jsonObject, String name, int fieldType, Object val) { if (name == null) { return false; diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index ff4b4da4..776e4f29 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -569,32 +569,7 @@ public class AppMaster { mService.updateMntn(newUrl); } } else if (TextUtils.equals(cmd, CMD_UPDATE_CONFIG)) { - JSONArray jsonConfigs = null; - try { - jsonConfigs = jsonObject.getJSONArray("configs"); - String path = jsonObject.optString("path", null); - String fileName = jsonObject.optString("fileName", null); - - mService.logger.warning("Recv Update Config: " + path + " " + fileName); - - if (jsonConfigs != null) { - - for (int idx = 0; idx < jsonConfigs.length(); idx++) { - JSONObject jsonConfig = jsonConfigs.getJSONObject(idx); - String configName = jsonConfig.optString("name", null); - int fieldType = jsonConfig.optInt("type", 0); - Object val = jsonConfig.opt("value"); - - updateConfig(path, fileName, configName, fieldType, val); - } - - String packageName = jsonObject.optString("packageName", null); - if (packageName != null) { - MicroPhotoContext.restartApp(mService.getApplicationContext(), packageName); - } - } - } catch (Exception ex) { - } + updateConfigs(jsonObject); } else if (TextUtils.equals(cmd, CMD_PUSH_FILE)) { String path = jsonObject.optString("path", null); @@ -837,40 +812,33 @@ public class AppMaster { } } - private boolean updateConfig(String path, String fileName, String name, int fieldType, Object val) { + private void updateConfigs(JSONObject jsonObject) { + JSONArray jsonConfigs = null; + try { + jsonConfigs = jsonObject.getJSONArray("configs"); + String path = jsonObject.optString("path", null); + String fileName = jsonObject.optString("fileName", null); - if (name == null) { - return false; - } + mService.logger.warning("Recv Update Config: " + path + " " + fileName); - File configFile = new File(Environment.getExternalStorageDirectory(), path); - if (!configFile.exists()) { - if (val == null) { - // Should delete the config field - return true; - } + if (jsonConfigs != null) { - configFile.mkdirs(); - } + for (int idx = 0; idx < jsonConfigs.length(); idx++) { + JSONObject jsonConfig = jsonConfigs.getJSONObject(idx); + String configName = jsonConfig.optString("name", null); + int fieldType = jsonConfig.optInt("type", 0); + Object val = jsonConfig.opt("value"); - configFile = new File(configFile, fileName); - if (!configFile.exists() && val == null) { - return true; - } + JSONUtils.updateConfigFile(path, fileName, configName, fieldType, val); + } - JSONObject jsonObject = JSONUtils.loadJson(configFile.getAbsolutePath()); - if (jsonObject == null) { - if (val == null) { - return true; + String packageName = jsonObject.optString("packageName", null); + if (packageName != null) { + MicroPhotoContext.restartApp(mService.getApplicationContext(), packageName); + } } - jsonObject = new JSONObject(); - } - - if (!JSONUtils.updateJsonProperty(jsonObject, name, fieldType, val)) { - return false; + } catch (Exception ex) { } - - return JSONUtils.saveJson(configFile.getAbsolutePath(), jsonObject); } private String buildMntnServer(String server, int port) {