修复取消后无法重新启动的bug

pull/330/head
laoyuyu 6 years ago
parent 7f975b2e91
commit ec6f632aff
  1. 1
      Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java
  2. 3
      Aria/src/main/java/com/arialyy/aria/core/command/AbsCmdFactory.java
  3. 8
      Aria/src/main/java/com/arialyy/aria/core/command/group/AbsGroupCmd.java
  4. 7
      Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCancelCmd.java
  5. 6
      Aria/src/main/java/com/arialyy/aria/core/command/group/GroupCmdFactory.java
  6. 7
      Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStartCmd.java
  7. 7
      Aria/src/main/java/com/arialyy/aria/core/command/group/GroupStopCmd.java
  8. 10
      Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java
  9. 4
      Aria/src/main/java/com/arialyy/aria/core/command/normal/AddCmd.java
  10. 7
      Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelAllCmd.java
  11. 8
      Aria/src/main/java/com/arialyy/aria/core/command/normal/CancelCmd.java
  12. 10
      Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java
  13. 19
      Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java
  14. 7
      Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java
  15. 9
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  16. 7
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StopAllCmd.java
  17. 10
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java
  18. 37
      Aria/src/main/java/com/arialyy/aria/core/common/AEvent.java
  19. 20
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  20. 27
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  21. 6
      Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java
  22. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  23. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  24. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  25. 7
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java
  26. 17
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java
  27. 14
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  28. 25
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  29. 14
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  30. 1
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  31. 2
      Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java
  32. 5
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  33. 9
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadGroupTaskQueue.java
  34. 9
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  35. 3
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  36. 25
      Aria/src/main/java/com/arialyy/aria/core/queue/TaskFactory.java
  37. 9
      Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java
  38. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/BaseNormalTarget.java
  39. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java
  40. 4
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  41. 11
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java
  42. 9
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java
  43. 4
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  44. 4
      README.md
  45. 9
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  46. 2
      build.gradle

@ -26,7 +26,6 @@ public abstract class AbsCmd<T extends AbsTaskEntity> implements ICmd {
protected ITaskQueue mQueue; protected ITaskQueue mQueue;
protected T mTaskEntity; protected T mTaskEntity;
protected String TAG; protected String TAG;
protected String mTargetName;
/** /**
* 是否是下载任务的命令 * 是否是下载任务的命令

@ -24,10 +24,9 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
public abstract class AbsCmdFactory<TASK_ENTITY extends AbsTaskEntity, CMD extends AbsCmd> { public abstract class AbsCmdFactory<TASK_ENTITY extends AbsTaskEntity, CMD extends AbsCmd> {
/** /**
* @param target 创建任务的对象
* @param entity 下载实体 * @param entity 下载实体
* @param taskType {@link ICmd#TASK_TYPE_DOWNLOAD}{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}{@link * @param taskType {@link ICmd#TASK_TYPE_DOWNLOAD}{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}{@link
* ICmd#TASK_TYPE_UPLOAD} * ICmd#TASK_TYPE_UPLOAD}
*/ */
public abstract CMD createCmd(String target, TASK_ENTITY entity, int type, int taskType); public abstract CMD createCmd(TASK_ENTITY entity, int type, int taskType);
} }

