增加任务组的注解事件

pull/330/head
AriaLyy 7 years ago
parent 2f94560c9b
commit c948aa7555
  1. 13
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  2. 26
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  3. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java
  4. 27
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  5. 9
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java
  6. 9
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  7. 33
      Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java
  8. 9
      Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
  9. 69
      Aria/src/main/java/com/arialyy/aria/core/upload/ProxyHelper.java
  10. 20
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  11. 99
      AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java
  12. 14
      AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java
  13. 454
      AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java
  14. 1
      AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java
  15. 53
      AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java
  16. 14
      AriaCompiler/src/main/java/com/arialyy/compiler/ProxyMethodParam.java
  17. 53
      AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java
  18. 64
      app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java

@ -17,14 +17,11 @@ package com.arialyy.aria.core.download;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsGroupTarget; import com.arialyy.aria.core.inf.AbsGroupTarget;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Created by AriaL on 2017/6/29. * 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; private String mGroupName;
/** /**
* 是否已经设置了文件路径 * 是否已经设置了文件路径
@ -138,7 +135,7 @@ public class DownloadGroupTarget
*/ */
public DownloadGroupTarget setSubtaskFileName(List<String> subTaskFileName) { public DownloadGroupTarget setSubtaskFileName(List<String> subTaskFileName) {
if (subTaskFileName == null || subTaskFileName.isEmpty()) return this; if (subTaskFileName == null || subTaskFileName.isEmpty()) return this;
mSubtaskFileName.addAll(subTaskFileName); mSubTaskFileName.addAll(subTaskFileName);
if (mUrls.size() != subTaskFileName.size()) { if (mUrls.size() != subTaskFileName.size()) {
throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致"); throw new IllegalArgumentException("下载链接数必须要和保存路径的数量一致");
} }
@ -146,7 +143,7 @@ public class DownloadGroupTarget
List<DownloadEntity> entities = mEntity.getSubTask(); List<DownloadEntity> entities = mEntity.getSubTask();
int i = 0; int i = 0;
for (DownloadEntity entity : entities) { for (DownloadEntity entity : entities) {
entity.setFileName(mSubtaskFileName.get(i)); entity.setFileName(mSubTaskFileName.get(i));
entity.update(); entity.update();
} }
} }
@ -161,8 +158,8 @@ public class DownloadGroupTarget
for (int i = 0, len = mUrls.size(); i < len; i++) { for (int i = 0, len = mUrls.size(); i < len; i++) {
DownloadEntity entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
entity.setDownloadUrl(mUrls.get(i)); entity.setDownloadUrl(mUrls.get(i));
String fileName = mSubtaskFileName.isEmpty() ? CommonUtil.keyToHashKey(mUrls.get(i)) String fileName = mSubTaskFileName.isEmpty() ? CommonUtil.keyToHashKey(mUrls.get(i))
: mSubtaskFileName.get(i); : mSubTaskFileName.get(i);
entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName); entity.setDownloadPath(mEntity.getDirPath() + "/" + fileName);
entity.setGroupName(mGroupName); entity.setGroupName(mGroupName);
entity.setGroupChild(true); 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.AbsReceiver;
import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.normal.NormalCmdFactory; 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.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.ISchedulerListener; 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.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.lang.reflect.Method;
/** /**
* Created by lyy on 2016/12/5. * Created by lyy on 2016/12/5.
* 下载功能接收器 * 下载功能接收器
*/ */
public class DownloadReceiver extends AbsReceiver<DownloadEntity> { public class DownloadReceiver extends AbsReceiver {
private final String TAG = "DownloadReceiver"; private final String TAG = "DownloadReceiver";
public ISchedulerListener<DownloadTask> listener; public ISchedulerListener<DownloadTask> listener;
@ -72,7 +76,15 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
* 将当前类注册到Aria * 将当前类注册到Aria
*/ */
public DownloadReceiver register() { 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; return this;
} }
@ -80,7 +92,15 @@ public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
* 取消注册 * 取消注册
*/ */
@Override public void unRegister() { @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. * 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 String targetName;
public Object obj; public Object obj;

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

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

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

@ -17,54 +17,54 @@
package com.arialyy.aria.core.scheduler; package com.arialyy.aria.core.scheduler;
import android.os.Handler; 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. * 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 targetName 观察者创建该监听器的对象类名
* @param schedulerListener {@link ISchedulerListener} * @param schedulerListener {@link ISchedulerListener}
*/ */
public void addSchedulerListener(String targetName, ISchedulerListener<Task> schedulerListener); void addSchedulerListener(String targetName, ISchedulerListener<Task> schedulerListener);
/** /**
* @param targetName 观察者创建该监听器的对象类名 * @param targetName 观察者创建该监听器的对象类名
* 取消注册监听器 * 取消注册监听器
*/ */
public void removeSchedulerListener(String targetName, void removeSchedulerListener(String targetName, ISchedulerListener<Task> schedulerListener);
ISchedulerListener<Task> schedulerListener);
/** /**
* 将当前类注册到Aria * 将当前类注册到Aria
* *
* @param obj 观察者类 * @param obj 观察者类
*/ */
public void register(Object obj); void register(Object obj);
/** /**
* 移除注册 * 移除注册
* *
* @param obj 观察者类 * @param obj 观察者类
*/ */
public void unRegister(Object obj); void unRegister(Object obj);
} }

@ -32,7 +32,6 @@ public class UploadSchedulers
private UploadSchedulers() { private UploadSchedulers() {
mQueue = UploadTaskQueue.getInstance(); mQueue = UploadTaskQueue.getInstance();
isDownload = false;
} }
public static UploadSchedulers getInstance() { public static UploadSchedulers getInstance() {
@ -44,4 +43,12 @@ public class UploadSchedulers
return INSTANCE; 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); final AriaManager am = AriaManager.getInstance(AriaManager.APP);
am.setCmd(CommonUtil.createCmd(targetName, new DownloadTaskEntity(), am.setCmd(CommonUtil.createCmd(targetName, new DownloadTaskEntity(),
NormalCmdFactory.TASK_CANCEL_ALL)) NormalCmdFactory.TASK_CANCEL_ALL)).exe();
.exe();
Set<String> keys = am.getReceiver().keySet(); Set<String> keys = am.getReceiver().keySet();
for (String key : keys) { for (String key : keys) {
@ -110,7 +109,10 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
return this; return this;
} }
@Override public void removeSchedulerListener() { /**
* @see #unRegister()
*/
@Deprecated @Override public void removeSchedulerListener() {
if (listener != null) { if (listener != null) {
UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener); UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
} }
@ -120,11 +122,19 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
* 将当前类注册到Aria * 将当前类注册到Aria
*/ */
public UploadReceiver register() { 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; return this;
} }
@Override public void unRegister() { @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() { @Override public Set<String> getSupportedAnnotationTypes() {
Set<String> annotataions = new LinkedHashSet<>(); Set<String> annotataions = new LinkedHashSet<>();
//单任务下载的注解
annotataions.add(Download.onPre.class.getCanonicalName()); annotataions.add(Download.onPre.class.getCanonicalName());
annotataions.add(Download.onNoSupportBreakPoint.class.getCanonicalName()); annotataions.add(Download.onNoSupportBreakPoint.class.getCanonicalName());
annotataions.add(Download.onTaskCancel.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.onTaskRunning.class.getCanonicalName());
annotataions.add(Download.onTaskStart.class.getCanonicalName()); annotataions.add(Download.onTaskStart.class.getCanonicalName());
annotataions.add(Download.onTaskStop.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.onPre.class.getCanonicalName());
annotataions.add(Upload.onNoSupportBreakPoint.class.getCanonicalName()); annotataions.add(Upload.onNoSupportBreakPoint.class.getCanonicalName());
annotataions.add(Upload.onTaskCancel.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) { public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
mHandler.clean(); mHandler.clean();
mHandler.handleDownload(roundEnv); mHandler.handleDownload(roundEnv);
mHandler.handleDownloadGroup(roundEnv);
mHandler.handleUpload(roundEnv); mHandler.handleUpload(roundEnv);
mHandler.createProxyFile(); mHandler.createProxyFile();
return true; return true;

@ -16,6 +16,7 @@
package com.arialyy.compiler; package com.arialyy.compiler;
import com.arialyy.annotations.Download; import com.arialyy.annotations.Download;
import com.arialyy.annotations.DownloadGroup;
import com.arialyy.annotations.Upload; import com.arialyy.annotations.Upload;
import com.squareup.javapoet.ClassName; import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.CodeBlock;
@ -52,7 +53,8 @@ class ElementHandle {
private Filer mFiler; private Filer mFiler;
private Elements mElementUtil; 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) { ElementHandle(Filer filer, Elements elements) {
mFiler = filer; mFiler = filer;
@ -66,32 +68,62 @@ class ElementHandle {
* PackageElement 一般代表Package * PackageElement 一般代表Package
*/ */
void handleDownload(RoundEnvironment roundEnv) { void handleDownload(RoundEnvironment roundEnv) {
saveMethod(true, roundEnv, Download.onNoSupportBreakPoint.class, saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onNoSupportBreakPoint.class,
ProxyConstance.DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT); ProxyConstance.TASK_NO_SUPPORT_BREAKPOINT);
saveMethod(true, roundEnv, Download.onPre.class, ProxyConstance.DOWNLOAD_PRE); saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onPre.class, ProxyConstance.PRE);
saveMethod(true, roundEnv, Download.onTaskCancel.class, ProxyConstance.DOWNLOAD_TASK_CANCEL); saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskCancel.class,
saveMethod(true, roundEnv, Download.onTaskComplete.class, ProxyConstance.TASK_CANCEL);
ProxyConstance.DOWNLOAD_TASK_COMPLETE); saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskComplete.class,
saveMethod(true, roundEnv, Download.onTaskFail.class, ProxyConstance.DOWNLOAD_TASK_FAIL); ProxyConstance.TASK_COMPLETE);
saveMethod(true, roundEnv, Download.onTaskPre.class, ProxyConstance.DOWNLOAD_TASK_PRE); saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskFail.class, ProxyConstance.TASK_FAIL);
saveMethod(true, roundEnv, Download.onTaskResume.class, ProxyConstance.DOWNLOAD_TASK_RESUME); saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskPre.class, ProxyConstance.TASK_PRE);
saveMethod(true, roundEnv, Download.onTaskRunning.class, ProxyConstance.DOWNLOAD_TASK_RUNNING); saveMethod(TaskEnum.DOWNLOAD, roundEnv, Download.onTaskResume.class,
saveMethod(true, roundEnv, Download.onTaskStart.class, ProxyConstance.DOWNLOAD_TASK_START); ProxyConstance.TASK_RESUME);
saveMethod(true, roundEnv, Download.onTaskStop.class, ProxyConstance.DOWNLOAD_TASK_STOP); 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) { void handleUpload(RoundEnvironment roundEnv) {
saveMethod(false, roundEnv, Upload.onNoSupportBreakPoint.class, saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onNoSupportBreakPoint.class,
ProxyConstance.UPLOAD_TASK_NO_SUPPORT_BREAKPOINT); ProxyConstance.TASK_NO_SUPPORT_BREAKPOINT);
saveMethod(false, roundEnv, Upload.onPre.class, ProxyConstance.UPLOAD_PRE); saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onPre.class, ProxyConstance.PRE);
saveMethod(false, roundEnv, Upload.onTaskCancel.class, ProxyConstance.UPLOAD_TASK_CANCEL); saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskCancel.class, ProxyConstance.TASK_CANCEL);
saveMethod(false, roundEnv, Upload.onTaskComplete.class, ProxyConstance.UPLOAD_TASK_COMPLETE); saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskComplete.class,
saveMethod(false, roundEnv, Upload.onTaskFail.class, ProxyConstance.UPLOAD_TASK_FAIL); ProxyConstance.TASK_COMPLETE);
saveMethod(false, roundEnv, Upload.onTaskPre.class, ProxyConstance.UPLOAD_TASK_PRE); 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.onTaskResume.class);
saveMethod(false, roundEnv, Upload.onTaskRunning.class, ProxyConstance.UPLOAD_TASK_RUNNING); saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskRunning.class, ProxyConstance.TASK_RUNNING);
saveMethod(false, roundEnv, Upload.onTaskStart.class, ProxyConstance.UPLOAD_TASK_START); saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskStart.class, ProxyConstance.TASK_START);
saveMethod(false, roundEnv, Upload.onTaskStop.class, ProxyConstance.UPLOAD_TASK_STOP); saveMethod(TaskEnum.UPLOAD, roundEnv, Upload.onTaskStop.class, ProxyConstance.TASK_STOP);
} }
/** /**
@ -123,51 +155,125 @@ class ElementHandle {
* </pre> * </pre>
*/ */
void createProxyFile() { void createProxyFile() {
Set<String> keys = mMethods.keySet();
try { try {
for (String key : keys) { createProxyListenerFile();
ProxyEntity entity = mMethods.get(key); createProxyClassFile();
JavaFile jf = JavaFile.builder(entity.packageName, createProxyClass(entity)).build();
jf.writeTo(mFiler);
// 如果需要在控制台打印生成的文件,则去掉下面的注释
//jf.writeTo(System.out);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); 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 annotation {@link Download}{@link Upload}
* @param methodName 被代理类注解的方法名 * @param methodName 被代理类注解的方法名
*/ */
private MethodSpec createProxyMethod(boolean isDownload, Class<? extends Annotation> annotation, private MethodSpec createProxyMethod(TaskEnum taskEnum, Class<? extends Annotation> annotation,
String methodName) { String methodName) {
ClassName task = ClassName.get( ClassName task = ClassName.get(taskEnum.getPkg(), taskEnum.getClassName());
isDownload ? "com.arialyy.aria.core.download" : "com.arialyy.aria.core.upload",
isDownload ? "DownloadTask" : "UploadTask");
ParameterSpec parameterSpec = ParameterSpec parameterSpec =
ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build(); ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("Set<String> keys = keyMapping.get(\"") sb.append("Set<String> keys = keyMapping.get(\"").append(methodName).append("\");\n");
.append(methodName)
.append("\");\n");
sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n") sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n")
.append("\t\tobj.") .append("\t\tobj.")
.append(methodName) .append(methodName)
.append("(") .append("((")
.append(isDownload ? "(DownloadTask)" : "(UploadTask)") .append(taskEnum.getClassName())
.append("task);\n") .append(")task);\n")
.append("\t}\n} else {\n") .append("\t}\n} else {\n")
.append("\tobj.") .append("\tobj.")
.append(methodName) .append(methodName)
.append("(") .append("((")
.append(isDownload ? "(DownloadTask)" : "(UploadTask)") .append(taskEnum.getClassName())
.append("task);\n}\n"); .append(")task);\n}\n");
return MethodSpec.methodBuilder(annotation.getSimpleName()) return MethodSpec.methodBuilder(annotation.getSimpleName())
.addModifiers(Modifier.PUBLIC) .addModifiers(Modifier.PUBLIC)
@ -181,10 +287,9 @@ class ElementHandle {
/** /**
* 创建代理类 * 创建代理类
*/ */
private TypeSpec createProxyClass(ProxyEntity entity) { private TypeSpec createProxyClass(ProxyMethodParam entity) {
TypeSpec.Builder builder = TypeSpec.classBuilder( TaskEnum taskEnum = entity.taskEnum;
entity.className + (entity.isDownlaod ? ProxyConstance.DOWNLOAD_PROXY_CLASS_SUFFIX TypeSpec.Builder builder = TypeSpec.classBuilder(entity.className + taskEnum.getProxySuffix())
: ProxyConstance.UPLOAD_PROXY_CLASS_SUFFIX))
.addModifiers(Modifier.PUBLIC, Modifier.FINAL); .addModifiers(Modifier.PUBLIC, Modifier.FINAL);
//添加被代理的类的字段 //添加被代理的类的字段
@ -201,8 +306,7 @@ class ElementHandle {
//添加注解方法 //添加注解方法
for (Class<? extends Annotation> annotation : entity.methods.keySet()) { for (Class<? extends Annotation> annotation : entity.methods.keySet()) {
MethodSpec method = MethodSpec method = createProxyMethod(taskEnum, annotation, entity.methods.get(annotation));
createProxyMethod(entity.isDownlaod, annotation, entity.methods.get(annotation));
builder.addMethod(method); builder.addMethod(method);
} }
@ -241,9 +345,8 @@ class ElementHandle {
//创建父类参数 //创建父类参数
ClassName superClass = ClassName.get("com.arialyy.aria.core.scheduler", "AbsSchedulerListener"); ClassName superClass = ClassName.get("com.arialyy.aria.core.scheduler", "AbsSchedulerListener");
//创建泛型 //创建泛型
ClassName typeVariableName = ClassName.get( ClassName typeVariableName =
entity.isDownlaod ? "com.arialyy.aria.core.download" : "com.arialyy.aria.core.upload", ClassName.get(entity.taskEnum.getPkg(), entity.taskEnum.getClassName());
entity.isDownlaod ? "DownloadTask" : "UploadTask");
builder.superclass(ParameterizedTypeName.get(superClass, typeVariableName)); builder.superclass(ParameterizedTypeName.get(superClass, typeVariableName));
builder.addMethod(listener); builder.addMethod(listener);
return builder.build(); 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) { Class<? extends Annotation> annotationClazz, int annotationType) {
for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) { for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) {
ElementKind kind = element.getKind(); ElementKind kind = element.getKind();
@ -264,19 +367,19 @@ class ElementHandle {
ExecutableElement method = (ExecutableElement) element; ExecutableElement method = (ExecutableElement) element;
TypeElement classElement = (TypeElement) method.getEnclosingElement(); TypeElement classElement = (TypeElement) method.getEnclosingElement();
PackageElement packageElement = mElementUtil.getPackageOf(classElement); PackageElement packageElement = mElementUtil.getPackageOf(classElement);
checkDownloadMethod(isDownload, method); checkDownloadMethod(taskEnum, method);
String methodName = method.getSimpleName().toString(); String methodName = method.getSimpleName().toString();
String className = method.getEnclosingElement().toString(); //全类名 String className = method.getEnclosingElement().toString(); //全类名
ProxyEntity proxyEntity = mMethods.get(className); ProxyMethodParam proxyEntity = mMethods.get(className);
if (proxyEntity == null) { if (proxyEntity == null) {
proxyEntity = new ProxyEntity(); proxyEntity = new ProxyMethodParam();
proxyEntity.isDownlaod = isDownload; proxyEntity.taskEnum = taskEnum;
proxyEntity.packageName = packageElement.getQualifiedName().toString(); proxyEntity.packageName = packageElement.getQualifiedName().toString();
proxyEntity.className = classElement.getSimpleName().toString(); proxyEntity.className = classElement.getSimpleName().toString();
mMethods.put(className, proxyEntity); mMethods.put(className, proxyEntity);
} }
proxyEntity.methods.put(annotationClazz, methodName); 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; String[] keys = null;
if (isDownload) { switch (taskEnum) {
switch (annotationType) { case DOWNLOAD:
case ProxyConstance.DOWNLOAD_PRE: keys = getDownloadValues(method, annotationType);
keys = method.getAnnotation(Download.onPre.class).value(); addListenerMapping(clsName, ProxyConstance.COUNT_DOWNLOAD);
break; break;
case ProxyConstance.DOWNLOAD_TASK_PRE: case UPLOAD:
keys = method.getAnnotation(Download.onTaskPre.class).value(); keys = getUploadValues(method, annotationType);
break; addListenerMapping(clsName, ProxyConstance.COUNT_UPLOAD);
case ProxyConstance.DOWNLOAD_TASK_RESUME: break;
keys = method.getAnnotation(Download.onTaskResume.class).value(); case DOWNLOAD_GROUP:
break; keys = getDownloadGroupValues(method, annotationType);
case ProxyConstance.DOWNLOAD_TASK_START: addListenerMapping(clsName, ProxyConstance.COUNT_DOWNLOAD_GROUP);
keys = method.getAnnotation(Download.onTaskStart.class).value(); break;
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;
}
} }
return keys == null ? null : convertSet(keys); 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或参数不合法则抛异常
*/ */
private void checkDownloadMethod(boolean isDownload, ExecutableElement method) { private void checkDownloadMethod(TaskEnum taskEnum, ExecutableElement method) {
String methodName = method.getSimpleName().toString(); String methodName = method.getSimpleName().toString();
String className = method.getEnclosingElement().toString(); String className = method.getEnclosingElement().toString();
Set<Modifier> modifiers = method.getModifiers(); Set<Modifier> modifiers = method.getModifiers();
@ -370,16 +553,16 @@ class ElementHandle {
List<VariableElement> params = (List<VariableElement>) method.getParameters(); List<VariableElement> params = (List<VariableElement>) method.getParameters();
if (params.size() > 1) { if (params.size() > 1) {
throw new IllegalArgumentException( 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 throw new IllegalArgumentException(className
+ "." + "."
+ methodName + methodName
+ "参数【" + "参数【"
+ params.get(0).getSimpleName() + params.get(0).getSimpleName()
+ "】类型错误,参数必须是" + "】类型错误,参数必须是"
+ getCheckParams(isDownload)); + getCheckParams(taskEnum));
} }
} }
@ -398,8 +581,7 @@ class ElementHandle {
return set; return set;
} }
private String getCheckParams(boolean isDownload) { private String getCheckParams(TaskEnum taskEnum) {
return isDownload ? "com.arialyy.aria.core.download.DownloadTask" return taskEnum.pkg + "." + taskEnum.getClassName();
: "com.arialyy.aria.core.upload.UploadTask";
} }
} }

@ -22,7 +22,6 @@ import javax.tools.Diagnostic;
/** /**
* Created by Aria.Lao on 2017/6/6. * Created by Aria.Lao on 2017/6/6.
*/ */
class PrintLog { class PrintLog {
private volatile static PrintLog INSTANCE = null; private volatile static PrintLog INSTANCE = null;

@ -17,43 +17,44 @@ package com.arialyy.compiler;
/** /**
* Created by lyy on 2017/6/7. * Created by lyy on 2017/6/7.
* 扫描器常量
*/ */
interface ProxyConstance {
public interface ProxyConstance {
/** /**
* 设置观察者的方法 * 设置观察者的方法
*/ */
String SET_LISTENER = "setListener"; String SET_LISTENER = "setListener";
/** /**
* 下载的动态生成的代理类后缀 * 代理配置类
*/
String PROXY_COUNTER_PACKAGE = "com.arialyy.aria";
/**
* 代理分类统计
*/ */
String DOWNLOAD_PROXY_CLASS_SUFFIX = "$$DownloadListenerProxy"; String PROXY_COUNTER_NAME = "ProxyClassCounter";
/** /**
* 上传的动态生成的代理类后缀 * 代理分类统计映射表
*/ */
String UPLOAD_PROXY_CLASS_SUFFIX = "$$UploadListenerProxy"; String PROXY_COUNTER_MAP = "typeMapping";
String COUNT_DOWNLOAD = "download";
String COUNT_DOWNLOAD_GROUP = "downloadGroup";
String COUNT_UPLOAD = "upload";
int DOWNLOAD_PRE = 0X11; String COUNT_METHOD_DOWNLOAD = "getDownloadCounter";
int DOWNLOAD_TASK_PRE = 0X12; String COUNT_METHOD_DOWNLOAD_GROUP = "getDownloadGroupCounter";
int DOWNLOAD_TASK_RESUME = 0X13; String COUNT_METHOD_UPLOAD = "getUploadCounter";
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;
int UPLOAD_PRE = 0X11; int PRE = 0X11;
int UPLOAD_TASK_PRE = 0X12; int TASK_PRE = 0X12;
int UPLOAD_TASK_RESUME = 0X13; int TASK_RESUME = 0X13;
int UPLOAD_TASK_START = 0X14; int TASK_START = 0X14;
int UPLOAD_TASK_STOP = 0X15; int TASK_STOP = 0X15;
int UPLOAD_TASK_CANCEL = 0X16; int TASK_CANCEL = 0X16;
int UPLOAD_TASK_FAIL = 0X17; int TASK_FAIL = 0X17;
int UPLOAD_TASK_COMPLETE = 0X18; int TASK_COMPLETE = 0X18;
int UPLOAD_TASK_RUNNING = 0X19; int TASK_RUNNING = 0X19;
int UPLOAD_TASK_NO_SUPPORT_BREAKPOINT = 0X1A; int TASK_NO_SUPPORT_BREAKPOINT = 0X1A;
} }

@ -22,12 +22,12 @@ import java.util.Set;
/** /**
* Created by Aria.Lao on 2017/6/7. * Created by Aria.Lao on 2017/6/7.
* 创建代理方法的参数
*/ */
class ProxyMethodParam {
class ProxyEntity { String packageName;
public String packageName; String className;
public String className; TaskEnum taskEnum;
public boolean isDownlaod = true; Map<String, Set<String>> keyMappings = new HashMap<>();
public Map<String, Set<String>> keyMappings = new HashMap<>(); Map<Class<? extends Annotation>, String> methods = new HashMap<>();
public 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.os.Environment;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView; import android.widget.TextView;
import butterknife.Bind; import butterknife.Bind;
import com.arialyy.annotations.DownloadGroup;
import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.scheduler.AbsSchedulerListener; import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityDownloadGroupBinding; import com.arialyy.simple.databinding.ActivityDownloadGroupBinding;
@ -46,6 +46,7 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
@Override protected void init(Bundle savedInstanceState) { @Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState); super.init(savedInstanceState);
Aria.download(this).register();
setTitle("任务组"); setTitle("任务组");
mUrls = getModule(GroupModule.class).getUrls(); mUrls = getModule(GroupModule.class).getUrls();
} }
@ -54,25 +55,14 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
return R.layout.activity_download_group; return R.layout.activity_download_group;
} }
@Override protected void onResume() {
super.onResume();
//Aria.download(this).addSchedulerListener(new AbsSchedulerListener<DownloadTask>() {
//
//});
}
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.start: case R.id.start:
String text = ((TextView) view).getText().toString(); //String text = ((TextView) view).getText().toString();
if (text.equals("重新开始?") || text.equals("开始")) { Aria.download(this)
Aria.download(this) .load(mUrls)
.load(mUrls) .setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test")
.setDownloadDirPath(Environment.getExternalStorageDirectory().getPath() + "/group_test") .start();
.start();
} else if (text.equals("恢复")) {
//Aria.download(this).load(DOWNLOAD_URL).resume();
}
break; break;
case R.id.stop: case R.id.stop:
//Aria.download(this).load(DOWNLOAD_URL).pause(); //Aria.download(this).load(DOWNLOAD_URL).pause();
@ -82,4 +72,40 @@ public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBin
break; 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");
}
} }

Loading…
Cancel
Save