|
@@ -3,6 +3,9 @@ package com.cooleshow.base.utils.helper;
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
|
|
|
|
import com.cooleshow.base.bean.DownloadTaskBean;
|
|
import com.cooleshow.base.bean.DownloadTaskBean;
|
|
|
|
+import com.cooleshow.base.utils.LOG;
|
|
|
|
+import com.cooleshow.base.utils.ToastUtil;
|
|
|
|
+import com.cooleshow.base.utils.UiUtils;
|
|
import com.liulishuo.filedownloader.BaseDownloadTask;
|
|
import com.liulishuo.filedownloader.BaseDownloadTask;
|
|
import com.liulishuo.filedownloader.FileDownloadListener;
|
|
import com.liulishuo.filedownloader.FileDownloadListener;
|
|
import com.liulishuo.filedownloader.FileDownloadQueueSet;
|
|
import com.liulishuo.filedownloader.FileDownloadQueueSet;
|
|
@@ -21,9 +24,11 @@ public class DownloadHelper {
|
|
public static final int DOWNLOAD_STATUS_CALLBACK_ING = 1;//下载中
|
|
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_COMPLETE = 2;//下载完成
|
|
public static final int DOWNLOAD_STATUS_CALLBACK_ERROR = 3;//下载错误
|
|
public static final int DOWNLOAD_STATUS_CALLBACK_ERROR = 3;//下载错误
|
|
|
|
+
|
|
|
|
+ public static final int DOWNLOAD_STATUS_CALLBACK_CANCEL = 4;//取消下载
|
|
public static final String TAG = "DownloadHelper";
|
|
public static final String TAG = "DownloadHelper";
|
|
private volatile static DownloadHelper instance;
|
|
private volatile static DownloadHelper instance;
|
|
- private HashMap<String, ArrayList<Integer>> downloadIds;
|
|
|
|
|
|
+ private HashMap<String, ArrayList<String>> downloadIds;
|
|
private OnEventListener mEventListener;
|
|
private OnEventListener mEventListener;
|
|
|
|
|
|
private DownloadHelper() {
|
|
private DownloadHelper() {
|
|
@@ -50,14 +55,25 @@ public class DownloadHelper {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
this.mEventListener = onEventListener;
|
|
this.mEventListener = onEventListener;
|
|
|
|
+ if (getDownloadTaskInfos().size() != 0) {
|
|
|
|
+ UiUtils.postDelayed(new Runnable() {
|
|
|
|
+ @Override
|
|
|
|
+ public void run() {
|
|
|
|
+ ToastUtil.getInstance().showShort("当前已有课件资源下载中");
|
|
|
|
+ }
|
|
|
|
+ }, 0);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
final List<BaseDownloadTask> tasks = new ArrayList<>();
|
|
final List<BaseDownloadTask> tasks = new ArrayList<>();
|
|
- final ArrayList<Integer> taskIds = new ArrayList<>();
|
|
|
|
- Log.i("qwa","downloadUrls:"+downloadUrls.size());
|
|
|
|
|
|
+ final ArrayList<String> taskIds = new ArrayList<>();
|
|
|
|
+ Log.i("qwa", "downloadUrls:" + downloadUrls.size());
|
|
for (int i = 0; i < downloadUrls.size(); i++) {
|
|
for (int i = 0; i < downloadUrls.size(); i++) {
|
|
DownloadTaskBean downloadTaskBean = downloadUrls.get(i);
|
|
DownloadTaskBean downloadTaskBean = downloadUrls.get(i);
|
|
BaseDownloadTask task1 = FileDownloader.getImpl().create(downloadTaskBean.getUrl()).setPath(downloadTaskBean.getSavePath(), false);
|
|
BaseDownloadTask task1 = FileDownloader.getImpl().create(downloadTaskBean.getUrl()).setPath(downloadTaskBean.getSavePath(), false);
|
|
tasks.add(task1);
|
|
tasks.add(task1);
|
|
- taskIds.add(task1.getId());
|
|
|
|
|
|
+ String tag = task1.getId() + "_" + i;
|
|
|
|
+ task1.setTag(tag);
|
|
|
|
+ taskIds.add(tag);
|
|
}
|
|
}
|
|
getDownloadTaskInfos().put(idKey, taskIds);
|
|
getDownloadTaskInfos().put(idKey, taskIds);
|
|
start_multi(tasks);
|
|
start_multi(tasks);
|
|
@@ -109,19 +125,19 @@ public class DownloadHelper {
|
|
}
|
|
}
|
|
Log.d(TAG, "progress taskId:" + task.getId() + ",fileName:" + task.getFilename() + ",soFarBytes:" + soFarBytes + ",totalBytes:" + totalBytes + ",percent:" + soFarBytes * 1.0 / totalBytes + ",speed:" + task.getSpeed());
|
|
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();
|
|
|
|
|
|
+ HashMap<String, ArrayList<String>> downloadTaskInfos = getDownloadTaskInfos();
|
|
Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
- Log.i("progress", "downloadTaskInfos:" + downloadTaskInfos.size());
|
|
|
|
|
|
+ LOG.i("progress", "downloadTaskInfos:" + downloadTaskInfos.size());
|
|
while (iterator.hasNext()) {
|
|
while (iterator.hasNext()) {
|
|
String key = iterator.next();
|
|
String key = iterator.next();
|
|
- ArrayList<Integer> integers = downloadTaskInfos.get(key);
|
|
|
|
- Log.i("progress", "integers size:" + integers.size());
|
|
|
|
|
|
+ ArrayList<String> integers = downloadTaskInfos.get(key);
|
|
|
|
+ LOG.i("progress", "integers size:" + integers.size());
|
|
float maxProgress = 100f / integers.size();
|
|
float maxProgress = 100f / integers.size();
|
|
- Log.i("maxProgress", "maxProgress:" + maxProgress);
|
|
|
|
- int id = task.getId();
|
|
|
|
- Log.i("progress", "taskId:" + id);
|
|
|
|
|
|
+ LOG.i("maxProgress", "maxProgress:" + maxProgress);
|
|
|
|
+ String id = (String) task.getTag();
|
|
|
|
+ LOG.i("progress", "taskId:" + id);
|
|
int i = integers.indexOf(id);
|
|
int i = integers.indexOf(id);
|
|
- Log.i("progress", "integers indexof:" + i);
|
|
|
|
|
|
+ LOG.i("progress", "integers indexof:" + i);
|
|
if (i != -1) {
|
|
if (i != -1) {
|
|
float value = maxProgress * (soFarBytes * 1.0f / totalBytes);
|
|
float value = maxProgress * (soFarBytes * 1.0f / totalBytes);
|
|
int progress = (int) (maxProgress * (i) + value);
|
|
int progress = (int) (maxProgress * (i) + value);
|
|
@@ -139,12 +155,12 @@ public class DownloadHelper {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
Log.d(TAG, "blockComplete taskId:" + task.getId() + ",filePath:" + task.getPath() + ",fileName:" + task.getFilename() + ",speed:" + task.getSpeed() + ",isReuse:" + task.reuse());
|
|
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();
|
|
|
|
|
|
+ HashMap<String, ArrayList<String>> downloadTaskInfos = getDownloadTaskInfos();
|
|
Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
Log.i("blockComplete", "downloadTaskInfos:" + downloadTaskInfos.size());
|
|
Log.i("blockComplete", "downloadTaskInfos:" + downloadTaskInfos.size());
|
|
while (iterator.hasNext()) {
|
|
while (iterator.hasNext()) {
|
|
String key = iterator.next();
|
|
String key = iterator.next();
|
|
- ArrayList<Integer> integers = downloadTaskInfos.get(key);
|
|
|
|
|
|
+ ArrayList<String> integers = downloadTaskInfos.get(key);
|
|
Log.i("blockComplete", "integers size:" + integers.size());
|
|
Log.i("blockComplete", "integers size:" + integers.size());
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
for (int i = 0; i < integers.size(); i++) {
|
|
for (int i = 0; i < integers.size(); i++) {
|
|
@@ -152,7 +168,7 @@ public class DownloadHelper {
|
|
}
|
|
}
|
|
Log.i("blockComplete", "integers:" + stringBuilder);
|
|
Log.i("blockComplete", "integers:" + stringBuilder);
|
|
float totalProgress = 100f;
|
|
float totalProgress = 100f;
|
|
- int id = task.getId();
|
|
|
|
|
|
+ String id = (String) task.getTag();
|
|
Log.i("blockComplete", "taskId:" + id);
|
|
Log.i("blockComplete", "taskId:" + id);
|
|
int i = integers.indexOf(id);
|
|
int i = integers.indexOf(id);
|
|
Log.i("blockComplete", "integers indexof:" + i);
|
|
Log.i("blockComplete", "integers indexof:" + i);
|
|
@@ -192,6 +208,15 @@ public class DownloadHelper {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
Log.d(TAG, "paused taskId:" + task.getId() + ",soFarBytes:" + soFarBytes + ",totalBytes:" + totalBytes + ",percent:" + soFarBytes * 1.0 / totalBytes);
|
|
Log.d(TAG, "paused taskId:" + task.getId() + ",soFarBytes:" + soFarBytes + ",totalBytes:" + totalBytes + ",percent:" + soFarBytes * 1.0 / totalBytes);
|
|
|
|
+ Log.d(TAG, "paused taskId:" + task.getId() + ",soFarBytes:" + soFarBytes + ",totalBytes:" + totalBytes + ",percent:" + soFarBytes * 1.0 / totalBytes);
|
|
|
|
+ if (mEventListener != null) {
|
|
|
|
+ HashMap<String, ArrayList<String>> downloadTaskInfos = getDownloadTaskInfos();
|
|
|
|
+ Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ mEventListener.downloadCancel(iterator.next());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ getDownloadTaskInfos().clear();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -200,7 +225,7 @@ public class DownloadHelper {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if (mEventListener != null) {
|
|
if (mEventListener != null) {
|
|
- HashMap<String, ArrayList<Integer>> downloadTaskInfos = getDownloadTaskInfos();
|
|
|
|
|
|
+ HashMap<String, ArrayList<String>> downloadTaskInfos = getDownloadTaskInfos();
|
|
Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
Iterator<String> iterator = downloadTaskInfos.keySet().iterator();
|
|
while (iterator.hasNext()) {
|
|
while (iterator.hasNext()) {
|
|
mEventListener.downloadError(iterator.next());
|
|
mEventListener.downloadError(iterator.next());
|
|
@@ -225,7 +250,7 @@ public class DownloadHelper {
|
|
this.mEventListener = onEventListener;
|
|
this.mEventListener = onEventListener;
|
|
}
|
|
}
|
|
|
|
|
|
- public HashMap<String, ArrayList<Integer>> getDownloadTaskInfos() {
|
|
|
|
|
|
+ public HashMap<String, ArrayList<String>> getDownloadTaskInfos() {
|
|
if (downloadIds == null) {
|
|
if (downloadIds == null) {
|
|
downloadIds = new HashMap<>();
|
|
downloadIds = new HashMap<>();
|
|
}
|
|
}
|
|
@@ -237,6 +262,8 @@ public class DownloadHelper {
|
|
|
|
|
|
void downloadError(String id);
|
|
void downloadError(String id);
|
|
|
|
|
|
|
|
+ void downloadCancel(String id);
|
|
|
|
+
|
|
void onComplete(String filePath);
|
|
void onComplete(String filePath);
|
|
}
|
|
}
|
|
}
|
|
}
|