From c948aa75552ea0d98411c42c9551176e2848721e Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 10 Jul 2017 17:26:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=BB=E5=8A=A1=E7=BB=84?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E8=A7=A3=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/download/DownloadGroupTarget.java | 13 +- .../aria/core/download/DownloadReceiver.java | 26 +- .../arialyy/aria/core/inf/AbsReceiver.java | 2 +- .../aria/core/scheduler/AbsSchedulers.java | 27 +- .../scheduler/DownloadGroupSchedulers.java | 9 +- .../core/scheduler/DownloadSchedulers.java | 9 +- .../aria/core/scheduler/ISchedulers.java | 33 +- .../aria/core/scheduler/UploadSchedulers.java | 9 +- .../arialyy/aria/core/upload/ProxyHelper.java | 69 +++ .../aria/core/upload/UploadReceiver.java | 20 +- .../arialyy/annotations/DownloadGroup.java | 99 ++++ .../com/arialyy/compiler/AriaProcessor.java | 14 + .../com/arialyy/compiler/ElementHandle.java | 454 ++++++++++++------ .../java/com/arialyy/compiler/PrintLog.java | 1 - .../com/arialyy/compiler/ProxyConstance.java | 53 +- ...ProxyEntity.java => ProxyMethodParam.java} | 14 +- .../java/com/arialyy/compiler/TaskEnum.java | 53 ++ .../download/group/DownloadGroupActivity.java | 64 ++- 18 files changed, 731 insertions(+), 238 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/upload/ProxyHelper.java create mode 100644 AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java rename AriaCompiler/src/main/java/com/arialyy/compiler/{ProxyEntity.java => ProxyMethodParam.java} (76%) create mode 100644 AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index 01068408..96eca6d8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -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 mSubtaskFileName = new ArrayList<>(); + private List mSubTaskFileName = new ArrayList<>(); private String mGroupName; /** * 是否已经设置了文件路径 @@ -138,7 +135,7 @@ public class DownloadGroupTarget */ public DownloadGroupTarget setSubtaskFileName(List 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 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); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 182d752d..c09eceee 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -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 { +public class DownloadReceiver extends AbsReceiver { private final String TAG = "DownloadReceiver"; public ISchedulerListener listener; @@ -72,7 +76,15 @@ public class DownloadReceiver extends AbsReceiver { * 将当前类注册到Aria */ public DownloadReceiver register() { - DownloadSchedulers.getInstance().register(obj); + String className = obj.getClass().getName(); + Set dCounter = ProxyHelper.getInstance().downloadCounter; + Set 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 { * 取消注册 */ @Override public void unRegister() { - DownloadSchedulers.getInstance().unRegister(obj); + String className = obj.getClass().getName(); + Set dCounter = ProxyHelper.getInstance().downloadCounter; + Set dgCounter = ProxyHelper.getInstance().downloadGroupCounter; + if (dCounter.contains(className)) { + DownloadSchedulers.getInstance().unRegister(obj); + } + if (dgCounter.contains(className)) { + DownloadGroupSchedulers.getInstance().unRegister(obj); + } } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java index e19d6945..4e67339e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java @@ -20,7 +20,7 @@ package com.arialyy.aria.core.inf; * Created by AriaL on 2017/6/27. */ -public abstract class AbsReceiver implements IReceiver{ +public abstract class AbsReceiver implements IReceiver{ public String targetName; public Object obj; diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 255662ab..833d8b69 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -33,28 +33,32 @@ import java.util.concurrent.ConcurrentHashMap; /** * Created by lyy on 2017/6/4. + * 事件调度器,用于处理任务状态的调度 */ abstract class AbsSchedulers, QUEUE extends ITaskQueue> implements ISchedulers { 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> mSchedulerListeners = new ConcurrentHashMap<>(); private Map> mObservers = new ConcurrentHashMap<>(); + /** + * 设置调度器类型 + */ + abstract int getSchedulerType(); + + /** + * 设置代理类后缀名 + */ + abstract String getProxySuffix(); + /** * @param targetName 观察者,创建该监听器的对象类名 * @param schedulerListener {@link ISchedulerListener} @@ -109,8 +113,7 @@ abstract class AbsSchedulers createListener(String targetName) { AbsSchedulerListener 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) clazz.newInstance(); } catch (ClassNotFoundException e) { Log.e(TAG, targetName + ",没有Aria的Download或Upload注解方法"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java index d7d98fef..a663c886 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java @@ -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"; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java index 46e80fc0..3c492174 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java @@ -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"; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java index 7549b55a..a88e1f29 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java @@ -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 extends Handler.Callback { +public interface ISchedulers 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 extends Handler.Callback { * @param targetName 观察者,创建该监听器的对象类名 * @param schedulerListener {@link ISchedulerListener} */ - public void addSchedulerListener(String targetName, ISchedulerListener schedulerListener); + void addSchedulerListener(String targetName, ISchedulerListener schedulerListener); /** * @param targetName 观察者,创建该监听器的对象类名 * 取消注册监听器 */ - public void removeSchedulerListener(String targetName, - ISchedulerListener schedulerListener); + void removeSchedulerListener(String targetName, ISchedulerListener schedulerListener); /** * 将当前类注册到Aria * * @param obj 观察者类 */ - public void register(Object obj); + void register(Object obj); /** * 移除注册 * * @param obj 观察者类 */ - public void unRegister(Object obj); + void unRegister(Object obj); } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java index c3dfd361..be260d57 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java @@ -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"; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/ProxyHelper.java b/Aria/src/main/java/com/arialyy/aria/core/upload/ProxyHelper.java new file mode 100644 index 00000000..dd4fa338 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/ProxyHelper.java @@ -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 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) download.invoke(object)); + downloadGroupCounter = unmodifiableSet((Set) downloadGroup.invoke(object)); + uploadCounter = unmodifiableSet((Set) 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(); + } + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index eb410f07..00d44acb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -83,8 +83,7 @@ public class UploadReceiver extends AbsReceiver { 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 keys = am.getReceiver().keySet(); for (String key : keys) { @@ -110,7 +109,10 @@ public class UploadReceiver extends AbsReceiver { 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 { * 将当前类注册到Aria */ public UploadReceiver register() { - UploadSchedulers.getInstance().register(obj); + String className = obj.getClass().getName(); + Set 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 dCounter = ProxyHelper.getInstance().uploadCounter; + if (dCounter.contains(className)) { + UploadSchedulers.getInstance().unRegister(obj); + } } } \ No newline at end of file diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java new file mode 100644 index 00000000..6d139b7b --- /dev/null +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/DownloadGroup.java @@ -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} + *
+ *   
+ *      {@literal @}Download.onPre(groupName)
+ *       protected void onPre(DownloadGroupTask task) {
+ *       }
+ *   
+ * 
+ * {@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 }; + } +} diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java b/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java index 5ac4a533..9203afa8 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java @@ -42,6 +42,7 @@ import javax.lang.model.element.TypeElement; @Override public Set getSupportedAnnotationTypes() { Set 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 annotations, RoundEnvironment roundEnv) { mHandler.clean(); mHandler.handleDownload(roundEnv); + mHandler.handleDownloadGroup(roundEnv); mHandler.handleUpload(roundEnv); mHandler.createProxyFile(); return true; diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java index a09f4178..88ef94a3 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java @@ -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 mMethods = new HashMap<>(); + private Map mMethods = new HashMap<>(); + private Map> 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 { * */ void createProxyFile() { - Set 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 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 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 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 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 annotation, + private MethodSpec createProxyMethod(TaskEnum taskEnum, Class 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 keys = keyMapping.get(\"") - .append(methodName) - .append("\");\n"); + sb.append("Set 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 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 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 getValues(ExecutableElement method, boolean isDownload, int annotationType) { + private Set 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 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 modifiers = method.getModifiers(); @@ -370,16 +553,16 @@ class ElementHandle { List params = (List) 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(); } } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java b/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java index 1350de4b..161a2214 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java @@ -22,7 +22,6 @@ import javax.tools.Diagnostic; /** * Created by Aria.Lao on 2017/6/6. */ - class PrintLog { private volatile static PrintLog INSTANCE = null; diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java index 6ea65c33..98fcb980 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java @@ -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 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; - 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_METHOD_DOWNLOAD = "getDownloadCounter"; + String COUNT_METHOD_DOWNLOAD_GROUP = "getDownloadGroupCounter"; + String COUNT_METHOD_UPLOAD = "getUploadCounter"; - 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; + 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; } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyMethodParam.java similarity index 76% rename from AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java rename to AriaCompiler/src/main/java/com/arialyy/compiler/ProxyMethodParam.java index 41573cfc..c0912ca9 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyMethodParam.java @@ -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> keyMappings = new HashMap<>(); - public Map, String> methods = new HashMap<>(); +class ProxyMethodParam { + String packageName; + String className; + TaskEnum taskEnum; + Map> keyMappings = new HashMap<>(); + Map, String> methods = new HashMap<>(); } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java b/AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java new file mode 100644 index 00000000..293c19a5 --- /dev/null +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/TaskEnum.java @@ -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; + } + +} diff --git a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java index e7666bc2..560d2c96 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java @@ -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() { - // - //}); - } - 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 " + 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"); + } }