217 lines
7.6 KiB
Java
217 lines
7.6 KiB
Java
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.APKManager;
|
||
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;
|
||
private boolean mInside;
|
||
|
||
public APKUpdateCustomPopup(@NonNull Activity context, boolean inside) {
|
||
super(context);
|
||
mContext = context;
|
||
mInside = inside;
|
||
}
|
||
|
||
// 返回自定义弹窗的布局
|
||
@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() {
|
||
if (!mInside) {
|
||
if (!CommonAppConfig.IS_GOOGLE_PLAY && APKManager.get().getAPKGoogleIsUp() == 1) {
|
||
findViewById(R.id.dialog_close).setVisibility(GONE);
|
||
}
|
||
if (!CommonAppConfig.IS_GOOGLE_PLAY && APKManager.get().getAPKGoogleIsUp() == 1) {
|
||
findViewById(R.id.dialog_close).setVisibility(GONE);
|
||
}
|
||
}
|
||
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(APKManager.get().getAPKDes());
|
||
ViewClicksAntiShake.clicksAntiShake(findViewById(R.id.dialog_close), new ViewClicksAntiShake.ViewClicksCallBack() {
|
||
@Override
|
||
public void onViewClicks() {
|
||
if (CommonAppConfig.IS_GOOGLE_PLAY && APKManager.get().getAPKGoogleIsUp() != 1) {
|
||
dismiss();
|
||
}
|
||
if (!CommonAppConfig.IS_GOOGLE_PLAY && APKManager.get().getApkIsUp() != 1) {
|
||
dismiss();
|
||
}
|
||
}
|
||
}
|
||
);
|
||
ViewClicksAntiShake.clicksAntiShake(versionImmediateUse, new ViewClicksAntiShake.ViewClicksCallBack() {
|
||
@Override
|
||
public void onViewClicks() {
|
||
//不是谷歌
|
||
if (!CommonAppConfig.IS_GOOGLE_PLAY) {
|
||
versionImmediateUse.setVisibility(GONE);
|
||
updateLine.setVisibility(VISIBLE);
|
||
downloadAPK(mContext, APKManager.get().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;
|
||
}
|
||
}
|