任务组增加文件大小设置api

pull/330/head
AriaLyy 7 years ago
parent 23f063edf2
commit 219275bf23
  1. 21
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  2. 18
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  3. 10
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  4. 1
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java
  5. 3
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java
  6. 2
      build.gradle

@ -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)}进行下载操作那么你需要设置任务组的下载地址
*/

@ -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,9 +112,19 @@ abstract class AbsGroupUtil implements IUtil {
mCurrentLocation += file.exists() ? entity.getCurrentProgress() : 0;
mActualTaskNum++;
}
if (isNeedLoadFileSize) {
mTotalSize += entity.getFileSize();
}
}
updateFileSize();
}
void updateFileSize() {
if (isNeedLoadFileSize) {
mTaskEntity.getEntity().setFileSize(mTotalSize);
mTaskEntity.getEntity().update();
}
}
@Override public long getFileSize() {
return mTotalSize;

@ -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) {
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();
}
}
};

@ -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));
}

@ -74,7 +74,8 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
.setDownloadDirPath(
Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_3")
.setGroupAlias("任务组测试")
.setSubTaskFileName(getModule(GroupModule.class).getSubName())
.setSubFileName(getModule(GroupModule.class).getSubName())
.setFileSize(32895492)
.start();
break;
case R.id.stop:

@ -37,7 +37,7 @@ task clean(type: Delete) {
ext {
userOrg = 'arialyy'
groupId = 'com.arialyy.aria'
publishVersion = '3.2.20'
publishVersion = '3.2.21_dev_2'
repoName='maven'
desc = 'android 下载框架'
website = 'https://github.com/AriaLyy/Aria'

Loading…
Cancel
Save