任务组bug 修复

pull/330/head
AriaLyy 7 years ago
parent 041df07617
commit b8a86890a7
  1. 4
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  2. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DListener.java
  3. 46
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  4. 3
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  5. 8
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java
  6. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java
  7. 4
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/StateConstance.java
  8. 6
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java
  9. 15
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  10. 11
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  11. 11
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java
  12. 13
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  13. 11
      Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java
  14. 3
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java
  15. 22
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java
  16. 12
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadExecutePool.java
  17. 43
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/DownloadSharePool.java
  18. 4
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/IPool.java
  19. 28
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadExecutePool.java
  20. 43
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/UploadSharePool.java
  21. 7
      Aria/src/main/java/com/arialyy/aria/orm/DbEntity.java
  22. 7
      Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java
  23. 21
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  24. 12
      app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java
  25. 4
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java
  26. 10
      app/src/main/java/com/arialyy/simple/download/group/GroupModule.java
  27. 31
      app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java
  28. 92
      app/src/main/java/com/arialyy/simple/widget/SubStateLinearLayout.java
  29. 12
      app/src/main/res/layout/item_group_download.xml
  30. 8
      app/src/main/res/layout/layout_child_state.xml
  31. 20
      app/src/main/res/values/strings.xml

@ -63,7 +63,9 @@ class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
if (mod.equals(QueueMod.NOW.getTag())) { if (mod.equals(QueueMod.NOW.getTag())) {
mQueue.startTask(task); mQueue.startTask(task);
} else if (mod.equals(QueueMod.WAIT.getTag())) { } else if (mod.equals(QueueMod.WAIT.getTag())) {
if (mQueue.getCurrentExePoolNum() < maxTaskNum || task.getState() == IEntity.STATE_STOP) { if (mQueue.getCurrentExePoolNum() < maxTaskNum
|| task.getState() == IEntity.STATE_STOP
|| task.getState() == IEntity.STATE_COMPLETE) {
mQueue.startTask(task); mQueue.startTask(task);
} }
} }

