package com.application.zhangshi_app_android.ui.dialog;
|
|
import static com.blankj.utilcode.util.StringUtils.getString;
|
|
import android.annotation.SuppressLint;
|
import android.app.DownloadManager;
|
import android.content.Context;
|
import android.database.Cursor;
|
import android.net.Uri;
|
import android.os.Environment;
|
import android.os.Handler;
|
import android.util.Log;
|
import android.view.Gravity;
|
import android.view.View;
|
import android.widget.ProgressBar;
|
import android.widget.RelativeLayout;
|
import android.widget.TextView;
|
|
import androidx.annotation.StringRes;
|
|
import com.android.app_base.base.dialog.BaseDialog;
|
import com.android.app_base.utils.Utils;
|
import com.application.zhangshi_app_android.MyApplication;
|
import com.application.zhangshi_app_android.R;
|
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
/**
|
* @author Ljj
|
* @date 2023.07.25. 16:59
|
* @desc
|
*/
|
public final class VersionUpdateDialog {
|
@SuppressWarnings("unchecked")
|
public static class Builder<B extends VersionUpdateDialog.Builder<B>>
|
extends BaseDialog.Builder<B> {
|
|
private boolean mAutoDismiss = true;
|
|
private final TextView mTitleView;
|
private final TextView mContentView;
|
private final TextView mConfirmView;
|
private final ProgressBar progressBar;
|
private final TextView tv_pro;
|
private final TextView tv_pro_percent;
|
private final RelativeLayout layout_progress;
|
|
private String downloadUrl;
|
|
public Builder(Context context) {
|
super(context);
|
setContentView(R.layout.dialog_version_update);
|
setAnimStyle(BaseDialog.ANIM_IOS);
|
setGravity(Gravity.CENTER);
|
setBackgroundDimAmount(0.5F);
|
setCanceledOnTouchOutside(true);
|
mTitleView = findViewById(R.id.tv_update_dialog_title);
|
mContentView = findViewById(R.id.tv_update_dialog_content);
|
mConfirmView = findViewById(R.id.tv_update_dialog_confirm);
|
progressBar = findViewById(R.id.pb_download);
|
tv_pro = findViewById(R.id.tv_pro);
|
tv_pro_percent = findViewById(R.id.tv_pro_percent);
|
layout_progress = findViewById(R.id.layout_progress);
|
setOnClickListener(mConfirmView);
|
}
|
|
public B setDownloadUrl(String downloadUrl) {
|
this.downloadUrl = downloadUrl;
|
return (B) this;
|
}
|
|
public B setTitle(@StringRes int id) {
|
return setTitle(getString(id));
|
}
|
|
public B setTitle(CharSequence text) {
|
mTitleView.setText(text);
|
return (B) this;
|
}
|
|
public B setContent(CharSequence text) {
|
mContentView.setText(text);
|
return (B) this;
|
}
|
|
public B setConfirm(@StringRes int id) {
|
return setConfirm(getString(id));
|
}
|
|
public B setConfirm(CharSequence text) {
|
mConfirmView.setText(text);
|
return (B) this;
|
}
|
|
public B setAutoDismiss(boolean dismiss) {
|
mAutoDismiss = dismiss;
|
return (B) this;
|
}
|
|
public void autoDismiss() {
|
if (mAutoDismiss) {
|
dismiss();
|
}
|
}
|
|
@Override
|
public void onClick(View v) {
|
if (v == mConfirmView) {
|
changeVisi(View.GONE,View.VISIBLE);
|
if (downloadUrl != null){
|
startDownLoadApp(downloadUrl);
|
}
|
}
|
}
|
|
|
@Override
|
public BaseDialog show() {
|
return super.show();
|
}
|
|
|
public long totalBytes;
|
public long downloadBytes;
|
public int dl_progress;
|
|
private Handler handler = new Handler();
|
Runnable runnable = new Runnable() {
|
@Override
|
public void run() {
|
// TODO Auto-generated method stub
|
progressBar.setProgress(dl_progress);
|
tv_pro.setText((downloadBytes * 100 / totalBytes) + "%");
|
tv_pro_percent.setText((downloadBytes * 100 / totalBytes) + "/100");
|
}
|
};
|
|
public void changeVisi(int a, int b) {
|
mConfirmView.setVisibility(a);
|
layout_progress.setVisibility(b);
|
progressBar.setProgress(0);
|
tv_pro.setText("0");
|
tv_pro_percent.setText("0/100");
|
}
|
|
/**
|
* 不涉及数据库,单独下载文件
|
*/
|
public void startDownLoadApp(String url) {
|
String fileName = "app_update";
|
//创建下载任务,downloadUrl就是下载链接
|
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//下载进行中和下载完成的通知栏是否显示
|
//用于设置下载时时候在状态栏显示通知信息
|
request.setNotificationVisibility(request.VISIBILITY_VISIBLE);
|
request.allowScanningByMediaScanner();//设置允许被扫描到
|
request.setVisibleInDownloadsUi(true);//下载的文件可以被系统的Downloads应用扫描到并管理
|
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); //指定下载路径和下载文件名
|
|
final DownloadManager downloadManager = (DownloadManager) MyApplication.getInstance().getSystemService(Context.DOWNLOAD_SERVICE);//获取下载管理器
|
final long downloadID = downloadManager.enqueue(request);//将下载任务加入下载队列,否则不会进行下载
|
|
final DownloadManager.Query query = new DownloadManager.Query();
|
query.setFilterById(downloadID);//筛选下载任务,传入任务ID,可变参数
|
|
|
final Timer mTimer = new Timer();
|
mTimer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
updateDownloadSize(query, mTimer, downloadManager, fileName);
|
}
|
}, 0, 1000);
|
}
|
|
@SuppressLint("Range")
|
private void updateDownloadSize(DownloadManager.Query query, Timer mTimer, DownloadManager downloadManager, String fileName) {
|
Cursor c = null;
|
try {
|
c = downloadManager.query(query);
|
if (c != null) {
|
if (c.moveToFirst()) {
|
int downloadBytesIdx = c.getColumnIndexOrThrow(
|
DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
|
int totalBytesIdx = c.getColumnIndexOrThrow(
|
DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
|
totalBytes = c.getLong(totalBytesIdx);
|
downloadBytes = c.getLong(downloadBytesIdx);
|
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
// 已下载的字节大小
|
final long downloadedSoFar = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
|
// 下载文件的总字节大小
|
final long totalSize = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
|
dl_progress = (int) (downloadedSoFar * 100 / totalSize);
|
switch (status) {
|
case DownloadManager.STATUS_PAUSED:
|
Log.d("APPUpdate", "下载暂停:" + fileName);
|
case DownloadManager.STATUS_PENDING:
|
Log.d("APPUpdate", "下载延迟:" + fileName);
|
case DownloadManager.STATUS_RUNNING:
|
Log.d("APPUpdate", "正在下载:" + fileName);
|
handler.postDelayed(runnable, 100);
|
break;
|
case DownloadManager.STATUS_SUCCESSFUL:
|
Log.d("APPUpdate", "下载完成:" + fileName);
|
mTimer.cancel();
|
dismiss();
|
handler=null;
|
//下载完成安装APK
|
Utils.installAPK(getContext(), fileName);
|
break;
|
case DownloadManager.STATUS_FAILED:
|
Log.d("APPUpdate", "下载失败:" + fileName);
|
setContent("下载失败,请重新下载");
|
changeVisi(View.VISIBLE,View.GONE);
|
break;
|
}
|
}
|
}
|
} finally {
|
if (c != null) {
|
c.close();
|
}
|
}
|
|
}
|
|
}
|
}
|