**
* 版本更新工具类
*
* @author liujianang
*/
public class VersionUtils {
@SuppressLint("StaticFieldLeak")
private static Activity context;
private boolean isFromSplash;
private boolean isFromMain;
private static final int sleepTime = 500;
/**
* 下载apk文件进度显示
*/
private ProgressDialog progressDialog;
public VersionUtils(Activity context, boolean isFromSplash, boolean isFromMain) {
VersionUtils.context = context;
this.isFromSplash = isFromSplash;
this.isFromMain = isFromMain;
}
/**
* @param context
* @return 返回客户端版本名称
*/
@SuppressWarnings("JavaDoc")
public static String getLocalVersionName(Context context) {
String versionName = null;
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
versionName = info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
/**
* @param context
* @return 返回客户端版本号,如果获取不到则返回-1
*/
@SuppressWarnings("JavaDoc")
public static int getLocalVersionCode(Context context) {
int versionCode = -1;
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
versionCode = info.versionCode;
} catch (Exception e) {
e.printStackTrace();
}
return versionCode;
}
/**
* 检查版本
*/
public void checkVersion() {
new PgyUpdateManager.Builder()
.setForced(true) //设置是否强制更新,非自定义回调更新接口此方法有用
.setUserCanRetry(false) //失败后是否提示重新下载,非自定义下载 apk 回调此方法有用
.setDeleteHistroyApk(false) // 检查更新前是否删除本地历史 Apk
.setUpdateManagerListener(new UpdateManagerListener() {
@Override
public void onNoUpdateAvailable() {
if (isFromSplash) {
// 如果不需要更新并且在加载界面则走正常进入app的流程
intoApplication();
} else if (isFromMain) {
} else {
// 不在加载界面才会提示
ToastUtil.showShort("当前已是最新版本");
}
}
@Override
public void onUpdateAvailable(AppBean appBean) {
// 将新版本信息封装到AppBean中
String content = "检测到新版本,立即升级?";
if (!TextUtils.isEmpty(appBean.getReleaseNote())) {
content = appBean.getReleaseNote();
}
UpdateDialog updateDialog = new UpdateDialog();
updateDialog.newInstance(content);
updateDialog.show(context.getFragmentManager(), "updateDialog");
updateDialog.setmListener(() -> {
updateDialog.dismiss();
progressDialog = new ProgressDialog(context, R.style.downLoadingDialogStyle);
progressDialog.setMessage("正在下载...");
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
//调用以下方法,DownloadFileListener 才有效;如果完全使用自己的下载方法,不需要设置DownloadFileListener
PgyUpdateManager.downLoadApk(appBean.getDownloadURL());
});
// final View view = LayoutInflater.from(context).inflate(R.layout.dialog_update_log, null);
// TextView tvUpdate = (TextView) view.findViewById(R.id.tv_update);
// Button button = (Button) view.findViewById(R.id.btn_updata);
// tvUpdate.setText(content);
//
// final Dialog dialog = new Dialog(context);
// try {
// int dividerID = context.getResources().getIdentifier("android:id/titleDivider", null, null);
// View divider = dialog.findViewById(dividerID);
// divider.setBackgroundColor(Color.TRANSPARENT);
// } catch (Exception e) {
// //上面的代码,是用来去除Holo主题的蓝色线条
// e.printStackTrace();
// }
// dialog.setContentView(view);
// dialog.setCancelable(false);
// dialog.setCanceledOnTouchOutside(false);
// dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
// dialog.show();
//
// button.setOnClickListener(v -> {
// dialog.dismiss();
// progressDialog = new ProgressDialog(context);
// progressDialog.setMessage("正在下载...");
// progressDialog.setIndeterminate(true);
// progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// progressDialog.setCancelable(false);
// progressDialog.setCanceledOnTouchOutside(false);
// progressDialog.show();
// //调用以下方法,DownloadFileListener 才有效;如果完全使用自己的下载方法,不需要设置DownloadFileListener
// PgyUpdateManager.downLoadApk(appBean.getDownloadURL());
// });
}
@Override
public void checkUpdateFailed(Exception e) {
//更新检测失败回调
if (isFromSplash) {
// 如果不需要更新并且在加载界面则走正常进入app的流程
intoApplication();
} else if (isFromMain) {
} else {
// 不在加载界面才会提示
ToastUtil.showShort("当前已是最新版本");
}
}
})
//注意 :下载方法调用 PgyUpdateManager.downLoadApk(appBean.getDownloadURL()); 此回调才有效
.setDownloadFileListener(new DownloadFileListener() {
// 使用蒲公英提供的下载方法,这个接口才有效。
@Override
public void downloadFailed() {
//下载失败
progressDialog.dismiss();
}
@Override
public void downloadSuccessful(Uri uri) {
progressDialog.dismiss();
// 使用蒲公英提供的安装方法提示用户 安装apk
PgyUpdateManager.installApk(uri);
}
@Override
public void onProgressUpdate(Integer... integers) {
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgress(integers[0]);
}
})
.register();
}
/**
* 进入应用
*/
public static void intoApplication() {
// 判断是否进入向导界面
int versionCodeInSP = SPUtils.getInt(MarketApp.VERSION_CODE, -1);
if (getLocalVersionCode(context) != versionCodeInSP) {
SPUtils.putBoolean(MarketApp.IS_GUIDED, false);
}
boolean guided = SPUtils.getBoolean(MarketApp.IS_GUIDED, false);
final Intent intent = new Intent();
if (context == null) {
return;
}
if (!guided) {
intent.setClass(context, GuideActivity.class);
} else {
intent.setClass(context, LoginActivity.class);
String account = SPUtils.getString(DBConstant.LOGIN_ACCOUNT);
if (!TextUtils.isEmpty(account)) {
UserDBHelper userDb = new UserDBHelper(context);
UserVo userVo = userDb.getUserInfo(account);
if (userVo != null) {
login2HuanXin();
return;
}
}
}
context.startActivity(intent);
context.finish();
context = null;
}
private static void login2HuanXin() {
DemoApplication.getThreadPoolManager().addExecuteTask(() -> {
if (DemoHelper.getInstance().isLoggedIn()) {
// auto login mode, make sure all group and conversation is loaed before enter the main screen
long start = System.currentTimeMillis();
EMClient.getInstance().chatManager().loadAllConversations();
EMClient.getInstance().groupManager().loadAllGroups();
long costTime = System.currentTimeMillis() - start;
//wait
if (sleepTime - costTime > 0) {
try {
Thread.sleep(sleepTime - costTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String topActivityName = EasyUtils.getTopActivityName(EMClient.getInstance().getContext());
if (topActivityName != null && (topActivityName.equals(VideoCallActivity.class.getName()) || topActivityName.equals(VoiceCallActivity.class.getName()))) {
// nop
// avoid main screen overlap Calling Activity
} else {
//enter main screen
if (context != null) {
context.startActivity(new Intent(context, MainActivity.class));
startGesturePassword(context);
}
}
if (context != null) {
context.finish();
context = null;
}
} else {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (context != null) {
context.startActivity(new Intent(context, LoginActivity.class));
context.finish();
context = null;
}
}
});
}
public static void startGesturePassword(Context context) {
String gesturePassword = DemoApplication.getAcaChe().getAsString(ZTConstant.GESTURE_PASSWORD);
if (!TextUtils.isEmpty(gesturePassword)) {
Intent intent2 = new Intent(context, GestureLoginActivity.class);
context.startActivity(intent2);
DemoApplication.getInstance().setFirst(true);
}
}
}