新增APk内置更新功能
This commit is contained in:
@@ -199,6 +199,15 @@ public class CommonHttpUtil {
|
||||
JSONArray levelArray = obj.getJSONArray("levelanchor_new");
|
||||
new NewLevelManager(context).UpAnchorDataLevel(levelArray.toJSONString());
|
||||
}
|
||||
if (obj.containsKey("apk_ver")) {
|
||||
IMLoginManager.get(context).setApkVer(obj.getString("apk_ver"));
|
||||
}
|
||||
if (obj.containsKey("apk_url")) {
|
||||
IMLoginManager.get(context).setAPKUrl(obj.getString("apk_url"));
|
||||
}
|
||||
if (obj.containsKey("apk_des")) {
|
||||
IMLoginManager.get(context).setAPKDes(obj.getString("apk_des"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String error = "info[0]:" + info[0] + "\n\n\n" + "Exception:" + e.getClass() + "---message--->" + e.getMessage();
|
||||
ErrorActivity.forward("GetConfig接口返回数据异常", error);
|
||||
|
||||
@@ -18,6 +18,7 @@ import com.yunbao.common.manager.base.BaseCacheManager;
|
||||
import com.yunbao.common.manager.imrongcloud.MessageIMManager;
|
||||
import com.yunbao.common.manager.imrongcloud.RongcloudIMManager;
|
||||
import com.yunbao.common.utils.SpUtil;
|
||||
import com.yunbao.common.utils.VersionUtil;
|
||||
import com.yunbao.common.views.floatingview.APPEasyFloat;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
@@ -39,6 +40,33 @@ public class IMLoginManager extends BaseCacheManager {
|
||||
private final String IS_HINT = "is_hint";
|
||||
private final String IS_HINT2 = "is_hint2";
|
||||
private final String SELECT_CLARITY = "selectClarity";
|
||||
private final String APK_VER = "apk_ver";
|
||||
private final String APK_URL = "apk_url";
|
||||
private final String APK_DES = "apk_des";
|
||||
|
||||
public void setAPKUrl(String apkUrl) {
|
||||
put(APK_URL, apkUrl);
|
||||
}
|
||||
|
||||
public void setAPKDes(String apkDes) {
|
||||
put(APK_DES, apkDes);
|
||||
}
|
||||
|
||||
public String getAPKUrl() {
|
||||
return getString(APK_URL);
|
||||
}
|
||||
|
||||
public String getAPKDes() {
|
||||
return getString(APK_DES);
|
||||
}
|
||||
|
||||
public void setApkVer(String apkVer) {
|
||||
put(APK_VER, apkVer);
|
||||
}
|
||||
|
||||
public boolean getApkVerNew() {
|
||||
return TextUtils.equals(VersionUtil.getVersion(), getString(APK_VER));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置清晰度
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
package com.yunbao.common.views;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
import com.lxj.xpopup.core.CenterPopupView;
|
||||
import com.yunbao.common.CommonAppConfig;
|
||||
import com.yunbao.common.R;
|
||||
import com.yunbao.common.manager.IMLoginManager;
|
||||
import com.yunbao.common.utils.APKDownloadUtil;
|
||||
import com.yunbao.common.utils.ToastUtil;
|
||||
import com.yunbao.common.views.weight.ViewClicksAntiShake;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
/**
|
||||
* apk更新弹窗
|
||||
*/
|
||||
public class APKUpdateCustomPopup extends CenterPopupView {
|
||||
private TextView updateText, versionImmediateUse;
|
||||
private LinearLayout updateLine;
|
||||
private ProgressBar progressBar;
|
||||
private Activity mContext;
|
||||
|
||||
public APKUpdateCustomPopup(@NonNull Activity context) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
// 返回自定义弹窗的布局
|
||||
@Override
|
||||
protected int getImplLayoutId() {
|
||||
return R.layout.apk_update_custom_popup;
|
||||
}
|
||||
|
||||
// 执行初始化操作,比如:findView,设置点击,或者任何你弹窗内的业务逻辑
|
||||
@Override
|
||||
protected void onCreate() {
|
||||
super.onCreate();
|
||||
initView();
|
||||
initData();
|
||||
}
|
||||
|
||||
private void initData() {
|
||||
|
||||
}
|
||||
|
||||
private void initView() {
|
||||
updateText = findViewById(R.id.update_text);
|
||||
versionImmediateUse = findViewById(R.id.version_immediate_use);
|
||||
updateLine = findViewById(R.id.update_line);
|
||||
progressBar = findViewById(R.id.progressBar);
|
||||
versionImmediateUse.setVisibility(VISIBLE);
|
||||
updateLine.setVisibility(GONE);
|
||||
updateText.setText(IMLoginManager.get(getContext()).getAPKDes());
|
||||
ViewClicksAntiShake.clicksAntiShake(versionImmediateUse, new ViewClicksAntiShake.ViewClicksCallBack() {
|
||||
@Override
|
||||
public void onViewClicks() {
|
||||
//不是谷歌
|
||||
if (!CommonAppConfig.IS_GOOGLE_PLAY) {
|
||||
versionImmediateUse.setVisibility(GONE);
|
||||
updateLine.setVisibility(VISIBLE);
|
||||
downloadAPK(mContext, IMLoginManager.get(getContext()).getAPKUrl(), new APKDownloadUtil.OnUpdateListener() {
|
||||
@Override
|
||||
public void updateFailure(int code, String error) {
|
||||
ToastUtil.show(error);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
|
||||
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.pdlive.shayu"));
|
||||
mContext.startActivity(i);
|
||||
mContext.finish();
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void downloadAPK(Activity context, String url, APKDownloadUtil.OnUpdateListener listener) {
|
||||
Request request = new Request.Builder().url(url)
|
||||
.addHeader("Accept-Encoding", "identity").build();
|
||||
File downloadFile = new File(context.getCacheDir(), "update_app.apk");
|
||||
try {
|
||||
if (!downloadFile.exists()
|
||||
&& !downloadFile.createNewFile()) {
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
new OkHttpClient().newCall(request).enqueue(new Callback() {
|
||||
private Handler handler = new Handler();
|
||||
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
// 下载失败
|
||||
handler.post(() -> {
|
||||
listener.updateFailure(-1, e.getMessage());
|
||||
dismiss();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) {
|
||||
Looper.prepare();
|
||||
byte[] buf = new byte[2048];
|
||||
int len;
|
||||
try (InputStream inputStream = response.body().byteStream();
|
||||
FileOutputStream outputStream = new FileOutputStream(downloadFile)) {
|
||||
long total = response.body().contentLength();
|
||||
long sum = 0;
|
||||
while ((len = inputStream.read(buf)) != -1) {
|
||||
outputStream.write(buf, 0, len);
|
||||
sum += len;
|
||||
int progress = (int) (sum * 1.0f / total * 100);
|
||||
// 下载中
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
progressBar.setProgress(progress);
|
||||
}
|
||||
});
|
||||
}
|
||||
outputStream.flush();
|
||||
//启动安装app
|
||||
installApk(context, downloadFile, context.getPackageName() + ".fileprovider");
|
||||
handler.post(() -> dismiss());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
new Handler().post(() -> {
|
||||
listener.updateFailure(-1, e.getMessage());
|
||||
dismiss();
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装apk
|
||||
*
|
||||
* @param context
|
||||
* @param file
|
||||
*/
|
||||
public void installApk(Context context, File file, String authority) {
|
||||
Intent intent = getInstallIntent(context, file, authority);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取安装Intent
|
||||
*
|
||||
* @param context
|
||||
* @param file
|
||||
* @param authority
|
||||
* @return
|
||||
*/
|
||||
public Intent getInstallIntent(Context context, File file, String authority) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
Uri uriData;
|
||||
String type = "application/vnd.android.package-archive";
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
uriData = FileProvider.getUriForFile(context, authority, file);
|
||||
} else {
|
||||
uriData = Uri.fromFile(file);
|
||||
}
|
||||
intent.setDataAndType(uriData, type);
|
||||
return intent;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user