From 8049af34e04e26ae72d37b6aa1e972fa638e521c Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 7 Jun 2017 18:13:04 +0800 Subject: [PATCH] =?UTF-8?q?apt=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/core/download/DownloadReceiver.java | 16 ++ .../core/scheduler/AbsSchedulerListener.java | 59 +++++ .../aria/core/scheduler/AbsSchedulers.java | 66 +++++- .../core/scheduler/DownloadSchedulers.java | 1 + .../aria/core/scheduler/ISchedulers.java | 14 ++ .../aria/core/scheduler/UploadSchedulers.java | 1 + .../com/arialyy/annotations/Download.java | 15 ++ .../java/com/arialyy/annotations/Upload.java | 19 +- AriaCompiler/build.gradle | 2 +- .../com/arialyy/compiler/AriaProcessor.java | 21 +- .../com/arialyy/compiler/ElementHandle.java | 178 ++++++++++++++- .../java/com/arialyy/compiler/PrintLog.java | 17 +- .../com/arialyy/compiler/ProxyConstance.java | 38 ++++ .../com/arialyy/compiler/ProxyEntity.java | 32 +++ .../main/java/com/arialyy/compiler/test.java | 20 -- app/build.gradle | 2 +- .../simple/base/adapter/AbsHolder.java | 48 +++++ .../simple/base/adapter/AbsRVAdapter.java | 72 +++++++ .../base/adapter/RvItemClickSupport.java | 204 ++++++++++++++++++ .../simple/download/SingleTaskActivity.java | 7 +- .../multi_download/DownloadAdapter.java | 4 +- .../multi_download/FileListAdapter.java | 4 +- app/src/main/res/values/ids.xml | 19 ++ 23 files changed, 809 insertions(+), 50 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java create mode 100644 AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java create mode 100644 AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java delete mode 100644 AriaCompiler/src/main/java/com/arialyy/compiler/test.java create mode 100644 app/src/main/java/com/arialyy/simple/base/adapter/AbsHolder.java create mode 100644 app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java create mode 100644 app/src/main/java/com/arialyy/simple/base/adapter/RvItemClickSupport.java create mode 100644 app/src/main/res/values/ids.xml 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 d52a18af..da2cd54b 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 @@ -68,6 +68,22 @@ public class DownloadReceiver implements IReceiver { return this; } + /** + * 将当前类注册到Aria + */ + public DownloadReceiver register() { + DownloadSchedulers.getInstance().register(targetName); + return this; + } + + /** + * 取消注册 + */ + public DownloadReceiver unRegister() { + DownloadSchedulers.getInstance().unRegister(targetName); + return this; + } + /** * 移除回调 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java new file mode 100644 index 00000000..4c73c8f5 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulerListener.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.arialyy.aria.core.scheduler; + +import com.arialyy.aria.core.inf.ITask; + +/** + * Created by Aria.Lao on 2017/6/7. + */ +public class AbsSchedulerListener implements ISchedulerListener { + @Override public void onPre(TASK task) { + + } + + @Override public void onTaskPre(TASK task) { + + } + + @Override public void onTaskResume(TASK task) { + + } + + @Override public void onTaskStart(TASK task) { + + } + + @Override public void onTaskStop(TASK task) { + + } + + @Override public void onTaskCancel(TASK task) { + + } + + @Override public void onTaskFail(TASK task) { + + } + + @Override public void onTaskComplete(TASK task) { + + } + + @Override public void onTaskRunning(TASK task) { + + } +} 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 4fee513f..864010a1 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 @@ -26,27 +26,40 @@ import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.queue.ITaskQueue; import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.compiler.ProxyConstance; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** - * Created by AriaL on 2017/6/4. + * Created by lyy on 2017/6/4. */ public abstract class AbsSchedulers, QUEUE extends ITaskQueue> implements ISchedulers { private static final String TAG = "AbsSchedulers"; protected QUEUE mQueue; + protected boolean isDownload = true; private Map> mSchedulerListeners = new ConcurrentHashMap<>(); - @Override - public void addSchedulerListener(String targetName, ISchedulerListener schedulerListener) { + private Map> mObservers = new ConcurrentHashMap<>(); + + /** + * @param targetName 观察者,创建该监听器的对象类名 + * @param schedulerListener {@link ISchedulerListener} + * @see #register(String) + */ + @Deprecated @Override public void addSchedulerListener(String targetName, + ISchedulerListener schedulerListener) { mSchedulerListeners.put(targetName, schedulerListener); } + /** + * @param targetName 观察者,创建该监听器的对象类名 + * @see #unRegister(String) + */ @Override public void removeSchedulerListener(String targetName, ISchedulerListener schedulerListener) { //该内存泄露解决方案:http://stackoverflow.com/questions/14585829/how-safe-is-to-delete-already-removed-concurrenthashmap-element @@ -57,6 +70,46 @@ public abstract class AbsSchedulers listener = mObservers.get(targetName); + if (listener == null) { + mObservers.put(targetName, createListener(targetName)); + } + } + + @Override public void unRegister(String targetName) { + for (Iterator>> iter = + mObservers.entrySet().iterator(); iter.hasNext(); ) { + Map.Entry> entry = iter.next(); + if (entry.getKey().equals(targetName)) iter.remove(); + } + } + + /** + * 创建代理类 + * + * @param targetName 通过观察者创建对应的Aria事件代理 + */ + private AbsSchedulerListener createListener(String targetName) { + AbsSchedulerListener listener = null; + try { + Class clazz = Class.forName( + targetName + (isDownload ? ProxyConstance.DOWNLOAD_PROXY_CLASS_SUFFIX + : ProxyConstance.UPLOAD_PROXY_CLASS_SUFFIX)); + listener = (AbsSchedulerListener) clazz.newInstance(); + } catch (ClassNotFoundException e) { + Log.e(TAG, targetName + ",没有Aria的Download或Upload注解方法"); + //e.printStackTrace(); + } catch (InstantiationException e) { + Log.e(TAG, e.getMessage()); + //e.printStackTrace(); + } catch (IllegalAccessException e) { + Log.e(TAG, e.getMessage()); + //e.printStackTrace(); + } + return listener; + } + @Override public boolean handleMessage(Message msg) { TASK task = (TASK) msg.obj; if (task == null) { @@ -98,7 +151,12 @@ public abstract class AbsSchedulers 0) { Set keys = mSchedulerListeners.keySet(); for (String key : keys) { - callback(state, task, mSchedulerListeners.get(key)); + if (mSchedulerListeners.size() > 0) { + callback(state, task, mSchedulerListeners.get(key)); + } + if (mObservers.size() > 0) { + callback(state, task, mObservers.get(key)); + } } } } 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 ed8731bb..f6137abf 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,6 +34,7 @@ public class DownloadSchedulers private DownloadSchedulers() { mQueue = DownloadTaskQueue.getInstance(); + isDownload = true; } public static DownloadSchedulers getInstance() { 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 5a240b7b..dc673315 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 @@ -80,4 +80,18 @@ public interface ISchedulers extends Handler.Callback { */ public void removeSchedulerListener(String targetName, ISchedulerListener schedulerListener); + + /** + * 将当前类注册到Aria + * + * @param targetName 类完整路径 + */ + public void register(String targetName); + + /** + * 移除注册 + * + * @param targetName 类完整路径 + */ + public void unRegister(String targetName); } \ 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 37a6e3d0..c3dfd361 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,6 +32,7 @@ public class UploadSchedulers private UploadSchedulers() { mQueue = UploadTaskQueue.getInstance(); + isDownload = false; } public static UploadSchedulers getInstance() { diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java index bbd0385a..7a6cf5d6 100644 --- a/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/Download.java @@ -1,3 +1,18 @@ +/* + * 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; diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java index fe136bb7..50de0d2a 100644 --- a/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java @@ -1,3 +1,18 @@ +/* + * 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; @@ -15,8 +30,8 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { } - @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { - } + //@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { + //} @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { } diff --git a/AriaCompiler/build.gradle b/AriaCompiler/build.gradle index 665aa139..523d9a5d 100644 --- a/AriaCompiler/build.gradle +++ b/AriaCompiler/build.gradle @@ -8,7 +8,7 @@ dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.google.auto:auto-common:0.6' compile 'com.google.auto.service:auto-service:1.0-rc2' - compile 'com.squareup:javapoet:1.7.0' + compile 'com.squareup:javapoet:1.9.0' compile project(':AriaAnnotations') sourceCompatibility = "1.7" diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java b/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java index 9bcb0bec..5ac4a533 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/AriaProcessor.java @@ -1,3 +1,18 @@ +/* + * 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; import com.arialyy.annotations.Download; @@ -22,7 +37,7 @@ import javax.lang.model.element.TypeElement; @Override public synchronized void init(ProcessingEnvironment processingEnv) { super.init(processingEnv); PrintLog.init(processingEnv.getMessager()); - mHandler = new ElementHandle(processingEnv.getFiler()); + mHandler = new ElementHandle(processingEnv.getFiler(), processingEnv.getElementUtils()); } @Override public Set getSupportedAnnotationTypes() { @@ -43,7 +58,7 @@ import javax.lang.model.element.TypeElement; annotataions.add(Upload.onTaskComplete.class.getCanonicalName()); annotataions.add(Upload.onTaskFail.class.getCanonicalName()); annotataions.add(Upload.onTaskPre.class.getCanonicalName()); - annotataions.add(Upload.onTaskResume.class.getCanonicalName()); + //annotataions.add(Upload.onTaskResume.class.getCanonicalName()); annotataions.add(Upload.onTaskRunning.class.getCanonicalName()); annotataions.add(Upload.onTaskStart.class.getCanonicalName()); annotataions.add(Upload.onTaskStop.class.getCanonicalName()); @@ -56,10 +71,10 @@ import javax.lang.model.element.TypeElement; @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { - PrintLog.getInstance().info("开始扫描"); mHandler.clean(); mHandler.handleDownload(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 4d662c48..8bb9cfea 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java @@ -1,10 +1,32 @@ +/* + * 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; import com.arialyy.annotations.Download; import com.arialyy.annotations.Upload; +import com.squareup.javapoet.ClassName; +import com.squareup.javapoet.FieldSpec; +import com.squareup.javapoet.JavaFile; +import com.squareup.javapoet.MethodSpec; +import com.squareup.javapoet.ParameterSpec; +import com.squareup.javapoet.TypeSpec; +import com.squareup.javapoet.ParameterizedTypeName; +import java.io.IOException; import java.lang.annotation.Annotation; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -14,7 +36,10 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; +import javax.lang.model.util.Elements; /** * Created by lyy on 2017/6/6. @@ -23,10 +48,12 @@ import javax.lang.model.element.VariableElement; class ElementHandle { private Filer mFiler; - private Map> mMethods = new HashMap<>(); + private Elements mElementUtil; + private Map mMethods = new HashMap<>(); - ElementHandle(Filer filer) { + ElementHandle(Filer filer, Elements elements) { mFiler = filer; + mElementUtil = elements; } /** @@ -55,12 +82,137 @@ class ElementHandle { saveMethod(false, roundEnv, Upload.onTaskComplete.class); saveMethod(false, roundEnv, Upload.onTaskFail.class); saveMethod(false, roundEnv, Upload.onTaskPre.class); - saveMethod(false, roundEnv, Upload.onTaskResume.class); + //saveMethod(false, roundEnv, Upload.onTaskResume.class); saveMethod(false, roundEnv, Upload.onTaskRunning.class); saveMethod(false, roundEnv, Upload.onTaskStart.class); saveMethod(false, roundEnv, Upload.onTaskStop.class); } + void printMethods() { + //Set keys = mMethods.keySet(); + //for (String key : keys) { + // ProxyEntity entity = mMethods.get(key); + // for (String method : entity.methods) { + // PrintLog.getInstance().info(method); + // } + //} + PrintLog.getInstance().info("size ==> " + mMethods.size()); + } + + /** + * 在build文件夹中生成如下代码的文件 + *
+   *   
+   * package com.arialyy.simple.download;
+   *
+   * import com.arialyy.aria.core.download.DownloadTask;
+   * import com.arialyy.aria.core.scheduler.AbsSchedulerListener;
+   *
+   * public final class SingleTaskActivity$$DownloadListenerProxy extends
+   * AbsSchedulerListener {
+   * private SingleTaskActivity obj;
+   *
+   *    public void onPre(final DownloadTask task) {
+   *      obj.onPre((DownloadTask)task);
+   *    }
+   *
+   *    public void onTaskStart(final DownloadTask task) {
+   *      obj.onStart((DownloadTask)task);
+   *    }
+   *
+   *    public void setListener(final SingleTaskActivity obj) {
+   *      this.obj = obj;
+   *    }
+   * }
+   *   
+   * 
+ */ + 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); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 创建代理方法 + * + * @param isDownload 是否是下载的注解 + * @param annotation {@link Download}、{@link Upload} + * @param methodName 被代理类注解的方法名 + */ + private MethodSpec createProxyMethod(boolean isDownload, Class annotation, + String methodName) { + ClassName task = ClassName.get( + isDownload ? "com.arialyy.aria.core.download" : "com.arialyy.aria.core.upload", + isDownload ? "DownloadTask" : "UploadTask"); + ParameterSpec parameterSpec = + ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build(); + return MethodSpec.methodBuilder(annotation.getSimpleName()) + .addModifiers(Modifier.PUBLIC) + .returns(void.class) + .addParameter(parameterSpec) + .addAnnotation(Override.class) + .addCode("obj." + + methodName + + "(" + + (isDownload ? "(DownloadTask)" : "(UploadTask)") + + "task);\n") + .build(); + } + + /** + * 创建代理类 + */ + private TypeSpec createProxyClass(ProxyEntity entity) { + TypeSpec.Builder builder = TypeSpec.classBuilder( + entity.className + (entity.isDownlaod ? ProxyConstance.DOWNLOAD_PROXY_CLASS_SUFFIX + : ProxyConstance.UPLOAD_PROXY_CLASS_SUFFIX)) + .addModifiers(Modifier.PUBLIC, Modifier.FINAL); + + //添加被代理的类的字段 + ClassName obj = ClassName.get(entity.packageName, entity.className); + FieldSpec observerField = FieldSpec.builder(obj, "obj").addModifiers(Modifier.PRIVATE).build(); + builder.addField(observerField); + + //添加注解方法 + for (Class annotation : entity.methods.keySet()) { + MethodSpec method = + createProxyMethod(entity.isDownlaod, annotation, entity.methods.get(annotation)); + builder.addMethod(method); + } + + //添加设置代理的类 + ParameterSpec parameterSpec = + ParameterSpec.builder(obj, "obj").addModifiers(Modifier.FINAL).build(); + MethodSpec listener = MethodSpec.methodBuilder(ProxyConstance.SET_LISTENER) + .addModifiers(Modifier.PUBLIC) + .returns(void.class) + .addParameter(parameterSpec) + .addCode("this.obj = obj;\n") + .build(); + builder.addJavadoc("该文件为Aria自动生成的代理文件,请不要修改该文件的任何代码!\n"); + + //创建父类参数 + 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"); + builder.superclass(ParameterizedTypeName.get(superClass, typeVariableName)); + builder.addMethod(listener); + return builder.build(); + } + void clean() { mMethods.clear(); } @@ -74,15 +226,21 @@ class ElementHandle { ElementKind kind = element.getKind(); if (kind == ElementKind.METHOD) { ExecutableElement method = (ExecutableElement) element; + TypeElement classElement = (TypeElement) method.getEnclosingElement(); + PackageElement packageElement = mElementUtil.getPackageOf(classElement); checkDownloadMethod(isDownload, method); String methodName = method.getSimpleName().toString(); - String className = method.getEnclosingElement().toString(); - Set methods = mMethods.get(className); - if (methods == null) { - methods = new HashSet<>(); - mMethods.put(className, methods); + String className = method.getEnclosingElement().toString(); //全类名 + ProxyEntity proxyEntity = mMethods.get(className); + if (proxyEntity == null) { + proxyEntity = new ProxyEntity(); + proxyEntity.isDownlaod = isDownload; + //proxyEntity.packageName = classElement.getQualifiedName().toString(); + proxyEntity.packageName = packageElement.getQualifiedName().toString(); + proxyEntity.className = classElement.getSimpleName().toString(); + mMethods.put(className, proxyEntity); } - methods.add(methodName); + proxyEntity.methods.put(annotationClazz, methodName); } } } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java b/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java index 62383c8b..1350de4b 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/PrintLog.java @@ -1,3 +1,18 @@ +/* + * 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; import javax.annotation.processing.Messager; @@ -8,7 +23,7 @@ import javax.tools.Diagnostic; * Created by Aria.Lao on 2017/6/6. */ -public class PrintLog { +class PrintLog { private volatile static PrintLog INSTANCE = null; private Messager mMessager; diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java new file mode 100644 index 00000000..7e9ac1ea --- /dev/null +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java @@ -0,0 +1,38 @@ +/* + * 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 lyy on 2017/6/7. + */ + +public interface ProxyConstance { + /** + * 设置观察者的方法 + */ + String SET_LISTENER = "setListener"; + + /** + * 下载的动态生成的代理类后缀 + */ + String DOWNLOAD_PROXY_CLASS_SUFFIX = "$$DownloadListenerProxy"; + + /** + * 上传的动态生成的代理类后缀 + */ + String UPLOAD_PROXY_CLASS_SUFFIX = "$$UploadListenerProxy"; + +} diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java new file mode 100644 index 00000000..9c6e9070 --- /dev/null +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java @@ -0,0 +1,32 @@ +/* + * 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; + +import java.lang.annotation.Annotation; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Aria.Lao on 2017/6/7. + */ + +class ProxyEntity { + public String packageName; + public String className; + public boolean isDownlaod = true; + + public Map, String> methods = new HashMap<>(); +} diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/test.java b/AriaCompiler/src/main/java/com/arialyy/compiler/test.java deleted file mode 100644 index 05722241..00000000 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/test.java +++ /dev/null @@ -1,20 +0,0 @@ -//package com.arialyy.compiler; -// -///** -// * Created by AriaL on 2017/6/6. -// */ -// -//public class test { -// Object obj; -// -// public void setObj(Object obj){ -// this.obj = obj; -// } -// -// public void onPre(DownloadTask task){ -// obj.method(task); -// } -// -// -// -//} diff --git a/app/build.gradle b/app/build.gradle index 4eba9ffb..09412848 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,6 +24,7 @@ android { packagingOptions { exclude 'META-INF/services/javax.annotation.processing.Processor' + exclude 'squareup.javapoet.*' } } @@ -36,7 +37,6 @@ dependencies { compile 'com.google.code.gson:gson:2.7' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.arialyy.frame:MVVM2:2.2.0' - compile 'com.arialyy.absadapter:AbsAdapter:1.1.2' compile project(':Aria') // compile 'com.arialyy.aria:Aria:3.0.0' } diff --git a/app/src/main/java/com/arialyy/simple/base/adapter/AbsHolder.java b/app/src/main/java/com/arialyy/simple/base/adapter/AbsHolder.java new file mode 100644 index 00000000..c7d48f9f --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/base/adapter/AbsHolder.java @@ -0,0 +1,48 @@ +/* + * 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.simple.base.adapter; + +import android.support.annotation.IdRes; +import android.support.v7.widget.RecyclerView; +import android.util.SparseArray; +import android.view.View; + +import butterknife.ButterKnife; + +/** + * Created by lyy on 2015/12/3. + * 通用Holder + */ +public class AbsHolder extends RecyclerView.ViewHolder { + View mView; + private SparseArray mViews = new SparseArray<>(); + + public AbsHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + } + + @SuppressWarnings("unchecked") + public T getView(@IdRes int id) { + View view = mViews.get(id); + if (view == null) { + view = mView.findViewById(id); + mViews.put(id, view); + } + return (T) view; + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java b/app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java new file mode 100644 index 00000000..744ccd94 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/base/adapter/AbsRVAdapter.java @@ -0,0 +1,72 @@ +/* + * 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.simple.base.adapter; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by lyy on 2015/12/3. + * RecyclerView 通用Adapter + */ +public abstract class AbsRVAdapter + extends RecyclerView.Adapter { + protected String TAG; + protected List mData = new ArrayList<>(); + protected Context mContext; + Holder holder; + + public AbsRVAdapter(Context context, List data) { + mData = data; + mContext = context; + String arrays[] = getClass().getName().split("\\."); + TAG = arrays[arrays.length - 1]; + } + + @Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = + LayoutInflater.from(parent.getContext()).inflate(setLayoutId(viewType), parent, false); + holder = getViewHolder(view, viewType); + return holder; + } + + protected abstract Holder getViewHolder(View convertView, int viewType); + + @Override public void onBindViewHolder(Holder holder, int position) { + bindData(holder, position, mData.get(position)); + } + + public Context getContext() { + return mContext; + } + + @Override public int getItemCount() { + return mData.size(); + } + + /** + * item 的type + */ + protected abstract int setLayoutId(int type); + + protected abstract void bindData(Holder holder, int position, T item); +} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/base/adapter/RvItemClickSupport.java b/app/src/main/java/com/arialyy/simple/base/adapter/RvItemClickSupport.java new file mode 100644 index 00000000..e42bb060 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/base/adapter/RvItemClickSupport.java @@ -0,0 +1,204 @@ +/* + * 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.simple.base.adapter; + +import android.support.v7.widget.RecyclerView; +import android.view.KeyEvent; +import android.view.MotionEvent; +import android.view.View; +import com.arialyy.simple.R; + +/* + * RecyclerView item 事件监听帮助类 + * RvItemClickSupport.addTo(recyclerView).setOnItemClickListener(new RvItemClickSupport.OnItemClickListener() { + * + * @Override + * public void onItemClicked(RecyclerView recyclerView, int position, View v) { + * //处理你的事件 + * }); + */ +public class RvItemClickSupport { + private final RecyclerView mRecyclerView; + private OnItemClickListener mOnItemClickListener; + private OnItemLongClickListener mOnItemLongClickListener; + private OnItemTouchListener mOnItemTouchListener; + private OnItemFocusChangeListener mOnItemFocusChangeListener; + private OnItemKeyListener mOnItemKeyListener; + + public interface OnItemClickListener { + void onItemClicked(RecyclerView recyclerView, int position, View v); + } + + public interface OnItemLongClickListener { + boolean onItemLongClicked(RecyclerView recyclerView, int position, View v); + } + + public interface OnItemTouchListener { + public void onTouchEvent(RecyclerView rv, MotionEvent e, int position, View v); + } + + public interface OnItemFocusChangeListener { + public void onFocusChange(View v, int position, boolean hasFocus); + } + + public interface OnItemKeyListener { + public boolean onKey(View v, int keyCode, int position, KeyEvent event); + } + + private View.OnFocusChangeListener mOnFocusChangeListener = new View.OnFocusChangeListener() { + + @Override public void onFocusChange(View v, boolean hasFocus) { + if (mOnItemFocusChangeListener != null) { + RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); + mOnItemFocusChangeListener.onFocusChange(v, holder.getAdapterPosition(), + holder.itemView.hasFocus()); + } + } + }; + + private View.OnClickListener mOnClickListener = new View.OnClickListener() { + @Override public void onClick(View v) { + if (mOnItemClickListener != null) { + RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); + mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v); + } + } + }; + + private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() { + @Override public boolean onLongClick(View v) { + if (mOnItemLongClickListener != null) { + RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); + return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, + holder.getAdapterPosition(), v); + } + return false; + } + }; + + private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() { + @Override public boolean onTouch(View v, MotionEvent event) { + if (mOnItemTouchListener != null) { + RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); + mOnItemTouchListener.onTouchEvent(mRecyclerView, event, holder.getAdapterPosition(), v); + } + return false; + } + }; + + private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() { + @Override public boolean onKey(View v, int keyCode, KeyEvent event) { + if (mOnItemKeyListener != null) { + RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); + return mOnItemKeyListener.onKey(v, keyCode, holder.getAdapterPosition(), event); + } + return false; + } + }; + + private RecyclerView.OnChildAttachStateChangeListener mAttachListener = + new RecyclerView.OnChildAttachStateChangeListener() { + @Override public void onChildViewAttachedToWindow(View view) { + if (mOnItemClickListener != null) { + view.setOnClickListener(mOnClickListener); + } + if (mOnItemLongClickListener != null) { + view.setOnLongClickListener(mOnLongClickListener); + } + if (mOnItemTouchListener != null) { + view.setOnTouchListener(mOnTouchListener); + } + if (mOnItemFocusChangeListener != null) { + view.setOnFocusChangeListener(mOnFocusChangeListener); + } + if (mOnItemKeyListener != null) { + view.setOnKeyListener(mOnKeyListener); + } + } + + @Override public void onChildViewDetachedFromWindow(View view) { + + } + }; + + private RvItemClickSupport(RecyclerView recyclerView) { + mRecyclerView = recyclerView; + mRecyclerView.setTag(R.id.item_click_support, this); + mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener); + } + + public static RvItemClickSupport addTo(RecyclerView view) { + RvItemClickSupport support = (RvItemClickSupport) view.getTag(R.id.item_click_support); + if (support == null) { + support = new RvItemClickSupport(view); + } + return support; + } + + public static RvItemClickSupport removeFrom(RecyclerView view) { + RvItemClickSupport support = (RvItemClickSupport) view.getTag(R.id.item_click_support); + if (support != null) { + support.detach(view); + } + return support; + } + + /** + * 设置按键监听 + */ + public RvItemClickSupport setOnItemKeyListenr(OnItemKeyListener onItemKeyListener) { + mOnItemKeyListener = onItemKeyListener; + return this; + } + + /** + * 设置焦点监听 + */ + public RvItemClickSupport setOnItemFocusChangeListener( + OnItemFocusChangeListener onItemFocusChangeListener) { + mOnItemFocusChangeListener = onItemFocusChangeListener; + return this; + } + + /** + * 设置触摸监听 + */ + public RvItemClickSupport setOnItemTouchListener(OnItemTouchListener onItemTouchListener) { + mOnItemTouchListener = onItemTouchListener; + return this; + } + + /** + * 设置点击监听 + */ + public RvItemClickSupport setOnItemClickListener(OnItemClickListener listener) { + mOnItemClickListener = listener; + return this; + } + + /** + * 设置长按监听 + */ + public RvItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) { + mOnItemLongClickListener = listener; + return this; + } + + private void detach(RecyclerView view) { + view.removeOnChildAttachStateChangeListener(mAttachListener); + view.setTag(R.id.item_click_support, null); + } +} diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index 03bb115c..d4dcbdda 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -34,7 +34,6 @@ import android.widget.TextView; import android.widget.Toast; import butterknife.Bind; import com.arialyy.annotations.Download; -import com.arialyy.annotations.Test; import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; @@ -134,12 +133,12 @@ public class SingleTaskActivity extends BaseActivity { } }; - @Download.onPre private void hehe(String str, DownloadTask task) { + @Download.onPre void onPre(DownloadTask task) { } - @Test - public void gg(){ + @Download.onTaskStart + void onStart(DownloadTask task){ } diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java index 4b3dff78..d05e51a4 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java @@ -22,12 +22,12 @@ import android.view.View; import android.widget.Button; import android.widget.TextView; import butterknife.Bind; -import com.arialyy.absadapter.common.AbsHolder; -import com.arialyy.absadapter.recycler_view.AbsRVAdapter; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; +import com.arialyy.simple.base.adapter.AbsHolder; +import com.arialyy.simple.base.adapter.AbsRVAdapter; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; import java.util.List; import java.util.Map; diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java b/app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java index 13040407..bdf64ed1 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java @@ -22,10 +22,10 @@ import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import butterknife.Bind; -import com.arialyy.absadapter.common.AbsHolder; -import com.arialyy.absadapter.recycler_view.AbsRVAdapter; import com.arialyy.aria.core.Aria; import com.arialyy.simple.R; +import com.arialyy.simple.base.adapter.AbsHolder; +import com.arialyy.simple.base.adapter.AbsRVAdapter; import java.util.List; import java.util.Map; import java.util.Set; diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml new file mode 100644 index 00000000..4bb1b5c2 --- /dev/null +++ b/app/src/main/res/values/ids.xml @@ -0,0 +1,19 @@ + + + + + \ No newline at end of file