|
|
@ -15,22 +15,21 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package com.arialyy.aria.schedulers; |
|
|
|
package com.arialyy.aria.schedulers; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.Intent; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Message; |
|
|
|
import android.os.Message; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import com.arialyy.annotations.TaskEnum; |
|
|
|
import com.arialyy.annotations.TaskEnum; |
|
|
|
import com.arialyy.aria.core.AriaConfig; |
|
|
|
import com.arialyy.aria.core.AriaConfig; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.DuaContext; |
|
|
|
import com.arialyy.aria.core.common.AbsEntity; |
|
|
|
import com.arialyy.aria.core.common.AbsEntity; |
|
|
|
import com.arialyy.aria.core.common.AbsNormalEntity; |
|
|
|
import com.arialyy.aria.core.common.AbsNormalEntity; |
|
|
|
import com.arialyy.aria.core.group.GroupSendParams; |
|
|
|
import com.arialyy.aria.core.group.GroupSendParams; |
|
|
|
import com.arialyy.aria.core.inf.IEntity; |
|
|
|
import com.arialyy.aria.core.inf.IEntity; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.inf.ITaskQueue; |
|
|
|
import com.arialyy.aria.core.inf.TaskSchedulerType; |
|
|
|
import com.arialyy.aria.core.inf.TaskSchedulerType; |
|
|
|
import com.arialyy.aria.core.listener.ISchedulers; |
|
|
|
import com.arialyy.aria.core.listener.ISchedulers; |
|
|
|
import com.arialyy.aria.core.manager.TaskWrapperManager; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.queue.DGroupTaskQueue; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.queue.DTaskQueue; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.queue.ITaskQueue; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.queue.UTaskQueue; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.task.AbsTask; |
|
|
|
import com.arialyy.aria.core.task.AbsTask; |
|
|
|
import com.arialyy.aria.core.task.DownloadGroupTask; |
|
|
|
import com.arialyy.aria.core.task.DownloadGroupTask; |
|
|
|
import com.arialyy.aria.core.task.DownloadTask; |
|
|
|
import com.arialyy.aria.core.task.DownloadTask; |
|
|
@ -43,6 +42,7 @@ import java.util.Iterator; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
import timber.log.Timber; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Created by lyy on 2017/6/4. 事件调度器,用于处理任务状态的调度 |
|
|
|
* Created by lyy on 2017/6/4. 事件调度器,用于处理任务状态的调度 |
|
|
@ -77,15 +77,15 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param taskType 任务类型 |
|
|
|
* @param taskType 任务类型 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
ITaskQueue getQueue(int taskType) { |
|
|
|
ITaskQueue<ITask> getQueue(int taskType) { |
|
|
|
if (taskType == ITask.DOWNLOAD) { |
|
|
|
if (taskType == ITask.DOWNLOAD) { |
|
|
|
return DTaskQueue.getInstance(); |
|
|
|
return DuaContext.INSTANCE.getServiceManager().getDownloadQueue(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (taskType == ITask.DOWNLOAD_GROUP) { |
|
|
|
if (taskType == ITask.DOWNLOAD_GROUP) { |
|
|
|
return DGroupTaskQueue.getInstance(); |
|
|
|
return DuaContext.INSTANCE.getServiceManager().getDownloadGroupQueue(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (taskType == ITask.UPLOAD) { |
|
|
|
if (taskType == ITask.UPLOAD) { |
|
|
|
return UTaskQueue.getInstance(); |
|
|
|
return DuaContext.INSTANCE.getServiceManager().getUploadQueue(); |
|
|
|
} |
|
|
|
} |
|
|
|
throw new NullPointerException("任务类型错误,type = " + taskType); |
|
|
|
throw new NullPointerException("任务类型错误,type = " + taskType); |
|
|
|
} |
|
|
|
} |
|
|
@ -96,6 +96,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
* @param obj 观察者类 |
|
|
|
* @param obj 观察者类 |
|
|
|
* @param taskEnum 任务类型 {@link TaskEnum} |
|
|
|
* @param taskEnum 任务类型 {@link TaskEnum} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
public void register(Object obj, TaskEnum taskEnum) { |
|
|
|
public void register(Object obj, TaskEnum taskEnum) { |
|
|
|
String targetName = obj.getClass().getName(); |
|
|
|
String targetName = obj.getClass().getName(); |
|
|
|
Map<TaskEnum, Object> listeners = mObservers.get(getKey(obj)); |
|
|
|
Map<TaskEnum, Object> listeners = mObservers.get(getKey(obj)); |
|
|
@ -136,6 +137,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param obj 观察者类 |
|
|
|
* @param obj 观察者类 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
public void unRegister(Object obj) { |
|
|
|
public void unRegister(Object obj) { |
|
|
|
if (!mObservers.containsKey(getKey(obj))) { |
|
|
|
if (!mObservers.containsKey(getKey(obj))) { |
|
|
|
return; |
|
|
|
return; |
|
|
@ -303,32 +305,32 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
* 处理普通任务和任务组的事件 |
|
|
|
* 处理普通任务和任务组的事件 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void handleNormalEvent(TASK task, int what) { |
|
|
|
private void handleNormalEvent(TASK task, int what) { |
|
|
|
ITaskQueue queue = getQueue(task.getTaskType()); |
|
|
|
ITaskQueue<ITask> queue = getQueue(task.getTaskType()); |
|
|
|
switch (what) { |
|
|
|
switch (what) { |
|
|
|
case STOP: |
|
|
|
case STOP: |
|
|
|
if (task.getTaskState() == IEntity.STATE_WAIT) { |
|
|
|
queue.stopTask(task); |
|
|
|
break; |
|
|
|
if (!queue.isFull()) { |
|
|
|
} |
|
|
|
Timber.d("stop task success, if there is a task, start the next task, taskId: %s", |
|
|
|
queue.removeTaskFormQueue(task.getKey()); |
|
|
|
task.getTaskId()); |
|
|
|
if (queue.getCurrentExePoolNum() < queue.getMaxTaskNum()) { |
|
|
|
|
|
|
|
ALog.d(TAG, String.format("停止任务【%s】成功,尝试开始下一任务", task.getTaskName())); |
|
|
|
|
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ALog.d(TAG, String.format("停止任务【%s】成功", task.getTaskName())); |
|
|
|
Timber.d("stop task success, taskId: %s", task.getTaskId()); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case CANCEL: |
|
|
|
case CANCEL: |
|
|
|
queue.removeTaskFormQueue(task.getKey()); |
|
|
|
queue.removeTask(task); |
|
|
|
if (queue.getCurrentExePoolNum() < queue.getMaxTaskNum()) { |
|
|
|
|
|
|
|
ALog.d(TAG, String.format("删除任务【%s】成功,尝试开始下一任务", task.getTaskName())); |
|
|
|
if (!queue.isFull()) { |
|
|
|
|
|
|
|
Timber.d("remove task success, if there is a task, start the next task, taskId: %s", |
|
|
|
|
|
|
|
task.getTaskId()); |
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ALog.d(TAG, String.format("删除任务【%s】成功", task.getTaskName())); |
|
|
|
Timber.d("remove task success, taskId: %s", task.getTaskId()); |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case COMPLETE: |
|
|
|
case COMPLETE: |
|
|
|
queue.removeTaskFormQueue(task.getKey()); |
|
|
|
queue.removeTask(task); |
|
|
|
ALog.d(TAG, String.format("任务【%s】处理完成", task.getTaskName())); |
|
|
|
Timber.d("task complete, taskId: %s", task.getTaskId()); |
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case FAIL: |
|
|
|
case FAIL: |
|
|
@ -339,14 +341,6 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
if (what == FAIL || what == CHECK_FAIL) { |
|
|
|
if (what == FAIL || what == CHECK_FAIL) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (what == CANCEL || what == COMPLETE) { |
|
|
|
|
|
|
|
TaskWrapperManager.getInstance().removeTaskWrapper(task.getTaskWrapper()); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (what != RUNNING) { |
|
|
|
|
|
|
|
TaskWrapperManager.getInstance().putTaskWrapper(task.getTaskWrapper()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
normalTaskCallback(what, task); |
|
|
|
normalTaskCallback(what, task); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -425,7 +419,8 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void normalTaskCallback(int state, TASK task, NormalTaskListenerInterface<TASK> listener) { |
|
|
|
private void normalTaskCallback(int state, TASK task, |
|
|
|
|
|
|
|
NormalTaskListenerInterface<TASK> listener) { |
|
|
|
if (listener != null) { |
|
|
|
if (listener != null) { |
|
|
|
if (task == null && state != ISchedulers.CHECK_FAIL) { |
|
|
|
if (task == null && state != ISchedulers.CHECK_FAIL) { |
|
|
|
ALog.e(TAG, "TASK 为null,回调失败"); |
|
|
|
ALog.e(TAG, "TASK 为null,回调失败"); |
|
|
@ -518,22 +513,19 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
* |
|
|
|
* |
|
|
|
* @param task 下载任务 |
|
|
|
* @param task 下载任务 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void handleFailTask(final ITaskQueue queue, final TASK task) { |
|
|
|
private void handleFailTask(final ITaskQueue<ITask> queue, final TASK task) { |
|
|
|
if (!task.isNeedRetry() || task.isStop() || task.isCancel()) { |
|
|
|
if (!task.getTaskState().getNeedRetry() || task.isStop() || task.isCancel()) { |
|
|
|
queue.removeTaskFormQueue(task.getKey()); |
|
|
|
queue.removeTask(task); |
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
normalTaskCallback(FAIL, task); |
|
|
|
normalTaskCallback(FAIL, task); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int num = task.getTaskWrapper().getConfig().getReTryNum(); |
|
|
|
|
|
|
|
boolean isNotNetRetry = mAriaConfig.getAConfig().isNotNetRetry(); |
|
|
|
boolean isNotNetRetry = mAriaConfig.getAConfig().isNotNetRetry(); |
|
|
|
|
|
|
|
|
|
|
|
if ((!NetUtils.isConnected(mAriaConfig.getAPP()) && !isNotNetRetry) |
|
|
|
if ((!NetUtils.isConnected(mAriaConfig.getAPP()) && !isNotNetRetry)) { |
|
|
|
|| task.getTaskWrapper().getEntity().getFailNum() > num) { |
|
|
|
queue.removeTask(task); |
|
|
|
queue.removeTaskFormQueue(task.getKey()); |
|
|
|
|
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
startNextTask(queue, task.getSchedulerType()); |
|
|
|
TaskWrapperManager.getInstance().removeTaskWrapper(task.getTaskWrapper()); |
|
|
|
|
|
|
|
normalTaskCallback(FAIL, task); |
|
|
|
normalTaskCallback(FAIL, task); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -544,7 +536,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 启动下一个任务,条件:任务停止,取消下载,任务完成 |
|
|
|
* 启动下一个任务,条件:任务停止,取消下载,任务完成 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void startNextTask(final ITaskQueue queue, int schedulerType) { |
|
|
|
void startNextTask(final ITaskQueue<ITask> queue, int schedulerType) { |
|
|
|
if (schedulerType == TaskSchedulerType.TYPE_STOP_NOT_NEXT) { |
|
|
|
if (schedulerType == TaskSchedulerType.TYPE_STOP_NOT_NEXT) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -555,8 +547,12 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers { |
|
|
|
} |
|
|
|
} |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if (newTask.getTaskState() == IEntity.STATE_WAIT) { |
|
|
|
if (newTask.getTaskState().getState() == IEntity.STATE_WAIT) { |
|
|
|
queue.startTask(newTask); |
|
|
|
queue.startTask(newTask); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override public void init(@NonNull Context context) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|