diff --git a/app/src/main/java/com/gh/common/constant/Constants.java b/app/src/main/java/com/gh/common/constant/Constants.java index 7cb7bec47a..b02bb2f575 100644 --- a/app/src/main/java/com/gh/common/constant/Constants.java +++ b/app/src/main/java/com/gh/common/constant/Constants.java @@ -48,6 +48,9 @@ public class Constants { public static final String RAW_GAME_ICON = "raw_game_icon"; public static final String GAME_ICON_SUBSCRIPT = "game_icon_subscript"; + public static final String EXTRA_DOWNLOAD_TYPE = "extra_download_type"; + public static final String SILENT_UPDATE = "静默更新"; + // 新用户首次启动光环的时间 public static final String SP_INITIAL_USAGE_TIME = "initial_usage_time"; diff --git a/app/src/main/java/com/gh/common/util/DownloadNotificationHelper.kt b/app/src/main/java/com/gh/common/util/DownloadNotificationHelper.kt index 380c1a0176..0a6965a9c6 100644 --- a/app/src/main/java/com/gh/common/util/DownloadNotificationHelper.kt +++ b/app/src/main/java/com/gh/common/util/DownloadNotificationHelper.kt @@ -42,6 +42,8 @@ object DownloadNotificationHelper { val downloadNotificationId = (entity.gameId + entity.packageName).hashCode() val xapkStatus = entity.meta[XapkInstaller.XAPK_UNZIP_STATUS] + if (entity.getMetaExtra(Constants.EXTRA_DOWNLOAD_TYPE) == Constants.SILENT_UPDATE) return + val intent = Intent() if (entity.status == DownloadStatus.done && xapkStatus != XapkUnzipStatus.FAILURE.name) { intent.putExtra(EntranceUtils.KEY_DATA, entity.toJson()) @@ -56,7 +58,6 @@ object DownloadNotificationHelper { PendingIntent.FLAG_UPDATE_CURRENT) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // importance == IMPORTANCE_DEFAULT 时每次更新都会触发震动 val channel = NotificationChannel(DOWNLOAD_CHANNEL_ID, DOWNLOAD_CHANNEL_ID, NotificationManager.IMPORTANCE_LOW) notificationManager.createNotificationChannel(channel) } @@ -69,7 +70,6 @@ object DownloadNotificationHelper { .setGroup(DOWNLOAD_GROUP_KEY) .setWhen(whenTime) - if (xapkStatus == XapkUnzipStatus.FAILURE.name) { builder.setContentText("《" + entity.name + "》解压失败,点击查看详情~") } else { 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 90bfec6c09..6eab28c109 100644 --- a/app/src/main/java/com/gh/common/util/DownloadObserver.kt +++ b/app/src/main/java/com/gh/common/util/DownloadObserver.kt @@ -107,9 +107,11 @@ object DownloadObserver { if (DownloadStatus.done == downloadEntity.status) { if (downloadEntity.name.contains(mApplication.getString(R.string.app_name))) { MtaHelper.onEvent("软件更新", "下载完成") + // 会有 ActivityNotFoundException 异常,catch 掉不管了 tryWithDefaultCatch { - // 会有 ActivityNotFoundException 异常,catch 掉不管了 - PackageInstaller.install(mApplication, downloadEntity) + if (Constants.SILENT_UPDATE != downloadEntity.getMetaExtra(Constants.EXTRA_DOWNLOAD_TYPE)) { + PackageInstaller.install(mApplication, downloadEntity) + } } DataLogUtils.uploadUpgradeLog(mApplication, "install") //上传更新安装数据 } else { 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 0fff1b700c..5dcef4d81d 100644 --- a/app/src/main/java/com/gh/gamecenter/manager/UpdateManager.java +++ b/app/src/main/java/com/gh/gamecenter/manager/UpdateManager.java @@ -15,9 +15,11 @@ import android.widget.TextView; import androidx.constraintlayout.widget.ConstraintLayout; +import com.gh.common.constant.Constants; import com.gh.common.util.DataLogUtils; import com.gh.common.util.DialogUtils; import com.gh.common.util.DisplayUtils; +import com.gh.common.util.ExtensionsKt; import com.gh.common.util.MD5Utils; import com.gh.common.util.MtaHelper; import com.gh.common.util.NetworkUtils; @@ -29,7 +31,6 @@ import com.gh.gamecenter.R; 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.DataWatcher; import com.lightgame.download.DownloadEntity; import com.lightgame.download.DownloadStatus; @@ -66,6 +67,7 @@ public class UpdateManager { private Dialog downloadDialog; private Dialog loadingDialog; + private Dialog updateDialog; private ProgressBar app_pb_progress; private TextView appProgressSize; @@ -74,49 +76,59 @@ public class UpdateManager { private View appProgressFilling; private View appProgressAnchor; + private TextView cancelUpdateTextView; + private TextView downloadedHintView; + private TextView confirmTextView; + private boolean isShowDownload; private boolean isChecking; private DataWatcher dataWatcher = new DataWatcher() { @Override public void onDataChanged(DownloadEntity downloadEntity) { - if (downloadEntity.getName().contains("光环助手") && 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)); + 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); - } - - 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(); + 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 (appEntity != null && appEntity.isForce()) { - AppManager.getInstance().finishAllActivity(); + + 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()) { + AppManager.getInstance().finishAllActivity(); + } + } else if (DownloadStatus.neterror.equals(downloadEntity.getStatus())) { + Utils.toast(mContext, "网络错误,请稍后重试"); + } else if (DownloadStatus.timeout.equals(downloadEntity.getStatus())) { + Utils.toast(mContext, "请求超时,请稍后重试"); + } else if (DownloadStatus.notfound.equals(downloadEntity.getStatus())) { + Utils.toast(mContext, "下载链接异常,请稍后重试"); + } else if (DownloadStatus.hijack.equals(downloadEntity.getStatus())) { + Utils.toast(mContext, "网络劫持,请稍后重试"); + } + } else if (updateDialog != null && updateDialog.isShowing()) { + if (DownloadStatus.done.equals(downloadEntity.getStatus())) { + updateUpdateDialogView(true); } - } else if (DownloadStatus.neterror.equals(downloadEntity.getStatus())) { - Utils.toast(mContext, "网络错误,请稍后重试"); - } else if (DownloadStatus.timeout.equals(downloadEntity.getStatus())) { - Utils.toast(mContext, "请求超时,请稍后重试"); - } else if (DownloadStatus.notfound.equals(downloadEntity.getStatus())) { - Utils.toast(mContext, "下载链接异常,请稍后重试"); - } else if (DownloadStatus.hijack.equals(downloadEntity.getStatus())) { - Utils.toast(mContext, "网络劫持,请稍后重试"); } } } @@ -144,12 +156,12 @@ public class UpdateManager { if (!isAutoCheck) { loadingDialog = DialogUtils.showWaitDialog(mContext, "检查更新中..."); } - String channel = HaloApp.getInstance().getChannel(); - RetrofitManager.getInstance(mContext).getApi().getUpdate(PackageUtils.getVersionName(), PackageUtils.getVersionCode(), channel) + String channel = "GH_102"; + RetrofitManager.getInstance(mContext).getApi().getUpdate("3.7.4", 100, channel) .map(appEntity -> { boolean isShowUpdateDialog = false; - if (appEntity.getVersionCode() > PackageUtils.getVersionCode()) { + if (appEntity.getVersionCode() > 100) { // 助手有更新 UpdateManager.this.appEntity = appEntity; @@ -231,14 +243,25 @@ public class UpdateManager { // 显示助手有更新提示框 private void showUpdateDialog(final String md5) { - final Dialog updateDialog = new Dialog(mContext); + updateDialog = new Dialog(mContext); Window window = updateDialog.getWindow(); if (window != null) { window.setBackgroundDrawableResource(android.R.color.transparent); } View view = View.inflate(mContext, R.layout.app_update_hint_dialog, null); - View cancelBtn = view.findViewById(R.id.cancel); + cancelUpdateTextView = view.findViewById(R.id.cancel); + downloadedHintView = view.findViewById(R.id.downloadedHint); + confirmTextView = view.findViewById(R.id.confirm); + + if (NetworkUtils.isWifiConnected(mContext)) { + if (!isUpdateFileExist(md5)) { + updateUpdateDialogView(false); + createUpdate(md5); + } else { + updateUpdateDialogView(true); + } + } TextView content = view.findViewById(R.id.desc); content.setText(Html.fromHtml(appEntity.getContent())); @@ -249,31 +272,29 @@ public class UpdateManager { TextView size = view.findViewById(R.id.size); size.setText(String.format("大小 %s", appEntity.getSize())); - view.setOnClickListener(v -> cancelBtn.performClick()); + view.setOnClickListener(v -> cancelUpdateTextView.performClick()); - cancelBtn.setOnClickListener( - v -> { - if (appEntity.isForce()) { - AppManager.getInstance().finishAllActivity(); - } else { - updateDialog.dismiss(); - } - }); + cancelUpdateTextView.setOnClickListener(v -> { + if (appEntity.isForce()) { + AppManager.getInstance().finishAllActivity(); + } else { + updateDialog.dismiss(); + } + }); - view.findViewById(R.id.confirm).setOnClickListener( - v -> { - updateDialog.dismiss(); - String path = FileUtils.getDownloadPath(mContext, - "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); - File file = new File(path); - if (file.exists() && file.length() > 0) { - DataLogUtils.uploadUpgradeLog(mContext, "install"); //上传更新安装数据 - PackageInstaller.install(mContext, path); - } else { - MtaHelper.onEvent("软件更新", "下载开始"); - showDownloadDialog(md5); - } - }); + confirmTextView.setOnClickListener(v -> { + updateDialog.dismiss(); + String path = FileUtils.getDownloadPath(mContext, + "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); + File file = new File(path); + if (file.exists() && file.length() > 0) { + DataLogUtils.uploadUpgradeLog(mContext, "install"); //上传更新安装数据 + PackageInstaller.install(mContext, path); + } else { + MtaHelper.onEvent("软件更新", "下载开始"); + showDownloadDialog(md5); + } + }); if (appEntity.isForce()) { updateDialog.setCanceledOnTouchOutside(false); @@ -330,8 +351,11 @@ public class UpdateManager { isShowDownload = true; - DownloadManager.getInstance(mContext).addObserver(dataWatcher); + createUpdate(md5); + } + private void createUpdate(String md5) { + DownloadManager.getInstance(mContext).addObserver(dataWatcher); // 添加到下载列表 String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); File file = new File(path); @@ -344,12 +368,39 @@ public class UpdateManager { downloadEntity.setName("光环助手V" + appEntity.getVersion()); downloadEntity.setPath(path); downloadEntity.setPlatform("官方版"); + ExtensionsKt.addMetaExtra(downloadEntity, Constants.EXTRA_DOWNLOAD_TYPE, Constants.SILENT_UPDATE); downloadEntity.setPackageName(mContext.getPackageName()); DownloadManager.getInstance(mContext).cancel(downloadEntity.getUrl(), false, true); DownloadManager.getInstance(mContext).pauseAll(); DownloadManager.getInstance(mContext).add(downloadEntity); } + private boolean isUpdateFileExist(String md5) { + String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk"); + File file = new File(path); + return file.exists() && file.length() > 0; + } + + private void updateUpdateDialogView(boolean isUpdateDownloaded) { + if (isUpdateDownloaded) { + confirmTextView.setText("立即安装"); + downloadedHintView.setVisibility(View.VISIBLE); + if (appEntity.isForce()) { + cancelUpdateTextView.setText("暂不安装,退出光环"); + } else { + cancelUpdateTextView.setText("暂不安装"); + } + } else { + confirmTextView.setText("立即更新"); + downloadedHintView.setVisibility(View.GONE); + if (appEntity.isForce()) { + cancelUpdateTextView.setText("暂不更新,退出光环"); + } else { + cancelUpdateTextView.setText("暂不更新"); + } + } + } + private String getUpdateOnceOnlySpKey() { return "UPDATE_ONCE_ONLY_KEY" + PackageUtils.getVersionCode(); } diff --git a/app/src/main/res/layout/app_update_hint_dialog.xml b/app/src/main/res/layout/app_update_hint_dialog.xml index ae2d555a6a..08c668632a 100644 --- a/app/src/main/res/layout/app_update_hint_dialog.xml +++ b/app/src/main/res/layout/app_update_hint_dialog.xml @@ -1,6 +1,7 @@ @@ -87,14 +88,26 @@ app:layout_constraintRight_toRightOf="@id/head_background" app:layout_constraintTop_toBottomOf="@id/version" /> + + + app:layout_constraintTop_toBottomOf="@id/downloadedHint" + app:layout_goneMarginTop="24dp" /> + + - - \ No newline at end of file