diff --git a/Aria/bintray-release.gradle b/Aria/bintray-release.gradle new file mode 100644 index 00000000..37240e31 --- /dev/null +++ b/Aria/bintray-release.gradle @@ -0,0 +1,12 @@ +apply plugin: 'bintray-release' +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 +} \ No newline at end of file diff --git a/Aria/build.gradle b/Aria/build.gradle index 39f2e4fe..0c1d0be7 100644 --- a/Aria/build.gradle +++ b/Aria/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'com.android.library' -apply plugin: 'bintray-release' android { compileSdkVersion 23 @@ -23,53 +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' - -// 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') -//} diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java index 6bad005f..fe6d481b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.command; import android.util.Log; +import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTaskEntity; @@ -34,10 +35,10 @@ class AddCmd extends AbsCmd { if (!canExeCmd) return; ITask task = mQueue.getTask(mEntity.getEntity()); if (task == null) { + mEntity.getEntity().setState(IEntity.STATE_WAIT); mQueue.createTask(mTargetName, mEntity); } else { Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); } } - } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java index 5be974de..ae725b51 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java @@ -48,8 +48,14 @@ public class CmdFactory { * 设置任务为最高优先级 */ public static final int TASK_HIGHEST_PRIORITY = 0x128; - - public static final int TASK_SINGLE = 0x126; + /** + * 停止所有任务 + */ + public static final int TASK_STOP_ALL = 0x129; + /** + * 恢复所有停止的任务 + */ + public static final int TASK_RESUME_ALL = 0x130; private static volatile CmdFactory INSTANCE = null; @@ -70,23 +76,25 @@ public class CmdFactory { * @param target 创建任务的对象 * @param entity 下载实体 * @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link - * #TASK_STOP}、{@link #TASK_HIGHEST_PRIORITY} + * #TASK_STOP}、{@link #TASK_HIGHEST_PRIORITY}、{@link #TASK_STOP_ALL}、{@link #TASK_RESUME_ALL} */ - public AbsCmd createCmd(String target, T entity, int type) { + public AbsCmd createCmd(String target, T entity, int type) { switch (type) { case TASK_CREATE: - return new AddCmd(target, entity); + return new AddCmd<>(target, entity); case TASK_RESUME: case TASK_START: - return new StartCmd(target, entity); + return new StartCmd<>(target, entity); case TASK_CANCEL: - return new CancelCmd(target, entity); + return new CancelCmd<>(target, entity); case TASK_STOP: - return new StopCmd(target, entity); + return new StopCmd<>(target, entity); case TASK_HIGHEST_PRIORITY: - return new HighestPriorityCmd(target, entity); - case TASK_SINGLE: - //return new SingleCmd(target, entity); + return new HighestPriorityCmd<>(target, entity); + case TASK_STOP_ALL: + return new StopAllCmd<>(target, entity); + case TASK_RESUME_ALL: + return new ResumeAllCmd<>(target, entity); default: return null; } 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 new file mode 100644 index 00000000..c7110077 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/command/ResumeAllCmd.java @@ -0,0 +1,51 @@ +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; +import com.arialyy.aria.orm.DbEntity; +import java.util.List; + +/** + * Created by AriaL on 2017/6/13. + * 恢复所有停止的任务 + * 1.如果执行队列没有满,则开始下载任务,直到执行队列满 + * 2.如果队列执行队列已经满了,则将所有任务添加到等待队列中 + */ +final class ResumeAllCmd extends AbsCmd { + /** + * @param targetName 产生任务的对象名 + */ + ResumeAllCmd(String targetName, T entity) { + super(targetName, entity); + } + + @Override public void executeCmd() { + List allEntity = + DbEntity.findDatas(DownloadEntity.class, "state=?", IEntity.STATE_STOP + ""); + for (DownloadEntity entity : allEntity) { + int exeNum = mQueue.getExeTaskNum(); + if (exeNum == 0 || exeNum < mQueue.getMaxTaskNum()) { + ITask task = createTask(entity); + mQueue.startTask(task); + } else { + entity.setState(IEntity.STATE_WAIT); + createTask(entity); + } + } + } + + private ITask createTask(DownloadEntity entity) { + ITask task = mQueue.getTask(entity); + if (task == null) { + DownloadTaskEntity taskEntity = new DownloadTaskEntity(entity); + task = mQueue.createTask(mTargetName, taskEntity); + } else { + Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); + } + return task; + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StopAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StopAllCmd.java new file mode 100644 index 00000000..2c16378e --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StopAllCmd.java @@ -0,0 +1,20 @@ +package com.arialyy.aria.core.command; + +import com.arialyy.aria.core.inf.AbsTaskEntity; + +/** + * Created by AriaL on 2017/6/13. + * 停止所有任务的命令,并清空所有等待队列 + */ +final class StopAllCmd extends AbsCmd { + /** + * @param targetName 产生任务的对象名 + */ + StopAllCmd(String targetName, T entity) { + super(targetName, entity); + } + + @Override public void executeCmd() { + mQueue.stopAllTask(); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/ConnectionHelp.java index dd3efc4d..e05f0aee 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/ConnectionHelp.java @@ -63,38 +63,50 @@ class ConnectionHelp { static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConnection conn) throws ProtocolException { conn.setRequestMethod(entity.requestEnum.name); + Set keys = null; if (entity.headers != null && entity.headers.size() > 0) { - Set keys = entity.headers.keySet(); + keys = entity.headers.keySet(); for (String key : keys) { conn.setRequestProperty(key, entity.headers.get(key)); } } - conn.setRequestProperty("Charset", "UTF-8"); - conn.setRequestProperty("User-Agent", - "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"); + if (keys == null || !keys.contains("Charset")) { + conn.setRequestProperty("Charset", "UTF-8"); + } + if (keys == null || !keys.contains("User-Agent")) { + conn.setRequestProperty("User-Agent", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); + } + if (keys == null || !keys.contains("Accept")) { + StringBuilder accept = new StringBuilder(); + accept.append("image/gif, ") + .append("image/jpeg, ") + .append("image/pjpeg, ") + .append("image/webp, ") + .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()); + } + if (keys == null || !keys.contains("Accept-Encoding")) { + conn.setRequestProperty("Accept-Encoding", "identity"); + } + if (keys == null || !keys.contains("Accept-Charset")) { + conn.setRequestProperty("Accept-Charset", "UTF-8"); + } + if (keys == null || !keys.contains("Connection")) { + conn.setRequestProperty("Connection", "Keep-Alive"); + } //302获取重定向地址 conn.setInstanceFollowRedirects(false); return conn; 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 d315e9b0..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 @@ -17,6 +17,7 @@ package com.arialyy.aria.core.download; import android.support.annotation.NonNull; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.inf.ICmd; import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.command.CmdFactory; import com.arialyy.aria.core.command.AbsCmd; @@ -133,19 +134,23 @@ public class DownloadReceiver implements IReceiver { } /** - * 停止所有正在下载的任务 + * 停止所有正在下载的任务,并清空等待队列。 */ @Override public void stopAllTask() { final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); - List allEntity = DbEntity.findAllData(DownloadEntity.class); - List stopCmds = new ArrayList<>(); - for (DownloadEntity entity : allEntity) { - if (entity.getState() == DownloadEntity.STATE_RUNNING) { - stopCmds.add( - CommonUtil.createCmd(targetName, new DownloadTaskEntity(entity), CmdFactory.TASK_STOP)); - } - } - ariaManager.setCmds(stopCmds).exe(); + ariaManager.setCmd(CmdFactory.getInstance() + .createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_STOP_ALL)).exe(); + } + + /** + * 恢复所有正在下载的任务 + * 1.如果执行队列没有满,则开始下载任务,直到执行队列满 + * 2.如果队列执行队列已经满了,则将所有任务添加到等待队列中 + */ + public void resumeAllTask() { + final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); + ariaManager.setCmd(CmdFactory.getInstance() + .createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_RESUME_ALL)).exe(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java index 9a9fd416..75f6a17b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java @@ -25,6 +25,8 @@ public class DownloadTaskEntity extends AbsTaskEntity { public DownloadEntity downloadEntity; + public DownloadTaskEntity(){} + public DownloadTaskEntity(DownloadEntity downloadEntity) { this.downloadEntity = downloadEntity; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java index 943294c7..2f08ba86 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java @@ -81,7 +81,7 @@ class DownloadUtil implements IDownloadUtil, Runnable { + ".properties"); try { if (!mConfigFile.exists()) { //记录文件被删除,则重新下载 - handleNewTask(); + isNewTask = true; CommonUtil.createFile(mConfigFile.getPath()); } else { isNewTask = !mDownloadFile.exists(); @@ -295,10 +295,6 @@ class DownloadUtil implements IDownloadUtil, Runnable { return; } int fileLength = conn.getContentLength(); - if (fileLength < SUB_LEN) { - THREAD_NUM = 1; - CONSTANCE.THREAD_NUM = THREAD_NUM; - } Properties pro = createConfigFile(fileLength); int blockSize = fileLength / THREAD_NUM; int[] recordL = new int[THREAD_NUM]; @@ -325,7 +321,7 @@ class DownloadUtil implements IDownloadUtil, Runnable { recordL[rl] = i; rl++; } else { - handleNewTask(); + handleNewTask(fileLength); } if (isNewTask) { recordL[rl] = i; @@ -380,7 +376,7 @@ class DownloadUtil implements IDownloadUtil, Runnable { //分配每条线程的下载区间 pro = CommonUtil.loadConfig(mConfigFile); if (pro.isEmpty()) { - handleNewTask(); + handleNewTask(fileLength); } else { Set keys = pro.keySet(); int num = 0; @@ -390,7 +386,7 @@ class DownloadUtil implements IDownloadUtil, Runnable { } } if (num == 0) { - handleNewTask(); + handleNewTask(fileLength); return pro; } THREAD_NUM = num; @@ -400,7 +396,7 @@ class DownloadUtil implements IDownloadUtil, Runnable { if (state != null && Integer.parseInt(state + "") == 1) { continue; } - handleNewTask(); + handleNewTask(fileLength); return pro; } } @@ -412,9 +408,10 @@ class DownloadUtil implements IDownloadUtil, Runnable { /** * 处理新任务 */ - private void handleNewTask() { + private void handleNewTask(long fileLength) { isNewTask = true; - THREAD_NUM = AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); + THREAD_NUM = fileLength < SUB_LEN ? 1 + : AriaManager.getInstance(mContext).getDownloadConfig().getThreadNum(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index a979739a..625d28dd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -17,11 +17,14 @@ package com.arialyy.aria.core.queue; import android.util.Log; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.queue.pool.CachePool; import com.arialyy.aria.core.queue.pool.ExecutePool; +import java.util.Set; /** * Created by lyy on 2017/2/23. @@ -33,6 +36,25 @@ abstract class AbsTaskQueue mCachePool = new CachePool<>(); ExecutePool mExecutePool; + /** + * 停止所有任务 + */ + @Override public void stopAllTask() { + Set exeKeys = mExecutePool.getAllTask().keySet(); + for (String key : exeKeys) { + TASK task = mExecutePool.getAllTask().get(key); + if (task != null && task.isRunning()) task.stop(); + } + Set cacheKeys = mCachePool.getAllTask().keySet(); + for (String key : cacheKeys) { + mExecutePool.removeTask(key); + } + } + + @Override public int getMaxTaskNum() { + return AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); + } + /** * 获取任务执行池 */ @@ -61,7 +83,7 @@ abstract class AbsTaskQueue { + /** + * 停止所有任务 + */ + void stopAllTask(); + /** * 设置任务为最高优先级任务 + * * @param task {@link DownloadTask}、{@link UploadTask} */ void setTaskHighestPriority(TASK task); @@ -67,9 +73,9 @@ public interface ITaskQueue * + * {@literal @}Download.onPre(DownloadUrl) * protected void onPre(DownloadTask task) { - * if (task.getKey().equals(DOWNLOAD_URL)) { - * mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()).sendToTarget(); - * } + * mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()).sendToTarget(); * } * * + * {@literal @}Download.onPre("http://www.baidu.com"),如果你的注解中增加了url描述, + * 则表示,所有下载任务中,只有下载地址为"http://www.baidu.com"的任务才能回调该注解的方法。 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Download { /** * 如果你在方法中添加{@code @Download.onPre}注解,在预处理完成时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Download.onTaskPre}注解,在任务预处理完成时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Download.onTaskResume}注解,在任务恢复下载时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { + String[] value() default { AriaConstance.NO_URL }; } + /** * 如果你在方法中添加{@code @Download.onTaskStart}注解,在任务开始下载时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Download.onTaskStop}注解,在任务停止时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Download.onTaskCancel}l注解,在任务取消时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Download.onTaskFail)注解,在任务预失败时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Download.onTaskComplete}注解,在任务完成时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Download.onTaskRunning}注解,在任务正在下载,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning { + String[] value() default { AriaConstance.NO_URL }; } /** @@ -92,5 +103,6 @@ import java.lang.annotation.Target; */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onNoSupportBreakPoint { + String[] value() default { AriaConstance.NO_URL }; } } diff --git a/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java b/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java index bf1a2cd1..7906bcc7 100644 --- a/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java +++ b/AriaAnnotations/src/main/java/com/arialyy/annotations/Upload.java @@ -25,11 +25,14 @@ import java.lang.annotation.Target; * Aria下载事件被注解的方法中,参数仅能有一个,参数类型为{@link com.arialyy.aria.core.upload.UploadTask} *
  *   
