运维新增 下载增量更新功能

lowmem
liuguijing 2 months ago
parent c0e0b3874f
commit e580737761

@ -1,5 +1,8 @@
package com.xypower.common; package com.xypower.common;
import android.content.Context;
import java.io.FileInputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/MD5Util.class */ /* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/MD5Util.class */
@ -29,4 +32,25 @@ public class MD5Util {
} }
return r.toString(); return r.toString();
} }
public static String getFileMd5(String filePath) {
try (FileInputStream fis = new FileInputStream(filePath)) {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[8192]; // 使用大缓冲区提升性能:ml-citation{ref="5,7" data="citationList"}
int len;
while ((len = fis.read(buffer)) != -1) {
md.update(buffer, 0, len);
}
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff)); // 处理字节转十六进制:ml-citation{ref="3,7" data="citationList"}
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
} }

@ -215,5 +215,5 @@ Java_com_xypower_mpmaster_MpMasterService_applyDiff(JNIEnv *env, jclass clazz, j
env->ReleaseStringUTFChars(patch_file, patchFile); env->ReleaseStringUTFChars(patch_file, patchFile);
env->ReleaseStringUTFChars(new_file, newFile); env->ReleaseStringUTFChars(new_file, newFile);
return result; return result == 0 ? JNI_TRUE : JNI_FALSE;
} }