@ -36,11 +36,7 @@ public abstract class AbsGroupCmd<T extends AbsGroupTaskEntity> extends AbsCmd<T
AbsGroupTask tempTask; AbsGroupTask tempTask;
/** AbsGroupCmd(T entity) {
* @param targetName 创建任务的对象名
*/
AbsGroupCmd(String targetName, T entity) {
mTargetName = targetName;
mTaskEntity = entity; mTaskEntity = entity;
TAG = CommonUtil.getClassName(this); TAG = CommonUtil.getClassName(this);
if (entity instanceof DownloadGroupTaskEntity) { if (entity instanceof DownloadGroupTaskEntity) {
@ -55,7 +51,7 @@ public abstract class AbsGroupCmd<T extends AbsGroupTaskEntity> extends AbsCmd<T
* @return 创建的任务 * @return 创建的任务
*/ */
AbsTask createTask() { AbsTask createTask() {
tempTask = (AbsGroupTask) mQueue.createTask(mTargetName, mTaskEntity); tempTask = (AbsGroupTask) mQueue.createTask(mTaskEntity);
return tempTask; return tempTask;
} }

@ -22,11 +22,8 @@ import com.arialyy.aria.core.inf.AbsGroupTaskEntity;
* 删除任务组子任务 * 删除任务组子任务
*/ */
class GroupCancelCmd<T extends AbsGroupTaskEntity> extends AbsGroupCmd<T> { class GroupCancelCmd<T extends AbsGroupTaskEntity> extends AbsGroupCmd<T> {
/** GroupCancelCmd(T entity) {
* @param targetName 创建任务的对象名 super(entity);
*/
GroupCancelCmd(String targetName, T entity) {
super(targetName, entity);
} }
@Override public void executeCmd() { @Override public void executeCmd() {

@ -62,13 +62,13 @@ public class GroupCmdFactory {
AbsGroupCmd cmd = null; AbsGroupCmd cmd = null;
switch (type) { switch (type) {
case SUB_TASK_START: case SUB_TASK_START:
cmd = new GroupStartCmd<>(target, entity); cmd = new GroupStartCmd<>(entity);
break; break;
case SUB_TASK_STOP: case SUB_TASK_STOP:
cmd = new GroupStopCmd<>(target, entity); cmd = new GroupStopCmd<>(entity);
break; break;
case SUB_TASK_CANCEL: case SUB_TASK_CANCEL:
cmd = new GroupCancelCmd<>(target, entity); cmd = new GroupCancelCmd<>(entity);
} }
if (cmd != null) { if (cmd != null) {
cmd.childUrl = childUrl; cmd.childUrl = childUrl;

@ -22,11 +22,8 @@ import com.arialyy.aria.core.inf.AbsGroupTaskEntity;
* 任务组开始命令该命令负责处理任务组子任务的开始\恢复等工作 * 任务组开始命令该命令负责处理任务组子任务的开始\恢复等工作
*/ */
class GroupStartCmd<T extends AbsGroupTaskEntity> extends AbsGroupCmd<T> { class GroupStartCmd<T extends AbsGroupTaskEntity> extends AbsGroupCmd<T> {
/** GroupStartCmd( T entity) {
* @param targetName 创建任务的对象名 super( entity);
*/
GroupStartCmd(String targetName, T entity) {
super(targetName, entity);
} }
@Override public void executeCmd() { @Override public void executeCmd() {

@ -22,11 +22,8 @@ import com.arialyy.aria.core.inf.AbsGroupTaskEntity;
* 停止任务组的命令 * 停止任务组的命令
*/ */
class GroupStopCmd<T extends AbsGroupTaskEntity> extends AbsGroupCmd<T> { class GroupStopCmd<T extends AbsGroupTaskEntity> extends AbsGroupCmd<T> {
/** GroupStopCmd(T entity) {
* @param targetName 创建任务的对象名 super(entity);
*/
GroupStopCmd(String targetName, T entity) {
super(targetName, entity);
} }
@Override public void executeCmd() { @Override public void executeCmd() {

@ -46,13 +46,11 @@ public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
int taskType; int taskType;
/** /**
* @param targetName 产生任务的对象名
* @param taskType 下载任务类型{@link ICmd#TASK_TYPE_DOWNLOAD}{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}{@link * @param taskType 下载任务类型{@link ICmd#TASK_TYPE_DOWNLOAD}{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}{@link
* ICmd#TASK_TYPE_UPLOAD} * ICmd#TASK_TYPE_UPLOAD}
*/ */
AbsNormalCmd(String targetName, T entity, int taskType) { AbsNormalCmd(T entity, int taskType) {
this.taskType = taskType; this.taskType = taskType;
mTargetName = targetName;
mTaskEntity = entity; mTaskEntity = entity;
TAG = CommonUtil.getClassName(this); TAG = CommonUtil.getClassName(this);
if (taskType == ICmd.TASK_TYPE_DOWNLOAD) { if (taskType == ICmd.TASK_TYPE_DOWNLOAD) {
@ -126,7 +124,7 @@ public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
*/ */
void removeTask(AbsTaskEntity taskEntity) { void removeTask(AbsTaskEntity taskEntity) {
AbsTask tempTask = getTask(taskEntity.getEntity()); AbsTask tempTask = getTask(taskEntity.getEntity());
if (tempTask == null){ if (tempTask == null) {
tempTask = createTask(taskEntity); tempTask = createTask(taskEntity);
} }
mQueue.cancelTask(tempTask); mQueue.cancelTask(tempTask);
@ -181,7 +179,7 @@ public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
* @return 创建的任务 * @return 创建的任务
*/ */
AbsTask createTask() { AbsTask createTask() {
tempTask = mQueue.createTask(mTargetName, mTaskEntity); tempTask = mQueue.createTask(mTaskEntity);
return tempTask; return tempTask;
} }
@ -193,6 +191,6 @@ public abstract class AbsNormalCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
*/ */
AbsTask createTask(AbsTaskEntity taskEntity) { AbsTask createTask(AbsTaskEntity taskEntity) {
TEManager.getInstance().addTEntity(taskEntity); TEManager.getInstance().addTEntity(taskEntity);
return mQueue.createTask(mTargetName, taskEntity); return mQueue.createTask(taskEntity);
} }
} }

@ -27,8 +27,8 @@ import com.arialyy.aria.util.ALog;
*/ */
class AddCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> { class AddCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
AddCmd(String targetName, T entity, int taskType) { AddCmd(T entity, int taskType) {
super(targetName, entity, taskType); super(entity, taskType);
} }
@Override public void executeCmd() { @Override public void executeCmd() {

@ -41,11 +41,8 @@ public class CancelAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
*/ */
public boolean removeFile = false; public boolean removeFile = false;
/** CancelAllCmd(T entity, int taskType) {
* @param targetName 产生任务的对象名 super(entity, taskType);
*/
CancelAllCmd(String targetName, T entity, int taskType) {
super(targetName, entity, taskType);
} }
@Override public void executeCmd() { @Override public void executeCmd() {

@ -16,7 +16,6 @@
package com.arialyy.aria.core.command.normal; package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -31,8 +30,8 @@ public class CancelCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
*/ */
public boolean removeFile = false; public boolean removeFile = false;
CancelCmd(String targetName, T entity, int taskType) { CancelCmd(T entity, int taskType) {
super(targetName, entity, taskType); super(entity, taskType);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
@ -43,9 +42,6 @@ public class CancelCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
} }
if (task != null) { if (task != null) {
mTaskEntity.setRemoveFile(removeFile); mTaskEntity.setRemoveFile(removeFile);
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
removeTask(); removeTask();
} }
} }

@ -36,11 +36,8 @@ import com.arialyy.aria.util.NetUtils;
* 目前只只支持单下载任务的最高优先级任务 * 目前只只支持单下载任务的最高优先级任务
*/ */
final class HighestPriorityCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> { final class HighestPriorityCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
/** HighestPriorityCmd(T entity, int taskType) {
* @param targetName 产生任务的对象名 super(entity, taskType);
*/
HighestPriorityCmd(String targetName, T entity, int taskType) {
super(targetName, entity, taskType);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
@ -54,9 +51,6 @@ final class HighestPriorityCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T>
task = (DownloadTask) createTask(); task = (DownloadTask) createTask();
} }
if (task != null) { if (task != null) {
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
((DownloadTaskQueue) mQueue).setTaskHighestPriority(task); ((DownloadTaskQueue) mQueue).setTaskHighestPriority(task);
} }
} }

@ -78,32 +78,31 @@ public class NormalCmdFactory extends AbsCmdFactory<AbsTaskEntity, AbsNormalCmd>
} }
/** /**
* @param target 创建任务的对象
* @param entity 下载实体 * @param entity 下载实体
* @param type 命令类型{@link #TASK_CREATE}{@link #TASK_START}{@link #TASK_CANCEL}{@link * @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} * #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 * @param taskType {@link ICmd#TASK_TYPE_DOWNLOAD}{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}{@link
* ICmd#TASK_TYPE_UPLOAD} * ICmd#TASK_TYPE_UPLOAD}
*/ */
public AbsNormalCmd createCmd(String target, AbsTaskEntity entity, int type, int taskType) { public AbsNormalCmd createCmd(AbsTaskEntity entity, int type, int taskType) {
switch (type) { switch (type) {
case TASK_CREATE: case TASK_CREATE:
return new AddCmd<>(target, entity, taskType); return new AddCmd<>(entity, taskType);
case TASK_RESUME: case TASK_RESUME:
case TASK_START: case TASK_START:
return new StartCmd<>(target, entity, taskType); return new StartCmd<>(entity, taskType);
case TASK_CANCEL: case TASK_CANCEL:
return new CancelCmd<>(target, entity, taskType); return new CancelCmd<>(entity, taskType);
case TASK_STOP: case TASK_STOP:
return new StopCmd<>(target, entity, taskType); return new StopCmd<>(entity, taskType);
case TASK_HIGHEST_PRIORITY: case TASK_HIGHEST_PRIORITY:
return new HighestPriorityCmd<>(target, entity, taskType); return new HighestPriorityCmd<>(entity, taskType);
case TASK_STOP_ALL: case TASK_STOP_ALL:
return new StopAllCmd<>(target, entity, taskType); return new StopAllCmd<>(entity, taskType);
case TASK_RESUME_ALL: case TASK_RESUME_ALL:
return new ResumeAllCmd<>(target, entity, taskType); return new ResumeAllCmd<>(entity, taskType);
case TASK_CANCEL_ALL: case TASK_CANCEL_ALL:
return new CancelAllCmd<>(target, entity, taskType); return new CancelAllCmd<>(entity, taskType);
default: default:
return null; return null;
} }

@ -32,11 +32,8 @@ import java.util.List;
final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> { final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
private List<AbsTaskEntity> mWaitList = new ArrayList<>(); private List<AbsTaskEntity> mWaitList = new ArrayList<>();
/** ResumeAllCmd(T entity, int taskType) {
* @param targetName 产生任务的对象名 super(entity, taskType);
*/
ResumeAllCmd(String targetName, T entity, int taskType) {
super(targetName, entity, taskType);
} }
@Override public void executeCmd() { @Override public void executeCmd() {

@ -16,8 +16,6 @@
package com.arialyy.aria.core.command.normal; package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.QueueMod; import com.arialyy.aria.core.common.QueueMod;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
@ -46,8 +44,8 @@ import java.util.List;
*/ */
class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> { class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
StartCmd(String targetName, T entity, int taskType) { StartCmd(T entity, int taskType) {
super(targetName, entity, taskType); super(entity, taskType);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
@ -68,9 +66,6 @@ class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
AbsTask task = getTask(); AbsTask task = getTask();
if (task == null) { if (task == null) {
task = createTask(); task = createTask();
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
// 任务不存在时,根据配置不同,对任务执行操作 // 任务不存在时,根据配置不同,对任务执行操作
if (mod.equals(QueueMod.NOW.getTag())) { if (mod.equals(QueueMod.NOW.getTag())) {
startTask(); startTask();

@ -7,11 +7,8 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
* 停止所有任务的命令并清空所有等待队列 * 停止所有任务的命令并清空所有等待队列
*/ */
final class StopAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> { final class StopAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
/** StopAllCmd(T entity, int taskType) {
* @param targetName 产生任务的对象名 super(entity, taskType);
*/
StopAllCmd(String targetName, T entity, int taskType) {
super(targetName, entity, taskType);
} }
@Override public void executeCmd() { @Override public void executeCmd() {

@ -16,10 +16,9 @@
package com.arialyy.aria.core.command.normal; package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsTask; 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.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
/** /**
@ -28,8 +27,8 @@ import com.arialyy.aria.util.ALog;
*/ */
class StopCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> { class StopCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
StopCmd(String targetName, T entity, int taskType) { StopCmd(T entity, int taskType) {
super(targetName, entity, taskType); super(entity, taskType);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
@ -42,9 +41,6 @@ class StopCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
ALog.w(TAG, "停止命令执行失败,【调度器中没有该任务】"); ALog.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
} }
} else { } else {
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
stopTask(); stopTask();
} }
} }

@ -0,0 +1,37 @@
/*
* 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.common;
/**
* Aria事件总线
*/
public class AEvent {
private static final Object LOCK = new Object();
private static volatile AEvent INSTANCE = null;
private AEvent() {
}
public static AEvent getInstance() {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new AEvent();
}
}
return INSTANCE;
}
}

@ -42,7 +42,8 @@ import java.util.TimerTask;
* Created by AriaL on 2017/7/1. * Created by AriaL on 2017/7/1.
* 任务处理器 * 任务处理器
*/ */
public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>> { public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>>
implements Runnable {
private static final String STATE = "_state_"; private static final String STATE = "_state_";
private static final String RECORD = "_record_"; private static final String RECORD = "_record_";
/** /**
@ -139,7 +140,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
mStartThreadNum = 1; mStartThreadNum = 1;
} else { } else {
mTotalThreadNum = mTotalThreadNum =
mTaskEntity.isNewTask() ? (mStartThreadNum = setNewTaskThreadNum()) : mTotalThreadNum; mTaskEntity.isNewTask() ? setNewTaskThreadNum() : mTotalThreadNum;
} }
mConstance.START_THREAD_NUM = mTotalThreadNum; mConstance.START_THREAD_NUM = mTotalThreadNum;
@ -168,10 +169,16 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
} else { } else {
handleBreakpoint(); handleBreakpoint();
} }
startTimer(); startTimer();
} }
@Override public void run() {
if (mConstance.isRunning) {
return;
}
startFlow();
}
/** /**
* 预处理完成 * 预处理完成
*/ */
@ -272,10 +279,7 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 直接调用的时候会自动启动线程执行 * 直接调用的时候会自动启动线程执行
*/ */
public void start() { public void start() {
if (mConstance.isRunning) { new Thread(this).start();
return;
}
startFlow();
} }
public void resume() { public void resume() {
@ -343,8 +347,8 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length())); ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length()));
tr.startLocation = file.length(); tr.startLocation = file.length();
tr.isComplete = false; tr.isComplete = false;
mStartThreadNum++;
} }
mStartThreadNum++;
} }
} else { } else {
for (ThreadRecord tr : mRecord.threadRecords) { for (ThreadRecord tr : mRecord.threadRecords) {

@ -37,7 +37,7 @@ import java.util.concurrent.Executors;
* 任务线程 * 任务线程
*/ */
public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>> public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY extends AbsTaskEntity<ENTITY>>
implements Callable<TASK_ENTITY> { implements Callable<AbsThreadTask> {
/** /**
* 线程重试次数 * 线程重试次数
*/ */
@ -60,13 +60,14 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
protected int mConnectTimeOut; //连接超时时间 protected int mConnectTimeOut; //连接超时时间
protected int mReadTimeOut; //流读取的超时时间 protected int mReadTimeOut; //流读取的超时时间
protected boolean isNotNetRetry = false; //断网情况是否重试 protected boolean isNotNetRetry = false; //断网情况是否重试
private boolean taskBreak = false; //任务中断 private boolean taskBreak = false; //任务跳出
protected int mThreadNum; protected int mThreadNum;
/** /**
* 速度限制工具 * 速度限制工具
*/ */
protected BandwidthLimiter mSpeedBandUtil; protected BandwidthLimiter mSpeedBandUtil;
protected AriaManager mAridManager; protected AriaManager mAridManager;
protected boolean isInterrupted = false;
private Thread mConfigThread = new Thread(new Runnable() { private Thread mConfigThread = new Thread(new Runnable() {
@Override public void run() { @Override public void run() {
@ -91,6 +92,23 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
} }
} }
/**
* 设置线程是否中断
* @param isInterrupted {@code true} 中断
*/
public void setInterrupted(boolean isInterrupted) {
this.isInterrupted = isInterrupted;
}
/**
* 线程是否存活
* @return {@code true}存活
*/
protected boolean isLive(){
Thread t = Thread.currentThread();
return !t.isInterrupted() && !isInterrupted;
}
/** /**
* 当前线程是否完成对于不支持断点的任务一律未完成 * 当前线程是否完成对于不支持断点的任务一律未完成
* {@code true} 完成{@code false} 未完成 * {@code true} 完成{@code false} 未完成
@ -435,4 +453,9 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
} }
} }
} }
@Override public AbsThreadTask call() throws Exception {
isInterrupted = true;
return this;
}
} }

@ -52,8 +52,8 @@ abstract class AbsDownloadTarget<TARGET extends AbsTarget, ENTITY extends AbsEnt
protected void setHighestPriority() { protected void setHighestPriority() {
if (checkEntity()) { if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, .setCmd(CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_HIGHEST_PRIORITY,
NormalCmdFactory.TASK_HIGHEST_PRIORITY, checkTaskType())) checkTaskType()))
.exe(); .exe();
} }
} }
@ -64,7 +64,7 @@ abstract class AbsDownloadTarget<TARGET extends AbsTarget, ENTITY extends AbsEnt
public void add() { public void add() {
if (checkEntity()) { if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CREATE, .setCmd(CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_CREATE,
checkTaskType())) checkTaskType()))
.exe(); .exe();
} }

@ -93,11 +93,9 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity> {
public static class Builder { public static class Builder {
DownloadGroupTaskEntity taskEntity; DownloadGroupTaskEntity taskEntity;
Handler outHandler; Handler outHandler;
String targetName;
public Builder(String targetName, DownloadGroupTaskEntity taskEntity) { public Builder(DownloadGroupTaskEntity taskEntity) {
CheckUtil.checkTaskEntity(taskEntity); CheckUtil.checkTaskEntity(taskEntity);
this.targetName = targetName;
this.taskEntity = taskEntity; this.taskEntity = taskEntity;
} }
@ -117,9 +115,7 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity> {
} }
public DownloadGroupTask build() { public DownloadGroupTask build() {
DownloadGroupTask task = new DownloadGroupTask(taskEntity, outHandler); return new DownloadGroupTask(taskEntity, outHandler);
task.setTargetName(targetName);
return task;
} }
} }
} }

@ -370,7 +370,7 @@ public class DownloadReceiver extends AbsReceiver {
public void stopAllTask() { public void stopAllTask() {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(NormalCmdFactory.getInstance() .setCmd(NormalCmdFactory.getInstance()
.createCmd(targetName, new DownloadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL, .createCmd(new DownloadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL,
ICmd.TASK_TYPE_DOWNLOAD)) ICmd.TASK_TYPE_DOWNLOAD))
.exe(); .exe();
} }
@ -383,7 +383,7 @@ public class DownloadReceiver extends AbsReceiver {
public void resumeAllTask() { public void resumeAllTask() {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(NormalCmdFactory.getInstance() .setCmd(NormalCmdFactory.getInstance()
.createCmd(targetName, new DownloadTaskEntity(), NormalCmdFactory.TASK_RESUME_ALL, .createCmd(new DownloadTaskEntity(), NormalCmdFactory.TASK_RESUME_ALL,
ICmd.TASK_TYPE_DOWNLOAD)) ICmd.TASK_TYPE_DOWNLOAD))
.exe(); .exe();
} }
@ -397,7 +397,7 @@ public class DownloadReceiver extends AbsReceiver {
public void removeAllTask(boolean removeFile) { public void removeAllTask(boolean removeFile) {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
CancelAllCmd cancelCmd = CancelAllCmd cancelCmd =
(CancelAllCmd) CommonUtil.createNormalCmd(targetName, new DownloadTaskEntity(), (CancelAllCmd) CommonUtil.createNormalCmd(new DownloadTaskEntity(),
NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_DOWNLOAD); NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_DOWNLOAD);
cancelCmd.removeFile = removeFile; cancelCmd.removeFile = removeFile;
ariaManager.setCmd(cancelCmd).exe(); ariaManager.setCmd(cancelCmd).exe();

@ -143,10 +143,8 @@ public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
public static class Builder { public static class Builder {
DownloadTaskEntity taskEntity; DownloadTaskEntity taskEntity;
Handler outHandler; Handler outHandler;
String targetName;
public Builder(String targetName, DownloadTaskEntity taskEntity) { public Builder(DownloadTaskEntity taskEntity) {
this.targetName = targetName;
this.taskEntity = taskEntity; this.taskEntity = taskEntity;
} }
@ -166,9 +164,7 @@ public class DownloadTask extends AbsNormalTask<DownloadTaskEntity> {
} }
public DownloadTask build() { public DownloadTask build() {
DownloadTask task = new DownloadTask(taskEntity, outHandler); return new DownloadTask(taskEntity, outHandler);
task.setTargetName(targetName);
return task;
} }
} }
} }

@ -31,13 +31,12 @@ import java.util.Set;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
/** /**
* Created by AriaL on 2017/6/30. * Created by AriaL on 2017/6/30.
* 任务组核心逻辑 * 任务组核心逻辑
*/ */
public abstract class AbsGroupUtil implements IUtil { public abstract class AbsGroupUtil implements IUtil, Runnable {
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private final String TAG = "AbsGroupUtil"; private final String TAG = "AbsGroupUtil";
/** /**
@ -286,6 +285,10 @@ public abstract class AbsGroupUtil implements IUtil {
} }
@Override public void start() { @Override public void start() {
new Thread(this).start();
}
@Override public void run() {
if (isStop || isCancel) { if (isStop || isCancel) {
closeTimer(); closeTimer();
return; return;

@ -53,10 +53,11 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
isBlock = STATE.TASK_RECORD.isBlock; isBlock = STATE.TASK_RECORD.isBlock;
} }
@Override public DownloadTaskEntity call() throws Exception { @Override public FtpThreadTask call() throws Exception {
super.call();
if (mConfig.THREAD_RECORD.isComplete) { if (mConfig.THREAD_RECORD.isComplete) {
handleComplete(); handleComplete();
return mTaskEntity; return this;
} }
mChildCurrentLocation = mConfig.START_LOCATION; mChildCurrentLocation = mConfig.START_LOCATION;
FTPClient client = null; FTPClient client = null;
@ -69,7 +70,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
client = createClient(); client = createClient();
if (client == null) { if (client == null) {
fail(mChildCurrentLocation, "ftp client 创建失败", null); fail(mChildCurrentLocation, "ftp client 创建失败", null);
return mTaskEntity; return this;
} }
if (mConfig.START_LOCATION > 0) { if (mConfig.START_LOCATION > 0) {
client.setRestartOffset(mConfig.START_LOCATION); client.setRestartOffset(mConfig.START_LOCATION);
@ -81,7 +82,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
String.format("获取文件信息错误,错误码为:%s,msg:%s", reply, client.getReplyString()), String.format("获取文件信息错误,错误码为:%s,msg:%s", reply, client.getReplyString()),
null); null);
client.disconnect(); client.disconnect();
return mTaskEntity; return this;
} }
String remotePath = String remotePath =
new String(mTaskEntity.getUrlEntity().remotePath.getBytes(charSet), SERVER_CHARSET); new String(mTaskEntity.getUrlEntity().remotePath.getBytes(charSet), SERVER_CHARSET);
@ -93,7 +94,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
String.format("获取流失败,错误码为:%s,msg:%s", reply, client.getReplyString()), String.format("获取流失败,错误码为:%s,msg:%s", reply, client.getReplyString()),
null); null);
client.disconnect(); client.disconnect();
return mTaskEntity; return this;
} }
if (isOpenDynamicFile) { if (isOpenDynamicFile) {
@ -118,7 +119,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
e.printStackTrace(); e.printStackTrace();
} }
} }
return mTaskEntity; return this;
} }
/** /**
@ -165,7 +166,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
foc = fos.getChannel(); foc = fos.getChannel();
fic = Channels.newChannel(is); fic = Channels.newChannel(is);
ByteBuffer bf = ByteBuffer.allocate(mBufSize); ByteBuffer bf = ByteBuffer.allocate(mBufSize);
while (!Thread.currentThread().isInterrupted() && (len = fic.read(bf)) != -1) { while (isLive() && (len = fic.read(bf)) != -1) {
if (isBreak()) { if (isBreak()) {
break; break;
} }
@ -216,7 +217,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
file.seek(mConfig.START_LOCATION); file.seek(mConfig.START_LOCATION);
byte[] buffer = new byte[mBufSize]; byte[] buffer = new byte[mBufSize];
int len; int len;
while (!Thread.currentThread().isInterrupted() && (len = is.read(buffer)) != -1) { while (isLive() && (len = is.read(buffer)) != -1) {
if (isBreak()) { if (isBreak()) {
break; break;
} }

@ -56,10 +56,11 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
isBlock = STATE.TASK_RECORD.isBlock; isBlock = STATE.TASK_RECORD.isBlock;
} }
@Override public DownloadTaskEntity call() { @Override public HttpThreadTask call() throws Exception {
super.call();
if (getThreadRecord().isComplete) { if (getThreadRecord().isComplete) {
handleComplete(); handleComplete();
return mTaskEntity; return this;
} }
HttpURLConnection conn = null; HttpURLConnection conn = null;
BufferedInputStream is = null; BufferedInputStream is = null;
@ -67,7 +68,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
//当前子线程的下载位置 //当前子线程的下载位置
mChildCurrentLocation = mConfig.START_LOCATION; mChildCurrentLocation = mConfig.START_LOCATION;
try { try {
URL url = new URL(CommonUtil.convertUrl(mConfig.URL + 1)); URL url = new URL(CommonUtil.convertUrl(mConfig.URL));
conn = ConnectionHelp.handleConnection(url, mTaskEntity); conn = ConnectionHelp.handleConnection(url, mTaskEntity);
if (mConfig.SUPPORT_BP) { if (mConfig.SUPPORT_BP) {
ALog.d(TAG, ALog.d(TAG,
@ -118,7 +119,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
e.printStackTrace(); e.printStackTrace();
} }
} }
return mTaskEntity; return this;
} }
/** /**
@ -135,7 +136,8 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
fic = Channels.newChannel(is); fic = Channels.newChannel(is);
ByteBuffer bf = ByteBuffer.allocate(mBufSize); ByteBuffer bf = ByteBuffer.allocate(mBufSize);
//如果要通过 Future 的 cancel 方法取消正在运行的任务,那么该任务必定是可以 对线程中断做出响应 的任务。 //如果要通过 Future 的 cancel 方法取消正在运行的任务,那么该任务必定是可以 对线程中断做出响应 的任务。
while (!Thread.currentThread().isInterrupted() && (len = fic.read(bf)) != -1) {
while (isLive() && (len = fic.read(bf)) != -1) {
if (isBreak()) { if (isBreak()) {
break; break;
} }
@ -194,7 +196,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
throws IOException { throws IOException {
byte[] buffer = new byte[mBufSize]; byte[] buffer = new byte[mBufSize];
int len; int len;
while (!Thread.currentThread().isInterrupted() && (len = is.read(buffer)) != -1) { while (isLive() && (len = is.read(buffer)) != -1) {
if (isBreak()) { if (isBreak()) {
break; break;
} }

@ -216,8 +216,8 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
@Override public void start() { @Override public void start() {
if (checkEntity()) { if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START, .setCmd(
checkTaskType())) CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_START, checkTaskType()))
.exe(); .exe();
} }
} }
@ -236,8 +236,8 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
@Override public void stop() { @Override public void stop() {
if (checkEntity()) { if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_STOP, .setCmd(
checkTaskType())) CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_STOP, checkTaskType()))
.exe(); .exe();
} }
} }
@ -248,8 +248,8 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
@Override public void resume() { @Override public void resume() {
if (checkEntity()) { if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START, .setCmd(
checkTaskType())) CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_START, checkTaskType()))
.exe(); .exe();
} }
} }
@ -260,7 +260,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
@Override public void cancel() { @Override public void cancel() {
if (checkEntity()) { if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CANCEL, .setCmd(CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_CANCEL,
checkTaskType())) checkTaskType()))
.exe(); .exe();
} }
@ -274,10 +274,8 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
List<ICmd> cmds = new ArrayList<>(); List<ICmd> cmds = new ArrayList<>();
int taskType = checkTaskType(); int taskType = checkTaskType();
cmds.add( cmds.add(
CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_STOP, CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_STOP, taskType));
taskType)); cmds.add(CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_START, taskType));
cmds.add(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_START,
taskType));
AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe(); AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe();
} }
} }
@ -290,8 +288,9 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
*/ */
public void cancel(boolean removeFile) { public void cancel(boolean removeFile) {
if (checkEntity()) { if (checkEntity()) {
CancelCmd cancelCmd = (CancelCmd) CommonUtil.createNormalCmd(mTargetName, mTaskEntity, CancelCmd cancelCmd =
NormalCmdFactory.TASK_CANCEL, checkTaskType()); (CancelCmd) CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_CANCEL,
checkTaskType());
cancelCmd.removeFile = removeFile; cancelCmd.removeFile = removeFile;
AriaManager.getInstance(AriaManager.APP).setCmd(cancelCmd).exe(); AriaManager.getInstance(AriaManager.APP).setCmd(cancelCmd).exe();
} }

@ -32,12 +32,8 @@ public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity> implements ITas
protected TASK_ENTITY mTaskEntity; protected TASK_ENTITY mTaskEntity;
protected Handler mOutHandler; protected Handler mOutHandler;
/**
* 用于生成该任务对象的hash码
*/
private String mTargetName;
protected Context mContext; protected Context mContext;
protected boolean isHeighestTask = false; boolean isHeighestTask = false;
private boolean isCancel = false, isStop = false; private boolean isCancel = false, isStop = false;
protected IUtil mUtil; protected IUtil mUtil;
@ -212,14 +208,6 @@ public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity> implements ITas
*/ */
public abstract String getTaskName(); public abstract String getTaskName();
public String getTargetName() {
return mTargetName;
}
@Override public void setTargetName(String targetName) {
this.mTargetName = targetName;
}
public boolean isHighestPriorityTask() { public boolean isHighestPriorityTask() {
return isHeighestTask; return isHeighestTask;
} }

@ -90,5 +90,4 @@ public interface ITask<TASK_ENTITY extends AbsTaskEntity> {
*/ */
String getConvertCurrentProgress(); String getConvertCurrentProgress();
void setTargetName(String targetName);
} }

@ -87,6 +87,8 @@ public class ThreadTaskManager {
if (future.isDone() || future.isCancelled()) { if (future.isDone() || future.isCancelled()) {
continue; continue;
} }
AbsThreadTask task = (AbsThreadTask) future.get();
task.setInterrupted(true);
future.cancel(true); future.cancel(true);
} }
} catch (Exception e) { } catch (Exception e) {

@ -187,7 +187,6 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
} }
@Override public void removeTaskFormQueue(String key) { @Override public void removeTaskFormQueue(String key) {
//TEManager.getInstance().removeTEntity(key);
TASK task = mExecutePool.getTask(key); TASK task = mExecutePool.getTask(key);
if (task != null) { if (task != null) {
ALog.d(TAG, String.format("从执行池删除任务【%s】%s", task.getTaskName(), ALog.d(TAG, String.format("从执行池删除任务【%s】%s", task.getTaskName(),
@ -205,10 +204,6 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
ALog.e(TAG, "任务重试失败,原因:task 为null"); ALog.e(TAG, "任务重试失败,原因:task 为null");
return; return;
} }
//if (!NetUtils.isConnected(AriaManager.APP)) {
// ALog.e(TAG, "任务【" + task.getTaskName() + "】重试失败,原因:网络未连接");
// return;
//}
if (!task.isRunning()) { if (!task.isRunning()) {
task.start(); task.start();
} else { } else {

@ -16,7 +16,6 @@
package com.arialyy.aria.core.queue; package com.arialyy.aria.core.queue;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
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;
@ -60,20 +59,16 @@ public class DownloadGroupTaskQueue
return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum();
} }
@Override public DownloadGroupTask createTask(String targetName, DownloadGroupTaskEntity entity) { @Override public DownloadGroupTask createTask(DownloadGroupTaskEntity entity) {
DownloadGroupTask task = null; DownloadGroupTask task = null;
if (!TextUtils.isEmpty(targetName)) {
if (mCachePool.getTask(entity.getEntity().getKey()) == null if (mCachePool.getTask(entity.getEntity().getKey()) == null
&& mExecutePool.getTask(entity.getEntity().getKey()) == null) { && mExecutePool.getTask(entity.getEntity().getKey()) == null) {
task = (DownloadGroupTask) TaskFactory.getInstance() task = (DownloadGroupTask) TaskFactory.getInstance()
.createTask(targetName, entity, DownloadGroupSchedulers.getInstance()); .createTask(entity, DownloadGroupSchedulers.getInstance());
mCachePool.putTask(task); mCachePool.putTask(task);
} else { } else {
ALog.w(TAG, "任务已存在"); ALog.w(TAG, "任务已存在");
} }
} else {
ALog.e(TAG, "target name 为 null!!");
}
return task; return task;
} }

@ -16,7 +16,6 @@
package com.arialyy.aria.core.queue; package com.arialyy.aria.core.queue;
import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
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;
@ -104,20 +103,16 @@ public class DownloadTaskQueue extends AbsTaskQueue<DownloadTask, DownloadTaskEn
} }
} }
@Override public DownloadTask createTask(String target, DownloadTaskEntity entity) { @Override public DownloadTask createTask(DownloadTaskEntity entity) {
DownloadTask task = null; DownloadTask task = null;
if (!TextUtils.isEmpty(target)) {
if (mCachePool.getTask(entity.getEntity().getKey()) == null if (mCachePool.getTask(entity.getEntity().getKey()) == null
&& mExecutePool.getTask(entity.getEntity().getKey()) == null) { && mExecutePool.getTask(entity.getEntity().getKey()) == null) {
task = (DownloadTask) TaskFactory.getInstance() task = (DownloadTask) TaskFactory.getInstance()
.createTask(target, entity, DownloadSchedulers.getInstance()); .createTask(entity, DownloadSchedulers.getInstance());
mCachePool.putTask(task); mCachePool.putTask(task);
} else { } else {
ALog.w(TAG, "任务已存在"); ALog.w(TAG, "任务已存在");
} }
} else {
ALog.e(TAG, "target name 为 null!!");
}
return task; return task;
} }

@ -113,10 +113,9 @@ public interface ITaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
* 创建一个新的任务创建时只是将新任务存储到缓存池 * 创建一个新的任务创建时只是将新任务存储到缓存池
* *
* @param entity 任务实体{@link DownloadTaskEntity}{@link UploadTaskEntity} * @param entity 任务实体{@link DownloadTaskEntity}{@link UploadTaskEntity}
* @param targetName 生成该任务的对象
* @return {@link DownloadTask}{@link UploadTask} * @return {@link DownloadTask}{@link UploadTask}
*/ */
TASK createTask(String targetName, TASK_ENTITY entity); TASK createTask(TASK_ENTITY entity);
/** /**
* 通过工作实体缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务 * 通过工作实体缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务

@ -21,8 +21,8 @@ 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.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.inf.ITask;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.core.scheduler.UploadSchedulers; import com.arialyy.aria.core.scheduler.UploadSchedulers;
@ -61,13 +61,13 @@ class TaskFactory {
* @return {@link DownloadTask}{@link UploadTask}{@link DownloadGroupTask} * @return {@link DownloadTask}{@link UploadTask}{@link DownloadGroupTask}
*/ */
<TASK_ENTITY extends AbsTaskEntity, SCHEDULER extends ISchedulers> ITask createTask( <TASK_ENTITY extends AbsTaskEntity, SCHEDULER extends ISchedulers> ITask createTask(
String targetName, TASK_ENTITY entity, SCHEDULER schedulers) { TASK_ENTITY entity, SCHEDULER schedulers) {
if (entity instanceof DownloadTaskEntity) { if (entity instanceof DownloadTaskEntity) {
return createDownloadTask(targetName, (DownloadTaskEntity) entity, schedulers); return createDownloadTask((DownloadTaskEntity) entity, schedulers);
} else if (entity instanceof UploadTaskEntity) { } else if (entity instanceof UploadTaskEntity) {
return createUploadTask(targetName, (UploadTaskEntity) entity, schedulers); return createUploadTask((UploadTaskEntity) entity, schedulers);
} else if (entity instanceof DownloadGroupTaskEntity) { } else if (entity instanceof DownloadGroupTaskEntity) {
return createDownloadGroupTask(targetName, (DownloadGroupTaskEntity) entity, schedulers); return createDownloadGroupTask((DownloadGroupTaskEntity) entity, schedulers);
} }
return null; return null;
} }
@ -78,9 +78,9 @@ class TaskFactory {
* @param entity 下载任务实体{@link DownloadGroupTask} * @param entity 下载任务实体{@link DownloadGroupTask}
* @param schedulers {@link ISchedulers} * @param schedulers {@link ISchedulers}
*/ */
private DownloadGroupTask createDownloadGroupTask(String targetName, private DownloadGroupTask createDownloadGroupTask(DownloadGroupTaskEntity entity,
DownloadGroupTaskEntity entity, ISchedulers schedulers) { ISchedulers schedulers) {
DownloadGroupTask.Builder builder = new DownloadGroupTask.Builder(targetName, entity); DownloadGroupTask.Builder builder = new DownloadGroupTask.Builder(entity);
builder.setOutHandler(schedulers); builder.setOutHandler(schedulers);
return builder.build(); return builder.build();
} }
@ -89,10 +89,8 @@ class TaskFactory {
* @param entity 上传任务实体{@link UploadTaskEntity} * @param entity 上传任务实体{@link UploadTaskEntity}
* @param schedulers {@link ISchedulers} * @param schedulers {@link ISchedulers}
*/ */
private UploadTask createUploadTask(String targetName, UploadTaskEntity entity, private UploadTask createUploadTask(UploadTaskEntity entity, ISchedulers schedulers) {
ISchedulers schedulers) {
UploadTask.Builder builder = new UploadTask.Builder(); UploadTask.Builder builder = new UploadTask.Builder();
builder.setTargetName(targetName);
builder.setUploadTaskEntity(entity); builder.setUploadTaskEntity(entity);
builder.setOutHandler(schedulers); builder.setOutHandler(schedulers);
return builder.build(); return builder.build();
@ -102,9 +100,8 @@ class TaskFactory {
* @param entity 下载任务实体{@link DownloadTaskEntity} * @param entity 下载任务实体{@link DownloadTaskEntity}
* @param schedulers {@link ISchedulers} * @param schedulers {@link ISchedulers}
*/ */
private DownloadTask createDownloadTask(String targetName, DownloadTaskEntity entity, private DownloadTask createDownloadTask(DownloadTaskEntity entity, ISchedulers schedulers) {
ISchedulers schedulers) { DownloadTask.Builder builder = new DownloadTask.Builder(entity);
DownloadTask.Builder builder = new DownloadTask.Builder(targetName, entity);
builder.setOutHandler(schedulers); builder.setOutHandler(schedulers);
return builder.build(); return builder.build();
} }

@ -16,7 +16,6 @@
package com.arialyy.aria.core.queue; package com.arialyy.aria.core.queue;
import android.text.TextUtils;
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.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool;
@ -62,20 +61,16 @@ public class UploadTaskQueue extends AbsTaskQueue<UploadTask, UploadTaskEntity>
return AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum(); return AriaManager.getInstance(AriaManager.APP).getUploadConfig().getMaxTaskNum();
} }
@Override public UploadTask createTask(String targetName, UploadTaskEntity entity) { @Override public UploadTask createTask(UploadTaskEntity entity) {
UploadTask task = null; UploadTask task = null;
if (!TextUtils.isEmpty(targetName)) {
if (mCachePool.getTask(entity.getEntity().getKey()) == null if (mCachePool.getTask(entity.getEntity().getKey()) == null
&& mExecutePool.getTask(entity.getEntity().getKey()) == null) { && mExecutePool.getTask(entity.getEntity().getKey()) == null) {
task = (UploadTask) TaskFactory.getInstance() task = (UploadTask) TaskFactory.getInstance()
.createTask(targetName, entity, UploadSchedulers.getInstance()); .createTask(entity, UploadSchedulers.getInstance());
mCachePool.putTask(task); mCachePool.putTask(task);
} else { } else {
ALog.w(TAG, "任务已存在"); ALog.w(TAG, "任务已存在");
} }
} else {
ALog.e(TAG, "target name 为 null是!!");
}
return task; return task;
} }
} }

@ -80,7 +80,7 @@ abstract class BaseNormalTarget<TARGET extends AbsUploadTarget>
mEntity.save(); mEntity.save();
mTaskEntity.save(); mTaskEntity.save();
} }
if (mTaskEntity.getUrlEntity().isFtps) { if (mTaskEntity.getUrlEntity() != null && mTaskEntity.getUrlEntity().isFtps) {
if (TextUtils.isEmpty(mTaskEntity.getUrlEntity().storePath)) { if (TextUtils.isEmpty(mTaskEntity.getUrlEntity().storePath)) {
ALog.e(TAG, "证书路径为空"); ALog.e(TAG, "证书路径为空");
return false; return false;

@ -54,7 +54,7 @@ public class FtpUploadTarget extends BaseNormalTarget<FtpUploadTarget>
public void add() { public void add() {
if (checkEntity()) { if (checkEntity()) {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createNormalCmd(mTargetName, mTaskEntity, NormalCmdFactory.TASK_CREATE, .setCmd(CommonUtil.createNormalCmd(mTaskEntity, NormalCmdFactory.TASK_CREATE,
checkTaskType())) checkTaskType()))
.exe(); .exe();
} }

@ -123,7 +123,7 @@ public class UploadReceiver extends AbsReceiver {
public void stopAllTask() { public void stopAllTask() {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(NormalCmdFactory.getInstance() .setCmd(NormalCmdFactory.getInstance()
.createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL, .createCmd(new UploadTaskEntity(), NormalCmdFactory.TASK_STOP_ALL,
ICmd.TASK_TYPE_UPLOAD)) ICmd.TASK_TYPE_UPLOAD))
.exe(); .exe();
} }
@ -137,7 +137,7 @@ public class UploadReceiver extends AbsReceiver {
public void removeAllTask(boolean removeFile) { public void removeAllTask(boolean removeFile) {
final AriaManager am = AriaManager.getInstance(AriaManager.APP); final AriaManager am = AriaManager.getInstance(AriaManager.APP);
CancelAllCmd cancelCmd = CancelAllCmd cancelCmd =
(CancelAllCmd) CommonUtil.createNormalCmd(targetName, new UploadTaskEntity(), (CancelAllCmd) CommonUtil.createNormalCmd(new UploadTaskEntity(),
NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_UPLOAD); NormalCmdFactory.TASK_CANCEL_ALL, ICmd.TASK_TYPE_UPLOAD);
cancelCmd.removeFile = removeFile; cancelCmd.removeFile = removeFile;
am.setCmd(cancelCmd).exe(); am.setCmd(cancelCmd).exe();

@ -50,7 +50,8 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
return mAridManager.getUploadConfig().getMaxSpeed(); return mAridManager.getUploadConfig().getMaxSpeed();
} }
@Override public UploadTaskEntity call() throws Exception { @Override public FtpThreadTask call() throws Exception {
super.call();
//当前子线程的下载位置 //当前子线程的下载位置
mChildCurrentLocation = mConfig.START_LOCATION; mChildCurrentLocation = mConfig.START_LOCATION;
FTPClient client = null; FTPClient client = null;
@ -61,7 +62,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
mConfig.THREAD_ID, mConfig.START_LOCATION, mConfig.END_LOCATION)); mConfig.THREAD_ID, mConfig.START_LOCATION, mConfig.END_LOCATION));
client = createClient(); client = createClient();
if (client == null) { if (client == null) {
return mTaskEntity; return this;
} }
initPath(); initPath();
client.makeDirectory(dir); client.makeDirectory(dir);
@ -71,7 +72,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.FILE_ACTION_OK) { if (!FTPReply.isPositivePreliminary(reply) && reply != FTPReply.FILE_ACTION_OK) {
fail(mChildCurrentLocation, "上传失败,错误码为:" + reply + ",msg:" + client.getReplyString(), null); fail(mChildCurrentLocation, "上传失败,错误码为:" + reply + ",msg:" + client.getReplyString(), null);
client.disconnect(); client.disconnect();
return mTaskEntity; return this;
} }
file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize); file = new BufferedRandomAccessFile(mConfig.TEMP_FILE, "rwd", mBufSize);
@ -81,7 +82,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
} }
upload(client, file); upload(client, file);
if (isBreak()) { if (isBreak()) {
return mTaskEntity; return this;
} }
ALog.i(TAG, ALog.i(TAG,
String.format("任务【%s】线程__%s__上传完毕", mConfig.TEMP_FILE.getName(), mConfig.THREAD_ID)); String.format("任务【%s】线程__%s__上传完毕", mConfig.TEMP_FILE.getName(), mConfig.THREAD_ID));
@ -112,7 +113,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
e.printStackTrace(); e.printStackTrace();
} }
} }
return mTaskEntity; return this;
} }
private void initPath() throws UnsupportedEncodingException { private void initPath() throws UnsupportedEncodingException {

@ -57,12 +57,13 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
isNotNetRetry = mAridManager.getUploadConfig().isNotNetRetry(); isNotNetRetry = mAridManager.getUploadConfig().isNotNetRetry();
} }
@Override public UploadTaskEntity call() throws Exception { @Override public HttpThreadTask call() throws Exception {
super.call();
File uploadFile = new File(mEntity.getFilePath()); File uploadFile = new File(mEntity.getFilePath());
if (!uploadFile.exists()) { if (!uploadFile.exists()) {
ALog.e(TAG, String.format("【%s】,文件不存在。", mEntity.getFilePath())); ALog.e(TAG, String.format("【%s】,文件不存在。", mEntity.getFilePath()));
fail(); fail();
return mTaskEntity; return this;
} }
mListener.onPre(); mListener.onPre();
URL url; URL url;
@ -103,7 +104,7 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
e.printStackTrace(); e.printStackTrace();
fail(); fail();
} }
return mTaskEntity; return this;
} }
private void fail() { private void fail() {
@ -200,7 +201,7 @@ class HttpThreadTask extends AbsThreadTask<UploadEntity, UploadTaskEntity> {
if (status == HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(mHttpConn.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(mHttpConn.getInputStream()));
String line; String line;
while (!Thread.currentThread().isInterrupted() && (line = reader.readLine()) != null) { while (isLive() && (line = reader.readLine()) != null) {
response.append(line); response.append(line);
} }
reader.close(); reader.close();

@ -689,9 +689,9 @@ public class CommonUtil {
* @param taskType {@link ICmd#TASK_TYPE_DOWNLOAD}{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}{@link * @param taskType {@link ICmd#TASK_TYPE_DOWNLOAD}{@link ICmd#TASK_TYPE_DOWNLOAD_GROUP}{@link
* ICmd#TASK_TYPE_UPLOAD} * ICmd#TASK_TYPE_UPLOAD}
*/ */
public static <T extends AbsTaskEntity> AbsNormalCmd createNormalCmd(String target, T entity, public static <T extends AbsTaskEntity> AbsNormalCmd createNormalCmd(T entity,
int cmd, int taskType) { int cmd, int taskType) {
return NormalCmdFactory.getInstance().createCmd(target, entity, cmd, taskType); return NormalCmdFactory.getInstance().createCmd(entity, cmd, taskType);
} }
/** /**

@ -32,8 +32,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/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) [![Download](https://api.bintray.com/packages/arialyy/maven/AriaCompiler/images/download.svg)](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion)
```java ```java
compile 'com.arialyy.aria:aria-core:3.4.12' compile 'com.arialyy.aria:aria-core:3.4.15'
annotationProcessor 'com.arialyy.aria:aria-compiler:3.4.12' annotationProcessor 'com.arialyy.aria:aria-compiler:3.4.15'
``` ```
如果出现android support,请将 `compile 'com.arialyy.aria:aria-core:<last-version>'`替换为 如果出现android support,请将 `compile 'com.arialyy.aria:aria-core:<last-version>'`替换为
``` ```

@ -83,7 +83,6 @@ public class UploadTask extends AbsNormalTask<UploadTaskEntity> {
public static class Builder { public static class Builder {
private Handler mOutHandler; private Handler mOutHandler;
private UploadTaskEntity mTaskEntity; private UploadTaskEntity mTaskEntity;
private String mTargetName;
public void setOutHandler(ISchedulers outHandler) { public void setOutHandler(ISchedulers outHandler) {
try { try {
@ -98,18 +97,12 @@ public class UploadTask extends AbsNormalTask<UploadTaskEntity> {
mTaskEntity = taskEntity; mTaskEntity = taskEntity;
} }
public void setTargetName(String targetName) {
mTargetName = targetName;
}
public Builder() { public Builder() {
} }
public UploadTask build() { public UploadTask build() {
UploadTask task = new UploadTask(mTaskEntity, mOutHandler); return new UploadTask(mTaskEntity, mOutHandler);
task.setTargetName(mTargetName);
return task;
} }
} }
} }

@ -39,7 +39,7 @@ task clean(type: Delete) {
ext { ext {
userOrg = 'arialyy' userOrg = 'arialyy'
groupId = 'com.arialyy.aria' groupId = 'com.arialyy.aria'
publishVersion = '3.4.12' publishVersion = '3.4.15'
// publishVersion = '1.0.3' //FTP插件 // publishVersion = '1.0.3' //FTP插件
repoName='maven' repoName='maven'
desc = 'android 下载框架' desc = 'android 下载框架'

Loading…
Cancel
Save