commit
5a70764596
@ -0,0 +1,103 @@ |
|||||||
|
/* |
||||||
|
* 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; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import android.util.Log; |
||||||
|
import com.arialyy.aria.core.command.group.GroupCmdFactory; |
||||||
|
import com.arialyy.aria.core.inf.BaseGroupTaskEntity; |
||||||
|
import com.arialyy.aria.util.CommonUtil; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/9/4. |
||||||
|
* 子任务管理器 |
||||||
|
*/ |
||||||
|
public class SubTaskManager { |
||||||
|
private String TAG = "SubTaskManager"; |
||||||
|
private BaseGroupTaskEntity mEntity; |
||||||
|
private String mTargetName; |
||||||
|
|
||||||
|
public SubTaskManager(String targetName, BaseGroupTaskEntity entity) { |
||||||
|
mTargetName = targetName; |
||||||
|
mEntity = entity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 启动任务组中的子任务 |
||||||
|
* |
||||||
|
* @param url 子任务下载地址 |
||||||
|
*/ |
||||||
|
public void startSubTask(String url) { |
||||||
|
if (checkUrl(url)) { |
||||||
|
AriaManager.getInstance(AriaManager.APP) |
||||||
|
.setCmd( |
||||||
|
CommonUtil.createGroupCmd(mTargetName, mEntity, GroupCmdFactory.SUB_TASK_START, url)) |
||||||
|
.exe(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 停止任务组中的子任务 |
||||||
|
* |
||||||
|
* @param url 子任务下载地址 |
||||||
|
*/ |
||||||
|
public void stopSubTask(String url) { |
||||||
|
if (checkUrl(url)) { |
||||||
|
AriaManager.getInstance(AriaManager.APP) |
||||||
|
.setCmd( |
||||||
|
CommonUtil.createGroupCmd(mTargetName, mEntity, GroupCmdFactory.SUB_TASK_STOP, url)) |
||||||
|
.exe(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除子任务组中的子任务 |
||||||
|
* |
||||||
|
* @param url 子任务下载地址 |
||||||
|
*/ |
||||||
|
public void cancelSubTask(String url) { |
||||||
|
if (checkUrl(url)) { |
||||||
|
AriaManager.getInstance(AriaManager.APP) |
||||||
|
.setCmd( |
||||||
|
CommonUtil.createGroupCmd(mTargetName, mEntity, GroupCmdFactory.SUB_TASK_CANCEL, url)) |
||||||
|
.exe(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查任务地址 |
||||||
|
* |
||||||
|
* @param url 子任务地址 |
||||||
|
* @return {@code false} 任务地址不合法 |
||||||
|
*/ |
||||||
|
private boolean checkUrl(String url) { |
||||||
|
if (TextUtils.isEmpty(url)) { |
||||||
|
Log.e(TAG, "子任务地址不能为null"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
List<String> urls = mEntity.getEntity().getUrls(); |
||||||
|
if (urls == null || urls.isEmpty()) { |
||||||
|
Log.e(TAG, "任务组任务链接为null"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (!urls.contains(url)) { |
||||||
|
Log.e(TAG, "任务组中没有改Url【+ " + url + "】"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
@ -1,118 +0,0 @@ |
|||||||
/* |
|
||||||
* 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.download.downloader; |
|
||||||
|
|
||||||
import android.text.TextUtils; |
|
||||||
import android.util.Log; |
|
||||||
import com.arialyy.aria.core.AriaManager; |
|
||||||
import java.io.IOException; |
|
||||||
import org.apache.commons.net.ftp.FTPClient; |
|
||||||
import org.apache.commons.net.ftp.FTPReply; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by Aria.Lao on 2017/7/26. |
|
||||||
*/ |
|
||||||
public class FtpClientHelp { |
|
||||||
private final String TAG = "FtpClientHelp"; |
|
||||||
private static volatile FtpClientHelp INSTANCE = null; |
|
||||||
|
|
||||||
private FTPClient client; |
|
||||||
private String serverIp, user, pw, account; |
|
||||||
private int port; |
|
||||||
|
|
||||||
private FtpClientHelp() { |
|
||||||
} |
|
||||||
|
|
||||||
public static FtpClientHelp getInstnce() { |
|
||||||
if (INSTANCE == null) { |
|
||||||
synchronized (AriaManager.LOCK) { |
|
||||||
INSTANCE = new FtpClientHelp(); |
|
||||||
} |
|
||||||
} |
|
||||||
return INSTANCE; |
|
||||||
} |
|
||||||
|
|
||||||
public FTPClient getClient() { |
|
||||||
if (client == null || !client.isConnected()) { |
|
||||||
createClient(); |
|
||||||
} |
|
||||||
return client; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 登录到FTP服务器,当客户端为null或客户端没有连接到FTP服务器时才会执行登录操作 |
|
||||||
*/ |
|
||||||
public FTPClient login(String serverIp, int port, String user, String pw, String account) { |
|
||||||
this.serverIp = serverIp; |
|
||||||
this.port = port; |
|
||||||
this.user = user; |
|
||||||
this.pw = pw; |
|
||||||
this.account = account; |
|
||||||
if (client == null || !client.isConnected()) { |
|
||||||
createClient(); |
|
||||||
} |
|
||||||
return client; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 登出 |
|
||||||
*/ |
|
||||||
public void logout() { |
|
||||||
try { |
|
||||||
if (client != null && client.isConnected()) { |
|
||||||
client.logout(); |
|
||||||
} |
|
||||||
} catch (IOException e) { |
|
||||||
e.printStackTrace(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
FTPClient createClient() { |
|
||||||
new Thread(new Runnable() { |
|
||||||
@Override public void run() { |
|
||||||
client = new FTPClient(); |
|
||||||
try { |
|
||||||
client.connect(serverIp, port); |
|
||||||
if (!TextUtils.isEmpty(account)) { |
|
||||||
client.login(user, pw); |
|
||||||
} else { |
|
||||||
client.login(user, pw, account); |
|
||||||
} |
|
||||||
int reply = client.getReplyCode(); |
|
||||||
if (!FTPReply.isPositiveCompletion(reply)) { |
|
||||||
client.disconnect(); |
|
||||||
Log.e(TAG, "无法连接到ftp服务器,错误码为:" + reply); |
|
||||||
} |
|
||||||
} catch (IOException e) { |
|
||||||
Log.d(TAG, e.getMessage()); |
|
||||||
} finally { |
|
||||||
synchronized (FtpClientHelp.this) { |
|
||||||
FtpClientHelp.this.notify(); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
}).start(); |
|
||||||
synchronized (FtpClientHelp.this) { |
|
||||||
try { |
|
||||||
wait(); |
|
||||||
} catch (InterruptedException e) { |
|
||||||
e.printStackTrace(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return client; |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* 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.inf; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/9/5. |
||||||
|
*/ |
||||||
|
public abstract class BaseGroupTaskEntity<ENTITY extends AbsGroupEntity> extends AbsTaskEntity<ENTITY>{ |
||||||
|
@Override public ENTITY getEntity() { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
/* |
||||||
|
* 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.inf; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/9/8. |
||||||
|
* 任务组参数传递 |
||||||
|
*/ |
||||||
|
public class GroupSendParams<GROUP_TASK extends AbsGroupTask, ENTITY extends AbsNormalEntity> { |
||||||
|
|
||||||
|
public GROUP_TASK groupTask; |
||||||
|
public ENTITY entity; |
||||||
|
|
||||||
|
public GroupSendParams() { |
||||||
|
} |
||||||
|
|
||||||
|
public GroupSendParams(GROUP_TASK groupTask, ENTITY entity) { |
||||||
|
this.groupTask = groupTask; |
||||||
|
this.entity = entity; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,127 @@ |
|||||||
|
/* |
||||||
|
* 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.squareup.javapoet.ClassName; |
||||||
|
import com.squareup.javapoet.CodeBlock; |
||||||
|
import com.squareup.javapoet.FieldSpec; |
||||||
|
import com.squareup.javapoet.JavaFile; |
||||||
|
import com.squareup.javapoet.MethodSpec; |
||||||
|
import com.squareup.javapoet.ParameterizedTypeName; |
||||||
|
import com.squareup.javapoet.TypeSpec; |
||||||
|
import java.io.IOException; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
import javax.annotation.processing.Filer; |
||||||
|
import javax.lang.model.element.Modifier; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/9/6. |
||||||
|
* 各类注解统计技术类 |
||||||
|
*/ |
||||||
|
final class CountFiler { |
||||||
|
private Filer mFiler; |
||||||
|
private ParamObtainUtil mPbUtil; |
||||||
|
|
||||||
|
CountFiler(Filer filer, ParamObtainUtil pbUtil) { |
||||||
|
mFiler = filer; |
||||||
|
mPbUtil = pbUtil; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 每一种注解对应的统计类 |
||||||
|
*/ |
||||||
|
void createCountFile() throws IOException { |
||||||
|
Set<String> keys = mPbUtil.getListenerClass().keySet(); |
||||||
|
if (keys.size() == 0) return; |
||||||
|
TypeSpec.Builder builder = TypeSpec.classBuilder(ProxyConstance.PROXY_COUNTER_NAME) |
||||||
|
.addModifiers(Modifier.PUBLIC, Modifier.FINAL); |
||||||
|
|
||||||
|
FieldSpec mappingField = FieldSpec.builder( |
||||||
|
ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), |
||||||
|
ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.get(String.class))), |
||||||
|
ProxyConstance.PROXY_COUNTER_MAP) |
||||||
|
.addModifiers(Modifier.PRIVATE) |
||||||
|
.initializer("new $T()", HashMap.class) |
||||||
|
.build(); |
||||||
|
builder.addField(mappingField); |
||||||
|
|
||||||
|
//增加构造函数
|
||||||
|
CodeBlock.Builder cb = CodeBlock.builder(); |
||||||
|
cb.add("Set<String> set = null;\n"); |
||||||
|
for (String key : keys) { |
||||||
|
addTypeData(key, mPbUtil.getListenerClass().get(key), cb); |
||||||
|
} |
||||||
|
MethodSpec structure = |
||||||
|
MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addCode(cb.build()).build(); |
||||||
|
builder.addMethod(structure); |
||||||
|
|
||||||
|
builder.addMethod( |
||||||
|
createMethod(ProxyConstance.COUNT_METHOD_DOWNLOAD, ProxyConstance.COUNT_DOWNLOAD)); |
||||||
|
builder.addMethod( |
||||||
|
createMethod(ProxyConstance.COUNT_METHOD_UPLOAD, ProxyConstance.COUNT_UPLOAD)); |
||||||
|
builder.addMethod(createMethod(ProxyConstance.COUNT_METHOD_DOWNLOAD_GROUP, |
||||||
|
ProxyConstance.COUNT_DOWNLOAD_GROUP)); |
||||||
|
builder.addMethod(createMethod(ProxyConstance.COUNT_METHOD_DOWNLOAD_GROUP_SUB, |
||||||
|
ProxyConstance.COUNT_DOWNLOAD_GROUP_SUB)); |
||||||
|
|
||||||
|
JavaFile jf = JavaFile.builder(ProxyConstance.PROXY_COUNTER_PACKAGE, builder.build()).build(); |
||||||
|
createFile(jf); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建不同任务类型的代理类集合 |
||||||
|
* |
||||||
|
* @param key {@link ParamObtainUtil#addListenerMapping(String, String)} |
||||||
|
*/ |
||||||
|
private MethodSpec createMethod(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 ParamObtainUtil#addListenerMapping(String, String)} |
||||||
|
*/ |
||||||
|
private void addTypeData(String type, Set<String> clsNames, CodeBlock.Builder cb) { |
||||||
|
if (clsNames == null || clsNames.isEmpty()) return; |
||||||
|
StringBuilder sb = new StringBuilder(); |
||||||
|
sb.append("set = new $T();\n"); |
||||||
|
for (String clsName : clsNames) { |
||||||
|
sb.append("set.add(\"").append(clsName).append("\");\n"); |
||||||
|
} |
||||||
|
sb.append("typeMapping.put(\"").append(type).append("\", ").append("set);\n"); |
||||||
|
cb.add(sb.toString(), ClassName.get(HashSet.class)); |
||||||
|
} |
||||||
|
|
||||||
|
private void createFile(JavaFile jf) throws IOException { |
||||||
|
//jf.writeTo(System.out);
|
||||||
|
if (ProxyConstance.DEBUG) { |
||||||
|
// 如果需要在控制台打印生成的文件,则去掉下面的注释
|
||||||
|
jf.writeTo(System.out); |
||||||
|
} else { |
||||||
|
jf.writeTo(mFiler); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,231 @@ |
|||||||
|
/* |
||||||
|
* 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.CodeBlock; |
||||||
|
import com.squareup.javapoet.FieldSpec; |
||||||
|
import com.squareup.javapoet.JavaFile; |
||||||
|
import com.squareup.javapoet.MethodSpec; |
||||||
|
import com.squareup.javapoet.ParameterSpec; |
||||||
|
import com.squareup.javapoet.ParameterizedTypeName; |
||||||
|
import com.squareup.javapoet.TypeSpec; |
||||||
|
import java.io.IOException; |
||||||
|
import java.lang.annotation.Annotation; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
import javax.annotation.processing.Filer; |
||||||
|
import javax.lang.model.element.Modifier; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/9/6. |
||||||
|
* 任务事件代理文件 |
||||||
|
* |
||||||
|
* <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> |
||||||
|
*/ |
||||||
|
final class EventProxyFiler { |
||||||
|
|
||||||
|
private Filer mFiler; |
||||||
|
private ParamObtainUtil mPbUtil; |
||||||
|
|
||||||
|
EventProxyFiler(Filer filer, ParamObtainUtil pbUtil) { |
||||||
|
mFiler = filer; |
||||||
|
mPbUtil = pbUtil; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建任务事件代理文件 |
||||||
|
*/ |
||||||
|
void createEventProxyFile() throws IOException { |
||||||
|
Set<String> keys = mPbUtil.getMethodParams().keySet(); |
||||||
|
for (String key : keys) { |
||||||
|
ProxyClassParam entity = mPbUtil.getMethodParams().get(key); |
||||||
|
JavaFile jf = JavaFile.builder(entity.packageName, createProxyClass(entity)).build(); |
||||||
|
createFile(jf); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建代理方法 |
||||||
|
* |
||||||
|
* @param taskEnum 任务类型枚举{@link TaskEnum} |
||||||
|
* @param annotation {@link Download}、{@link Upload} |
||||||
|
* @param methodName 被代理类注解的方法名 |
||||||
|
*/ |
||||||
|
private MethodSpec createProxyMethod(TaskEnum taskEnum, Class<? extends Annotation> annotation, |
||||||
|
String methodName) { |
||||||
|
ClassName task = ClassName.get(taskEnum.getPkg(), taskEnum.getClassName()); |
||||||
|
|
||||||
|
ParameterSpec taskParam = |
||||||
|
ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build(); |
||||||
|
|
||||||
|
String callCode; |
||||||
|
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) { |
||||||
|
callCode = "task, subEntity"; |
||||||
|
} else { |
||||||
|
callCode = "task"; |
||||||
|
} |
||||||
|
StringBuilder sb = new StringBuilder(); |
||||||
|
sb.append("Set<String> keys = keyMapping.get(\"").append(methodName).append("\");\n"); |
||||||
|
sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n") |
||||||
|
.append("\t\tobj.") |
||||||
|
.append(methodName) |
||||||
|
.append("((") |
||||||
|
.append(taskEnum.getClassName()) |
||||||
|
.append(")") |
||||||
|
.append(callCode) |
||||||
|
.append(");\n\t}\n} else {\n") |
||||||
|
.append("\tobj.") |
||||||
|
.append(methodName) |
||||||
|
.append("((") |
||||||
|
.append(taskEnum.getClassName()) |
||||||
|
.append(")") |
||||||
|
.append(callCode) |
||||||
|
.append(");\n}\n"); |
||||||
|
|
||||||
|
MethodSpec.Builder builder = MethodSpec.methodBuilder(annotation.getSimpleName()) |
||||||
|
.addModifiers(Modifier.PUBLIC) |
||||||
|
.returns(void.class) |
||||||
|
.addParameter(taskParam) |
||||||
|
.addAnnotation(Override.class) |
||||||
|
.addCode(sb.toString()); |
||||||
|
|
||||||
|
//任务组接口
|
||||||
|
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) { |
||||||
|
ClassName subTask = ClassName.get(TaskEnum.DOWNLOAD_ENTITY.pkg, TaskEnum.DOWNLOAD_ENTITY.className); |
||||||
|
ParameterSpec subTaskParam = |
||||||
|
ParameterSpec.builder(subTask, "subEntity").addModifiers(Modifier.FINAL).build(); |
||||||
|
|
||||||
|
builder.addParameter(subTaskParam); |
||||||
|
} |
||||||
|
return builder.build(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建代理类 |
||||||
|
*/ |
||||||
|
private TypeSpec createProxyClass(ProxyClassParam entity) { |
||||||
|
TypeSpec.Builder builder = |
||||||
|
TypeSpec.classBuilder(entity.proxyClassName).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); |
||||||
|
|
||||||
|
//添加url映射表
|
||||||
|
FieldSpec mappingField = FieldSpec.builder( |
||||||
|
ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), |
||||||
|
ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.get(String.class))), |
||||||
|
"keyMapping").addModifiers(Modifier.PRIVATE).initializer("new $T()", HashMap.class).build(); |
||||||
|
builder.addField(mappingField); |
||||||
|
|
||||||
|
//添加注解方法
|
||||||
|
for (TaskEnum te : entity.methods.keySet()) { |
||||||
|
Map<Class<? extends Annotation>, String> temp = entity.methods.get(te); |
||||||
|
if (temp != null) { |
||||||
|
for (Class<? extends Annotation> annotation : temp.keySet()) { |
||||||
|
MethodSpec method = createProxyMethod(te, annotation, temp.get(annotation)); |
||||||
|
builder.addMethod(method); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
//增加构造函数
|
||||||
|
CodeBlock.Builder cb = CodeBlock.builder(); |
||||||
|
cb.add("Set<String> set = null;\n"); |
||||||
|
for (String methodName : entity.keyMappings.keySet()) { |
||||||
|
//PrintLog.getInstance().info("methodName ====> " + methodName);
|
||||||
|
Set<String> keys = entity.keyMappings.get(methodName); |
||||||
|
if (keys == null || keys.size() == 0) continue; |
||||||
|
StringBuilder sb = new StringBuilder(); |
||||||
|
sb.append("set = new $T();\n"); |
||||||
|
for (String key : keys) { |
||||||
|
if (key.isEmpty()) continue; |
||||||
|
sb.append("set.add(\"").append(key).append("\");\n"); |
||||||
|
} |
||||||
|
|
||||||
|
sb.append("keyMapping.put(\"").append(methodName).append("\", ").append("set);\n"); |
||||||
|
cb.add(sb.toString(), ClassName.get(HashSet.class)); |
||||||
|
} |
||||||
|
MethodSpec structure = |
||||||
|
MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addCode(cb.build()).build(); |
||||||
|
builder.addMethod(structure); |
||||||
|
|
||||||
|
//添加设置代理的类
|
||||||
|
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 taskTypeVariable = |
||||||
|
ClassName.get(entity.mainTaskEnum.pkg, entity.mainTaskEnum.className); |
||||||
|
//子任务泛型参数
|
||||||
|
ClassName subTaskTypeVariable = |
||||||
|
ClassName.get(entity.subTaskEnum.pkg, entity.subTaskEnum.className); |
||||||
|
|
||||||
|
builder.superclass( |
||||||
|
ParameterizedTypeName.get(superClass, taskTypeVariable, subTaskTypeVariable)); |
||||||
|
builder.addMethod(listener); |
||||||
|
return builder.build(); |
||||||
|
} |
||||||
|
|
||||||
|
private void createFile(JavaFile jf) throws IOException { |
||||||
|
if (ProxyConstance.DEBUG) { |
||||||
|
// 如果需要在控制台打印生成的文件,则去掉下面的注释
|
||||||
|
jf.writeTo(System.out); |
||||||
|
} else { |
||||||
|
jf.writeTo(mFiler); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,221 @@ |
|||||||
|
/* |
||||||
|
* 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.Collections; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
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 Aria.Lao on 2017/9/6. |
||||||
|
* 构建代理文件的参数获取工具 |
||||||
|
*/ |
||||||
|
class ParamObtainUtil { |
||||||
|
private Map<String, ProxyClassParam> mMethodParams = new HashMap<>(); |
||||||
|
private Map<String, Set<String>> mListenerClass = new HashMap<>(); |
||||||
|
private Elements mElementUtil; |
||||||
|
|
||||||
|
ParamObtainUtil(Elements elements) { |
||||||
|
mElementUtil = elements; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取需要创建的代理类参数 |
||||||
|
*/ |
||||||
|
Map<String, Set<String>> getListenerClass() { |
||||||
|
return mListenerClass; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取搜索到的代理方法参数 |
||||||
|
*/ |
||||||
|
Map<String, ProxyClassParam> getMethodParams() { |
||||||
|
return mMethodParams; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 查找并保存扫描到的方法 |
||||||
|
*/ |
||||||
|
void saveMethod(TaskEnum taskEnum, RoundEnvironment roundEnv, |
||||||
|
Class<? extends Annotation> annotationClazz, int annotationType) { |
||||||
|
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(taskEnum, method); |
||||||
|
String methodName = method.getSimpleName().toString(); |
||||||
|
String className = method.getEnclosingElement().toString(); //全类名\
|
||||||
|
String key = className + taskEnum.proxySuffix; |
||||||
|
ProxyClassParam proxyEntity = mMethodParams.get(key); |
||||||
|
|
||||||
|
if (proxyEntity == null) { |
||||||
|
proxyEntity = new ProxyClassParam(); |
||||||
|
proxyEntity.taskEnums = new HashSet<>(); |
||||||
|
proxyEntity.packageName = packageElement.getQualifiedName().toString(); |
||||||
|
proxyEntity.className = classElement.getSimpleName().toString(); |
||||||
|
proxyEntity.proxyClassName = proxyEntity.className + taskEnum.proxySuffix; |
||||||
|
proxyEntity.mainTaskEnum = taskEnum; |
||||||
|
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB || taskEnum == TaskEnum.DOWNLOAD_GROUP) { |
||||||
|
proxyEntity.subTaskEnum = TaskEnum.DOWNLOAD_ENTITY; |
||||||
|
} |
||||||
|
mMethodParams.put(key, proxyEntity); |
||||||
|
} |
||||||
|
proxyEntity.taskEnums.add(taskEnum); |
||||||
|
if (proxyEntity.methods.get(taskEnum) == null) { |
||||||
|
proxyEntity.methods.put(taskEnum, new HashMap<Class<? extends Annotation>, String>()); |
||||||
|
} |
||||||
|
proxyEntity.methods.get(taskEnum).put(annotationClazz, methodName); |
||||||
|
proxyEntity.keyMappings.put(methodName, getValues(taskEnum, method, annotationType)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取注解的内容 |
||||||
|
*/ |
||||||
|
private Set<String> getValues(TaskEnum taskEnum, ExecutableElement method, int annotationType) { |
||||||
|
String clsName = method.getEnclosingElement().toString(); |
||||||
|
String[] keys = null; |
||||||
|
switch (taskEnum) { |
||||||
|
case DOWNLOAD: |
||||||
|
keys = ValuesUtil.getDownloadValues(method, annotationType); |
||||||
|
addListenerMapping(clsName, ProxyConstance.COUNT_DOWNLOAD); |
||||||
|
break; |
||||||
|
case UPLOAD: |
||||||
|
keys = ValuesUtil.getUploadValues(method, annotationType); |
||||||
|
addListenerMapping(clsName, ProxyConstance.COUNT_UPLOAD); |
||||||
|
break; |
||||||
|
case DOWNLOAD_GROUP: |
||||||
|
keys = ValuesUtil.getDownloadGroupValues(method, annotationType); |
||||||
|
addListenerMapping(clsName, ProxyConstance.COUNT_DOWNLOAD_GROUP); |
||||||
|
break; |
||||||
|
case DOWNLOAD_GROUP_SUB: |
||||||
|
keys = ValuesUtil.getDownloadGroupSubValues(method, annotationType); |
||||||
|
addListenerMapping(clsName, ProxyConstance.COUNT_DOWNLOAD_GROUP_SUB); |
||||||
|
break; |
||||||
|
} |
||||||
|
return keys == null ? null : convertSet(keys); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 添加方法映射 |
||||||
|
* |
||||||
|
* @param clsName 注解事件的类 |
||||||
|
* @param key {@link ProxyConstance#COUNT_DOWNLOAD}、{@link ProxyConstance#COUNT_UPLOAD}、{@link |
||||||
|
* ProxyConstance#COUNT_DOWNLOAD_GROUP}、{@link ProxyConstance#COUNT_DOWNLOAD_GROUP_SUB} |
||||||
|
*/ |
||||||
|
void addListenerMapping(String clsName, String key) { |
||||||
|
Set<String> cls = mListenerClass.get(key); |
||||||
|
if (cls == null) { |
||||||
|
cls = new HashSet<>(); |
||||||
|
mListenerClass.put(key, cls); |
||||||
|
} |
||||||
|
cls.add(clsName); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查和下载相关的方法,如果被注解的方法为private或参数不合法,则抛异常 |
||||||
|
*/ |
||||||
|
private void checkDownloadMethod(TaskEnum taskEnum, 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 (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) { |
||||||
|
if (params.size() != 2) { |
||||||
|
throw new IllegalArgumentException(className |
||||||
|
+ "." |
||||||
|
+ methodName |
||||||
|
+ "参数错误, 参数只有两个,且第一个参数必须是" |
||||||
|
+ getCheckParams(taskEnum) |
||||||
|
+ ",第二个参数必须是" |
||||||
|
+ getCheckSubParams(taskEnum)); |
||||||
|
} |
||||||
|
} else { |
||||||
|
if (params.size() != 1) { |
||||||
|
throw new IllegalArgumentException( |
||||||
|
className + "." + methodName + "参数错误, 参数只能有一个,且参数必须是" + getCheckParams(taskEnum)); |
||||||
|
} |
||||||
|
} |
||||||
|
if (!params.get(0).asType().toString().equals(getCheckParams(taskEnum))) { |
||||||
|
throw new IllegalArgumentException(className |
||||||
|
+ "." |
||||||
|
+ methodName |
||||||
|
+ "参数【" |
||||||
|
+ params.get(0).getSimpleName() |
||||||
|
+ "】类型错误,参数必须是" |
||||||
|
+ getCheckParams(taskEnum)); |
||||||
|
} |
||||||
|
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) { |
||||||
|
if (!params.get(1).asType().toString().equals(getCheckSubParams(taskEnum))) { |
||||||
|
throw new IllegalArgumentException(className |
||||||
|
+ "." |
||||||
|
+ methodName |
||||||
|
+ "参数【" |
||||||
|
+ params.get(0).getSimpleName() |
||||||
|
+ "】类型错误,参数必须是" |
||||||
|
+ getCheckSubParams(taskEnum)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 字符串数组转set |
||||||
|
* |
||||||
|
* @param keys 注解中查到的key |
||||||
|
*/ |
||||||
|
private Set<String> convertSet(final String[] keys) { |
||||||
|
if (keys == null || keys.length == 0) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
if (keys[0].isEmpty()) return null; |
||||||
|
Set<String> set = new HashSet<>(); |
||||||
|
Collections.addAll(set, keys); |
||||||
|
return set; |
||||||
|
} |
||||||
|
|
||||||
|
private String getCheckParams(TaskEnum taskEnum) { |
||||||
|
return taskEnum.pkg + "." + taskEnum.className; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 检查任务组子任务第二个参数 |
||||||
|
*/ |
||||||
|
private String getCheckSubParams(TaskEnum taskEnum) { |
||||||
|
if (taskEnum == TaskEnum.DOWNLOAD_GROUP_SUB) { |
||||||
|
return TaskEnum.DOWNLOAD_ENTITY.pkg + "." + TaskEnum.DOWNLOAD_ENTITY.className; |
||||||
|
} |
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,175 @@ |
|||||||
|
/* |
||||||
|
* 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.DownloadGroup; |
||||||
|
import com.arialyy.annotations.Upload; |
||||||
|
import javax.lang.model.element.ExecutableElement; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/9/6. |
||||||
|
* 获取注解value工具 |
||||||
|
*/ |
||||||
|
final class ValuesUtil { |
||||||
|
/** |
||||||
|
* 获取下载任务组子任务的的注解数据 |
||||||
|
*/ |
||||||
|
static String[] getDownloadGroupSubValues(ExecutableElement method, int annotationType) { |
||||||
|
String[] values = null; |
||||||
|
switch (annotationType) { |
||||||
|
case ProxyConstance.TASK_PRE: |
||||||
|
values = method.getAnnotation(DownloadGroup.onSubTaskPre.class).value(); |
||||||
|
break; |
||||||
|
case ProxyConstance.TASK_START: |
||||||
|
values = method.getAnnotation(DownloadGroup.onSubTaskStart.class).value(); |
||||||
|
break; |
||||||
|
case ProxyConstance.TASK_RUNNING: |
||||||
|
values = method.getAnnotation(DownloadGroup.onSubTaskRunning.class).value(); |
||||||
|
break; |
||||||
|
case ProxyConstance.TASK_STOP: |
||||||
|
values = method.getAnnotation(DownloadGroup.onSubTaskStop.class).value(); |
||||||
|
break; |
||||||
|
case ProxyConstance.TASK_COMPLETE: |
||||||
|
values = method.getAnnotation(DownloadGroup.onSubTaskComplete.class).value(); |
||||||
|
break; |
||||||
|
case ProxyConstance.TASK_CANCEL: |
||||||
|
//values = method.getAnnotation(DownloadGroup.onSubTaskCancel.class).value();
|
||||||
|
break; |
||||||
|
case ProxyConstance.TASK_FAIL: |
||||||
|
values = method.getAnnotation(DownloadGroup.onSubTaskFail.class).value(); |
||||||
|
break; |
||||||
|
} |
||||||
|
return values; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取下载任务组的注解数据 |
||||||
|
*/ |
||||||
|
static 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; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取上传的注解数据 |
||||||
|
*/ |
||||||
|
static 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; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取下载的注解数据 |
||||||
|
*/ |
||||||
|
static 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; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,149 @@ |
|||||||
|
/* |
||||||
|
* 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.download.group; |
||||||
|
|
||||||
|
import android.annotation.SuppressLint; |
||||||
|
import android.content.Context; |
||||||
|
import android.os.Bundle; |
||||||
|
import android.view.Gravity; |
||||||
|
import android.view.View; |
||||||
|
import android.view.ViewGroup; |
||||||
|
import android.view.Window; |
||||||
|
import android.view.WindowManager; |
||||||
|
import android.widget.TextView; |
||||||
|
import butterknife.Bind; |
||||||
|
import butterknife.OnClick; |
||||||
|
import com.arialyy.annotations.DownloadGroup; |
||||||
|
import com.arialyy.aria.core.Aria; |
||||||
|
import com.arialyy.aria.core.download.DownloadEntity; |
||||||
|
import com.arialyy.aria.core.download.DownloadGroupTask; |
||||||
|
import com.arialyy.frame.util.show.L; |
||||||
|
import com.arialyy.simple.R; |
||||||
|
import com.arialyy.simple.base.BaseDialog; |
||||||
|
import com.arialyy.simple.databinding.DialogSubTaskHandlerBinding; |
||||||
|
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Aria.Lao on 2017/9/5. |
||||||
|
*/ |
||||||
|
@SuppressLint("ValidFragment") public class ChildHandleDialog |
||||||
|
extends BaseDialog<DialogSubTaskHandlerBinding> { |
||||||
|
@Bind(R.id.sub_task) TextView mSub; |
||||||
|
@Bind(R.id.task_group) TextView mGroup; |
||||||
|
@Bind(R.id.pb) HorizontalProgressBarWithNumber mPb; |
||||||
|
private String mGroupName; |
||||||
|
private String mChildName; |
||||||
|
private List<String> mUrls; |
||||||
|
private DownloadEntity mChildEntity; |
||||||
|
|
||||||
|
public ChildHandleDialog(Context context, List<String> urls, DownloadEntity childEntity) { |
||||||
|
super(context); |
||||||
|
setStyle(STYLE_NO_TITLE, R.style.Theme_Light_Dialog); |
||||||
|
mChildEntity = childEntity; |
||||||
|
mGroupName = "任务组测试"; |
||||||
|
mUrls = urls; |
||||||
|
mChildName = childEntity.getFileName(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override protected void init(Bundle savedInstanceState) { |
||||||
|
super.init(savedInstanceState); |
||||||
|
Aria.download(this).register(); |
||||||
|
initWidget(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onDestroy() { |
||||||
|
super.onDestroy(); |
||||||
|
Aria.download(this).unRegister(); |
||||||
|
} |
||||||
|
|
||||||
|
private void initWidget() { |
||||||
|
mGroup.setText("任务组:" + mGroupName); |
||||||
|
mSub.setText("子任务:" + mChildName); |
||||||
|
mPb.setProgress((int) (mChildEntity.getCurrentProgress() * 100 / mChildEntity.getFileSize())); |
||||||
|
|
||||||
|
Window window = getDialog().getWindow(); |
||||||
|
window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM); |
||||||
|
WindowManager.LayoutParams p = window.getAttributes(); |
||||||
|
p.width = ViewGroup.LayoutParams.MATCH_PARENT; |
||||||
|
window.setAttributes(p); |
||||||
|
window.setWindowAnimations(R.style.dialogStyle); |
||||||
|
} |
||||||
|
|
||||||
|
@DownloadGroup.onSubTaskRunning void onSubTaskRunning(DownloadGroupTask groupTask, |
||||||
|
DownloadEntity subEntity) { |
||||||
|
if (!subEntity.getUrl().equals(mChildEntity.getUrl())) return; |
||||||
|
mPb.setProgress(subEntity.getPercent()); |
||||||
|
} |
||||||
|
|
||||||
|
@DownloadGroup.onSubTaskPre void onSubTaskPre(DownloadGroupTask groupTask, |
||||||
|
DownloadEntity subEntity) { |
||||||
|
if (!subEntity.getUrl().equals(mChildEntity.getUrl())) return; |
||||||
|
L.d(TAG, subEntity.getPercent() + ""); |
||||||
|
} |
||||||
|
|
||||||
|
@DownloadGroup.onSubTaskStop void onSubTaskStop(DownloadGroupTask groupTask, |
||||||
|
DownloadEntity subEntity) { |
||||||
|
if (!subEntity.getUrl().equals(mChildEntity.getUrl())) return; |
||||||
|
mSub.setText("子任务:" + mChildName + ",状态:任务停止"); |
||||||
|
} |
||||||
|
|
||||||
|
@DownloadGroup.onSubTaskStart void onSubTaskStart(DownloadGroupTask groupTask, |
||||||
|
DownloadEntity subEntity) { |
||||||
|
if (!subEntity.getUrl().equals(mChildEntity.getUrl())) return; |
||||||
|
mSub.setText("子任务:" + mChildName + ",状态:下载中"); |
||||||
|
} |
||||||
|
|
||||||
|
//@DownloadGroup.onSubTaskCancel void onSubTaskCancel(DownloadGroupTask groupTask,
|
||||||
|
// DownloadEntity subEntity) {
|
||||||
|
// mSub.setText("子任务:" + mChildName + ",状态:取消下载");
|
||||||
|
//}
|
||||||
|
|
||||||
|
@DownloadGroup.onSubTaskComplete void onSubTaskComplete(DownloadGroupTask groupTask, |
||||||
|
DownloadEntity subEntity) { |
||||||
|
if (!subEntity.getUrl().equals(mChildEntity.getUrl())) return; |
||||||
|
mSub.setText("子任务:" + mChildName + ",状态:任务完成"); |
||||||
|
mPb.setProgress(100); |
||||||
|
} |
||||||
|
|
||||||
|
@DownloadGroup.onSubTaskFail void onSubTaskFail(DownloadGroupTask groupTask, |
||||||
|
DownloadEntity subEntity) { |
||||||
|
if (!subEntity.getUrl().equals(mChildEntity.getUrl())) return; |
||||||
|
L.d(TAG, subEntity.getPercent() + ""); |
||||||
|
} |
||||||
|
|
||||||
|
@Override protected int setLayoutId() { |
||||||
|
return R.layout.dialog_sub_task_handler; |
||||||
|
} |
||||||
|
|
||||||
|
@OnClick({ R.id.start, R.id.stop }) void onClick(View view) { |
||||||
|
switch (view.getId()) { |
||||||
|
case R.id.start: |
||||||
|
Aria.download(this).load(mUrls).getSubTaskManager().startSubTask(mChildEntity.getUrl()); |
||||||
|
break; |
||||||
|
case R.id.stop: |
||||||
|
Aria.download(this).load(mUrls).getSubTaskManager().stopSubTask(mChildEntity.getUrl()); |
||||||
|
break; |
||||||
|
//case R.id.cancel:
|
||||||
|
// Aria.download(this).load(mUrls).getSubTaskManager().cancelSubTask(mChildEntity.getUrl());
|
||||||
|
// break;
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override protected void dataCallback(int result, Object obj) { |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,5 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<set xmlns:android="http://schemas.android.com/apk/res/android"> |
||||||
|
<translate android:fromYDelta="100%" |
||||||
|
android:duration="100"/> |
||||||
|
</set> |
@ -0,0 +1,6 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<set xmlns:android="http://schemas.android.com/apk/res/android"> |
||||||
|
<translate |
||||||
|
android:duration="100" |
||||||
|
android:toYDelta="100%"/> |
||||||
|
</set> |
@ -0,0 +1,76 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<layout xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
|
|
||||||
|
> |
||||||
|
|
||||||
|
<RelativeLayout |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:background="@color/white" |
||||||
|
android:padding="16dp" |
||||||
|
> |
||||||
|
|
||||||
|
|
||||||
|
<TextView |
||||||
|
android:id="@+id/task_group" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:text="任务组:" |
||||||
|
/> |
||||||
|
|
||||||
|
<TextView |
||||||
|
android:id="@+id/sub_task" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_below="@+id/task_group" |
||||||
|
android:layout_marginTop="5dp" |
||||||
|
android:text="子任务:" |
||||||
|
/> |
||||||
|
|
||||||
|
<com.arialyy.simple.widget.HorizontalProgressBarWithNumber |
||||||
|
android:id="@+id/pb" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_below="@+id/sub_task" |
||||||
|
android:layout_marginTop="10dp" |
||||||
|
android:max="100" |
||||||
|
/> |
||||||
|
|
||||||
|
<LinearLayout |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_below="@+id/pb" |
||||||
|
android:orientation="horizontal" |
||||||
|
> |
||||||
|
|
||||||
|
<Button |
||||||
|
android:id="@+id/start" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_weight="1" |
||||||
|
android:text="开始" |
||||||
|
style="?buttonBarButtonStyle" |
||||||
|
/> |
||||||
|
|
||||||
|
<Button |
||||||
|
android:id="@+id/stop" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_weight="1" |
||||||
|
android:text="停止" |
||||||
|
style="?buttonBarButtonStyle" |
||||||
|
/> |
||||||
|
|
||||||
|
<!--<Button--> |
||||||
|
<!--android:id="@+id/cancel"--> |
||||||
|
<!--android:layout_width="wrap_content"--> |
||||||
|
<!--android:layout_height="wrap_content"--> |
||||||
|
<!--android:layout_weight="1"--> |
||||||
|
<!--android:text="删除"--> |
||||||
|
<!--style="?buttonBarButtonStyle"--> |
||||||
|
<!--/>--> |
||||||
|
|
||||||
|
</LinearLayout> |
||||||
|
</RelativeLayout> |
||||||
|
|
||||||
|
</layout> |
Loading…
Reference in new issue