From 3667fc107bb022a448289126b8c431372321b1af Mon Sep 17 00:00:00 2001 From: lyy <511455842@qq.com> Date: Thu, 3 Nov 2016 16:29:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simple/activity/SingleTaskActivity.java | 8 +- .../simple/adapter/DownloadAdapter.java | 8 +- .../downloadutil/core/DownloadManager.java | 57 +++++++++----- .../downloadutil/core/DownloadTaskQueue.java | 74 +++++++++---------- .../downloadutil/core/command/AddCmd.java | 11 ++- .../downloadutil/core/command/CancelCmd.java | 12 +-- .../downloadutil/core/command/CmdFactory.java | 28 ++++--- .../core/command/IDownloadCmd.java | 32 ++------ .../downloadutil/core/command/StartCmd.java | 12 +-- .../downloadutil/core/command/StopCmd.java | 16 ++-- .../downloadutil/core/inf/ITaskQueue.java | 26 ++----- 11 files changed, 133 insertions(+), 151 deletions(-) diff --git a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java index 3cf14841..872d8c49 100644 --- a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java @@ -205,20 +205,20 @@ public class SingleTaskActivity extends BaseActivity { mEntity.setDownloadUrl(mDownloadUrl); mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk"); List 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(); } } diff --git a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java index 91f29b50..50c8d3c5 100644 --- a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java @@ -39,7 +39,7 @@ public class DownloadAdapter extends AbsRVAdapter mCommands = new ArrayList<>(); - private Context mContext; + public static final String CURRENT_SPEED = "CURRENT_SPEED"; + private static final String TAG = "DownloadManager"; + private static final Object LOCK = new Object(); + private static volatile DownloadManager INSTANCE = null; + private List 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; + } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java index 4daf4235..53571ee2 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java @@ -12,13 +12,12 @@ import com.arialyy.downloadutil.core.pool.ExecutePool; * Created by lyy on 2016/8/17. * 下载任务队列 */ -public class DownloadTaskQueue implements ITaskQueue, IDownloader { - 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; +final class DownloadTaskQueue implements ITaskQueue { + private static final String TAG = "DownloadTaskQueue"; + 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; + } + } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/AddCmd.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/AddCmd.java index 08144ecc..00e77fec 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/AddCmd.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/AddCmd.java @@ -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, "添加命令执行失败,【该任务已经存在】"); } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CancelCmd.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CancelCmd.java index 1c0c1d50..48368a59 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CancelCmd.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CancelCmd.java @@ -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); } } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java index b1bdbe66..5674dde0 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java @@ -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); } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/IDownloadCmd.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/IDownloadCmd.java index fb5e59ae..9f734585 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/IDownloadCmd.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/IDownloadCmd.java @@ -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; - DownloadEntity mEntity; - String TAG; + 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(); } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StartCmd.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StartCmd.java index 0ffdbbb8..80032e53 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StartCmd.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StartCmd.java @@ -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); } } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StopCmd.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StopCmd.java index 865228d8..f8fc1093 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StopCmd.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StopCmd.java @@ -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); } } } \ No newline at end of file diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITaskQueue.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITaskQueue.java index 549d05c7..2fa7517f 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITaskQueue.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITaskQueue.java @@ -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); }