@ -22,6 +22,7 @@ import com.xypower.common.MD5Util;
import com.xypower.common.MicroPhotoContext; import com.xypower.common.MicroPhotoContext;
import com.xypower.common.ThermalInfoUtil; import com.xypower.common.ThermalInfoUtil;
import com.xypower.common.ZipUtils; import com.xypower.common.ZipUtils;
import com.xypower.mpmaster.sms.StringUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -509,28 +510,26 @@ public class AppMaster {
if (TextUtils.isEmpty(content)) { if (TextUtils.isEmpty(content)) {
return; return;
} }
try { try {
mService.logger.info(content); mService.logger.info(content);
JSONObject jsonObject = new JSONObject(content); JSONObject jsonObject = new JSONObject(content);
int isUpgrade = jsonObject.optInt("isUpgrade", 0); int isUpgrade = jsonObject.optInt("isUpgrade", 0);
String oldMd5 = jsonObject.optString("oldMd5");
String newMd5 = jsonObject.optString("newMd5");
String url = jsonObject.optString("url", null); String url = jsonObject.optString("url", null);
long cid = jsonObject.optLong("cid", 0); long cid = jsonObject.optLong("cid", 0);
int mntnMode = jsonObject.optInt("yw", 0); int mntnMode = jsonObject.optInt("yw", 0);
int quickHbMode = jsonObject.optInt("kxt", 0); int quickHbMode = jsonObject.optInt("kxt", 0);
mService.setMntnMode(mntnMode != 0, quickHbMode != 0); mService.setMntnMode(mntnMode != 0, quickHbMode != 0);
if (isUpgrade == 1 && !TextUtils.isEmpty(url)) { if (isUpgrade == 1 && !TextUtils.isEmpty(url)) {
upgradeApp(cid, "upgrade", url); upgradeApp(cid, "upgrade", url);
} else if (isUpgrade == 2 && !TextUtils.isEmpty(url)) {
upgradeAppOta(cid,"upgrade",url,oldMd5,newMd5);
} }
processCmd(cid, jsonObject); processCmd(cid, jsonObject);
JSONArray cmdObjects = jsonObject.optJSONArray("cmds"); JSONArray cmdObjects = jsonObject.optJSONArray("cmds");
if (cmdObjects != null) { if (cmdObjects != null) {
int cnt = cmdObjects.length(); int cnt = cmdObjects.length();
for (int idx = 0; idx < cnt; idx++) { for (int idx = 0; idx < cnt; idx++) {
@ -541,11 +540,9 @@ public class AppMaster {
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void sendResult(long cid, int result, String action, String content) { private void sendResult(long cid, int result, String action, String content) {
@ -996,15 +993,12 @@ public class AppMaster {
} }
private void upgradeApp(long cid, String action, String url) { private void upgradeApp(long cid, String action, String url) {
mService.logger.warning("Recv Upgrade Cmd: url=" + url); mService.logger.warning("Recv Upgrade Cmd: url=" + url);
FileDownloader dl = new FileDownloader(); FileDownloader dl = new FileDownloader();
File path = new File(MicroPhotoContext.buildAppDir(mService.getApplicationContext()), "packages"); File path = new File(MicroPhotoContext.buildAppDir(mService.getApplicationContext()), "packages");
if (!path.exists()) { if (!path.exists()) {
path.mkdirs(); path.mkdirs();
} }
File file = new File(path, "app.apk"); File file = new File(path, "app.apk");
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
@ -1014,7 +1008,6 @@ public class AppMaster {
// sendResult(cid, 1, action, action + ":" + mCmdid); // sendResult(cid, 1, action, action + ":" + mCmdid);
Context context = mService.getApplicationContext(); Context context = mService.getApplicationContext();
mService.logger.info("Upgrade APP: " + url); mService.logger.info("Upgrade APP: " + url);
SysApi.installApk(context, apkPath, context.getPackageName(), true); SysApi.installApk(context, apkPath, context.getPackageName(), true);
sendResult(cid, 1, action, action + ":" + mCmdid + " is installing"); sendResult(cid, 1, action, action + ":" + mCmdid + " is installing");
} else { } else {
@ -1023,6 +1016,57 @@ public class AppMaster {
} }
} }
private void upgradeAppOta(long cid, String action, String url,String oldMd5,String newMd5) {
mService.logger.warning("Recv upgradeAppOta Cmd: url=" + url);
if (StringUtils.isEmpty(oldMd5)|| StringUtils.isEmpty(newMd5)) {
mService.logger.warning("upgradeAppOta oldMd5或者newMd5缺失");
return;
}
Context context = mService.getApplicationContext();
String urlMd5 = MD5Util.md5(url);//获取URL的Md5
File path = new File(MicroPhotoContext.buildAppDir(mService.getApplicationContext()), "packages");
if (!path.exists()) {
path.mkdirs();
}
//Patch存储路径
File patchFile = new File(path, urlMd5+".PATCH");
if (patchFile.exists()) {
patchFile.delete();
}
String patchPath = patchFile.getAbsolutePath();
//老的Apk路径
File oldApk = new File(path, oldMd5 + ".apk");
if (!oldApk.exists()) {
mService.logger.warning("upgradeAppOta未找到对应apk:" + oldMd5);
return;
}
String oldApkPath = oldApk.getAbsolutePath();
//新的Apk路径
File newApk = new File(path, newMd5 + ".apk");
String newApkPath = newApk.getAbsolutePath();
//文件下载
FileDownloader dl = new FileDownloader();
if (dl.download(url, patchPath)) {
mService.logger.info("upgradeAppOta APP: " + url);
if (oldApk.exists()) {
boolean b = MpMasterService.applyPatch(oldApkPath, patchPath, newApkPath);
if (b ) {
String fileMd5 = MD5Util.getFileMd5(newApk.getAbsolutePath());
if (newMd5.equals(fileMd5)) {
SysApi.installApk(context, newApkPath, context.getPackageName(), true);
sendResult(cid, 1, action, action + ":" + mCmdid + " is installing");
}
}
}
} else {
mService.logger.warning("Failed to Download:" + url);
sendResult(cid, 1, action, action + ":" + mCmdid + " download failed");
}
}
private void upgradeOta(long cid, String action, String url, String fileName, String md5) { private void upgradeOta(long cid, String action, String url, String fileName, String md5) {
mService.logger.warning("Recv Upgrade OTA: url=" + url); mService.logger.warning("Recv Upgrade OTA: url=" + url);

@ -177,18 +177,16 @@ public class MainActivity extends AppCompatActivity {
// File pathFile = new File(path); // File pathFile = new File(path);
// if (!pathFile.exists() && !pathFile.mkdirs()) { // if (!pathFile.exists() && !pathFile.mkdirs()) {
// } // }
// String old = pathFile.getAbsolutePath() + "/data/mpapp_v1.3.149_rel_20250429.apk"; // String old = pathFile.getAbsolutePath() + "/data/mpapp_v1.3.150_rel_20250430.apk";
// String newapp = pathFile.getAbsolutePath() + "/data/mpapp_v1.3.150_rel_20250429.apk"; // String newapp = pathFile.getAbsolutePath() + "/data/mpapp_v1.3.151_rel_20250430.apk";
// String patch = pathFile.getAbsolutePath() + "/data/test.patch"; // String patch = pathFile.getAbsolutePath() + "/data/test.patch";
// String newpatch = pathFile.getAbsolutePath() + "/data/111.patch";
// String newpath = pathFile.getAbsolutePath() + "/data/new.apk"; // String newpath = pathFile.getAbsolutePath() + "/data/new.apk";
// ////
// boolean b = MpMasterService.applyPatch(old, newpatch, newpath); // boolean b = MpMasterService.applyPatch(old, patch, newpath);
// System.out.println("" + b); // System.out.println("" + b);
// //
//// boolean b = MpMasterService.applyDiff(old, newapp, newpatch); // boolean b = MpMasterService.applyDiff(old, newapp, patch);
//// System.out.println("" + b); // System.out.println("ceshi ceshi" + b);
//
// } // }
// }).start(); // }).start();

@ -1514,7 +1514,10 @@ public class MpMasterService extends Service {
public native static void rebootDevice(); public native static void rebootDevice();
//用于将差分包跟老app合并生成新App
public native static boolean applyPatch(String oldFile, String patchFile, String newFile); public native static boolean applyPatch(String oldFile, String patchFile, String newFile);
//用于将新app跟老app计算生成差分包
public native static boolean applyDiff(String oldFile, String patchFile, String newFile); public native static boolean applyDiff(String oldFile, String patchFile, String newFile);

Loading…
Cancel
Save