@ -128,6 +128,12 @@ final class DListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
private void saveData(int state, long location) { private void saveData(int state, long location) {
if (state == IEntity.STATE_CANCEL) { if (state == IEntity.STATE_CANCEL) {
entity.deleteData(); entity.deleteData();
} else if (state == IEntity.STATE_COMPLETE) {
entity.setState(state);
entity.setComplete(true);
entity.setCompleteTime(System.currentTimeMillis());
entity.setCurrentProgress(entity.getFileSize());
entity.update();
} else { } else {
entity.setState(state); entity.setState(state);
if (location != -1) { if (location != -1) {

@ -18,6 +18,7 @@ package com.arialyy.aria.core.download;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsGroupTarget; import com.arialyy.aria.core.inf.AbsGroupTarget;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.DbUtil;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.File; import java.io.File;
@ -131,6 +132,7 @@ public class DownloadGroupTarget
throw new NullPointerException("任务组文件夹保存路径不能为null"); throw new NullPointerException("任务组文件夹保存路径不能为null");
} }
isSetDirPathed = true;
if (mEntity.getDirPath().equals(groupDirPath)) return this; if (mEntity.getDirPath().equals(groupDirPath)) return this;
File file = new File(groupDirPath); File file = new File(groupDirPath);
@ -148,12 +150,11 @@ public class DownloadGroupTarget
mEntity.setSubTasks(createSubTask()); mEntity.setSubTasks(createSubTask());
} }
mEntity.update(); mEntity.update();
isSetDirPathed = true;
return this; return this;
} }
/** /**
* 改变任务组文件夹路径 * 改变任务组文件夹路径修改文件夹路径会将子任务所有路径更换
* *
* @param newDirPath 新的文件夹路径 * @param newDirPath 新的文件夹路径
*/ */
@ -161,8 +162,17 @@ public class DownloadGroupTarget
List<DownloadEntity> subTask = mEntity.getSubTask(); List<DownloadEntity> subTask = mEntity.getSubTask();
if (subTask != null && !subTask.isEmpty()) { if (subTask != null && !subTask.isEmpty()) {
for (DownloadEntity entity : subTask) { for (DownloadEntity entity : subTask) {
File file = new File(entity.getDownloadPath()); String oldPath = entity.getDownloadPath();
file.renameTo(new File(newDirPath, entity.getFileName())); String newPath = newDirPath + "/" + entity.getFileName();
File file = new File(oldPath);
file.renameTo(new File(newPath));
DbEntity.exeSql("UPDATE DownloadEntity SET downloadPath='"
+ newPath
+ "' WHERE downloadPath='"
+ oldPath
+ "'");
DbEntity.exeSql(
"UPDATE DownloadTaskEntity SET key='" + newPath + "' WHERE key='" + oldPath + "'");
} }
} else { } else {
mEntity.setSubTasks(createSubTask()); mEntity.setSubTasks(createSubTask());
@ -170,9 +180,9 @@ public class DownloadGroupTarget
} }
/** /**
* 设置子任务文件名该方法如果{@link #setDownloadDirPath(String)}调用则不生效 * 设置子任务文件名该方法必须{@link #setDownloadDirPath(String)}调用则不生效
*/ */
public DownloadGroupTarget setSubtaskFileName(List<String> subTaskFileName) { public DownloadGroupTarget setSubTaskFileName(List<String> subTaskFileName) {
if (subTaskFileName == null || subTaskFileName.isEmpty()) return this; if (subTaskFileName == null || subTaskFileName.isEmpty()) return this;
mSubTaskFileName.addAll(subTaskFileName); mSubTaskFileName.addAll(subTaskFileName);
if (mUrls.size() != subTaskFileName.size()) { if (mUrls.size() != subTaskFileName.size()) {
@ -182,13 +192,33 @@ public class DownloadGroupTarget
List<DownloadEntity> entities = mEntity.getSubTask(); List<DownloadEntity> entities = mEntity.getSubTask();
int i = 0; int i = 0;
for (DownloadEntity entity : entities) { for (DownloadEntity entity : entities) {
entity.setFileName(mSubTaskFileName.get(i)); String newName = mSubTaskFileName.get(i);
entity.update(); updateSubFileName(entity, newName);
i++;
} }
} }
return this; return this;
} }
/**
* 更新子任务文件名
*/
private void updateSubFileName(DownloadEntity entity, String newName) {
if (!newName.equals(entity.getFileName())) {
String oldPath = mEntity.getDirPath() + "/" + entity.getFileName();
String newPath = mEntity.getDirPath() + "/" + newName;
File file = new File(oldPath);
if (file.exists()) {
file.renameTo(new File(newPath));
}
DbEntity.exeSql(
"UPDATE DownloadTaskEntity SET key='" + newPath + "' WHERE key='" + oldPath + "'");
entity.setDownloadPath(newPath);
entity.setFileName(newName);
entity.update();
}
}
/** /**
* 创建子任务 * 创建子任务
*/ */

