任务组子任务控制
This commit is contained in:
@@ -17,6 +17,7 @@ package com.arialyy.aria.core.download;
|
||||
|
||||
import android.os.Handler;
|
||||
import com.arialyy.aria.core.download.downloader.IDownloadGroupListener;
|
||||
import com.arialyy.aria.core.inf.GroupSendParams;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
|
||||
/**
|
||||
@@ -26,13 +27,16 @@ import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
class DownloadGroupListener extends BaseDListener<DownloadGroupEntity, DownloadGroupTask>
|
||||
implements IDownloadGroupListener {
|
||||
private final String TAG = "DownloadGroupListener";
|
||||
private GroupSendParams<DownloadGroupTask, DownloadEntity> mSeedEntity;
|
||||
|
||||
DownloadGroupListener(DownloadGroupTask task, Handler outHandler) {
|
||||
super(task, outHandler);
|
||||
mSeedEntity = new GroupSendParams<>();
|
||||
mSeedEntity.groupTask = task;
|
||||
}
|
||||
|
||||
@Override public void onSubPre(DownloadEntity subEntity) {
|
||||
sendInState2Target(ISchedulers.SUB_PRE);
|
||||
sendInState2Target(ISchedulers.SUB_PRE, subEntity);
|
||||
}
|
||||
|
||||
@Override public void supportBreakpoint(boolean support, DownloadEntity subEntity) {
|
||||
@@ -40,31 +44,31 @@ class DownloadGroupListener extends BaseDListener<DownloadGroupEntity, DownloadG
|
||||
}
|
||||
|
||||
@Override public void onSubStart(DownloadEntity subEntity) {
|
||||
sendInState2Target(ISchedulers.SUB_START);
|
||||
sendInState2Target(ISchedulers.SUB_START, subEntity);
|
||||
}
|
||||
|
||||
@Override public void onSubStop(DownloadEntity subEntity) {
|
||||
saveCurrentLocation();
|
||||
sendInState2Target(ISchedulers.SUB_STOP);
|
||||
sendInState2Target(ISchedulers.SUB_STOP, subEntity);
|
||||
}
|
||||
|
||||
@Override public void onSubComplete(DownloadEntity subEntity) {
|
||||
saveCurrentLocation();
|
||||
sendInState2Target(ISchedulers.SUB_COMPLETE);
|
||||
sendInState2Target(ISchedulers.SUB_COMPLETE, subEntity);
|
||||
}
|
||||
|
||||
@Override public void onSubFail(DownloadEntity subEntity) {
|
||||
saveCurrentLocation();
|
||||
sendInState2Target(ISchedulers.SUB_FAIL);
|
||||
sendInState2Target(ISchedulers.SUB_FAIL, subEntity);
|
||||
}
|
||||
|
||||
@Override public void onSubCancel(DownloadEntity entity) {
|
||||
@Override public void onSubCancel(DownloadEntity subEntity) {
|
||||
saveCurrentLocation();
|
||||
sendInState2Target(ISchedulers.SUB_CANCEL);
|
||||
sendInState2Target(ISchedulers.SUB_CANCEL, subEntity);
|
||||
}
|
||||
|
||||
@Override public void onSubRunning(DownloadEntity subEntity) {
|
||||
sendInState2Target(ISchedulers.SUB_RUNNING);
|
||||
sendInState2Target(ISchedulers.SUB_RUNNING, subEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,9 +76,10 @@ class DownloadGroupListener extends BaseDListener<DownloadGroupEntity, DownloadG
|
||||
*
|
||||
* @param state {@link ISchedulers#START}
|
||||
*/
|
||||
private void sendInState2Target(int state) {
|
||||
private void sendInState2Target(int state, DownloadEntity subEntity) {
|
||||
if (outHandler.get() != null) {
|
||||
outHandler.get().obtainMessage(state, ISchedulers.IS_SUB_TASK, 0, mTask).sendToTarget();
|
||||
mSeedEntity.entity = subEntity;
|
||||
outHandler.get().obtainMessage(state, ISchedulers.IS_SUB_TASK, 0, mSeedEntity).sendToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.inf;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/9/8.
|
||||
* 任务组参数传递
|
||||
*/
|
||||
public class GroupSendParams<GROUP_TASK extends AbsGroupTask, ENTITY extends AbsNormalEntity> {
|
||||
|
||||
public GROUP_TASK groupTask;
|
||||
public ENTITY entity;
|
||||
|
||||
public GroupSendParams() {
|
||||
}
|
||||
|
||||
public GroupSendParams(GROUP_TASK groupTask, ENTITY entity) {
|
||||
this.groupTask = groupTask;
|
||||
this.entity = entity;
|
||||
}
|
||||
}
|
@@ -15,13 +15,13 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.scheduler;
|
||||
|
||||
import com.arialyy.aria.core.inf.AbsNormalTask;
|
||||
import com.arialyy.aria.core.inf.AbsNormalEntity;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/6/7.
|
||||
*/
|
||||
public class AbsSchedulerListener<TASK extends ITask, SUB_TASK extends AbsNormalTask> implements ISchedulerListener<TASK> {
|
||||
public class AbsSchedulerListener<TASK extends ITask, SUB_ENTITY extends AbsNormalEntity> implements ISchedulerListener<TASK> {
|
||||
|
||||
@Override public void onPre(TASK task) {
|
||||
|
||||
@@ -67,31 +67,31 @@ public class AbsSchedulerListener<TASK extends ITask, SUB_TASK extends AbsNormal
|
||||
|
||||
}
|
||||
|
||||
public void onSubTaskPre(TASK task, SUB_TASK subTask) {
|
||||
public void onSubTaskPre(TASK task, SUB_ENTITY subTask) {
|
||||
|
||||
}
|
||||
|
||||
public void onSubTaskStart(TASK task, SUB_TASK subTask) {
|
||||
public void onSubTaskStart(TASK task, SUB_ENTITY subTask) {
|
||||
|
||||
}
|
||||
|
||||
public void onSubTaskStop(TASK task, SUB_TASK subTask) {
|
||||
public void onSubTaskStop(TASK task, SUB_ENTITY subTask) {
|
||||
|
||||
}
|
||||
|
||||
public void onSubTaskCancel(TASK task, SUB_TASK subTask) {
|
||||
public void onSubTaskCancel(TASK task, SUB_ENTITY subTask) {
|
||||
|
||||
}
|
||||
|
||||
public void onSubTaskComplete(TASK task, SUB_TASK subTask) {
|
||||
public void onSubTaskComplete(TASK task, SUB_ENTITY subTask) {
|
||||
|
||||
}
|
||||
|
||||
public void onSubTaskFail(TASK task, SUB_TASK subTask) {
|
||||
public void onSubTaskFail(TASK task, SUB_ENTITY subTask) {
|
||||
|
||||
}
|
||||
|
||||
public void onSubTaskRunning(TASK task, SUB_TASK subTask) {
|
||||
public void onSubTaskRunning(TASK task, SUB_ENTITY subTask) {
|
||||
|
||||
}
|
||||
}
|
@@ -21,9 +21,10 @@ import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
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.inf.AbsNormalEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTask;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.inf.GroupSendParams;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.queue.ITaskQueue;
|
||||
import com.arialyy.aria.core.upload.UploadTask;
|
||||
@@ -46,7 +47,8 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
|
||||
|
||||
protected QUEUE mQueue;
|
||||
|
||||
private Map<String, AbsSchedulerListener<TASK, AbsNormalTask>> mObservers = new ConcurrentHashMap<>();
|
||||
private Map<String, AbsSchedulerListener<TASK, AbsNormalEntity>> mObservers =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 设置调度器类型
|
||||
@@ -60,7 +62,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
|
||||
|
||||
@Override public void register(Object obj) {
|
||||
String targetName = obj.getClass().getName();
|
||||
AbsSchedulerListener<TASK, AbsNormalTask> listener = mObservers.get(targetName);
|
||||
AbsSchedulerListener<TASK, AbsNormalEntity> listener = mObservers.get(targetName);
|
||||
if (listener == null) {
|
||||
listener = createListener(targetName);
|
||||
if (listener != null) {
|
||||
@@ -73,9 +75,9 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
|
||||
}
|
||||
|
||||
@Override public void unRegister(Object obj) {
|
||||
for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK, AbsNormalTask>>> iter =
|
||||
for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK, AbsNormalEntity>>> iter =
|
||||
mObservers.entrySet().iterator(); iter.hasNext(); ) {
|
||||
Map.Entry<String, AbsSchedulerListener<TASK, AbsNormalTask>> entry = iter.next();
|
||||
Map.Entry<String, AbsSchedulerListener<TASK, AbsNormalEntity>> entry = iter.next();
|
||||
if (entry.getKey().equals(obj.getClass().getName())) iter.remove();
|
||||
}
|
||||
}
|
||||
@@ -85,11 +87,11 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
|
||||
*
|
||||
* @param targetName 通过观察者创建对应的Aria事件代理
|
||||
*/
|
||||
private AbsSchedulerListener<TASK, AbsNormalTask> createListener(String targetName) {
|
||||
AbsSchedulerListener<TASK, AbsNormalTask> listener = null;
|
||||
private AbsSchedulerListener<TASK, AbsNormalEntity> createListener(String targetName) {
|
||||
AbsSchedulerListener<TASK, AbsNormalEntity> listener = null;
|
||||
try {
|
||||
Class clazz = Class.forName(targetName + getProxySuffix());
|
||||
listener = (AbsSchedulerListener<TASK, AbsNormalTask>) clazz.newInstance();
|
||||
listener = (AbsSchedulerListener<TASK, AbsNormalEntity>) clazz.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
Log.e(TAG, targetName + ",没有Aria的Download或Upload注解方法");
|
||||
} catch (InstantiationException e) {
|
||||
@@ -101,49 +103,54 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
|
||||
}
|
||||
|
||||
@Override public boolean handleMessage(Message msg) {
|
||||
if (msg.arg1 == IS_SUB_TASK) {
|
||||
return handleSubEvent(msg);
|
||||
}
|
||||
|
||||
TASK task = (TASK) msg.obj;
|
||||
if (task == null) {
|
||||
Log.e(TAG, "请传入下载任务");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (msg.arg1 == IS_SUB_TASK) {
|
||||
handleSubEvent(task, msg.what);
|
||||
} else {
|
||||
handleNormalEvent(task, msg.what);
|
||||
}
|
||||
|
||||
handleNormalEvent(task, msg.what);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理任务组子任务事件
|
||||
*/
|
||||
private void handleSubEvent(TASK task, int what) {
|
||||
ENTITY entity = task.getEntity();
|
||||
private boolean handleSubEvent(Message msg) {
|
||||
GroupSendParams params = (GroupSendParams) msg.obj;
|
||||
if (mObservers.size() > 0) {
|
||||
Set<String> keys = mObservers.keySet();
|
||||
for (String key : keys) {
|
||||
AbsSchedulerListener<TASK, AbsNormalTask> listener = mObservers.get(key);
|
||||
switch (what) {
|
||||
AbsSchedulerListener<TASK, AbsNormalEntity> listener = mObservers.get(key);
|
||||
switch (msg.what) {
|
||||
case SUB_PRE:
|
||||
//listener.onSubTaskPre(task, );
|
||||
listener.onSubTaskPre((TASK) params.groupTask, params.entity);
|
||||
break;
|
||||
case SUB_START:
|
||||
listener.onSubTaskStart((TASK) params.groupTask, params.entity);
|
||||
break;
|
||||
case SUB_STOP:
|
||||
listener.onSubTaskStop((TASK) params.groupTask, params.entity);
|
||||
break;
|
||||
case SUB_FAIL:
|
||||
listener.onSubTaskFail((TASK) params.groupTask, params.entity);
|
||||
break;
|
||||
case SUB_RUNNING:
|
||||
listener.onSubTaskRunning((TASK) params.groupTask, params.entity);
|
||||
break;
|
||||
case SUB_CANCEL:
|
||||
listener.onSubTaskCancel((TASK) params.groupTask, params.entity);
|
||||
break;
|
||||
case SUB_COMPLETE:
|
||||
listener.onSubTaskComplete((TASK) params.groupTask, params.entity);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,7 +196,8 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
|
||||
}
|
||||
}
|
||||
|
||||
private void callback(int state, TASK task, AbsSchedulerListener<TASK, AbsNormalTask> listener) {
|
||||
private void callback(int state, TASK task,
|
||||
AbsSchedulerListener<TASK, AbsNormalEntity> listener) {
|
||||
if (listener != null) {
|
||||
if (task == null) {
|
||||
Log.e(TAG, "TASK 为null,回调失败");
|
||||
|
Reference in New Issue
Block a user