|
|
@ -43,6 +43,15 @@ import java.util.concurrent.Executors; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public abstract class AbsGroupUtil implements IUtil { |
|
|
|
public abstract class AbsGroupUtil implements IUtil { |
|
|
|
private final String TAG = "AbsGroupUtil"; |
|
|
|
private final String TAG = "AbsGroupUtil"; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* FTP文件夹 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected int FTP_DIR = 0xa1; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* HTTP 任务组 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected int HTTP_GROUP = 0xa2; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 任务组所有任务总长度 |
|
|
|
* 任务组所有任务总长度 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -50,7 +59,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
long mCurrentLocation = 0; |
|
|
|
long mCurrentLocation = 0; |
|
|
|
private ExecutorService mExePool; |
|
|
|
private ExecutorService mExePool; |
|
|
|
protected IDownloadGroupListener mListener; |
|
|
|
protected IDownloadGroupListener mListener; |
|
|
|
protected DownloadGroupTaskEntity mTaskEntity; |
|
|
|
protected DownloadGroupTaskEntity mGTEntity; |
|
|
|
private boolean isRunning = false; |
|
|
|
private boolean isRunning = false; |
|
|
|
private Timer mTimer; |
|
|
|
private Timer mTimer; |
|
|
|
/** |
|
|
|
/** |
|
|
@ -82,34 +91,32 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
private int mFailNum = 0; |
|
|
|
private int mFailNum = 0; |
|
|
|
//停止的任务数
|
|
|
|
//停止的任务数
|
|
|
|
private int mStopNum = 0; |
|
|
|
private int mStopNum = 0; |
|
|
|
|
|
|
|
|
|
|
|
//实际的下载任务数
|
|
|
|
//实际的下载任务数
|
|
|
|
int mActualTaskNum = 0; |
|
|
|
int mActualTaskNum = 0; |
|
|
|
//初始化完成的任务数
|
|
|
|
//初始化完成的任务数
|
|
|
|
int mInitNum = 0; |
|
|
|
int mInitNum = 0; |
|
|
|
// 初始化失败的任务数
|
|
|
|
// 初始化失败的任务数
|
|
|
|
int mInitFailNum = 0; |
|
|
|
int mInitFailNum = 0; |
|
|
|
|
|
|
|
|
|
|
|
//任务组大小
|
|
|
|
//任务组大小
|
|
|
|
int mGroupSize = 0; |
|
|
|
int mGroupSize = 0; |
|
|
|
|
|
|
|
|
|
|
|
AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { |
|
|
|
AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity groupEntity) { |
|
|
|
mListener = listener; |
|
|
|
mListener = listener; |
|
|
|
mTaskEntity = taskEntity; |
|
|
|
mGTEntity = groupEntity; |
|
|
|
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
|
|
|
mExePool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
|
|
|
List<DownloadTaskEntity> tasks = |
|
|
|
List<DownloadTaskEntity> tasks = |
|
|
|
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key); |
|
|
|
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mGTEntity.key); |
|
|
|
if (tasks != null && !tasks.isEmpty()) { |
|
|
|
if (tasks != null && !tasks.isEmpty()) { |
|
|
|
for (DownloadTaskEntity te : tasks) { |
|
|
|
for (DownloadTaskEntity te : tasks) { |
|
|
|
te.removeFile = mTaskEntity.removeFile; |
|
|
|
te.removeFile = mGTEntity.removeFile; |
|
|
|
if (te.getEntity() == null) continue; |
|
|
|
if (te.getEntity() == null) continue; |
|
|
|
mTasksMap.put(te.getEntity().getUrl(), te); |
|
|
|
mTasksMap.put(te.getEntity().getUrl(), te); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
mGroupSize = mTaskEntity.entity.getSubTask().size(); |
|
|
|
mGroupSize = mGTEntity.entity.getSubTask().size(); |
|
|
|
mTotalLen = taskEntity.getEntity().getFileSize(); |
|
|
|
mTotalLen = groupEntity.getEntity().getFileSize(); |
|
|
|
isNeedLoadFileSize = mTotalLen <= 1; |
|
|
|
isNeedLoadFileSize = mTotalLen <= 1; |
|
|
|
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { |
|
|
|
for (DownloadEntity entity : mGTEntity.entity.getSubTask()) { |
|
|
|
File file = new File(entity.getDownloadPath()); |
|
|
|
File file = new File(entity.getDownloadPath()); |
|
|
|
if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) { |
|
|
|
if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) { |
|
|
|
mCompleteNum++; |
|
|
|
mCompleteNum++; |
|
|
@ -126,10 +133,20 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
updateFileSize(); |
|
|
|
updateFileSize(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 获取任务类型 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return {@link #FTP_DIR}、{@link #HTTP_GROUP} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
abstract int getTaskType(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 更新任务组文件大小 |
|
|
|
|
|
|
|
*/ |
|
|
|
void updateFileSize() { |
|
|
|
void updateFileSize() { |
|
|
|
if (isNeedLoadFileSize) { |
|
|
|
if (isNeedLoadFileSize) { |
|
|
|
mTaskEntity.getEntity().setFileSize(mTotalLen); |
|
|
|
mGTEntity.getEntity().setFileSize(mTotalLen); |
|
|
|
mTaskEntity.getEntity().update(); |
|
|
|
mGTEntity.getEntity().update(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -170,7 +187,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
* @param url 子任务下载地址 |
|
|
|
* @param url 子任务下载地址 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void cancelSubTask(String url) { |
|
|
|
public void cancelSubTask(String url) { |
|
|
|
List<String> urls = mTaskEntity.entity.getUrls(); |
|
|
|
List<String> urls = mGTEntity.entity.getUrls(); |
|
|
|
if (urls != null && !urls.isEmpty() && urls.contains(url)) { |
|
|
|
if (urls != null && !urls.isEmpty() && urls.contains(url)) { |
|
|
|
urls.remove(url); |
|
|
|
urls.remove(url); |
|
|
|
DownloadTaskEntity det = |
|
|
|
DownloadTaskEntity det = |
|
|
@ -183,7 +200,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
mListener.onCancel(); |
|
|
|
mListener.onCancel(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
mTaskEntity.update(); |
|
|
|
mGTEntity.update(); |
|
|
|
} |
|
|
|
} |
|
|
|
Downloader d = getDownloader(url, false); |
|
|
|
Downloader d = getDownloader(url, false); |
|
|
|
if (d != null) { |
|
|
|
if (d != null) { |
|
|
@ -254,7 +271,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
delDownloadInfo(); |
|
|
|
delDownloadInfo(); |
|
|
|
mTaskEntity.deleteData(); |
|
|
|
mGTEntity.deleteData(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void onCancel() { |
|
|
|
public void onCancel() { |
|
|
@ -266,20 +283,20 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void delDownloadInfo() { |
|
|
|
private void delDownloadInfo() { |
|
|
|
List<DownloadTaskEntity> tasks = |
|
|
|
List<DownloadTaskEntity> tasks = |
|
|
|
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mTaskEntity.key); |
|
|
|
DbEntity.findDatas(DownloadTaskEntity.class, "groupName=?", mGTEntity.key); |
|
|
|
if (tasks != null && !tasks.isEmpty()) { |
|
|
|
if (tasks != null && !tasks.isEmpty()) { |
|
|
|
for (DownloadTaskEntity taskEntity : tasks) { |
|
|
|
for (DownloadTaskEntity taskEntity : tasks) { |
|
|
|
CommonUtil.delDownloadTaskConfig(mTaskEntity.removeFile, taskEntity); |
|
|
|
CommonUtil.delDownloadTaskConfig(mGTEntity.removeFile, taskEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
File dir = new File(mTaskEntity.getEntity().getDirPath()); |
|
|
|
File dir = new File(mGTEntity.getEntity().getDirPath()); |
|
|
|
if (mTaskEntity.removeFile) { |
|
|
|
if (mGTEntity.removeFile) { |
|
|
|
if (dir.exists()) { |
|
|
|
if (dir.exists()) { |
|
|
|
dir.delete(); |
|
|
|
dir.delete(); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (!mTaskEntity.getEntity().isComplete()) { |
|
|
|
if (!mGTEntity.getEntity().isComplete()) { |
|
|
|
dir.delete(); |
|
|
|
dir.delete(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -387,30 +404,34 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
DownloadTaskEntity taskEntity = mTasksMap.get(entity.getUrl()); |
|
|
|
DownloadTaskEntity taskEntity = mTasksMap.get(entity.getUrl()); |
|
|
|
if (taskEntity != null) { |
|
|
|
if (taskEntity != null) { |
|
|
|
taskEntity.entity = entity; |
|
|
|
taskEntity.entity = entity; |
|
|
|
//ftp登录的
|
|
|
|
if (getTaskType() == FTP_DIR) { |
|
|
|
taskEntity.urlEntity = createFtpUrlEntity(entity); |
|
|
|
taskEntity.urlEntity = createFtpUrlEntity(entity); |
|
|
|
|
|
|
|
} |
|
|
|
mTasksMap.put(entity.getUrl(), taskEntity); |
|
|
|
mTasksMap.put(entity.getUrl(), taskEntity); |
|
|
|
return taskEntity; |
|
|
|
return taskEntity; |
|
|
|
} |
|
|
|
} |
|
|
|
taskEntity = new DownloadTaskEntity(); |
|
|
|
taskEntity = new DownloadTaskEntity(); |
|
|
|
taskEntity.entity = entity; |
|
|
|
taskEntity.entity = entity; |
|
|
|
taskEntity.headers = mTaskEntity.headers; |
|
|
|
taskEntity.headers = mGTEntity.headers; |
|
|
|
taskEntity.requestEnum = mTaskEntity.requestEnum; |
|
|
|
taskEntity.requestEnum = mGTEntity.requestEnum; |
|
|
|
taskEntity.redirectUrlKey = mTaskEntity.redirectUrlKey; |
|
|
|
taskEntity.redirectUrlKey = mGTEntity.redirectUrlKey; |
|
|
|
taskEntity.removeFile = mTaskEntity.removeFile; |
|
|
|
taskEntity.removeFile = mGTEntity.removeFile; |
|
|
|
taskEntity.groupName = mTaskEntity.key; |
|
|
|
taskEntity.groupName = mGTEntity.key; |
|
|
|
taskEntity.isGroupTask = true; |
|
|
|
taskEntity.isGroupTask = true; |
|
|
|
taskEntity.requestType = mTaskEntity.requestType; |
|
|
|
taskEntity.requestType = mGTEntity.requestType; |
|
|
|
//ftp登录的
|
|
|
|
taskEntity.key = entity.getDownloadPath(); |
|
|
|
|
|
|
|
if (getTaskType() == FTP_DIR) { |
|
|
|
taskEntity.urlEntity = createFtpUrlEntity(entity); |
|
|
|
taskEntity.urlEntity = createFtpUrlEntity(entity); |
|
|
|
|
|
|
|
} |
|
|
|
taskEntity.save(); |
|
|
|
taskEntity.save(); |
|
|
|
mTasksMap.put(entity.getUrl(), taskEntity); |
|
|
|
mTasksMap.put(entity.getUrl(), taskEntity); |
|
|
|
return taskEntity; |
|
|
|
return taskEntity; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private FtpUrlEntity createFtpUrlEntity(DownloadEntity entity) { |
|
|
|
private FtpUrlEntity createFtpUrlEntity(DownloadEntity entity) { |
|
|
|
FtpUrlEntity urlEntity = CommonUtil.getFtpUrlInfo(entity.getUrl()); |
|
|
|
FtpUrlEntity urlEntity = mGTEntity.urlEntity.clone(); |
|
|
|
urlEntity.validAddr = mTaskEntity.urlEntity.validAddr; |
|
|
|
urlEntity.url = entity.getUrl(); |
|
|
|
|
|
|
|
urlEntity.remotePath = CommonUtil.getRemotePath(entity.getUrl()); |
|
|
|
return urlEntity; |
|
|
|
return urlEntity; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|