diff --git a/Aria/build.gradle b/Aria/build.gradle index 587b9ad5..7120c44d 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -26,6 +26,5 @@ dependencies { compile 'com.arialyy.aria:aria-ftp-plug:1.0.3' // compile project(':AriaFtpPlug') - } apply from: 'bintray-release.gradle' diff --git a/Aria/src/main/java/com/arialyy/aria/core/SubTaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/SubTaskManager.java index 058c713a..803f9275 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/SubTaskManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/SubTaskManager.java @@ -18,7 +18,7 @@ package com.arialyy.aria.core; import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.command.group.GroupCmdFactory; -import com.arialyy.aria.core.inf.BaseGroupTaskEntity; +import com.arialyy.aria.core.inf.AbsGroupTaskEntity; import com.arialyy.aria.util.CommonUtil; import java.util.List; @@ -28,10 +28,10 @@ import java.util.List; */ public class SubTaskManager { private String TAG = "SubTaskManager"; - private BaseGroupTaskEntity mEntity; + private AbsGroupTaskEntity mEntity; private String mTargetName; - public SubTaskManager(String targetName, BaseGroupTaskEntity entity) { + public SubTaskManager(String targetName, AbsGroupTaskEntity entity) { mTargetName = targetName; mEntity = entity; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java index 75fcb5ef..d0def907 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java @@ -26,6 +26,8 @@ public abstract class AbsCmdFactory extends AbsCmd { +public abstract class AbsGroupCmd extends AbsCmd { /** * 需要控制的子任务url */ @@ -60,7 +60,7 @@ public abstract class AbsGroupCmd extends AbsCmd< } boolean checkTask() { - tempTask = (AbsGroupTask) mQueue.getTask(mTaskEntity.getEntity()); + tempTask = (AbsGroupTask) mQueue.getTask(mTaskEntity.getEntity().getKey()); if (tempTask == null) { createTask(); if (tempTask.isComplete()) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java index 44fb4241..8c0c7c75 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java @@ -15,13 +15,13 @@ */ package com.arialyy.aria.core.command.group; -import com.arialyy.aria.core.inf.BaseGroupTaskEntity; +import com.arialyy.aria.core.inf.AbsGroupTaskEntity; /** * Created by AriaL on 2017/6/29. * 删除任务组 */ -class GroupCancelCmd extends AbsGroupCmd { +class GroupCancelCmd extends AbsGroupCmd { /** * @param targetName 创建任务的对象名 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCmdFactory.java index f2dbfa63..8af60348 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCmdFactory.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.command.group; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.inf.BaseGroupTaskEntity; +import com.arialyy.aria.core.inf.AbsGroupTaskEntity; /** * Created by AriaL on 2017/6/29. @@ -57,7 +57,7 @@ public class GroupCmdFactory { * @param type 命令类型{@link #SUB_TASK_START}、{@link #SUB_TASK_STOP}、{@link #SUB_TASK_CANCEL} * @param childUrl 需要控制的子任务url */ - public AbsGroupCmd createCmd(String target, BaseGroupTaskEntity entity, int type, + public AbsGroupCmd createCmd(String target, AbsGroupTaskEntity entity, int type, String childUrl) { AbsGroupCmd cmd = null; switch (type) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java index c4dba5b0..747e794c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java @@ -15,13 +15,13 @@ */ package com.arialyy.aria.core.command.group; -import com.arialyy.aria.core.inf.BaseGroupTaskEntity; +import com.arialyy.aria.core.inf.AbsGroupTaskEntity; /** * Created by AriaL on 2017/6/29. * 任务组开始命令,该命令负责处理任务组子任务的开始\恢复等工作 */ -class GroupStartCmd extends AbsGroupCmd { +class GroupStartCmd extends AbsGroupCmd { /** * @param targetName 创建任务的对象名 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStopCmd.java index 66262e66..483a7608 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStopCmd.java @@ -15,13 +15,13 @@ */ package com.arialyy.aria.core.command.group; -import com.arialyy.aria.core.inf.BaseGroupTaskEntity; +import com.arialyy.aria.core.inf.AbsGroupTaskEntity; /** * Created by AriaL on 2017/6/29. * 停止任务组的命令 */ -class GroupStopCmd extends AbsGroupCmd { +class GroupStopCmd extends AbsGroupCmd { /** * @param targetName 创建任务的对象名 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java index 6e56f850..f46fb19a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java @@ -16,7 +16,9 @@ package com.arialyy.aria.core.command.normal; +import android.util.Log; import com.arialyy.aria.core.command.AbsCmd; +import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsEntity; @@ -39,28 +41,44 @@ public abstract class AbsNormalCmd extends AbsCmd { boolean canExeCmd = true; private AbsTask tempTask = null; + int taskType; /** * @param targetName 产生任务的对象名 + * @param taskType 下载任务类型{@link ICmd#TASK_TYPE_DOWNLOAD}、{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}、{@link + * ICmd#TASK_TYPE_UPLOAD} */ - AbsNormalCmd(String targetName, T entity) { - //canExeCmd = CheckUtil.checkCmdEntity(entity, - // !(this instanceof CancelCmd) || !(this instanceof StopCmd)); + AbsNormalCmd(String targetName, T entity, int taskType) { + this.taskType = taskType; mTargetName = targetName; mTaskEntity = entity; TAG = CommonUtil.getClassName(this); - if (entity instanceof DownloadTaskEntity) { + if (taskType == ICmd.TASK_TYPE_DOWNLOAD) { + if (!(entity instanceof DownloadTaskEntity)) { + Log.w(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD"); + return; + } mQueue = DownloadTaskQueue.getInstance(); - isDownloadCmd = true; - } else if (entity instanceof UploadTaskEntity) { - mQueue = UploadTaskQueue.getInstance(); - isDownloadCmd = false; - } else if (entity instanceof DownloadGroupTaskEntity) { + } else if (taskType == ICmd.TASK_TYPE_DOWNLOAD_GROUP) { + if (!(entity instanceof DownloadGroupTaskEntity)) { + Log.w(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD_GROUP"); + return; + } mQueue = DownloadGroupTaskQueue.getInstance(); - isDownloadCmd = true; + } else if (taskType == ICmd.TASK_TYPE_UPLOAD) { + if (!(entity instanceof UploadTaskEntity)) { + Log.w(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_UPLOAD"); + return; + } + mQueue = UploadTaskQueue.getInstance(); + } else { + Log.w(TAG, "任务类型错误,任务类型应该为ICM.TASK_TYPE_DOWNLOAD、TASK_TYPE_DOWNLOAD_GROUP、TASK_TYPE_UPLOAD"); } + isDownloadCmd = taskType < ICmd.TASK_TYPE_UPLOAD; } + + /** * 删除所有任务 */ @@ -88,7 +106,7 @@ public abstract class AbsNormalCmd extends AbsCmd { */ void removeTask() { if (tempTask == null) createTask(); - mQueue.removeTask(tempTask); + mQueue.cancelTask(tempTask); } /** @@ -113,7 +131,7 @@ public abstract class AbsNormalCmd extends AbsCmd { * @return 执行任务 */ AbsTask getTask() { - tempTask = mQueue.getTask(mTaskEntity.getEntity()); + tempTask = mQueue.getTask(mTaskEntity.getEntity().getKey()); return tempTask; } @@ -123,7 +141,7 @@ public abstract class AbsNormalCmd extends AbsCmd { * @return 执行任务 */ AbsTask getTask(AbsEntity entity) { - tempTask = mQueue.getTask(entity); + tempTask = mQueue.getTask(entity.getKey()); return tempTask; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java index 8d2ccc39..079599d7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java @@ -27,8 +27,8 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; */ class AddCmd extends AbsNormalCmd { - AddCmd(String targetName, T entity) { - super(targetName, entity); + AddCmd(String targetName, T entity, int taskType) { + super(targetName, entity, taskType); } @Override public void executeCmd() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java index cd31f719..3f3efe11 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java @@ -38,8 +38,8 @@ public class CancelAllCmd extends AbsNormalCmd { /** * @param targetName 产生任务的对象名 */ - CancelAllCmd(String targetName, T entity) { - super(targetName, entity); + CancelAllCmd(String targetName, T entity, int taskType) { + super(targetName, entity, taskType); } @Override public void executeCmd() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java index ee0341e7..31db4cdf 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java @@ -31,8 +31,8 @@ public class CancelCmd extends AbsNormalCmd { */ public boolean removeFile = false; - CancelCmd(String targetName, T entity) { - super(targetName, entity); + CancelCmd(String targetName, T entity, int taskType) { + super(targetName, entity, taskType); } @Override public void executeCmd() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java index 61f454f8..5ea17930 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java @@ -40,8 +40,8 @@ final class HighestPriorityCmd extends AbsNormalCmd /** * @param targetName 产生任务的对象名 */ - HighestPriorityCmd(String targetName, T entity) { - super(targetName, entity); + HighestPriorityCmd(String targetName, T entity, int taskType) { + super(targetName, entity, taskType); } @Override public void executeCmd() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java index 2ec194ed..005e31b0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java @@ -18,6 +18,7 @@ package com.arialyy.aria.core.command.normal; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.AbsCmdFactory; +import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.inf.AbsTaskEntity; /** @@ -81,29 +82,30 @@ public class NormalCmdFactory extends AbsCmdFactory * @param entity 下载实体 * @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link * #TASK_STOP}、{@link #TASK_HIGHEST_PRIORITY}、{@link #TASK_STOP_ALL}、{@link #TASK_RESUME_ALL} + * @param taskType {@link ICmd#TASK_TYPE_DOWNLOAD}、{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}、{@link + * ICmd#TASK_TYPE_UPLOAD} */ - public AbsNormalCmd createCmd(String target, AbsTaskEntity entity, int type) { + public AbsNormalCmd createCmd(String target, AbsTaskEntity entity, int type, int taskType) { switch (type) { case TASK_CREATE: - return new AddCmd<>(target, entity); + return new AddCmd<>(target, entity, taskType); case TASK_RESUME: case TASK_START: - return new StartCmd<>(target, entity); + return new StartCmd<>(target, entity, taskType); case TASK_CANCEL: - return new CancelCmd<>(target, entity); + return new CancelCmd<>(target, entity, taskType); case TASK_STOP: - return new StopCmd<>(target, entity); + return new StopCmd<>(target, entity, taskType); case TASK_HIGHEST_PRIORITY: - return new HighestPriorityCmd<>(target, entity); + return new HighestPriorityCmd<>(target, entity, taskType); case TASK_STOP_ALL: - return new StopAllCmd<>(target, entity); + return new StopAllCmd<>(target, entity, taskType); case TASK_RESUME_ALL: - return new ResumeAllCmd<>(target, entity); + return new ResumeAllCmd<>(target, entity, taskType); case TASK_CANCEL_ALL: - return new CancelAllCmd<>(target, entity); + return new CancelAllCmd<>(target, entity, taskType); default: return null; } } - } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java index 84f9c2dc..e6e0cd8c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java @@ -12,6 +12,7 @@ import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.NetUtils; +import java.util.ArrayList; import java.util.List; /** @@ -19,13 +20,16 @@ import java.util.List; * 恢复所有停止的任务 * 1.如果执行队列没有满,则开始下载任务,直到执行队列满 * 2.如果队列执行队列已经满了,则将所有任务添加到等待队列中 + * 3.如果队列中只有等待状态的任务,如果执行队列没有满,则会启动等待状态的任务,如果执行队列已经满了,则会将所有等待状态的任务加载到缓存队列中 */ final class ResumeAllCmd extends AbsNormalCmd { + private List mWaitList = new ArrayList<>(); + /** * @param targetName 产生任务的对象名 */ - ResumeAllCmd(String targetName, T entity) { - super(targetName, entity); + ResumeAllCmd(String targetName, T entity, int taskType) { + super(targetName, entity, taskType); } @Override public void executeCmd() { @@ -34,53 +38,85 @@ final class ResumeAllCmd extends AbsNormalCmd { return; } if (isDownloadCmd) { - resumeDownload(); + resumeTask(findTaskData(1)); + resumeTask(findTaskData(2)); } else { - resumeUpload(); + resumeTask(findTaskData(3)); } + resumeWaitTask(); } /** - * 恢复下载,包括普通任务和任务组 + * 查找数据库中的所有任务数据 + * + * @param type {@code 1}单任务下载任务;{@code 2}任务组下载任务;{@code 3} 单任务上传任务 */ - private void resumeDownload() { - List dTaskEntity = - DbEntity.findDatas(DownloadTaskEntity.class, "isGroupTask=?", "false"); - if (dTaskEntity != null && !dTaskEntity.isEmpty()) { - for (DownloadTaskEntity te : dTaskEntity) { - if (te == null || te.getEntity() == null) continue; - int state = te.getState(); - if (state == IEntity.STATE_STOP || state == IEntity.STATE_OTHER) { - resumeEntity(te); + private List findTaskData(int type) { + List tempList = new ArrayList<>(); + switch (type) { + case 1: + List dTaskEntity = + DbEntity.findDatas(DownloadTaskEntity.class, "isGroupTask=?", "false"); + if (dTaskEntity != null && !dTaskEntity.isEmpty()) { + tempList.addAll(dTaskEntity); } - } + break; + case 2: + List groupTask = + DbEntity.findAllData(DownloadGroupTaskEntity.class); + if (groupTask != null && !groupTask.isEmpty()) { + tempList.addAll(groupTask); + } + break; + case 3: + List uTaskEntity = + DbEntity.findDatas(UploadTaskEntity.class, "isGroupTask=?", "false"); + if (uTaskEntity != null && !uTaskEntity.isEmpty()) { + tempList.addAll(uTaskEntity); + } + break; } + return tempList; + } - List groupTask = DbEntity.findAllData(DownloadGroupTaskEntity.class); - if (groupTask != null && !groupTask.isEmpty()) { - for (DownloadGroupTaskEntity te : groupTask) { + /** + * 恢复任务 + */ + private void resumeTask(List taskList) { + if (taskList != null && !taskList.isEmpty()) { + for (AbsTaskEntity te : taskList) { if (te == null || te.getEntity() == null) continue; int state = te.getState(); if (state == IEntity.STATE_STOP || state == IEntity.STATE_OTHER) { resumeEntity(te); + } else if (state == IEntity.STATE_WAIT) { + mWaitList.add(te); + } else if (state == IEntity.STATE_RUNNING) { + if (!mQueue.taskIsRunning(te.getEntity().getKey())) { + resumeEntity(te); + } } } } } /** - * 恢复上传,包括普通任务和任务组 + * 处理等待状态的任务 */ - private void resumeUpload() { - List dTaskEntity = - DbEntity.findDatas(UploadTaskEntity.class, "isGroupTask=?", "false"); - if (dTaskEntity != null && !dTaskEntity.isEmpty()) { - for (UploadTaskEntity te : dTaskEntity) { - if (te == null || te.getEntity() == null) continue; - int state = te.getState(); - if (state == IEntity.STATE_STOP || state == IEntity.STATE_OTHER) { - resumeEntity(te); - } + private void resumeWaitTask() { + int maxTaskNum; + AriaManager manager = AriaManager.getInstance(AriaManager.APP); + if (isDownloadCmd) { + maxTaskNum = manager.getDownloadConfig().getMaxTaskNum(); + } else { + maxTaskNum = manager.getUploadConfig().getMaxTaskNum(); + } + if (mWaitList == null || mWaitList.isEmpty()) return; + for (AbsTaskEntity te : mWaitList) { + if (mQueue.getCurrentExePoolNum() < maxTaskNum) { + startTask(createTask(te)); + } else { + createTask(te); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index 7c502251..b1439957 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -20,10 +20,16 @@ import android.text.TextUtils; import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.QueueMod; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.upload.UploadTaskEntity; +import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.NetUtils; +import java.util.ArrayList; +import java.util.List; /** * Created by lyy on 2016/8/22. @@ -32,8 +38,8 @@ import com.arialyy.aria.util.NetUtils; */ class StartCmd extends AbsNormalCmd { - StartCmd(String targetName, T entity) { - super(targetName, entity); + StartCmd(String targetName, T entity, int taskType) { + super(targetName, entity, taskType); } @Override public void executeCmd() { @@ -65,6 +71,9 @@ class StartCmd extends AbsNormalCmd { } else if (mod.equals(QueueMod.WAIT.getTag())) { if (mQueue.getCurrentExePoolNum() < maxTaskNum || task.getState() == IEntity.STATE_STOP + || task.getState() == IEntity.STATE_FAIL + || task.getState() == IEntity.STATE_OTHER + || task.getState() == IEntity.STATE_POST_PRE || task.getState() == IEntity.STATE_COMPLETE) { startTask(); } @@ -74,5 +83,61 @@ class StartCmd extends AbsNormalCmd { startTask(); } } + if (mQueue.getCurrentCachePoolNum() == 0){ + findAllWaitTask(); + } + } + + /** + * 当缓冲队列为null时,查找数据库中所有等待中的任务 + */ + private void findAllWaitTask() { + new Thread(new WaitTaskThread()).start(); + } + + private class WaitTaskThread implements Runnable { + + @Override public void run() { + if (isDownloadCmd) { + handleTask(findWaitData(1)); + handleTask(findWaitData(2)); + } else { + handleTask(findWaitData(3)); + } + } + + private List findWaitData(int type) { + List waitList = new ArrayList<>(); + switch (type) { + case 1: + List dEntity = + DbEntity.findDatas(DownloadTaskEntity.class, "groupName=? and state=?", "", "3"); + if (dEntity != null && !dEntity.isEmpty()) { + waitList.addAll(dEntity); + } + break; + case 2: + List dgEntity = + DbEntity.findDatas(DownloadGroupTaskEntity.class, "state=?", "3"); + if (dgEntity != null && !dgEntity.isEmpty()) { + waitList.addAll(dgEntity); + } + break; + case 3: + List uEntity = + DbEntity.findDatas(UploadTaskEntity.class, "groupName=? and state=?", "", "3"); + if (uEntity != null && !uEntity.isEmpty()) { + waitList.addAll(uEntity); + } + break; + } + return waitList; + } + + private void handleTask(List waitList) { + for (AbsTaskEntity te : waitList) { + createTask(te); + } + } } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java index a21483c3..7f55e573 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java @@ -10,8 +10,8 @@ final class StopAllCmd extends AbsNormalCmd { /** * @param targetName 产生任务的对象名 */ - StopAllCmd(String targetName, T entity) { - super(targetName, entity); + StopAllCmd(String targetName, T entity, int taskType) { + super(targetName, entity, taskType); } @Override public void executeCmd() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java index a631f9ab..6bd75bf5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java @@ -28,8 +28,8 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; */ class StopCmd extends AbsNormalCmd { - StopCmd(String targetName, T entity) { - super(targetName, entity); + StopCmd(String targetName, T entity, int taskType) { + super(targetName, entity, taskType); } @Override public void executeCmd() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index 8a2032b8..af0a580d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -328,9 +328,11 @@ public abstract class AbsFileer= 0) { Long r = Long.parseLong(record + ""); - mConstance.CURRENT_LOCATION += r - startL; + if (r > startL) { + mConstance.CURRENT_LOCATION += r - startL; + startL = r; + } Log.d(TAG, "任务【" + mEntity.getFileName() + "】线程__" + i + "__恢复下载"); - startL = r; recordL[rl] = i; rl++; } else { diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java index 8d7f7553..92c27fa1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java @@ -59,6 +59,7 @@ public abstract class AbsThreadTask info) { @@ -74,6 +75,7 @@ public abstract class AbsThreadTask 5000) { + mLastSaveTime = System.currentTimeMillis(); + new Thread(new Runnable() { + @Override public void run() { + final long currentTemp = mChildCurrentLocation; + try { + writeConfig(false, currentTemp); + } catch (IOException e) { + e.printStackTrace(); + } + } + }).start(); + } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java index dd82e880..7318d844 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java @@ -19,6 +19,7 @@ import android.os.Handler; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.scheduler.ISchedulers; @@ -28,13 +29,14 @@ import java.lang.ref.WeakReference; /** * 下载监听类 */ -class BaseDListener> +class BaseDListener, TASK extends AbsTask> implements IDownloadListener { protected WeakReference outHandler; private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 private long mLastLen = 0; //上一次发送长度 private boolean isFirst = true; protected ENTITY mEntity; + protected TASK_ENTITY mTaskEntity; protected TASK mTask; private boolean isConvertSpeed = false; boolean isWait = false; @@ -43,7 +45,8 @@ class BaseDListener> BaseDListener(TASK task, Handler outHandler) { this.outHandler = new WeakReference<>(outHandler); this.mTask = new WeakReference<>(task).get(); - this.mEntity = this.mTask.getEntity(); + this.mEntity = mTask.getTaskEntity().getEntity(); + this.mTaskEntity = mTask.getTaskEntity(); final AriaManager manager = AriaManager.getInstance(AriaManager.APP); isConvertSpeed = manager.getDownloadConfig().isConvertSpeed(); mLastLen = mEntity.getCurrentProgress(); @@ -85,7 +88,7 @@ class BaseDListener> } handleSpeed(speed); sendInState2Target(ISchedulers.RUNNING); - if (System.currentTimeMillis() - mLastSaveTime >= RUN_SAVE_INTERVAL){ + if (System.currentTimeMillis() - mLastSaveTime >= RUN_SAVE_INTERVAL) { saveData(IEntity.STATE_RUNNING, currentLocation); mLastSaveTime = System.currentTimeMillis(); } @@ -141,6 +144,7 @@ class BaseDListener> private void saveData(int state, long location) { mEntity.setComplete(state == IEntity.STATE_COMPLETE); + mTaskEntity.state = state; if (state == IEntity.STATE_CANCEL) { mEntity.setState(state); mEntity.deleteData(); @@ -156,5 +160,6 @@ class BaseDListener> } mEntity.update(); } + mTaskEntity.update(); } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java index 3aba2a50..9d52c280 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseGroupTarget.java @@ -19,7 +19,7 @@ import android.text.TextUtils; import com.arialyy.aria.core.SubTaskManager; import com.arialyy.aria.core.inf.AbsDownloadTarget; import com.arialyy.aria.core.inf.AbsTarget; -import com.arialyy.aria.core.inf.BaseGroupTaskEntity; +import com.arialyy.aria.core.inf.AbsGroupTaskEntity; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; import java.io.File; @@ -29,7 +29,7 @@ import java.util.List; /** * Created by Aria.Lao on 2017/7/26. */ -abstract class BaseGroupTarget +abstract class BaseGroupTarget extends AbsDownloadTarget { List mUrls = new ArrayList<>(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java index e127bec8..321a8311 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java @@ -24,7 +24,8 @@ import com.arialyy.aria.core.scheduler.ISchedulers; * Created by Aria.Lao on 2017/7/20. * 任务组下载事件 */ -class DownloadGroupListener extends BaseDListener +class DownloadGroupListener + extends BaseDListener implements IDownloadGroupListener { private final String TAG = "DownloadGroupListener"; private GroupSendParams mSeedEntity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java index 377df7ff..3aba90b7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java @@ -19,8 +19,6 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.common.IUtil; -import com.arialyy.aria.core.download.downloader.AbsGroupUtil; import com.arialyy.aria.core.download.downloader.DownloadGroupUtil; import com.arialyy.aria.core.download.downloader.FtpDirDownloadUtil; import com.arialyy.aria.core.inf.AbsGroupTask; @@ -32,13 +30,12 @@ import com.arialyy.aria.util.CheckUtil; * Created by AriaL on 2017/6/27. * 任务组任务 */ -public class DownloadGroupTask extends AbsGroupTask { +public class DownloadGroupTask extends AbsGroupTask { private final String TAG = "DownloadGroupTask"; private DownloadGroupListener mListener; private DownloadGroupTask(DownloadGroupTaskEntity taskEntity, Handler outHandler) { mTaskEntity = taskEntity; - mEntity = taskEntity.getEntity(); mOutHandler = outHandler; mContext = AriaManager.APP; mListener = new DownloadGroupListener(this, mOutHandler); @@ -56,6 +53,10 @@ public class DownloadGroupTask extends AbsGroupTask { +public class DownloadGroupTaskEntity extends AbsGroupTaskEntity { @OneToOne(table = DownloadGroupEntity.class, key = "groupName") public DownloadGroupEntity entity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java index 319c3a58..78cf1d9f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java @@ -22,7 +22,7 @@ import com.arialyy.aria.core.inf.IDownloadListener; * Created by Aria.Lao on 2017/7/20. * 普通任务下载的事件监听器 */ -class DownloadListener extends BaseDListener +class DownloadListener extends BaseDListener implements IDownloadListener { DownloadListener(DownloadTask task, Handler outHandler) { super(task, outHandler); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index aeb323c3..24ecb16d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -18,6 +18,7 @@ package com.arialyy.aria.core.download; import android.support.annotation.NonNull; import android.text.TextUtils; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.command.normal.CancelAllCmd; import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.common.ProxyHelper; @@ -256,7 +257,8 @@ public class DownloadReceiver extends AbsReceiver { @Override public void stopAllTask() { AriaManager.getInstance(AriaManager.APP) .setCmd(NormalCmdFactory.getInstance() - .createCmd(targetName, new DownloadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL)) + .createCmd(targetName, new DownloadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL, + ICmd.TASK_TYPE_DOWNLOAD)) .exe(); } @@ -268,7 +270,8 @@ public class DownloadReceiver extends AbsReceiver { public void resumeAllTask() { AriaManager.getInstance(AriaManager.APP) .setCmd(NormalCmdFactory.getInstance() - .createCmd(targetName, new DownloadTaskEntity(), NormalCmdFactory.TASK_RESUME_ALL)) + .createCmd(targetName, new DownloadTaskEntity(), NormalCmdFactory.TASK_RESUME_ALL, + ICmd.TASK_TYPE_DOWNLOAD)) .exe(); } @@ -282,7 +285,7 @@ public class DownloadReceiver extends AbsReceiver { final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); CancelAllCmd cancelCmd = (CancelAllCmd) CommonUtil.createNormalCmd(targetName, new DownloadTaskEntity(), - NormalCmdFactory.TASK_CANCEL_ALL); + NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_DOWNLOAD); cancelCmd.removeFile = removeFile; ariaManager.setCmd(cancelCmd).exe(); Set keys = ariaManager.getReceiver().keySet(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index c8423d3e..33f5ddc2 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -74,7 +74,6 @@ public class DownloadTarget entity.save(); mTaskEntity.save(); } - mEntity = mTaskEntity.entity; } @@ -165,7 +164,7 @@ public class DownloadTarget * 是否在下载 */ public boolean isDownloading() { - DownloadTask task = DownloadTaskQueue.getInstance().getTask(mEntity); + DownloadTask task = DownloadTaskQueue.getInstance().getTask(mEntity.getKey()); return task != null && task.isRunning(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index ddd11083..111de5ae 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -23,7 +23,6 @@ import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.core.download.downloader.SimpleDownloadUtil; import com.arialyy.aria.core.inf.AbsNormalTask; -import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.scheduler.ISchedulers; import java.io.File; @@ -31,18 +30,20 @@ import java.io.File; * Created by lyy on 2016/8/11. * 下载任务类 */ -public class DownloadTask extends AbsNormalTask { +public class DownloadTask extends AbsNormalTask { public static final String TAG = "DownloadTask"; private DownloadListener mListener; + private DownloadEntity mEntity; private IUtil mUtil; private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) { - mEntity = taskEntity.getEntity(); + mTaskEntity = taskEntity; mOutHandler = outHandler; mContext = AriaManager.APP; mListener = new DownloadListener(this, mOutHandler); mUtil = new SimpleDownloadUtil(taskEntity, mListener); + mEntity = taskEntity.getEntity(); } /** @@ -58,6 +59,10 @@ public class DownloadTask extends AbsNormalTask { return mEntity.getDownloadPath(); } + public DownloadEntity getEntity() { + return mTaskEntity.getEntity(); + } + /** * 获取当前下载任务的下载地址 * diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java index 51f5f9ad..5cbd1bf5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java @@ -15,6 +15,7 @@ */ package com.arialyy.aria.core.download; +import com.arialyy.aria.core.inf.AbsNormalTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.orm.Ignore; import com.arialyy.aria.orm.OneToOne; @@ -23,7 +24,7 @@ import com.arialyy.aria.orm.OneToOne; * Created by lyy on 2017/1/23. * 下载任务实体 */ -public class DownloadTaskEntity extends AbsTaskEntity { +public class DownloadTaskEntity extends AbsNormalTaskEntity { @OneToOne(table = DownloadEntity.class, key = "downloadPath") public DownloadEntity entity; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsDownloadTarget.java index bebe3282..d223b2c9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsDownloadTarget.java @@ -97,8 +97,8 @@ public abstract class AbsDownloadTarget - extends AbsTask { - - protected TASK_ENTITY mTaskEntity; +public abstract class AbsGroupTask + extends AbsTask { protected AbsGroupUtil mUtil; @Override public String getKey() { - return mEntity.getGroupName(); + return mTaskEntity.getEntity().getGroupName(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/BaseGroupTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskEntity.java similarity index 88% rename from Aria/src/main/java/com/arialyy/aria/core/inf/BaseGroupTaskEntity.java rename to Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskEntity.java index 8a8eb97f..066c8fe9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/BaseGroupTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTaskEntity.java @@ -19,7 +19,7 @@ package com.arialyy.aria.core.inf; /** * Created by lyy on 2017/9/5. */ -public abstract class BaseGroupTaskEntity extends AbsTaskEntity{ +public abstract class AbsGroupTaskEntity extends AbsTaskEntity{ @Override public ENTITY getEntity() { return null; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java index a89f5e31..7b86e535 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java @@ -18,7 +18,8 @@ package com.arialyy.aria.core.inf; /** * Created by lyy on 2017/6/3. */ -public abstract class AbsNormalTask extends AbsTask { +public abstract class AbsNormalTask + extends AbsTask { /** * 暂停任务,并让任务处于等待状态 @@ -39,6 +40,4 @@ public abstract class AbsNormalTask extends AbsTask extends AbsTaskEntity { +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index a214e027..8b7f80de 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -23,6 +23,9 @@ import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.command.normal.CancelCmd; import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.download.DownloadGroupTaskEntity; +import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.util.CommonUtil; import java.util.ArrayList; import java.util.List; @@ -165,10 +168,23 @@ public abstract class AbsTarget cmds = new ArrayList<>(); - cmds.add(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_STOP)); - cmds.add(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START)); + int taskType = checkTaskType(); + cmds.add( + CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_STOP, taskType)); + cmds.add(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START, + taskType)); AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe(); } @@ -220,7 +242,7 @@ public abstract class AbsTarget implements ITask { +public abstract class AbsTask implements ITask { /** * 是否需要重试,默认为true */ public boolean needRetry = true; - protected ENTITY mEntity; + protected TASK_ENTITY mTaskEntity; protected Handler mOutHandler; /** @@ -44,14 +44,14 @@ public abstract class AbsTask implements ITask * @return {@code true} 已经完成,{@code false} 未完成 */ public boolean isComplete() { - return mEntity.isComplete(); + return mTaskEntity.getEntity().isComplete(); } /** * 获取当前下载进度 */ @Override public long getCurrentProgress() { - return mEntity.getCurrentProgress(); + return mTaskEntity.getEntity().getCurrentProgress(); } /** @@ -60,10 +60,10 @@ public abstract class AbsTask implements ITask * @return 如:已经下载3mb的大小,则返回{@code 3mb} */ @Override public String getConvertCurrentProgress() { - if (mEntity.getCurrentProgress() == 0) { + if (mTaskEntity.getEntity().getCurrentProgress() == 0) { return "0b"; } - return CommonUtil.formatFileSize(mEntity.getCurrentProgress()); + return CommonUtil.formatFileSize(mTaskEntity.getEntity().getCurrentProgress()); } /** @@ -72,17 +72,17 @@ public abstract class AbsTask implements ITask * @return 如果文件长度为0,则返回0m,否则返回转换后的长度1b、1kb、1mb、1gb、1tb */ @Override public String getConvertFileSize() { - if (mEntity.getFileSize() == 0) { + if (mTaskEntity.getEntity().getFileSize() == 0) { return "0mb"; } - return CommonUtil.formatFileSize(mEntity.getFileSize()); + return CommonUtil.formatFileSize(mTaskEntity.getEntity().getFileSize()); } /** * 获取文件大小 */ @Override public long getFileSize() { - return mEntity.getFileSize(); + return mTaskEntity.getEntity().getFileSize(); } /** @@ -91,10 +91,11 @@ public abstract class AbsTask implements ITask * @return 返回百分比进度,如果文件长度为0,返回0 */ @Override public int getPercent() { - if (mEntity.getFileSize() == 0) { + if (mTaskEntity.getEntity().getFileSize() == 0) { return 0; } - return (int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize()); + return (int) (mTaskEntity.getEntity().getCurrentProgress() * 100 / mTaskEntity.getEntity() + .getFileSize()); } /** @@ -103,7 +104,8 @@ public abstract class AbsTask implements ITask * @return {@link IEntity} */ public int getState() { - return mEntity == null ? IEntity.STATE_OTHER : mEntity.getState(); + return mTaskEntity.getEntity() == null ? IEntity.STATE_OTHER + : mTaskEntity.getEntity().getState(); } /** @@ -112,7 +114,7 @@ public abstract class AbsTask implements ITask * @return 如果实体不存在,则返回null,否则返回扩展字段 */ @Override public String getExtendField() { - return mEntity == null ? null : mEntity.getStr(); + return mTaskEntity.getEntity() == null ? null : mTaskEntity.getEntity().getStr(); } /** @@ -133,7 +135,7 @@ public abstract class AbsTask implements ITask * 才能生效 */ @Override public long getSpeed() { - return mEntity.getSpeed(); + return mTaskEntity.getEntity().getSpeed(); } /** @@ -154,11 +156,11 @@ public abstract class AbsTask implements ITask * 才能生效 */ @Override public String getConvertSpeed() { - return mEntity.getConvertSpeed(); + return mTaskEntity.getEntity().getConvertSpeed(); } - @Override public ENTITY getEntity() { - return mEntity; + @Override public TASK_ENTITY getTaskEntity() { + return mTaskEntity; } public String getTargetName() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java index 0afb9a4e..8819c4ff 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java @@ -24,6 +24,7 @@ import java.util.Map; /** * Created by lyy on 2017/2/23. + * 所有任务实体的父类 */ public abstract class AbsTaskEntity extends DbEntity { /** @@ -60,6 +61,11 @@ public abstract class AbsTaskEntity extends DbEntity { */ @Ignore public boolean isNewTask = false; + /** + * 任务状态,和Entity的state同步 + */ + public int state = IEntity.STATE_WAIT; + /** * 请求类型 * {@link AbsTaskEntity#HTTP}、{@link AbsTaskEntity#FTP} diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java index e8613c1d..552a8068 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java @@ -76,7 +76,7 @@ public abstract class AbsUploadTarget { +public interface ITask { /** * 获取下载状态 @@ -45,7 +45,7 @@ public interface ITask { /** * 获取信息实体 */ - ENTITY getEntity(); + TASK_ENTITY getTaskEntity(); void start(); @@ -91,5 +91,4 @@ public interface ITask { String getConvertCurrentProgress(); void setTargetName(String targetName); - } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index c4be525b..a08aff6e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -18,7 +18,6 @@ package com.arialyy.aria.core.queue; import android.util.Log; import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; @@ -30,8 +29,8 @@ import com.arialyy.aria.util.NetUtils; * Created by lyy on 2017/2/23. * 任务队列 */ -abstract class AbsTaskQueue - implements ITaskQueue { +abstract class AbsTaskQueue + implements ITaskQueue { private final String TAG = "AbsTaskQueue"; BaseCachePool mCachePool; BaseExecutePool mExecutePool; @@ -74,11 +73,6 @@ abstract class AbsTaskQueue { + extends AbsTaskQueue { private static volatile DownloadGroupTaskQueue INSTANCE = null; private final String TAG = "DownloadGroupTaskQueue"; @@ -70,10 +70,6 @@ public class DownloadGroupTaskQueue return task; } - @Override public String getKey(DownloadGroupEntity entity) { - return entity.getGroupName(); - } - @Override public int getConfigMaxNum() { return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index b96f2ea1..735ff34f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -35,7 +35,7 @@ import java.util.Set; * 下载任务队列 */ public class DownloadTaskQueue - extends AbsTaskQueue { + extends AbsTaskQueue { private static final String TAG = "DownloadTaskQueue"; private static volatile DownloadTaskQueue INSTANCE = null; @@ -59,10 +59,6 @@ public class DownloadTaskQueue return DownloadSharePool.getInstance().executePool; } - @Override public String getKey(DownloadEntity entity) { - return entity.getUrl(); - } - @Override public int getConfigMaxNum() { return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java index 76594a81..8326c050 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java @@ -16,15 +16,11 @@ package com.arialyy.aria.core.queue; -import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.download.DownloadTaskEntity; +import com.arialyy.aria.core.download.DownloadGroupTask; import com.arialyy.aria.core.download.DownloadTask; -import com.arialyy.aria.core.inf.AbsEntity; -import com.arialyy.aria.core.inf.AbsNormalTask; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsTask; -import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; -import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.core.upload.UploadTaskEntity; @@ -32,7 +28,7 @@ import com.arialyy.aria.core.upload.UploadTaskEntity; * Created by lyy on 2016/8/16. * 任务功能接口 */ -public interface ITaskQueue { +public interface ITaskQueue { /** * 通过key判断任务是否正在执行 @@ -67,18 +63,18 @@ public interface ITaskQueue { +public class UploadTaskQueue extends AbsTaskQueue { private static final String TAG = "UploadTaskQueue"; private static volatile UploadTaskQueue INSTANCE = null; @@ -55,10 +55,6 @@ public class UploadTaskQueue extends AbsTaskQueue implements IPool { } } - @Override public TASK getTask(String downloadUrl) { + @Override public TASK getTask(String key) { synchronized (AriaManager.LOCK) { - if (TextUtils.isEmpty(downloadUrl)) { + if (TextUtils.isEmpty(key)) { Log.e(TAG, "请传入有效的任务key"); return null; } - String key = CommonUtil.keyToHashKey(downloadUrl); - return mExecuteMap.get(key); + return mExecuteMap.get(CommonUtil.keyToHashKey(key)); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 2704e55b..94c6ce9e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -37,7 +37,7 @@ import java.util.concurrent.ConcurrentHashMap; * Created by lyy on 2017/6/4. * 事件调度器,用于处理任务状态的调度 */ -abstract class AbsSchedulers, QUEUE extends ITaskQueue> +abstract class AbsSchedulers, QUEUE extends ITaskQueue> implements ISchedulers { private final String TAG = "AbsSchedulers"; @@ -148,14 +148,13 @@ abstract class AbsSchedulers { + AbsSchedulers { private final String TAG = "DownloadGroupSchedulers"; private static volatile DownloadGroupSchedulers INSTANCE = null; diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java index 250c75e6..85b6cac3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java @@ -27,7 +27,7 @@ import com.arialyy.aria.core.download.DownloadTask; * 任务下载器,提供抽象的方法供具体的实现类操作 */ public class DownloadSchedulers - extends AbsSchedulers { + extends AbsSchedulers { private final String TAG = "DownloadSchedulers"; private static volatile DownloadSchedulers INSTANCE = null; diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java index 893915b4..b832c931 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java @@ -17,7 +17,6 @@ package com.arialyy.aria.core.scheduler; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.queue.UploadTaskQueue; -import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.core.upload.UploadTaskEntity; @@ -25,8 +24,7 @@ import com.arialyy.aria.core.upload.UploadTaskEntity; * Created by lyy on 2017/2/27. * 上传任务调度器 */ -public class UploadSchedulers - extends AbsSchedulers { +public class UploadSchedulers extends AbsSchedulers { private static final String TAG = "UploadSchedulers"; private static volatile UploadSchedulers INSTANCE = null; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java b/Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java index aee07f50..3a2dad54 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/BaseUListener.java @@ -19,6 +19,7 @@ import android.os.Handler; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IUploadListener; import com.arialyy.aria.core.scheduler.ISchedulers; @@ -28,13 +29,14 @@ import java.lang.ref.WeakReference; /** * 下载监听类 */ -class BaseUListener> +class BaseUListener, TASK extends AbsTask> implements IUploadListener { private WeakReference outHandler; private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 private long mLastLen = 0; //上一次发送长度 private boolean isFirst = true; protected ENTITY mEntity; + protected TASK_ENTITY mTaskEntity; protected TASK mTask; private boolean isConvertSpeed = false; boolean isWait = false; @@ -43,7 +45,8 @@ class BaseUListener> BaseUListener(TASK task, Handler outHandler) { this.outHandler = new WeakReference<>(outHandler); this.mTask = new WeakReference<>(task).get(); - this.mEntity = this.mTask.getEntity(); + this.mEntity = this.mTask.getTaskEntity().getEntity(); + this.mTaskEntity = this.mTask.getTaskEntity(); final AriaManager manager = AriaManager.getInstance(AriaManager.APP); isConvertSpeed = manager.getDownloadConfig().isConvertSpeed(); mLastLen = mEntity.getCurrentProgress(); @@ -129,6 +132,7 @@ class BaseUListener> private void saveData(int state, long location) { mEntity.setComplete(state == IEntity.STATE_COMPLETE); + mTaskEntity.state = state; if (state == IEntity.STATE_CANCEL) { mEntity.deleteData(); } else if (state == IEntity.STATE_COMPLETE) { @@ -143,5 +147,6 @@ class BaseUListener> } mEntity.update(); } + mTaskEntity.update(); } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index 2036547c..ae1b0ce7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.upload; import android.support.annotation.NonNull; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.common.ProxyHelper; import com.arialyy.aria.core.command.normal.NormalCmdFactory; import com.arialyy.aria.core.download.DownloadTaskEntity; @@ -79,7 +80,7 @@ public class UploadReceiver extends AbsReceiver { @Override public void stopAllTask() { AriaManager.getInstance(AriaManager.APP) .setCmd(NormalCmdFactory.getInstance() - .createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL)) + .createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL, ICmd.TASK_TYPE_UPLOAD)) .exe(); } @@ -93,7 +94,7 @@ public class UploadReceiver extends AbsReceiver { final AriaManager am = AriaManager.getInstance(AriaManager.APP); am.setCmd(CommonUtil.createNormalCmd(targetName, new DownloadTaskEntity(), - NormalCmdFactory.TASK_CANCEL_ALL)).exe(); + NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_UPLOAD)).exe(); Set keys = am.getReceiver().keySet(); for (String key : keys) { diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index ceecb7d6..3d4d7410 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -33,7 +33,7 @@ import java.util.Map; class DBConfig { static Map mapping = new HashMap<>(); static String DB_NAME; - static int VERSION = 15; + static int VERSION = 16; static { if (TextUtils.isEmpty(DB_NAME)) { diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index 30480ce9..9d6a1730 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -24,6 +24,7 @@ import android.text.TextUtils; import android.util.Base64; import android.util.Log; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.command.group.AbsGroupCmd; import com.arialyy.aria.core.command.group.GroupCmdFactory; import com.arialyy.aria.core.command.normal.AbsNormalCmd; @@ -33,7 +34,7 @@ import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; -import com.arialyy.aria.core.inf.BaseGroupTaskEntity; +import com.arialyy.aria.core.inf.AbsGroupTaskEntity; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.orm.DbEntity; @@ -437,10 +438,13 @@ public class CommonUtil { /** * 创建任务命令 + * + * @param taskType {@link ICmd#TASK_TYPE_DOWNLOAD}、{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}、{@link + * ICmd#TASK_TYPE_UPLOAD} */ public static AbsNormalCmd createNormalCmd(String target, T entity, - int cmd) { - return NormalCmdFactory.getInstance().createCmd(target, entity, cmd); + int cmd, int taskType) { + return NormalCmdFactory.getInstance().createCmd(target, entity, cmd, taskType); } /** @@ -448,7 +452,7 @@ public class CommonUtil { * * @param childUrl 子任务url */ - public static AbsGroupCmd createGroupCmd(String target, T entity, + public static AbsGroupCmd createGroupCmd(String target, T entity, int cmd, String childUrl) { return GroupCmdFactory.getInstance().createCmd(target, entity, cmd, childUrl); } diff --git a/DEV_LOG.md b/DEV_LOG.md index 3db7b4e7..16c211cc 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -1,4 +1,5 @@ ## 开发日志 + + v_3.3.4 优化任务代码结构,修复上一个版本暂停后无法自动执行任务的问题 + v_3.3.3 修复进度条错乱的问题,修复同一时间多次调用start导致重复下载的问题 + v_3.3.2 新加reTry(),修复上一个版本不会回调失败事件的问题;增加running状态下5秒钟保存一次数据库的功能;修复FTP断点上传文件不完整的问题 + v_3.3.1 增加网络事件,网络未连接,将不会重试下载,修复删除未开始任务,状态回调错误 diff --git a/README.md b/README.md index 94fadb2e..aa3b8b26 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ Aria有以下特点: [![Download](https://api.bintray.com/packages/arialyy/maven/AriaApi/images/download.svg)](https://bintray.com/arialyy/maven/AriaApi/_latestVersion) [![Download](https://api.bintray.com/packages/arialyy/maven/AriaCompiler/images/download.svg)](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion) ```java -compile 'com.arialyy.aria:aria-core:3.3.3' -annotationProcessor 'com.arialyy.aria:aria-compiler:3.3.3' +compile 'com.arialyy.aria:aria-core:3.3.4' +annotationProcessor 'com.arialyy.aria:aria-compiler:3.3.4' ``` *** diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 2b8785d4..32cf6bd1 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -8,7 +8,7 @@ - + diff --git a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index d5d6e08a..0df8d652 100644 --- a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -18,8 +18,8 @@ package com.arialyy.aria.core.upload; import android.os.Handler; import android.os.Looper; import android.util.Log; +import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.inf.AbsNormalTask; -import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.core.upload.uploader.SimpleUploadUtil; @@ -27,27 +27,31 @@ import com.arialyy.aria.core.upload.uploader.SimpleUploadUtil; * Created by lyy on 2017/2/23. * 上传任务 */ -public class UploadTask extends AbsNormalTask { +public class UploadTask extends AbsNormalTask { private static final String TAG = "UploadTask"; private SimpleUploadUtil mUtil; - private BaseUListener mListener; + private BaseUListener mListener; private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) { + mTaskEntity = taskEntity; mOutHandler = outHandler; - mEntity = taskEntity.getEntity(); mListener = new BaseUListener<>(this, mOutHandler); mUtil = new SimpleUploadUtil(taskEntity, mListener); } @Override public String getKey() { - return mEntity.getFilePath(); + return mTaskEntity.getEntity().getFilePath(); } @Override public boolean isRunning() { return mUtil.isRunning(); } + public UploadEntity getEntity() { + return mTaskEntity.getEntity(); + } + @Override public void start() { if (mUtil.isRunning()) { Log.d(TAG, "任务正在下载"); @@ -60,7 +64,7 @@ public class UploadTask extends AbsNormalTask { if (mUtil.isRunning()) { mUtil.stop(); } else { - mListener.onStop(mEntity.getCurrentProgress()); + mListener.onStop(getCurrentProgress()); } } diff --git a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java index 03c377b2..137848ef 100644 --- a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java +++ b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java @@ -15,7 +15,7 @@ */ package com.arialyy.aria.core.upload; -import com.arialyy.aria.core.inf.AbsTaskEntity; +import com.arialyy.aria.core.inf.AbsNormalTaskEntity; import com.arialyy.aria.orm.OneToOne; import java.util.HashMap; import java.util.Map; @@ -24,7 +24,7 @@ import java.util.Map; * Created by lyy on 2017/2/9. * 上传任务实体 */ -public class UploadTaskEntity extends AbsTaskEntity { +public class UploadTaskEntity extends AbsNormalTaskEntity { public String attachment; //文件上传需要的key public String contentType = "multipart/form-data"; //上传的文件类型 public String userAgent = "User-Agent"; diff --git a/build.gradle b/build.gradle index 0a49f17d..75431c43 100644 --- a/build.gradle +++ b/build.gradle @@ -37,7 +37,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.3.3' + publishVersion = '3.3.4' // publishVersion = '1.0.3' //FTP插件 repoName='maven' desc = 'android 下载框架'