package cn.flightfeather.thirdappmodule.util.updateApp;
|
|
import android.app.Activity;
|
import android.app.DownloadManager;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageManager;
|
import android.net.Uri;
|
import android.os.Environment;
|
import android.support.v7.app.AlertDialog;
|
import android.widget.Toast;
|
|
import cn.flightfeather.thirdappmodule.CommonApplication;
|
import cn.flightfeather.thirdappmodule.R;
|
import cn.flightfeather.thirdappmodule.bean.vo.VersionVo;
|
import cn.flightfeather.thirdappmodule.httpservice.VersionService;
|
import cn.flightfeather.thirdappmodule.util.GlobalConfig;
|
import retrofit2.Call;
|
import retrofit2.Callback;
|
import retrofit2.Response;
|
|
/**
|
* @author riku
|
* app更新工具
|
*/
|
public class UpdateAppUtil {
|
|
private VersionVo versionVo;
|
private CommonApplication application;
|
private Activity activity;
|
private DownloadManager downloadManager;
|
private long downloadId;
|
|
public UpdateAppUtil(CommonApplication application, Activity activity) {
|
this.application = application;
|
this.activity = activity;
|
}
|
|
/**
|
* 监测当前app版本是否最新
|
*/
|
public void checkUpdateApp(final boolean needToast) {
|
Call<VersionVo> getLatestVersion = application.getRetrofit().create(VersionService.class).getLatestVersion();
|
getLatestVersion.enqueue(new Callback<VersionVo>() {
|
@Override
|
public void onResponse(Call<VersionVo> call, Response<VersionVo> response) {
|
if (response.body() != null) {
|
versionVo = response.body();
|
int localVersion = getLocalVersion();
|
//比较服务器和本地的版本号大小
|
if (versionVo.getVersionCode() > localVersion && versionVo.isEnable()) {
|
// TODO: 2019/2/21
|
AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
|
dialog.setMessage("有更新可用,是否更新?");
|
dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
Toast.makeText(application, R.string.downloading, Toast.LENGTH_SHORT).show();
|
downLoadApk();
|
}
|
});
|
dialog.setNegativeButton(R.string.cancel, null);
|
dialog.show();
|
} else {
|
//设置中手动检查更新时,需要提示;开启app自动检查时不需要提示
|
if (needToast) {
|
Toast.makeText(application, "已是最新版本", Toast.LENGTH_SHORT).show();
|
}
|
}
|
}
|
}
|
|
@Override
|
public void onFailure(Call<VersionVo> call, Throwable t) {
|
Toast.makeText(application, "网络连接失败", Toast.LENGTH_SHORT).show();
|
}
|
});
|
}
|
|
public int getLocalVersion() {
|
int localVersion = 0;
|
String localVersionName = "";
|
try {
|
PackageInfo packageInfo = application.getApplicationContext()
|
.getPackageManager()
|
.getPackageInfo(application.getPackageName(), 0);
|
localVersion = packageInfo.versionCode;
|
localVersionName = packageInfo.versionName;
|
GlobalConfig.getInstance().setVersionCode(localVersion);
|
GlobalConfig.getInstance().setVersionName(localVersionName);
|
} catch (PackageManager.NameNotFoundException e) {
|
e.printStackTrace();
|
}
|
return localVersion;
|
}
|
|
public void downLoadApk() {
|
try {
|
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(versionVo.getDownloadUrl()));
|
request.setTitle(application.getString(R.string.app_name));
|
request.setDescription("正在下载...");
|
request.setDestinationInExternalFilesDir(application, Environment.DIRECTORY_DOWNLOADS, "supervision.apk");
|
downloadManager = (DownloadManager) application.getSystemService(Context.DOWNLOAD_SERVICE);
|
downloadId = downloadManager.enqueue(request);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void removeDownLoad() {
|
downloadManager.remove(downloadId);
|
}
|
}
|