基于第三方框架update-app,这个工具类有待完善,这里只是保存一下
compile 'com.qianwen:update-app:3.5.2'
import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.vector.update_app.HttpManager;
import com.vector.update_app.UpdateAppBean;
import com.vector.update_app.UpdateAppManager;
import com.vector.update_app.UpdateCallback;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import cn.dongchen.bigsteward.library_common.R;
import cn.dongchen.bigsteward.library_common.gson.VersionGson;
import cn.dongchen.bigsteward.library_common.network.RetrofitUtils;
import cn.dongchen.bigsteward.library_common.network.result.BaseData;
import cn.dongchen.bigsteward.library_common.permission.PermissionCallBack;
import cn.dongchen.bigsteward.library_common.permission.PermissionsUtils;
import cn.dongchen.bigsteward.library_common.rx.BaseObserver;
import cn.dongchen.bigsteward.library_common.rx.BaseRxJava;
import cn.dongchen.bigsteward.library_common.toast.ToastUtils;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
/**
* @author fada
* 下载工具类
*/
public class UpdateAppHttpUtil implements HttpManager {
/**
* 更新版本
*
* @param activity
*/
public static void updateVersion(Activity activity, boolean needToast) {
new PermissionsUtils
.Build()
.permissions(PermissionsUtils.PERMISSION_WRITE_EXTERNAL_STORAGE)
.message("允许应用程序写入外部存储")
.permissionCallBack(new PermissionCallBack() {
@Override
public void granted() {
update(activity, needToast);
}
@Override
public void refuse() {
ToastUtils.getInstance().showText("您拒绝了权限,无法继续更新");
}
@Override
public void noAgain() {
ToastUtils.getInstance().showText("您拒绝了权限,无法继续更新");
}
})
.build()
.getPermission(activity);
}
/**
* 更新app
*
* @param activity
* @param needToast
*/
private static void update(Activity activity, boolean needToast) {
Map<String, String> params = new HashMap<>(2);
params.put("versionNum", SystemUtils.getVersionName(activity));
params.put("type", "0");
new UpdateAppManager
.Builder()
//当前Activity
.setActivity(activity)
//更新地址
.setUpdateUrl("http://122.225.55.70:9880/Butler/checkVersionApp")
.setParams(params)
//实现httpManager接口的对象
.setHttpManager(new UpdateAppHttpUtil())
//设置主体色
.setThemeColor(activity.getResources().getColor(R.color.colorAccent))
//设置头图片
.setTopPic(R.drawable.update_dada)
.build()
.checkNewApp(new UpdateCallback() {
@Override
protected UpdateAppBean parseJson(String json) {
UpdateAppBean updateAppBean = new UpdateAppBean();
if (TextUtils.isEmpty(json)) {
updateAppBean.setUpdate("No");
if (needToast) {
ToastUtils.getInstance().showText("已经是最新版了");
}
return updateAppBean;
}
VersionGson version = GsonUtils.getInstance().fromJson(json,
VersionGson.class);
//是否需要更新
updateAppBean.setUpdate("Yes");
//文件地址
updateAppBean.setApkFileUrl(version.getUploadUrl());
//新版本号
updateAppBean.setNewVersion(version.getVersionNum());
//强制更新
updateAppBean.setConstraint(version.getNeedForceUpdate() == 0);
//设置内容
updateAppBean.setUpdateLog(version.getUpdateContent());
return updateAppBean;
}
@Overr 大专栏 UpdateAppHttpUtilside
protected void hasNewApp(UpdateAppBean updateApp, UpdateAppManager
updateAppManager) {
super.hasNewApp(updateApp, updateAppManager);
}
@Override
protected void onAfter() {
super.onAfter();
}
@Override
protected void noNewApp(String error) {
super.noNewApp(error);
}
@Override
protected void onBefore() {
super.onBefore();
}
});
}
/**
* 异步get
*
* @param url get请求地址
* @param params get参数
* @param callBack 回调
*/
@Override
public void asyncGet(@NonNull String url, @NonNull Map<String, String> params, @NonNull final
Callback callBack) {
Observable<BaseData<VersionGson>> observable = RetrofitUtils
.getInstance()
.getServerices()
.checkVersion(params);
BaseRxJava.bindRx(observable, new BaseObserver<BaseData<VersionGson>>() {
@Override
public void onError(String error) {
callBack.onError(error);
}
@Override
public void onNext(BaseData<VersionGson> versionGsonBaseData) {
VersionGson data = versionGsonBaseData.getData();
if (data != null && versionGsonBaseData.getCode() != 0) {
callBack.onResponse(data.toString());
} else {
callBack.onResponse("");
}
}
@Override
public void onComplete() {
}
});
}
/**
* 异步post
*
* @param url post请求地址
* @param params post请求参数
* @param callBack 回调
*/
@Override
public void asyncPost(@NonNull String url, @NonNull Map<String, String> params, @NonNull
final Callback callBack) {
}
/**
* 下载
*
* @param url 下载地址
* @param path 文件保存路径
* @param fileName 文件名称
* @param callback 回调
*/
@Override
public void download(@NonNull String url, @NonNull String path, @NonNull String fileName,
@NonNull final FileCallback callback) {
LogUtils.d("download() called with: url = [" + url + "], path = [" + path + "], fileName " +
"= [" + fileName + "], callback = [" + callback + "]");
callback.onBefore();
RetrofitUtils
.getInstance()
.getServerices()
.downloadFile(url)
.subscribeOn(Schedulers.io())
.map(responseBody -> {
File file = new File(path);
//目录是否存在,不存在则创建
if (!file.exists()) {
file.mkdir();
}
//创建文件
file = new File(file, fileName);
//文件读写
byte[] bytes = new byte[1024 * 1024];
int len = 0;
final long total = responseBody.contentLength();
long sum = 0;
FileOutputStream outputStream = new FileOutputStream(file);
InputStream inputStream = responseBody.byteStream();
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
sum += len;
final float progress = sum * 1.0f / total;
RetrofitUtils.getInstance().getRetrofit().callbackExecutor().execute(() -> {
callback.onProgress(progress, total);
});
}
//关闭流
outputStream.close();
inputStream.close();
return file;
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new BaseObserver<File>() {
@Override
public void onError(String error) {
callback.onError(error);
}
@Override
public void onNext(File file) {
callback.onResponse(file);
}
@Override
public void onComplete() {
}
});
}
}