316 lines
14 KiB
Java
316 lines
14 KiB
Java
package com.gh.gamecenter.manager;
|
||
|
||
import android.app.Dialog;
|
||
import android.content.*;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
import android.text.Html;
|
||
import android.view.View;
|
||
import android.view.Window;
|
||
import android.widget.*;
|
||
import com.gh.base.AppController;
|
||
import com.gh.common.constant.Config;
|
||
import com.gh.common.util.*;
|
||
import com.gh.download.*;
|
||
import com.gh.gamecenter.R;
|
||
import com.gh.gamecenter.entity.AppEntity;
|
||
import com.gh.gamecenter.entity.GameUpdateEntity;
|
||
import com.gh.gamecenter.retrofit.Response;
|
||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||
import retrofit2.HttpException;
|
||
import rx.android.schedulers.AndroidSchedulers;
|
||
import rx.functions.Func1;
|
||
import rx.schedulers.Schedulers;
|
||
|
||
import java.io.File;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.Date;
|
||
import java.util.Locale;
|
||
|
||
/**
|
||
* Created by LGT on 2016/12/8.
|
||
* 助手更新 工具类
|
||
*/
|
||
public class UpdateManager {
|
||
|
||
private Context context;
|
||
|
||
private AppEntity appEntity;
|
||
|
||
private Dialog downloadDialog;
|
||
private ProgressBar app_pb_progress;
|
||
private TextView app_tv_speed;
|
||
private TextView app_tv_percent;
|
||
|
||
private Dialog loadingDialog;
|
||
|
||
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(context).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(context, "网络错误,请稍后重试");
|
||
} else if (DownloadStatus.timeout.equals(downloadEntity.getStatus())) {
|
||
Utils.toast(context, "请求超时,请稍后重试");
|
||
} else if (DownloadStatus.notfound.equals(downloadEntity.getStatus())) {
|
||
Utils.toast(context, "下载链接异常,请稍后重试");
|
||
} else if (DownloadStatus.hijack.equals(downloadEntity.getStatus())) {
|
||
Utils.toast(context, "网络劫持,请稍后重试");
|
||
}
|
||
}
|
||
}
|
||
};
|
||
|
||
private UpdateManager(Context context) {
|
||
this.context = 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 isCheck, final Handler handler) {
|
||
if (isChecking) {
|
||
return;
|
||
}
|
||
isChecking = true;
|
||
if (!isCheck) {
|
||
loadingDialog = DialogUtils.showWaitDialog(context, "检查更新中...");
|
||
}
|
||
String channel = AppController.getInstance().getChannel();
|
||
RetrofitManager.getApi().getUpdate(PackageUtils.getVersionName(context), channel)
|
||
.map(new Func1<AppEntity, String>() {
|
||
@Override
|
||
public String call(AppEntity appEntity) {
|
||
String md5 = null;
|
||
|
||
float version = Float.valueOf(appEntity.getVersion());
|
||
float currentVersion = Float.valueOf(PackageUtils.getVersionName(context));
|
||
if (version > currentVersion) {
|
||
// 助手有更新
|
||
UpdateManager.this.appEntity = appEntity;
|
||
|
||
// 添加到更新列表
|
||
GameUpdateEntity gameUpdateEntity = new GameUpdateEntity();
|
||
gameUpdateEntity.setName("光环助手V" + appEntity.getVersion());
|
||
gameUpdateEntity.setPackageName(context.getPackageName());
|
||
gameUpdateEntity.setSize(appEntity.getSize());
|
||
gameUpdateEntity.setVersion(appEntity.getVersion());
|
||
gameUpdateEntity.setUrl(appEntity.getUrl());
|
||
gameUpdateEntity.setPlatform("官方版");
|
||
gameUpdateEntity.setId("5618b86e8ab49e17088b4575");
|
||
PackageManager.addUpdate(0, gameUpdateEntity);
|
||
|
||
if (isCheck) {
|
||
if (appEntity.isForce()) {
|
||
// 强制更新
|
||
md5 = MD5Utils.getUpdateMD5(appEntity.getUrl(), appEntity.getContent());
|
||
} else {
|
||
// 非强制更新
|
||
if ("EVERY_TIME_OPEN".equals(appEntity.getAlert())) {
|
||
// 每次都提示
|
||
md5 = MD5Utils.getUpdateMD5(appEntity.getUrl(), appEntity.getContent());
|
||
} else if (!"NEVER".equals(appEntity.getAlert())) {
|
||
// 一天提示一次
|
||
SharedPreferences sp = context.getSharedPreferences(
|
||
Config.PREFERENCE, Context.MODE_PRIVATE);
|
||
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());
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
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 (!isCheck) {
|
||
Toast.makeText(context, "已是最新版本", Toast.LENGTH_SHORT).show();
|
||
if (handler != null) {
|
||
handler.sendEmptyMessage(1);
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(HttpException e) {
|
||
isChecking = false;
|
||
if (loadingDialog != null) {
|
||
loadingDialog.dismiss();
|
||
}
|
||
if (!isCheck) {
|
||
if (handler != null) {
|
||
handler.sendEmptyMessage(1);
|
||
}
|
||
if (e != null && e.code() == 304) {
|
||
Toast.makeText(context, "您的光环助手已是最新版本", Toast.LENGTH_SHORT).show();
|
||
return;
|
||
}
|
||
|
||
Toast.makeText(context, "检查更新失败", Toast.LENGTH_SHORT).show();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
// 显示助手有更新提示框
|
||
private void showUpdateDialog(final String md5) {
|
||
final Dialog updateDialog = new Dialog(context);
|
||
|
||
View view = View.inflate(context, R.layout.app_update_hint_dialog, null);
|
||
|
||
TextView content = (TextView) view.findViewById(R.id.updeta_content);
|
||
content.setText(Html.fromHtml(appEntity.getContent()));
|
||
|
||
TextView versison = (TextView) view.findViewById(R.id.update_app_version);
|
||
versison.setText(String.format("光环助手V%s更新内容:", appEntity.getVersion()));
|
||
|
||
TextView size = (TextView) view.findViewById(R.id.update_app_size);
|
||
size.setText(String.format("大小:%s", appEntity.getSize()));
|
||
|
||
view.findViewById(R.id.update_cancel).setOnClickListener(
|
||
new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
if (appEntity.isForce()) {
|
||
AppManager.getInstance().finishAllActivity();
|
||
} else {
|
||
updateDialog.dismiss();
|
||
}
|
||
}
|
||
});
|
||
|
||
view.findViewById(R.id.update_confirm).setOnClickListener(
|
||
new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
updateDialog.dismiss();
|
||
String path = FileUtils.getDownloadPath(context,
|
||
"光环助手V" + appEntity.getVersion() + "_" + md5 + ".apk");
|
||
File file = new File(path);
|
||
if (file.exists() && file.length() > 0) {
|
||
DataLogUtils.uploadUpgradeLog(context, "install"); //上传更新安装数据
|
||
context.startActivity(PackageUtils.getInstallIntent(context, path));
|
||
} else {
|
||
DataUtils.onEvent(context, "软件更新", "下载开始");
|
||
showDownloadDialog(md5);
|
||
}
|
||
}
|
||
});
|
||
|
||
if (appEntity.isForce()) {
|
||
updateDialog.setCanceledOnTouchOutside(false);
|
||
updateDialog.setCancelable(false);
|
||
}
|
||
updateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
updateDialog.setContentView(view);
|
||
updateDialog.show();
|
||
|
||
DataLogUtils.uploadUpgradeLog(context, "notice"); //上传更新通知弹窗数据
|
||
}
|
||
|
||
private void showDownloadDialog(String md5) {
|
||
downloadDialog = new Dialog(context);
|
||
|
||
View view = View.inflate(context, R.layout.app_updating_dialog, null);
|
||
|
||
app_pb_progress = (ProgressBar) view.findViewById(R.id.app_pb_progress);
|
||
app_tv_speed = (TextView) view.findViewById(R.id.app_tv_speed);
|
||
app_tv_percent = (TextView) view.findViewById(R.id.app_tv_percent);
|
||
|
||
view.findViewById(R.id.app_tv_cancel).setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
DownloadManager.getInstance(context).cancel(appEntity.getUrl());
|
||
if (appEntity.isForce()) {
|
||
AppManager.getInstance().finishAllActivity();
|
||
} else {
|
||
downloadDialog.dismiss();
|
||
}
|
||
}
|
||
});
|
||
|
||
downloadDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
||
@Override
|
||
public void onDismiss(DialogInterface dialog) {
|
||
DownloadManager.getInstance(context).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(context).addObserver(dataWatcher);
|
||
|
||
// 添加到下载列表
|
||
String path = FileUtils.getDownloadPath(context, "光环助手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(context.getPackageName());
|
||
DownloadManager.getInstance(context).cancel(downloadEntity.getUrl(), false);
|
||
DownloadManager.getInstance(context).pauseAll();
|
||
DownloadManager.getInstance(context).add(downloadEntity);
|
||
}
|
||
|
||
}
|