修复使用强制更新时偶尔无法唤起安装的问题和多线程下载更新的冲突问题

This commit is contained in:
juntao
2020-12-04 17:22:48 +08:00
parent 9daf01d9a5
commit dfdd12bf18
2 changed files with 78 additions and 46 deletions

View File

@ -36,6 +36,7 @@ import com.gh.gamecenter.entity.AppEntity;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.halo.assistant.HaloApp;
import com.lightgame.download.DataChanger;
import com.lightgame.download.DataWatcher;
import com.lightgame.download.DownloadEntity;
import com.lightgame.download.DownloadStatus;
@ -96,38 +97,43 @@ public class UpdateManager {
public void onDataChanged(DownloadEntity downloadEntity) {
if (downloadEntity.getName().contains("光环助手")) {
if (downloadDialog != null && downloadDialog.isShowing() && isShowDownload) {
float size = (((float) downloadEntity.getProgress() / 1024) / 1024);
DecimalFormat df = new DecimalFormat("0.00");
appProgressSize.setText((df.format(size) + "MB"));
appProgressRemain.setText(String.format("剩余%s", SpeedUtils.getRemainSecondTime(downloadEntity.getSize(),
downloadEntity.getProgress(), downloadEntity.getSpeed() * 1024)));
app_pb_progress.setProgress((int) (downloadEntity.getPercent() * 10));
int width = app_pb_progress.getWidth();
int marLeft = (int) (downloadEntity.getPercent() / 100 * width);
ViewGroup.LayoutParams anchorLp = appProgressAnchor.getLayoutParams();
if (anchorLp instanceof ConstraintLayout.LayoutParams) {
((ConstraintLayout.LayoutParams) anchorLp).leftMargin = marLeft;
appProgressAnchor.setLayoutParams(anchorLp);
// 被取消任务的状态不用来更新页面
if (!DownloadStatus.cancel.equals(downloadEntity.getStatus())) {
float size = (((float) downloadEntity.getProgress() / 1024) / 1024);
DecimalFormat df = new DecimalFormat("0.00");
appProgressSize.setText((df.format(size) + "MB"));
appProgressRemain.setText(String.format("剩余%s", SpeedUtils.getRemainSecondTime(downloadEntity.getSize(),
downloadEntity.getProgress(), downloadEntity.getSpeed() * 1024)));
app_pb_progress.setProgress((int) (downloadEntity.getPercent() * 10));
int width = app_pb_progress.getWidth();
int marLeft = (int) (downloadEntity.getPercent() / 100 * width);
ViewGroup.LayoutParams anchorLp = appProgressAnchor.getLayoutParams();
if (anchorLp instanceof ConstraintLayout.LayoutParams) {
((ConstraintLayout.LayoutParams) anchorLp).leftMargin = marLeft;
appProgressAnchor.setLayoutParams(anchorLp);
}
if (downloadEntity.getSize() != lastUpdateFileSize) {
lastUpdateFileSize = downloadEntity.getSize();
SPUtils.setLong(Constants.LAST_GHZS_UPDATE_FILE_SIZE, lastUpdateFileSize);
}
ViewGroup.LayoutParams fillingLp = appProgressFilling.getLayoutParams();
fillingLp.width = marLeft + DisplayUtils.dip2px(5);
appProgressFilling.setLayoutParams(fillingLp);
appProgressPercent.setText(((int) downloadEntity.getPercent() + "%"));
}
if (downloadEntity.getSize() != lastUpdateFileSize) {
lastUpdateFileSize = downloadEntity.getSize();
SPUtils.setLong(Constants.LAST_GHZS_UPDATE_FILE_SIZE, lastUpdateFileSize);
}
ViewGroup.LayoutParams fillingLp = appProgressFilling.getLayoutParams();
fillingLp.width = marLeft + DisplayUtils.dip2px(5);
appProgressFilling.setLayoutParams(fillingLp);
appProgressPercent.setText(((int) downloadEntity.getPercent() + "%"));
if (DownloadStatus.done.equals(downloadEntity.getStatus())) {
DownloadManager.getInstance(mContext).cancel(downloadEntity.getUrl(), false, true);
if (downloadDialog != null) {
downloadDialog.dismiss();
}
if (appEntity != null && appEntity.isForce()) {
exitApp();
AppExecutor.getUiExecutor().executeWithDelay(() -> exitApp(), 1000);
}
} else if (DownloadStatus.neterror.equals(downloadEntity.getStatus())) {
Utils.toast(mContext, "网络错误,请稍后重试");
@ -406,30 +412,55 @@ public class UpdateManager {
private void createUpdate(String md5, boolean isSilentUpdate) {
DownloadManager.getInstance(mContext).addObserver(dataWatcher);
// 添加到下载列表
String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk");
File file = new File(path);
if (file.exists() && file.delete()) {
Utils.log(file.getName() + " file delete success.");
boolean shouldCancelPreviousDownload = true; // 是否应该取消旧更新任务
// 在部分设备上取消正在进行中的旧下载任务再创建新下载任务有机率造成新下载任务依然带有 "静默更新" 标签导致无法唤起安装
// 所以这里对当前静默更新任务正在进行中的时候就不用删旧任务,直接改 meta 标签
if (!isSilentUpdate
&& DownloadManager.getInstance(mContext).isTaskDownloading(appEntity.getUrl())) {
try {
DownloadEntity entity = DataChanger.INSTANCE.getDownloadEntries().get(appEntity.getUrl());
if (entity != null) {
ExtensionsKt.addMetaExtra(entity, Constants.EXTRA_DOWNLOAD_TYPE, "不再是静默更新");
DownloadManager.getInstance(mContext).updateDownloadEntity(entity);
DownloadManager.getInstance(mContext).resume(entity, false);
shouldCancelPreviousDownload = false;
}
} catch (Exception e) {
// 出现异常走删旧下载任务重下流程
shouldCancelPreviousDownload = true;
e.printStackTrace();
}
}
DownloadEntity downloadEntity = new DownloadEntity();
downloadEntity.setUrl(appEntity.getUrl());
downloadEntity.setName("光环助手V" + appEntity.getVersion());
downloadEntity.setPath(path);
downloadEntity.setPlatform("官方版");
// 预下载完成或者还没进行预下载的都进这里,先删掉旧的下载文件再进行下载
if (shouldCancelPreviousDownload) {
String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk");
File file = new File(path);
if (file.exists() && file.delete()) {
Utils.log(file.getName() + " file delete success.");
}
if (isSilentUpdate) {
ExtensionsKt.addMetaExtra(downloadEntity, Constants.EXTRA_DOWNLOAD_TYPE, Constants.SILENT_UPDATE);
DownloadEntity downloadEntity = new DownloadEntity();
downloadEntity.setUrl(appEntity.getUrl());
downloadEntity.setName("光环助手V" + appEntity.getVersion());
downloadEntity.setPath(path);
downloadEntity.setPlatform("官方版");
if (isSilentUpdate) {
ExtensionsKt.addMetaExtra(downloadEntity, Constants.EXTRA_DOWNLOAD_TYPE, Constants.SILENT_UPDATE);
}
downloadEntity.setPackageName(mContext.getPackageName());
DownloadManager.getInstance(mContext).cancel(appEntity.getUrl(), true, true);
DownloadManager.getInstance(mContext).pauseAll();
AppExecutor.getUiExecutor().executeWithDelay(() -> {
DownloadManager.getInstance(mContext).add(downloadEntity);
}, 200);
}
downloadEntity.setPackageName(mContext.getPackageName());
DownloadManager.getInstance(mContext).cancel(downloadEntity.getUrl(), true, true);
DownloadManager.getInstance(mContext).pauseAll();
AppExecutor.getUiExecutor().executeWithDelay(() -> {
DownloadManager.getInstance(mContext).add(downloadEntity);
}, 200);
}
private boolean isUpdateFileDownloaded(String md5) {