merge v_3.0
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'bintray-release'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
@ -7,8 +8,8 @@ android {
|
||||
defaultConfig {
|
||||
minSdkVersion 9
|
||||
targetSdkVersion 23
|
||||
versionCode 315
|
||||
versionName "3.1.5"
|
||||
versionCode 317
|
||||
versionName "3.1.7"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
@ -19,8 +20,57 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compile fileTree(include: ['*.jar'], dir: 'libs')
|
||||
testCompile 'junit:junit:4.12'
|
||||
compile 'com.android.support:appcompat-v7:23.1.1'
|
||||
// compile project(':AriaCompiler')
|
||||
compile project(':AriaAnnotations')
|
||||
}
|
||||
apply from: 'jcenter.gradle'
|
||||
//apply from: 'jcenter.gradle'
|
||||
|
||||
// Jar
|
||||
//task androidJar(type: Jar) {
|
||||
// dependsOn assemble
|
||||
// group 'Build'
|
||||
// description 'blah blah'
|
||||
// from zipTree(
|
||||
// 'build/intermediates/bundles/release/classes.jar')
|
||||
// from zipTree(
|
||||
// '../AriaCompiler/build/libs/AriaCompiler.jar')
|
||||
// from zipTree(
|
||||
// '../AriaAnnotation/build/libs/AriaAnnotation.jar')
|
||||
//
|
||||
//}
|
||||
//
|
||||
//
|
||||
//// javadoc tasks
|
||||
//android.libraryVariants.all { variant ->
|
||||
// task("javadoc${variant.name.capitalize()}", type: Javadoc) {
|
||||
// description "Generates Javadoc for $variant.name."
|
||||
// group 'Docs'
|
||||
// source = variant.javaCompile.source
|
||||
// source "../AriaAnnotation/src/main/java"
|
||||
//
|
||||
// exclude '**/BuildConfig.java'
|
||||
// exclude '**/R.java'
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
publish {
|
||||
artifactId = 'aria-core'
|
||||
userOrg = rootProject.userOrg
|
||||
groupId = rootProject.groupId
|
||||
// uploadName = rootProject.uploadName
|
||||
uploadName = 'AriaApi'
|
||||
publishVersion = rootProject.publishVersion
|
||||
description = rootProject.description
|
||||
website = rootProject.website
|
||||
licences = rootProject.licences
|
||||
}
|
||||
|
||||
//task copyJar(type: Copy) {
|
||||
// from('build/libs/permission-lib.jar')
|
||||
// into('../output/')
|
||||
// rename ('permission-lib.jar', 'mpermissions.jar')
|
||||
//}
|
||||
|
@ -26,6 +26,8 @@ import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.widget.PopupWindow;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.annotations.Upload;
|
||||
import com.arialyy.aria.core.download.DownloadReceiver;
|
||||
import com.arialyy.aria.core.scheduler.IDownloadSchedulerListener;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
||||
@ -171,8 +173,12 @@ import com.arialyy.aria.core.upload.UploadTask;
|
||||
|
||||
/**
|
||||
* 上传任务状态监听
|
||||
*
|
||||
* @see Upload
|
||||
* @deprecated 请使用注解函数的方式来实现事件的获取
|
||||
*/
|
||||
public static class UploadSchedulerListener implements ISchedulerListener<UploadTask> {
|
||||
@Deprecated public static class UploadSchedulerListener
|
||||
implements ISchedulerListener<UploadTask> {
|
||||
|
||||
/**
|
||||
* 预处理,有时有些地址链接比较慢,这时可以先在这个地方出来一些界面上的UI,如按钮的状态。
|
||||
@ -218,8 +224,11 @@ import com.arialyy.aria.core.upload.UploadTask;
|
||||
|
||||
/**
|
||||
* 下载任务状态监听
|
||||
*
|
||||
* @see Download
|
||||
* @deprecated 请使用注解函数的方式来实现事件的获取
|
||||
*/
|
||||
public static class DownloadSchedulerListener
|
||||
@Deprecated public static class DownloadSchedulerListener
|
||||
implements IDownloadSchedulerListener<DownloadTask> {
|
||||
/**
|
||||
* 预处理,有时有些地址链接比较慢,这时可以先在这个地方出来一些界面上的UI,如按钮的状态。
|
||||
|
@ -176,11 +176,13 @@ import org.xml.sax.SAXException;
|
||||
if (isDownload) {
|
||||
DownloadReceiver dReceiver = new DownloadReceiver();
|
||||
dReceiver.targetName = obj.getClass().getName();
|
||||
dReceiver.obj = obj;
|
||||
mReceivers.put(key, dReceiver);
|
||||
receiver = dReceiver;
|
||||
} else {
|
||||
UploadReceiver uReceiver = new UploadReceiver();
|
||||
uReceiver.targetName = obj.getClass().getName();
|
||||
uReceiver.obj = obj;
|
||||
mReceivers.put(key, uReceiver);
|
||||
receiver = uReceiver;
|
||||
}
|
||||
@ -303,6 +305,7 @@ import org.xml.sax.SAXException;
|
||||
if (key.contains(clsName)) {
|
||||
IReceiver receiver = mReceivers.get(key);
|
||||
receiver.removeSchedulerListener();
|
||||
receiver.unRegister();
|
||||
receiver.destroy();
|
||||
iter.remove();
|
||||
break;
|
||||
|
@ -83,10 +83,23 @@ class ConfigHelper extends DefaultHandler {
|
||||
case "convertSpeed":
|
||||
loadConvertSpeed(value);
|
||||
break;
|
||||
case "maxSpeed":
|
||||
loadMaxSpeed(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadMaxSpeed(String value) {
|
||||
double maxSpeed = 0.0;
|
||||
if (!TextUtils.isEmpty(value)) {
|
||||
maxSpeed = Double.parseDouble(value);
|
||||
}
|
||||
if (isDownloadConfig){
|
||||
mDownloadConfig.msxSpeed = maxSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConvertSpeed(String value) {
|
||||
boolean open = Boolean.parseBoolean(value);
|
||||
if (isDownloadConfig) {
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.arialyy.aria.core;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
@ -171,6 +172,9 @@ class Configuration {
|
||||
} else if (type == float.class || type == Float.class) {
|
||||
field.setFloat(this, Float.parseFloat(value));
|
||||
} else if (type == double.class || type == Double.class) {
|
||||
if (TextUtils.isEmpty(value)){
|
||||
value = "0.0";
|
||||
}
|
||||
field.setDouble(this, Double.parseDouble(value));
|
||||
} else if (type == long.class || type == Long.class) {
|
||||
field.setLong(this, Long.parseLong(value));
|
||||
@ -235,10 +239,26 @@ class Configuration {
|
||||
*/
|
||||
int threadNum = 3;
|
||||
|
||||
/**
|
||||
* 设置最大下载速度,单位:kb, 为0表示不限速
|
||||
*/
|
||||
double msxSpeed = 0.0;
|
||||
|
||||
public int getIOTimeOut() {
|
||||
return iOTimeOut;
|
||||
}
|
||||
|
||||
public double getMsxSpeed() {
|
||||
return msxSpeed;
|
||||
}
|
||||
|
||||
public DownloadConfig setMsxSpeed(double msxSpeed) {
|
||||
this.msxSpeed = msxSpeed;
|
||||
saveKey("msxSpeed", String.valueOf(msxSpeed));
|
||||
DownloadTaskQueue.getInstance().setMaxSpeed(msxSpeed);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DownloadConfig setIOTimeOut(int iOTimeOut) {
|
||||
this.iOTimeOut = iOTimeOut;
|
||||
saveKey("iOTimeOut", iOTimeOut + "");
|
||||
|
@ -71,9 +71,29 @@ class ConnectionHelp {
|
||||
}
|
||||
conn.setRequestProperty("Charset", "UTF-8");
|
||||
conn.setRequestProperty("User-Agent",
|
||||
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
|
||||
conn.setRequestProperty("Accept",
|
||||
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
|
||||
StringBuilder accept = new StringBuilder();
|
||||
accept.append("image/gif, ")
|
||||
.append("image/jpeg, ")
|
||||
.append("image/pjpeg, ")
|
||||
.append("image/webp, ")
|
||||
.append("image/*, ")
|
||||
.append("application/xml")
|
||||
.append("application/xaml+xml, ")
|
||||
.append("application/xhtml+xml, ")
|
||||
.append("application/x-shockwave-flash, ")
|
||||
.append("application/x-ms-xbap, ")
|
||||
.append("application/x-ms-application, ")
|
||||
.append("application/msword, ")
|
||||
.append("application/vnd.ms-excel, ")
|
||||
.append("application/vnd.ms-xpsdocument, ")
|
||||
.append("application/vnd.ms-powerpoint, ")
|
||||
.append("text/plain, ")
|
||||
.append("text/html, ")
|
||||
.append("*/*");
|
||||
conn.setRequestProperty("Accept", accept.toString());
|
||||
conn.setRequestProperty("Accept-Encoding", "identity");
|
||||
conn.setRequestProperty("Accept-Charset", "UTF-8");
|
||||
conn.setRequestProperty("Connection", "Keep-Alive");
|
||||
//302获取重定向地址
|
||||
conn.setInstanceFollowRedirects(false);
|
||||
|
@ -36,8 +36,19 @@ import java.util.Set;
|
||||
public class DownloadReceiver implements IReceiver<DownloadEntity> {
|
||||
private static final String TAG = "DownloadReceiver";
|
||||
public String targetName;
|
||||
public Object obj;
|
||||
public ISchedulerListener<DownloadTask> listener;
|
||||
|
||||
/**
|
||||
* 设置最大下载速度,单位:kb
|
||||
* 该方法为实验性功能,清不要轻易在生产环境中使用。
|
||||
*
|
||||
* @param maxSpeed 为0表示不限速
|
||||
*/
|
||||
@Deprecated public void setMaxSpeed(double maxSpeed) {
|
||||
AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMsxSpeed(maxSpeed);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link #load(String)},请使用该方法
|
||||
*/
|
||||
@ -61,17 +72,37 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
|
||||
|
||||
/**
|
||||
* 添加调度器回调
|
||||
*
|
||||
* @see #register()
|
||||
*/
|
||||
public DownloadReceiver addSchedulerListener(ISchedulerListener<DownloadTask> listener) {
|
||||
@Deprecated public DownloadReceiver addSchedulerListener(
|
||||
ISchedulerListener<DownloadTask> listener) {
|
||||
this.listener = listener;
|
||||
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除回调
|
||||
* 将当前类注册到Aria
|
||||
*/
|
||||
@Override public void removeSchedulerListener() {
|
||||
public DownloadReceiver register() {
|
||||
DownloadSchedulers.getInstance().register(obj);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消注册
|
||||
*/
|
||||
@Override public void unRegister() {
|
||||
DownloadSchedulers.getInstance().unRegister(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除回调
|
||||
*
|
||||
* @see #unRegister()
|
||||
*/
|
||||
@Deprecated @Override public void removeSchedulerListener() {
|
||||
if (listener != null) {
|
||||
DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
|
||||
}
|
||||
@ -133,6 +164,7 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
|
||||
for (String key : keys) {
|
||||
IReceiver receiver = ariaManager.getReceiver().get(key);
|
||||
receiver.removeSchedulerListener();
|
||||
receiver.unRegister();
|
||||
ariaManager.getReceiver().remove(key);
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
@ -50,6 +51,19 @@ public class DownloadTask extends AbsTask<DownloadTaskEntity, DownloadEntity> {
|
||||
mUtil = new DownloadUtil(mContext, taskEntity, mListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件保存路径
|
||||
*
|
||||
* @return 如果路径不存在,返回null
|
||||
*/
|
||||
public String getDownloadPath() {
|
||||
File file = new File(mEntity.getDownloadPath());
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
}
|
||||
return mEntity.getDownloadPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前下载任务的下载地址
|
||||
*
|
||||
@ -88,6 +102,15 @@ public class DownloadTask extends AbsTask<DownloadTaskEntity, DownloadEntity> {
|
||||
stop(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置最大下载速度,单位:kb
|
||||
*
|
||||
* @param maxSpeed 为0表示不限速
|
||||
*/
|
||||
public void setMaxSpeed(double maxSpeed) {
|
||||
mUtil.setMaxSpeed(maxSpeed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
|
@ -107,6 +107,15 @@ class DownloadUtil implements IDownloadUtil, Runnable {
|
||||
return CONSTANCE.isDownloading;
|
||||
}
|
||||
|
||||
public void setMaxSpeed(double maxSpeed) {
|
||||
for (int i = 0; i < THREAD_NUM; i++) {
|
||||
SingleThreadTask task = (SingleThreadTask) mTask.get(i);
|
||||
if (task != null) {
|
||||
task.setMaxSpeed(maxSpeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消下载
|
||||
*/
|
||||
@ -323,7 +332,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
|
||||
rl++;
|
||||
}
|
||||
if (i == (THREAD_NUM - 1)) {
|
||||
//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
||||
//最后一个线程的结束位置即为文件的总长度
|
||||
endL = fileLength;
|
||||
}
|
||||
addSingleTask(i, startL, endL, fileLength);
|
||||
|
@ -63,4 +63,9 @@ interface IDownloadUtil {
|
||||
* 删除temp文件
|
||||
*/
|
||||
public void delTempFile();
|
||||
|
||||
/**
|
||||
* 设置最大下载速度
|
||||
*/
|
||||
public void setMaxSpeed(double maxSpeed);
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.download;
|
||||
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
@ -23,10 +24,13 @@ import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/1/18.
|
||||
@ -41,6 +45,11 @@ final class SingleThreadTask implements Runnable {
|
||||
private IDownloadListener mListener;
|
||||
private DownloadStateConstance CONSTANCE;
|
||||
|
||||
/**
|
||||
* speed = (bufSize / 1024) * CoresNum / sleepTime; (8192 / 1024) * 4 / 1= 32 kb/s
|
||||
*/
|
||||
private long mSleepTime = 0;
|
||||
|
||||
SingleThreadTask(DownloadStateConstance constance, IDownloadListener listener,
|
||||
DownloadUtil.ConfigEntity downloadInfo) {
|
||||
AriaManager manager = AriaManager.getInstance(AriaManager.APP);
|
||||
@ -53,6 +62,22 @@ final class SingleThreadTask implements Runnable {
|
||||
mConfigFPath = downloadInfo.CONFIG_FILE_PATH;
|
||||
}
|
||||
mBufSize = manager.getDownloadConfig().getBuffSize();
|
||||
setMaxSpeed(AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMsxSpeed());
|
||||
}
|
||||
|
||||
void setMaxSpeed(double maxSpeed) {
|
||||
if (-0.9999 < maxSpeed && maxSpeed < 0.00001) {
|
||||
mSleepTime = 0;
|
||||
} else {
|
||||
BigDecimal db = new BigDecimal(
|
||||
((mBufSize / 1024) * (filterVersion() ? 1 : CONSTANCE.THREAD_NUM) / maxSpeed) * 1000);
|
||||
db.setScale(0, BigDecimal.ROUND_HALF_UP);
|
||||
mSleepTime = db.longValue();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean filterVersion() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
@ -73,7 +98,7 @@ final class SingleThreadTask implements Runnable {
|
||||
+ "】");
|
||||
//在头里面请求下载开始位置和结束位置
|
||||
conn.setRequestProperty("Range",
|
||||
"bytes=" + mConfigEntity.START_LOCATION + "-" + mConfigEntity.END_LOCATION);
|
||||
"bytes=" + mConfigEntity.START_LOCATION + "-" + (mConfigEntity.END_LOCATION - 1));
|
||||
} else {
|
||||
Log.w(TAG, "该下载不支持断点");
|
||||
}
|
||||
@ -97,7 +122,7 @@ final class SingleThreadTask implements Runnable {
|
||||
if (CONSTANCE.isStop) {
|
||||
break;
|
||||
}
|
||||
//把下载数据数据写入文件
|
||||
Thread.sleep(mSleepTime);
|
||||
file.write(buffer, 0, len);
|
||||
progress(len);
|
||||
}
|
||||
|
@ -38,6 +38,16 @@ public abstract class AbsTarget<ENTITY extends AbsEntity, TASK_ENTITY extends Ab
|
||||
protected TASK_ENTITY taskEntity;
|
||||
protected String targetName;
|
||||
|
||||
/**
|
||||
* 设置扩展字段,用来保存你的其它数据,如果你的数据比较多,你可以把你的数据转换为JSON字符串,然后再存到Aria中
|
||||
*
|
||||
* @param str 扩展数据
|
||||
*/
|
||||
public AbsTarget setExtendField(String str) {
|
||||
entity.setStr(str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务状态
|
||||
*
|
||||
|
@ -31,6 +31,11 @@ public interface IReceiver<ENTITY extends IEntity> {
|
||||
*/
|
||||
public void removeSchedulerListener();
|
||||
|
||||
/**
|
||||
* 移除观察者
|
||||
*/
|
||||
public void unRegister();
|
||||
|
||||
/**
|
||||
* 停止所有任务
|
||||
*/
|
||||
|
@ -91,6 +91,15 @@ public class DownloadTaskQueue
|
||||
}
|
||||
}
|
||||
|
||||
public void setMaxSpeed(double maxSpeed){
|
||||
Map<String, DownloadTask> tasks = mExecutePool.getAllTask();
|
||||
Set<String> keys = tasks.keySet();
|
||||
for (String key : keys){
|
||||
DownloadTask task = tasks.get(key);
|
||||
task.setMaxSpeed(maxSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void setMaxTaskNum(int downloadNum) {
|
||||
int oldMaxSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum;
|
||||
int diff = downloadNum - oldMaxSize;
|
||||
|
@ -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) {
|
||||
|
||||
}
|
||||
}
|
@ -32,21 +32,43 @@ import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Created by AriaL on 2017/6/4.
|
||||
* Created by lyy on 2017/6/4.
|
||||
*/
|
||||
public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity, TASK extends ITask<ENTITY>, QUEUE extends ITaskQueue<TASK, TASK_ENTITY, ENTITY>>
|
||||
implements ISchedulers<TASK> {
|
||||
private static final String TAG = "AbsSchedulers";
|
||||
|
||||
/**
|
||||
* 下载的动态生成的代理类后缀
|
||||
*/
|
||||
String DOWNLOAD_PROXY_CLASS_SUFFIX = "$$DownloadListenerProxy";
|
||||
|
||||
/**
|
||||
* 上传的动态生成的代理类后缀
|
||||
*/
|
||||
String UPLOAD_PROXY_CLASS_SUFFIX = "$$UploadListenerProxy";
|
||||
|
||||
protected QUEUE mQueue;
|
||||
protected boolean isDownload = true;
|
||||
|
||||
private Map<String, ISchedulerListener<TASK>> mSchedulerListeners = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void addSchedulerListener(String targetName, ISchedulerListener<TASK> schedulerListener) {
|
||||
private Map<String, AbsSchedulerListener<TASK>> mObservers = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* @param targetName 观察者,创建该监听器的对象类名
|
||||
* @param schedulerListener {@link ISchedulerListener}
|
||||
* @see #register(Object)
|
||||
*/
|
||||
@Deprecated @Override public void addSchedulerListener(String targetName,
|
||||
ISchedulerListener<TASK> schedulerListener) {
|
||||
mSchedulerListeners.put(targetName, schedulerListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param targetName 观察者,创建该监听器的对象类名
|
||||
* @see #unRegister(Object)
|
||||
*/
|
||||
@Override public void removeSchedulerListener(String targetName,
|
||||
ISchedulerListener<TASK> schedulerListener) {
|
||||
//该内存泄露解决方案:http://stackoverflow.com/questions/14585829/how-safe-is-to-delete-already-removed-concurrenthashmap-element
|
||||
@ -57,6 +79,52 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void register(Object obj) {
|
||||
String targetName = obj.getClass().getName();
|
||||
AbsSchedulerListener<TASK> listener = mObservers.get(targetName);
|
||||
if (listener == null) {
|
||||
listener = createListener(targetName);
|
||||
if (listener != null) {
|
||||
listener.setListener(obj);
|
||||
mObservers.put(targetName, listener);
|
||||
} else {
|
||||
Log.e(TAG, "注册错误,没有【" + targetName + "】观察者");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void unRegister(Object obj) {
|
||||
for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK>>> iter =
|
||||
mObservers.entrySet().iterator(); iter.hasNext(); ) {
|
||||
Map.Entry<String, AbsSchedulerListener<TASK>> entry = iter.next();
|
||||
if (entry.getKey().equals(obj.getClass().getName())) iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建代理类
|
||||
*
|
||||
* @param targetName 通过观察者创建对应的Aria事件代理
|
||||
*/
|
||||
private AbsSchedulerListener<TASK> createListener(String targetName) {
|
||||
AbsSchedulerListener<TASK> listener = null;
|
||||
try {
|
||||
Class clazz = Class.forName(
|
||||
targetName + (isDownload ? DOWNLOAD_PROXY_CLASS_SUFFIX : UPLOAD_PROXY_CLASS_SUFFIX));
|
||||
listener = (AbsSchedulerListener<TASK>) clazz.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
Log.e(TAG, targetName + ",没有Aria的Download或Upload注解方法");
|
||||
//e.printStackTrace();
|
||||
} catch (InstantiationException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
//e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
//e.printStackTrace();
|
||||
}
|
||||
return listener;
|
||||
}
|
||||
|
||||
@Override public boolean handleMessage(Message msg) {
|
||||
TASK task = (TASK) msg.obj;
|
||||
if (task == null) {
|
||||
@ -100,6 +168,11 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
|
||||
for (String key : keys) {
|
||||
callback(state, task, mSchedulerListeners.get(key));
|
||||
}
|
||||
} else if (mObservers.size() > 0) {
|
||||
Set<String> keys = mObservers.keySet();
|
||||
for (String key : keys) {
|
||||
callback(state, task, mObservers.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@ public class DownloadSchedulers
|
||||
|
||||
private DownloadSchedulers() {
|
||||
mQueue = DownloadTaskQueue.getInstance();
|
||||
isDownload = true;
|
||||
}
|
||||
|
||||
public static DownloadSchedulers getInstance() {
|
||||
|
@ -80,4 +80,18 @@ public interface ISchedulers<Task extends ITask> extends Handler.Callback {
|
||||
*/
|
||||
public void removeSchedulerListener(String targetName,
|
||||
ISchedulerListener<Task> schedulerListener);
|
||||
|
||||
/**
|
||||
* 将当前类注册到Aria
|
||||
*
|
||||
* @param obj 观察者类
|
||||
*/
|
||||
public void register(Object obj);
|
||||
|
||||
/**
|
||||
* 移除注册
|
||||
*
|
||||
* @param obj 观察者类
|
||||
*/
|
||||
public void unRegister(Object obj);
|
||||
}
|
@ -32,6 +32,7 @@ public class UploadSchedulers
|
||||
|
||||
private UploadSchedulers() {
|
||||
mQueue = UploadTaskQueue.getInstance();
|
||||
isDownload = false;
|
||||
}
|
||||
|
||||
public static UploadSchedulers getInstance() {
|
||||
|
@ -19,8 +19,10 @@ import android.support.annotation.NonNull;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.command.AbsCmd;
|
||||
import com.arialyy.aria.core.command.CmdFactory;
|
||||
import com.arialyy.aria.core.download.DownloadReceiver;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.IReceiver;
|
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
||||
import com.arialyy.aria.core.scheduler.UploadSchedulers;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
@ -38,6 +40,7 @@ import java.util.regex.Pattern;
|
||||
public class UploadReceiver implements IReceiver<UploadEntity> {
|
||||
private static final String TAG = "DownloadReceiver";
|
||||
public String targetName;
|
||||
public Object obj;
|
||||
public ISchedulerListener<UploadTask> listener;
|
||||
|
||||
/**
|
||||
@ -127,4 +130,16 @@ public class UploadReceiver implements IReceiver<UploadEntity> {
|
||||
UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前类注册到Aria
|
||||
*/
|
||||
public UploadReceiver register() {
|
||||
UploadSchedulers.getInstance().register(obj);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public void unRegister() {
|
||||
UploadSchedulers.getInstance().unRegister(obj);
|
||||
}
|
||||
}
|
@ -30,7 +30,7 @@ public class UploadTaskEntity extends AbsTaskEntity {
|
||||
public String contentType = "multipart/form-data"; //上传的文件类型
|
||||
public String userAgent = "User-Agent";
|
||||
public String charset = "utf-8";
|
||||
|
||||
|
||||
/**
|
||||
* 文件上传表单
|
||||
*/
|
||||
|
@ -26,6 +26,7 @@ import com.arialyy.aria.core.command.CmdFactory;
|
||||
import com.arialyy.aria.core.command.AbsCmd;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
@ -40,6 +41,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/1/22.
|
||||
@ -47,6 +49,35 @@ import java.util.Properties;
|
||||
public class CommonUtil {
|
||||
private static final String TAG = "CommonUtil";
|
||||
|
||||
/**
|
||||
* 获取CPU核心数
|
||||
*/
|
||||
public static int getNumCores() {
|
||||
//Private Class to display only CPU devices in the directory listing
|
||||
class CpuFilter implements FileFilter {
|
||||
@Override public boolean accept(File pathname) {
|
||||
//Check if filename is "cpu", followed by a single digit number
|
||||
return Pattern.matches("cpu[0-9]", pathname.getName());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
//Get directory containing CPU info
|
||||
File dir = new File("/sys/devices/system/cpu/");
|
||||
//Filter to only list the devices we care about
|
||||
File[] files = dir.listFiles(new CpuFilter());
|
||||
Log.d(TAG, "CPU Count: " + files.length);
|
||||
//Return the number of cores (virtual CPU devices)
|
||||
return files.length;
|
||||
} catch (Exception e) {
|
||||
//Print exception
|
||||
Log.d(TAG, "CPU Count: Failed.");
|
||||
e.printStackTrace();
|
||||
//Default to return 1 core
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过流创建文件
|
||||
*/
|
||||
|
1
AriaAnnotations/.gitignore
vendored
Normal file
1
AriaAnnotations/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/build
|
25
AriaAnnotations/build.gradle
Normal file
25
AriaAnnotations/build.gradle
Normal file
@ -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 {
|
||||
}
|
||||
}
|
1
AriaCompiler/.gitignore
vendored
Normal file
1
AriaCompiler/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/build
|
28
AriaCompiler/build.gradle
Normal file
28
AriaCompiler/build.gradle
Normal file
@ -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<>();
|
||||
}
|
115
README.md
115
README.md
@ -24,9 +24,15 @@ Aria怎样使用?
|
||||
如果你觉得Aria对你有帮助,您的star和issues将是对我最大支持.`^_^`
|
||||
|
||||
## 下载
|
||||
[](https://bintray.com/arialyy/maven/Aria/_latestVersion)</br>
|
||||
[](https://bintray.com/arialyy/maven/AriaApi/_latestVersion)
|
||||
[](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion)
|
||||
```java
|
||||
<<<<<<< HEAD
|
||||
compile 'com.arialyy.aria:Aria:3.1.6'
|
||||
=======
|
||||
compile 'com.arialyy.aria:Aria:3.1.7'
|
||||
annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.7'
|
||||
>>>>>>> v_3.0
|
||||
```
|
||||
|
||||
## 示例
|
||||
@ -50,7 +56,7 @@ compile 'com.arialyy.aria:Aria:3.1.6'
|
||||
```java
|
||||
Aria.download(this)
|
||||
.load(DOWNLOAD_URL)
|
||||
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") //文件保存路径
|
||||
.setDownloadPath(DOWNLOAD_PATH) //文件保存路径
|
||||
.add();
|
||||
```
|
||||
|
||||
@ -59,7 +65,7 @@ compile 'com.arialyy.aria:Aria:3.1.6'
|
||||
```java
|
||||
Aria.download(this)
|
||||
.load(DOWNLOAD_URL) //读取下载地址
|
||||
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") //设置文件保存的完整路径
|
||||
.setDownloadPath(DOWNLOAD_PATH) //设置文件保存的完整路径
|
||||
.start(); //启动下载
|
||||
```
|
||||
* 暂停
|
||||
@ -80,47 +86,69 @@ compile 'com.arialyy.aria:Aria:3.1.6'
|
||||
|
||||
### 下载状态获取
|
||||
如果你希望读取下载进度或下载信息,那么你需要创建事件类,并在onResume(Activity、Fragment)或构造函数(Dialog、PopupWindow),将该事件类注册到Aria管理器。
|
||||
* 创建事件类
|
||||
|
||||
* 将对象注册到Aria
|
||||
|
||||
` Aria.download(this).register();`或`Aria.upload(this).register();`
|
||||
```java
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Aria.download(this).register();
|
||||
}
|
||||
```
|
||||
|
||||
* 使用'@Download'或'@Upload'注解你的函数
|
||||
|
||||
**注意:**
|
||||
- 注解回掉采用Apt的方式实现,所以,你不需要担心这会影响你机器的性能
|
||||
- 被注解的方法**不能被private修饰**
|
||||
- 被注解的方法**只能有一个参数,并且参数类型必须是`DownloadTask`或`UploadTask`**
|
||||
- 方法名可以为任意字符串
|
||||
|
||||
* 除了在widget(Activity、Fragment、Dialog、Popupwindow)中使用注解方法外,你还可以在Service、Notification等组件中使用注解函数。
|
||||
|
||||
```java
|
||||
final static class MySchedulerListener extends Aria.DownloadSchedulerListener{
|
||||
@Override public void onTaskPre(DownloadTask task) {
|
||||
//通过下载地址可以判断任务是否是你指定的任务
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
|
||||
.sendToTarget();
|
||||
}
|
||||
}
|
||||
@Download.onPre
|
||||
protected void onPre(DownloadTask task) {}
|
||||
|
||||
@Override public void onTaskStop(DownloadTask task) {
|
||||
super.onTaskStop(task);
|
||||
@Download.onTaskStart
|
||||
void taskStart(DownloadTask task) {
|
||||
//通过下载地址可以判断任务是否是你指定的任务
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(DownloadTask task) {
|
||||
super.onTaskCancel(task);
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(DownloadTask task) {
|
||||
super.onTaskRunning(task);
|
||||
}
|
||||
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
* 将事件注册到Aria
|
||||
@Download.onTaskRunning
|
||||
protected void running(DownloadTask task) {}
|
||||
|
||||
@Download.onTaskResume
|
||||
void taskResume(DownloadTask task) {}
|
||||
|
||||
@Download.onTaskStop
|
||||
void taskStop(DownloadTask task) {}
|
||||
|
||||
@Download.onTaskCancel
|
||||
void taskCancel(DownloadTask task) {}
|
||||
|
||||
@Download.onTaskFail
|
||||
void taskFail(DownloadTask task) {}
|
||||
|
||||
@Download.onTaskComplete
|
||||
void taskComplete(DownloadTask task) {}
|
||||
|
||||
@Download.onNoSupportBreakPoint
|
||||
public void onNoSupportBreakPoint(DownloadTask task) {}
|
||||
|
||||
<<<<<<< HEAD
|
||||
```java
|
||||
@Override protected void onResume() {
|
||||
super.onResume();
|
||||
Aria.download(this).addSchedulerListener(new MySchedulerListener());
|
||||
}
|
||||
=======
|
||||
>>>>>>> v_3.0
|
||||
```
|
||||
|
||||
### 使用广播接收器接收Aria的任务下载状态
|
||||
有的时候,你可能希望在广播中接收Aria的下载事件,那么你需要
|
||||
* 在[配置文件](#配置文件设置参数)中把` <openBroadcast value="false"/>`中的value改为true
|
||||
* [点击该链接查看注册Aria广播的方式](https://github.com/AriaLyy/Aria/blob/master/BroadCast.md)
|
||||
|
||||
### Aria参数配置
|
||||
#### 配置文件设置参数
|
||||
@ -135,7 +163,7 @@ compile 'com.arialyy.aria:Aria:3.1.6'
|
||||
<!--设置下载线程,线程下载数改变后,新的下载任务才会生效-->
|
||||
<threadNum value="4"/>
|
||||
|
||||
<!--是否打开下载广播,默认为false-->
|
||||
<!--是否打开下载广播,默认为false,不建议使用广播,你可以使用Download注解来实现事件回调-->
|
||||
<openBroadcast value="false"/>
|
||||
|
||||
<!--设置下载队列最大任务数, 默认为2-->
|
||||
@ -145,7 +173,7 @@ compile 'com.arialyy.aria:Aria:3.1.6'
|
||||
<reTryNum value="10"/>
|
||||
|
||||
<!--设置重试间隔,单位为毫秒,默认2000毫秒-->
|
||||
<reTryInterval value="2000"/>
|
||||
<reTryInterval value="5000"/>
|
||||
|
||||
<!--设置url连接超时时间,单位为毫秒,默认5000毫秒-->
|
||||
<connectTimeOut value="5000"/>
|
||||
@ -160,12 +188,15 @@ compile 'com.arialyy.aria:Aria:3.1.6'
|
||||
<ca name="" path=""/>
|
||||
|
||||
<!--是否需要转换速度单位,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s,如果不需要将返回byte长度-->
|
||||
<convertSpeed value="false"/>
|
||||
<convertSpeed value="true"/>
|
||||
|
||||
<!--设置最大下载速度,单位:kb, 为0表示不限速-->
|
||||
<maxSpeed value="0"/>
|
||||
|
||||
</download>
|
||||
|
||||
<upload>
|
||||
<!--是否打开上传广播,默认为false-->
|
||||
<!--是否打开上传广播,默认为false,不建议使用广播,你可以使用Upload注解来实现事件回调-->
|
||||
<openBroadcast value="false"/>
|
||||
|
||||
<!--设置上传队列最大任务数, 默认为2-->
|
||||
@ -260,11 +291,18 @@ Aria.download(this).load(DOWNLOAD_URL).setDownloadPath(PATH).setHighestPriority(
|
||||
```java
|
||||
Aria.upload(this).load(filePath).cancel();
|
||||
```
|
||||
|
||||
|
||||
## 混淆配置
|
||||
```
|
||||
-dontwarn com.arialyy.aria.**
|
||||
-keep class com.arialyy.aria.**{*;}
|
||||
-keep class **$$DownloadListenerProxy{ *; }
|
||||
-keep class **$$UploadListenerProxy{ *; }
|
||||
-keepclasseswithmembernames class * {
|
||||
@Download.* <methods>;
|
||||
@Upload.* <methods>;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## 其他
|
||||
@ -278,7 +316,12 @@ Aria.download(this).load(DOWNLOAD_URL).setDownloadPath(PATH).setHighestPriority(
|
||||
|
||||
|
||||
## 开发日志
|
||||
<<<<<<< HEAD
|
||||
+ v_3.1.6 [取消任务时onTaskCancel回调两次的bug](https://github.com/AriaLyy/Aria/issues/33)
|
||||
=======
|
||||
+ v_3.1.7 修复某些文件下载不了的bug,增加apt注解方法,事件获取更加简单了
|
||||
+ v_3.1.6 取消任务时onTaskCancel回调两次的bug
|
||||
>>>>>>> v_3.0
|
||||
+ v_3.1.5 优化代码结构,增加优先下载任务功能。
|
||||
+ v_3.1.4 修复快速切换,暂停、恢复功能时,概率性出现的重新下载问题,添加onPre()回调,onPre()用于请求地址之前执行界面UI更新操作。
|
||||
+ v_3.1.0 添加Aria配置文件,优化代码
|
||||
|
@ -25,6 +25,11 @@ android {
|
||||
packagingOptions {
|
||||
exclude 'META-INF/services/javax.annotation.processing.Processor'
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
abortOnError false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -36,7 +41,8 @@ dependencies {
|
||||
compile 'com.google.code.gson:gson:2.7'
|
||||
compile 'com.squareup.okhttp3:okhttp:3.2.0'
|
||||
compile 'com.arialyy.frame:MVVM2:2.2.0'
|
||||
compile 'com.arialyy.absadapter:AbsAdapter:1.1.2'
|
||||
compile project(':Aria')
|
||||
// compile 'com.arialyy.aria:Aria:3.0.0'
|
||||
// compile 'com.arialyy.aria:aria-core:3.1.7'
|
||||
// annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.7'
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
<!--设置下载线程,线程下载数改变后,新的下载任务才会生效-->
|
||||
<threadNum value="4"/>
|
||||
|
||||
<!--是否打开下载广播,默认为false-->
|
||||
<!--是否打开下载广播,默认为false,不建议使用广播,你可以使用Download注解来实现事件回调-->
|
||||
<openBroadcast value="false"/>
|
||||
|
||||
<!--设置下载队列最大任务数, 默认为2-->
|
||||
@ -37,7 +37,7 @@
|
||||
</download>
|
||||
|
||||
<upload>
|
||||
<!--是否打开上传广播,默认为false-->
|
||||
<!--是否打开上传广播,默认为false,不建议使用广播,你可以使用Upload注解来实现事件回调-->
|
||||
<openBroadcast value="false"/>
|
||||
|
||||
<!--设置上传队列最大任务数, 默认为2-->
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import butterknife.OnClick;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.download.DownloadTarget;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
@ -61,7 +62,7 @@ public class DownloadDialog extends AbsDialog {
|
||||
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
|
||||
mPb.setProgress(p);
|
||||
}
|
||||
Aria.download(this).addSchedulerListener(new MyDialogDownloadCallback());
|
||||
Aria.download(this).register();
|
||||
DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL);
|
||||
if (entity != null) {
|
||||
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
|
||||
@ -89,6 +90,29 @@ public class DownloadDialog extends AbsDialog {
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskPre public void onTaskPre(DownloadTask task) {
|
||||
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
|
||||
setBtState(false);
|
||||
}
|
||||
|
||||
@Download.onTaskStop public void onTaskStop(DownloadTask task) {
|
||||
setBtState(true);
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
|
||||
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) {
|
||||
setBtState(true);
|
||||
mPb.setProgress(0);
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
|
||||
@Download.onTaskRunning public void onTaskRunning(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mPb.setProgress(task.getPercent());
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
}
|
||||
|
||||
@Override protected void dataCallback(int result, Object obj) {
|
||||
|
||||
}
|
||||
@ -98,34 +122,4 @@ public class DownloadDialog extends AbsDialog {
|
||||
mCancel.setEnabled(!startEnable);
|
||||
mStop.setEnabled(!startEnable);
|
||||
}
|
||||
|
||||
private class MyDialogDownloadCallback extends Aria.DownloadSchedulerListener {
|
||||
|
||||
@Override public void onTaskPre(DownloadTask task) {
|
||||
super.onTaskPre(task);
|
||||
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
|
||||
setBtState(false);
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(DownloadTask task) {
|
||||
super.onTaskStop(task);
|
||||
setBtState(true);
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(DownloadTask task) {
|
||||
super.onTaskCancel(task);
|
||||
setBtState(true);
|
||||
mPb.setProgress(0);
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(DownloadTask task) {
|
||||
super.onTaskRunning(task);
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mPb.setProgress(task.getPercent());
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import butterknife.OnClick;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.download.DownloadTarget;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
@ -63,7 +64,7 @@ public class DownloadPopupWindow extends AbsPopupWindow {
|
||||
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
|
||||
mPb.setProgress(p);
|
||||
}
|
||||
Aria.download(this).addSchedulerListener(new MyDialogDownloadCallback());
|
||||
Aria.download(this).register();
|
||||
DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL);
|
||||
if (entity != null) {
|
||||
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
|
||||
@ -101,37 +102,30 @@ public class DownloadPopupWindow extends AbsPopupWindow {
|
||||
mStop.setEnabled(!startEnable);
|
||||
}
|
||||
|
||||
private class MyDialogDownloadCallback extends Aria.DownloadSchedulerListener {
|
||||
@Download.onTaskPre public void onTaskPre(DownloadTask task) {
|
||||
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
|
||||
setBtState(false);
|
||||
}
|
||||
|
||||
@Override public void onTaskPre(DownloadTask task) {
|
||||
super.onTaskPre(task);
|
||||
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
|
||||
setBtState(false);
|
||||
}
|
||||
@Download.onTaskStop public void onTaskStop(DownloadTask task) {
|
||||
setBtState(true);
|
||||
mSpeed.setText("0.0kb/s");
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(DownloadTask task) {
|
||||
super.onTaskStop(task);
|
||||
setBtState(true);
|
||||
mSpeed.setText("0.0kb/s");
|
||||
}
|
||||
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) {
|
||||
setBtState(true);
|
||||
mPb.setProgress(0);
|
||||
mSpeed.setText("0.0kb/s");
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(DownloadTask task) {
|
||||
super.onTaskCancel(task);
|
||||
setBtState(true);
|
||||
@Download.onTaskRunning public void onTaskRunning(DownloadTask task) {
|
||||
long current = task.getCurrentProgress();
|
||||
long len = task.getFileSize();
|
||||
if (len == 0) {
|
||||
mPb.setProgress(0);
|
||||
mSpeed.setText("0.0kb/s");
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(DownloadTask task) {
|
||||
super.onTaskRunning(task);
|
||||
long current = task.getCurrentProgress();
|
||||
long len = task.getFileSize();
|
||||
if (len == 0) {
|
||||
mPb.setProgress(0);
|
||||
} else {
|
||||
mPb.setProgress((int) ((current * 100) / len));
|
||||
}
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
} else {
|
||||
mPb.setProgress((int) ((current * 100) / len));
|
||||
}
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
}
|
||||
|
@ -33,18 +33,21 @@ import android.widget.RadioGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import butterknife.Bind;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.download.DownloadTarget;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.frame.util.FileUtil;
|
||||
import com.arialyy.frame.util.show.L;
|
||||
import com.arialyy.frame.util.show.T;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.base.BaseActivity;
|
||||
import com.arialyy.simple.databinding.ActivitySingleBinding;
|
||||
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
|
||||
import java.io.File;
|
||||
|
||||
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
public static final int DOWNLOAD_PRE = 0x01;
|
||||
@ -60,7 +63,10 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
|
||||
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
|
||||
"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
|
||||
//"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk";
|
||||
//"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815";
|
||||
//"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk";
|
||||
//"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt";
|
||||
//"http://down8.androidgame-store.com/201706122321/97967927DD4E53D9905ECAA7874C8128/new/game1/19/45319/com.neuralprisma-2.5.2.174-2000174_1494784835.apk?f=web_1";
|
||||
//不支持断点的链接
|
||||
//"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
|
||||
//"http://172.18.104.50:8080/download/_302turn";
|
||||
@ -71,15 +77,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
@Bind(R.id.size) TextView mSize;
|
||||
@Bind(R.id.speed) TextView mSpeed;
|
||||
@Bind(R.id.speeds) RadioGroup mRg;
|
||||
private DownloadEntity mEntity;
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (action.equals(Aria.ACTION_START)) {
|
||||
L.d("START");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private Handler mUpdateHandler = new Handler() {
|
||||
@Override public void handleMessage(Message msg) {
|
||||
@ -132,6 +129,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
}
|
||||
};
|
||||
|
||||
@Override protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Aria.download(this).register();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置start 和 stop 按钮状态
|
||||
*/
|
||||
@ -140,28 +142,105 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
mStop.setEnabled(!state);
|
||||
}
|
||||
|
||||
@Override protected void onResume() {
|
||||
super.onResume();
|
||||
Aria.download(this).addSchedulerListener(new MySchedulerListener());
|
||||
//registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
|
||||
}
|
||||
|
||||
@Override public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_single_task_activity, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override public boolean onMenuItemClick(MenuItem item) {
|
||||
if (item.getItemId() == R.id.help) {
|
||||
String msg = "一些小知识点:\n"
|
||||
+ "1、你可以通过task.getKey().equals(DOWNLOAD_URL)判断是否是当前页面的下载,以防止progress乱跳\n"
|
||||
+ "2、当遇到网络慢的情况时,你可以先使用onPre()更新UI界面,待连接成功时,再在onTaskPre()获取完整的task数据,然后给UI界面设置正确的数据\n"
|
||||
+ "3、你可以在界面初始化时通过Aria.download(this).load(DOWNLOAD_URL).getPercent()等方法快速获取相关任务的一些数据";
|
||||
showMsgDialog("tip", msg);
|
||||
double speed = -1;
|
||||
String msg = "";
|
||||
switch (item.getItemId()) {
|
||||
case R.id.help:
|
||||
msg = "一些小知识点:\n"
|
||||
+ "1、你可以通过task.getKey().equals(DOWNLOAD_URL)判断是否是当前页面的下载,以防止progress乱跳\n"
|
||||
+ "2、当遇到网络慢的情况时,你可以先使用onPre()更新UI界面,待连接成功时,再在onTaskPre()获取完整的task数据,然后给UI界面设置正确的数据\n"
|
||||
+ "3、你可以在界面初始化时通过Aria.download(this).load(DOWNLOAD_URL).getPercent()等方法快速获取相关任务的一些数据";
|
||||
showMsgDialog("tip", msg);
|
||||
break;
|
||||
case R.id.speed_0:
|
||||
speed = 0.0;
|
||||
break;
|
||||
case R.id.speed_128:
|
||||
speed = 128.0;
|
||||
break;
|
||||
case R.id.speed_256:
|
||||
speed = 256.0;
|
||||
break;
|
||||
case R.id.speed_512:
|
||||
speed = 512.0;
|
||||
break;
|
||||
case R.id.speed_1m:
|
||||
speed = 1024.0;
|
||||
break;
|
||||
}
|
||||
if (speed > -1) {
|
||||
msg = item.getTitle().toString();
|
||||
Aria.download(this).setMaxSpeed(speed);
|
||||
T.showShort(this, msg);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Download.onPre protected void onPre(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
|
||||
.sendToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskStart void taskStart(DownloadTask task) {
|
||||
//通过下载地址可以判断任务是否是你指定的任务
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getDownloadEntity().getFileSize())
|
||||
.sendToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskRunning protected void running(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskResume void taskResume(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getFileSize()).sendToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskStop void taskStop(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
|
||||
L.d(TAG, "task__stop");
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskCancel void taskCancel(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
|
||||
L.d(TAG, "task__cancel");
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskFail void taskFail(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskComplete void taskComplete(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onNoSupportBreakPoint public void onNoSupportBreakPoint(DownloadTask task) {
|
||||
if (task.getKey().equals(DOWNLOAD_URL)) {
|
||||
T.showShort(SingleTaskActivity.this, "该下载链接不支持断点");
|
||||
}
|
||||
}
|
||||
|
||||
@Override protected int setLayoutId() {
|
||||
return R.layout.activity_single;
|
||||
}
|
||||
@ -203,6 +282,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
break;
|
||||
}
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
|
||||
private class MySchedulerListener extends Aria.DownloadSchedulerListener {
|
||||
|
||||
@ -263,4 +343,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
}
|
||||
}
|
||||
}
|
||||
=======
|
||||
>>>>>>> v_3.0
|
||||
}
|
@ -23,6 +23,7 @@ import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import butterknife.OnClick;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.download.DownloadTarget;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
@ -61,11 +62,7 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
|
||||
} else {
|
||||
setBtState(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onResume() {
|
||||
super.onResume();
|
||||
Aria.download(this).addSchedulerListener(new DownloadFragment.MyDialogDownloadCallback());
|
||||
Aria.download(this).register();
|
||||
}
|
||||
|
||||
@OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) {
|
||||
@ -85,6 +82,33 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
|
||||
}
|
||||
}
|
||||
|
||||
@Download.onTaskPre public void onTaskPre(DownloadTask task) {
|
||||
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
|
||||
setBtState(false);
|
||||
}
|
||||
|
||||
@Download.onTaskStop public void onTaskStop(DownloadTask task) {
|
||||
setBtState(true);
|
||||
mSpeed.setText("0.0kb/s");
|
||||
}
|
||||
|
||||
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) {
|
||||
setBtState(true);
|
||||
mPb.setProgress(0);
|
||||
mSpeed.setText("0.0kb/s");
|
||||
}
|
||||
|
||||
@Download.onTaskRunning public void onTaskRunning(DownloadTask task) {
|
||||
long current = task.getCurrentProgress();
|
||||
long len = task.getFileSize();
|
||||
if (len == 0) {
|
||||
mPb.setProgress(0);
|
||||
} else {
|
||||
mPb.setProgress((int) ((current * 100) / len));
|
||||
}
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
|
||||
@Override protected void onDelayLoad() {
|
||||
|
||||
}
|
||||
@ -102,38 +126,4 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
|
||||
mCancel.setEnabled(!startEnable);
|
||||
mStop.setEnabled(!startEnable);
|
||||
}
|
||||
|
||||
private class MyDialogDownloadCallback extends Aria.DownloadSchedulerListener {
|
||||
|
||||
@Override public void onTaskPre(DownloadTask task) {
|
||||
super.onTaskPre(task);
|
||||
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
|
||||
setBtState(false);
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(DownloadTask task) {
|
||||
super.onTaskStop(task);
|
||||
setBtState(true);
|
||||
mSpeed.setText("0.0kb/s");
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(DownloadTask task) {
|
||||
super.onTaskCancel(task);
|
||||
setBtState(true);
|
||||
mPb.setProgress(0);
|
||||
mSpeed.setText("0.0kb/s");
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(DownloadTask task) {
|
||||
super.onTaskRunning(task);
|
||||
long current = task.getCurrentProgress();
|
||||
long len = task.getFileSize();
|
||||
if (len == 0) {
|
||||
mPb.setProgress(0);
|
||||
} else {
|
||||
mPb.setProgress((int) ((current * 100) / len));
|
||||
}
|
||||
mSpeed.setText(task.getConvertSpeed());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,12 +22,12 @@ import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import butterknife.Bind;
|
||||
import com.arialyy.absadapter.common.AbsHolder;
|
||||
import com.arialyy.absadapter.recycler_view.AbsRVAdapter;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.base.adapter.AbsHolder;
|
||||
import com.arialyy.simple.base.adapter.AbsRVAdapter;
|
||||
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -22,10 +22,10 @@ import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import butterknife.Bind;
|
||||
import com.arialyy.absadapter.common.AbsHolder;
|
||||
import com.arialyy.absadapter.recycler_view.AbsRVAdapter;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.base.adapter.AbsHolder;
|
||||
import com.arialyy.simple.base.adapter.AbsRVAdapter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
@ -21,6 +21,7 @@ import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import butterknife.Bind;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
@ -46,6 +47,7 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
|
||||
|
||||
@Override protected void init(Bundle savedInstanceState) {
|
||||
super.init(savedInstanceState);
|
||||
Aria.download(this).register();
|
||||
setTitle("下载列表");
|
||||
List<DownloadEntity> temps = Aria.download(this).getTaskList();
|
||||
if (temps != null && !temps.isEmpty()) {
|
||||
@ -56,53 +58,38 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
|
||||
mList.setAdapter(mAdapter);
|
||||
}
|
||||
|
||||
@Override protected void onResume() {
|
||||
super.onResume();
|
||||
Aria.download(this).addSchedulerListener(new MySchedulerListener());
|
||||
@Download.onPre void onPre(DownloadTask task) {
|
||||
L.d(TAG, "download onPre");
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
|
||||
private class MySchedulerListener extends Aria.DownloadSchedulerListener {
|
||||
@Override public void onTaskPre(DownloadTask task) {
|
||||
super.onTaskPre(task);
|
||||
L.d(TAG, "download onPre");
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
@Download.onTaskStart void taskStart(DownloadTask task) {
|
||||
L.d(TAG, "download start");
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
|
||||
@Override public void onTaskStart(DownloadTask task) {
|
||||
super.onTaskStart(task);
|
||||
L.d(TAG, "download start");
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
@Download.onTaskResume void taskResume(DownloadTask task) {
|
||||
L.d(TAG, "download resume");
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
|
||||
@Override public void onTaskResume(DownloadTask task) {
|
||||
super.onTaskResume(task);
|
||||
L.d(TAG, "download resume");
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
@Download.onTaskStop void taskStop(DownloadTask task) {
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(DownloadTask task) {
|
||||
super.onTaskRunning(task);
|
||||
mAdapter.setProgress(task.getDownloadEntity());
|
||||
}
|
||||
@Download.onTaskCancel void taskCancel(DownloadTask task) {
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(DownloadTask task) {
|
||||
super.onTaskStop(task);
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
@Download.onTaskFail void taskFail(DownloadTask task) {
|
||||
L.d(TAG, "download fail");
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(DownloadTask task) {
|
||||
super.onTaskCancel(task);
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
@Download.onTaskComplete void taskComplete(DownloadTask task) {
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
|
||||
@Override public void onTaskComplete(DownloadTask task) {
|
||||
super.onTaskComplete(task);
|
||||
mAdapter.updateState(task.getDownloadEntity());
|
||||
}
|
||||
|
||||
@Override public void onTaskFail(DownloadTask task) {
|
||||
super.onTaskFail(task);
|
||||
L.d(TAG, "download fail");
|
||||
}
|
||||
@Download.onTaskRunning void taskRunning(DownloadTask task) {
|
||||
mAdapter.setProgress(task.getDownloadEntity());
|
||||
}
|
||||
}
|
||||
|
@ -23,12 +23,15 @@ import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.View;
|
||||
import butterknife.Bind;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
import com.arialyy.frame.util.show.T;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.base.BaseActivity;
|
||||
import com.arialyy.simple.databinding.ActivityMultiBinding;
|
||||
import com.arialyy.simple.download.DownloadModule;
|
||||
import com.arialyy.simple.download.SingleTaskActivity;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -47,6 +50,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
|
||||
|
||||
@Override protected void init(Bundle savedInstanceState) {
|
||||
super.init(savedInstanceState);
|
||||
Aria.download(this).register();
|
||||
setTitle("多任务下载");
|
||||
mData.addAll(getModule(DownloadModule.class).createMultiTestList());
|
||||
mAdapter = new FileListAdapter(this, mData);
|
||||
@ -69,14 +73,28 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override protected void onResume() {
|
||||
super.onResume();
|
||||
Aria.download(this).addSchedulerListener(new DownloadListener());
|
||||
@Download.onTaskStart void taskStart(DownloadTask task) {
|
||||
mAdapter.updateBtState(task.getKey(), false);
|
||||
}
|
||||
|
||||
@Override protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
//unregisterReceiver(mReceiver);
|
||||
@Download.onTaskResume void taskResume(DownloadTask task) {
|
||||
mAdapter.updateBtState(task.getKey(), false);
|
||||
}
|
||||
|
||||
@Download.onTaskStop void taskStop(DownloadTask task) {
|
||||
mAdapter.updateBtState(task.getKey(), true);
|
||||
}
|
||||
|
||||
@Download.onTaskCancel void taskCancel(DownloadTask task) {
|
||||
mAdapter.updateBtState(task.getKey(), true);
|
||||
}
|
||||
|
||||
@Download.onTaskFail void taskFail(DownloadTask task) {
|
||||
mAdapter.updateBtState(task.getKey(), true);
|
||||
}
|
||||
|
||||
@Download.onTaskComplete void taskComplete(DownloadTask task) {
|
||||
mAdapter.updateBtState(task.getKey(), true);
|
||||
}
|
||||
|
||||
@Override protected void dataCallback(int result, Object data) {
|
||||
@ -85,31 +103,4 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
|
||||
Aria.get(this).getDownloadConfig().setMaxTaskNum(Integer.parseInt(data + ""));
|
||||
}
|
||||
}
|
||||
|
||||
private class DownloadListener extends Aria.DownloadSchedulerListener {
|
||||
|
||||
@Override public void onTaskStart(DownloadTask task) {
|
||||
super.onTaskStart(task);
|
||||
mAdapter.updateBtState(task.getDownloadUrl(), false);
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(DownloadTask task) {
|
||||
super.onTaskRunning(task);
|
||||
}
|
||||
|
||||
@Override public void onTaskResume(DownloadTask task) {
|
||||
super.onTaskResume(task);
|
||||
mAdapter.updateBtState(task.getDownloadUrl(), false);
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(DownloadTask task) {
|
||||
super.onTaskStop(task);
|
||||
mAdapter.updateBtState(task.getDownloadUrl(), true);
|
||||
}
|
||||
|
||||
@Override public void onTaskComplete(DownloadTask task) {
|
||||
super.onTaskComplete(task);
|
||||
mAdapter.updateBtState(task.getDownloadUrl(), true);
|
||||
}
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@ import android.content.Intent;
|
||||
import android.os.Environment;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
import com.arialyy.frame.util.show.T;
|
||||
@ -45,7 +46,7 @@ public class DownloadService extends Service {
|
||||
@Override public void onCreate() {
|
||||
super.onCreate();
|
||||
mNotify = new DownloadNotification(getApplicationContext());
|
||||
Aria.download(this).addSchedulerListener(new MySchedulerListener());
|
||||
Aria.download(this).register();
|
||||
Aria.download(this)
|
||||
.load(DOWNLOAD_URL)
|
||||
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/service_task.apk")
|
||||
@ -54,45 +55,37 @@ public class DownloadService extends Service {
|
||||
|
||||
@Override public void onDestroy() {
|
||||
super.onDestroy();
|
||||
Aria.download(this).removeSchedulerListener();
|
||||
Aria.download(this).unRegister();
|
||||
}
|
||||
|
||||
private class MySchedulerListener extends Aria.DownloadSchedulerListener {
|
||||
@Download.onNoSupportBreakPoint public void onNoSupportBreakPoint(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), "该下载链接不支持断点");
|
||||
}
|
||||
|
||||
@Override public void onNoSupportBreakPoint(DownloadTask task) {
|
||||
super.onNoSupportBreakPoint(task);
|
||||
T.showShort(getApplicationContext(), "该下载链接不支持断点");
|
||||
}
|
||||
@Download.onTaskStart public void onTaskStart(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",开始下载");
|
||||
}
|
||||
|
||||
@Override public void onTaskStart(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",开始下载");
|
||||
}
|
||||
@Download.onTaskStop public void onTaskStop(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",停止下载");
|
||||
}
|
||||
|
||||
@Override public void onTaskResume(DownloadTask task) {
|
||||
super.onTaskResume(task);
|
||||
}
|
||||
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",取消下载");
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",停止下载");
|
||||
}
|
||||
@Download.onTaskFail public void onTaskFail(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",下载失败");
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",取消下载");
|
||||
}
|
||||
@Download.onTaskComplete public void onTaskComplete(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",下载完成");
|
||||
mNotify.upload(100);
|
||||
}
|
||||
|
||||
@Override public void onTaskFail(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",下载失败");
|
||||
}
|
||||
|
||||
@Override public void onTaskComplete(DownloadTask task) {
|
||||
T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",下载完成");
|
||||
mNotify.upload(100);
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(DownloadTask task) {
|
||||
long len = task.getFileSize();
|
||||
int p = (int) (task.getCurrentProgress() * 100 / len);
|
||||
mNotify.upload(p);
|
||||
}
|
||||
@Download.onTaskRunning public void onTaskRunning(DownloadTask task) {
|
||||
long len = task.getFileSize();
|
||||
int p = (int) (task.getCurrentProgress() * 100 / len);
|
||||
mNotify.upload(p);
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import butterknife.Bind;
|
||||
import butterknife.OnClick;
|
||||
import com.arialyy.annotations.Upload;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.upload.UploadTask;
|
||||
import com.arialyy.frame.util.FileUtil;
|
||||
@ -78,6 +79,7 @@ public class UploadActivity extends BaseActivity<ActivityUploadMeanBinding> {
|
||||
|
||||
@Override protected void init(Bundle savedInstanceState) {
|
||||
super.init(savedInstanceState);
|
||||
Aria.upload(this).register();
|
||||
}
|
||||
|
||||
@OnClick(R.id.upload) void upload() {
|
||||
@ -96,50 +98,30 @@ public class UploadActivity extends BaseActivity<ActivityUploadMeanBinding> {
|
||||
Aria.upload(this).load(FILE_PATH).cancel();
|
||||
}
|
||||
|
||||
@Override protected void onResume() {
|
||||
super.onResume();
|
||||
Aria.upload(this).addSchedulerListener(new UploadListener(mHandler));
|
||||
@Upload.onPre public void onPre(UploadTask task) {
|
||||
}
|
||||
|
||||
static class UploadListener extends Aria.UploadSchedulerListener {
|
||||
WeakReference<Handler> handler;
|
||||
@Upload.onTaskPre public void taskPre(UploadTask task) {
|
||||
L.d(TAG, "fileSize = " + task.getConvertFileSize());
|
||||
}
|
||||
|
||||
UploadListener(Handler handler) {
|
||||
this.handler = new WeakReference<>(handler);
|
||||
}
|
||||
@Upload.onTaskStart public void taskStart(UploadTask task) {
|
||||
mHandler.obtainMessage(START, task).sendToTarget();
|
||||
}
|
||||
|
||||
@Override public void onPre(UploadTask task) {
|
||||
super.onPre(task);
|
||||
}
|
||||
@Upload.onTaskStop public void taskStop(UploadTask task) {
|
||||
mHandler.obtainMessage(STOP, task).sendToTarget();
|
||||
}
|
||||
|
||||
@Override public void onTaskPre(UploadTask task) {
|
||||
super.onTaskPre(task);
|
||||
L.d(TAG, "fileSize = " + task.getConvertFileSize());
|
||||
}
|
||||
@Upload.onTaskCancel public void taskCancel(UploadTask task) {
|
||||
mHandler.obtainMessage(CANCEL, task).sendToTarget();
|
||||
}
|
||||
|
||||
@Override public void onTaskStart(UploadTask task) {
|
||||
super.onTaskStart(task);
|
||||
handler.get().obtainMessage(START, task).sendToTarget();
|
||||
}
|
||||
@Upload.onTaskRunning public void taskRunning(UploadTask task) {
|
||||
mHandler.obtainMessage(RUNNING, task).sendToTarget();
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(UploadTask task) {
|
||||
super.onTaskStop(task);
|
||||
handler.get().obtainMessage(STOP, task).sendToTarget();
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(UploadTask task) {
|
||||
super.onTaskCancel(task);
|
||||
handler.get().obtainMessage(CANCEL, task).sendToTarget();
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(UploadTask task) {
|
||||
super.onTaskRunning(task);
|
||||
handler.get().obtainMessage(RUNNING, task).sendToTarget();
|
||||
}
|
||||
|
||||
@Override public void onTaskComplete(UploadTask task) {
|
||||
super.onTaskComplete(task);
|
||||
handler.get().obtainMessage(COMPLETE, task).sendToTarget();
|
||||
}
|
||||
@Upload.onTaskComplete public void taskComplete(UploadTask task) {
|
||||
mHandler.obtainMessage(COMPLETE, task).sendToTarget();
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,26 @@
|
||||
android:title="tip"
|
||||
app:showAsAction="ifRoom"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/speed_0"
|
||||
android:title="不限速"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/speed_128"
|
||||
android:title="最大速度(128kb)"
|
||||
/>
|
||||
<item
|
||||
android:id="@+id/speed_256"
|
||||
android:title="最大速度(256kb)"
|
||||
/>
|
||||
|
||||
<item
|
||||
android:id="@+id/speed_512"
|
||||
android:title="最大速度(512kb)"
|
||||
/>
|
||||
|
||||
<item
|
||||
android:id="@+id/speed_1m"
|
||||
android:title="最大速度(1mb)"
|
||||
/>
|
||||
</menu>
|
19
app/src/main/res/values/ids.xml
Normal file
19
app/src/main/res/values/ids.xml
Normal file
@ -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>
|
51
build.gradle
51
build.gradle
@ -1,26 +1,45 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:2.2.2'
|
||||
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
|
||||
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
}
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:2.2.2'
|
||||
classpath 'com.novoda:bintray-release:0.5.0'
|
||||
// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
|
||||
// classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
tasks.withType(Javadoc) {
|
||||
options {
|
||||
encoding "UTF-8"
|
||||
charSet 'UTF-8'
|
||||
links "http://docs.oracle.com/javase/7/docs/api"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
delete rootProject.buildDir
|
||||
delete rootProject.buildDir
|
||||
}
|
||||
|
||||
ext {
|
||||
userOrg = 'arialyy'
|
||||
groupId = 'com.arialyy.aria'
|
||||
// publishVersion = '0.0.6'
|
||||
publishVersion = '3.1.7'
|
||||
repoName='maven'
|
||||
desc = 'android 下载框架'
|
||||
website = 'https://github.com/AriaLyy/Aria'
|
||||
licences = ['Apache-2.0']
|
||||
}
|
||||
|
0
cache/build/mPro.properties
vendored
0
cache/build/mPro.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
|
||||
|
@ -1 +1 @@
|
||||
include ':app', ':Aria'
|
||||
include ':app', ':Aria', ':AriaAnnotations', ':AriaCompiler'
|
||||
|
Reference in New Issue
Block a user