针对单个任务设置监听器, https://github.com/AriaLyy/Aria/issues/37
This commit is contained in:
12
Aria/bintray-release.gradle
Normal file
12
Aria/bintray-release.gradle
Normal file
@ -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
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'bintray-release'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
@ -27,49 +26,4 @@ dependencies {
|
||||
// compile project(':AriaAnnotations')
|
||||
}
|
||||
//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')
|
||||
//}
|
||||
//apply from: 'bintray-release.gradle'
|
@ -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 <T extends AbsTaskEntity> AbsCmd createCmd(String target, T entity, int type) {
|
||||
public <T extends AbsTaskEntity> AbsCmd<T> 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;
|
||||
}
|
||||
|
@ -0,0 +1,49 @@
|
||||
package com.arialyy.aria.core.command;
|
||||
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
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<T extends AbsTaskEntity> extends AbsCmd<T> {
|
||||
/**
|
||||
* @param targetName 产生任务的对象名
|
||||
*/
|
||||
ResumeAllCmd(String targetName, T entity) {
|
||||
super(targetName, entity);
|
||||
}
|
||||
|
||||
@Override public void executeCmd() {
|
||||
List<DownloadEntity> 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) {
|
||||
task = mQueue.createTask(mTargetName, mEntity);
|
||||
} else {
|
||||
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
|
||||
}
|
||||
return task;
|
||||
}
|
||||
}
|
@ -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<T extends AbsTaskEntity> extends AbsCmd<T> {
|
||||
/**
|
||||
* @param targetName 产生任务的对象名
|
||||
*/
|
||||
StopAllCmd(String targetName, T entity) {
|
||||
super(targetName, entity);
|
||||
}
|
||||
|
||||
@Override public void executeCmd() {
|
||||
mQueue.stopAllTask();
|
||||
}
|
||||
}
|
@ -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,21 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止所有正在下载的任务
|
||||
* 停止所有正在下载的任务,并清空等待队列。
|
||||
*/
|
||||
@Override public void stopAllTask() {
|
||||
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
|
||||
List<DownloadEntity> allEntity = DbEntity.findAllData(DownloadEntity.class);
|
||||
List<AbsCmd> 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复所有正在下载的任务
|
||||
*/
|
||||
public void resumeAllTask() {
|
||||
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
|
||||
ariaManager.setCmd(CmdFactory.getInstance()
|
||||
.createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_RESUME_ALL)).exe();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,6 +25,8 @@ public class DownloadTaskEntity extends AbsTaskEntity {
|
||||
|
||||
public DownloadEntity downloadEntity;
|
||||
|
||||
public DownloadTaskEntity(){}
|
||||
|
||||
public DownloadTaskEntity(DownloadEntity downloadEntity) {
|
||||
this.downloadEntity = downloadEntity;
|
||||
}
|
||||
|
@ -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<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
|
||||
CachePool<TASK> mCachePool = new CachePool<>();
|
||||
ExecutePool<TASK> mExecutePool;
|
||||
|
||||
/**
|
||||
* 停止所有任务
|
||||
*/
|
||||
@Override public void stopAllTask() {
|
||||
Set<String> exeKeys = mExecutePool.getAllTask().keySet();
|
||||
for (String key : exeKeys) {
|
||||
TASK task = mExecutePool.getAllTask().get(key);
|
||||
if (task != null && task.isRunning()) task.stop();
|
||||
}
|
||||
Set<String> 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<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
|
||||
*
|
||||
* @return 当前正在执行的任务数
|
||||
*/
|
||||
@Override public int executePoolSize() {
|
||||
@Override public int getExeTaskNum() {
|
||||
return mExecutePool.size();
|
||||
}
|
||||
|
||||
|
@ -32,8 +32,14 @@ import com.arialyy.aria.core.upload.UploadTaskEntity;
|
||||
*/
|
||||
public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends IEntity> {
|
||||
|
||||
/**
|
||||
* 停止所有任务
|
||||
*/
|
||||
void stopAllTask();
|
||||
|
||||
/**
|
||||
* 设置任务为最高优先级任务
|
||||
*
|
||||
* @param task {@link DownloadTask}、{@link UploadTask}
|
||||
*/
|
||||
void setTaskHighestPriority(TASK task);
|
||||
@ -67,9 +73,9 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
|
||||
void reTryStart(TASK task);
|
||||
|
||||
/**
|
||||
* 任务池队列大小
|
||||
* 获取正在执行的任务数量
|
||||
*/
|
||||
int executePoolSize();
|
||||
int getExeTaskNum();
|
||||
|
||||
/**
|
||||
* 任务缓存池大小
|
||||
@ -79,10 +85,15 @@ public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntit
|
||||
/**
|
||||
* 设置最大任务数
|
||||
*
|
||||
* @param newMaxNum 下载任务数
|
||||
* @param newMaxNum 最大任务数
|
||||
*/
|
||||
void setMaxTaskNum(int newMaxNum);
|
||||
|
||||
/**
|
||||
* 获取可执行队列的大小
|
||||
*/
|
||||
int getMaxTaskNum();
|
||||
|
||||
/**
|
||||
* 创建一个新的任务,创建时只是将新任务存储到缓存池
|
||||
*
|
||||
|
@ -140,7 +140,7 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
|
||||
}
|
||||
case CANCEL:
|
||||
mQueue.removeTask(entity);
|
||||
if (mQueue.executePoolSize() < AriaManager.getInstance(AriaManager.APP)
|
||||
if (mQueue.getExeTaskNum() < AriaManager.getInstance(AriaManager.APP)
|
||||
.getUploadConfig()
|
||||
.getMaxTaskNum()) {
|
||||
startNextTask();
|
||||
|
Reference in New Issue
Block a user