This commit is contained in:
juntao
2020-08-13 15:43:41 +08:00
parent b90d1b4f38
commit 46ac569f70
3 changed files with 43 additions and 17 deletions

View File

@ -15,6 +15,7 @@ import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.gh.common.AppExecutor;
import com.gh.common.constant.Constants;
import com.gh.common.util.DataLogUtils;
import com.gh.common.util.DialogUtils;
@ -25,6 +26,7 @@ import com.gh.common.util.MtaHelper;
import com.gh.common.util.NetworkUtils;
import com.gh.common.util.PackageInstaller;
import com.gh.common.util.PackageUtils;
import com.gh.common.util.SPUtils;
import com.gh.common.util.SpeedUtils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.R;
@ -83,6 +85,7 @@ public class UpdateManager {
private boolean isShowDownload;
private boolean isChecking;
private long lastUpdateFileSize = 0L;
private DataWatcher dataWatcher = new DataWatcher() {
@Override
@ -104,6 +107,11 @@ public class UpdateManager {
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);
@ -127,6 +135,11 @@ public class UpdateManager {
Utils.toast(mContext, "网络劫持,请稍后重试");
}
} else if (updateDialog != null && updateDialog.isShowing()) {
if (downloadEntity.getSize() != lastUpdateFileSize) {
lastUpdateFileSize = downloadEntity.getSize();
SPUtils.setLong(Constants.LAST_GHZS_UPDATE_FILE_SIZE, lastUpdateFileSize);
}
if (DownloadStatus.done.equals(downloadEntity.getStatus())) {
updateUpdateDialogView(true);
}
@ -142,6 +155,8 @@ public class UpdateManager {
this.isShowDownload = false;
this.isChecking = false;
this.loadingDialog = null;
lastUpdateFileSize = SPUtils.getLong(Constants.LAST_GHZS_UPDATE_FILE_SIZE, 0L);
}
public static UpdateManager getInstance(Context context) {
@ -158,11 +173,12 @@ public class UpdateManager {
loadingDialog = DialogUtils.showWaitDialog(mContext, "检查更新中...");
}
String channel = HaloApp.getInstance().getChannel();
RetrofitManager.getInstance(mContext).getApi().getUpdate(PackageUtils.getVersionName(), PackageUtils.getVersionCode(), channel)
RetrofitManager.getInstance(mContext).getApi().getUpdate("3.7.4", 100, channel)
.map(appEntity -> {
boolean isShowUpdateDialog = false;
if (appEntity.getVersionCode() > PackageUtils.getVersionCode()) {
// if (appEntity.getVersionCode() > PackageUtils.getVersionCode()) {
if (true) {
// 助手有更新
UpdateManager.this.appEntity = appEntity;
@ -256,9 +272,9 @@ public class UpdateManager {
confirmTextView = view.findViewById(R.id.confirm);
if (NetworkUtils.isWifiConnected(mContext)) {
if (!isUpdateFileExist(md5)) {
if (!isUpdateFileDownloaded(md5)) {
updateUpdateDialogView(false);
createUpdate(md5);
createUpdate(md5, true);
} else {
updateUpdateDialogView(true);
}
@ -284,11 +300,13 @@ public class UpdateManager {
});
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) {
if (!isUpdateFileDownloaded(md5)) {
updateDialog.dismiss();
} else if (isUpdateFileDownloaded(md5) && !appEntity.isForce()){
updateDialog.dismiss();
}
String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk");
if (isUpdateFileDownloaded(md5)) {
DataLogUtils.uploadUpgradeLog(mContext, "install"); //上传更新安装数据
PackageInstaller.install(mContext, path);
} else {
@ -352,10 +370,10 @@ public class UpdateManager {
isShowDownload = true;
createUpdate(md5);
createUpdate(md5, false);
}
private void createUpdate(String md5) {
private void createUpdate(String md5, boolean isSilentUpdate) {
DownloadManager.getInstance(mContext).addObserver(dataWatcher);
// 添加到下载列表
String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk");
@ -369,18 +387,25 @@ public class UpdateManager {
downloadEntity.setName("光环助手V" + appEntity.getVersion());
downloadEntity.setPath(path);
downloadEntity.setPlatform("官方版");
ExtensionsKt.addMetaExtra(downloadEntity, Constants.EXTRA_DOWNLOAD_TYPE, Constants.SILENT_UPDATE);
if (isSilentUpdate) {
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);
AppExecutor.getUiExecutor().executeWithDelay(() -> {
DownloadManager.getInstance(mContext).add(downloadEntity);
}, 200);
}
private boolean isUpdateFileExist(String md5) {
// TODO 判断是否真的完成了下载
private boolean isUpdateFileDownloaded(String md5) {
String path = FileUtils.getDownloadPath(mContext, "光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk");
File file = new File(path);
return file.exists() && file.length() > 0;
return file.exists() && file.length() == SPUtils.getLong(Constants.LAST_GHZS_UPDATE_FILE_SIZE, 0);
}
private void updateUpdateDialogView(boolean isUpdateDownloaded) {