From 219275bf2382e4fff4740cc37015f820cff63a24 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Fri, 18 Aug 2017 18:21:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=BB=84=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=A7=E5=B0=8F=E8=AE=BE=E7=BD=AEapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/download/DownloadGroupTarget.java | 21 +++++++++++++++++++ .../download/downloader/AbsGroupUtil.java | 20 ++++++++++++++++-- .../downloader/DownloadGroupUtil.java | 12 ++++++++--- .../downloader/FtpDirDownloadUtil.java | 1 - .../download/group/DownloadGroupActivity.java | 3 ++- build.gradle | 2 +- 6 files changed, 51 insertions(+), 8 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index 1dbbf2ff..6d938325 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.download; import android.text.TextUtils; +import android.util.Log; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; @@ -59,6 +60,26 @@ public class DownloadGroupTarget mEntity = mTaskEntity.entity; } + /** + * 任务组总任务大小,任务组是一个抽象的概念,没有真实的数据实体,任务组的大小是Aria动态获取子任务大小相加而得到的, + * 如果你知道当前任务组总大小,你也可以调用该方法给任务组设置大小 + * + * 为了更好的用户体验,建议直接设置任务组文件大小 + * + * @param fileSize 任务组总大小 + */ + public DownloadGroupTarget setFileSize(long fileSize) { + if (fileSize <= 0) { + Log.w(TAG, "文件大小不能小于 0"); + return this; + } + if (mEntity.getFileSize() <= 1 || mEntity.getFileSize() != fileSize) { + mEntity.setFileSize(fileSize); + mEntity.update(); + } + return this; + } + /** * 如果你是使用{@link DownloadReceiver#load(DownloadGroupEntity)}进行下载操作,那么你需要设置任务组的下载地址 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index f91f4fc9..543f452e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -44,7 +44,7 @@ abstract class AbsGroupUtil implements IUtil { * 任务组所有任务总大小 */ long mTotalSize = 0; - protected long mCurrentLocation = 0; + long mCurrentLocation = 0; private ExecutorService mExePool; protected IDownloadGroupListener mListener; protected DownloadGroupTaskEntity mTaskEntity; @@ -83,6 +83,10 @@ abstract class AbsGroupUtil implements IUtil { private int mFailNum = 0; //实际的下载任务数 int mActualTaskNum = 0; + /** + * 是否需要读取文件长度,{@code true}需要 + */ + boolean isNeedLoadFileSize = true; AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { mListener = listener; @@ -95,6 +99,8 @@ abstract class AbsGroupUtil implements IUtil { mTasksMap.put(te.getEntity().getUrl(), te); } } + mTotalSize = taskEntity.getEntity().getFileSize(); + isNeedLoadFileSize = mTotalSize <= 1; for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { File file = new File(entity.getDownloadPath()); if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) { @@ -106,7 +112,17 @@ abstract class AbsGroupUtil implements IUtil { mCurrentLocation += file.exists() ? entity.getCurrentProgress() : 0; mActualTaskNum++; } - mTotalSize += entity.getFileSize(); + if (isNeedLoadFileSize) { + mTotalSize += entity.getFileSize(); + } + } + updateFileSize(); + } + + void updateFileSize() { + if (isNeedLoadFileSize) { + mTaskEntity.getEntity().setFileSize(mTotalSize); + mTaskEntity.getEntity().update(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index 490b45cc..b49a3189 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -91,12 +91,16 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { @Override public void onComplete(String url, int code) { DownloadTaskEntity te = mExeMap.get(url); if (te != null) { - mTotalSize += te.getEntity().getFileSize(); + if (isNeedLoadFileSize) { + mTotalSize += te.getEntity().getFileSize(); + } startChildDownload(te); } mInitNum++; - if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()) { + if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size() + || !isNeedLoadFileSize) { startRunningFlow(); + updateFileSize(); } } @@ -116,8 +120,10 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { if (mActualTaskNum < 0) mActualTaskNum = 0; } failNum++; - if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()) { + if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size() + || !isNeedLoadFileSize) { startRunningFlow(); + updateFileSize(); } } }; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java index f8c97c13..82e77f55 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java @@ -37,7 +37,6 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { new FtpDirInfoThread(mTaskEntity, new OnFileInfoCallback() { @Override public void onComplete(String url, int code) { if (code >= 200 && code < 300) { - mTotalSize = mTaskEntity.getEntity().getFileSize(); for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { mExeMap.put(entity.getUrl(), createChildDownloadTask(entity)); } diff --git a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java index cc10cb35..ff883db4 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java @@ -74,7 +74,8 @@ public class DownloadGroupActivity extends BaseActivity