From 01ba765278e747b55fd7820032b20ca7033cec10 Mon Sep 17 00:00:00 2001 From: lyy <511455842@qq.com> Date: Thu, 18 Aug 2016 22:30:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=99=A8=E7=BC=96=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../downloadutil/core/DownloadTarget.java | 80 ++++++++++++++++--- .../downloadutil/core/IDownloadTarget.java | 30 +++++++ .../com/arialyy/downloadutil/core/Task.java | 7 ++ .../arialyy/downloadutil/core/TaskEnum.java | 9 +++ .../downloadutil/core/TaskFactory.java | 44 ++++++++++ .../arialyy/downloadutil/core/inf/ITask.java | 4 +- .../downloadutil/core/pool/CachePool.java | 7 +- .../downloadutil/core/pool/ExecutePool.java | 10 +-- 8 files changed, 162 insertions(+), 29 deletions(-) create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskEnum.java create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskFactory.java diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java index 6de35bb8..3ec260ce 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java @@ -1,69 +1,125 @@ package com.arialyy.downloadutil.core; +import android.content.Context; +import android.util.Log; + +import com.arialyy.downloadutil.entity.DownloadEntity; + /** * Created by lyy on 2016/8/17. * 下载任务调度类 */ public class DownloadTarget extends IDownloadTarget { - + private static final String TAG = "DownloadTarget"; private static final Object LOCK = new Object(); private static volatile DownloadTarget INSTANCE = null; + private Context mContext; public static DownloadTarget getInstance() { + if (INSTANCE == null) { + Log.e(TAG, "请在Application中调用DownloadTarget.init()方法注册下载器"); + return null; + } + return INSTANCE; + } + + /** + * 初始化下载器 + * + * @param context 全局Context + */ + public static void init(Context context) { if (INSTANCE == null) { synchronized (LOCK) { - INSTANCE = new DownloadTarget(); + INSTANCE = new DownloadTarget(context); } } - return INSTANCE; } private DownloadTarget() { + super(); + } + private DownloadTarget(Context context) { + super(); + mContext = context; } @Override public void startTask(Task task) { - + if (mExecutePool.putTask(task)) { + task.start(); + } } @Override public void stopTask(Task task) { - + if (mExecutePool.removeTask(task)) { + task.stop(); + } } @Override public void cancelTask(Task task) { - + if (mExecutePool.removeTask(task)) { + task.cancel(); + } } @Override public void reTryStart(Task task) { - + if (!task.getDownloadUtil().isDownloading()) { + task.start(); + } else { + Log.w(TAG, "任务没有完全停止,重试下载失败"); + } } @Override public void createTask(String downloadUrl, String downloadPath) { - + DownloadEntity entity = new DownloadEntity(); + entity.setDownloadUrl(downloadUrl); + entity.setDownloadPath(downloadPath); + Task task = TaskFactory.getInstance().createTask(mContext, entity, mTaskHandler); + mCachePool.putTask(task); } @Override public Task getTask(String downloadUrl) { - return null; + Task task = mCachePool.getTask(downloadUrl); + if (task == null) { + task = mExecutePool.getTask(downloadUrl); + } + return task; } @Override public int getTaskState(String downloadUrl) { - return 0; + Task task = getTask(downloadUrl); + if (task == null) { + Log.e(TAG, "没有找到下载链接为【" + downloadUrl + "】的下载任务"); + return -1; + } + return task.getDownloadEntity().getState(); } @Override public void removeTask(String downloadUrl) { - + Task task = mCachePool.getTask(downloadUrl); + if (task != null) { + Log.d(TAG, "任务删除" + (mCachePool.removeTask(task) ? "成功" : "失败")); + } else { + task = mExecutePool.getTask(downloadUrl); + } + if (task != null) { + Log.d(TAG, "任务删除" + (mCachePool.removeTask(task) ? "成功" : "失败")); + } else { + Log.w(TAG, "没有找到下载链接为【" + downloadUrl + "】的任务"); + } } @Override public Task getNextTask() { - return null; + return mCachePool.pollTask(); } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/IDownloadTarget.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/IDownloadTarget.java index 59928fbf..bee0e374 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/IDownloadTarget.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/IDownloadTarget.java @@ -166,6 +166,36 @@ public abstract class IDownloadTarget implements IDownloader, ITask { handleFailTask(entity); break; } + callback(msg.what, entity); + } + + /** + * 回调 + * + * @param state 状态 + * @param entity 下载实体 + */ + private void callback(int state, DownloadEntity entity) { + if (target.mTargetListener != null) { + Task task = target.getTask(entity.getDownloadUrl()); + switch (state) { + case START: + target.mTargetListener.onTaskStart(task); + break; + case STOP: + target.mTargetListener.onTaskStop(task); + break; + case CANCEL: + target.mTargetListener.onTaskCancel(task); + break; + case COMPLETE: + target.mTargetListener.onTaskCancel(task); + break; + case FAIL: + target.mTargetListener.onTaskFail(task); + break; + } + } } /** diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java index c30b3b93..bbd7a997 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java @@ -55,6 +55,13 @@ public class Task { } } + /** + * 获取下载工具 + */ + public DownLoadUtil getDownloadUtil() { + return util; + } + /** * 取消下载 */ diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskEnum.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskEnum.java new file mode 100644 index 00000000..d6e52c2f --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskEnum.java @@ -0,0 +1,9 @@ +package com.arialyy.downloadutil.core; + +/** + * Created by lyy on 2016/8/18. + * 创建任务类型 + */ +public enum TaskEnum { + ORDINARY, NO_USR_CALLBACK +} diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskFactory.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskFactory.java new file mode 100644 index 00000000..25e7680c --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskFactory.java @@ -0,0 +1,44 @@ +package com.arialyy.downloadutil.core; + +import android.content.Context; +import android.os.Handler; + +import com.arialyy.downloadutil.entity.DownloadEntity; + +/** + * Created by lyy on 2016/8/18. + * 任务工厂 + */ +public class TaskFactory { + private static final String TAG = "TaskFactory"; + + private static final Object LOCK = new Object(); + private static volatile TaskFactory INSTANCE = null; + + private TaskFactory() { + + } + + public static TaskFactory getInstance() { + if (INSTANCE == null) { + synchronized (LOCK) { + INSTANCE = new TaskFactory(); + } + } + return INSTANCE; + } + + /** + * 创建普通下载任务 + * @param context + * @param entity 下载实体 + * @param handler {@link com.arialyy.downloadutil.core.IDownloadTarget.AutoTaskHandler} + * @return + */ + public Task createTask(Context context, DownloadEntity entity, Handler handler) { + Task.Builder builder = new Task.Builder(context, entity); + builder.setOutHandler(handler); + return builder.builder(); + } + +} diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.java index b1ab819d..27c49d10 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.java @@ -9,7 +9,7 @@ import com.arialyy.downloadutil.core.Task; public interface ITask { /** - * 创建一个新的下载任务 + * 创建一个新的下载任务,创建时只是将新任务存储到缓存池 * * @param downloadUrl 下载链接 * @param downloadPath 保存路径 @@ -28,7 +28,7 @@ public interface ITask { * 通过下载链接搜索下载任务 * * @param downloadUrl 下载链接 - * @return {@link com.arialyy.downloadutil.entity.DownloadEntity#STATE_FAIL} + * @return {@code -1 ==> 错误},{@link com.arialyy.downloadutil.entity.DownloadEntity#STATE_FAIL} */ public int getTaskState(String downloadUrl); diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java index 53ba514b..aab9ef72 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java @@ -79,12 +79,7 @@ public class CachePool implements IPool { return null; } String key = Util.keyToHashKey(downloadUrl); - Task task = mCacheArray.get(key); - if (task != null) { - mCacheArray.remove(key); - mCacheQueue.remove(task); - } - return task; + return mCacheArray.get(key); } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java index 16e3d25a..7a8789e9 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java @@ -105,7 +105,6 @@ public class ExecutePool implements IPool { synchronized (LOCK) { Task task = mExecuteQueue.poll(); if (task != null) { - task.stop(); String url = task.getDownloadEntity().getDownloadUrl(); mExecuteArray.remove(Util.keyToHashKey(url)); } @@ -121,13 +120,7 @@ public class ExecutePool implements IPool { return null; } String key = Util.keyToHashKey(downloadUrl); - Task task = mExecuteArray.get(key); - if (task != null) { - task.stop(); - mExecuteArray.remove(key); - mExecuteQueue.remove(task); - } - return task; + return mExecuteArray.get(key); } } @@ -138,7 +131,6 @@ public class ExecutePool implements IPool { Log.e(TAG, "任务不能为空"); return false; } else { - task.stop(); String key = Util.keyToHashKey(task.getDownloadEntity().getDownloadUrl()); mExecuteArray.remove(key); return mExecuteQueue.remove(task);