+ *      {@literal @}Upload.onPre
  *       protected void onPre(UploadTask task) {
  *        L.d(TAG, "fileSize = " + task.getConvertFileSize());
  *       }
  *   
  * 
+ * {@literal @}Upload.onPre("/mnt/sdcard/test.zip"),如果你的注解中增加了path描述, + * 则表示,所有上传任务中,只有上传路径为"/mnt/sdcard/test.zip"的任务才能回调该注解的方法。 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface Upload { @@ -37,12 +40,14 @@ import java.lang.annotation.Target; * 如果你在方法中添加{@code @Upload.onPre}注解,在预处理完成时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onPre { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Upload.onTaskPre}注解,在任务预处理完成时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskPre { + String[] value() default { AriaConstance.NO_URL }; } /** @@ -50,40 +55,47 @@ import java.lang.annotation.Target; */ //@Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskResume { //} + /** * 如果你在方法中添加{@code @Upload.onTaskStart}注解,在任务开始下载时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStart { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Upload.onTaskStop}注解,在任务停止时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskStop { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Upload.onTaskCancel}l注解,在任务取消时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskCancel { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Upload.onTaskFail)注解,在任务预失败时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskFail { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Upload.onTaskComplete}注解,在任务完成时,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskComplete { + String[] value() default { AriaConstance.NO_URL }; } /** * 如果你在方法中添加{@code @Upload.onTaskRunning}注解,在任务正在下载,Aria会调用该方法 */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onTaskRunning { + String[] value() default { AriaConstance.NO_URL }; } /** @@ -91,5 +103,6 @@ import java.lang.annotation.Target; */ @Retention(RetentionPolicy.CLASS) @Target(ElementType.METHOD) public @interface onNoSupportBreakPoint { + String[] value() default { AriaConstance.NO_URL }; } } diff --git a/AriaCompiler/bintray-release.gradle b/AriaCompiler/bintray-release.gradle new file mode 100644 index 00000000..7fb12803 --- /dev/null +++ b/AriaCompiler/bintray-release.gradle @@ -0,0 +1,12 @@ +apply plugin: 'bintray-release' +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 +} \ No newline at end of file diff --git a/AriaCompiler/build.gradle b/AriaCompiler/build.gradle index 3e915676..a76e8659 100644 --- a/AriaCompiler/build.gradle +++ b/AriaCompiler/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'java' -apply plugin: 'bintray-release' tasks.withType(JavaCompile) { options.encoding = "UTF-8" @@ -15,14 +14,4 @@ dependencies { 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 -} \ 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 3ec58d74..a09f4178 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ElementHandle.java @@ -18,6 +18,7 @@ package com.arialyy.compiler; import com.arialyy.annotations.Download; import com.arialyy.annotations.Upload; import com.squareup.javapoet.ClassName; +import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.FieldSpec; import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.MethodSpec; @@ -26,7 +27,9 @@ import com.squareup.javapoet.TypeSpec; import com.squareup.javapoet.ParameterizedTypeName; import java.io.IOException; import java.lang.annotation.Annotation; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -63,40 +66,32 @@ class ElementHandle { * 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); + saveMethod(true, roundEnv, Download.onNoSupportBreakPoint.class, + ProxyConstance.DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT); + saveMethod(true, roundEnv, Download.onPre.class, ProxyConstance.DOWNLOAD_PRE); + saveMethod(true, roundEnv, Download.onTaskCancel.class, ProxyConstance.DOWNLOAD_TASK_CANCEL); + saveMethod(true, roundEnv, Download.onTaskComplete.class, + ProxyConstance.DOWNLOAD_TASK_COMPLETE); + saveMethod(true, roundEnv, Download.onTaskFail.class, ProxyConstance.DOWNLOAD_TASK_FAIL); + saveMethod(true, roundEnv, Download.onTaskPre.class, ProxyConstance.DOWNLOAD_TASK_PRE); + saveMethod(true, roundEnv, Download.onTaskResume.class, ProxyConstance.DOWNLOAD_TASK_RESUME); + saveMethod(true, roundEnv, Download.onTaskRunning.class, ProxyConstance.DOWNLOAD_TASK_RUNNING); + saveMethod(true, roundEnv, Download.onTaskStart.class, ProxyConstance.DOWNLOAD_TASK_START); + saveMethod(true, roundEnv, Download.onTaskStop.class, ProxyConstance.DOWNLOAD_TASK_STOP); } 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.onNoSupportBreakPoint.class, + ProxyConstance.UPLOAD_TASK_NO_SUPPORT_BREAKPOINT); + saveMethod(false, roundEnv, Upload.onPre.class, ProxyConstance.UPLOAD_PRE); + saveMethod(false, roundEnv, Upload.onTaskCancel.class, ProxyConstance.UPLOAD_TASK_CANCEL); + saveMethod(false, roundEnv, Upload.onTaskComplete.class, ProxyConstance.UPLOAD_TASK_COMPLETE); + saveMethod(false, roundEnv, Upload.onTaskFail.class, ProxyConstance.UPLOAD_TASK_FAIL); + saveMethod(false, roundEnv, Upload.onTaskPre.class, ProxyConstance.UPLOAD_TASK_PRE); //saveMethod(false, roundEnv, Upload.onTaskResume.class); - saveMethod(false, roundEnv, Upload.onTaskRunning.class); - saveMethod(false, roundEnv, Upload.onTaskStart.class); - saveMethod(false, roundEnv, Upload.onTaskStop.class); - } - - void printMethods() { - //Set keys = mMethods.keySet(); - //for (String key : keys) { - // ProxyEntity entity = mMethods.get(key); - // for (String method : entity.methods) { - // PrintLog.getInstance().info(method); - // } - //} - PrintLog.getInstance().info("size ==> " + mMethods.size()); + saveMethod(false, roundEnv, Upload.onTaskRunning.class, ProxyConstance.UPLOAD_TASK_RUNNING); + saveMethod(false, roundEnv, Upload.onTaskStart.class, ProxyConstance.UPLOAD_TASK_START); + saveMethod(false, roundEnv, Upload.onTaskStop.class, ProxyConstance.UPLOAD_TASK_STOP); } /** @@ -157,16 +152,29 @@ class ElementHandle { isDownload ? "DownloadTask" : "UploadTask"); ParameterSpec parameterSpec = ParameterSpec.builder(task, "task").addModifiers(Modifier.FINAL).build(); + StringBuilder sb = new StringBuilder(); + sb.append("Set keys = keyMapping.get(\"") + .append(methodName) + .append("\");\n"); + sb.append("if (keys != null) {\n\tif (keys.contains(task.getKey())) {\n") + .append("\t\tobj.") + .append(methodName) + .append("(") + .append(isDownload ? "(DownloadTask)" : "(UploadTask)") + .append("task);\n") + .append("\t}\n} else {\n") + .append("\tobj.") + .append(methodName) + .append("(") + .append(isDownload ? "(DownloadTask)" : "(UploadTask)") + .append("task);\n}\n"); + return MethodSpec.methodBuilder(annotation.getSimpleName()) .addModifiers(Modifier.PUBLIC) .returns(void.class) .addParameter(parameterSpec) .addAnnotation(Override.class) - .addCode("obj." - + methodName - + "(" - + (isDownload ? "(DownloadTask)" : "(UploadTask)") - + "task);\n") + .addCode(sb.toString()) .build(); } @@ -184,6 +192,13 @@ class ElementHandle { FieldSpec observerField = FieldSpec.builder(obj, "obj").addModifiers(Modifier.PRIVATE).build(); builder.addField(observerField); + //添加url映射表 + FieldSpec mappingField = FieldSpec.builder( + ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), + ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.get(String.class))), + "keyMapping").addModifiers(Modifier.PRIVATE).initializer("new $T()", HashMap.class).build(); + builder.addField(mappingField); + //添加注解方法 for (Class annotation : entity.methods.keySet()) { MethodSpec method = @@ -191,6 +206,26 @@ class ElementHandle { builder.addMethod(method); } + //增加构造函数 + CodeBlock.Builder cb = CodeBlock.builder(); + cb.add("Set set = null;\n"); + for (String methodName : entity.keyMappings.keySet()) { + Set keys = entity.keyMappings.get(methodName); + if (keys == null || keys.size() == 0) continue; + StringBuilder sb = new StringBuilder(); + sb.append("set = new $T();\n"); + for (String key : keys) { + if (key.isEmpty()) continue; + sb.append("set.add(\"").append(key).append("\");\n"); + } + + sb.append("keyMapping.put(\"").append(methodName).append("\", ").append("set);\n"); + cb.add(sb.toString(), ClassName.get(HashSet.class)); + } + MethodSpec structure = + MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addCode(cb.build()).build(); + builder.addMethod(structure); + //添加设置代理的类 ParameterSpec parameterSpec = ParameterSpec.builder(Object.class, "obj").addModifiers(Modifier.FINAL).build(); @@ -222,7 +257,7 @@ class ElementHandle { * 查找并保存扫描到的方法 */ private void saveMethod(boolean isDownload, RoundEnvironment roundEnv, - Class annotationClazz) { + Class annotationClazz, int annotationType) { for (Element element : roundEnv.getElementsAnnotatedWith(annotationClazz)) { ElementKind kind = element.getKind(); if (kind == ElementKind.METHOD) { @@ -236,16 +271,92 @@ class ElementHandle { 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); + proxyEntity.keyMappings.put(methodName, getValues(method, isDownload, annotationType)); } } } + /** + * 获取注解的内容 + */ + private Set getValues(ExecutableElement method, boolean isDownload, int annotationType) { + String[] keys = null; + if (isDownload) { + switch (annotationType) { + case ProxyConstance.DOWNLOAD_PRE: + keys = method.getAnnotation(Download.onPre.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_PRE: + keys = method.getAnnotation(Download.onTaskPre.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_RESUME: + keys = method.getAnnotation(Download.onTaskResume.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_START: + keys = method.getAnnotation(Download.onTaskStart.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_RUNNING: + keys = method.getAnnotation(Download.onTaskRunning.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_STOP: + keys = method.getAnnotation(Download.onTaskStop.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_COMPLETE: + keys = method.getAnnotation(Download.onTaskComplete.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_CANCEL: + keys = method.getAnnotation(Download.onTaskCancel.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_FAIL: + keys = method.getAnnotation(Download.onTaskFail.class).value(); + break; + case ProxyConstance.DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT: + keys = method.getAnnotation(Download.onNoSupportBreakPoint.class).value(); + break; + } + } else { + switch (annotationType) { + case ProxyConstance.UPLOAD_PRE: + keys = method.getAnnotation(Upload.onPre.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_PRE: + keys = method.getAnnotation(Upload.onTaskPre.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_RESUME: + //keys = method.getAnnotation(Upload.onTaskResume.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_START: + keys = method.getAnnotation(Upload.onTaskStart.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_RUNNING: + keys = method.getAnnotation(Upload.onTaskRunning.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_STOP: + keys = method.getAnnotation(Upload.onTaskStop.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_COMPLETE: + keys = method.getAnnotation(Upload.onTaskComplete.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_CANCEL: + keys = method.getAnnotation(Upload.onTaskCancel.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_FAIL: + keys = method.getAnnotation(Upload.onTaskFail.class).value(); + break; + case ProxyConstance.UPLOAD_TASK_NO_SUPPORT_BREAKPOINT: + keys = method.getAnnotation(Upload.onNoSupportBreakPoint.class).value(); + break; + } + } + + return keys == null ? null : convertSet(keys); + } + /** * 检查和下载相关的方法,如果被注解的方法为private或参数不合法,则抛异常 */ @@ -272,6 +383,21 @@ class ElementHandle { } } + /** + * 字符串数组转set + * + * @param keys 注解中查到的key + */ + private Set convertSet(final String[] keys) { + if (keys == null || keys.length == 0) { + return null; + } + if (keys[0].isEmpty()) return null; + Set set = new HashSet<>(); + Collections.addAll(set, keys); + return set; + } + private String getCheckParams(boolean isDownload) { return isDownload ? "com.arialyy.aria.core.download.DownloadTask" : "com.arialyy.aria.core.upload.UploadTask"; diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java index 7e9ac1ea..6ea65c33 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyConstance.java @@ -35,4 +35,25 @@ public interface ProxyConstance { */ String UPLOAD_PROXY_CLASS_SUFFIX = "$$UploadListenerProxy"; + int DOWNLOAD_PRE = 0X11; + int DOWNLOAD_TASK_PRE = 0X12; + int DOWNLOAD_TASK_RESUME = 0X13; + int DOWNLOAD_TASK_START = 0X14; + int DOWNLOAD_TASK_STOP = 0X15; + int DOWNLOAD_TASK_CANCEL = 0X16; + int DOWNLOAD_TASK_FAIL = 0X17; + int DOWNLOAD_TASK_COMPLETE = 0X18; + int DOWNLOAD_TASK_RUNNING = 0X19; + int DOWNLOAD_TASK_NO_SUPPORT_BREAKPOINT = 0X1A; + + int UPLOAD_PRE = 0X11; + int UPLOAD_TASK_PRE = 0X12; + int UPLOAD_TASK_RESUME = 0X13; + int UPLOAD_TASK_START = 0X14; + int UPLOAD_TASK_STOP = 0X15; + int UPLOAD_TASK_CANCEL = 0X16; + int UPLOAD_TASK_FAIL = 0X17; + int UPLOAD_TASK_COMPLETE = 0X18; + int UPLOAD_TASK_RUNNING = 0X19; + int UPLOAD_TASK_NO_SUPPORT_BREAKPOINT = 0X1A; } diff --git a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java index 9c6e9070..41573cfc 100644 --- a/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java +++ b/AriaCompiler/src/main/java/com/arialyy/compiler/ProxyEntity.java @@ -18,6 +18,7 @@ package com.arialyy.compiler; import java.lang.annotation.Annotation; import java.util.HashMap; import java.util.Map; +import java.util.Set; /** * Created by Aria.Lao on 2017/6/7. @@ -27,6 +28,6 @@ class ProxyEntity { public String packageName; public String className; public boolean isDownlaod = true; - + public Map> keyMappings = new HashMap<>(); public Map, String> methods = new HashMap<>(); } diff --git a/README.md b/README.md index d976efcc..1f3ab442 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,13 @@ 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 +<<<<<<< HEAD compile 'com.arialyy.aria:aria-core: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' +>>>>>>> v_3.0 ``` ## 示例 @@ -81,19 +86,29 @@ annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.8' ``` ### 下载状态获取 +<<<<<<< HEAD 如果你希望读取下载进度或下载信息,那么你需要 * 将对象注册到Aria +======= +如果你希望读取下载进度或下载信息,那么你需要创建事件类,并在onResume(Activity、Fragment)或构造函数(Dialog、PopupWindow),将该事件类注册到Aria管理器。 -` Aria.download(this).register();`或`Aria.upload(this).register();` -```java +1. 将对象注册到Aria +>>>>>>> v_3.0 + + `Aria.download(this).register();`或`Aria.upload(this).register();` + ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Aria.download(this).register(); } -``` + ``` +<<<<<<< HEAD * 使用`@Download`或`@Upload`注解你的函数 +======= +2. 使用`@Download`或`@Upload`注解你的函数 +>>>>>>> v_3.0 **注意:** - 注解回掉采用Apt的方式实现,所以,你不需要担心这会影响你机器的性能 @@ -101,18 +116,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) {} @@ -136,6 +147,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参数配置 #### 配置文件设置参数 @@ -215,24 +239,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对象中获取下载的文件大小 ``` @@ -245,6 +278,7 @@ Aria.download(this).removeAllTask(); int percent = task.getPercent(); } ``` + * 设置高优先级任务 如果你希望优先下载某一个任务,你可以 ``` java @@ -258,8 +292,6 @@ tip: 如果你数据比较多,或者数据比较复杂,你可以先把数据 Aria.download(this).load(DOWNLOAD_URL).setExtendField(str) ``` -**tips:为了防止内存泄露的情况,事件类需要使用staic进行修饰** - ## 上传 * 添加任务(只添加,不上传) @@ -310,6 +342,7 @@ Aria.download(this).load(DOWNLOAD_URL).setExtendField(str) ## 开发日志 + + 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/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c2b2e5e9..b91acce7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -30,6 +30,7 @@ + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 522cfa0c..f9ed36f3 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -4,7 +4,7 @@ - + @@ -20,7 +20,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/MainActivity.java b/app/src/main/java/com/arialyy/simple/MainActivity.java index 69cd6d6e..7b315dab 100644 --- a/app/src/main/java/com/arialyy/simple/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/MainActivity.java @@ -26,6 +26,7 @@ import com.arialyy.aria.core.Aria; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityMainBinding; import com.arialyy.simple.download.DownloadActivity; +import com.arialyy.simple.test.TestMutilTaskSysDownload; import com.arialyy.simple.upload.UploadActivity; /** @@ -51,4 +52,8 @@ public class MainActivity extends BaseActivity { @OnClick(R.id.upload) public void uploadDemo() { startActivity(new Intent(this, UploadActivity.class)); } + + @OnClick(R.id.multi_test) public void mutliTest() { + startActivity(new Intent(this, TestMutilTaskSysDownload.class)); + } } 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 144b96c3..6e3b468e 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -63,8 +63,8 @@ public class SingleTaskActivity extends BaseActivity { //"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://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815"; - //"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"; //不支持断点的链接 @@ -182,63 +182,46 @@ public class SingleTaskActivity extends BaseActivity { return true; } - @Download.onPre protected void onPre(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()) - .sendToTarget(); - } + @Download.onPre(DOWNLOAD_URL) protected void onPre(DownloadTask task) { + 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.onTaskStart(DOWNLOAD_URL) void taskStart(DownloadTask task) { + 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.onTaskRunning(DOWNLOAD_URL) protected void running(DownloadTask task) { + 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.onTaskResume(DOWNLOAD_URL) void taskResume(DownloadTask task) { + 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.onTaskStop(DOWNLOAD_URL) void taskStop(DownloadTask task) { + 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.onTaskCancel(DOWNLOAD_URL) void taskCancel(DownloadTask task) { + 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.onTaskFail(DOWNLOAD_URL) void taskFail(DownloadTask task) { + L.d(TAG, "task__fail"); + mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE); } - @Download.onTaskComplete void taskComplete(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE); - } + @Download.onTaskComplete(DOWNLOAD_URL) void taskComplete(DownloadTask task) { + mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE); } - @Download.onNoSupportBreakPoint public void onNoSupportBreakPoint(DownloadTask task) { - if (task.getKey().equals(DOWNLOAD_URL)) { - T.showShort(SingleTaskActivity.this, "该下载链接不支持断点"); - } + @Download.onNoSupportBreakPoint(DOWNLOAD_URL) + public void onNoSupportBreakPoint(DownloadTask task) { + T.showShort(SingleTaskActivity.this, "该下载链接不支持断点"); } @Override protected int setLayoutId() { diff --git a/app/src/main/java/com/arialyy/simple/download/fragment_download/FragmentActivity.java b/app/src/main/java/com/arialyy/simple/download/fragment_download/FragmentActivity.java index 9b3c48cb..93ab023f 100644 --- a/app/src/main/java/com/arialyy/simple/download/fragment_download/FragmentActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/fragment_download/FragmentActivity.java @@ -16,6 +16,7 @@ package com.arialyy.simple.download.fragment_download; +import android.os.Bundle; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.FragmentDownloadBinding; @@ -28,4 +29,9 @@ public class FragmentActivity extends BaseActivity { @Override protected int setLayoutId() { return R.layout.activity_fragment; } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + setTile("Fragment 中使用"); + } } 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 d05e51a4..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; @@ -152,8 +153,8 @@ public class DownloadAdapter extends AbsRVAdapter { @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 { + + @Override protected int setLayoutId() { + return R.layout.test_activity_multi; + } + + public void onClick(View view) { + String baseUrl = "http://file.bmob.cn/"; + String[] urlArray = { + "M02/3B/A4/oYYBAFaOeUSAc1QiAAFTbmA7AHs052.jpg", + "M02/3B/A4/oYYBAFaOeUaAfYC-AAFD8zf9NXc879.jpg", + "M02/3B/A4/oYYBAFaOeUuAOxhnAACSdmbqSac702.jpg", + "M02/3B/A4/oYYBAFaOeU2AFAIGAAFICximvXc924.jpg", + "M02/3B/A4/oYYBAFaOeVCAPWMQAAFm2KWCq_E721.jpg", + "M02/3B/A4/oYYBAFaOeVOAbiv9AAFfCTTgr94948.jpg", + "M02/3B/A4/oYYBAFaOeVaAMR3tAAFf3yTuuCM577.jpg", + "M02/3B/A4/oYYBAFaOeVmACEWhAAEt72ecbpg468.jpg", + "M02/3B/A4/oYYBAFaOeVyAHHt4AAFg9e9bRio507.jpg", + "M02/3B/A4/oYYBAFaOeV-AClYXAAESLGY0gag424.jpg", + "M02/3B/A4/oYYBAFaOeWKAA7N0AAF3omYOJUI703.jpg", + "M02/3B/A4/oYYBAFaOeWWAD2lrAAFN7eRFxBs575.jpg", + "M02/3B/A4/oYYBAFaOeWiAdCVEAAFg4273Dus313.jpg", + "M02/3B/A4/oYYBAFaOeWyAJDm5AAF8JVoGVb0705.jpg", + "M02/3B/A4/oYYBAFaOeW-AUoA8AAGjKiHkXUo181.jpg", + "M02/3B/A4/oYYBAFaOeXKABIamAAFU7J7vraE265.jpg", + "M02/3B/A5/oYYBAFaOeXaAW09jAAFf37qdwDA457.jpg", + "M02/3B/A5/oYYBAFaOeXmAWmS7AAFtLNpWjgo967.jpg", + "M02/3B/A5/oYYBAFaOeX2AQf9cAAF2fhwS2UE145.jpg", + "M02/3B/A5/oYYBAFaOeYCAKGnLAAFVAzks-qU937.jpg", + "M02/3B/A5/oYYBAFaOeYOAMODNAAF6HjTTMq4819.jpg", + "M02/3B/A5/oYYBAFaOeYeAbn8uAAFLSQLw48Q042.jpg", + "M02/3B/A5/oYYBAFaOeYqAMJThAAFtrNe4UNM047.jpg", + "M02/3B/A5/oYYBAFaOeY2AbnQvAAFNSXWn0Dc026.jpg", + "M02/3B/A5/oYYBAFaOeZCAIsr0AAFHZFEVhPc682.jpg", + "M02/3B/A5/oYYBAFaOeZOAGvITAAFqPmfcc9c471.jpg", + "M02/3B/A5/oYYBAFaOeZaATvjbAAFHDmALnhE003.jpg", + "M02/3B/A5/oYYBAFaOeZmAJPuVAAFfPJC2wsE319.jpg", + "M02/3B/A5/oYYBAFaOeZyAXtAmAAFfArJNwtM371.jpg", + "M02/3B/A5/oYYBAFaOeZ-AGZN0AAFgqwYYCS8004.jpg", + "M02/3B/A5/oYYBAFaOeaOAbbrGAAFcq59JjUo205.jpg", + "M02/3B/A5/oYYBAFaOeaSAdFyoAACaxVxgUJA092.jpg" + }; + int maxNum = Aria.get(this).getDownloadConfig().getMaxTaskNum(); + for (int i = 0; i < urlArray.length; i++) { + if (i < maxNum) { + + Aria.download(this) + .load(baseUrl + urlArray[i]) + .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg") + //.addHeader("Accept-Encoding", "gzip,deflate,sdcn") + .start(); + } else { + Aria.download(this) + .load(baseUrl + urlArray[i]) + .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg") + //.addHeader("Accept-Encoding", "gzip,deflate,sdcn") + .add(); + } + } + } +} diff --git a/app/src/main/res/layout/activity_fragment.xml b/app/src/main/res/layout/activity_fragment.xml index 80976a7a..7e467096 100644 --- a/app/src/main/res/layout/activity_fragment.xml +++ b/app/src/main/res/layout/activity_fragment.xml @@ -6,6 +6,7 @@ android:layout_height="match_parent" android:orientation="vertical" > + + +