优化代码

master
Matthew 10 months ago
parent a209ed89b0
commit 1581663e7a

@ -167,6 +167,42 @@ public class JSONUtils {
return false; 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) { public static boolean updateJsonProperty(JSONObject jsonObject, String name, int fieldType, Object val) {
if (name == null) { if (name == null) {
return false; return false;

@ -569,32 +569,7 @@ public class AppMaster {
mService.updateMntn(newUrl); mService.updateMntn(newUrl);
} }
} else if (TextUtils.equals(cmd, CMD_UPDATE_CONFIG)) { } else if (TextUtils.equals(cmd, CMD_UPDATE_CONFIG)) {
JSONArray jsonConfigs = null; updateConfigs(jsonObject);
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) {
}
} else if (TextUtils.equals(cmd, CMD_PUSH_FILE)) { } else if (TextUtils.equals(cmd, CMD_PUSH_FILE)) {
String path = jsonObject.optString("path", null); 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) { mService.logger.warning("Recv Update Config: " + path + " " + fileName);
return false;
}
File configFile = new File(Environment.getExternalStorageDirectory(), path); if (jsonConfigs != null) {
if (!configFile.exists()) {
if (val == null) {
// Should delete the config field
return true;
}
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); JSONUtils.updateConfigFile(path, fileName, configName, fieldType, val);
if (!configFile.exists() && val == null) { }
return true;
}
JSONObject jsonObject = JSONUtils.loadJson(configFile.getAbsolutePath()); String packageName = jsonObject.optString("packageName", null);
if (jsonObject == null) { if (packageName != null) {
if (val == null) { MicroPhotoContext.restartApp(mService.getApplicationContext(), packageName);
return true; }
} }
jsonObject = new JSONObject(); } catch (Exception ex) {
}
if (!JSONUtils.updateJsonProperty(jsonObject, name, fieldType, val)) {
return false;
} }
return JSONUtils.saveJson(configFile.getAbsolutePath(), jsonObject);
} }
private String buildMntnServer(String server, int port) { private String buildMntnServer(String server, int port) {

Loading…
Cancel
Save