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 f914863adc..bd03b0ba80 100644 --- a/app/src/main/java/com/gh/gamecenter/manager/UpdateManager.java +++ b/app/src/main/java/com/gh/gamecenter/manager/UpdateManager.java @@ -1,5 +1,6 @@ package com.gh.gamecenter.manager; +import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.SharedPreferences; @@ -18,6 +19,7 @@ import android.widget.TextView; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.core.app.NotificationManagerCompat; +import com.gh.base.CurrentActivityHolder; import com.gh.common.AppExecutor; import com.gh.common.constant.Constants; import com.gh.common.exposure.ExposureEvent; @@ -64,6 +66,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import retrofit2.HttpException; +// 吐了,这个祖传的类,代码写得不是一般的糟糕,还不敢随便改 : ( /** * Created by LGT on 2016/12/8. * 助手更新 工具类 @@ -76,6 +79,7 @@ public class UpdateManager { private final static String ONCE_ONLY_SECOND_OPEN = "ONCE_ONLY_SECOND_OPEN"; private Context mContext; + private Context mApplicationContext; private SharedPreferences mSp; @@ -140,7 +144,7 @@ public class UpdateManager { } if (DownloadStatus.done.equals(downloadEntity.getStatus())) { - DownloadManager.getInstance(mContext).cancel(downloadEntity.getUrl(), false, true); + DownloadManager.getInstance(mApplicationContext).cancel(downloadEntity.getUrl(), false, true); if (downloadDialog != null) { try { downloadDialog.dismiss(); @@ -152,13 +156,13 @@ public class UpdateManager { AppExecutor.getUiExecutor().executeWithDelay(() -> exitApp(), 1000); } } else if (DownloadStatus.neterror.equals(downloadEntity.getStatus())) { - Utils.toast(mContext, "网络错误,请稍后重试"); + Utils.toast(mApplicationContext, "网络错误,请稍后重试"); } else if (DownloadStatus.timeout.equals(downloadEntity.getStatus())) { - Utils.toast(mContext, "请求超时,请稍后重试"); + Utils.toast(mApplicationContext, "请求超时,请稍后重试"); } else if (DownloadStatus.notfound.equals(downloadEntity.getStatus())) { - Utils.toast(mContext, "下载链接异常,请稍后重试"); + Utils.toast(mApplicationContext, "下载链接异常,请稍后重试"); } else if (DownloadStatus.hijack.equals(downloadEntity.getStatus())) { - Utils.toast(mContext, "网络劫持,请稍后重试"); + Utils.toast(mApplicationContext, "网络劫持,请稍后重试"); } } else { if (downloadEntity.getSize() != lastUpdateFileSize) { @@ -186,7 +190,8 @@ public class UpdateManager { private UpdateManager(Context context) { mContext = context; - mSp = PreferenceManager.getDefaultSharedPreferences(mContext); + mApplicationContext = context.getApplicationContext(); + mSp = PreferenceManager.getDefaultSharedPreferences(mApplicationContext); this.isShowDownload = false; this.isChecking = false; @@ -209,7 +214,7 @@ public class UpdateManager { loadingDialog = DialogUtils.showWaitDialog(mContext, "检查更新中..."); } String channel = HaloApp.getInstance().getChannel(); - RetrofitManager.getInstance(mContext).getApi().getUpdate(PackageUtils.getGhVersionName(), PackageUtils.getGhVersionCode(), channel) + RetrofitManager.getInstance(mApplicationContext).getApi().getUpdate(PackageUtils.getGhVersionName(), PackageUtils.getGhVersionCode(), channel) .map(appEntity -> { boolean isShowUpdateDialog = false; @@ -261,7 +266,7 @@ public class UpdateManager { if (!appEntity.isForce() && isAutoCheck && !isUpdateFileDownloaded(response) - && NetworkUtils.isWifiConnected(mContext)) { + && NetworkUtils.isWifiConnected(mApplicationContext)) { createUpdate(response, true); } else { showUpdateDialog(response); @@ -273,7 +278,7 @@ public class UpdateManager { handler.sendMessage(message); } } else if (!isAutoCheck) { - Utils.toast(mContext, "已是最新版本"); + Utils.toast(mApplicationContext, "已是最新版本"); if (handler != null) { handler.sendEmptyMessage(1); } @@ -293,11 +298,11 @@ public class UpdateManager { handler.sendEmptyMessage(1); } if (e != null && (e.code() == 304 || e.code() == 404)) { - Utils.toast(mContext, "您的光环助手已是最新版本"); + Utils.toast(mApplicationContext, "您的光环助手已是最新版本"); return; } - Utils.toast(mContext, "检查更新失败"); + Utils.toast(mApplicationContext, "检查更新失败"); } invokeDismissCallback(); @@ -307,7 +312,9 @@ public class UpdateManager { // 显示助手有更新提示框 private void showUpdateDialog(final String md5) { - updateDialog = new Dialog(mContext); + Context context = getValidContext(); + + updateDialog = new Dialog(context); updateDialog.setOnDismissListener(dialog -> { invokeDismissCallback(); }); @@ -316,7 +323,8 @@ public class UpdateManager { window.setBackgroundDrawableResource(android.R.color.transparent); } - View view = View.inflate(mContext, R.layout.app_update_hint_dialog, null); + View view = View.inflate(context, R.layout.app_update_hint_dialog, null); + cancelUpdateTextView = view.findViewById(R.id.cancel); downloadedHintView = view.findViewById(R.id.downloadedHint); confirmTextView = view.findViewById(R.id.confirm); @@ -331,7 +339,7 @@ public class UpdateManager { R.color.theme_font, false, () -> { - DirectUtils.directToExternalBrowser(mContext, appEntity.getSpareLink()); + DirectUtils.directToExternalBrowser(context, appEntity.getSpareLink()); return null; }) .build(); @@ -340,7 +348,7 @@ public class UpdateManager { externalTextTv.setVisibility(View.VISIBLE); } - if (NetworkUtils.isWifiConnected(mContext)) { + if (NetworkUtils.isWifiConnected(context)) { if (!isUpdateFileDownloaded(md5)) { updateUpdateDialogView(false); createUpdate(md5, true); @@ -376,11 +384,11 @@ public class UpdateManager { } else if (isUpdateFileDownloaded(md5) && !appEntity.isForce()) { updateDialog.dismiss(); } - String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); + String path = FileUtils.getDownloadPath(context, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); // 产品不接受显示下载完成文案从立即更新变为立即安装,所以文案为立即更新时一律不执行安装 if (isUpdateFileDownloaded(md5) && confirmTextView.getText() != "立即更新") { - DataLogUtils.uploadUpgradeLog(mContext, "install"); //上传更新安装数据 - PackageInstaller.install(mContext, false, path); + DataLogUtils.uploadUpgradeLog(context, "install"); //上传更新安装数据 + PackageInstaller.install(context, false, path); } else { MtaHelper.onEvent("软件更新", "下载开始"); @@ -396,20 +404,22 @@ public class UpdateManager { updateDialog.setContentView(view); updateDialog.show(); - DataLogUtils.uploadUpgradeLog(mContext, "notice"); //上传更新通知弹窗数据 + DataLogUtils.uploadUpgradeLog(context, "notice"); //上传更新通知弹窗数据 } private void exitApp() { - NotificationManagerCompat.from(HaloApp.getInstance().getApplication()).cancelAll(); + NotificationManagerCompat.from(mApplicationContext).cancelAll(); AppManager.getInstance().finishAllActivity(); } private void showDownloadDialog(String md5) { - if (NetworkUtils.isMobileConnected(mContext)) { - Utils.toast(mContext, "当前使用移动数据进行下载"); + Context context = getValidContext(); + + if (NetworkUtils.isMobileConnected(context)) { + Utils.toast(context, "当前使用移动数据进行下载"); } - downloadDialog = new Dialog(mContext); + downloadDialog = new Dialog(context); downloadDialog.setOnDismissListener(dialog -> { invokeDismissCallback(); }); @@ -418,7 +428,7 @@ public class UpdateManager { window.setBackgroundDrawableResource(android.R.color.transparent); } - View view = View.inflate(mContext, R.layout.app_updating_dialog, null); + View view = View.inflate(context, R.layout.app_updating_dialog, null); app_pb_progress = view.findViewById(R.id.progress); appProgressSize = view.findViewById(R.id.size); @@ -428,7 +438,7 @@ public class UpdateManager { appProgressFilling = view.findViewById(R.id.progress_filling); view.findViewById(R.id.app_tv_cancel).setOnClickListener(v -> { - DownloadManager.getInstance(mContext).cancel(appEntity.getUrl()); + DownloadManager.getInstance(context).cancel(appEntity.getUrl()); if (appEntity.isForce()) { exitApp(); } else { @@ -437,11 +447,11 @@ public class UpdateManager { }); downloadDialog.setOnDismissListener(dialog -> { - DownloadManager.getInstance(mContext).removeObserver(dataWatcher); + DownloadManager.getInstance(context).removeObserver(dataWatcher); isShowDownload = false; }); - int dialogWidth = mContext.getResources().getDisplayMetrics().widthPixels - DisplayUtils.dip2px(60); + int dialogWidth = context.getResources().getDisplayMetrics().widthPixels - DisplayUtils.dip2px(60); downloadDialog.setCanceledOnTouchOutside(false); downloadDialog.setCancelable(false); downloadDialog.closeOptionsMenu(); @@ -456,20 +466,20 @@ public class UpdateManager { } private void createUpdate(String md5, boolean isSilentUpdate) { - DownloadManager.getInstance(mContext).addObserver(dataWatcher); + DownloadManager.getInstance(mApplicationContext).addObserver(dataWatcher); boolean shouldCancelPreviousDownload = true; // 是否应该取消旧更新任务 // 在部分设备上取消正在进行中的旧下载任务再创建新下载任务有机率造成新下载任务依然带有 "静默更新" 标签导致无法唤起安装 // 所以这里对当前静默更新任务正在进行中的时候就不用删旧任务,直接改 meta 标签 if (!isSilentUpdate - && DownloadManager.getInstance(mContext).isTaskDownloading(appEntity.getUrl())) { + && DownloadManager.getInstance(mApplicationContext).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); + DownloadManager.getInstance(mApplicationContext).updateDownloadEntity(entity); + DownloadManager.getInstance(mApplicationContext).resume(entity, false); shouldCancelPreviousDownload = false; } @@ -482,7 +492,7 @@ public class UpdateManager { // 预下载完成或者还没进行预下载的都进这里,先删掉旧的下载文件再进行下载 if (shouldCancelPreviousDownload) { - String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); + String path = FileUtils.getDownloadPath(mApplicationContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); File file = new File(path); if (file.exists() && file.delete()) { Utils.log(file.getName() + " file delete success."); @@ -506,18 +516,34 @@ public class UpdateManager { 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(); + downloadEntity.setPackageName(mApplicationContext.getPackageName()); + DownloadManager.getInstance(mApplicationContext).cancel(appEntity.getUrl(), true, true); + DownloadManager.getInstance(mApplicationContext).pauseAll(); AppExecutor.getUiExecutor().executeWithDelay(() -> { - DownloadManager.getInstance(mContext).add(downloadEntity); + DownloadManager.getInstance(mApplicationContext).add(downloadEntity); }, 200); } } + /** + * 尽量获取有效的 activity context + */ + private Context getValidContext() { + Context context = mContext; + + Activity currentActivity = CurrentActivityHolder.getCurrentActivity(); + if (currentActivity != null + && mContext != currentActivity + && !currentActivity.isFinishing()) { + context = currentActivity; + } + + return context; + } + private boolean isUpdateFileDownloaded(String md5) { - String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); + String path = FileUtils.getDownloadPath(mApplicationContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); File file = new File(path); return file.exists() && file.length() == SPUtils.getLong(Constants.LAST_GHZS_UPDATE_FILE_SIZE, 0);