Commit 347211e1 by pye52

增加版本相同时不重复安装的机制(避免后台多次发送同一条指令导致重复下载更新包)

parent 247d0772
......@@ -53,12 +53,24 @@ public class UpdateCommandWorker extends CommandWorker {
@NonNull
@Override
public Result doWork() {
LogUtils.i(TAG, "开始执行更新任务");
if (commandUpdate.getData() == null || commandUpdate.getData().getUrl() == null) {
LogUtils.d(TAG, "更新包地址异常: " + commandUpdate.toString());
return failed("更新包地址异常");
}
File f = new File(UPDATE_APK_PATH, UPDATE_APK);
f.deleteOnExit();
File updateApk = new File(UPDATE_APK_PATH, UPDATE_APK);
if (updateApk.exists()) {
// 若文件存在,则检查安装文件版本是否与当前应用版本一致
// 若一致则说明已安装完毕,直接返回即可
AppUtils.AppInfo info = AppUtils.getApkInfo(updateApk);
if (info != null
&& info.getPackageName().equals(BuildConfig.APPLICATION_ID)
&& info.getVersionCode() == BuildConfig.VERSION_CODE) {
LogUtils.d(TAG, "版本" + info.getVersionCode() + "已安装完毕");
return success("已是最新版本");
}
}
updateApk.deleteOnExit();
String url = commandUpdate.getData().getUrl();
Request request = new Request.Builder()
......@@ -80,13 +92,13 @@ public class UpdateCommandWorker extends CommandWorker {
}
is = body.byteStream();
fos = new FileOutputStream(f);
fos = new FileOutputStream(updateApk);
long total = body.contentLength();
float totalBytesRead = 0f;
BufferedSource source = body.source();
BufferedSink sink = Okio.buffer(Okio.sink(f));
BufferedSink sink = Okio.buffer(Okio.sink(updateApk));
Buffer sinkBuffer = sink.getBuffer();
for (long bytesRead; (bytesRead = source.read(sinkBuffer, BUFFER_SIZE)) != -1; ) {
......@@ -100,7 +112,7 @@ public class UpdateCommandWorker extends CommandWorker {
source.close();
body.close();
} catch (IOException e) {
f.deleteOnExit();
updateApk.deleteOnExit();
LogUtils.w(TAG, "更新包下载失败: " + e.getMessage(), e);
return failed("更新包下载失败");
} finally {
......@@ -119,7 +131,6 @@ public class UpdateCommandWorker extends CommandWorker {
}
// 检查apk是否为合法安装文件并进入安装状态
File updateApk = new File(UPDATE_APK_PATH, UPDATE_APK);
AppUtils.AppInfo info = AppUtils.getApkInfo(updateApk);
LogUtils.d(TAG, "更新包下载成功,开始安装: " + (info == null ? "null" : info.getPackageName()));
if (info == null || !info.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment