优化
This commit is contained in:
@ -3,6 +3,7 @@ package com.arialyy.downloadutil.core;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
|
import com.arialyy.downloadutil.core.inf.IDownloadUtil;
|
||||||
import com.arialyy.downloadutil.util.Util;
|
import com.arialyy.downloadutil.util.Util;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -20,56 +21,79 @@ import java.util.concurrent.Executors;
|
|||||||
* Created by lyy on 2015/8/25.
|
* Created by lyy on 2015/8/25.
|
||||||
* 下载工具类
|
* 下载工具类
|
||||||
*/
|
*/
|
||||||
final class DownLoadUtil {
|
final class DownloadUtil implements IDownloadUtil {
|
||||||
private static final String TAG = "DownLoadUtil";
|
private static final String TAG = "DownloadUtil";
|
||||||
private static final Object LOCK = new Object();
|
private static final Object LOCK = new Object();
|
||||||
//下载监听
|
//下载监听
|
||||||
private IDownloadListener mListener;
|
private IDownloadListener mListener;
|
||||||
/**
|
/**
|
||||||
* 线程数
|
* 线程数
|
||||||
*/
|
*/
|
||||||
private static final int THREAD_NUM = 3;
|
private final int THREAD_NUM;
|
||||||
private static final int TIME_OUT = 5000; //超时时间
|
private int mConnectTimeOut = 5000 * 4; //连接超时时间
|
||||||
|
private int mReadTimeOut = 5000 * 20; //流读取的超时时间
|
||||||
/**
|
/**
|
||||||
* 已经完成下载任务的线程数量
|
* 已经完成下载任务的线程数量
|
||||||
*/
|
*/
|
||||||
private int mCompleteThreadNum = 0;
|
private int mCompleteThreadNum = 0;
|
||||||
private boolean isDownloading = false;
|
private boolean isDownloading = false;
|
||||||
private boolean isStop = false;
|
private boolean isStop = false;
|
||||||
private boolean isCancel = false;
|
private boolean isCancel = false;
|
||||||
private long mCurrentLocation;
|
private boolean isNewTask = true;
|
||||||
boolean isNewTask = true;
|
private int mCancelNum = 0;
|
||||||
private int mCancelNum = 0;
|
private long mCurrentLocation = 0;
|
||||||
private int mStopNum = 0;
|
private int mStopNum = 0;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private DownloadEntity mDownloadEntity;
|
private DownloadEntity mDownloadEntity;
|
||||||
private ExecutorService mFixedThreadPool = Executors.newFixedThreadPool(THREAD_NUM);
|
private ExecutorService mFixedThreadPool;
|
||||||
private SparseArray<Runnable> mTask = new SparseArray<>();
|
private SparseArray<Runnable> 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();
|
mContext = context.getApplicationContext();
|
||||||
mDownloadEntity = entity;
|
mDownloadEntity = entity;
|
||||||
|
mListener = downloadListener;
|
||||||
|
THREAD_NUM = threadNum;
|
||||||
|
mFixedThreadPool = Executors.newFixedThreadPool(THREAD_NUM);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDownloadListener getListener() {
|
public IDownloadListener getListener() {
|
||||||
return mListener;
|
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;
|
return mCurrentLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDownloading() {
|
@Override public boolean isDownloading() {
|
||||||
return isDownloading;
|
return isDownloading;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取消下载
|
* 取消下载
|
||||||
*/
|
*/
|
||||||
public void cancelDownload() {
|
@Override public void cancelDownload() {
|
||||||
isCancel = true;
|
isCancel = true;
|
||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
mFixedThreadPool.shutdown();
|
mFixedThreadPool.shutdown();
|
||||||
@ -84,7 +108,7 @@ final class DownLoadUtil {
|
|||||||
/**
|
/**
|
||||||
* 停止下载
|
* 停止下载
|
||||||
*/
|
*/
|
||||||
public void stopDownload() {
|
@Override public void stopDownload() {
|
||||||
isStop = true;
|
isStop = true;
|
||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
mFixedThreadPool.shutdown();
|
mFixedThreadPool.shutdown();
|
||||||
@ -99,7 +123,7 @@ final class DownLoadUtil {
|
|||||||
/**
|
/**
|
||||||
* 删除下载记录文件
|
* 删除下载记录文件
|
||||||
*/
|
*/
|
||||||
public void delConfigFile() {
|
@Override public void delConfigFile() {
|
||||||
if (mContext != null && mDownloadEntity != null) {
|
if (mContext != null && mDownloadEntity != null) {
|
||||||
File dFile = new File(mDownloadEntity.getDownloadPath());
|
File dFile = new File(mDownloadEntity.getDownloadPath());
|
||||||
File config =
|
File config =
|
||||||
@ -113,7 +137,7 @@ final class DownLoadUtil {
|
|||||||
/**
|
/**
|
||||||
* 删除temp文件
|
* 删除temp文件
|
||||||
*/
|
*/
|
||||||
public void delTempFile() {
|
@Override public void delTempFile() {
|
||||||
if (mContext != null && mDownloadEntity != null) {
|
if (mContext != null && mDownloadEntity != null) {
|
||||||
File dFile = new File(mDownloadEntity.getDownloadPath());
|
File dFile = new File(mDownloadEntity.getDownloadPath());
|
||||||
if (dFile.exists()) {
|
if (dFile.exists()) {
|
||||||
@ -123,12 +147,9 @@ final class DownLoadUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多线程断点续传下载文件,暂停和继续
|
* 多线程断点续传下载文件,开始下载
|
||||||
*
|
|
||||||
* @param downloadListener 下载进度监听 {@link DownloadListener}
|
|
||||||
*/
|
*/
|
||||||
public void start(IDownloadListener downloadListener) {
|
@Override public void startDownload() {
|
||||||
mListener = downloadListener;
|
|
||||||
isDownloading = true;
|
isDownloading = true;
|
||||||
mCurrentLocation = 0;
|
mCurrentLocation = 0;
|
||||||
isStop = false;
|
isStop = false;
|
||||||
@ -160,7 +181,7 @@ final class DownLoadUtil {
|
|||||||
URL url = new URL(downloadUrl);
|
URL url = new URL(downloadUrl);
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
setConnectParam(conn);
|
setConnectParam(conn);
|
||||||
conn.setConnectTimeout(TIME_OUT * 4);
|
conn.setConnectTimeout(mConnectTimeOut * 4);
|
||||||
conn.connect();
|
conn.connect();
|
||||||
int len = conn.getContentLength();
|
int len = conn.getContentLength();
|
||||||
if (len < 0) { //网络被劫持时会出现这个问题
|
if (len < 0) { //网络被劫持时会出现这个问题
|
||||||
@ -273,6 +294,10 @@ final class DownLoadUtil {
|
|||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override public void resumeDownload() {
|
||||||
|
startDownload();
|
||||||
|
}
|
||||||
|
|
||||||
private void failDownload(String msg) {
|
private void failDownload(String msg) {
|
||||||
Log.e(TAG, msg);
|
Log.e(TAG, msg);
|
||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
@ -323,8 +348,8 @@ final class DownLoadUtil {
|
|||||||
conn.setRequestProperty("Range",
|
conn.setRequestProperty("Range",
|
||||||
"bytes=" + dEntity.startLocation + "-" + dEntity.endLocation);
|
"bytes=" + dEntity.startLocation + "-" + dEntity.endLocation);
|
||||||
setConnectParam(conn);
|
setConnectParam(conn);
|
||||||
conn.setConnectTimeout(TIME_OUT * 4);
|
conn.setConnectTimeout(mConnectTimeOut);
|
||||||
conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数
|
conn.setReadTimeout(mReadTimeOut); //设置读取流的等待时间,必须设置该参数
|
||||||
is = conn.getInputStream();
|
is = conn.getInputStream();
|
||||||
//创建可设置位置的文件
|
//创建可设置位置的文件
|
||||||
RandomAccessFile file = new RandomAccessFile(dEntity.tempFile, "rwd");
|
RandomAccessFile file = new RandomAccessFile(dEntity.tempFile, "rwd");
|
||||||
@ -469,7 +494,7 @@ final class DownLoadUtil {
|
|||||||
/**
|
/**
|
||||||
* 子线程下载信息类
|
* 子线程下载信息类
|
||||||
*/
|
*/
|
||||||
private class ConfigEntity {
|
private static class ConfigEntity {
|
||||||
//文件大小
|
//文件大小
|
||||||
long fileSize;
|
long fileSize;
|
||||||
String downloadUrl;
|
String downloadUrl;
|
||||||
@ -479,8 +504,8 @@ final class DownLoadUtil {
|
|||||||
File tempFile;
|
File tempFile;
|
||||||
Context context;
|
Context context;
|
||||||
|
|
||||||
public ConfigEntity(Context context, long fileSize, String downloadUrl, File file, int threadId,
|
private ConfigEntity(Context context, long fileSize, String downloadUrl, File file,
|
||||||
long startLocation, long endLocation) {
|
int threadId, long startLocation, long endLocation) {
|
||||||
this.fileSize = fileSize;
|
this.fileSize = fileSize;
|
||||||
this.downloadUrl = downloadUrl;
|
this.downloadUrl = downloadUrl;
|
||||||
this.tempFile = file;
|
this.tempFile = file;
|
||||||
@ -490,51 +515,4 @@ final class DownLoadUtil {
|
|||||||
this.context = context;
|
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() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ import android.content.Intent;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import com.arialyy.downloadutil.core.inf.IDownloadUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyy on 2016/8/11.
|
* Created by lyy on 2016/8/11.
|
||||||
@ -17,12 +18,17 @@ public class Task {
|
|||||||
private IDownloadListener mListener;
|
private IDownloadListener mListener;
|
||||||
private Handler mOutHandler;
|
private Handler mOutHandler;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private DownLoadUtil mUtil;
|
private IDownloadUtil mUtil;
|
||||||
|
|
||||||
private Task(Context context, DownloadEntity entity) {
|
private Task(Context context, DownloadEntity entity) {
|
||||||
mContext = context.getApplicationContext();
|
mContext = context.getApplicationContext();
|
||||||
mEntity = entity;
|
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, "任务正在下载");
|
Log.d(TAG, "任务正在下载");
|
||||||
} else {
|
} else {
|
||||||
if (mListener == null) {
|
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;
|
return mUtil;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +129,7 @@ public class Task {
|
|||||||
/**
|
/**
|
||||||
* 下载监听类
|
* 下载监听类
|
||||||
*/
|
*/
|
||||||
private class DownloadListener extends DownLoadUtil.DownloadListener {
|
private class DListener extends DownloadListener {
|
||||||
Handler outHandler;
|
Handler outHandler;
|
||||||
Context context;
|
Context context;
|
||||||
Intent sendIntent;
|
Intent sendIntent;
|
||||||
@ -134,7 +140,7 @@ public class Task {
|
|||||||
boolean isFirst = true;
|
boolean isFirst = true;
|
||||||
DownloadEntity downloadEntity;
|
DownloadEntity downloadEntity;
|
||||||
|
|
||||||
DownloadListener(Context context, DownloadEntity downloadEntity, Handler outHandler) {
|
DListener(Context context, DownloadEntity downloadEntity, Handler outHandler) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.outHandler = outHandler;
|
this.outHandler = outHandler;
|
||||||
this.downloadEntity = downloadEntity;
|
this.downloadEntity = downloadEntity;
|
||||||
@ -234,29 +240,47 @@ public class Task {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
DownloadEntity downloadEntity;
|
DownloadEntity downloadEntity;
|
||||||
IDownloadListener listener;
|
Handler outHandler;
|
||||||
Handler outHandler;
|
Context context;
|
||||||
Context context;
|
int threadNum = 3;
|
||||||
|
IDownloadUtil downloadUtil;
|
||||||
|
|
||||||
public Builder(Context context, DownloadEntity downloadEntity) {
|
public Builder(Context context, DownloadEntity downloadEntity) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.downloadEntity = downloadEntity;
|
this.downloadEntity = downloadEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder setDownloadListener(IDownloadListener listener) {
|
/**
|
||||||
this.listener = listener;
|
* 设置自定义Handler处理下载状态时间
|
||||||
return this;
|
*
|
||||||
}
|
* @param outHandler {@link IDownloadTarget.AutoTaskHandler}
|
||||||
|
*/
|
||||||
public Builder setOutHandler(Handler outHandler) {
|
public Builder setOutHandler(Handler outHandler) {
|
||||||
this.outHandler = outHandler;
|
this.outHandler = outHandler;
|
||||||
return this;
|
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() {
|
public Task build() {
|
||||||
Task task = new Task(context, downloadEntity);
|
Task task = new Task(context, downloadEntity);
|
||||||
task.mListener = listener;
|
|
||||||
task.mOutHandler = outHandler;
|
task.mOutHandler = outHandler;
|
||||||
downloadEntity.save();
|
downloadEntity.save();
|
||||||
return task;
|
return task;
|
||||||
|
@ -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();
|
||||||
|
}
|
Reference in New Issue
Block a user