This commit is contained in:
juntao
2020-08-12 21:57:46 +08:00
parent a14f35a4f0
commit 422abe1b87
5 changed files with 154 additions and 82 deletions

View File

@ -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();
}