From 72e296adeb5372c341bdca7349ecfb0de76e19b5 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Fri, 16 Jun 2017 21:35:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20https://github.com/AriaLyy?= =?UTF-8?q?/Aria/issues/47?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria/build.gradle | 6 +- .../aria/core/command/ResumeAllCmd.java | 4 +- .../aria/core/download/DownloadReceiver.java | 2 + .../com/arialyy/aria/util/CommonUtil.java | 3 + AriaAnnotations/build.gradle | 2 +- .../com/arialyy/compiler/ElementHandle.java | 2 +- README.md | 64 ++++++++++++------- .../simple/download/SingleTaskActivity.java | 1 + .../multi_download/DownloadAdapter.java | 1 + .../multi_download/MultiDownloadActivity.java | 26 +++++++- .../main/res/menu/menu_highest_priority.xml | 3 +- app/src/main/res/menu/menu_mutil_task.xml | 10 +++ build.gradle | 4 +- 13 files changed, 92 insertions(+), 36 deletions(-) create mode 100644 app/src/main/res/menu/menu_mutil_task.xml diff --git a/Aria/build.gradle b/Aria/build.gradle index 3a7a4c2e..0c1d0be7 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -22,8 +22,8 @@ dependencies { 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') +// compile project(':AriaCompiler') + compile project(':AriaAnnotations') } +apply from: 'bintray-release.gradle' //apply from: 'jcenter.gradle' -//apply from: 'bintray-release.gradle' \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java index 563aacd2..c7110077 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java @@ -2,6 +2,7 @@ package com.arialyy.aria.core.command; import android.util.Log; import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; @@ -40,7 +41,8 @@ final class ResumeAllCmd extends AbsCmd { private ITask createTask(DownloadEntity entity) { ITask task = mQueue.getTask(entity); if (task == null) { - task = mQueue.createTask(mTargetName, mEntity); + DownloadTaskEntity taskEntity = new DownloadTaskEntity(entity); + task = mQueue.createTask(mTargetName, taskEntity); } else { Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index b9a7d4e7..8f29f02f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -144,6 +144,8 @@ public class DownloadReceiver implements IReceiver { /** * 恢复所有正在下载的任务 + * 1.如果执行队列没有满,则开始下载任务,直到执行队列满 + * 2.如果队列执行队列已经满了,则将所有任务添加到等待队列中 */ public void resumeAllTask() { final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index d3a13ba9..28d447ad 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -352,6 +352,9 @@ public class CommonUtil { * @param size file.length() 获取文件大小 */ public static String formatFileSize(double size) { + if (size < 0) { + return "0kb"; + } double kiloByte = size / 1024; if (kiloByte < 1) { return size + "b"; diff --git a/AriaAnnotations/build.gradle b/AriaAnnotations/build.gradle index 0b581ad5..a9f2139b 100644 --- a/AriaAnnotations/build.gradle +++ b/AriaAnnotations/build.gradle @@ -11,4 +11,4 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) } -//apply from: 'bintray-release.gradle' \ No newline at end of file +apply from: 'bintray-release.gradle' \ No newline at end of file diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java index 4e918479..a09f4178 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java @@ -154,7 +154,7 @@ class ElementHandle { ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build(); StringBuilder sb = new StringBuilder(); sb.append("Set keys = keyMapping.get(\"") - .append(annotation.getSimpleName()) + .append(methodName) .append("\");\n"); sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n") .append("\t\tobj.") diff --git a/README.md b/README.md index 46e552f1..f1e78ebd 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ Aria怎样使用? [![Download](https://api.bintray.com/packages/arialyy/maven/AriaApi/images/download.svg)](https://bintray.com/arialyy/maven/AriaApi/_latestVersion) [![Download](https://api.bintray.com/packages/arialyy/maven/AriaCompiler/images/download.svg)](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion) ```java -compile 'com.arialyy.aria:Aria:3.1.8' -annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.8' +compile 'com.arialyy.aria:Aria:3.1.9' +annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.9' ``` ## 示例 @@ -83,18 +83,18 @@ annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.8' ### 下载状态获取 如果你希望读取下载进度或下载信息,那么你需要创建事件类,并在onResume(Activity、Fragment)或构造函数(Dialog、PopupWindow),将该事件类注册到Aria管理器。 -* 将对象注册到Aria +1. 将对象注册到Aria -` Aria.download(this).register();`或`Aria.upload(this).register();` -```java + `Aria.download(this).register();`或`Aria.upload(this).register();` + ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Aria.download(this).register(); } -``` + ``` -* 使用'@Download'或'@Upload'注解你的函数 +2. 使用`@Download`或`@Upload`注解你的函数 **注意:** - 注解回掉采用Apt的方式实现,所以,你不需要担心这会影响你机器的性能 @@ -102,18 +102,14 @@ annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.8' - 被注解的方法**只能有一个参数,并且参数类型必须是`DownloadTask`或`UploadTask`** - 方法名可以为任意字符串 -* 除了在widget(Activity、Fragment、Dialog、Popupwindow)中使用注解方法外,你还可以在Service、Notification等组件中使用注解函数。 +3. 除了在widget(Activity、Fragment、Dialog、Popupwindow)中使用注解方法外,你还可以在Service、Notification等组件中使用注解函数。 ```java - @Download.onPre + @Download.onPre(DOWNLOAD_URL) protected void onPre(DownloadTask task) {} @Download.onTaskStart - void taskStart(DownloadTask task) { - //通过下载地址可以判断任务是否是你指定的任务 - if (task.getKey().equals(DOWNLOAD_URL)) { - } - } + void taskStart(DownloadTask task) {} @Download.onTaskRunning protected void running(DownloadTask task) {} @@ -137,6 +133,19 @@ annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.8' public void onNoSupportBreakPoint(DownloadTask task) {} ``` +4. 如果你希望对单个任务,或某一些特定任务设置监听器。 + 在注解中添加任务的下载地址,**则表示只有该任务才会触发被注解的方法**。 + + ```java + @Download.onTaskRunning({ + "https://test.xx.apk", + "http://test.xx2.apk" + }) void taskRunning(DownloadTask task) { + mAdapter.setProgress(task.getDownloadEntity()); + } + ``` +在上面的例子中,只有下载地址是`https://test.xx.apk`和`http://test.xx2.apk`才会触发 +`taskRunning(DownloadTask task)`方法。 ### Aria参数配置 #### 配置文件设置参数 @@ -216,24 +225,33 @@ Aria.get(this).getDownloadConfig().setMaxTaskNum(3); ### 常用接口 * 停止所有任务 -```java -Aria.download(this).stopAllTask(); -``` + ```java + Aria.download(this).stopAllTask(); + ``` + +* 恢复所有停止的任务 + + ```java +Aria.download(this).resumeAllTask(); + ``` + * 删除所有任务 -```java -Aria.download(this).removeAllTask(); -``` + ```java + Aria.download(this).removeAllTask(); + ``` + * 获取当前任务的下载速度 速度参数有点特殊,需要[下载事件支持](#下载状态获取) ``` java @Override public void onTaskRunning(DownloadTask task) { - //如果你打开了速度单位转换配置,将可以通过以下方法获取带单位的下载速度,如:1 m/s + //如果你打开了速度单位转换配置,将可以通过以下方法获取带单位的下载速度,如:1 mb/s String convertSpeed = task.getConvertSpeed(); //如果你有自己的单位格式,可以通过以下方法获取原始byte长度 long speed = task.getSpeed(); } ``` + * 获取下载的文件大小、当前进度百分比 同样的,你也可以在DownloadTask对象中获取下载的文件大小 ``` @@ -246,14 +264,13 @@ Aria.download(this).removeAllTask(); int percent = task.getPercent(); } ``` + * 设置高优先级任务 如果你希望优先下载某一个任务,你可以 ``` java Aria.download(this).load(DOWNLOAD_URL).setDownloadPath(PATH).setHighestPriority(); ``` -**tips:为了防止内存泄露的情况,事件类需要使用staic进行修饰** - ## 上传 * 添加任务(只添加,不上传) @@ -304,6 +321,7 @@ Aria.download(this).load(DOWNLOAD_URL).setDownloadPath(PATH).setHighestPriority( ## 开发日志 + + v_3.1.9 修复stopAll队列没有任务时崩溃的问题,增加针对单个任务监听的功能 + v_3.1.7 修复某些文件下载不了的bug,增加apt注解方法,事件获取更加简单了 + v_3.1.6 取消任务时onTaskCancel回调两次的bug + v_3.1.5 优化代码结构,增加优先下载任务功能。 diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index 79717755..6e3b468e 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -211,6 +211,7 @@ public class SingleTaskActivity extends BaseActivity { } @Download.onTaskFail(DOWNLOAD_URL) void taskFail(DownloadTask task) { + L.d(TAG, "task__fail"); mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE); } diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java index 1d26aeaf..16ccf516 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java @@ -24,6 +24,7 @@ import android.widget.TextView; import butterknife.Bind; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.util.CommonUtil; import com.arialyy.simple.R; import com.arialyy.simple.base.adapter.AbsHolder; diff --git a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java index cefec9a0..502a5ce3 100644 --- a/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java @@ -20,12 +20,15 @@ import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; +import android.view.Menu; +import android.view.MenuItem; 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; 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.ActivityMultiDownloadBinding; @@ -35,12 +38,16 @@ import java.util.List; /** * Created by AriaL on 2017/1/6. */ - public class MultiDownloadActivity extends BaseActivity { @Bind(R.id.list) RecyclerView mList; private DownloadAdapter mAdapter; private List mData = new ArrayList<>(); + String[] mFilterStr = new String[] { + "https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk", + "http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk" + }; + @Override protected int setLayoutId() { return R.layout.activity_multi_download; } @@ -58,6 +65,16 @@ public class MultiDownloadActivity extends BaseActivity - + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index e46ea63f..3e0391ff 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:2.2.2' -// classpath 'com.novoda:bintray-release:0.5.0' + 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 @@ -37,7 +37,7 @@ ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' // publishVersion = '0.0.6' - publishVersion = '3.1.8' + publishVersion = '3.1.9' repoName='maven' desc = 'android 下载框架' website = 'https://github.com/AriaLyy/Aria'