增加任务组的注解事件

This commit is contained in:
AriaLyy
2017-07-10 17:26:54 +08:00
parent 2f94560c9b
commit c948aa7555
18 changed files with 731 additions and 238 deletions

View File

@ -17,14 +17,11 @@ package com.arialyy.aria.core.download;
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsGroupTarget;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Created by AriaL on 2017/6/29.
@ -35,7 +32,7 @@ public class DownloadGroupTarget
/**
* 子任务文件名
*/
private List<String> mSubtaskFileName = new ArrayList<>();
private List<String> mSubTaskFileName = new ArrayList<>();
private String mGroupName;
/**
* 是否已经设置了文件路径
@ -138,7 +135,7 @@ public class DownloadGroupTarget
*/
public DownloadGroupTarget setSubtaskFileName(List<String> subTaskFileName) {
if (subTaskFileName == null || subTaskFileName.isEmpty()) return this;
mSubtaskFileName.addAll(subTaskFileName);
mSubTaskFileName.addAll(subTaskFileName);
if (mUrls.size() != subTaskFileName.size()) {
throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致");
}
@ -146,7 +143,7 @@ public class DownloadGroupTarget
List<DownloadEntity> entities = mEntity.getSubTask();
int i = 0;
for (DownloadEntity entity : entities) {
entity.setFileName(mSubtaskFileName.get(i));
entity.setFileName(mSubTaskFileName.get(i));
entity.update();
}
}
@ -161,8 +158,8 @@ public class DownloadGroupTarget
for (int i = 0, len = mUrls.size(); i < len; i++) {
DownloadEntity entity = new DownloadEntity();
entity.setDownloadUrl(mUrls.get(i));
String fileName = mSubtaskFileName.isEmpty() ? CommonUtil.keyToHashKey(mUrls.get(i))
: mSubtaskFileName.get(i);
String fileName = mSubTaskFileName.isEmpty() ? CommonUtil.keyToHashKey(mUrls.get(i))
: mSubTaskFileName.get(i);
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
entity.setGroupName(mGroupName);
entity.setGroupChild(true);

View File

@ -20,18 +20,22 @@ import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.scheduler.DownloadGroupSchedulers;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulerListener;
import com.arialyy.aria.core.upload.ProxyHelper;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Set;
import java.lang.reflect.Method;
/**
* Created by lyy on 2016/12/5.
* 下载功能接收器
*/
public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
public class DownloadReceiver extends AbsReceiver {
private final String TAG = "DownloadReceiver";
public ISchedulerListener<DownloadTask> listener;
@ -72,7 +76,15 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
* 将当前类注册到Aria
*/
public DownloadReceiver register() {
DownloadSchedulers.getInstance().register(obj);
String className = obj.getClass().getName();
Set<String> dCounter = ProxyHelper.getInstance().downloadCounter;
Set<String> dgCounter = ProxyHelper.getInstance().downloadGroupCounter;
if (dCounter.contains(className)) {
DownloadSchedulers.getInstance().register(obj);
}
if (dgCounter.contains(className)) {
DownloadGroupSchedulers.getInstance().register(obj);
}
return this;
}
@ -80,7 +92,15 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
* 取消注册
*/
@Override public void unRegister() {
DownloadSchedulers.getInstance().unRegister(obj);
String className = obj.getClass().getName();
Set<String> dCounter = ProxyHelper.getInstance().downloadCounter;
Set<String> dgCounter = ProxyHelper.getInstance().downloadGroupCounter;
if (dCounter.contains(className)) {
DownloadSchedulers.getInstance().unRegister(obj);
}
if (dgCounter.contains(className)) {
DownloadGroupSchedulers.getInstance().unRegister(obj);
}
}
/**

View File

@ -20,7 +20,7 @@ package com.arialyy.aria.core.inf;
* Created by AriaL on 2017/6/27.
*/
public abstract class AbsReceiver<ENTITY extends AbsNormalEntity> implements IReceiver<ENTITY>{
public abstract class AbsReceiver<ENTITY extends AbsEntity> implements IReceiver<ENTITY>{
public String targetName;
public Object obj;

View File

@ -33,28 +33,32 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* Created by lyy on 2017/6/4.
* 事件调度器,用于处理任务状态的调度
*/
abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>, QUEUE extends ITaskQueue<TASK, TASK_ENTITY, ENTITY>>
implements ISchedulers<TASK> {
private final String TAG = "AbsSchedulers";
/**
* 下载的动态生成的代理类后缀
*/
private String DOWNLOAD_PROXY_CLASS_SUFFIX = "$$DownloadListenerProxy";
/**
* 上传的动态生成的代理类后缀
*/
private String UPLOAD_PROXY_CLASS_SUFFIX = "$$UploadListenerProxy";
static final int DOWNLOAD = 0xa1;
static final int UPLOAD = 0xa2;
static final int DOWNLOAD_GROUP = 0xa3;
protected QUEUE mQueue;
protected boolean isDownload = true;
private Map<String, ISchedulerListener<TASK>> mSchedulerListeners = new ConcurrentHashMap<>();
private Map<String, AbsSchedulerListener<TASK>> mObservers = new ConcurrentHashMap<>();
/**
* 设置调度器类型
*/
abstract int getSchedulerType();
/**
* 设置代理类后缀名
*/
abstract String getProxySuffix();
/**
* @param targetName 观察者,创建该监听器的对象类名
* @param schedulerListener {@link ISchedulerListener}
@ -109,8 +113,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
private AbsSchedulerListener<TASK> createListener(String targetName) {
AbsSchedulerListener<TASK> listener = null;
try {
Class clazz = Class.forName(
targetName + (isDownload ? DOWNLOAD_PROXY_CLASS_SUFFIX : UPLOAD_PROXY_CLASS_SUFFIX));
Class clazz = Class.forName(targetName + getProxySuffix());
listener = (AbsSchedulerListener<TASK>) clazz.newInstance();
} catch (ClassNotFoundException e) {
Log.e(TAG, targetName + "没有Aria的Download或Upload注解方法");

View File

@ -31,7 +31,6 @@ public class DownloadGroupSchedulers extends
private DownloadGroupSchedulers() {
mQueue = DownloadGroupTaskQueue.getInstance();
isDownload = true;
}
public static DownloadGroupSchedulers getInstance() {
@ -42,4 +41,12 @@ public class DownloadGroupSchedulers extends
}
return INSTANCE;
}
@Override int getSchedulerType() {
return DOWNLOAD_GROUP;
}
@Override String getProxySuffix() {
return "$$DownloadGroupListenerProxy";
}
}

View File

@ -34,7 +34,6 @@ public class DownloadSchedulers
private DownloadSchedulers() {
mQueue = DownloadTaskQueue.getInstance();
isDownload = true;
}
public static DownloadSchedulers getInstance() {
@ -45,4 +44,12 @@ public class DownloadSchedulers
}
return INSTANCE;
}
@Override int getSchedulerType() {
return DOWNLOAD;
}
@Override String getProxySuffix() {
return "$$DownloadListenerProxy";
}
}

View File

@ -17,54 +17,54 @@
package com.arialyy.aria.core.scheduler;
import android.os.Handler;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.AbsTask;
/**
* Created by lyy on 2016/11/2.
* 调度器功能接口
*/
public interface ISchedulers<Task extends ITask> extends Handler.Callback {
public interface ISchedulers<Task extends AbsTask> extends Handler.Callback {
/**
* 断点支持
*/
public static final int SUPPORT_BREAK_POINT = 9;
int SUPPORT_BREAK_POINT = 9;
/**
* 任务预加载
*/
public static final int PRE = 0;
int PRE = 0;
/**
* 任务预加载完成
*/
public static final int POST_PRE = 1;
int POST_PRE = 1;
/**
* 任务开始
*/
public static final int START = 2;
int START = 2;
/**
* 任务停止
*/
public static final int STOP = 3;
int STOP = 3;
/**
* 任务失败
*/
public static final int FAIL = 4;
int FAIL = 4;
/**
* 任务取消
*/
public static final int CANCEL = 5;
int CANCEL = 5;
/**
* 任务完成
*/
public static final int COMPLETE = 6;
int COMPLETE = 6;
/**
* 任务处理中
*/
public static final int RUNNING = 7;
int RUNNING = 7;
/**
* 恢复任务
*/
public static final int RESUME = 8;
int RESUME = 8;
/**
* 注册下载器监听,一个观察者只能注册一次监听
@ -72,26 +72,25 @@ public interface ISchedulers<Task extends ITask> extends Handler.Callback {
* @param targetName 观察者,创建该监听器的对象类名
* @param schedulerListener {@link ISchedulerListener}
*/
public void addSchedulerListener(String targetName, ISchedulerListener<Task> schedulerListener);
void addSchedulerListener(String targetName, ISchedulerListener<Task> schedulerListener);
/**
* @param targetName 观察者,创建该监听器的对象类名
* 取消注册监听器
*/
public void removeSchedulerListener(String targetName,
ISchedulerListener<Task> schedulerListener);
void removeSchedulerListener(String targetName, ISchedulerListener<Task> schedulerListener);
/**
* 将当前类注册到Aria
*
* @param obj 观察者类
*/
public void register(Object obj);
void register(Object obj);
/**
* 移除注册
*
* @param obj 观察者类
*/
public void unRegister(Object obj);
void unRegister(Object obj);
}

View File

@ -32,7 +32,6 @@ public class UploadSchedulers
private UploadSchedulers() {
mQueue = UploadTaskQueue.getInstance();
isDownload = false;
}
public static UploadSchedulers getInstance() {
@ -44,4 +43,12 @@ public class UploadSchedulers
return INSTANCE;
}
@Override int getSchedulerType() {
return UPLOAD;
}
@Override String getProxySuffix() {
return "$$UploadListenerProxy";
}
}

View File

@ -0,0 +1,69 @@
/*
* 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.upload;
import com.arialyy.aria.core.AriaManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;
import static java.util.Collections.unmodifiableSet;
/**
* Created by Aria.Lao on 2017/7/10.
* 代理参数获取
*/
public class ProxyHelper {
public Set<String> downloadCounter, uploadCounter, downloadGroupCounter;
public static volatile ProxyHelper INSTANCE = null;
private ProxyHelper() {
init();
}
public static ProxyHelper getInstance() {
if (INSTANCE == null) {
synchronized (AriaManager.LOCK) {
INSTANCE = new ProxyHelper();
}
}
return INSTANCE;
}
private void init() {
try {
Class clazz = Class.forName("com.arialyy.aria.ProxyClassCounter");
Method download = clazz.getMethod("getDownloadCounter");
Method downloadGroup = clazz.getMethod("getDownloadGroupCounter");
Method upload = clazz.getMethod("getUploadCounter");
Object object = clazz.newInstance();
downloadCounter = unmodifiableSet((Set<String>) download.invoke(object));
downloadGroupCounter = unmodifiableSet((Set<String>) downloadGroup.invoke(object));
uploadCounter = unmodifiableSet((Set<String>) upload.invoke(object));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

View File

@ -83,8 +83,7 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
final AriaManager am = AriaManager.getInstance(AriaManager.APP);
am.setCmd(CommonUtil.createCmd(targetName, new DownloadTaskEntity(),
NormalCmdFactory.TASK_CANCEL_ALL))
.exe();
NormalCmdFactory.TASK_CANCEL_ALL)).exe();
Set<String> keys = am.getReceiver().keySet();
for (String key : keys) {
@ -110,7 +109,10 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
return this;
}
@Override public void removeSchedulerListener() {
/**
* @see #unRegister()
*/
@Deprecated @Override public void removeSchedulerListener() {
if (listener != null) {
UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
}
@ -120,11 +122,19 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
* 将当前类注册到Aria
*/
public UploadReceiver register() {
UploadSchedulers.getInstance().register(obj);
String className = obj.getClass().getName();
Set<String> dCounter = ProxyHelper.getInstance().uploadCounter;
if (dCounter.contains(className)) {
UploadSchedulers.getInstance().register(obj);
}
return this;
}
@Override public void unRegister() {
UploadSchedulers.getInstance().unRegister(obj);
String className = obj.getClass().getName();
Set<String> dCounter = ProxyHelper.getInstance().uploadCounter;
if (dCounter.contains(className)) {
UploadSchedulers.getInstance().unRegister(obj);
}
}
}