Files
pdlivexp/common/src/main/java/com/yunbao/common/utils/APKDownloadUtil.java

186 lines
6.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.yunbao.common.utils;
import android.app.Activity;
import android.app.Dialog;
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.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.core.content.FileProvider;
import com.yunbao.common.R;
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;
public class APKDownloadUtil {
/**
* APK文件下载
*
* @param url
*/
public void downloadAPK(Activity context, String url, 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();
}
showDialod(context);
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());
progressDialog.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() {
tvProgress.setText(progress + "%");
progressBar.setProgress(progress);
}
});
}
outputStream.flush();
//启动安装app
installApk(context, downloadFile, context.getPackageName() + ".fileprovider");
handler.post(() -> progressDialog.dismiss());
} catch (Exception e) {
e.printStackTrace();
new Handler().post(() -> {
listener.updateFailure(-1, e.getMessage());
progressDialog.dismiss();
});
}
}
});
}
/**
* 更新监听
*/
public interface OnUpdateListener {
void updateFailure(int code, String error);
void onProgress(int progress);
}
//
// /**
// * 启动app安装
// *
// * @param context
// * @param filePath
// */
// private static void startInstall(Context context, File filePath) {
//
// Intent install = new Intent(Intent.ACTION_VIEW);
// install.setDataAndType(Uri.parse("file://" + filePath.getAbsolutePath()),
// "application/vnd.android.package-archive");
// install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(install);
// }
/**
* 安装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;
}
public TextView tvProgress;
public ProgressBar progressBar;
public Dialog progressDialog;
private void showDialod(Activity context) {
if (progressDialog == null) {
progressDialog = new Dialog(context, R.style.dialog);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_progress, null);
tvProgress = view.findViewById(R.id.tvProgress);
progressBar = view.findViewById(R.id.progressBar);
progressDialog.setContentView(view);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
Window window = progressDialog.getWindow();// 这部分是设置dialog宽高的宽高是我从sharedpreferences里面获取到的。之前程序启动的时候有获取
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = width / 4 * 3;
window.setAttributes(lp);
}
progressDialog.setCancelable(false);
progressDialog.show();
}
}