290 lines
12 KiB
Java
290 lines
12 KiB
Java
package com.gh.gamecenter.manager;
|
||
|
||
import android.app.Dialog;
|
||
import android.content.Context;
|
||
import android.content.SharedPreferences;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
import android.preference.PreferenceManager;
|
||
import android.text.Html;
|
||
import android.view.View;
|
||
import android.view.Window;
|
||
import android.widget.ProgressBar;
|
||
import android.widget.TextView;
|
||
|
||
import com.gh.common.util.DataLogUtils;
|
||
import com.gh.common.util.DataUtils;
|
||
import com.gh.common.util.DialogUtils;
|
||
import com.gh.common.util.MD5Utils;
|
||
import com.gh.common.util.PackageUtils;
|
||
import com.gh.common.util.SpeedUtils;
|
||
import com.gh.download.DownloadManager;
|
||
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;
|
||
import com.lightgame.download.FileUtils;
|
||
import com.lightgame.utils.AppManager;
|
||
import com.lightgame.utils.Utils;
|
||
|
||
import java.io.File;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.Date;
|
||
import java.util.Locale;
|
||
|
||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||
import io.reactivex.schedulers.Schedulers;
|
||
import retrofit2.HttpException;
|
||
|
||
/**
|
||
* Created by LGT on 2016/12/8.
|
||
* 助手更新 工具类
|
||
*/
|
||
public class UpdateManager {
|
||
|
||
private Context mContext;
|
||
|
||
private AppEntity appEntity;
|
||
|
||
private Dialog downloadDialog;
|
||
private Dialog loadingDialog;
|
||
|
||
private ProgressBar app_pb_progress;
|
||
private TextView app_tv_speed;
|
||
private TextView app_tv_percent;
|
||
|
||
private boolean isShowDownload;
|
||
private boolean isChecking;
|
||
|
||
private DataWatcher dataWatcher = new DataWatcher() {
|
||
@Override
|
||
public void onDataChanged(DownloadEntity downloadEntity) {
|
||
if (downloadEntity.getName().contains("光环助手") && isShowDownload) {
|
||
app_tv_speed.setText(String.format("%s(剩%s)", SpeedUtils.getSpeed(downloadEntity.getSpeed()),
|
||
SpeedUtils.getRemainTime(downloadEntity.getSize(),
|
||
downloadEntity.getProgress(), downloadEntity.getSpeed() * 1024)));
|
||
app_pb_progress.setProgress((int) (downloadEntity.getPercent() * 10));
|
||
app_tv_percent.setText(String.format(
|
||
Locale.getDefault(), "%s%%", String.valueOf(downloadEntity.getPercent())));
|
||
if (DownloadStatus.done.equals(downloadEntity.getStatus())) {
|
||
DownloadManager.getInstance(mContext).cancel(downloadEntity.getUrl(), false);
|
||
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, "网络劫持,请稍后重试");
|
||
}
|
||
}
|
||
}
|
||
};
|
||
|
||
private UpdateManager(Context context) {
|
||
mContext = context;
|
||
this.isShowDownload = false;
|
||
this.isChecking = false;
|
||
this.loadingDialog = null;
|
||
}
|
||
|
||
public static UpdateManager getInstance(Context context) {
|
||
return new UpdateManager(context);
|
||
}
|
||
|
||
// 检查更新
|
||
public void checkUpdate(final boolean isAutoCheck, final Handler handler) {
|
||
if (isChecking) {
|
||
return;
|
||
}
|
||
isChecking = true;
|
||
if (!isAutoCheck) {
|
||
loadingDialog = DialogUtils.showWaitDialog(mContext, "检查更新中...");
|
||
}
|
||
String channel = HaloApp.getInstance().getChannel();
|
||
RetrofitManager.getInstance(mContext).getApi().getUpdate(PackageUtils.getVersionName(), channel)
|
||
.map(appEntity -> {
|
||
String md5 = null;
|
||
|
||
float version = Float.valueOf(appEntity.getVersion());
|
||
float currentVersion = Float.valueOf(PackageUtils.getVersionName());
|
||
if (version > currentVersion) {
|
||
// 助手有更新
|
||
UpdateManager.this.appEntity = appEntity;
|
||
|
||
// 手动更新 强制更新 每次都提示
|
||
if (!isAutoCheck || appEntity.isForce() || "EVERY_TIME_OPEN".equals(appEntity.getAlert())) {
|
||
md5 = MD5Utils.getUpdateMD5(appEntity.getUrl(), appEntity.getContent());
|
||
} else if (!"NEVER".equals(appEntity.getAlert())) { // 一天提示一次
|
||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||
String showUpdateTime = sp.getString("show_update_tiem", null);
|
||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
|
||
String today = format.format(new Date());
|
||
if (!today.equals(showUpdateTime)) {
|
||
sp.edit().putString("show_update_tiem", today).apply();
|
||
md5 = MD5Utils.getUpdateMD5(appEntity.getUrl(), appEntity.getContent());
|
||
}
|
||
}
|
||
}
|
||
return md5;
|
||
})
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<String>() {
|
||
@Override
|
||
public void onResponse(String response) {
|
||
isChecking = false;
|
||
if (loadingDialog != null) {
|
||
loadingDialog.dismiss();
|
||
}
|
||
if (response != null) {
|
||
showUpdateDialog(response);
|
||
if (handler != null) {
|
||
Message message = new Message();
|
||
message.what = 0;
|
||
message.obj = appEntity.getVersion();
|
||
handler.sendMessage(message);
|
||
}
|
||
} else if (!isAutoCheck) {
|
||
Utils.toast(mContext, "已是最新版本");
|
||
if (handler != null) {
|
||
handler.sendEmptyMessage(1);
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(HttpException e) {
|
||
isChecking = false;
|
||
if (loadingDialog != null) {
|
||
loadingDialog.dismiss();
|
||
}
|
||
if (!isAutoCheck) {
|
||
if (handler != null) {
|
||
handler.sendEmptyMessage(1);
|
||
}
|
||
if (e != null && e.code() == 304) {
|
||
Utils.toast(mContext, "您的光环助手已是最新版本");
|
||
return;
|
||
}
|
||
|
||
Utils.toast(mContext, "检查更新失败");
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
// 显示助手有更新提示框
|
||
private void showUpdateDialog(final String md5) {
|
||
final Dialog updateDialog = new Dialog(mContext);
|
||
|
||
View view = View.inflate(mContext, R.layout.app_update_hint_dialog, null);
|
||
|
||
TextView content = view.findViewById(R.id.updeta_content);
|
||
content.setText(Html.fromHtml(appEntity.getContent()));
|
||
|
||
TextView version = view.findViewById(R.id.update_app_version);
|
||
version.setText(String.format("光环助手V%s更新内容:", appEntity.getVersion()));
|
||
|
||
TextView size = view.findViewById(R.id.update_app_size);
|
||
size.setText(String.format("大小:%s", appEntity.getSize()));
|
||
|
||
view.findViewById(R.id.update_cancel).setOnClickListener(
|
||
v -> {
|
||
if (appEntity.isForce()) {
|
||
AppManager.getInstance().finishAllActivity();
|
||
} else {
|
||
updateDialog.dismiss();
|
||
}
|
||
});
|
||
|
||
view.findViewById(R.id.update_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"); //上传更新安装数据
|
||
mContext.startActivity(PackageUtils.getInstallIntent(mContext, path));
|
||
} else {
|
||
DataUtils.onEvent(mContext, "软件更新", "下载开始");
|
||
showDownloadDialog(md5);
|
||
}
|
||
});
|
||
|
||
if (appEntity.isForce()) {
|
||
updateDialog.setCanceledOnTouchOutside(false);
|
||
updateDialog.setCancelable(false);
|
||
}
|
||
updateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
updateDialog.setContentView(view);
|
||
updateDialog.show();
|
||
|
||
DataLogUtils.uploadUpgradeLog(mContext, "notice"); //上传更新通知弹窗数据
|
||
}
|
||
|
||
private void showDownloadDialog(String md5) {
|
||
downloadDialog = new Dialog(mContext);
|
||
|
||
View view = View.inflate(mContext, R.layout.app_updating_dialog, null);
|
||
|
||
app_pb_progress = view.findViewById(R.id.app_pb_progress);
|
||
app_tv_speed = view.findViewById(R.id.app_tv_speed);
|
||
app_tv_percent = view.findViewById(R.id.app_tv_percent);
|
||
|
||
view.findViewById(R.id.app_tv_cancel).setOnClickListener(v -> {
|
||
DownloadManager.getInstance(mContext).cancel(appEntity.getUrl());
|
||
if (appEntity.isForce()) {
|
||
AppManager.getInstance().finishAllActivity();
|
||
} else {
|
||
downloadDialog.dismiss();
|
||
}
|
||
});
|
||
|
||
downloadDialog.setOnDismissListener(dialog -> {
|
||
DownloadManager.getInstance(mContext).removeObserver(dataWatcher);
|
||
isShowDownload = false;
|
||
});
|
||
|
||
downloadDialog.setCanceledOnTouchOutside(false);
|
||
downloadDialog.setCancelable(false);
|
||
downloadDialog.closeOptionsMenu();
|
||
downloadDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
downloadDialog.setContentView(view);
|
||
downloadDialog.show();
|
||
|
||
isShowDownload = true;
|
||
|
||
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.");
|
||
}
|
||
|
||
DownloadEntity downloadEntity = new DownloadEntity();
|
||
downloadEntity.setUrl(appEntity.getUrl());
|
||
downloadEntity.setName("光环助手V" + appEntity.getVersion());
|
||
downloadEntity.setPath(path);
|
||
downloadEntity.setPlatform("官方版");
|
||
downloadEntity.setPackageName(mContext.getPackageName());
|
||
DownloadManager.getInstance(mContext).cancel(downloadEntity.getUrl(), false);
|
||
DownloadManager.getInstance(mContext).pauseAll();
|
||
DownloadManager.getInstance(mContext).add(downloadEntity);
|
||
}
|
||
|
||
}
|