pull/2/head
lyy 8 years ago
parent d1b48b2f64
commit 3667fc107b
  1. 8
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java
  2. 8
      app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java
  3. 23
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java
  4. 66
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java
  5. 11
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/AddCmd.java
  6. 12
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CancelCmd.java
  7. 28
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java
  8. 28
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/IDownloadCmd.java
  9. 12
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StartCmd.java
  10. 16
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StopCmd.java
  11. 26
      downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITaskQueue.java

@ -205,20 +205,20 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mEntity.setDownloadUrl(mDownloadUrl);
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
List<IDownloadCmd> commands = new ArrayList<>();
IDownloadCmd addCMD = mFactory.createCmd(this, mEntity, CmdFactory.TASK_CREATE);
IDownloadCmd startCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_START);
IDownloadCmd addCMD = mFactory.createCmd(mEntity, CmdFactory.TASK_CREATE);
IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START);
commands.add(addCMD);
commands.add(startCmd);
mManager.setCmds(commands).exe();
}
private void stop() {
IDownloadCmd stopCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_STOP);
IDownloadCmd stopCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_STOP);
mManager.setCmd(stopCmd).exe();
}
private void cancel() {
IDownloadCmd cancelCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_CANCEL);
IDownloadCmd cancelCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_CANCEL);
mManager.setCmd(cancelCmd).exe();
}
}

@ -39,7 +39,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
int i = 0;
for (DownloadEntity entity : data) {
mPositions.put(entity.getDownloadUrl(), i);
addCmd.add(mFactory.createCmd(context, entity, CmdFactory.TASK_CREATE));
addCmd.add(mFactory.createCmd(entity, CmdFactory.TASK_CREATE));
i++;
}
mManager.setCmds(addCmd).exe();
@ -127,7 +127,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
@Override public void onClick(View v) {
mData.remove(item);
notifyDataSetChanged();
IDownloadCmd cancelCmd = mFactory.createCmd(getContext(), item, CmdFactory.TASK_CANCEL);
IDownloadCmd cancelCmd = mFactory.createCmd(item, CmdFactory.TASK_CANCEL);
mManager.setCmd(cancelCmd).exe();
}
});
@ -167,12 +167,12 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
private void start(DownloadEntity entity) {
IDownloadCmd startCmd = mFactory.createCmd(getContext(), entity, CmdFactory.TASK_START);
IDownloadCmd startCmd = mFactory.createCmd(entity, CmdFactory.TASK_START);
mManager.setCmd(startCmd).exe();
}
private void stop(DownloadEntity entity) {
IDownloadCmd stopCmd = mFactory.createCmd(getContext(), entity, CmdFactory.TASK_STOP);
IDownloadCmd stopCmd = mFactory.createCmd(entity, CmdFactory.TASK_STOP);
mManager.setCmd(stopCmd).exe();
}
}

