|
@@ -0,0 +1,242 @@
|
|
|
+package com.cooleshow.base.utils.helper;
|
|
|
+
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import com.cooleshow.base.bean.DownloadTaskBean;
|
|
|
+import com.liulishuo.filedownloader.BaseDownloadTask;
|
|
|
+import com.liulishuo.filedownloader.FileDownloadListener;
|
|
|
+import com.liulishuo.filedownloader.FileDownloadQueueSet;
|
|
|
+import com.liulishuo.filedownloader.FileDownloadSampleListener;
|
|
|
+import com.liulishuo.filedownloader.FileDownloader;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Author by pq, Date on 2023/1/11.
|
|
|
+ */
|
|
|
+public class DownloadHelper {
|
|
|
+ public static final int DOWNLOAD_STATUS_CALLBACK_ING = 1;//下载中
|
|
|
+ public static final int DOWNLOAD_STATUS_CALLBACK_COMPLETE = 2;//下载完成
|
|
|
+ public static final int DOWNLOAD_STATUS_CALLBACK_ERROR = 3;//下载错误
|
|
|
+ public static final String TAG = "DownloadHelper";
|
|
|
+ private volatile static DownloadHelper instance;
|
|
|
+ private HashMap<String, ArrayList<Integer>> downloadIds;
|
|
|
+ private OnEventListener mEventListener;
|
|
|
+
|
|
|
+ private DownloadHelper() {
|
|
|
+ downloadIds = new HashMap<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static DownloadHelper getInstance() {
|
|
|
+ if (instance == null) {
|
|
|
+ synchronized (DownloadHelper.class) {
|
|
|
+ if (instance == null) {
|
|
|
+ instance = new DownloadHelper();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void stop() {
|
|
|
+ FileDownloader.getImpl().pauseAll();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void startDownload(String idKey, ArrayList<DownloadTaskBean> downloadUrls, OnEventListener onEventListener) {
|
|
|
+ if (downloadUrls == null || downloadUrls.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.mEventListener = onEventListener;
|
|
|
+ final List<BaseDownloadTask> tasks = new ArrayList<>();
|
|
|
+ final ArrayList<Integer> taskIds = new ArrayList<>();
|
|
|
+ Log.i("qwa","downloadUrls:"+downloadUrls.size());
|
|
|
+ for (int i = 0; i < downloadUrls.size(); i++) {
|
|
|
+ DownloadTaskBean downloadTaskBean = downloadUrls.get(i);
|
|
|
+ BaseDownloadTask task1 = FileDownloader.getImpl().create(downloadTaskBean.getUrl()).setPath(downloadTaskBean.getSavePath(), false);
|
|
|
+ tasks.add(task1);
|
|
|
+ taskIds.add(task1.getId());
|
|
|
+ }
|
|
|
+ getDownloadTaskInfos().put(idKey, taskIds);
|
|
|
+ start_multi(tasks);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void start_multi(List<BaseDownloadTask> tasks) {
|
|
|
+ downloadListener = createLis();
|
|
|
+ //(1) 创建 FileDownloadQueueSet
|
|
|
+ final FileDownloadQueueSet queueSet = new FileDownloadQueueSet(downloadListener);
|
|
|
+ //(3) 设置参数
|
|
|
+
|
|
|
+ // 每个任务的进度 无回调
|
|
|
+ //queueSet.disableCallbackProgressTimes();
|
|
|
+ // do not want each task's download progress's callback,we just consider which task will completed.
|
|
|
+
|
|
|
+ queueSet.setCallbackProgressTimes(1000);
|
|
|
+ queueSet.setCallbackProgressMinInterval(100);
|
|
|
+ //失败 重试次数
|
|
|
+ queueSet.setAutoRetryTimes(3);
|
|
|
+
|
|
|
+ //避免掉帧
|
|
|
+ FileDownloader.enableAvoidDropFrame();
|
|
|
+
|
|
|
+ //(4)串行下载
|
|
|
+ queueSet.downloadSequentially(tasks);
|
|
|
+
|
|
|
+ //(5)任务启动
|
|
|
+ queueSet.start();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 多任务下载
|
|
|
+ private FileDownloadListener downloadListener;
|
|
|
+
|
|
|
+ public FileDownloadListener createLis() {
|
|
|
+ return new FileDownloadSampleListener() {
|
|
|
+ @Override
|
|
|
+ protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
+ if (task.getListener() != downloadListener) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Log.d(TAG, "pending taskId:" + task.getId() + ",fileName:" + task.getFilename() + ",soFarBytes:" + soFarBytes + ",totalBytes:" + totalBytes + ",percent:" + soFarBytes * 1.0 / totalBytes);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
+ if (task.getListener() != downloadListener) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Log.d(TAG, "progress taskId:" + task.getId() + ",fileName:" + task.getFilename() + ",soFarBytes:" + soFarBytes + ",totalBytes:" + totalBytes + ",percent:" + soFarBytes * 1.0 / totalBytes + ",speed:" + task.getSpeed());
|
|
|
+
|
|
|
+ HashMap<String, ArrayList<Integer>> downloadTaskInfos = getDownloadTaskInfos();
|
|
|
+ Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
|
+ Log.i("progress", "downloadTaskInfos:" + downloadTaskInfos.size());
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ String key = iterator.next();
|
|
|
+ ArrayList<Integer> integers = downloadTaskInfos.get(key);
|
|
|
+ Log.i("progress", "integers size:" + integers.size());
|
|
|
+ float maxProgress = 100f / integers.size();
|
|
|
+ Log.i("maxProgress", "maxProgress:" + maxProgress);
|
|
|
+ int id = task.getId();
|
|
|
+ Log.i("progress", "taskId:" + id);
|
|
|
+ int i = integers.indexOf(id);
|
|
|
+ Log.i("progress", "integers indexof:" + i);
|
|
|
+ if (i != -1) {
|
|
|
+ float value = maxProgress * (soFarBytes * 1.0f / totalBytes);
|
|
|
+ int progress = (int) (maxProgress * (i) + value);
|
|
|
+ if (mEventListener != null) {
|
|
|
+ Log.i("progress", "progress:" + progress);
|
|
|
+ mEventListener.onProgress(key, progress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void blockComplete(BaseDownloadTask task) {
|
|
|
+ if (task.getListener() != downloadListener) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Log.d(TAG, "blockComplete taskId:" + task.getId() + ",filePath:" + task.getPath() + ",fileName:" + task.getFilename() + ",speed:" + task.getSpeed() + ",isReuse:" + task.reuse());
|
|
|
+ HashMap<String, ArrayList<Integer>> downloadTaskInfos = getDownloadTaskInfos();
|
|
|
+ Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
|
+ Log.i("blockComplete", "downloadTaskInfos:" + downloadTaskInfos.size());
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ String key = iterator.next();
|
|
|
+ ArrayList<Integer> integers = downloadTaskInfos.get(key);
|
|
|
+ Log.i("blockComplete", "integers size:" + integers.size());
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ for (int i = 0; i < integers.size(); i++) {
|
|
|
+ stringBuilder.append(integers.get(i)).append(",");
|
|
|
+ }
|
|
|
+ Log.i("blockComplete", "integers:" + stringBuilder);
|
|
|
+ float totalProgress = 100f;
|
|
|
+ int id = task.getId();
|
|
|
+ Log.i("blockComplete", "taskId:" + id);
|
|
|
+ int i = integers.indexOf(id);
|
|
|
+ Log.i("blockComplete", "integers indexof:" + i);
|
|
|
+ if (i != -1) {
|
|
|
+ float progress;
|
|
|
+ if (i != integers.size() - 1) {
|
|
|
+ progress = (i + 1) * (totalProgress / integers.size());
|
|
|
+ } else {
|
|
|
+ progress = 100;
|
|
|
+ getDownloadTaskInfos().clear();
|
|
|
+ if (mEventListener != null) {
|
|
|
+ mEventListener.onComplete(task.getPath());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (mEventListener != null) {
|
|
|
+ Log.i("blockComplete", "progress:" + progress);
|
|
|
+ mEventListener.onProgress(key, (int) progress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void completed(BaseDownloadTask task) {
|
|
|
+ if (task.getListener() != downloadListener) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (mEventListener != null) {
|
|
|
+ mEventListener.onComplete(task.getPath());
|
|
|
+ }
|
|
|
+ Log.d(TAG, "completed taskId:" + task.getId() + ",isReuse:" + task.reuse());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
|
|
|
+ if (task.getListener() != downloadListener) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Log.d(TAG, "paused taskId:" + task.getId() + ",soFarBytes:" + soFarBytes + ",totalBytes:" + totalBytes + ",percent:" + soFarBytes * 1.0 / totalBytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void error(BaseDownloadTask task, Throwable e) {
|
|
|
+ if (task.getListener() != downloadListener) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (mEventListener != null) {
|
|
|
+ HashMap<String, ArrayList<Integer>> downloadTaskInfos = getDownloadTaskInfos();
|
|
|
+ Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ mEventListener.downloadError(iterator.next());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FileDownloader.getImpl().clearAllTaskData();
|
|
|
+ getDownloadTaskInfos().clear();
|
|
|
+ Log.d(TAG, "error taskId:" + task.getId() + ",e:" + e.getLocalizedMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void warn(BaseDownloadTask task) {
|
|
|
+ if (task.getListener() != downloadListener) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Log.d(TAG, "warn taskId:" + task.getId());
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ public void replaceCallBack(OnEventListener onEventListener) {
|
|
|
+ this.mEventListener = onEventListener;
|
|
|
+ }
|
|
|
+
|
|
|
+ public HashMap<String, ArrayList<Integer>> getDownloadTaskInfos() {
|
|
|
+ if (downloadIds == null) {
|
|
|
+ downloadIds = new HashMap<>();
|
|
|
+ }
|
|
|
+ return downloadIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface OnEventListener {
|
|
|
+ void onProgress(String id, int progress);
|
|
|
+
|
|
|
+ void downloadError(String id);
|
|
|
+
|
|
|
+ void onComplete(String filePath);
|
|
|
+ }
|
|
|
+}
|