From 78b76f539907ad47ca64f58f33c4183167051965 Mon Sep 17 00:00:00 2001 From: lyy <511455842@qq.com> Date: Tue, 16 Aug 2016 23:12:39 +0800 Subject: [PATCH] =?UTF-8?q?v=5F2.0=20=E5=BC=80=E5=8F=91=EF=BC=8C=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=B1=A0=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 | 33 ++++++++++++++++ .../com/arialyy/downloadutil/core/Task.java | 38 ++++++++++++++++++- .../downloadutil/core/inf/IDownloader.java | 31 +++++++++++++++ .../arialyy/downloadutil/core/inf/IPool.java | 7 ++++ .../arialyy/downloadutil/core/inf/ITask.java | 34 +++++++++++++++++ .../downloadutil/core/pool/CachePool.java | 5 +++ .../downloadutil/core/pool/ExecutePool.java | 5 +++ 7 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloader.java create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.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 new file mode 100644 index 00000000..31819ba9 --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java @@ -0,0 +1,33 @@ +package com.arialyy.downloadutil.core; + +import com.arialyy.downloadutil.core.inf.IDownloader; +import com.arialyy.downloadutil.core.inf.ITask; +import com.arialyy.downloadutil.core.pool.CachePool; +import com.arialyy.downloadutil.core.pool.ExecutePool; + +/** + * Created by lyy on 2016/8/16. + * 任务下载器,提供抽象的方法供具体的实现类操作 + */ +public abstract class DownloadTarget implements IDownloader, ITask { + protected CachePool mCachePool = CachePool.getInstance(); + protected ExecutePool mExecutePool = ExecutePool.getInstance(); + + /** + * 获取当前运行的任务数 + * + * @return 当前正在执行的任务数 + */ + public int getCurrentTaskNum() { + return mExecutePool.size(); + } + + /** + * 获取缓存任务数 + * + * @return 获取缓存的任务数 + */ + public int getCacheTaskNum() { + return mCachePool.size(); + } +} 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 a97d22a6..ee26af3c 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java @@ -17,7 +17,27 @@ import java.net.HttpURLConnection; * 下载任务类 */ public class Task { - public static final String TAG = "Task"; + public static final String TAG = "Task"; + /** + * 任务开始 + */ + public static final int START = 1; + /** + * 任务停止 + */ + public static final int STOP = 2; + /** + * 任务失败 + */ + public static final int FAILE = 3; + /** + * 任务取消 + */ + public static final int CANCEL = 4; + /** + * 任务完成 + */ + public static final int COMPLETE = 5; DownloadEntity downloadEntity; IDownloadListener listener; Handler outHandler; @@ -101,6 +121,7 @@ public class Task { public void onStart(long startLocation) { super.onStart(startLocation); downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING); + sendInState2Target(START); sendIntent(DownloadManager.ACTION_START, startLocation); } @@ -117,6 +138,7 @@ public class Task { public void onStop(long stopLocation) { super.onStop(stopLocation); downloadEntity.setState(DownloadEntity.STATE_STOP); + sendInState2Target(STOP); sendIntent(DownloadManager.ACTION_STOP, stopLocation); } @@ -124,6 +146,7 @@ public class Task { public void onCancel() { super.onCancel(); downloadEntity.setState(DownloadEntity.STATE_CANCEL); + sendInState2Target(CANCEL); sendIntent(DownloadManager.ACTION_CANCEL, -1); downloadEntity.deleteData(); } @@ -133,6 +156,7 @@ public class Task { super.onComplete(); downloadEntity.setState(DownloadEntity.STATE_COMPLETE); downloadEntity.setDownloadComplete(true); + sendInState2Target(COMPLETE); sendIntent(DownloadManager.ACTION_COMPLETE, -1); } @@ -140,9 +164,21 @@ public class Task { public void onFail() { super.onFail(); downloadEntity.setState(DownloadEntity.STATE_FAIL); + sendInState2Target(FAILE); sendIntent(DownloadManager.ACTION_FAIL, -1); } + /** + * 将任务状态发送给下载器 + * + * @param state {@link Task#START} + */ + private void sendInState2Target(int state) { + if (outHandler != null) { + outHandler.obtainMessage(state, downloadEntity).sendToTarget(); + } + } + private void sendIntent(String action, long location) { downloadEntity.save(); Intent intent = new Intent(); diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloader.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloader.java new file mode 100644 index 00000000..a8b71c97 --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloader.java @@ -0,0 +1,31 @@ +package com.arialyy.downloadutil.core.inf; + +import com.arialyy.downloadutil.core.Task; + +/** + * Created by lyy on 2016/8/16. + * 下载功能接口 + */ +public interface IDownloader { + /** + * 开始任务 + * + * @param task {@link Task} + */ + public void startTask(Task task); + + /** + * 停止任务 + * + * @param task {@link Task} + */ + public void stopTask(Task task); + + /** + * 取消任务 + * + * @param task {@link Task} + */ + public void cancelTask(Task task); + +} diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IPool.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IPool.java index 3e4aed3c..bf85bb92 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IPool.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IPool.java @@ -36,4 +36,11 @@ public interface IPool { * @return true:移除成功 */ public boolean removeTask(Task task); + + /** + * 池子大小 + * + * @return 返回缓存池或者当前任务池大小 + */ + public int size(); } 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 new file mode 100644 index 00000000..ee9c3171 --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.java @@ -0,0 +1,34 @@ +package com.arialyy.downloadutil.core.inf; + +import com.arialyy.downloadutil.core.Task; + +/** + * Created by lyy on 2016/8/16. + * 任务功能接口 + */ +public interface ITask { + + /** + * 创建一个新的下载任务 + * + * @param downloadUrl 下载链接 + * @param downloadPath 保存路径 + */ + public void createTask(String downloadUrl, String downloadPath); + + /** + * 通过下载链接从缓存池或任务池搜索下载任务 + * + * @param downloadUrl 下载链接 + * @return {@link Task} + */ + public Task searchTask(String downloadUrl); + + /** + * 通过下载链接搜索下载任务 + * + * @param downloadUrl 下载链接 + * @return {@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 5f3d721a..b16124b8 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 @@ -96,4 +96,9 @@ public class CachePool implements IPool { } } } + + @Override + public int size() { + return mCacheQueue.size(); + } } 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 012d6efd..e6bd80c1 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 @@ -141,4 +141,9 @@ public class ExecutePool implements IPool { } } + @Override + public int size() { + return mExecuteQueue.size(); + } + }