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. 57
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java
  4. 74
      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. 32
      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.setDownloadUrl(mDownloadUrl);
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk"); mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
List<IDownloadCmd> commands = new ArrayList<>(); List<IDownloadCmd> commands = new ArrayList<>();
IDownloadCmd addCMD = mFactory.createCmd(this, mEntity, CmdFactory.TASK_CREATE); IDownloadCmd addCMD = mFactory.createCmd(mEntity, CmdFactory.TASK_CREATE);
IDownloadCmd startCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_START); IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START);
commands.add(addCMD); commands.add(addCMD);
commands.add(startCmd); commands.add(startCmd);
mManager.setCmds(commands).exe(); mManager.setCmds(commands).exe();
} }
private void stop() { private void stop() {
IDownloadCmd stopCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_STOP); IDownloadCmd stopCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_STOP);
mManager.setCmd(stopCmd).exe(); mManager.setCmd(stopCmd).exe();
} }
private void cancel() { private void cancel() {
IDownloadCmd cancelCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_CANCEL); IDownloadCmd cancelCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_CANCEL);
mManager.setCmd(cancelCmd).exe(); mManager.setCmd(cancelCmd).exe();
} }
} }

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

@ -4,6 +4,7 @@ import android.app.Application;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import com.arialyy.downloadutil.core.command.IDownloadCmd; 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.DbEntity;
import com.arialyy.downloadutil.orm.DbUtil; import com.arialyy.downloadutil.orm.DbUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -17,56 +18,57 @@ public class DownloadManager {
/** /**
* 预处理完成 * 预处理完成
*/ */
public static final String ACTION_PRE = "ACTION_PRE"; public static final String ACTION_PRE = "ACTION_PRE";
/** /**
* 下载开始前事件 * 下载开始前事件
*/ */
public static final String ACTION_POST_PRE = "ACTION_POST_PRE"; public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
/** /**
* 开始下载事件 * 开始下载事件
*/ */
public static final String ACTION_START = "ACTION_START"; public static final String ACTION_START = "ACTION_START";
/** /**
* 恢复下载事件 * 恢复下载事件
*/ */
public static final String ACTION_RESUME = "ACTION_RESUME"; public static final String ACTION_RESUME = "ACTION_RESUME";
/** /**
* 正在下载事件 * 正在下载事件
*/ */
public static final String ACTION_RUNNING = "ACTION_RUNNING"; public static final String ACTION_RUNNING = "ACTION_RUNNING";
/** /**
* 停止下载事件 * 停止下载事件
*/ */
public static final String ACTION_STOP = "ACTION_STOP"; public static final String ACTION_STOP = "ACTION_STOP";
/** /**
* 取消下载事件 * 取消下载事件
*/ */
public static final String ACTION_CANCEL = "ACTION_CANCEL"; public static final String ACTION_CANCEL = "ACTION_CANCEL";
/** /**
* 下载完成事件 * 下载完成事件
*/ */
public static final String ACTION_COMPLETE = "ACTION_COMPLETE"; public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
/** /**
* 下载失败事件 * 下载失败事件
*/ */
public static final String ACTION_FAIL = "ACTION_FAIL"; public static final String ACTION_FAIL = "ACTION_FAIL";
/** /**
* 下载实体 * 下载实体
*/ */
public static final String ENTITY = "DOWNLOAD_ENTITY"; public static final String ENTITY = "DOWNLOAD_ENTITY";
/** /**
* 位置 * 位置
*/ */
public static final String CURRENT_LOCATION = "CURRENT_LOCATION"; public static final String CURRENT_LOCATION = "CURRENT_LOCATION";
/** /**
* 速度 * 速度
*/ */
public static final String CURRENT_SPEED = "CURRENT_SPEED"; public static final String CURRENT_SPEED = "CURRENT_SPEED";
private static final String TAG = "DownloadManager"; private static final String TAG = "DownloadManager";
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static volatile DownloadManager INSTANCE = null; private static volatile DownloadManager INSTANCE = null;
private List<IDownloadCmd> mCommands = new ArrayList<>(); private List<IDownloadCmd> mCommands = new ArrayList<>();
private Context mContext; private Context mContext;
private ITaskQueue mTaskQueue;
private DownloadManager() { private DownloadManager() {
@ -74,7 +76,8 @@ public class DownloadManager {
private DownloadManager(Context context) { private DownloadManager(Context context) {
mContext = context; mContext = context;
DownloadTaskQueue.init(context); DownloadTaskQueue.Builder builder = new DownloadTaskQueue.Builder(context);
mTaskQueue = builder.build();
DbUtil.init(context); DbUtil.init(context);
} }
@ -102,6 +105,13 @@ public class DownloadManager {
return DbEntity.findAllData(DownloadEntity.class); return DbEntity.findAllData(DownloadEntity.class);
} }
/**
* 获取任务队列
*/
public ITaskQueue getTaskQueue() {
return mTaskQueue;
}
/** /**
* 设置命令 * 设置命令
*/ */
@ -125,8 +135,17 @@ public class DownloadManager {
*/ */
public synchronized void exe() { public synchronized void exe() {
for (IDownloadCmd command : mCommands) { for (IDownloadCmd command : mCommands) {
command.executeComment(); command.executeCmd();
} }
mCommands.clear(); 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. * 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 String TAG = "DownloadTaskQueue";
private static final Object LOCK = new Object(); private CachePool mCachePool = CachePool.getInstance();
private static volatile DownloadTaskQueue INSTANCE = null; private ExecutePool mExecutePool = ExecutePool.getInstance();
private CachePool mCachePool = CachePool.getInstance(); private Context mContext;
private ExecutePool mExecutePool = ExecutePool.getInstance(); private IDownloadSchedulers mSchedulers;
private Context mContext;
private DownloadTaskQueue() { private DownloadTaskQueue() {
} }
@ -28,22 +27,6 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader {
mContext = context; 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) { @Override public Task createTask(DownloadEntity entity) {
return createTask(entity, null); Task task = TaskFactory.getInstance().createTask(mContext, entity, mSchedulers);
}
@Override public Task createTask(DownloadEntity entity, IDownloadSchedulers schedulers) {
if (schedulers == null) {
schedulers = DownloadSchedulers.getInstance(this);
}
Task task = TaskFactory.getInstance().createTask(mContext, entity, schedulers);
mCachePool.putTask(task); mCachePool.putTask(task);
return task; return task;
} }
@ -129,15 +105,6 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader {
return task; 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) { @Override public void removeTask(DownloadEntity entity) {
Task task = mExecutePool.getTask(entity.getDownloadUrl()); Task task = mExecutePool.getTask(entity.getDownloadUrl());
if (task != null) { if (task != null) {
@ -155,4 +122,31 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader {
@Override public Task getNextTask() { @Override public Task getNextTask() {
return mCachePool.pollTask(); 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; package com.arialyy.downloadutil.core.command;
import android.content.Context;
import android.util.Log; import android.util.Log;
import com.arialyy.downloadutil.core.DownloadEntity; import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.Task; import com.arialyy.downloadutil.core.Task;
@ -11,14 +10,14 @@ import com.arialyy.downloadutil.core.Task;
*/ */
class AddCmd extends IDownloadCmd { class AddCmd extends IDownloadCmd {
AddCmd(Context context, DownloadEntity entity) { AddCmd(DownloadEntity entity) {
super(context, entity); super(entity);
} }
@Override public void executeComment() { @Override public void executeCmd() {
Task task = target.getTask(mEntity); Task task = mQueue.getTask(mEntity);
if (task == null) { if (task == null) {
target.createTask(mEntity); mQueue.createTask(mEntity);
} else { } else {
Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
} }

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

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

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

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

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

@ -7,7 +7,7 @@ import com.arialyy.downloadutil.core.Task;
* Created by lyy on 2016/8/16. * 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); 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); 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 * @return 下载任务 or null
*/ */
public Task getNextTask(); public Task getNextTask();
/**
* 设置下载调度器
*
* @param schedulers 下载调度器{@link IDownloadSchedulers}
*/
public void setScheduler(IDownloadSchedulers schedulers);
} }

Loading…
Cancel
Save