348 lines
11 KiB
Java
348 lines
11 KiB
Java
package cn.shangyu.gdxzExpert.utils;
|
|
|
|
import android.Manifest;
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.os.Environment;
|
|
import android.view.View;
|
|
import android.view.View.OnClickListener;
|
|
|
|
import com.github.dfqin.grantor.PermissionListener;
|
|
import com.github.dfqin.grantor.PermissionsUtil;
|
|
import com.lidroid.xutils.HttpUtils;
|
|
import com.lidroid.xutils.exception.HttpException;
|
|
import com.lidroid.xutils.http.HttpHandler;
|
|
import com.lidroid.xutils.http.RequestParams;
|
|
import com.lidroid.xutils.http.ResponseInfo;
|
|
import com.lidroid.xutils.http.callback.RequestCallBack;
|
|
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
|
|
|
|
import java.io.File;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import cn.shangyu.gdxzExpert.BuildConfig;
|
|
import cn.shangyu.gdxzExpert.R;
|
|
import cn.shangyu.gdxzExpert.bean.VersionBean;
|
|
import cn.shangyu.gdxzExpert.http.HttpHelper;
|
|
import cn.shangyu.gdxzExpert.ui.CustomProgressDialog;
|
|
import cn.shangyu.gdxzExpert.widget.ECAlertDialog;
|
|
|
|
public class UpdateVersionUtils {
|
|
private static String des = null;// 版本描述
|
|
private static String apkurl = null;// 新版本url
|
|
private static String version;// 版本
|
|
public static CustomProgressDialog dialog;
|
|
private static ECAlertDialog buildAlert;
|
|
private static Context ct;
|
|
|
|
/**
|
|
* 检查更新
|
|
*
|
|
* @param
|
|
*/
|
|
public static void checkUpdate(Context context, final String url, final boolean isShowProgressDialog) {
|
|
ct = context;
|
|
if (0 == CommonUtil.isNetworkAvailable(context)) {
|
|
ToastUtil.showToast("无网络,请检查网络连接!");
|
|
return;
|
|
}
|
|
if (isShowProgressDialog) {
|
|
dialog = (CustomProgressDialog) DialogUtil.createProgressDialog(context, "检查更新中...");
|
|
dialog.show();
|
|
}
|
|
|
|
RequestParams params = new RequestParams();
|
|
params.addBodyParameter("appType", String.valueOf(1));// 1专家端 2患者端 必填
|
|
params.addBodyParameter("sysType", String.valueOf(1));// 1.android 2.ios
|
|
// 必填
|
|
HttpHelper.loadData(HttpMethod.POST, url, params,
|
|
new RequestCallBack<String>() {
|
|
|
|
@Override
|
|
public void onSuccess(ResponseInfo<String> arg0) {
|
|
// TODO Auto-generated method stub
|
|
LogUtil.i("数据请求成功: " + arg0.result);
|
|
checkIsUpdate(arg0.result,isShowProgressDialog);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(HttpException arg0, String arg1) {
|
|
// TODO Auto-generated method stub
|
|
if (dialog != null && dialog.isShowing()) {
|
|
dialog.dismiss();
|
|
}
|
|
LogUtil.i("检查更新失败了: " + arg1);
|
|
if (isShowProgressDialog) {
|
|
ToastUtil.showToast("检查更新失败,请稍候再试");
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private static void checkIsUpdate(String result, boolean isShowProgressDialog) {
|
|
// TODO Auto-generated method stub
|
|
if (dialog != null && dialog.isShowing()) {
|
|
dialog.dismiss();
|
|
}
|
|
VersionBean versionBean = GsonTools.fromGsonToBean(result, VersionBean.class);
|
|
String code = versionBean.code;
|
|
String message = versionBean.message;
|
|
if ("1".equals(code)) {
|
|
if (versionBean.data == null) {
|
|
if (isShowProgressDialog) {
|
|
ToastUtil.showToast("当前为最新版本无需更新");
|
|
}
|
|
return;
|
|
}
|
|
version = versionBean.data.version;
|
|
des = versionBean.data.intro;
|
|
apkurl = versionBean.data.path;
|
|
if (!StringUtil.isEmpty(version)) {
|
|
Pattern pattern = Pattern.compile("[0-9]*");
|
|
Matcher isNum = pattern.matcher(version);
|
|
if(!isNum.matches()){
|
|
if (isShowProgressDialog) {
|
|
ToastUtil.showToast("当前为最新版本无需更新");
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
if (StringUtil.isEmpty(version) || StringUtil.isEmpty(apkurl) || getVerCode() == -1 || getVerCode() >= Long.parseLong(version)) {
|
|
// 版本一致,没有新版本,
|
|
if (isShowProgressDialog) {
|
|
ToastUtil.showToast("当前为最新版本无需更新");
|
|
}
|
|
} else {
|
|
// 有新版本,弹出一升级对话框
|
|
if (getVerCode() < Long.parseLong(version)) {
|
|
showUpdateDialog();
|
|
}
|
|
// showUpdateDialog();
|
|
}
|
|
} else {
|
|
ToastUtil.showToast(message);
|
|
}
|
|
}
|
|
|
|
private static void showUpdateDialog() {
|
|
if (StringUtil.isEmpty(des)) {
|
|
des = "有新的版本,是否更新?";
|
|
}
|
|
buildAlert = ECAlertDialog.buildAlert(ct, des,
|
|
"下次再说", "后台升级", new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {// 取消
|
|
// TODO Auto-generated method stub
|
|
buildAlert.dismiss();
|
|
}
|
|
|
|
}, new DialogInterface.OnClickListener() {// 下载升级
|
|
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
// TODO Auto-generated method stub
|
|
// downloadApp 使用 Environment.getExternalStorageDirectory(),这是公共存储,需要权限
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
boolean needsPermission = Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU;
|
|
boolean hasPermission = true;
|
|
|
|
if (needsPermission) {
|
|
// Android 12及以下需要存储权限(用于保存到公共存储)
|
|
hasPermission = com.github.dfqin.grantor.PermissionsUtil.hasPermission(ct, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
|
}
|
|
// Android 13+ 保存到公共存储仍需要权限(但通常使用应用私有目录更好)
|
|
|
|
if (hasPermission) {
|
|
// 下载
|
|
downloadApp();
|
|
} else {
|
|
PermissionsUtil.requestPermission((Activity)ct, new PermissionListener() {
|
|
@Override
|
|
public void permissionGranted(@NonNull String[] permissions) {
|
|
// 下载
|
|
downloadApp();
|
|
|
|
}
|
|
|
|
@Override
|
|
public void permissionDenied(@NonNull String[] permissions) {
|
|
//用户拒绝了访问存储的申请
|
|
//
|
|
}
|
|
|
|
}, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE});
|
|
}
|
|
|
|
} else {
|
|
// 下载
|
|
downloadApp();
|
|
}
|
|
|
|
}
|
|
});
|
|
buildAlert.setTitle(R.string.app_tip);
|
|
buildAlert.setCanceledOnTouchOutside(false);
|
|
buildAlert.show();
|
|
}
|
|
|
|
private static HttpHandler<File> hand;
|
|
private static int progress = 0;
|
|
|
|
private static void downloadApp() {
|
|
// 下载apk,并且安装
|
|
if (Environment.getExternalStorageState().equals(
|
|
Environment.MEDIA_MOUNTED)) {
|
|
// sdcard存在
|
|
HttpUtils http = new HttpUtils();
|
|
//下载的路径,及app安装包的命名
|
|
//***************************************************
|
|
hand = http.download(apkurl, Environment
|
|
.getExternalStorageDirectory().getAbsolutePath()
|
|
+ "/gdxzExpert/app" + "/gdxzExpert_" + version + ".apk",
|
|
false, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
|
|
false, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
|
|
new RequestCallBack<File>() {
|
|
|
|
@Override
|
|
public void onStart() {
|
|
ToastUtil.showToast("后台下载中,下载完成自动安装。");
|
|
// 显示更新进度对话框
|
|
// updateDialog();
|
|
}
|
|
|
|
@Override
|
|
public void onLoading(long total, long current,
|
|
boolean isUploading) {
|
|
progress = (int) (current * 100 / total);
|
|
LogUtil.i("下载进度:"+progress);
|
|
// 设置进度值
|
|
// setDialogProgress(progress);
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(ResponseInfo<File> responseInfo) {
|
|
ToastUtil.showToast("下载成功!");
|
|
installAPK(responseInfo.result);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(HttpException error, String msg) {
|
|
// ToastUtil.showToast("下载失败!");
|
|
//maybe the file has downloaded completely
|
|
// if (msg.contains("maybe the file has downloaded completely")) {
|
|
//
|
|
// }
|
|
LogUtil.i("下载失败:"+error+msg);
|
|
}
|
|
});
|
|
} else {
|
|
ToastUtil.showToast("SD卡已拔出,无法下载");
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置进度 setProgress
|
|
*
|
|
* @param progress
|
|
* void
|
|
*/
|
|
private void setDialogProgress(int progress) {
|
|
// TODO Auto-generated method stub
|
|
DialogUtil.setMessage("正在下载,已完成" + progress + "%");
|
|
DialogUtil.setProgress(progress);
|
|
}
|
|
|
|
/**
|
|
* 显示下载升级进度的对话框 updateDialog
|
|
*
|
|
* void
|
|
*/
|
|
private void updateDialog() {
|
|
// TODO Auto-generated method stub
|
|
DialogUtil.getMyProDialog(ct, "取消下载", "正在下载,已完成"
|
|
+ progress + "%", new OnClickListener() {
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
// TODO Auto-generated method stub
|
|
// 调用cancel()方法停止下载
|
|
hand.cancel();
|
|
}
|
|
});
|
|
}
|
|
|
|
// 安装APK
|
|
private static void installAPK(File t) {
|
|
Intent intent = new Intent();
|
|
intent.setAction("android.intent.action.VIEW");
|
|
intent.addCategory("android.intent.category.DEFAULT");
|
|
intent.setDataAndType(Uri.fromFile(t),
|
|
"application/vnd.android.package-archive");
|
|
ct.startActivity(intent);
|
|
}
|
|
|
|
/**
|
|
* 得到应用程序的版本
|
|
*/
|
|
public static String getVersionName() {
|
|
// try {
|
|
// 管理手机的APP
|
|
// PackageManager packageManager = UIUtils.getContext().getPackageManager();
|
|
// 得到APP功能清单文件
|
|
// PackageInfo info = packageManager.getPackageInfo(UIUtils.getContext().getPackageName(), 0);
|
|
|
|
return BuildConfig.VERSION_NAME;
|
|
// } catch (NameNotFoundException e) {
|
|
// e.printStackTrace();
|
|
// }
|
|
// return null;
|
|
}
|
|
/**
|
|
* 得到应用程序的版本
|
|
*/
|
|
public static int getVerCode() {
|
|
// int verCode = -1;
|
|
// try {
|
|
// // 管理手机的APP
|
|
// PackageManager packageManager = UIUtils.getContext().getPackageManager();
|
|
// // 得到APP功能清单文件
|
|
// PackageInfo info = packageManager.getPackageInfo(UIUtils.getContext().getPackageName(), 0);
|
|
return BuildConfig.VERSION_CODE;
|
|
// } catch (NameNotFoundException e) {
|
|
// e.printStackTrace();
|
|
// }
|
|
// return verCode;
|
|
}
|
|
|
|
/**
|
|
* 判断新的版本是否大于当前版本
|
|
* */
|
|
public static boolean isUpdataInfo(String versionName,String newVersion){
|
|
String[] split = versionName.split("\\.");
|
|
String[] split1 = newVersion.split("\\.");
|
|
|
|
int i = Integer.parseInt(split[0]);
|
|
int i1 = Integer.parseInt(split1[0]);
|
|
int j = Integer.parseInt(split[1]);
|
|
int j1 = Integer.parseInt(split1[1]);
|
|
int k = Integer.parseInt(split[2]);
|
|
int k1 = Integer.parseInt(split1[2]);
|
|
if (i1>i){
|
|
return true;
|
|
}else if (j1>j&&i1>=i){
|
|
return true;
|
|
}else if (k1>k&&j1>=j&&i1>=i){
|
|
return true;
|
|
}else
|
|
return false;
|
|
}
|
|
}
|