增加任务组的注解事件
This commit is contained in:
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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注解方法");
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/6/6.
|
||||
* Aria下载事件被注解的方法中,参数仅能有一个,参数类型为{@link com.arialyy.aria.core.download.DownloadGroupTask}
|
||||
* <pre>
|
||||
* <code>
|
||||
* {@literal @}Download.onPre(groupName)
|
||||
* protected void onPre(DownloadGroupTask task) {
|
||||
* }
|
||||
* </code>
|
||||
* </pre>
|
||||
* {@literal @}Download.onPre("myGroupName"),如果你的注解中增加了url描述,
|
||||
* 则表示,所有下载任务中,只有下载地址为"myGroupName"的任务才能回调该注解的方法。
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface DownloadGroup {
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onPre}注解,在预处理完成时,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onTaskPre}注解,在任务预处理完成时,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onTaskResume}注解,在任务恢复下载时,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onTaskStart}注解,在任务开始下载时,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onTaskStop}注解,在任务停止时,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onTaskCancel}l注解,在任务取消时,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onTaskFail)注解,在任务预失败时,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onTaskComplete}注解,在任务完成时,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果你在方法中添加{@code @Download.onTaskRunning}注解,在任务正在下载,Aria会调用该方法
|
||||
*/
|
||||
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning {
|
||||
String[] value() default { AriaConstance.NO_URL };
|
||||
}
|
||||
}
|
@ -42,6 +42,7 @@ import javax.lang.model.element.TypeElement;
|
||||
|
||||
@Override public Set<String> getSupportedAnnotationTypes() {
|
||||
Set<String> annotataions = new LinkedHashSet<>();
|
||||
//单任务下载的注解
|
||||
annotataions.add(Download.onPre.class.getCanonicalName());
|
||||
annotataions.add(Download.onNoSupportBreakPoint.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskCancel.class.getCanonicalName());
|
||||
@ -52,6 +53,18 @@ import javax.lang.model.element.TypeElement;
|
||||
annotataions.add(Download.onTaskRunning.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskStart.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskStop.class.getCanonicalName());
|
||||
//下载任务的注解
|
||||
annotataions.add(Download.onPre.class.getCanonicalName());
|
||||
annotataions.add(Download.onNoSupportBreakPoint.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskCancel.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskComplete.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskFail.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskPre.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskResume.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskRunning.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskStart.class.getCanonicalName());
|
||||
annotataions.add(Download.onTaskStop.class.getCanonicalName());
|
||||
//上传任务的注解
|
||||
annotataions.add(Upload.onPre.class.getCanonicalName());
|
||||
annotataions.add(Upload.onNoSupportBreakPoint.class.getCanonicalName());
|
||||
annotataions.add(Upload.onTaskCancel.class.getCanonicalName());
|
||||
@ -73,6 +86,7 @@ import javax.lang.model.element.TypeElement;
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
mHandler.clean();
|
||||
mHandler.handleDownload(roundEnv);
|
||||
mHandler.handleDownloadGroup(roundEnv);
|
||||
mHandler.handleUpload(roundEnv);
|
||||
mHandler.createProxyFile();
|
||||
return true;
|
||||
|
@ -16,6 +16,7 @@
|
||||
package com.arialyy.compiler;
|
||||
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.annotations.DownloadGroup;
|
||||
import com.arialyy.annotations.Upload;
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.CodeBlock;
|
||||
@ -52,7 +53,8 @@ class ElementHandle {
|
||||
|
||||
private Filer mFiler;
|
||||
private Elements mElementUtil;
|
||||
private Map<String, ProxyEntity> mMethods = new HashMap<>();
|
||||
private Map<String, ProxyMethodParam> mMethods = new HashMap<>();
|
||||
private Map<String, Set<String>> mListenerClass = new HashMap<>();
|
||||
|
||||
ElementHandle(Filer filer, Elements elements) {
|
||||
mFiler = filer;
|
||||
@ -66,32 +68,62 @@ class ElementHandle {
|
||||
* PackageElement 一般代表Package
|
||||
*/
|
||||
void handleDownload(RoundEnvironment roundEnv) {
|
||||
saveMethod(true, roundEnv, Download.onNoSupportBreakPoint.class,
|
||||
ProxyConstance.DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT);
|
||||
saveMethod(true, roundEnv, Download.onPre.class, ProxyConstance.DOWNLOAD_PRE);
|
||||
saveMethod(true, roundEnv, Download.onTaskCancel.class, ProxyConstance.DOWNLOAD_TASK_CANCEL);
|
||||
saveMethod(true, roundEnv, Download.onTaskComplete.class,
|
||||
ProxyConstance.DOWNLOAD_TASK_COMPLETE);
|
||||
saveMethod(true, roundEnv, Download.onTaskFail.class, ProxyConstance.DOWNLOAD_TASK_FAIL);
|
||||
saveMethod(true, roundEnv, Download.onTaskPre.class, ProxyConstance.DOWNLOAD_TASK_PRE);
|
||||
saveMethod(true, roundEnv, Download.onTaskResume.class, ProxyConstance.DOWNLOAD_TASK_RESUME);
|
||||
saveMethod(true, roundEnv, Download.onTaskRunning.class, ProxyConstance.DOWNLOAD_TASK_RUNNING);
|
||||
saveMethod(true, roundEnv, Download.onTaskStart.class, ProxyConstance.DOWNLOAD_TASK_START);
|
||||
saveMethod(true, roundEnv, Download.onTaskStop.class, ProxyConstance.DOWNLOAD_TASK_STOP);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onNoSupportBreakPoint.class,
|
||||
ProxyConstance.TASK_NO_SUPPORT_BREAKPOINT);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onPre.class, ProxyConstance.PRE);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskCancel.class,
|
||||
ProxyConstance.TASK_CANCEL);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskComplete.class,
|
||||
ProxyConstance.TASK_COMPLETE);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskFail.class, ProxyConstance.TASK_FAIL);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskPre.class, ProxyConstance.TASK_PRE);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskResume.class,
|
||||
ProxyConstance.TASK_RESUME);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskRunning.class,
|
||||
ProxyConstance.TASK_RUNNING);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskStart.class, ProxyConstance.TASK_START);
|
||||
saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskStop.class, ProxyConstance.TASK_STOP);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理搜索到的下载任务组注解
|
||||
*/
|
||||
void handleDownloadGroup(RoundEnvironment roundEnv) {
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onPre.class, ProxyConstance.PRE);
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onTaskCancel.class,
|
||||
ProxyConstance.TASK_CANCEL);
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onTaskComplete.class,
|
||||
ProxyConstance.TASK_COMPLETE);
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onTaskFail.class,
|
||||
ProxyConstance.TASK_FAIL);
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onTaskPre.class,
|
||||
ProxyConstance.TASK_PRE);
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onTaskResume.class,
|
||||
ProxyConstance.TASK_RESUME);
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onTaskRunning.class,
|
||||
ProxyConstance.TASK_RUNNING);
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onTaskStart.class,
|
||||
ProxyConstance.TASK_START);
|
||||
saveMethod(TaskEnum.DOWNLOAD_GROUP, roundEnv, DownloadGroup.onTaskStop.class,
|
||||
ProxyConstance.TASK_STOP);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理搜索到的上传注解F
|
||||
*/
|
||||
void handleUpload(RoundEnvironment roundEnv) {
|
||||
saveMethod(false, roundEnv, Upload.onNoSupportBreakPoint.class,
|
||||
ProxyConstance.UPLOAD_TASK_NO_SUPPORT_BREAKPOINT);
|
||||
saveMethod(false, roundEnv, Upload.onPre.class, ProxyConstance.UPLOAD_PRE);
|
||||
saveMethod(false, roundEnv, Upload.onTaskCancel.class, ProxyConstance.UPLOAD_TASK_CANCEL);
|
||||
saveMethod(false, roundEnv, Upload.onTaskComplete.class, ProxyConstance.UPLOAD_TASK_COMPLETE);
|
||||
saveMethod(false, roundEnv, Upload.onTaskFail.class, ProxyConstance.UPLOAD_TASK_FAIL);
|
||||
saveMethod(false, roundEnv, Upload.onTaskPre.class, ProxyConstance.UPLOAD_TASK_PRE);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onNoSupportBreakPoint.class,
|
||||
ProxyConstance.TASK_NO_SUPPORT_BREAKPOINT);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onPre.class, ProxyConstance.PRE);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskCancel.class, ProxyConstance.TASK_CANCEL);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskComplete.class,
|
||||
ProxyConstance.TASK_COMPLETE);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskFail.class, ProxyConstance.TASK_FAIL);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskPre.class, ProxyConstance.TASK_PRE);
|
||||
//saveMethod(false, roundEnv, Upload.onTaskResume.class);
|
||||
saveMethod(false, roundEnv, Upload.onTaskRunning.class, ProxyConstance.UPLOAD_TASK_RUNNING);
|
||||
saveMethod(false, roundEnv, Upload.onTaskStart.class, ProxyConstance.UPLOAD_TASK_START);
|
||||
saveMethod(false, roundEnv, Upload.onTaskStop.class, ProxyConstance.UPLOAD_TASK_STOP);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskRunning.class, ProxyConstance.TASK_RUNNING);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskStart.class, ProxyConstance.TASK_START);
|
||||
saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskStop.class, ProxyConstance.TASK_STOP);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,51 +155,125 @@ class ElementHandle {
|
||||
* </pre>
|
||||
*/
|
||||
void createProxyFile() {
|
||||
Set<String> keys = mMethods.keySet();
|
||||
try {
|
||||
for (String key : keys) {
|
||||
ProxyEntity entity = mMethods.get(key);
|
||||
JavaFile jf = JavaFile.builder(entity.packageName, createProxyClass(entity)).build();
|
||||
|
||||
jf.writeTo(mFiler);
|
||||
// 如果需要在控制台打印生成的文件,则去掉下面的注释
|
||||
//jf.writeTo(System.out);
|
||||
}
|
||||
createProxyListenerFile();
|
||||
createProxyClassFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建事件代理文件
|
||||
*/
|
||||
private void createProxyListenerFile() throws IOException {
|
||||
Set<String> keys = mMethods.keySet();
|
||||
for (String key : keys) {
|
||||
ProxyMethodParam entity = mMethods.get(key);
|
||||
JavaFile jf = JavaFile.builder(entity.packageName, createProxyClass(entity)).build();
|
||||
|
||||
jf.writeTo(mFiler);
|
||||
// 如果需要在控制台打印生成的文件,则去掉下面的注释
|
||||
//jf.writeTo(System.out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 每一种注解的类集合
|
||||
*/
|
||||
private void createProxyClassFile() throws IOException {
|
||||
Set<String> keys = mListenerClass.keySet();
|
||||
TypeSpec.Builder builder = TypeSpec.classBuilder(ProxyConstance.PROXY_COUNTER_NAME)
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
|
||||
|
||||
FieldSpec mappingField = FieldSpec.builder(
|
||||
ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class),
|
||||
ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.get(String.class))),
|
||||
ProxyConstance.PROXY_COUNTER_MAP)
|
||||
.addModifiers(Modifier.PRIVATE)
|
||||
.initializer("new $T()", HashMap.class)
|
||||
.build();
|
||||
builder.addField(mappingField);
|
||||
|
||||
//增加构造函数
|
||||
CodeBlock.Builder cb = CodeBlock.builder();
|
||||
cb.add("Set<String> set = null;\n");
|
||||
for (String key : keys) {
|
||||
addTypeData(key, mListenerClass.get(key), cb);
|
||||
}
|
||||
MethodSpec structure =
|
||||
MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addCode(cb.build()).build();
|
||||
builder.addMethod(structure);
|
||||
|
||||
builder.addMethod(
|
||||
creatMethod(ProxyConstance.COUNT_METHOD_DOWNLOAD, ProxyConstance.COUNT_DOWNLOAD));
|
||||
builder.addMethod(creatMethod(ProxyConstance.COUNT_METHOD_UPLOAD, ProxyConstance.COUNT_UPLOAD));
|
||||
builder.addMethod(creatMethod(ProxyConstance.COUNT_METHOD_DOWNLOAD_GROUP,
|
||||
ProxyConstance.COUNT_DOWNLOAD_GROUP));
|
||||
|
||||
JavaFile jf = JavaFile.builder(ProxyConstance.PROXY_COUNTER_PACKAGE, builder.build()).build();
|
||||
jf.writeTo(mFiler);
|
||||
//jf.writeTo(System.out);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建不同任务类型的代理类集合
|
||||
*
|
||||
* @param key {@link #addListenerMapping(String, String)}
|
||||
*/
|
||||
private MethodSpec creatMethod(String methodName, String key) {
|
||||
MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
|
||||
ParameterizedTypeName returnName =
|
||||
ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.get(String.class));
|
||||
builder.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
|
||||
.returns(returnName)
|
||||
.addCode("return " + ProxyConstance.PROXY_COUNTER_MAP + ".get(\"" + key + "\");\n");
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加每一种注解对应类
|
||||
*
|
||||
* @param type {@link #addListenerMapping(String, String)}
|
||||
*/
|
||||
private void addTypeData(String type, Set<String> clsNames, CodeBlock.Builder cb) {
|
||||
if (clsNames == null || clsNames.isEmpty()) return;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("set = new $T();\n");
|
||||
for (String clsName : clsNames) {
|
||||
sb.append("set.add(\"").append(clsName).append("\");\n");
|
||||
}
|
||||
sb.append("typeMapping.put(\"").append(type).append("\", ").append("set);\n");
|
||||
cb.add(sb.toString(), ClassName.get(HashSet.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建代理方法
|
||||
*
|
||||
* @param isDownload 是否是下载的注解
|
||||
* @param taskEnum 任务类型枚举{@link TaskEnum}
|
||||
* @param annotation {@link Download}、{@link Upload}
|
||||
* @param methodName 被代理类注解的方法名
|
||||
*/
|
||||
private MethodSpec createProxyMethod(boolean isDownload, Class<? extends Annotation> annotation,
|
||||
private MethodSpec createProxyMethod(TaskEnum taskEnum, Class<? extends Annotation> annotation,
|
||||
String methodName) {
|
||||
ClassName task = ClassName.get(
|
||||
isDownload ? "com.arialyy.aria.core.download" : "com.arialyy.aria.core.upload",
|
||||
isDownload ? "DownloadTask" : "UploadTask");
|
||||
ClassName task = ClassName.get(taskEnum.getPkg(), taskEnum.getClassName());
|
||||
|
||||
ParameterSpec parameterSpec =
|
||||
ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Set<String> keys = keyMapping.get(\"")
|
||||
.append(methodName)
|
||||
.append("\");\n");
|
||||
sb.append("Set<String> keys = keyMapping.get(\"").append(methodName).append("\");\n");
|
||||
sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n")
|
||||
.append("\t\tobj.")
|
||||
.append(methodName)
|
||||
.append("(")
|
||||
.append(isDownload ? "(DownloadTask)" : "(UploadTask)")
|
||||
.append("task);\n")
|
||||
.append("((")
|
||||
.append(taskEnum.getClassName())
|
||||
.append(")task);\n")
|
||||
.append("\t}\n} else {\n")
|
||||
.append("\tobj.")
|
||||
.append(methodName)
|
||||
.append("(")
|
||||
.append(isDownload ? "(DownloadTask)" : "(UploadTask)")
|
||||
.append("task);\n}\n");
|
||||
.append("((")
|
||||
.append(taskEnum.getClassName())
|
||||
.append(")task);\n}\n");
|
||||
|
||||
return MethodSpec.methodBuilder(annotation.getSimpleName())
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
@ -181,10 +287,9 @@ class ElementHandle {
|
||||
/**
|
||||
* 创建代理类
|
||||
*/
|
||||
private TypeSpec createProxyClass(ProxyEntity entity) {
|
||||
TypeSpec.Builder builder = TypeSpec.classBuilder(
|
||||
entity.className + (entity.isDownlaod ? ProxyConstance.DOWNLOAD_PROXY_CLASS_SUFFIX
|
||||
: ProxyConstance.UPLOAD_PROXY_CLASS_SUFFIX))
|
||||
private TypeSpec createProxyClass(ProxyMethodParam entity) {
|
||||
TaskEnum taskEnum = entity.taskEnum;
|
||||
TypeSpec.Builder builder = TypeSpec.classBuilder(entity.className + taskEnum.getProxySuffix())
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
|
||||
|
||||
//添加被代理的类的字段
|
||||
@ -201,8 +306,7 @@ class ElementHandle {
|
||||
|
||||
//添加注解方法
|
||||
for (Class<? extends Annotation> annotation : entity.methods.keySet()) {
|
||||
MethodSpec method =
|
||||
createProxyMethod(entity.isDownlaod, annotation, entity.methods.get(annotation));
|
||||
MethodSpec method = createProxyMethod(taskEnum, annotation, entity.methods.get(annotation));
|
||||
builder.addMethod(method);
|
||||
}
|
||||
|
||||
@ -241,9 +345,8 @@ class ElementHandle {
|
||||
//创建父类参数
|
||||
ClassName superClass = ClassName.get("com.arialyy.aria.core.scheduler", "AbsSchedulerListener");
|
||||
//创建泛型
|
||||
ClassName typeVariableName = ClassName.get(
|
||||
entity.isDownlaod ? "com.arialyy.aria.core.download" : "com.arialyy.aria.core.upload",
|
||||
entity.isDownlaod ? "DownloadTask" : "UploadTask");
|
||||
ClassName typeVariableName =
|
||||
ClassName.get(entity.taskEnum.getPkg(), entity.taskEnum.getClassName());
|
||||
builder.superclass(ParameterizedTypeName.get(superClass, typeVariableName));
|
||||
builder.addMethod(listener);
|
||||
return builder.build();
|
||||
@ -256,7 +359,7 @@ class ElementHandle {
|
||||
/**
|
||||
* 查找并保存扫描到的方法
|
||||
*/
|
||||
private void saveMethod(boolean isDownload, RoundEnvironment roundEnv,
|
||||
private void saveMethod(TaskEnum taskEnum, RoundEnvironment roundEnv,
|
||||
Class<? extends Annotation> annotationClazz, int annotationType) {
|
||||
for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) {
|
||||
ElementKind kind = element.getKind();
|
||||
@ -264,19 +367,19 @@ class ElementHandle {
|
||||
ExecutableElement method = (ExecutableElement) element;
|
||||
TypeElement classElement = (TypeElement) method.getEnclosingElement();
|
||||
PackageElement packageElement = mElementUtil.getPackageOf(classElement);
|
||||
checkDownloadMethod(isDownload, method);
|
||||
checkDownloadMethod(taskEnum, method);
|
||||
String methodName = method.getSimpleName().toString();
|
||||
String className = method.getEnclosingElement().toString(); //全类名
|
||||
ProxyEntity proxyEntity = mMethods.get(className);
|
||||
ProxyMethodParam proxyEntity = mMethods.get(className);
|
||||
if (proxyEntity == null) {
|
||||
proxyEntity = new ProxyEntity();
|
||||
proxyEntity.isDownlaod = isDownload;
|
||||
proxyEntity = new ProxyMethodParam();
|
||||
proxyEntity.taskEnum = taskEnum;
|
||||
proxyEntity.packageName = packageElement.getQualifiedName().toString();
|
||||
proxyEntity.className = classElement.getSimpleName().toString();
|
||||
mMethods.put(className, proxyEntity);
|
||||
}
|
||||
proxyEntity.methods.put(annotationClazz, methodName);
|
||||
proxyEntity.keyMappings.put(methodName, getValues(method, isDownload, annotationType));
|
||||
proxyEntity.keyMappings.put(methodName, getValues(taskEnum, method, annotationType));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -284,83 +387,163 @@ class ElementHandle {
|
||||
/**
|
||||
* 获取注解的内容
|
||||
*/
|
||||
private Set<String> getValues(ExecutableElement method, boolean isDownload, int annotationType) {
|
||||
private Set<String> getValues(TaskEnum taskEnum, ExecutableElement method, int annotationType) {
|
||||
String clsName = method.getEnclosingElement().toString();
|
||||
String[] keys = null;
|
||||
if (isDownload) {
|
||||
switch (annotationType) {
|
||||
case ProxyConstance.DOWNLOAD_PRE:
|
||||
keys = method.getAnnotation(Download.onPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_PRE:
|
||||
keys = method.getAnnotation(Download.onTaskPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_RESUME:
|
||||
keys = method.getAnnotation(Download.onTaskResume.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_START:
|
||||
keys = method.getAnnotation(Download.onTaskStart.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_RUNNING:
|
||||
keys = method.getAnnotation(Download.onTaskRunning.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_STOP:
|
||||
keys = method.getAnnotation(Download.onTaskStop.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_COMPLETE:
|
||||
keys = method.getAnnotation(Download.onTaskComplete.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_CANCEL:
|
||||
keys = method.getAnnotation(Download.onTaskCancel.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_FAIL:
|
||||
keys = method.getAnnotation(Download.onTaskFail.class).value();
|
||||
break;
|
||||
case ProxyConstance.DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT:
|
||||
keys = method.getAnnotation(Download.onNoSupportBreakPoint.class).value();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (annotationType) {
|
||||
case ProxyConstance.UPLOAD_PRE:
|
||||
keys = method.getAnnotation(Upload.onPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_PRE:
|
||||
keys = method.getAnnotation(Upload.onTaskPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_RESUME:
|
||||
//keys = method.getAnnotation(Upload.onTaskResume.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_START:
|
||||
keys = method.getAnnotation(Upload.onTaskStart.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_RUNNING:
|
||||
keys = method.getAnnotation(Upload.onTaskRunning.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_STOP:
|
||||
keys = method.getAnnotation(Upload.onTaskStop.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_COMPLETE:
|
||||
keys = method.getAnnotation(Upload.onTaskComplete.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_CANCEL:
|
||||
keys = method.getAnnotation(Upload.onTaskCancel.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_FAIL:
|
||||
keys = method.getAnnotation(Upload.onTaskFail.class).value();
|
||||
break;
|
||||
case ProxyConstance.UPLOAD_TASK_NO_SUPPORT_BREAKPOINT:
|
||||
keys = method.getAnnotation(Upload.onNoSupportBreakPoint.class).value();
|
||||
break;
|
||||
}
|
||||
switch (taskEnum) {
|
||||
case DOWNLOAD:
|
||||
keys = getDownloadValues(method, annotationType);
|
||||
addListenerMapping(clsName, ProxyConstance.COUNT_DOWNLOAD);
|
||||
break;
|
||||
case UPLOAD:
|
||||
keys = getUploadValues(method, annotationType);
|
||||
addListenerMapping(clsName, ProxyConstance.COUNT_UPLOAD);
|
||||
break;
|
||||
case DOWNLOAD_GROUP:
|
||||
keys = getDownloadGroupValues(method, annotationType);
|
||||
addListenerMapping(clsName, ProxyConstance.COUNT_DOWNLOAD_GROUP);
|
||||
break;
|
||||
}
|
||||
|
||||
return keys == null ? null : convertSet(keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加方法映射
|
||||
*
|
||||
* @param clsName 注解事件的类
|
||||
* @param key {@link ProxyConstance#COUNT_DOWNLOAD}、{@link ProxyConstance#COUNT_UPLOAD}、{@link
|
||||
* ProxyConstance#COUNT_DOWNLOAD_GROUP}
|
||||
*/
|
||||
private void addListenerMapping(String clsName, String key) {
|
||||
Set<String> cls = mListenerClass.get(key);
|
||||
if (cls == null) {
|
||||
cls = new HashSet<>();
|
||||
mListenerClass.put(key, cls);
|
||||
}
|
||||
cls.add(clsName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载任务组的注解数据
|
||||
*/
|
||||
private String[] getDownloadGroupValues(ExecutableElement method, int annotationType) {
|
||||
String[] values = null;
|
||||
switch (annotationType) {
|
||||
case ProxyConstance.PRE:
|
||||
values = method.getAnnotation(DownloadGroup.onPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_PRE:
|
||||
values = method.getAnnotation(DownloadGroup.onTaskPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_RESUME:
|
||||
values = method.getAnnotation(DownloadGroup.onTaskResume.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_START:
|
||||
values = method.getAnnotation(DownloadGroup.onTaskStart.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_RUNNING:
|
||||
values = method.getAnnotation(DownloadGroup.onTaskRunning.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_STOP:
|
||||
values = method.getAnnotation(DownloadGroup.onTaskStop.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_COMPLETE:
|
||||
values = method.getAnnotation(DownloadGroup.onTaskComplete.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_CANCEL:
|
||||
values = method.getAnnotation(DownloadGroup.onTaskCancel.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_FAIL:
|
||||
values = method.getAnnotation(DownloadGroup.onTaskFail.class).value();
|
||||
break;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传的注解数据
|
||||
*/
|
||||
private String[] getUploadValues(ExecutableElement method, int annotationType) {
|
||||
String[] values = null;
|
||||
switch (annotationType) {
|
||||
case ProxyConstance.PRE:
|
||||
values = method.getAnnotation(Upload.onPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_PRE:
|
||||
values = method.getAnnotation(Upload.onTaskPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_RESUME:
|
||||
//values = method.getAnnotation(Upload.onTaskResume.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_START:
|
||||
values = method.getAnnotation(Upload.onTaskStart.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_RUNNING:
|
||||
values = method.getAnnotation(Upload.onTaskRunning.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_STOP:
|
||||
values = method.getAnnotation(Upload.onTaskStop.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_COMPLETE:
|
||||
values = method.getAnnotation(Upload.onTaskComplete.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_CANCEL:
|
||||
values = method.getAnnotation(Upload.onTaskCancel.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_FAIL:
|
||||
values = method.getAnnotation(Upload.onTaskFail.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_NO_SUPPORT_BREAKPOINT:
|
||||
//values = method.getAnnotation(Upload.onNoSupportBreakPoint.class).value();
|
||||
break;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载的注解数据
|
||||
*/
|
||||
private String[] getDownloadValues(ExecutableElement method, int annotationType) {
|
||||
String[] values = null;
|
||||
switch (annotationType) {
|
||||
case ProxyConstance.PRE:
|
||||
values = method.getAnnotation(Download.onPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_PRE:
|
||||
values = method.getAnnotation(Download.onTaskPre.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_RESUME:
|
||||
values = method.getAnnotation(Download.onTaskResume.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_START:
|
||||
values = method.getAnnotation(Download.onTaskStart.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_RUNNING:
|
||||
values = method.getAnnotation(Download.onTaskRunning.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_STOP:
|
||||
values = method.getAnnotation(Download.onTaskStop.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_COMPLETE:
|
||||
values = method.getAnnotation(Download.onTaskComplete.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_CANCEL:
|
||||
values = method.getAnnotation(Download.onTaskCancel.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_FAIL:
|
||||
values = method.getAnnotation(Download.onTaskFail.class).value();
|
||||
break;
|
||||
case ProxyConstance.TASK_NO_SUPPORT_BREAKPOINT:
|
||||
values = method.getAnnotation(Download.onNoSupportBreakPoint.class).value();
|
||||
break;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查和下载相关的方法,如果被注解的方法为private或参数不合法,则抛异常
|
||||
*/
|
||||
private void checkDownloadMethod(boolean isDownload, ExecutableElement method) {
|
||||
private void checkDownloadMethod(TaskEnum taskEnum, ExecutableElement method) {
|
||||
String methodName = method.getSimpleName().toString();
|
||||
String className = method.getEnclosingElement().toString();
|
||||
Set<Modifier> modifiers = method.getModifiers();
|
||||
@ -370,16 +553,16 @@ class ElementHandle {
|
||||
List<VariableElement> params = (List<VariableElement>) method.getParameters();
|
||||
if (params.size() > 1) {
|
||||
throw new IllegalArgumentException(
|
||||
className + "." + methodName + "参数错误, 参数只有一个,且参数必须是" + getCheckParams(isDownload));
|
||||
className + "." + methodName + "参数错误, 参数只有一个,且参数必须是" + getCheckParams(taskEnum));
|
||||
}
|
||||
if (!params.get(0).asType().toString().equals(getCheckParams(isDownload))) {
|
||||
if (!params.get(0).asType().toString().equals(getCheckParams(taskEnum))) {
|
||||
throw new IllegalArgumentException(className
|
||||
+ "."
|
||||
+ methodName
|
||||
+ "参数【"
|
||||
+ params.get(0).getSimpleName()
|
||||
+ "】类型错误,参数必须是"
|
||||
+ getCheckParams(isDownload));
|
||||
+ getCheckParams(taskEnum));
|
||||
}
|
||||
}
|
||||
|
||||
@ -398,8 +581,7 @@ class ElementHandle {
|
||||
return set;
|
||||
}
|
||||
|
||||
private String getCheckParams(boolean isDownload) {
|
||||
return isDownload ? "com.arialyy.aria.core.download.DownloadTask"
|
||||
: "com.arialyy.aria.core.upload.UploadTask";
|
||||
private String getCheckParams(TaskEnum taskEnum) {
|
||||
return taskEnum.pkg + "." + taskEnum.getClassName();
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,6 @@ import javax.tools.Diagnostic;
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/6/6.
|
||||
*/
|
||||
|
||||
class PrintLog {
|
||||
|
||||
private volatile static PrintLog INSTANCE = null;
|
||||
|
@ -17,43 +17,44 @@ package com.arialyy.compiler;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/6/7.
|
||||
* 扫描器常量
|
||||
*/
|
||||
|
||||
public interface ProxyConstance {
|
||||
interface ProxyConstance {
|
||||
/**
|
||||
* 设置观察者的方法
|
||||
*/
|
||||
String SET_LISTENER = "setListener";
|
||||
|
||||
/**
|
||||
* 下载的动态生成的代理类后缀
|
||||
* 代理配置类
|
||||
*/
|
||||
String DOWNLOAD_PROXY_CLASS_SUFFIX = "$$DownloadListenerProxy";
|
||||
String PROXY_COUNTER_PACKAGE = "com.arialyy.aria";
|
||||
/**
|
||||
* 代理分类统计
|
||||
*/
|
||||
String PROXY_COUNTER_NAME = "ProxyClassCounter";
|
||||
|
||||
/**
|
||||
* 上传的动态生成的代理类后缀
|
||||
* 代理分类统计映射表
|
||||
*/
|
||||
String UPLOAD_PROXY_CLASS_SUFFIX = "$$UploadListenerProxy";
|
||||
String PROXY_COUNTER_MAP = "typeMapping";
|
||||
|
||||
int DOWNLOAD_PRE = 0X11;
|
||||
int DOWNLOAD_TASK_PRE = 0X12;
|
||||
int DOWNLOAD_TASK_RESUME = 0X13;
|
||||
int DOWNLOAD_TASK_START = 0X14;
|
||||
int DOWNLOAD_TASK_STOP = 0X15;
|
||||
int DOWNLOAD_TASK_CANCEL = 0X16;
|
||||
int DOWNLOAD_TASK_FAIL = 0X17;
|
||||
int DOWNLOAD_TASK_COMPLETE = 0X18;
|
||||
int DOWNLOAD_TASK_RUNNING = 0X19;
|
||||
int DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT = 0X1A;
|
||||
String COUNT_DOWNLOAD = "download";
|
||||
String COUNT_DOWNLOAD_GROUP = "downloadGroup";
|
||||
String COUNT_UPLOAD = "upload";
|
||||
|
||||
int UPLOAD_PRE = 0X11;
|
||||
int UPLOAD_TASK_PRE = 0X12;
|
||||
int UPLOAD_TASK_RESUME = 0X13;
|
||||
int UPLOAD_TASK_START = 0X14;
|
||||
int UPLOAD_TASK_STOP = 0X15;
|
||||
int UPLOAD_TASK_CANCEL = 0X16;
|
||||
int UPLOAD_TASK_FAIL = 0X17;
|
||||
int UPLOAD_TASK_COMPLETE = 0X18;
|
||||
int UPLOAD_TASK_RUNNING = 0X19;
|
||||
int UPLOAD_TASK_NO_SUPPORT_BREAKPOINT = 0X1A;
|
||||
String COUNT_METHOD_DOWNLOAD = "getDownloadCounter";
|
||||
String COUNT_METHOD_DOWNLOAD_GROUP = "getDownloadGroupCounter";
|
||||
String COUNT_METHOD_UPLOAD = "getUploadCounter";
|
||||
|
||||
int PRE = 0X11;
|
||||
int TASK_PRE = 0X12;
|
||||
int TASK_RESUME = 0X13;
|
||||
int TASK_START = 0X14;
|
||||
int TASK_STOP = 0X15;
|
||||
int TASK_CANCEL = 0X16;
|
||||
int TASK_FAIL = 0X17;
|
||||
int TASK_COMPLETE = 0X18;
|
||||
int TASK_RUNNING = 0X19;
|
||||
int TASK_NO_SUPPORT_BREAKPOINT = 0X1A;
|
||||
}
|
||||
|
@ -22,12 +22,12 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/6/7.
|
||||
* 创建代理方法的参数
|
||||
*/
|
||||
|
||||
class ProxyEntity {
|
||||
public String packageName;
|
||||
public String className;
|
||||
public boolean isDownlaod = true;
|
||||
public Map<String, Set<String>> keyMappings = new HashMap<>();
|
||||
public Map<Class<? extends Annotation>, String> methods = new HashMap<>();
|
||||
class ProxyMethodParam {
|
||||
String packageName;
|
||||
String className;
|
||||
TaskEnum taskEnum;
|
||||
Map<String, Set<String>> keyMappings = new HashMap<>();
|
||||
Map<Class<? extends Annotation>, String> methods = new HashMap<>();
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.compiler;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/7/10.
|
||||
* 任务类型枚举
|
||||
*/
|
||||
enum TaskEnum {
|
||||
DOWNLOAD("com.arialyy.aria.core.download", "DownloadTask", "$$DownloadListenerProxy"),
|
||||
DOWNLOAD_GROUP("com.arialyy.aria.core.download", "DownloadGroupTask", "$$DownloadGroupListenerProxy"),
|
||||
UPLOAD("com.arialyy.aria.core.upload", "UploadTask", "$$UploadListenerProxy"),
|
||||
UPLOAD_GROUP("com.arialyy.aria.core.upload", "UploadGroupTask", "$$UploadGroupListenerProxy");
|
||||
|
||||
String pkg, className, proxySuffix;
|
||||
|
||||
public String getClassName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
public String getProxySuffix() {
|
||||
return proxySuffix;
|
||||
}
|
||||
|
||||
public String getPkg() {
|
||||
return pkg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pkg 包名
|
||||
* @param className 任务完整类名
|
||||
* @param proxySuffix 事件代理后缀
|
||||
*/
|
||||
TaskEnum(String pkg, String className, String proxySuffix) {
|
||||
this.pkg = pkg;
|
||||
this.className = className;
|
||||
this.proxySuffix = proxySuffix;
|
||||
}
|
||||
|
||||
}
|
@ -19,12 +19,12 @@ import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.RadioGroup;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import com.arialyy.annotations.DownloadGroup;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
import com.arialyy.aria.core.scheduler.AbsSchedulerListener;
|
||||
import com.arialyy.aria.core.download.DownloadGroupTask;
|
||||
import com.arialyy.frame.util.show.L;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.base.BaseActivity;
|
||||
import com.arialyy.simple.databinding.ActivityDownloadGroupBinding;
|
||||
@ -46,6 +46,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
|
||||
|
||||
@Override protected void init(Bundle savedInstanceState) {
|
||||
super.init(savedInstanceState);
|
||||
Aria.download(this).register();
|
||||
setTitle("任务组");
|
||||
mUrls = getModule(GroupModule.class).getUrls();
|
||||
}
|
||||
@ -54,25 +55,14 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
|
||||
return R.layout.activity_download_group;
|
||||
}
|
||||
|
||||
@Override protected void onResume() {
|
||||
super.onResume();
|
||||
//Aria.download(this).addSchedulerListener(new AbsSchedulerListener<DownloadTask>() {
|
||||
//
|
||||
//});
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.start:
|
||||
String text = ((TextView) view).getText().toString();
|
||||
if (text.equals("重新开始?") || text.equals("开始")) {
|
||||
Aria.download(this)
|
||||
.load(mUrls)
|
||||
.setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test")
|
||||
.start();
|
||||
} else if (text.equals("恢复")) {
|
||||
//Aria.download(this).load(DOWNLOAD_URL).resume();
|
||||
}
|
||||
//String text = ((TextView) view).getText().toString();
|
||||
Aria.download(this)
|
||||
.load(mUrls)
|
||||
.setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test")
|
||||
.start();
|
||||
break;
|
||||
case R.id.stop:
|
||||
//Aria.download(this).load(DOWNLOAD_URL).pause();
|
||||
@ -82,4 +72,40 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@DownloadGroup.onPre() protected void onPre(DownloadGroupTask task) {
|
||||
L.d(TAG, "group pre");
|
||||
}
|
||||
|
||||
@DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) {
|
||||
L.d(TAG, "group task pre");
|
||||
}
|
||||
|
||||
@DownloadGroup.onTaskStart() void taskStart(DownloadGroupTask task) {
|
||||
L.d(TAG, "group task start");
|
||||
}
|
||||
|
||||
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) {
|
||||
L.d(TAG, "group task running ==> " + task.getPercent());
|
||||
}
|
||||
|
||||
@DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) {
|
||||
L.d(TAG, "group task resume");
|
||||
}
|
||||
|
||||
@DownloadGroup.onTaskStop() void taskStop(DownloadGroupTask task) {
|
||||
L.d(TAG, "group task stop");
|
||||
}
|
||||
|
||||
@DownloadGroup.onTaskCancel() void taskCancel(DownloadGroupTask task) {
|
||||
L.d(TAG, "group task cancel");
|
||||
}
|
||||
|
||||
@DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) {
|
||||
L.d(TAG, "group task fail");
|
||||
}
|
||||
|
||||
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) {
|
||||
L.d(TAG, "group task complete");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user