@ -180,7 +180,8 @@ public class DownloadReceiver extends AbsReceiver {
* 获取普通下载任务列表 * 获取普通下载任务列表
*/ */
@Override public List<DownloadEntity> getSimpleTaskList() { @Override public List<DownloadEntity> getSimpleTaskList() {
return DownloadEntity.findDatas(DownloadEntity.class, "isGroupChild=?", "false"); return DownloadEntity.findDatas(DownloadEntity.class, "isGroupChild=? and downloadPath!=''",
"false");
} }
/** /**

@ -105,6 +105,7 @@ public class DownloadGroupUtil implements IDownloadUtil {
if (entity.isComplete() && file.exists()) { if (entity.isComplete() && file.exists()) {
mTotalSize += entity.getFileSize(); mTotalSize += entity.getFileSize();
mCompleteNum++; mCompleteNum++;
mInitNum++;
} else { } else {
mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity)); mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity));
} }
@ -376,7 +377,12 @@ public class DownloadGroupUtil implements IDownloadUtil {
private void saveData(int state, long location) { private void saveData(int state, long location) {
entity.setState(state); entity.setState(state);
entity.setComplete(state == IEntity.STATE_COMPLETE); entity.setComplete(state == IEntity.STATE_COMPLETE);
entity.setCurrentProgress(location); if (entity.isComplete()) {
entity.setCompleteTime(System.currentTimeMillis());
entity.setCurrentProgress(entity.getFileSize());
} else {
entity.setCurrentProgress(location);
}
entity.update(); entity.update();
} }
} }

@ -93,11 +93,13 @@ class Downloader implements Runnable, IDownloadUtil {
try { try {
if (!mTaskEntity.isSupportBP) { if (!mTaskEntity.isSupportBP) {
mThreadNum = 1; mThreadNum = 1;
mConstance.THREAD_NUM = mThreadNum;
handleNoSupportBreakpointDownload(); handleNoSupportBreakpointDownload();
} else { } else {
mThreadNum = isNewTask ? (mEntity.getFileSize() <= SUB_LEN ? 1 mThreadNum = isNewTask ? (mEntity.getFileSize() <= SUB_LEN ? 1
: AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum()) : AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum())
: mRealThreadNum; : mRealThreadNum;
mConstance.THREAD_NUM = mThreadNum;
mFixedThreadPool = Executors.newFixedThreadPool(mThreadNum); mFixedThreadPool = Executors.newFixedThreadPool(mThreadNum);
handleBreakpoint(); handleBreakpoint();
} }
@ -298,7 +300,6 @@ class Downloader implements Runnable, IDownloadUtil {
entity.CONFIG_FILE_PATH = mConfigFile.getPath(); entity.CONFIG_FILE_PATH = mConfigFile.getPath();
entity.IS_SUPPORT_BREAK_POINT = mTaskEntity.isSupportBP; entity.IS_SUPPORT_BREAK_POINT = mTaskEntity.isSupportBP;
entity.DOWNLOAD_TASK_ENTITY = mTaskEntity; entity.DOWNLOAD_TASK_ENTITY = mTaskEntity;
mConstance.THREAD_NUM = mThreadNum;
SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity); SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity);
mTask.put(i, task); mTask.put(i, task);
} }
@ -390,7 +391,6 @@ class Downloader implements Runnable, IDownloadUtil {
entity.CONFIG_FILE_PATH = mConfigFile.getPath(); entity.CONFIG_FILE_PATH = mConfigFile.getPath();
entity.IS_SUPPORT_BREAK_POINT = mTaskEntity.isSupportBP; entity.IS_SUPPORT_BREAK_POINT = mTaskEntity.isSupportBP;
entity.DOWNLOAD_TASK_ENTITY = mTaskEntity; entity.DOWNLOAD_TASK_ENTITY = mTaskEntity;
mConstance.THREAD_NUM = mThreadNum;
SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity); SingleThreadTask task = new SingleThreadTask(mConstance, mListener, entity);
mTask.put(0, task); mTask.put(0, task);
mFixedThreadPool.execute(task); mFixedThreadPool.execute(task);

@ -45,10 +45,6 @@ final class StateConstance {
FAIL_NUM = 0; FAIL_NUM = 0;
} }
void setThreadNum(int threadNum) {
THREAD_NUM = threadNum;
}
/** /**
* 所有子线程是否都已经停止下载 * 所有子线程是否都已经停止下载
*/ */

@ -20,8 +20,6 @@ package com.arialyy.aria.core.inf;
*/ */
public abstract class AbsNormalTask<ENTITY extends AbsEntity> extends AbsTask<ENTITY> { public abstract class AbsNormalTask<ENTITY extends AbsEntity> extends AbsTask<ENTITY> {
private boolean isHeighestTask = false;
/** /**
* 暂停任务并让任务处于等待状态 * 暂停任务并让任务处于等待状态
*/ */
@ -42,7 +40,5 @@ public abstract class AbsNormalTask<ENTITY extends AbsEntity> extends AbsTask<EN
isHeighestTask = isHighestPriority; isHeighestTask = isHighestPriority;
} }
public boolean isHighestPriorityTask() {
return isHeighestTask;
}
} }

@ -33,6 +33,17 @@ public abstract class AbsTask<ENTITY extends AbsEntity> implements ITask<ENTITY>
private String mTargetName; private String mTargetName;
protected Context mContext; protected Context mContext;
protected boolean isHeighestTask = false;
/**
* 任务是否完成
*
* @return {@code true} 已经完成{@code false} 未完成
*/
public boolean isComplete() {
return mEntity.isComplete();
}
/** /**
* 获取当前下载进度 * 获取当前下载进度
*/ */
@ -154,4 +165,8 @@ public abstract class AbsTask<ENTITY extends AbsEntity> implements ITask<ENTITY>
@Override public void setTargetName(String targetName) { @Override public void setTargetName(String targetName) {
this.mTargetName = targetName; this.mTargetName = targetName;
} }
public boolean isHighestPriorityTask() {
return isHeighestTask;
}
} }

