From dfdd12bf18a37d6e6900cf85deb783aa4bdb15b9 Mon Sep 17 00:00:00 2001 From: juntao Date: Fri, 4 Dec 2020 17:22:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BD=BF=E7=94=A8=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=E6=9B=B4=E6=96=B0=E6=97=B6=E5=81=B6=E5=B0=94=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=94=A4=E8=B5=B7=E5=AE=89=E8=A3=85=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=92=8C=E5=A4=9A=E7=BA=BF=E7=A8=8B=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9A=84=E5=86=B2=E7=AA=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gh/common/util/DownloadObserver.kt | 7 +- .../gh/gamecenter/manager/UpdateManager.java | 117 +++++++++++------- 2 files changed, 78 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/gh/common/util/DownloadObserver.kt b/app/src/main/java/com/gh/common/util/DownloadObserver.kt index 0d8b5749b4..cfcd418c46 100644 --- a/app/src/main/java/com/gh/common/util/DownloadObserver.kt +++ b/app/src/main/java/com/gh/common/util/DownloadObserver.kt @@ -1,14 +1,13 @@ package com.gh.common.util -import android.content.pm.PackageManager import android.os.Build import android.preference.PreferenceManager import com.gh.base.BaseActivity import com.gh.common.constant.Constants import com.gh.common.exposure.ExposureUtils +import com.gh.common.exposure.meta.MetaUtil import com.gh.common.simulator.SimulatorDownloadManager import com.gh.common.simulator.SimulatorGameManager -import com.gh.common.exposure.meta.MetaUtil import com.gh.common.xapk.XapkInstaller import com.gh.download.DownloadDataHelper import com.gh.download.DownloadManager @@ -115,7 +114,9 @@ object DownloadObserver { // 会有 ActivityNotFoundException 异常,catch 掉不管了 tryWithDefaultCatch { if (Constants.SILENT_UPDATE != downloadEntity.getMetaExtra(Constants.EXTRA_DOWNLOAD_TYPE)) { - PackageInstaller.install(mApplication, downloadEntity) + // TODO 在 Android 11 上没有授权安装未知应用的权限前第一次调用这个方法系统会杀掉我们的进程... + // 没能找到类似的解释,最接近的是这个 https://issuetracker.google.com/issues/154157387,但也只是点授权杀进程而已 + PackageInstaller.install(mApplication, downloadEntity); DataLogUtils.uploadUpgradeLog(mApplication, "install") //上传更新安装数据 } } diff --git a/app/src/main/java/com/gh/gamecenter/manager/UpdateManager.java b/app/src/main/java/com/gh/gamecenter/manager/UpdateManager.java index 466b3a3787..3faa10e0b8 100644 --- a/app/src/main/java/com/gh/gamecenter/manager/UpdateManager.java +++ b/app/src/main/java/com/gh/gamecenter/manager/UpdateManager.java @@ -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) {