任务最高优先级开发

pull/330/head
AriaLyy 8 years ago
parent 9aa7df035a
commit 5ee2cba331
  1. 2
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java
  2. 2
      Aria/src/main/java/com/arialyy/aria/core/Configuration.java
  3. 2
      Aria/src/main/java/com/arialyy/aria/core/RequestEnum.java
  4. 2
      Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java
  5. 4
      Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java
  6. 2
      Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
  7. 5
      Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
  8. 53
      Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
  9. 53
      Aria/src/main/java/com/arialyy/aria/core/command/HighestPriorityCmd.java
  10. 7
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  11. 2
      Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
  12. 3
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadListener.java
  13. 13
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  14. 9
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  15. 2
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  16. 1
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java
  17. 2
      Aria/src/main/java/com/arialyy/aria/core/download/IDownloadUtil.java
  18. 2
      Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java
  19. 16
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  20. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/ICmd.java
  21. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java
  22. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java
  23. 14
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  24. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java
  25. 6
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  26. 37
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  27. 6
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  28. 2
      Aria/src/main/java/com/arialyy/aria/core/queue/UploadTaskQueue.java
  29. 3
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java
  30. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/IDownloadSchedulerListener.java
  31. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/ISchedulers.java
  32. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
  33. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/IUploadListener.java
  34. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java
  35. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java
  36. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  37. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
  38. 11
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  39. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java
  40. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java
  41. 2
      Aria/src/main/java/com/arialyy/aria/exception/FileException.java
  42. 2
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  43. 2
      Aria/src/main/java/com/arialyy/aria/orm/DbUtil.java
  44. 2
      Aria/src/main/java/com/arialyy/aria/util/CAConfiguration.java
  45. 9
      Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
  46. 2
      Aria/src/main/java/com/arialyy/aria/util/FileUtil.java
  47. 2
      Aria/src/main/java/com/arialyy/aria/util/ReflectionUtil.java
  48. 2
      Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java
  49. 2
      Aria/src/main/java/com/arialyy/aria/util/Speed.java
  50. 4
      Aria/src/main/java/com/arialyy/aria/window/AriaFileChangeActivity.java
  51. 2
      Aria/src/main/java/com/arialyy/aria/window/FileChangeAdapter.java
  52. 4
      Aria/src/main/java/com/arialyy/aria/window/FileEntity.java
  53. 8
      README.md
  54. 7
      app/src/main/java/com/arialyy/simple/download/DownloadModule.java
  55. 169
      app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java
  56. 66
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  57. 4
      app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java
  58. 9
      app/src/main/res/layout/activity_download_mean.xml
  59. 47
      app/src/main/res/layout/activity_highest_priority.xml