@ -35,9 +35,18 @@ import java.util.Set;
abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity> abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity>
implements ITaskQueue<TASK, TASK_ENTITY, ENTITY> { implements ITaskQueue<TASK, TASK_ENTITY, ENTITY> {
private final String TAG = "AbsTaskQueue"; private final String TAG = "AbsTaskQueue";
BaseCachePool<TASK> mCachePool = new BaseCachePool<>(); BaseCachePool<TASK> mCachePool;
BaseExecutePool<TASK> mExecutePool; BaseExecutePool<TASK> mExecutePool;
AbsTaskQueue() {
mCachePool = setCachePool();
mExecutePool = setExecutePool();
}
abstract BaseCachePool<TASK> setCachePool();
abstract BaseExecutePool<TASK> setExecutePool();
@Override public boolean taskIsRunning(String key) { @Override public boolean taskIsRunning(String key) {
return mExecutePool.getTask(key) != null; return mExecutePool.getTask(key) != null;
} }

@ -22,7 +22,9 @@ import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.queue.pool.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool;
import com.arialyy.aria.core.queue.pool.DownloadSharePool;
import com.arialyy.aria.core.scheduler.DQueueMapping; import com.arialyy.aria.core.scheduler.DQueueMapping;
import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers; import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers;
@ -46,7 +48,14 @@ public class DownloadGroupTaskQueue
} }
private DownloadGroupTaskQueue() { private DownloadGroupTaskQueue() {
mExecutePool = new BaseExecutePool<>(true); }
@Override BaseCachePool<DownloadGroupTask> setCachePool() {
return DownloadSharePool.getInstance().cachePool;
}
@Override BaseExecutePool<DownloadGroupTask> setExecutePool() {
return DownloadSharePool.getInstance().executePool;
} }
@Override public DownloadGroupTask createTask(String targetName, DownloadGroupTaskEntity entity) { @Override public DownloadGroupTask createTask(String targetName, DownloadGroupTaskEntity entity) {

@ -22,7 +22,9 @@ import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.queue.pool.DownloadExecutePool; import com.arialyy.aria.core.queue.pool.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool;
import com.arialyy.aria.core.queue.pool.DownloadSharePool;
import com.arialyy.aria.core.scheduler.DQueueMapping; import com.arialyy.aria.core.scheduler.DQueueMapping;
import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@ -48,7 +50,14 @@ public class DownloadTaskQueue
} }
private DownloadTaskQueue() { private DownloadTaskQueue() {
mExecutePool = new DownloadExecutePool(true); }
@Override BaseCachePool<DownloadTask> setCachePool() {
return DownloadSharePool.getInstance().cachePool;
}
@Override BaseExecutePool<DownloadTask> setExecutePool() {
return DownloadSharePool.getInstance().executePool;
} }
@Override public String getKey(DownloadEntity entity) { @Override public String getKey(DownloadEntity entity) {

@ -19,7 +19,9 @@ package com.arialyy.aria.core.queue;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.queue.pool.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool;
import com.arialyy.aria.core.queue.pool.UploadSharePool;
import com.arialyy.aria.core.scheduler.UploadSchedulers; import com.arialyy.aria.core.scheduler.UploadSchedulers;
import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.core.upload.UploadTask;
@ -43,7 +45,14 @@ public class UploadTaskQueue extends AbsTaskQueue<UploadTask, UploadTaskEntity,
} }
private UploadTaskQueue() { private UploadTaskQueue() {
mExecutePool = new BaseExecutePool<>(false); }
@Override BaseCachePool<UploadTask> setCachePool() {
return UploadSharePool.getInstance().cachePool;
}
@Override BaseExecutePool<UploadTask> setExecutePool() {
return UploadSharePool.getInstance().executePool;
} }
@Override public String getKey(UploadEntity entity) { @Override public String getKey(UploadEntity entity) {

@ -19,6 +19,7 @@ package com.arialyy.aria.core.queue.pool;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.util.HashMap; import java.util.HashMap;
@ -32,7 +33,7 @@ import java.util.concurrent.TimeUnit;
* Created by lyy on 2016/8/14. * Created by lyy on 2016/8/14.
* 任务缓存池所有下载任务最先缓存在这个池中 * 任务缓存池所有下载任务最先缓存在这个池中
*/ */
public class BaseCachePool<TASK extends ITask> implements IPool<TASK> { public class BaseCachePool<TASK extends AbsTask> implements IPool<TASK> {
private static final String TAG = "BaseCachePool"; private static final String TAG = "BaseCachePool";
private static final int MAX_NUM = Integer.MAX_VALUE; //最大下载任务数 private static final int MAX_NUM = Integer.MAX_VALUE; //最大下载任务数
private static final long TIME_OUT = 1000; private static final long TIME_OUT = 1000;

@ -19,6 +19,7 @@ package com.arialyy.aria.core.queue.pool;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.scheduler.DQueueMapping; import com.arialyy.aria.core.scheduler.DQueueMapping;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -31,23 +32,28 @@ import java.util.concurrent.TimeUnit;
* Created by lyy on 2016/8/15. * Created by lyy on 2016/8/15.
* 任务执行池所有当前下载任务都该任务池中默认下载大小为2 * 任务执行池所有当前下载任务都该任务池中默认下载大小为2
*/ */
public class BaseExecutePool<TASK extends ITask> implements IPool<TASK> { public class BaseExecutePool<TASK extends AbsTask> implements IPool<TASK> {
private final String TAG = "BaseExecutePool"; private final String TAG = "BaseExecutePool";
final long TIME_OUT = 1000; final long TIME_OUT = 1000;
ArrayBlockingQueue<TASK> mExecuteQueue; ArrayBlockingQueue<TASK> mExecuteQueue;
Map<String, TASK> mExecuteMap; Map<String, TASK> mExecuteMap;
protected int mSize; int mSize;
public BaseExecutePool(boolean isDownload) { BaseExecutePool() {
if (isDownload) { mSize = getMaxSize();
mSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum();
} else {
mSize = AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum();
}
mExecuteQueue = new ArrayBlockingQueue<>(mSize); mExecuteQueue = new ArrayBlockingQueue<>(mSize);
mExecuteMap = new HashMap<>(); mExecuteMap = new HashMap<>();
} }
/**
* 获取最大任务数配置
*
* @return {@link AriaManager#getDownloadConfig()} {@link AriaManager#getUploadConfig()}如果不设置默认返回2
*/
protected int getMaxSize() {
return 2;
}
/** /**
* 获取所有正在执行的任务 * 获取所有正在执行的任务
*/ */

@ -17,7 +17,7 @@ package com.arialyy.aria.core.queue.pool;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -26,14 +26,14 @@ import java.util.concurrent.TimeUnit;
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
* 单个下载任务的执行池 * 单个下载任务的执行池
*/ */
public class DownloadExecutePool extends BaseExecutePool<DownloadTask> { public class DownloadExecutePool<TASK extends AbsTask> extends BaseExecutePool<TASK> {
private final String TAG = "DownloadExecutePool"; private final String TAG = "DownloadExecutePool";
public DownloadExecutePool(boolean isDownload) { @Override protected int getMaxSize() {
super(isDownload); return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum();
} }
@Override public boolean putTask(DownloadTask task) { @Override public boolean putTask(TASK task) {
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
if (task == null) { if (task == null) {
Log.e(TAG, "任务不能为空!!"); Log.e(TAG, "任务不能为空!!");
@ -62,7 +62,7 @@ public class DownloadExecutePool extends BaseExecutePool<DownloadTask> {
@Override boolean pollFirstTask() { @Override boolean pollFirstTask() {
try { try {
DownloadTask oldTask = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS); TASK oldTask = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS);
if (oldTask == null) { if (oldTask == null) {
Log.e(TAG, "移除任务失败"); Log.e(TAG, "移除任务失败");
return false; return false;

@ -0,0 +1,43 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.queue.pool;
import com.arialyy.aria.core.AriaManager;
/**
* Created by Aria.Lao on 2017/7/17.
* 下载任务池该池子为简单任务和任务组共用
*/
public class DownloadSharePool {
private static volatile DownloadSharePool INSTANCE;
public DownloadExecutePool executePool;
public BaseCachePool cachePool;
private DownloadSharePool() {
executePool = new DownloadExecutePool<>();
cachePool = new BaseCachePool<>();
}
public static DownloadSharePool getInstance() {
if (INSTANCE == null) {
synchronized (AriaManager.LOCK) {
INSTANCE = new DownloadSharePool();
}
}
return INSTANCE;
}
}

@ -16,13 +16,13 @@
package com.arialyy.aria.core.queue.pool; package com.arialyy.aria.core.queue.pool;
import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTask;
/** /**
* Created by lyy on 2016/8/14. * Created by lyy on 2016/8/14.
* 任务池 * 任务池
*/ */
interface IPool<T extends ITask> { interface IPool<T extends AbsTask> {
/** /**
* 将下载任务添加到任务池中 * 将下载任务添加到任务池中
*/ */

@ -0,0 +1,28 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.queue.pool;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsTask;
/**
* Created by Aria.Lao on 2017/7/17.
*/
public class UploadExecutePool<TASK extends AbsTask> extends BaseExecutePool<TASK> {
@Override protected int getMaxSize() {
return AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum();
}
}

@ -0,0 +1,43 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.queue.pool;
import com.arialyy.aria.core.AriaManager;
/**
* Created by Aria.Lao on 2017/7/17.
* 下载任务池该池子为简单任务和任务组共用
*/
public class UploadSharePool {
private static volatile UploadSharePool INSTANCE;
public UploadExecutePool executePool;
public BaseCachePool cachePool;
private UploadSharePool() {
executePool = new UploadExecutePool();
cachePool = new BaseCachePool<>();
}
public static UploadSharePool getInstance() {
if (INSTANCE == null) {
synchronized (AriaManager.LOCK) {
INSTANCE = new UploadSharePool();
}
}
return INSTANCE;
}
}

@ -36,6 +36,13 @@ public class DbEntity {
} }
/**
* 直接执行sql语句
*/
public static void exeSql(String sql) {
DbUtil.getInstance().exeSql(sql);
}
/** /**
* 查询所有数据 * 查询所有数据
* *

@ -64,6 +64,13 @@ public class DbUtil {
return INSTANCE; return INSTANCE;
} }
/**
* 执行sql语句
*/
void exeSql(String sql) {
mDb.execSQL(sql);
}
/** /**
* 删除某条数据 * 删除某条数据
*/ */

@ -541,9 +541,11 @@ public class CommonUtil {
} }
/** /**
* 创建文件 当文件不存在的时候就创建一个文件否则直接返回文件 * 创建文件
* 当文件不存在的时候就创建一个文件
* 如果文件存在先删除原文件然后重新创建一个新文件
*/ */
public static File createFile(String path) { public static void createFile(String path) {
File file = new File(path); File file = new File(path);
if (!file.getParentFile().exists()) { if (!file.getParentFile().exists()) {
Log.d(TAG, "目标文件所在路径不存在,准备创建……"); Log.d(TAG, "目标文件所在路径不存在,准备创建……");
@ -552,19 +554,18 @@ public class CommonUtil {
} }
} }
// 创建目标文件 // 创建目标文件
if (file.exists()) {
final File to = new File(file.getAbsolutePath() + System.currentTimeMillis());
file.renameTo(to);
to.delete();
}
try { try {
if (!file.exists()) { if (file.createNewFile()) {
if (file.createNewFile()) { Log.d(TAG, "创建文件成功:" + file.getAbsolutePath());
Log.d(TAG, "创建文件成功:" + file.getAbsolutePath());
}
return file;
} else {
return file;
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null;
} }
/** /**

@ -60,6 +60,14 @@ public abstract class AbsRVAdapter<T, Holder extends AbsHolder>
bindData(holder, position, mData.get(position)); bindData(holder, position, mData.get(position));
} }
@Override public void onBindViewHolder(Holder holder, int position, List<Object> payloads) {
if (payloads == null || payloads.isEmpty()) {
bindData(holder, position, mData.get(position));
} else {
bindData(holder, position, mData.get(position), payloads);
}
}
public Context getContext() { public Context getContext() {
return mContext; return mContext;
} }
@ -74,4 +82,8 @@ public abstract class AbsRVAdapter<T, Holder extends AbsHolder>
protected abstract int setLayoutId(int type); protected abstract int setLayoutId(int type);
protected abstract void bindData(Holder holder, int position, T item); protected abstract void bindData(Holder holder, int position, T item);
protected void bindData(Holder holder, int position, T item, List<Object> payloads) {
}
} }

@ -69,7 +69,9 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
case R.id.start: case R.id.start:
Aria.download(this) Aria.download(this)
.load(mUrls) .load(mUrls)
.setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test") .setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_3")
.setGroupAlias("任务组测试")
.setSubTaskFileName(getModule(GroupModule.class).getSubName())
.start(); .start();
break; break;
case R.id.stop: case R.id.stop:

@ -32,12 +32,18 @@ public class GroupModule extends BaseModule {
List<String> getUrls() { List<String> getUrls() {
List<String> urls = new ArrayList<>(); List<String> urls = new ArrayList<>();
String[] str = getContext().getResources().getStringArray(R.array.download_url); String[] str = getContext().getResources().getStringArray(R.array.group_urls);
//String[] str = getContext().getResources().getStringArray(R.array.group_urls);
Collections.addAll(urls, str); Collections.addAll(urls, str);
return urls; return urls;
} }
List<String> getSubName(){
List<String> names = new ArrayList<>();
String[] str = getContext().getResources().getStringArray(R.array.group_names);
Collections.addAll(names, str);
return names;
}
//NormalList<String> convertPath(NormalList<String> urls){ //NormalList<String> convertPath(NormalList<String> urls){
// NormalList<String> paths = new ArrayList<>(); // NormalList<String> paths = new ArrayList<>();
// //

@ -21,6 +21,7 @@ import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import butterknife.Bind; import butterknife.Bind;
import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.Aria;
@ -111,6 +112,9 @@ public class DownloadAdapter extends AbsRVAdapter<AbsEntity, DownloadAdapter.Sim
} }
} }
/**
* 更新进度
*/
public synchronized void setProgress(AbsEntity entity) { public synchronized void setProgress(AbsEntity entity) {
String url = entity.getKey(); String url = entity.getKey();
int position = indexItem(url); int position = indexItem(url);
@ -119,7 +123,7 @@ public class DownloadAdapter extends AbsRVAdapter<AbsEntity, DownloadAdapter.Sim
} }
mData.set(position, entity); mData.set(position, entity);
notifyItemChanged(position); notifyItemChanged(position, entity);
} }
private synchronized int indexItem(String url) { private synchronized int indexItem(String url) {
@ -136,6 +140,24 @@ public class DownloadAdapter extends AbsRVAdapter<AbsEntity, DownloadAdapter.Sim
handleProgress(holder, item); handleProgress(holder, item);
} }
@Override protected void bindData(SimpleHolder holder, int position, AbsEntity item,
List<Object> payloads) {
AbsEntity entity = (AbsEntity) payloads.get(0);
updateSpeed(holder, entity);
}
/**
* 只更新速度
*/
private void updateSpeed(SimpleHolder holder, final AbsEntity entity) {
long size = entity.getFileSize();
long progress = entity.getCurrentProgress();
int current = size == 0 ? 0 : (int) (progress * 100 / size);
holder.speed.setText(entity.getConvertSpeed());
holder.fileSize.setText(covertCurrentSize(progress) + "/" + CommonUtil.formatFileSize(size));
holder.progress.setProgress(current);
}
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private void handleProgress(SimpleHolder holder, final AbsEntity entity) { private void handleProgress(SimpleHolder holder, final AbsEntity entity) {
String str = ""; String str = "";
@ -189,6 +211,11 @@ public class DownloadAdapter extends AbsRVAdapter<AbsEntity, DownloadAdapter.Sim
}); });
} }
private void handleSubChild(GroupHolder holder, final AbsEntity entity){
if (holder.childList.getVisibility() == View.GONE) return;
}
private boolean isSimpleDownload(AbsEntity entity) { private boolean isSimpleDownload(AbsEntity entity) {
return entity instanceof DownloadEntity; return entity instanceof DownloadEntity;
} }
@ -260,7 +287,7 @@ public class DownloadAdapter extends AbsRVAdapter<AbsEntity, DownloadAdapter.Sim
} }
class GroupHolder extends SimpleHolder { class GroupHolder extends SimpleHolder {
@Bind(R.id.child_list) NoScrollListView childList; @Bind(R.id.child_list) LinearLayout childList;
GroupHolder(View itemView) { GroupHolder(View itemView) {
super(itemView); super(itemView);

@ -0,0 +1,92 @@
package com.arialyy.simple.widget;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.simple.R;
import java.util.LinkedList;
import java.util.List;
/**
* Created by Aria.Lao on 2017/7/17.
*/
public class SubStateLinearLayout extends LinearLayout {
List<DownloadEntity> mSubData = new LinkedList<>();
public SubStateLinearLayout(Context context) {
super(context);
}
public SubStateLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SubStateLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void addData(List<DownloadEntity> datas) {
removeAllViews();
mSubData.clear();
mSubData.addAll(datas);
createShowView();
int i = 1;
for (DownloadEntity entity : datas) {
TextView view = createView(entity);
addView(view, i);
i++;
}
}
public void update(DownloadEntity entity) {
int position = mSubData.indexOf(entity) + 1;
if (position != 0) {
((TextView) getChildAt(position)).setText(entity.getFileName() + ": " + getPercent(entity));
}
}
private TextView createView(DownloadEntity entity) {
TextView view =
(TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_child_state, null);
view.setText(entity.getFileName() + ": " + getPercent(entity));
return view;
}
private void createShowView() {
TextView view =
(TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_child_state, null);
view.setText("点击显示子任务");
view.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
if (getVisibility() == GONE) {
showChild(true);
((TextView) v).setText("点击隐藏子任务");
} else {
showChild(false);
((TextView) v).setText("点击显示子任务");
}
}
});
addView(view, 0);
}
private void showChild(boolean show) {
for (int i = 1, count = getChildCount(); i < count; i++) {
getChildAt(i).setVisibility(show ? VISIBLE : GONE);
}
}
private int getPercent(DownloadEntity entity) {
long size = entity.getFileSize();
long progress = entity.getCurrentProgress();
int current = size == 0 ? 0 : (int) (progress * 100 / size);
return current;
}
}

@ -4,15 +4,23 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
> >
<include layout="@layout/layout_item_progress" <include
layout="@layout/layout_item_progress"
android:id="@+id/include" android:id="@+id/include"
/> />
<com.arialyy.simple.widget.NoScrollListView
<LinearLayout
android:id="@+id/child_list" android:id="@+id/child_list"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/include" android:layout_below="@+id/include"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
/> />
</RelativeLayout> </RelativeLayout>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:textColor="@color/black"
android:textSize="20sp"
/>

@ -47,17 +47,15 @@
</string-array> </string-array>
<string-array name="group_urls"> <string-array name="group_urls">
<item>http://img.sc115.com/uploads/allimg/110420/20110420225600154.jpg</item> <item>https://res5.d.cn/5a6a3384c1b2be1a65d84b914e6a6fef697637578b6db2eb1056d50b09cf1dcf289d4045df7ef95746e498e3d6a848ab84c89b77aa60194e2c48e5a7cb748265.apk</item>
<item>http://img05.tooopen.com/images/20160121/tooopen_sy_155168162826.jpg</item> <item>https://res5.d.cn/5a6a3384c1b2be1a52034c72752e8475414630ebc69318b84ef584115ebf5eaaab945ae07b7fe3596afc72a7940ff328d4a9553f6ae92d6c09ba4bfb533137f6.apk</item>
<item>http://img03.tooopen.com/images/20130811/tooopen_15265353.jpg</item> <item>https://res5.d.cn/5a6a3384c1b2be1a426f06bfc69034d69c44ae1a01da180cab8e59bd1a5e1a784bac46ba0c64579d14f0e80a4ce4f068af89b0369a393456f4f449a8829cad5c.apk</item>
<!--<item></item>--> </string-array>
<!--<item></item>-->
<!--<item></item>--> <string-array name="group_names">
<!--<item></item>--> <item>王者荣耀.apk</item>
<!--<item></item>--> <item>战斗吧剑灵.apk</item>
<!--<item></item>--> <item>天魔幻想.apk</item>
<!--<item></item>-->
<!--<item></item>-->
</string-array> </string-array>
</resources> </resources>

Loading…
Cancel
Save