@ -4,6 +4,7 @@ import android.app.Application;
import android.content.Context;
import android.util.Log;
import com.arialyy.downloadutil.core.command.IDownloadCmd;
import com.arialyy.downloadutil.core.inf.ITaskQueue;
import com.arialyy.downloadutil.orm.DbEntity;
import com.arialyy.downloadutil.orm.DbUtil;
import java.util.ArrayList;
@ -67,6 +68,7 @@ public class DownloadManager {
private static volatile DownloadManager INSTANCE = null;
private List<IDownloadCmd> mCommands = new ArrayList<>();
private Context mContext;
private ITaskQueue mTaskQueue;
private DownloadManager() {
@ -74,7 +76,8 @@ public class DownloadManager {
private DownloadManager(Context context) {
mContext = context;
DownloadTaskQueue.init(context);
DownloadTaskQueue.Builder builder = new DownloadTaskQueue.Builder(context);
mTaskQueue = builder.build();
DbUtil.init(context);
}
@ -102,6 +105,13 @@ public class DownloadManager {
return DbEntity.findAllData(DownloadEntity.class);
}
/**
* 获取任务队列
*/
public ITaskQueue getTaskQueue() {
return mTaskQueue;
}
/**
* 设置命令
*/
@ -125,8 +135,17 @@ public class DownloadManager {
*/
public synchronized void exe() {
for (IDownloadCmd command : mCommands) {
command.executeComment();
command.executeCmd();
}
mCommands.clear();
}
/**
* 设置下载器
*
* @param queue {@link ITaskQueue}
*/
public void setDownloadQueue(ITaskQueue queue) {
mTaskQueue = queue;
}
}

@ -12,13 +12,12 @@ import com.arialyy.downloadutil.core.pool.ExecutePool;
* Created by lyy on 2016/8/17.
* 下载任务队列
*/
public class DownloadTaskQueue implements ITaskQueue, IDownloader {
final class DownloadTaskQueue implements ITaskQueue {
private static final String TAG = "DownloadTaskQueue";
private static final Object LOCK = new Object();
private static volatile DownloadTaskQueue INSTANCE = null;
private CachePool mCachePool = CachePool.getInstance();
private ExecutePool mExecutePool = ExecutePool.getInstance();
private Context mContext;
private IDownloadSchedulers mSchedulers;
private DownloadTaskQueue() {
}
@ -28,22 +27,6 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader {
mContext = context;
}
public static DownloadTaskQueue getInstance() {
if (INSTANCE == null) {
throw new NullPointerException("请在Application中调用init进行注册");
}
return INSTANCE;
}
static DownloadTaskQueue init(Context context) {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new DownloadTaskQueue(context.getApplicationContext());
}
}
return INSTANCE;
}
/**
* 获取任务执行池
*/
@ -109,14 +92,7 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader {
}
@Override public Task createTask(DownloadEntity entity) {
return createTask(entity, null);
}
@Override public Task createTask(DownloadEntity entity, IDownloadSchedulers schedulers) {
if (schedulers == null) {
schedulers = DownloadSchedulers.getInstance(this);
}
Task task = TaskFactory.getInstance().createTask(mContext, entity, schedulers);
Task task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers);
mCachePool.putTask(task);
return task;
}
@ -129,15 +105,6 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader {
return task;
}
@Override public int getTaskState(DownloadEntity entity) {
Task task = getTask(entity);
if (task == null) {
Log.e(TAG, "没有找到下载链接为【" + entity.getDownloadUrl() + "】的下载任务");
return -1;
}
return task.getDownloadEntity().getState();
}
@Override public void removeTask(DownloadEntity entity) {
Task task = mExecutePool.getTask(entity.getDownloadUrl());
if (task != null) {
@ -155,4 +122,31 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader {
@Override public Task getNextTask() {
return mCachePool.pollTask();
}
@Override public void setScheduler(IDownloadSchedulers schedulers) {
mSchedulers = schedulers;
}
static class Builder {
Context context;
IDownloadSchedulers schedulers;
Builder(Context context) {
context = context.getApplicationContext();
}
public Builder setDownloadSchedulers(IDownloadSchedulers schedulers) {
this.schedulers = schedulers;
return this;
}
DownloadTaskQueue build() {
DownloadTaskQueue queue = new DownloadTaskQueue(context);
if (schedulers == null) {
schedulers = DownloadSchedulers.getInstance(queue);
}
queue.setScheduler(schedulers);
return queue;
}
}
}

@ -1,6 +1,5 @@
package com.arialyy.downloadutil.core.command;
import android.content.Context;
import android.util.Log;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.Task;
@ -11,14 +10,14 @@ import com.arialyy.downloadutil.core.Task;
*/
class AddCmd extends IDownloadCmd {
AddCmd(Context context, DownloadEntity entity) {
super(context, entity);
AddCmd(DownloadEntity entity) {
super(entity);
}
@Override public void executeComment() {
Task task = target.getTask(mEntity);
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
target.createTask(mEntity);
mQueue.createTask(mEntity);
} else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
}

@ -10,17 +10,17 @@ import com.arialyy.downloadutil.core.Task;
*/
class CancelCmd extends IDownloadCmd {
CancelCmd(Context context, DownloadEntity entity) {
super(context, entity);
CancelCmd(DownloadEntity entity) {
super(entity);
}
@Override public void executeComment() {
Task task = target.getTask(mEntity);
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
task = target.createTask(mEntity);
task = mQueue.createTask(mEntity);
}
if (task != null) {
target.cancelTask(task);
mQueue.cancelTask(task);
}
}
}

@ -1,6 +1,5 @@
package com.arialyy.downloadutil.core.command;
import android.content.Context;
import com.arialyy.downloadutil.core.DownloadEntity;
/**
@ -46,22 +45,21 @@ public class CmdFactory {
}
/**
* @param context context
* @param entity 下载实体
* @param type 命令类型{@link #TASK_CREATE}{@link #TASK_START}{@link #TASK_CANCEL}{@link
* #TASK_STOP}
*/
public IDownloadCmd createCmd(Context context, DownloadEntity entity, int type) {
public IDownloadCmd createCmd(DownloadEntity entity, int type) {
switch (type) {
case TASK_CREATE:
return createAddCmd(context, entity);
return createAddCmd(entity);
case TASK_RESUME:
case TASK_START:
return createStartCmd(context, entity);
return createStartCmd(entity);
case TASK_CANCEL:
return createCancelCmd(context, entity);
return createCancelCmd(entity);
case TASK_STOP:
return createStopCmd(context, entity);
return createStopCmd(entity);
default:
return null;
}
@ -72,8 +70,8 @@ public class CmdFactory {
*
* @return {@link StopCmd}
*/
private StopCmd createStopCmd(Context context, DownloadEntity entity) {
return new StopCmd(context, entity);
private StopCmd createStopCmd(DownloadEntity entity) {
return new StopCmd(entity);
}
/**
@ -81,8 +79,8 @@ public class CmdFactory {
*
* @return {@link AddCmd}
*/
private AddCmd createAddCmd(Context context, DownloadEntity entity) {
return new AddCmd(context, entity);
private AddCmd createAddCmd(DownloadEntity entity) {
return new AddCmd(entity);
}
/**
@ -90,8 +88,8 @@ public class CmdFactory {
*
* @return {@link StartCmd}
*/
private StartCmd createStartCmd(Context context, DownloadEntity entity) {
return new StartCmd(context, entity);
private StartCmd createStartCmd(DownloadEntity entity) {
return new StartCmd(entity);
}
/**
@ -99,7 +97,7 @@ public class CmdFactory {
*
* @return {@link CancelCmd}
*/
private CancelCmd createCancelCmd(Context context, DownloadEntity entity) {
return new CancelCmd(context, entity);
private CancelCmd createCancelCmd(DownloadEntity entity) {
return new CancelCmd(entity);
}
}

@ -1,8 +1,8 @@
package com.arialyy.downloadutil.core.command;
import android.content.Context;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadTaskQueue;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.inf.ITaskQueue;
import com.arialyy.downloadutil.help.CheckHelp;
import com.arialyy.downloadutil.util.Util;
@ -11,40 +11,24 @@ import com.arialyy.downloadutil.util.Util;
* 下载命令
*/
public abstract class IDownloadCmd {
DownloadTaskQueue target;
Context mContext;
ITaskQueue mQueue;
DownloadEntity mEntity;
String TAG;
/**
* @param context context
* @param entity 下载实体
*/
protected IDownloadCmd(Context context, DownloadEntity entity) {
IDownloadCmd(DownloadEntity entity) {
if (!CheckHelp.checkDownloadEntity(entity)) {
return;
}
target = DownloadTaskQueue.getInstance();
mContext = context;
mEntity = entity;
TAG = Util.getClassName(this);
}
public Context getContext() {
return mContext;
mQueue = DownloadManager.getInstance().getTaskQueue();
}
/**
* 执行命令
*/
public abstract void executeComment();
/**
* 设置下载器
*
* @param downloadTarget {@link DownloadTaskQueue}
*/
public void setDownloadQueue(DownloadTaskQueue downloadTarget) {
target = downloadTarget;
}
public abstract void executeCmd();
}

@ -10,17 +10,17 @@ import com.arialyy.downloadutil.core.Task;
*/
class StartCmd extends IDownloadCmd {
StartCmd(Context context, DownloadEntity entity) {
super(context, entity);
StartCmd(DownloadEntity entity) {
super(entity);
}
@Override public void executeComment() {
Task task = target.getTask(mEntity);
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
task = target.createTask(mEntity);
task = mQueue.createTask(mEntity);
}
if (task != null) {
target.startTask(task);
mQueue.startTask(task);
}
}
}

@ -1,6 +1,5 @@
package com.arialyy.downloadutil.core.command;
import android.content.Context;
import android.util.Log;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.Task;
@ -12,24 +11,23 @@ import com.arialyy.downloadutil.core.Task;
class StopCmd extends IDownloadCmd {
/**
* @param context context
* @param entity 下载实体
*/
StopCmd(Context context, DownloadEntity entity) {
super(context, entity);
StopCmd(DownloadEntity entity) {
super(entity);
}
@Override public void executeComment() {
Task task = target.getTask(mEntity);
@Override public void executeCmd() {
Task task = mQueue.getTask(mEntity);
if (task == null) {
if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
task = target.createTask(mEntity);
target.stopTask(task);
task = mQueue.createTask(mEntity);
mQueue.stopTask(task);
} else {
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
}
} else {
target.stopTask(task);
mQueue.stopTask(task);
}
}
}

@ -7,7 +7,7 @@ import com.arialyy.downloadutil.core.Task;
* Created by lyy on 2016/8/16.
* 任务功能接口
*/
public interface ITaskQueue {
public interface ITaskQueue extends IDownloader{
/**
* 创建一个新的下载任务创建时只是将新任务存储到缓存池
@ -17,15 +17,6 @@ public interface ITaskQueue {
*/
public Task createTask(DownloadEntity entity);
/**
* 创建一个新的下载任务创建时只是将新任务存储到缓存池
*
* @param entity 下载实体{@link DownloadEntity}
* @param schedulers 下载调度器{@link IDownloadSchedulers}
* @return {@link Task}
*/
public Task createTask(DownloadEntity entity, IDownloadSchedulers schedulers);
/**
* 通过下载链接从缓存池或任务池搜索下载任务如果缓存池或任务池都没有任务则创建新任务
*
@ -34,14 +25,6 @@ public interface ITaskQueue {
*/
public Task getTask(DownloadEntity entity);
/**
* 通过下载链接搜索下载任务
*
* @param entity 下载实体{@link DownloadEntity}
* @return {@code -1 ==> 错误}{@link DownloadEntity#STATE_FAIL}
*/
public int getTaskState(DownloadEntity entity);
/**
* 通过下载链接删除任务
*
@ -55,4 +38,11 @@ public interface ITaskQueue {
* @return 下载任务 or null
*/
public Task getNextTask();
/**
* 设置下载调度器
*
* @param schedulers 下载调度器{@link IDownloadSchedulers}
*/
public void setScheduler(IDownloadSchedulers schedulers);
}

Loading…
Cancel
Save