diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java index 803bade3..a921a768 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java @@ -3,6 +3,7 @@ package com.arialyy.downloadutil.core; import android.content.Context; import android.util.Log; import android.util.SparseArray; +import com.arialyy.downloadutil.core.inf.IDownloadUtil; import com.arialyy.downloadutil.util.Util; import java.io.File; import java.io.IOException; @@ -20,56 +21,79 @@ import java.util.concurrent.Executors; * Created by lyy on 2015/8/25. * 下载工具类 */ -final class DownLoadUtil { - private static final String TAG = "DownLoadUtil"; +final class DownloadUtil implements IDownloadUtil { + private static final String TAG = "DownloadUtil"; private static final Object LOCK = new Object(); //下载监听 - private IDownloadListener mListener; + private IDownloadListener mListener; /** * 线程数 */ - private static final int THREAD_NUM = 3; - private static final int TIME_OUT = 5000; //超时时间 + private final int THREAD_NUM; + private int mConnectTimeOut = 5000 * 4; //连接超时时间 + private int mReadTimeOut = 5000 * 20; //流读取的超时时间 /** * 已经完成下载任务的线程数量 */ - private int mCompleteThreadNum = 0; - private boolean isDownloading = false; - private boolean isStop = false; - private boolean isCancel = false; - private long mCurrentLocation; - boolean isNewTask = true; - private int mCancelNum = 0; - private int mStopNum = 0; - private Context mContext; - private DownloadEntity mDownloadEntity; - private ExecutorService mFixedThreadPool = Executors.newFixedThreadPool(THREAD_NUM); - private SparseArray mTask = new SparseArray<>(); + private int mCompleteThreadNum = 0; + private boolean isDownloading = false; + private boolean isStop = false; + private boolean isCancel = false; + private boolean isNewTask = true; + private int mCancelNum = 0; + private long mCurrentLocation = 0; + private int mStopNum = 0; + private Context mContext; + private DownloadEntity mDownloadEntity; + private ExecutorService mFixedThreadPool; + private SparseArray mTask = new SparseArray<>(); - public DownLoadUtil(Context context, DownloadEntity entity) { + DownloadUtil(Context context, DownloadEntity entity, IDownloadListener downloadListener) { + this(context, entity, downloadListener, 3); + } + + DownloadUtil(Context context, DownloadEntity entity, IDownloadListener downloadListener, + int threadNum) { mContext = context.getApplicationContext(); mDownloadEntity = entity; + mListener = downloadListener; + THREAD_NUM = threadNum; + mFixedThreadPool = Executors.newFixedThreadPool(THREAD_NUM); } public IDownloadListener getListener() { return mListener; } + /** + * 设置连接超时时间 + */ + public void setConnectTimeOut(int timeOut) { + mConnectTimeOut = timeOut; + } + + /** + * 设置流读取的超时时间 + */ + public void setReadTimeOut(int readTimeOut) { + mReadTimeOut = readTimeOut; + } + /** * 获取当前下载位置 */ - public long getCurrentLocation() { + @Override public long getCurrentLocation() { return mCurrentLocation; } - public boolean isDownloading() { + @Override public boolean isDownloading() { return isDownloading; } /** * 取消下载 */ - public void cancelDownload() { + @Override public void cancelDownload() { isCancel = true; isDownloading = false; mFixedThreadPool.shutdown(); @@ -84,7 +108,7 @@ final class DownLoadUtil { /** * 停止下载 */ - public void stopDownload() { + @Override public void stopDownload() { isStop = true; isDownloading = false; mFixedThreadPool.shutdown(); @@ -99,7 +123,7 @@ final class DownLoadUtil { /** * 删除下载记录文件 */ - public void delConfigFile() { + @Override public void delConfigFile() { if (mContext != null && mDownloadEntity != null) { File dFile = new File(mDownloadEntity.getDownloadPath()); File config = @@ -113,7 +137,7 @@ final class DownLoadUtil { /** * 删除temp文件 */ - public void delTempFile() { + @Override public void delTempFile() { if (mContext != null && mDownloadEntity != null) { File dFile = new File(mDownloadEntity.getDownloadPath()); if (dFile.exists()) { @@ -123,12 +147,9 @@ final class DownLoadUtil { } /** - * 多线程断点续传下载文件,暂停和继续 - * - * @param downloadListener 下载进度监听 {@link DownloadListener} + * 多线程断点续传下载文件,开始下载 */ - public void start(IDownloadListener downloadListener) { - mListener = downloadListener; + @Override public void startDownload() { isDownloading = true; mCurrentLocation = 0; isStop = false; @@ -160,7 +181,7 @@ final class DownLoadUtil { URL url = new URL(downloadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); setConnectParam(conn); - conn.setConnectTimeout(TIME_OUT * 4); + conn.setConnectTimeout(mConnectTimeOut * 4); conn.connect(); int len = conn.getContentLength(); if (len < 0) { //网络被劫持时会出现这个问题 @@ -273,6 +294,10 @@ final class DownLoadUtil { }).start(); } + @Override public void resumeDownload() { + startDownload(); + } + private void failDownload(String msg) { Log.e(TAG, msg); isDownloading = false; @@ -323,8 +348,8 @@ final class DownLoadUtil { conn.setRequestProperty("Range", "bytes=" + dEntity.startLocation + "-" + dEntity.endLocation); setConnectParam(conn); - conn.setConnectTimeout(TIME_OUT * 4); - conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数 + conn.setConnectTimeout(mConnectTimeOut); + conn.setReadTimeout(mReadTimeOut); //设置读取流的等待时间,必须设置该参数 is = conn.getInputStream(); //创建可设置位置的文件 RandomAccessFile file = new RandomAccessFile(dEntity.tempFile, "rwd"); @@ -469,7 +494,7 @@ final class DownLoadUtil { /** * 子线程下载信息类 */ - private class ConfigEntity { + private static class ConfigEntity { //文件大小 long fileSize; String downloadUrl; @@ -479,8 +504,8 @@ final class DownLoadUtil { File tempFile; Context context; - public ConfigEntity(Context context, long fileSize, String downloadUrl, File file, int threadId, - long startLocation, long endLocation) { + private ConfigEntity(Context context, long fileSize, String downloadUrl, File file, + int threadId, long startLocation, long endLocation) { this.fileSize = fileSize; this.downloadUrl = downloadUrl; this.tempFile = file; @@ -490,51 +515,4 @@ final class DownLoadUtil { this.context = context; } } - - public static class DownloadListener implements IDownloadListener { - - @Override public void onResume(long resumeLocation) { - - } - - @Override public void onCancel() { - - } - - @Override public void onFail() { - - } - - @Override public void onPre() { - - } - - @Override public void onPostPre(long fileSize) { - - } - - @Override public void onProgress(long currentLocation) { - - } - - @Override public void onChildComplete(long finishLocation) { - - } - - @Override public void onStart(long startLocation) { - - } - - @Override public void onChildResume(long resumeLocation) { - - } - - @Override public void onStop(long stopLocation) { - - } - - @Override public void onComplete() { - - } - } } \ No newline at end of file diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadListener.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadListener.java new file mode 100644 index 00000000..c3d1c08c --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadListener.java @@ -0,0 +1,48 @@ +package com.arialyy.downloadutil.core; + +public class DownloadListener implements IDownloadListener { + + @Override public void onResume(long resumeLocation) { + + } + + @Override public void onCancel() { + + } + + @Override public void onFail() { + + } + + @Override public void onPre() { + + } + + @Override public void onPostPre(long fileSize) { + + } + + @Override public void onProgress(long currentLocation) { + + } + + @Override public void onChildComplete(long finishLocation) { + + } + + @Override public void onStart(long startLocation) { + + } + + @Override public void onChildResume(long resumeLocation) { + + } + + @Override public void onStop(long stopLocation) { + + } + + @Override public void onComplete() { + + } +} \ No newline at end of file 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 3ddce890..f5bafbb5 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.net.Uri; import android.os.Handler; import android.util.Log; +import com.arialyy.downloadutil.core.inf.IDownloadUtil; /** * Created by lyy on 2016/8/11. @@ -17,12 +18,17 @@ public class Task { private IDownloadListener mListener; private Handler mOutHandler; private Context mContext; - private DownLoadUtil mUtil; + private IDownloadUtil mUtil; private Task(Context context, DownloadEntity entity) { mContext = context.getApplicationContext(); mEntity = entity; - mUtil = new DownLoadUtil(context, entity); + init(); + } + + private void init() { + mListener = new DListener(mContext, mEntity, mOutHandler); + mUtil = new DownloadUtil(mContext, mEntity, mListener); } /** @@ -33,9 +39,9 @@ public class Task { Log.d(TAG, "任务正在下载"); } else { if (mListener == null) { - mListener = new DownloadListener(mContext, mEntity, mOutHandler); + mListener = new DListener(mContext, mEntity, mOutHandler); } - mUtil.start(mListener); + mUtil.startDownload(); } } @@ -65,7 +71,7 @@ public class Task { /** * 获取下载工具 */ - public DownLoadUtil getDownloadUtil() { + public IDownloadUtil getDownloadUtil() { return mUtil; } @@ -123,7 +129,7 @@ public class Task { /** * 下载监听类 */ - private class DownloadListener extends DownLoadUtil.DownloadListener { + private class DListener extends DownloadListener { Handler outHandler; Context context; Intent sendIntent; @@ -134,7 +140,7 @@ public class Task { boolean isFirst = true; DownloadEntity downloadEntity; - DownloadListener(Context context, DownloadEntity downloadEntity, Handler outHandler) { + DListener(Context context, DownloadEntity downloadEntity, Handler outHandler) { this.context = context; this.outHandler = outHandler; this.downloadEntity = downloadEntity; @@ -234,29 +240,47 @@ public class Task { } public static class Builder { - DownloadEntity downloadEntity; - IDownloadListener listener; - Handler outHandler; - Context context; + DownloadEntity downloadEntity; + Handler outHandler; + Context context; + int threadNum = 3; + IDownloadUtil downloadUtil; public Builder(Context context, DownloadEntity downloadEntity) { this.context = context; this.downloadEntity = downloadEntity; } - public Builder setDownloadListener(IDownloadListener listener) { - this.listener = listener; - return this; - } - + /** + * 设置自定义Handler处理下载状态时间 + * + * @param outHandler {@link IDownloadTarget.AutoTaskHandler} + */ public Builder setOutHandler(Handler outHandler) { this.outHandler = outHandler; return this; } + /** + * 设置线程数 + */ + public Builder setThreadNum(int threadNum) { + this.threadNum = threadNum; + return this; + } + + ///** + // * 设置自定义下载工具 + // * + // * @param downloadUtil {@link IDownloadUtil} + // */ + //public Builder setDownloadUtil(IDownloadUtil downloadUtil) { + // this.downloadUtil = downloadUtil; + // return this; + //} + public Task build() { Task task = new Task(context, downloadEntity); - task.mListener = listener; task.mOutHandler = outHandler; downloadEntity.save(); return task; diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloadUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloadUtil.java new file mode 100644 index 00000000..83915bd0 --- /dev/null +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/IDownloadUtil.java @@ -0,0 +1,50 @@ +package com.arialyy.downloadutil.core.inf; + +/** + * Created by “AriaLyy@outlook.com” on 2016/10/31. + * 抽象的下载接口 + */ +public interface IDownloadUtil { + + /** + * 获取当前下载位置 + */ + public long getCurrentLocation(); + + /** + * 是否正在下载 + * + * @return true, 正在下载 + */ + public boolean isDownloading(); + + /** + * 取消下载 + */ + public void cancelDownload(); + + /** + * 停止下载 + */ + public void stopDownload(); + + /** + * 开始下载 + */ + public void startDownload(); + + /** + * 从上次断点恢复下载 + */ + public void resumeDownload(); + + /** + * 删除下载记录文件 + */ + public void delConfigFile(); + + /** + * 删除temp文件 + */ + public void delTempFile(); +}