@ -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 {

@ -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 {

@ -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请求方式目前支持GETPOST
*/
public enum RequestEnum {

@ -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 {

@ -38,13 +38,13 @@ public abstract class AbsCmd<T extends ITaskEntity> 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;

@ -31,7 +31,7 @@ class AddCmd<T extends ITaskEntity> extends AbsCmd<T> {
}
@Override public void executeCmd() {
if (!cancelExe) return;
if (!canExeCmd) return;
ITask task = mQueue.getTask(mEntity.getEntity());
if (task == null) {
mQueue.createTask(mTargetName, mEntity);

@ -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<T extends ITaskEntity> extends AbsCmd<T> {
}
@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);

@ -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 <T extends ITaskEntity> 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 <T extends ITaskEntity> StopCmd createStopCmd(String target, T entity) {
return new StopCmd(target, entity);
}
/**
* 创建下载任务命令
*
* @return {@link AddCmd}
*/
private <T extends ITaskEntity> AddCmd createAddCmd(String target, T entity) {
return new AddCmd(target, entity);
}
/**
* 创建启动下载命令
*
* @return {@link StartCmd}
*/
private <T extends ITaskEntity> StartCmd createStartCmd(String target, T entity) {
return new StartCmd(target, entity);
}
/**
* 创建启动下载命令
*
* @return {@link StartCmd}
*/
private <T extends ITaskEntity> CancelCmd createCancelCmd(String target, T entity) {
return new CancelCmd(target, entity);
}
}

@ -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<T extends ITaskEntity> extends AbsCmd<T> {
/**
* @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);
}
}
}

@ -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<T extends ITaskEntity> extends AbsCmd<T> {
}
@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 (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
mQueue.startTask(task);
}
}
}

@ -33,7 +33,7 @@ class StopCmd<T extends ITaskEntity> extends AbsCmd<T> {
}
@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) {

@ -16,6 +16,9 @@
package com.arialyy.aria.core.download;
/**
* @author lyy
*/
class DownloadListener implements IDownloadListener {
@Override public void onResume(long resumeLocation) {

@ -45,6 +45,19 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
super.resume();
}
/**
* 将任务设置为最高优先级任务最高优先级任务有以下特点
* 1在下载队列中有且只有一个最高优先级任务
* 2最高优先级任务会一直存在直到用户手动暂停或任务完成
* 3任务调度器不会暂停最高优先级任务
* 4用户手动暂停或任务完成后第二次重新执行该任务该命令将失效
* 5如果下载队列中已经满了则会停止队尾的任务
* 6把任务设置为最高优先级任务后将自动执行任务不需要重新调用start()启动任务
*/
@Override public void setHighestPriority() {
super.setHighestPriority();
}
/**
* 重定向后新url的key默认为location
*/

@ -44,6 +44,7 @@ public class DownloadTask implements ITask {
private Handler mOutHandler;
private IDownloadUtil mUtil;
private Context mContext;
private boolean isHeighestTask = false;
private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) {
mEntity = taskEntity.downloadEntity;
@ -142,6 +143,14 @@ public class DownloadTask implements ITask {
return mEntity.getDownloadUrl();
}
@Override public void setHighestPriority(boolean isHighestPriority) {
isHeighestTask = isHighestPriority;
}
@Override public boolean isHighestPriorityTask() {
return isHeighestTask;
}
@Override public String getKey() {
return getDownloadUrl();
}

@ -19,7 +19,7 @@ import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITaskEntity;
/**
* Created by Aria.Lao on 2017/1/23.
* Created by lyy on 2017/1/23.
* 下载任务实体
*/
public class DownloadTaskEntity extends ITaskEntity {

@ -430,7 +430,6 @@ class DownloadUtil implements IDownloadUtil, Runnable {
private void addSingleTask(int i, long startL, long endL, long fileLength) {
ConfigEntity entity = new ConfigEntity();
entity.FILE_SIZE = fileLength;
//entity.DOWNLOAD_URL = mDownloadEntity.getDownloadUrl();
entity.DOWNLOAD_URL = mDownloadEntity.isRedirect() ? mDownloadEntity.getRedirectUrl()
: mDownloadEntity.getDownloadUrl();
entity.TEMP_FILE = mDownloadFile;

@ -17,7 +17,7 @@
package com.arialyy.aria.core.download;
/**
* Created by AriaLyy@outlook.com on 2016/10/31.
* Created by lyy on 2016/10/31.
* 抽象的下载接口
*/
interface IDownloadUtil {

@ -244,7 +244,7 @@ final class SingleThreadTask implements Runnable {
* 将记录写入到配置文件
*/
private void writeConfig(String key, long record) throws IOException {
if (record != -1 && record != 0) {
if (record > 0) {
File configFile = new File(mConfigFPath);
Properties pro = CommonUtil.loadConfig(configFile);
pro.setProperty(key, String.valueOf(record));

@ -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<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity> {
protected ENTITY entity;
@ -41,10 +41,18 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
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();
}
/**

@ -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 {

@ -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 {

@ -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<ENTITY extends IEntity> {
/**

@ -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 为文件路径
*/

@ -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 {

@ -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<TASK extends ITask, TASK_ENTITY extends ITaskEntity, ENTITY extends IEntity>
@ -65,6 +65,10 @@ abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity,
return mExecutePool.size();
}
@Override public void setTaskHighestPriority(TASK task) {
}
@Override public TASK getTask(String url) {
TASK task = mExecutePool.getTask(url);
if (task == null) {

@ -25,6 +25,8 @@ import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.queue.pool.ExecutePool;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Created by lyy on 2016/8/17.
@ -49,16 +51,40 @@ public class DownloadTaskQueue
mExecutePool = new ExecutePool<>(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<DownloadTask> 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) ? "成功" : "失败"));
}
}
}

@ -32,6 +32,12 @@ import com.arialyy.aria.core.upload.UploadTaskEntity;
*/
public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity, ENTITY extends IEntity> {
/**
* 设置任务为最高优先级任务
* @param task {@link DownloadTask}{@link UploadTask}
*/
void setTaskHighestPriority(TASK task);
/**
* 开始任务
*

@ -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<UploadTask, UploadTaskEntity, UploadEntity> {

@ -115,6 +115,9 @@ public class ExecutePool<TASK extends ITask> implements IPool<TASK> {
Log.e(TAG, "移除任务失败");
return false;
}
if (oldTask.isHighestPriorityTask()) {
return false;
}
oldTask.stop();
String key = CommonUtil.keyToHashKey(oldTask.getKey());
mExecuteArray.remove(key);

@ -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<TASK extends ITask> extends ISchedulerListener<TASK> {

@ -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<Task extends ITask> extends Handler.Callback {

@ -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<UploadTask> {

@ -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 {

@ -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 {

@ -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 {

@ -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<UploadEntity> {

@ -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<UploadEntity, UploadTaskEntity> {

@ -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();
}

@ -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 {

@ -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 {

@ -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 {

@ -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 {

@ -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 {

@ -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 {

@ -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;

@ -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 {

@ -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 {

@ -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 {

@ -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 {
/**

@ -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;

@ -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 {

@ -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;

@ -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);
}
...
}
```

@ -52,6 +52,13 @@ public class DownloadModule extends BaseModule {
"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk");
}
/**
* 获取下载列表
*/
public List<DownloadEntity> getDownloadTaskList() {
return Aria.download(getContext()).getTaskList();
}
public String getRadomUrl() {
Random random = new Random();
int i = random.nextInt(2);

@ -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<ActivityHighestPriorityBinding> {
@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());
}
}
}
}

@ -151,11 +151,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//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<ActivitySingleBinding> {
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<ActivitySingleBinding> {
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,9 +206,12 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
@Override public void onTaskStart(DownloadTask task) {
//通过下载地址可以判断任务是否是你指定的任务
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
}
@Override public void onTaskResume(DownloadTask task) {
super.onTaskResume(task);

@ -38,11 +38,11 @@ import java.util.concurrent.ConcurrentHashMap;
* Created by Lyy on 2016/9/27.
* 下载列表适配器
*/
final class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter.MyHolder> {
public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter.MyHolder> {
private static final String TAG = "DownloadAdapter";
private Map<String, Integer> mPositions = new ConcurrentHashMap<>();
DownloadAdapter(Context context, List<DownloadEntity> data) {
public DownloadAdapter(Context context, List<DownloadEntity> data) {
super(context, data);
int i = 0;
for (DownloadEntity entity : data) {

@ -29,6 +29,15 @@
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/highest_priority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="将任务设置为最高优先级任务"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/dialog_task"
android:layout_width="match_parent"

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="taskName"
type="String"
/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/layout_bar"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="@{taskName}"
android:textColor="@android:color/black"
android:textSize="16sp"
/>
<include
layout="@layout/content_single"
android:id="@+id/task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/task"
android:layout_margin="16dp"
/>
</RelativeLayout>
</layout>
Loading…
Cancel
Save