From 5ee2cba33186ad00f648da188ba0520d63cfc9e5 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Fri, 2 Jun 2017 17:38:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=9C=80=E9=AB=98=E4=BC=98?= =?UTF-8?q?=E5=85=88=E7=BA=A7=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/ConfigHelper.java | 2 +- .../com/arialyy/aria/core/Configuration.java | 2 +- .../com/arialyy/aria/core/RequestEnum.java | 2 +- .../arialyy/aria/core/WidgetLiftManager.java | 2 +- .../com/arialyy/aria/core/command/AbsCmd.java | 4 +- .../com/arialyy/aria/core/command/AddCmd.java | 2 +- .../arialyy/aria/core/command/CancelCmd.java | 5 +- .../arialyy/aria/core/command/CmdFactory.java | 53 ++---- .../aria/core/command/HighestPriorityCmd.java | 53 ++++++ .../arialyy/aria/core/command/StartCmd.java | 9 +- .../arialyy/aria/core/command/StopCmd.java | 2 +- .../aria/core/download/DownloadListener.java | 3 + .../aria/core/download/DownloadTarget.java | 13 ++ .../aria/core/download/DownloadTask.java | 9 + .../core/download/DownloadTaskEntity.java | 2 +- .../aria/core/download/DownloadUtil.java | 1 - .../aria/core/download/IDownloadUtil.java | 2 +- .../aria/core/download/SingleThreadTask.java | 2 +- .../com/arialyy/aria/core/inf/AbsTarget.java | 16 +- .../java/com/arialyy/aria/core/inf/ICmd.java | 2 +- .../com/arialyy/aria/core/inf/IEntity.java | 2 +- .../com/arialyy/aria/core/inf/IReceiver.java | 2 +- .../java/com/arialyy/aria/core/inf/ITask.java | 14 +- .../arialyy/aria/core/inf/ITaskEntity.java | 2 +- .../arialyy/aria/core/queue/AbsTaskQueue.java | 6 +- .../aria/core/queue/DownloadTaskQueue.java | 37 +++- .../arialyy/aria/core/queue/ITaskQueue.java | 6 + .../aria/core/queue/UploadTaskQueue.java | 2 +- .../aria/core/queue/pool/ExecutePool.java | 3 + .../scheduler/IDownloadSchedulerListener.java | 2 +- .../aria/core/scheduler/ISchedulers.java | 2 +- .../aria/core/scheduler/UploadSchedulers.java | 2 +- .../aria/core/upload/IUploadListener.java | 2 +- .../aria/core/upload/UploadEntity.java | 2 +- .../aria/core/upload/UploadListener.java | 2 +- .../aria/core/upload/UploadReceiver.java | 2 +- .../aria/core/upload/UploadTarget.java | 2 +- .../arialyy/aria/core/upload/UploadTask.java | 11 +- .../aria/core/upload/UploadTaskEntity.java | 2 +- .../arialyy/aria/core/upload/UploadUtil.java | 2 +- .../arialyy/aria/exception/FileException.java | 2 +- .../java/com/arialyy/aria/orm/DBConfig.java | 2 +- .../java/com/arialyy/aria/orm/DbUtil.java | 2 +- .../arialyy/aria/util/CAConfiguration.java | 2 +- .../java/com/arialyy/aria/util/CheckUtil.java | 9 +- .../java/com/arialyy/aria/util/FileUtil.java | 2 +- .../com/arialyy/aria/util/ReflectionUtil.java | 2 +- .../com/arialyy/aria/util/SSLContextUtil.java | 2 +- .../java/com/arialyy/aria/util/Speed.java | 2 +- .../aria/window/AriaFileChangeActivity.java | 4 +- .../aria/window/FileChangeAdapter.java | 2 +- .../com/arialyy/aria/window/FileEntity.java | 4 +- README.md | 8 +- .../simple/download/DownloadModule.java | 7 + .../download/HighestPriorityActivity.java | 169 ++++++++++++++++++ .../simple/download/SingleTaskActivity.java | 70 ++------ .../multi_download/DownloadAdapter.java | 4 +- .../res/layout/activity_download_mean.xml | 9 + .../res/layout/activity_highest_priority.xml | 47 +++++ 59 files changed, 475 insertions(+), 165 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java create mode 100644 app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java create mode 100644 app/src/main/res/layout/activity_highest_priority.xml diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index 28065ee7..ccbe918d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -22,7 +22,7 @@ import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** - * Created by Aria.Lao on 2017/5/22. + * Created by lyy on 2017/5/22. * 读取配置文件 */ public class ConfigHelper extends DefaultHandler { diff --git a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java index 8bdc1c30..08cafdb9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -25,7 +25,7 @@ import java.util.List; import java.util.Properties; /** - * Created by AriaL on 2016/12/8. + * Created by lyy on 2016/12/8. * 信息配置 */ class Configuration { diff --git a/Aria/src/main/java/com/arialyy/aria/core/RequestEnum.java b/Aria/src/main/java/com/arialyy/aria/core/RequestEnum.java index 4d3766d5..71b82ecd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/RequestEnum.java +++ b/Aria/src/main/java/com/arialyy/aria/core/RequestEnum.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core; /** - * Created by Aria.Lao on 2017/1/23. + * Created by lyy on 2017/1/23. * url请求方式,目前支持GET、POST */ public enum RequestEnum { diff --git a/Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java b/Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java index f604c93f..b8903c91 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java @@ -24,7 +24,7 @@ import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; /** - * Created by Aria.Lao on 2017/2/7. + * Created by lyy on 2017/2/7. * 为组件添加生命周期 */ final class WidgetLiftManager { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java index 79f9b104..3a93f415 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java @@ -38,13 +38,13 @@ public abstract class AbsCmd implements ICmd { /** * 能否执行命令 */ - boolean cancelExe = true; + boolean canExeCmd = true; /** * @param targetName 产生任务的对象名 */ AbsCmd(String targetName, T entity) { - cancelExe = CheckUtil.checkCmdEntity(entity, + canExeCmd = CheckUtil.checkCmdEntity(entity, !(this instanceof CancelCmd) || !(this instanceof StopCmd)); mTargetName = targetName; mEntity = entity; 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 6db19781..0048d520 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 @@ -31,7 +31,7 @@ class AddCmd extends AbsCmd { } @Override public void executeCmd() { - if (!cancelExe) return; + if (!canExeCmd) return; ITask task = mQueue.getTask(mEntity.getEntity()); if (task == null) { mQueue.createTask(mTargetName, mEntity); diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java index 92ea7b42..ee474d01 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.command; +import android.text.TextUtils; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ITaskEntity; @@ -29,13 +30,13 @@ class CancelCmd extends AbsCmd { } @Override public void executeCmd() { - if (!cancelExe) return; + if (!canExeCmd) return; ITask task = mQueue.getTask(mEntity.getEntity()); if (task == null) { task = mQueue.createTask(mTargetName, mEntity); } if (task != null) { - if (mTargetName != null) { + if (!TextUtils.isEmpty(mTargetName)) { task.setTargetName(mTargetName); } mQueue.cancelTask(task); 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 a9e7efc9..84f9641a 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 @@ -43,6 +43,11 @@ public class CmdFactory { * 停止任务 */ public static final int TASK_STOP = 0x125; + /** + * 设置任务为最高优先级 + */ + public static final int TASK_HIGHEST_PRIORITY = 0x128; + public static final int TASK_SINGLE = 0x126; private static final Object LOCK = new Object(); @@ -65,59 +70,25 @@ public class CmdFactory { * @param target 创建任务的对象 * @param entity 下载实体 * @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link - * #TASK_STOP} + * #TASK_STOP}、{@link #TASK_HIGHEST_PRIORITY} */ public AbsCmd createCmd(String target, T entity, int type) { switch (type) { case TASK_CREATE: - return createAddCmd(target, entity); + return new AddCmd(target, entity); case TASK_RESUME: case TASK_START: - return createStartCmd(target, entity); + return new StartCmd(target, entity); case TASK_CANCEL: - return createCancelCmd(target, entity); + return new CancelCmd(target, entity); case TASK_STOP: - return createStopCmd(target, entity); + return new StopCmd(target, entity); + case TASK_HIGHEST_PRIORITY: + return new HighestPriorityCmd(target, entity); case TASK_SINGLE: //return new SingleCmd(target, entity); default: return null; } } - - /** - * 创建停止命令 - * - * @return {@link StopCmd} - */ - private StopCmd createStopCmd(String target, T entity) { - return new StopCmd(target, entity); - } - - /** - * 创建下载任务命令 - * - * @return {@link AddCmd} - */ - private AddCmd createAddCmd(String target, T entity) { - return new AddCmd(target, entity); - } - - /** - * 创建启动下载命令 - * - * @return {@link StartCmd} - */ - private StartCmd createStartCmd(String target, T entity) { - return new StartCmd(target, entity); - } - - /** - * 创建启动下载命令 - * - * @return {@link StartCmd} - */ - private CancelCmd createCancelCmd(String target, T entity) { - return new CancelCmd(target, entity); - } } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java new file mode 100644 index 00000000..0f100a84 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java @@ -0,0 +1,53 @@ +/* + * 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.command; + +import android.text.TextUtils; +import com.arialyy.aria.core.inf.ITask; +import com.arialyy.aria.core.inf.ITaskEntity; + +/** + * Created by lyy on 2017/6/2. + * 最高优先级命令,最高优先级命令有以下属性 + * 1、在下载队列中,有且只有一个最高优先级任务 + * 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 + * 3、任务调度器不会暂停最高优先级任务 + * 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 + * 5、如果下载队列中已经满了,则会停止队尾的任务 + * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 + */ +final class HighestPriorityCmd extends AbsCmd { + /** + * @param targetName 产生任务的对象名 + */ + HighestPriorityCmd(String targetName, T entity) { + super(targetName, entity); + } + + @Override public void executeCmd() { + if (!canExeCmd) return; + ITask task = mQueue.getTask(mEntity.getEntity()); + if (task == null) { + task = mQueue.createTask(mTargetName, mEntity); + } + if (task != null) { + if (!TextUtils.isEmpty(mTargetName)) { + task.setTargetName(mTargetName); + } + task.setHighestPriority(true); + } + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java index f6598e03..ce60242f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.command; -import android.util.Log; +import android.text.TextUtils; import com.arialyy.aria.core.inf.ITask; import com.arialyy.aria.core.inf.ITaskEntity; @@ -31,15 +31,16 @@ class StartCmd extends AbsCmd { } @Override public void executeCmd() { - if (!cancelExe) return; + if (!canExeCmd) return; ITask task = mQueue.getTask(mEntity.getEntity()); if (task == null) { task = mQueue.createTask(mTargetName, mEntity); } if (task != null) { - task.setTargetName(mTargetName); + if (!TextUtils.isEmpty(mTargetName)) { + task.setTargetName(mTargetName); + } mQueue.startTask(task); } } - } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java index 9594381e..645bf197 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java @@ -33,7 +33,7 @@ class StopCmd extends AbsCmd { } @Override public void executeCmd() { - if (!cancelExe) return; + if (!canExeCmd) return; ITask task = mQueue.getTask(mEntity.getEntity()); if (task == null) { if (mEntity.getEntity().getState() == IEntity.STATE_RUNNING) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java index b74f48a7..a2cbf237 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java @@ -16,6 +16,9 @@ package com.arialyy.aria.core.download; +/** + * @author lyy + */ class DownloadListener implements IDownloadListener { @Override public void onResume(long resumeLocation) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index 0d18ce41..b02306ca 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -45,6 +45,19 @@ public class DownloadTarget extends AbsTarget 0) { File configFile = new File(mConfigFPath); Properties pro = CommonUtil.loadConfig(configFile); pro.setProperty(key, String.valueOf(record)); diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index 0941d294..58803f4c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -33,7 +33,7 @@ import java.util.Map; import java.util.Set; /** - * Created by Aria.Lao on 2017/2/28. + * Created by lyy on 2017/2/28. */ public class AbsTarget { protected ENTITY entity; @@ -41,10 +41,18 @@ public class AbsTarget protected String targetName; /** - * 将该任务优先级提到最高 + * 将任务设置为最高优先级任务,最高优先级任务有以下特点: + * 1、在下载队列中,有且只有一个最高优先级任务 + * 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 + * 3、任务调度器不会暂停最高优先级任务 + * 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 + * 5、如果下载队列中已经满了,则会停止队尾的任务 + * 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 */ - protected void setToFirst(){ - + protected void setHighestPriority() { + AriaManager.getInstance(AriaManager.APP) + .setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_HIGHEST_PRIORITY)) + .exe(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ICmd.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ICmd.java index 8fd5fa9b..482cfb0c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ICmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ICmd.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.inf; /** - * Created by Aria.Lao on 2017/2/9. + * Created by lyy on 2017/2/9. */ public interface ICmd { diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java index fcca2e5e..d405f442 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java @@ -18,7 +18,7 @@ package com.arialyy.aria.core.inf; import com.arialyy.aria.orm.Ignore; /** - * Created by Aria.Lao on 2017/2/23. + * Created by lyy on 2017/2/23. */ public interface IEntity { diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java index 28c5de6f..c69b6cf1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java @@ -18,7 +18,7 @@ package com.arialyy.aria.core.inf; import java.util.List; /** - * Created by Aria.Lao on 2017/2/6. + * Created by lyy on 2017/2/6. */ public interface IReceiver { /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java index bf88e5e8..4a67f049 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java @@ -16,11 +16,23 @@ package com.arialyy.aria.core.inf; /** - * Created by Aria.Lao on 2017/2/13. + * Created by lyy on 2017/2/13. */ public interface ITask { + /** + * 设置任务为最高优先级任务,在下载队列中,有且只有一个最高优先级任务 + */ + public void setHighestPriority(boolean isHighestPriority); + + /** + * 该任务是否是最高优先级任务 + * + * @return {@code true} 任务为最高优先级任务 + */ + public boolean isHighestPriorityTask(); + /** * 唯一标识符,DownloadTask 为下载地址,UploadTask 为文件路径 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java index 003d6d1a..828cd989 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java @@ -20,7 +20,7 @@ import java.util.HashMap; import java.util.Map; /** - * Created by Aria.Lao on 2017/2/23. + * Created by lyy on 2017/2/23. */ public abstract class ITaskEntity { 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 15c10575..70f5523c 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 @@ -24,7 +24,7 @@ import com.arialyy.aria.core.queue.pool.CachePool; import com.arialyy.aria.core.queue.pool.ExecutePool; /** - * Created by Aria.Lao on 2017/2/23. + * Created by lyy on 2017/2/23. * 任务队列 */ abstract class AbsTaskQueue @@ -65,6 +65,10 @@ abstract class AbsTaskQueue(true); } + @Override public void setTaskHighestPriority(DownloadTask task) { + int maxSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMaxTaskNum(); + int currentSize = mExecutePool.size(); + if (currentSize == 0 || currentSize < maxSize) { + startTask(task); + } else { + Set tempTasks = new LinkedHashSet<>(); + for (int i = 0; i < maxSize; i++) { + DownloadTask oldTsk = mExecutePool.pollTask(); + if (oldTsk != null && oldTsk.isRunning()) { + oldTsk.stop(); + tempTasks.add(oldTsk); + } + } + startTask(task); + int i = 0, len = tempTasks.size() - 1; + for (DownloadTask oldTask : tempTasks) { + if (i < len) { + startTask(oldTask); + } + i++; + } + } + } + @Override public void setMaxTaskNum(int downloadNum) { - //原始长度 - int size = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; - int diff = downloadNum - size; - if (size == downloadNum) { + int oldMaxSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum; + int diff = downloadNum - oldMaxSize; + if (oldMaxSize == downloadNum) { Log.d(TAG, "设置的下载任务数和配置文件的下载任务数一直,跳过"); return; } //设置的任务数小于配置任务数 - if (diff <= -1 && mExecutePool.size() >= size) { + if (diff <= -1 && mExecutePool.size() >= oldMaxSize) { for (int i = 0, len = Math.abs(diff); i < len; i++) { DownloadTask eTask = mExecutePool.pollTask(); if (eTask != null) { @@ -103,5 +129,4 @@ public class DownloadTaskQueue Log.d(TAG, "从缓存池删除任务,删除" + (mCachePool.removeTask(task) ? "成功" : "失败")); } } - } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java index 95da95f3..fcdcac43 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java @@ -32,6 +32,12 @@ import com.arialyy.aria.core.upload.UploadTaskEntity; */ public interface ITaskQueue { + /** + * 设置任务为最高优先级任务 + * @param task {@link DownloadTask}、{@link UploadTask} + */ + void setTaskHighestPriority(TASK task); + /** * 开始任务 * diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java index 21513f72..011f0d48 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java @@ -25,7 +25,7 @@ import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.core.upload.UploadTaskEntity; /** - * Created by Aria.Lao on 2017/2/27. + * Created by lyy on 2017/2/27. * 上传任务队列 */ public class UploadTaskQueue extends AbsTaskQueue { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java index 61ac2d6f..b83df832 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java @@ -115,6 +115,9 @@ public class ExecutePool implements IPool { Log.e(TAG, "移除任务失败"); return false; } + if (oldTask.isHighestPriorityTask()) { + return false; + } oldTask.stop(); String key = CommonUtil.keyToHashKey(oldTask.getKey()); mExecuteArray.remove(key); diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulerListener.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulerListener.java index 53ecf709..d685697d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulerListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulerListener.java @@ -18,7 +18,7 @@ package com.arialyy.aria.core.scheduler; import com.arialyy.aria.core.inf.ITask; /** - * Created by Aria.Lao on 2017/4/5. + * Created by lyy on 2017/4/5. */ public interface IDownloadSchedulerListener extends ISchedulerListener { diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java index 5722eb87..5a240b7b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java @@ -20,7 +20,7 @@ import android.os.Handler; import com.arialyy.aria.core.inf.ITask; /** - * Created by “AriaLyy@outlook.com” on 2016/11/2. + * Created by lyy on 2016/11/2. * 调度器功能接口 */ public interface ISchedulers extends Handler.Callback { diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java index 349954f6..c1139095 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java @@ -29,7 +29,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** - * Created by Aria.Lao on 2017/2/27. + * Created by lyy on 2017/2/27. * 上传任务调度器 */ public class UploadSchedulers implements ISchedulers { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/IUploadListener.java b/Aria/src/main/java/com/arialyy/aria/core/upload/IUploadListener.java index 5f51824b..02a9e28c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/IUploadListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/IUploadListener.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.upload; /** - * Created by Aria.Lao on 2017/2/9. + * Created by lyy on 2017/2/9. * 上传监听 */ public interface IUploadListener { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java index f3d86e57..d8de9a98 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java @@ -22,7 +22,7 @@ import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.Ignore; /** - * Created by Aria.Lao on 2017/2/9. + * Created by lyy on 2017/2/9. * 上传文件实体 */ public class UploadEntity extends DbEntity implements IEntity, Parcelable { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java index 59c71b8e..b8cfcb6a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java @@ -16,7 +16,7 @@ package com.arialyy.aria.core.upload; /** - * Created by Aria.Lao on 2017/2/23. + * Created by lyy on 2017/2/23. */ public class UploadListener implements IUploadListener { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index 848843b5..98ceff94 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -32,7 +32,7 @@ import java.util.Set; import java.util.regex.Pattern; /** - * Created by Aria.Lao on 2017/2/6. + * Created by lyy on 2017/2/6. * 上传功能接收器 */ public class UploadReceiver implements IReceiver { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java index 58296f38..b0ad23c1 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java @@ -23,7 +23,7 @@ import com.arialyy.aria.orm.DbEntity; import java.util.Map; /** - * Created by Aria.Lao on 2017/2/28. + * Created by lyy on 2017/2/28. */ public class UploadTarget extends AbsTarget { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index d4d555bd..6df5fdd8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -30,7 +30,7 @@ import com.arialyy.aria.util.CommonUtil; import java.lang.ref.WeakReference; /** - * Created by Aria.Lao on 2017/2/23. + * Created by lyy on 2017/2/23. * 上传任务 */ public class UploadTask implements ITask { @@ -41,6 +41,7 @@ public class UploadTask implements ITask { private UploadUtil mUtil; private UListener mListener; + private boolean isHeighestTask = false; private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) { mOutHandler = outHandler; @@ -57,6 +58,14 @@ public class UploadTask implements ITask { mUploadEntity.deleteData(); } + @Override public void setHighestPriority(boolean isHighestPriority) { + isHeighestTask = isHighestPriority; + } + + @Override public boolean isHighestPriorityTask() { + return isHeighestTask; + } + @Override public String getKey() { return mUploadEntity.getFilePath(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java index 7db1ab9c..55e89685 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java @@ -21,7 +21,7 @@ import java.util.HashMap; import java.util.Map; /** - * Created by Aria.Lao on 2017/2/9. + * Created by lyy on 2017/2/9. * 上传任务实体 */ public class UploadTaskEntity extends ITaskEntity { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java index 228dc97d..88a87caa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java @@ -32,7 +32,7 @@ import java.util.Set; import java.util.UUID; /** - * Created by Aria.Lao on 2017/2/9. + * Created by lyy on 2017/2/9. * 上传工具 */ final class UploadUtil implements Runnable { diff --git a/Aria/src/main/java/com/arialyy/aria/exception/FileException.java b/Aria/src/main/java/com/arialyy/aria/exception/FileException.java index 734b2ead..7122da80 100644 --- a/Aria/src/main/java/com/arialyy/aria/exception/FileException.java +++ b/Aria/src/main/java/com/arialyy/aria/exception/FileException.java @@ -16,7 +16,7 @@ package com.arialyy.aria.exception; /** - * Created by Aria.Lao on 2017/1/18. + * Created by lyy on 2017/1/18. * Aria 文件异常 */ public class FileException extends NullPointerException { diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java index 8f2ed69c..5cee79b2 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java @@ -22,7 +22,7 @@ import java.util.HashMap; import java.util.Map; /** - * Created by Aria.Lao on 2017/4/6. + * Created by lyy on 2017/4/6. * 数据库配置信息 */ public class DBConfig { diff --git a/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java b/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java index 79a94c20..b24cbd15 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java @@ -27,7 +27,7 @@ import com.arialyy.aria.util.CommonUtil; import java.util.List; /** - * Created by AriaLyy on 2015/2/11. + * Created by lyy on 2015/2/11. * 数据库操作工具 */ public class DbUtil { diff --git a/Aria/src/main/java/com/arialyy/aria/util/CAConfiguration.java b/Aria/src/main/java/com/arialyy/aria/util/CAConfiguration.java index ac755396..f38ebd30 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CAConfiguration.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CAConfiguration.java @@ -17,7 +17,7 @@ package com.arialyy.aria.util; /** - * Created by Aria.Lao on 2017/1/11. + * Created by lyy on 2017/1/11. */ public class CAConfiguration { diff --git a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java index ec382c7b..eb008022 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java @@ -97,17 +97,18 @@ public class CheckUtil { /** * 检查命令实体 * - * @param checkPath 删除命令不需要检查下载路径和文件名 + * @param checkType 删除命令和停止命令不需要检查下载链接和保存路径 + * @return {@code false}实体无效 */ - public static boolean checkCmdEntity(ITaskEntity entity, boolean checkPath) { + public static boolean checkCmdEntity(ITaskEntity entity, boolean checkType) { boolean b = false; if (entity instanceof DownloadTaskEntity) { DownloadEntity entity1 = ((DownloadTaskEntity) entity).downloadEntity; if (entity1 == null) { Log.e(TAG, "下载实体不能为空"); - } else if (checkPath && TextUtils.isEmpty(entity1.getDownloadUrl())) { + } else if (checkType && TextUtils.isEmpty(entity1.getDownloadUrl())) { Log.e(TAG, "下载链接不能为空"); - } else if (checkPath && TextUtils.isEmpty(entity1.getDownloadPath())) { + } else if (checkType && TextUtils.isEmpty(entity1.getDownloadPath())) { Log.e(TAG, "保存路径不能为空"); } else { b = true; diff --git a/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java b/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java index f6d4df5b..fb4925e3 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/FileUtil.java @@ -31,7 +31,7 @@ import java.util.ArrayList; import java.util.List; /** - * Created by Aria.Lao on 2017/3/21. + * Created by lyy on 2017/3/21. */ public class FileUtil { diff --git a/Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java b/Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java index 9c197881..24426128 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java @@ -23,7 +23,7 @@ import java.util.Collections; import java.util.List; /** - * Created by “AriaLyy@outlook.com” on 2015/7/30. + * Created by lyy on 2015/7/30. * 反射工具类 */ public class ReflectionUtil { diff --git a/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java b/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java index b93328f8..8134383e 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java @@ -38,7 +38,7 @@ import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; /** - * Created by Aria.Lao on 2017/1/11. + * Created by lyy on 2017/1/11. * SSL证书工具 */ public class SSLContextUtil { diff --git a/Aria/src/main/java/com/arialyy/aria/util/Speed.java b/Aria/src/main/java/com/arialyy/aria/util/Speed.java index f26a2329..90cf0cee 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/Speed.java +++ b/Aria/src/main/java/com/arialyy/aria/util/Speed.java @@ -16,7 +16,7 @@ package com.arialyy.aria.util; /** - * Created by Aria.Lao on 2017/3/6. + * Created by lyy on 2017/3/6. */ public enum Speed { /** diff --git a/Aria/src/main/java/com/arialyy/aria/window/AriaFileChangeActivity.java b/Aria/src/main/java/com/arialyy/aria/window/AriaFileChangeActivity.java index dbef9a15..aa8ca56b 100644 --- a/Aria/src/main/java/com/arialyy/aria/window/AriaFileChangeActivity.java +++ b/Aria/src/main/java/com/arialyy/aria/window/AriaFileChangeActivity.java @@ -30,10 +30,10 @@ import java.util.List; import java.util.Map; /** - * Created by Aria.Lao on 2017/3/21. + * Created by lyy on 2017/3/21. * 文件选择 */ -public class AriaFileChangeActivity extends FragmentActivity { +class AriaFileChangeActivity extends FragmentActivity { final String ROOT_PAT = Environment.getExternalStorageDirectory().getPath(); ListView mList; FileChangeAdapter mAdapter; diff --git a/Aria/src/main/java/com/arialyy/aria/window/FileChangeAdapter.java b/Aria/src/main/java/com/arialyy/aria/window/FileChangeAdapter.java index 1e3335a9..c8a77ca5 100644 --- a/Aria/src/main/java/com/arialyy/aria/window/FileChangeAdapter.java +++ b/Aria/src/main/java/com/arialyy/aria/window/FileChangeAdapter.java @@ -29,7 +29,7 @@ import java.util.ArrayList; import java.util.List; /** - * Created by Aria.Lao on 2017/3/21. + * Created by lyy on 2017/3/21. */ final class FileChangeAdapter extends BaseAdapter { diff --git a/Aria/src/main/java/com/arialyy/aria/window/FileEntity.java b/Aria/src/main/java/com/arialyy/aria/window/FileEntity.java index e0978bf9..0f05b591 100644 --- a/Aria/src/main/java/com/arialyy/aria/window/FileEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/window/FileEntity.java @@ -18,10 +18,10 @@ package com.arialyy.aria.window; import android.graphics.drawable.Drawable; /** - * Created by Aria.Lao on 2017/3/21. + * Created by lyy on 2017/3/21. */ -public class FileEntity { +class FileEntity { public String fileName; public String fileInfo; public int fileIcon; diff --git a/README.md b/README.md index 992c5e3a..4a189153 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,11 @@ compile 'com.arialyy.aria:Aria:3.1.4' ```java final static class MySchedulerListener extends Aria.DownloadSchedulerListener{ @Override public void onTaskPre(DownloadTask task) { - super.onTaskPre(task); + //通过下载地址可以判断任务是否是你指定的任务 + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()) + .sendToTarget(); + } } @Override public void onTaskStop(DownloadTask task) { @@ -98,6 +102,8 @@ compile 'com.arialyy.aria:Aria:3.1.4' @Override public void onTaskRunning(DownloadTask task) { super.onTaskRunning(task); } + + ... } ``` diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadModule.java b/app/src/main/java/com/arialyy/simple/download/DownloadModule.java index f87865c1..65eeb9ce 100644 --- a/app/src/main/java/com/arialyy/simple/download/DownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadModule.java @@ -52,6 +52,13 @@ public class DownloadModule extends BaseModule { "http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"); } + /** + * 获取下载列表 + */ + public List getDownloadTaskList() { + return Aria.download(getContext()).getTaskList(); + } + public String getRadomUrl() { Random random = new Random(); int i = random.nextInt(2); diff --git a/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java b/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java new file mode 100644 index 00000000..9194e5d6 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java @@ -0,0 +1,169 @@ +package com.arialyy.simple.download; + +import android.os.Bundle; +import android.os.Environment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; +import butterknife.Bind; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.download.DownloadTarget; +import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.frame.util.show.L; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityHighestPriorityBinding; +import com.arialyy.simple.download.multi_download.DownloadAdapter; +import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; + +/** + * Created by lyy on 2017/6/2. + * 最高优先级任务Demo + */ +public class HighestPriorityActivity extends BaseActivity { + @Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb; + @Bind(R.id.start) Button mStart; + @Bind(R.id.stop) Button mStop; + @Bind(R.id.cancel) Button mCancel; + @Bind(R.id.size) TextView mSize; + @Bind(R.id.toolbar) Toolbar toolbar; + @Bind(R.id.speed) TextView mSpeed; + @Bind(R.id.list) RecyclerView mList; + + private String mTaskName = "狂野飙车8"; + private static final String DOWNLOAD_URL = + "http://static.gaoshouyou.com/d/82/ff/df82ed0af4ff4c1746cb191cf765aa8f.apk"; + private DownloadAdapter mAdapter; + + @Override protected int setLayoutId() { + return R.layout.activity_highest_priority; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + setSupportActionBar(toolbar); + toolbar.setTitle("最高优先级任务演示"); + getBinding().setTaskName("任务名:" + mTaskName + " (该任务是最高优先级任务)"); + initWidget(); + } + + private void initWidget() { + if (Aria.download(this).taskExists(DOWNLOAD_URL)) { + DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); + int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); + mPb.setProgress(p); + } + mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadTaskList()); + mList.setLayoutManager(new LinearLayoutManager(this)); + mList.setAdapter(mAdapter); + } + + @Override protected void onResume() { + super.onResume(); + Aria.download(this).addSchedulerListener(new MySchedulerListener()); + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.start: + String text = ((TextView) view).getText().toString(); + if (text.equals("重新开始?") || text.equals("开始")) { + Aria.download(this) + .load(DOWNLOAD_URL) + .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + + "/Download/" + + mTaskName + + ".apk") + .setHighestPriority(); + } else if (text.equals("恢复")) { + Aria.download(this).load(DOWNLOAD_URL).resume(); + } + break; + case R.id.stop: + Aria.download(this).load(DOWNLOAD_URL).pause(); + break; + case R.id.cancel: + Aria.download(this).load(DOWNLOAD_URL).cancel(); + break; + } + } + + /** + * 设置start 和 stop 按钮状态 + */ + private void setBtState(boolean state) { + mStart.setEnabled(state); + mStop.setEnabled(!state); + } + + private class MySchedulerListener extends Aria.DownloadSchedulerListener { + + @Override public void onTaskPre(DownloadTask task) { + super.onTaskPre(task); + if (task.getKey().equals(DOWNLOAD_URL)) { + mSize.setText(task.getConvertFileSize()); + } else { + mAdapter.updateState(task.getDownloadEntity()); + } + } + + @Override public void onTaskStart(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(false); + } else { + mAdapter.updateState(task.getDownloadEntity()); + } + } + + @Override public void onTaskResume(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(false); + } else { + mAdapter.updateState(task.getDownloadEntity()); + } + } + + @Override public void onTaskStop(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + } else { + mAdapter.updateState(task.getDownloadEntity()); + } + } + + @Override public void onTaskCancel(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + } else { + mAdapter.updateState(task.getDownloadEntity()); + } + } + + @Override public void onTaskFail(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + } else { + L.d(TAG, "download fail【" + task.getKey() + "】"); + } + } + + @Override public void onTaskComplete(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + } + } + + @Override public void onTaskRunning(DownloadTask task) { + if (task.getKey().equals(DOWNLOAD_URL)) { + setBtState(true); + mPb.setProgress(task.getPercent()); + mSpeed.setText(task.getConvertSpeed()); + } else { + mAdapter.setProgress(task.getDownloadEntity()); + } + } + } +} 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 b61773ab..86e1bc58 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -151,11 +151,6 @@ public class SingleTaskActivity extends BaseActivity { //registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter()); } - @Override protected void onDestroy() { - super.onDestroy(); - //unregisterReceiver(mReceiver); - } - @Override protected int setLayoutId() { return R.layout.activity_single; } @@ -174,33 +169,6 @@ public class SingleTaskActivity extends BaseActivity { int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); mPb.setProgress(p); } - //mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { - // @Override public void onCheckedChanged(RadioGroup group, int checkedId) { - // switch (checkedId) { - // case 1: - // Aria.get(this).setMaxSpeed(Speed.KB_256); - // break; - // case 2: - // Aria.get(this).setMaxSpeed(Speed.KB_512); - // break; - // case 3: - // Aria.get(this).setMaxSpeed(Speed.MB_1); - // break; - // case 4: - // Aria.get(this).setMaxSpeed(Speed.MB_2); - // break; - // case 5: - // Aria.get(this).setMaxSpeed(Speed.MAX); - // break; - // } - // stop(); - // new Handler().postDelayed(new Runnable() { - // @Override public void run() { - // start(); - // } - // }, 2000); - // } - //}); } public void onClick(View view) { @@ -208,40 +176,23 @@ public class SingleTaskActivity extends BaseActivity { case R.id.start: String text = ((TextView) view).getText().toString(); if (text.equals("重新开始?") || text.equals("开始")) { - start(); + Aria.download(this) + .load(DOWNLOAD_URL) + .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") + .start(); } else if (text.equals("恢复")) { - resume(); + Aria.download(this).load(DOWNLOAD_URL).resume(); } break; case R.id.stop: - stop(); + Aria.download(this).load(DOWNLOAD_URL).pause(); break; case R.id.cancel: - cancel(); + Aria.download(this).load(DOWNLOAD_URL).cancel(); break; } } - private void resume() { - Aria.download(this).load(DOWNLOAD_URL).resume(); - } - - private void start() { - Aria.download(this) - .load(DOWNLOAD_URL) - .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") - .start(); - } - - private void stop() { - Aria.download(this).load(DOWNLOAD_URL).pause(); - //Aria.download(this).load(DOWNLOAD_URL).removeRecord(); - } - - private void cancel() { - Aria.download(this).load(DOWNLOAD_URL).cancel(); - } - private class MySchedulerListener extends Aria.DownloadSchedulerListener { @Override public void onPre(String url) { @@ -255,8 +206,11 @@ public class SingleTaskActivity extends BaseActivity { } @Override public void onTaskStart(DownloadTask task) { - mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()) - .sendToTarget(); + //通过下载地址可以判断任务是否是你指定的任务 + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()) + .sendToTarget(); + } } @Override public void onTaskResume(DownloadTask task) { 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 4342f4d2..ff8da924 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 @@ -38,11 +38,11 @@ import java.util.concurrent.ConcurrentHashMap; * Created by Lyy on 2016/9/27. * 下载列表适配器 */ -final class DownloadAdapter extends AbsRVAdapter { +public class DownloadAdapter extends AbsRVAdapter { private static final String TAG = "DownloadAdapter"; private Map mPositions = new ConcurrentHashMap<>(); - DownloadAdapter(Context context, List data) { + public DownloadAdapter(Context context, List data) { super(context, data); int i = 0; for (DownloadEntity entity : data) { diff --git a/app/src/main/res/layout/activity_download_mean.xml b/app/src/main/res/layout/activity_download_mean.xml index e52848d1..3ac2c991 100644 --- a/app/src/main/res/layout/activity_download_mean.xml +++ b/app/src/main/res/layout/activity_download_mean.xml @@ -29,6 +29,15 @@ style="?buttonBarButtonStyle" /> +