添加通过父类接口判断来检查是某种下载任务类型

pull/709/head
陈斐 4 years ago
parent 981917fa52
commit a3df4016ff
  1. 50
      Aria/src/main/java/com/arialyy/aria/core/common/ProxyHelper.java
  2. 11
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTaskListener.java
  3. 11
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskListener.java
  4. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/BaseListenerInterface.java
  5. 8
      Aria/src/main/java/com/arialyy/aria/core/scheduler/M3U8PeerTaskListener.java
  6. 29
      Aria/src/main/java/com/arialyy/aria/core/scheduler/M3U8PeerTaskListenerInterface.java
  7. 24
      Aria/src/main/java/com/arialyy/aria/core/scheduler/NormalTaskListener.java
  8. 81
      Aria/src/main/java/com/arialyy/aria/core/scheduler/NormalTaskListenerInterface.java
  9. 18
      Aria/src/main/java/com/arialyy/aria/core/scheduler/SubTaskListener.java
  10. 42
      Aria/src/main/java/com/arialyy/aria/core/scheduler/SubTaskListenerInterface.java
  11. 42
      Aria/src/main/java/com/arialyy/aria/core/scheduler/TaskSchedulers.java
  12. 13
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskListener.java

@ -16,6 +16,13 @@
package com.arialyy.aria.core.common;
import com.arialyy.annotations.TaskEnum;
import com.arialyy.aria.core.download.DownloadGroupTaskListener;
import com.arialyy.aria.core.download.DownloadTaskListener;
import com.arialyy.aria.core.scheduler.BaseListenerInterface;
import com.arialyy.aria.core.scheduler.M3U8PeerTaskListenerInterface;
import com.arialyy.aria.core.scheduler.SubTaskListenerInterface;
import com.arialyy.aria.core.upload.UploadTaskListener;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -69,12 +76,25 @@ public class ProxyHelper {
* @return {@link #PROXY_TYPE_DOWNLOAD}如果没有实体对象则返回空的list
*/
public Set<Integer> checkProxyType(Class clazz) {
final String className = clazz.getName();
Set<Integer> result = mProxyCache.get(clazz.getName());
if (result != null) {
return result;
}
result = new HashSet<>();
result = checkProxyTypeByInterface(clazz);
if (result != null) {
return result;
}
result = checkProxyTypeByProxyClass(clazz);
if (!result.isEmpty()) {
mProxyCache.put(clazz.getName(), result);
}
return result;
}
private Set<Integer> checkProxyTypeByProxyClass(Class clazz) {
final String className = clazz.getName();
Set<Integer> result = new HashSet<>();
if (checkProxyExist(className, TaskEnum.DOWNLOAD_GROUP.proxySuffix)) {
result.add(PROXY_TYPE_DOWNLOAD_GROUP);
}
@ -93,9 +113,31 @@ public class ProxyHelper {
if (checkProxyExist(className, TaskEnum.DOWNLOAD_GROUP_SUB.proxySuffix)) {
result.add(PROXY_TYPE_DOWNLOAD_GROUP_SUB);
}
return result;
}
if (!result.isEmpty()) {
mProxyCache.put(clazz.getName(), result);
private Set<Integer> checkProxyTypeByInterface(Class clazz) {
if (clazz.isAssignableFrom(BaseListenerInterface.class)) {
return null;
}
Set<Integer> result = new HashSet<>();
if (clazz.isAssignableFrom(DownloadGroupTaskListener.class)) {
result.add(PROXY_TYPE_DOWNLOAD_GROUP);
}
if (clazz.isAssignableFrom(DownloadTaskListener.class)) {
result.add(PROXY_TYPE_DOWNLOAD);
}
if (clazz.isAssignableFrom(UploadTaskListener.class)) {
result.add(PROXY_TYPE_UPLOAD);
}
if (clazz.isAssignableFrom(M3U8PeerTaskListenerInterface.class)) {
result.add(PROXY_TYPE_M3U8_PEER);
}
if (clazz.isAssignableFrom(SubTaskListenerInterface.class)) {
result.add(PROXY_TYPE_DOWNLOAD_GROUP_SUB);
}
return result;
}

@ -0,0 +1,11 @@
package com.arialyy.aria.core.download;
import com.arialyy.aria.core.scheduler.NormalTaskListenerInterface;
import com.arialyy.aria.core.task.DownloadGroupTask;
/**
* @author ChenFei(chenfei0928 @ gmail.com)
* @date 2020-07-07 14:12
*/
public interface DownloadGroupTaskListener extends NormalTaskListenerInterface<DownloadGroupTask> {
}

@ -0,0 +1,11 @@
package com.arialyy.aria.core.download;
import com.arialyy.aria.core.scheduler.NormalTaskListenerInterface;
import com.arialyy.aria.core.task.DownloadTask;
/**
* @author ChenFei(chenfei0928 @ gmail.com)
* @date 2020-07-07 14:12
*/
public interface DownloadTaskListener extends NormalTaskListenerInterface<DownloadTask> {
}

@ -0,0 +1,8 @@
package com.arialyy.aria.core.scheduler;
/**
* @author ChenFei(chenfei0928 @ gmail.com)
* @date 2020-07-07 15:18
*/
public interface BaseListenerInterface {
}

@ -19,15 +19,15 @@ package com.arialyy.aria.core.scheduler;
* Created by Aria.Lao on 2019/6/26.
* m3u8切片事件回调类
*/
public class M3U8PeerTaskListener implements ISchedulerListener {
public class M3U8PeerTaskListener implements M3U8PeerTaskListenerInterface, ISchedulerListener{
public void onPeerStart(final String m3u8Url, final String peerPath, final int peerIndex) {
@Override public void onPeerStart(final String m3u8Url, final String peerPath, final int peerIndex) {
}
public void onPeerComplete(final String m3u8Url, final String peerPath, final int peerIndex) {
@Override public void onPeerComplete(final String m3u8Url, final String peerPath, final int peerIndex) {
}
public void onPeerFail(final String m3u8Url, final String peerPath, final int peerIndex) {
@Override public void onPeerFail(final String m3u8Url, final String peerPath, final int peerIndex) {
}
@Override public void setListener(Object obj) {

@ -0,0 +1,29 @@
/*
* 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.scheduler;
/**
* Created by Aria.Lao on 2019/6/26.
* m3u8切片事件回调类
*/
public interface M3U8PeerTaskListenerInterface extends BaseListenerInterface {
public void onPeerStart(final String m3u8Url, final String peerPath, final int peerIndex);
public void onPeerComplete(final String m3u8Url, final String peerPath, final int peerIndex);
public void onPeerFail(final String m3u8Url, final String peerPath, final int peerIndex);
}

@ -24,12 +24,12 @@ import com.arialyy.aria.core.task.UploadTask;
* Created by Aria.Lao on 2017/6/7.
* 普通任务事件{@link DownloadTask}{@link UploadTask}{@link DownloadGroupTask}回调类
*/
public class NormalTaskListener<TASK extends ITask> implements ISchedulerListener {
public class NormalTaskListener<TASK extends ITask> implements NormalTaskListenerInterface<TASK>, ISchedulerListener {
/**
* 队列已经满了继续创建任务将会回调该方法
*/
public void onWait(TASK task) {
@Override public void onWait(TASK task) {
}
@ -37,42 +37,42 @@ public class NormalTaskListener<TASK extends ITask> implements ISchedulerListene
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态
* 在这个回调中任务是获取不到文件大小下载速度等参数
*/
public void onPre(TASK task) {
@Override public void onPre(TASK task) {
}
/**
* 任务预加载完成
*/
public void onTaskPre(TASK task) {
@Override public void onTaskPre(TASK task) {
}
/**
* 任务恢复下载
*/
public void onTaskResume(TASK task) {
@Override public void onTaskResume(TASK task) {
}
/**
* 任务开始
*/
public void onTaskStart(TASK task) {
@Override public void onTaskStart(TASK task) {
}
/**
* 任务停止
*/
public void onTaskStop(TASK task) {
@Override public void onTaskStop(TASK task) {
}
/**
* 任务取消
*/
public void onTaskCancel(TASK task) {
@Override public void onTaskCancel(TASK task) {
}
@ -88,25 +88,25 @@ public class NormalTaskListener<TASK extends ITask> implements ISchedulerListene
/**
* 任务失败
*/
public void onTaskFail(TASK task, Exception e) {
@Override public void onTaskFail(TASK task, Exception e) {
}
/**
* 任务完成
*/
public void onTaskComplete(TASK task) {
@Override public void onTaskComplete(TASK task) {
}
/**
* 任务执行中
*/
public void onTaskRunning(TASK task) {
@Override public void onTaskRunning(TASK task) {
}
public void onNoSupportBreakPoint(TASK task) {
@Override public void onNoSupportBreakPoint(TASK task) {
}

@ -0,0 +1,81 @@
/*
* 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.scheduler;
import com.arialyy.aria.core.task.DownloadGroupTask;
import com.arialyy.aria.core.task.DownloadTask;
import com.arialyy.aria.core.task.ITask;
import com.arialyy.aria.core.task.UploadTask;
/**
* Created by Aria.Lao on 2017/6/7.
* 普通任务事件{@link DownloadTask}{@link UploadTask}{@link DownloadGroupTask}回调类
*/
public interface NormalTaskListenerInterface<TASK extends ITask> extends BaseListenerInterface {
/**
* 队列已经满了继续创建任务将会回调该方法
*/
public void onWait(TASK task);
/**
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态
* 在这个回调中任务是获取不到文件大小下载速度等参数
*/
public void onPre(TASK task);
/**
* 任务预加载完成
*/
public void onTaskPre(TASK task);
/**
* 任务恢复下载
*/
public void onTaskResume(TASK task);
/**
* 任务开始
*/
public void onTaskStart(TASK task);
/**
* 任务停止
*/
public void onTaskStop(TASK task);
/**
* 任务取消
*/
public void onTaskCancel(TASK task);
/**
* 任务失败
*/
public void onTaskFail(TASK task, Exception e);
/**
* 任务完成
*/
public void onTaskComplete(TASK task);
/**
* 任务执行中
*/
public void onTaskRunning(TASK task);
public void onNoSupportBreakPoint(TASK task);
}

@ -23,29 +23,29 @@ import com.arialyy.aria.core.task.ITask;
* 子任务事件回调类
*/
public class SubTaskListener<TASK extends ITask, SUB_ENTITY extends AbsNormalEntity>
implements ISchedulerListener {
implements SubTaskListenerInterface<TASK, SUB_ENTITY>, ISchedulerListener {
public void onNoSupportBreakPoint(TASK task) {
@Override public void onNoSupportBreakPoint(TASK task) {
}
public void onSubTaskPre(TASK task, SUB_ENTITY subTask) {
@Override public void onSubTaskPre(TASK task, SUB_ENTITY subTask) {
}
public void onSubTaskStart(TASK task, SUB_ENTITY subTask) {
@Override public void onSubTaskStart(TASK task, SUB_ENTITY subTask) {
}
public void onSubTaskStop(TASK task, SUB_ENTITY subTask) {
@Override public void onSubTaskStop(TASK task, SUB_ENTITY subTask) {
}
public void onSubTaskCancel(TASK task, SUB_ENTITY subTask) {
@Override public void onSubTaskCancel(TASK task, SUB_ENTITY subTask) {
}
public void onSubTaskComplete(TASK task, SUB_ENTITY subTask) {
@Override public void onSubTaskComplete(TASK task, SUB_ENTITY subTask) {
}
@ -54,11 +54,11 @@ public class SubTaskListener<TASK extends ITask, SUB_ENTITY extends AbsNormalEnt
}
public void onSubTaskFail(TASK task, SUB_ENTITY subTask, Exception e) {
@Override public void onSubTaskFail(TASK task, SUB_ENTITY subTask, Exception e) {
}
public void onSubTaskRunning(TASK task, SUB_ENTITY subTask) {
@Override public void onSubTaskRunning(TASK task, SUB_ENTITY subTask) {
}

@ -0,0 +1,42 @@
/*
* 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.scheduler;
import com.arialyy.aria.core.common.AbsNormalEntity;
import com.arialyy.aria.core.task.ITask;
/**
* Created by Aria.Lao on 2019/6/26.
* 子任务事件回调类
*/
public interface SubTaskListenerInterface<TASK extends ITask, SUB_ENTITY extends AbsNormalEntity> extends BaseListenerInterface {
public void onNoSupportBreakPoint(TASK task);
public void onSubTaskPre(TASK task, SUB_ENTITY subTask);
public void onSubTaskStart(TASK task, SUB_ENTITY subTask);
public void onSubTaskStop(TASK task, SUB_ENTITY subTask);
public void onSubTaskCancel(TASK task, SUB_ENTITY subTask);
public void onSubTaskComplete(TASK task, SUB_ENTITY subTask);
public void onSubTaskFail(TASK task, SUB_ENTITY subTask, Exception e);
public void onSubTaskRunning(TASK task, SUB_ENTITY subTask);
}

@ -54,7 +54,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
private static volatile TaskSchedulers INSTANCE;
private static FailureTaskHandler mFailureTaskHandler;
private Map<String, Map<TaskEnum, ISchedulerListener>> mObservers = new ConcurrentHashMap<>();
private Map<String, Map<TaskEnum, Object>> mObservers = new ConcurrentHashMap<>();
private AriaConfig mAriaConfig;
private TaskSchedulers() {
@ -99,15 +99,19 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
*/
public void register(Object obj, TaskEnum taskEnum) {
String targetName = obj.getClass().getName();
Map<TaskEnum, ISchedulerListener> listeners = mObservers.get(getKey(obj));
Map<TaskEnum, Object> listeners = mObservers.get(getKey(obj));
if (listeners == null) {
listeners = new ConcurrentHashMap<>();
mObservers.put(getKey(obj), listeners);
}
String proxyClassName = targetName + taskEnum.proxySuffix;
if (!hasProxyListener(listeners, taskEnum)) {
if (obj instanceof BaseListenerInterface){
listeners.put(taskEnum, (ISchedulerListener) obj);
return;
}
String proxyClassName = targetName + taskEnum.proxySuffix;
ISchedulerListener listener = createListener(proxyClassName);
if (listener != null) {
listener.setListener(obj);
@ -124,7 +128,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
* @param taskEnum 代理类类型
* @return true已注册代理类false没有注册代理类
*/
private boolean hasProxyListener(Map<TaskEnum, ISchedulerListener> listeners, TaskEnum taskEnum) {
private boolean hasProxyListener(Map<TaskEnum, Object> listeners, TaskEnum taskEnum) {
return !listeners.isEmpty() && listeners.get(taskEnum) != null;
}
@ -137,9 +141,9 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
if (!mObservers.containsKey(getKey(obj))) {
return;
}
for (Iterator<Map.Entry<String, Map<TaskEnum, ISchedulerListener>>> iter =
for (Iterator<Map.Entry<String, Map<TaskEnum, Object>>> iter =
mObservers.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, Map<TaskEnum, ISchedulerListener>> entry = iter.next();
Map.Entry<String, Map<TaskEnum, Object>> entry = iter.next();
if (entry.getKey().equals(getKey(obj))) {
iter.remove();
@ -202,12 +206,12 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
if (mObservers.size() > 0) {
Set<String> keys = mObservers.keySet();
for (String key : keys) {
Map<TaskEnum, ISchedulerListener> listeners = mObservers.get(key);
Map<TaskEnum, Object> listeners = mObservers.get(key);
if (listeners == null || listeners.isEmpty()) {
continue;
}
M3U8PeerTaskListener listener =
(M3U8PeerTaskListener) listeners.get(TaskEnum.M3U8_PEER);
M3U8PeerTaskListenerInterface listener =
(M3U8PeerTaskListenerInterface) listeners.get(TaskEnum.M3U8_PEER);
if (listener == null) {
continue;
}
@ -250,12 +254,12 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
if (mObservers.size() > 0) {
Set<String> keys = mObservers.keySet();
for (String key : keys) {
Map<TaskEnum, ISchedulerListener> listeners = mObservers.get(key);
Map<TaskEnum, Object> listeners = mObservers.get(key);
if (listeners == null || listeners.isEmpty()) {
continue;
}
SubTaskListener<TASK, AbsNormalEntity> listener =
(SubTaskListener<TASK, AbsNormalEntity>) listeners.get(TaskEnum.DOWNLOAD_GROUP_SUB);
SubTaskListenerInterface<TASK, AbsNormalEntity> listener =
(SubTaskListenerInterface<TASK, AbsNormalEntity>) listeners.get(TaskEnum.DOWNLOAD_GROUP_SUB);
if (listener == null) {
continue;
}
@ -368,18 +372,18 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
if (mObservers.size() > 0) {
Set<String> keys = mObservers.keySet();
for (String key : keys) {
Map<TaskEnum, ISchedulerListener> listeners = mObservers.get(key);
Map<TaskEnum, Object> listeners = mObservers.get(key);
if (listeners == null || listeners.isEmpty()) {
continue;
}
NormalTaskListener<TASK> listener = null;
NormalTaskListenerInterface<TASK> listener = null;
if (mObservers.get(key) != null) {
if (taskType == ITask.DOWNLOAD) {
listener = (NormalTaskListener<TASK>) listeners.get(TaskEnum.DOWNLOAD);
listener = (NormalTaskListenerInterface<TASK>) listeners.get(TaskEnum.DOWNLOAD);
} else if (taskType == ITask.DOWNLOAD_GROUP) {
listener = (NormalTaskListener<TASK>) listeners.get(TaskEnum.DOWNLOAD_GROUP);
listener = (NormalTaskListenerInterface<TASK>) listeners.get(TaskEnum.DOWNLOAD_GROUP);
} else if (taskType == ITask.DOWNLOAD_GROUP) {
listener = (NormalTaskListener<TASK>) listeners.get(TaskEnum.UPLOAD);
listener = (NormalTaskListenerInterface<TASK>) listeners.get(TaskEnum.UPLOAD);
}
}
if (listener != null) {
@ -399,7 +403,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
if (mObservers.size() > 0) {
Set<String> keys = mObservers.keySet();
for (String key : keys) {
Map<TaskEnum, ISchedulerListener> listeners = mObservers.get(key);
Map<TaskEnum, Object> listeners = mObservers.get(key);
if (listeners == null || listeners.isEmpty()) {
continue;
}
@ -420,7 +424,7 @@ public class TaskSchedulers<TASK extends ITask> implements ISchedulers {
}
}
private void normalTaskCallback(int state, TASK task, NormalTaskListener<TASK> listener) {
private void normalTaskCallback(int state, TASK task, NormalTaskListenerInterface<TASK> listener) {
if (listener != null) {
if (task == null && state != ISchedulers.CHECK_FAIL) {
ALog.e(TAG, "TASK 为null,回调失败");

@ -0,0 +1,13 @@
package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.scheduler.NormalTaskListenerInterface;
import com.arialyy.aria.core.task.UploadTask;
/**
* 上传任务接口
*
* @author ChenFei(chenfei0928 @ gmail.com)
* @date 2020-07-07 13:23
*/
public interface UploadTaskListener extends NormalTaskListenerInterface<UploadTask> {
}
Loading…
Cancel
Save