parent
854489851c
commit
f1d72e1651
@ -0,0 +1,67 @@ |
|||||||
|
/* |
||||||
|
* 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<TASK extends ITask> implements ISchedulerListener<TASK> { |
||||||
|
@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) { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public void onNoSupportBreakPoint(TASK task) { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public void setListener(Object obj) { |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1 @@ |
|||||||
|
/build |
@ -0,0 +1,25 @@ |
|||||||
|
apply plugin: 'java' |
||||||
|
apply plugin: 'bintray-release' |
||||||
|
|
||||||
|
tasks.withType(JavaCompile) { |
||||||
|
options.encoding = "UTF-8" |
||||||
|
} |
||||||
|
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_1_7 |
||||||
|
targetCompatibility = JavaVersion.VERSION_1_7 |
||||||
|
|
||||||
|
dependencies { |
||||||
|
compile fileTree(dir: 'libs', include: ['*.jar']) |
||||||
|
} |
||||||
|
|
||||||
|
publish { |
||||||
|
artifactId = 'aria-annotations' |
||||||
|
userOrg = rootProject.userOrg |
||||||
|
groupId = rootProject.groupId |
||||||
|
// uploadName = rootProject.uploadName |
||||||
|
uploadName = 'AriaAnnotations' |
||||||
|
publishVersion = rootProject.publishVersion |
||||||
|
description = rootProject.description |
||||||
|
website = rootProject.website |
||||||
|
licences = rootProject.licences |
||||||
|
} |
@ -0,0 +1,96 @@ |
|||||||
|
/* |
||||||
|
* 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.DownloadTask} |
||||||
|
* <pre> |
||||||
|
* <code> |
||||||
|
* protected void onPre(DownloadTask task) { |
||||||
|
* if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
* mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()).sendToTarget(); |
||||||
|
* } |
||||||
|
* } |
||||||
|
* </code> |
||||||
|
* </pre> |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Download { |
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onPre}注解,在预处理完成时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onTaskPre}注解,在任务预处理完成时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onTaskResume}注解,在任务恢复下载时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { |
||||||
|
} |
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onTaskStart}注解,在任务开始下载时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onTaskStop}注解,在任务停止时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onTaskCancel}l注解,在任务取消时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onTaskFail)注解,在任务预失败时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onTaskComplete}注解,在任务完成时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onTaskRunning}注解,在任务正在下载,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Download.onNoSupportBreakPoint}注解,如果该任务不支持断点,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) |
||||||
|
public @interface onNoSupportBreakPoint { |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,95 @@ |
|||||||
|
/* |
||||||
|
* 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 lyy on 2017/6/6. |
||||||
|
* Aria下载事件被注解的方法中,参数仅能有一个,参数类型为{@link com.arialyy.aria.core.upload.UploadTask} |
||||||
|
* <pre> |
||||||
|
* <code> |
||||||
|
* protected void onPre(UploadTask task) { |
||||||
|
* L.d(TAG, "fileSize = " + task.getConvertFileSize()); |
||||||
|
* } |
||||||
|
* </code> |
||||||
|
* </pre> |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Upload { |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onPre}注解,在预处理完成时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onTaskPre}注解,在任务预处理完成时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onTaskResume}注解,在任务恢复下载时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
//@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume {
|
||||||
|
//}
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onTaskStart}注解,在任务开始下载时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onTaskStop}注解,在任务停止时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onTaskCancel}l注解,在任务取消时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onTaskFail)注解,在任务预失败时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onTaskComplete}注解,在任务完成时,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onTaskRunning}注解,在任务正在下载,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 如果你在方法中添加{@code @Upload.onNoSupportBreakPoint}注解,如果该任务不支持断点,Aria会调用该方法 |
||||||
|
*/ |
||||||
|
@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) |
||||||
|
public @interface onNoSupportBreakPoint { |
||||||
|
} |
||||||
|
} |
@ -0,0 +1 @@ |
|||||||
|
/build |
@ -0,0 +1,28 @@ |
|||||||
|
apply plugin: 'java' |
||||||
|
apply plugin: 'bintray-release' |
||||||
|
|
||||||
|
tasks.withType(JavaCompile) { |
||||||
|
options.encoding = "UTF-8" |
||||||
|
} |
||||||
|
sourceCompatibility = JavaVersion.VERSION_1_7 |
||||||
|
targetCompatibility = JavaVersion.VERSION_1_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.9.0' |
||||||
|
compile project(':AriaAnnotations') |
||||||
|
} |
||||||
|
|
||||||
|
publish { |
||||||
|
artifactId = 'aria-compiler' |
||||||
|
userOrg = rootProject.userOrg |
||||||
|
groupId = rootProject.groupId |
||||||
|
// uploadName = rootProject.uploadName |
||||||
|
uploadName = 'AriaCompiler' |
||||||
|
publishVersion = rootProject.publishVersion |
||||||
|
description = rootProject.description |
||||||
|
website = rootProject.website |
||||||
|
licences = rootProject.licences |
||||||
|
} |
@ -0,0 +1,80 @@ |
|||||||
|
/* |
||||||
|
* 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.google.auto.service.AutoService; |
||||||
|
import java.util.LinkedHashSet; |
||||||
|
import java.util.Set; |
||||||
|
import javax.annotation.processing.AbstractProcessor; |
||||||
|
import javax.annotation.processing.ProcessingEnvironment; |
||||||
|
import javax.annotation.processing.Processor; |
||||||
|
import javax.annotation.processing.RoundEnvironment; |
||||||
|
import javax.lang.model.SourceVersion; |
||||||
|
import javax.lang.model.element.TypeElement; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/6/6. |
||||||
|
* 事件注解扫描器 |
||||||
|
*/ |
||||||
|
@AutoService(Processor.class) public class AriaProcessor extends AbstractProcessor { |
||||||
|
ElementHandle mHandler; |
||||||
|
|
||||||
|
@Override public synchronized void init(ProcessingEnvironment processingEnv) { |
||||||
|
super.init(processingEnv); |
||||||
|
PrintLog.init(processingEnv.getMessager()); |
||||||
|
mHandler = new ElementHandle(processingEnv.getFiler(), processingEnv.getElementUtils()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public Set<String> getSupportedAnnotationTypes() { |
||||||
|
Set<String> annotataions = new LinkedHashSet<>(); |
||||||
|
annotataions.add(Download.onPre.class.getCanonicalName()); |
||||||
|
annotataions.add(Download.onNoSupportBreakPoint.class.getCanonicalName()); |
||||||
|
annotataions.add(Download.onTaskCancel.class.getCanonicalName()); |
||||||
|
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()); |
||||||
|
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.onTaskRunning.class.getCanonicalName()); |
||||||
|
annotataions.add(Upload.onTaskStart.class.getCanonicalName()); |
||||||
|
annotataions.add(Upload.onTaskStop.class.getCanonicalName()); |
||||||
|
return annotataions; |
||||||
|
} |
||||||
|
|
||||||
|
@Override public SourceVersion getSupportedSourceVersion() { |
||||||
|
return SourceVersion.latestSupported(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
||||||
|
mHandler.clean(); |
||||||
|
mHandler.handleDownload(roundEnv); |
||||||
|
mHandler.handleUpload(roundEnv); |
||||||
|
mHandler.createProxyFile(); |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,279 @@ |
|||||||
|
/* |
||||||
|
* 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.List; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
import javax.annotation.processing.Filer; |
||||||
|
import javax.annotation.processing.RoundEnvironment; |
||||||
|
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. |
||||||
|
* 元素处理 |
||||||
|
*/ |
||||||
|
class ElementHandle { |
||||||
|
|
||||||
|
private Filer mFiler; |
||||||
|
private Elements mElementUtil; |
||||||
|
private Map<String, ProxyEntity> mMethods = new HashMap<>(); |
||||||
|
|
||||||
|
ElementHandle(Filer filer, Elements elements) { |
||||||
|
mFiler = filer; |
||||||
|
mElementUtil = elements; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* VariableElement 一般代表成员变量 |
||||||
|
* ExecutableElement 一般代表类中的方法 |
||||||
|
* TypeElement 一般代表代表类 |
||||||
|
* PackageElement 一般代表Package |
||||||
|
*/ |
||||||
|
void handleDownload(RoundEnvironment roundEnv) { |
||||||
|
saveMethod(true, roundEnv, Download.onNoSupportBreakPoint.class); |
||||||
|
saveMethod(true, roundEnv, Download.onPre.class); |
||||||
|
saveMethod(true, roundEnv, Download.onTaskCancel.class); |
||||||
|
saveMethod(true, roundEnv, Download.onTaskComplete.class); |
||||||
|
saveMethod(true, roundEnv, Download.onTaskFail.class); |
||||||
|
saveMethod(true, roundEnv, Download.onTaskPre.class); |
||||||
|
saveMethod(true, roundEnv, Download.onTaskResume.class); |
||||||
|
saveMethod(true, roundEnv, Download.onTaskRunning.class); |
||||||
|
saveMethod(true, roundEnv, Download.onTaskStart.class); |
||||||
|
saveMethod(true, roundEnv, Download.onTaskStop.class); |
||||||
|
} |
||||||
|
|
||||||
|
void handleUpload(RoundEnvironment roundEnv) { |
||||||
|
saveMethod(false, roundEnv, Upload.onNoSupportBreakPoint.class); |
||||||
|
saveMethod(false, roundEnv, Upload.onPre.class); |
||||||
|
saveMethod(false, roundEnv, Upload.onTaskCancel.class); |
||||||
|
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.onTaskRunning.class); |
||||||
|
saveMethod(false, roundEnv, Upload.onTaskStart.class); |
||||||
|
saveMethod(false, roundEnv, Upload.onTaskStop.class); |
||||||
|
} |
||||||
|
|
||||||
|
void printMethods() { |
||||||
|
//Set<String> 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文件夹中生成如下代码的文件 |
||||||
|
* <pre> |
||||||
|
* <code> |
||||||
|
* 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<DownloadTask> { |
||||||
|
* 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 Object obj) { |
||||||
|
* this.obj = (SingleTaskActivity)obj; |
||||||
|
* } |
||||||
|
* } |
||||||
|
* </code> |
||||||
|
* </pre> |
||||||
|
*/ |
||||||
|
void createProxyFile() { |
||||||
|
Set<String> keys = mMethods.keySet(); |
||||||
|
try { |
||||||
|
for (String key : keys) { |
||||||
|
ProxyEntity entity = mMethods.get(key); |
||||||
|
JavaFile jf = JavaFile.builder(entity.packageName, createProxyClass(entity)).build(); |
||||||
|
|
||||||
|
jf.writeTo(mFiler); |
||||||
|
// 如果需要在控制台打印生成的文件,则去掉下面的注释
|
||||||
|
//jf.writeTo(System.out);
|
||||||
|
} |
||||||
|
} catch (IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建代理方法 |
||||||
|
* |
||||||
|
* @param isDownload 是否是下载的注解 |
||||||
|
* @param annotation {@link Download}、{@link Upload} |
||||||
|
* @param methodName 被代理类注解的方法名 |
||||||
|
*/ |
||||||
|
private MethodSpec createProxyMethod(boolean isDownload, Class<? extends Annotation> 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<? extends Annotation> annotation : entity.methods.keySet()) { |
||||||
|
MethodSpec method = |
||||||
|
createProxyMethod(entity.isDownlaod, annotation, entity.methods.get(annotation)); |
||||||
|
builder.addMethod(method); |
||||||
|
} |
||||||
|
|
||||||
|
//添加设置代理的类
|
||||||
|
ParameterSpec parameterSpec = |
||||||
|
ParameterSpec.builder(Object.class, "obj").addModifiers(Modifier.FINAL).build(); |
||||||
|
MethodSpec listener = MethodSpec.methodBuilder(ProxyConstance.SET_LISTENER) |
||||||
|
.addModifiers(Modifier.PUBLIC) |
||||||
|
.returns(void.class) |
||||||
|
.addParameter(parameterSpec) |
||||||
|
.addAnnotation(Override.class) |
||||||
|
.addCode("this.obj = (" + entity.className + ")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(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 查找并保存扫描到的方法 |
||||||
|
*/ |
||||||
|
private void saveMethod(boolean isDownload, RoundEnvironment roundEnv, |
||||||
|
Class<? extends Annotation> annotationClazz) { |
||||||
|
for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) { |
||||||
|
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(); //全类名
|
||||||
|
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); |
||||||
|
} |
||||||
|
proxyEntity.methods.put(annotationClazz, methodName); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查和下载相关的方法,如果被注解的方法为private或参数不合法,则抛异常 |
||||||
|
*/ |
||||||
|
private void checkDownloadMethod(boolean isDownload, ExecutableElement method) { |
||||||
|
String methodName = method.getSimpleName().toString(); |
||||||
|
String className = method.getEnclosingElement().toString(); |
||||||
|
Set<Modifier> modifiers = method.getModifiers(); |
||||||
|
if (modifiers.contains(Modifier.PRIVATE)) { |
||||||
|
throw new IllegalAccessError(className + "." + methodName + "不能为private方法"); |
||||||
|
} |
||||||
|
List<VariableElement> params = (List<VariableElement>) method.getParameters(); |
||||||
|
if (params.size() > 1) { |
||||||
|
throw new IllegalArgumentException( |
||||||
|
className + "." + methodName + "参数错误, 参数只有一个,且参数必须是" + getCheckParams(isDownload)); |
||||||
|
} |
||||||
|
if (!params.get(0).asType().toString().equals(getCheckParams(isDownload))) { |
||||||
|
throw new IllegalArgumentException(className |
||||||
|
+ "." |
||||||
|
+ methodName |
||||||
|
+ "参数【" |
||||||
|
+ params.get(0).getSimpleName() |
||||||
|
+ "】类型错误,参数必须是" |
||||||
|
+ getCheckParams(isDownload)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private String getCheckParams(boolean isDownload) { |
||||||
|
return isDownload ? "com.arialyy.aria.core.download.DownloadTask" |
||||||
|
: "com.arialyy.aria.core.upload.UploadTask"; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,70 @@ |
|||||||
|
/* |
||||||
|
* 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; |
||||||
|
import javax.lang.model.element.Element; |
||||||
|
import javax.tools.Diagnostic; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/6/6. |
||||||
|
*/ |
||||||
|
|
||||||
|
class PrintLog { |
||||||
|
|
||||||
|
private volatile static PrintLog INSTANCE = null; |
||||||
|
private Messager mMessager; |
||||||
|
|
||||||
|
public static PrintLog init(Messager msg) { |
||||||
|
if (INSTANCE == null) { |
||||||
|
synchronized (PrintLog.class) { |
||||||
|
INSTANCE = new PrintLog(msg); |
||||||
|
} |
||||||
|
} |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
|
||||||
|
public static PrintLog getInstance() { |
||||||
|
return INSTANCE; |
||||||
|
} |
||||||
|
|
||||||
|
private PrintLog() { |
||||||
|
} |
||||||
|
|
||||||
|
private PrintLog(Messager msg) { |
||||||
|
mMessager = msg; |
||||||
|
} |
||||||
|
|
||||||
|
public void error(Element e, String msg, Object... args) { |
||||||
|
mMessager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args), e); |
||||||
|
} |
||||||
|
|
||||||
|
public void error(String msg, Object... args) { |
||||||
|
mMessager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args)); |
||||||
|
} |
||||||
|
|
||||||
|
private void warning(String msg) { |
||||||
|
mMessager.printMessage(Diagnostic.Kind.WARNING, msg); |
||||||
|
} |
||||||
|
|
||||||
|
public void error(String msg) { |
||||||
|
mMessager.printMessage(Diagnostic.Kind.ERROR, msg); |
||||||
|
} |
||||||
|
|
||||||
|
public void info(String str) { |
||||||
|
mMessager.printMessage(Diagnostic.Kind.NOTE, str); |
||||||
|
} |
||||||
|
} |
@ -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"; |
||||||
|
|
||||||
|
} |
@ -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<Class<? extends Annotation>, String> methods = new HashMap<>(); |
||||||
|
} |
@ -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<View> mViews = new SparseArray<>(); |
||||||
|
|
||||||
|
public AbsHolder(View itemView) { |
||||||
|
super(itemView); |
||||||
|
ButterKnife.bind(this, itemView); |
||||||
|
} |
||||||
|
|
||||||
|
@SuppressWarnings("unchecked") |
||||||
|
public <T extends View> T getView(@IdRes int id) { |
||||||
|
View view = mViews.get(id); |
||||||
|
if (view == null) { |
||||||
|
view = mView.findViewById(id); |
||||||
|
mViews.put(id, view); |
||||||
|
} |
||||||
|
return (T) view; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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<T, Holder extends AbsHolder> |
||||||
|
extends RecyclerView.Adapter<Holder> { |
||||||
|
protected String TAG; |
||||||
|
protected List<T> mData = new ArrayList<>(); |
||||||
|
protected Context mContext; |
||||||
|
Holder holder; |
||||||
|
|
||||||
|
public AbsRVAdapter(Context context, List<T> 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); |
||||||
|
} |
@ -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); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,19 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?><!-- |
||||||
|
Copyright (C) 2014 Lucas Rocha |
||||||
|
|
||||||
|
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. |
||||||
|
--> |
||||||
|
|
||||||
|
<resources> |
||||||
|
<item name="item_click_support" type="id" /> |
||||||
|
</resources> |
@ -1 +1 @@ |
|||||||
include ':app', ':Aria' |
include ':app', ':Aria', ':AriaAnnotations', ':AriaCompiler' |
||||||
|
Loading…
Reference in new issue