|
|
@ -45,7 +45,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
private long mCurrentLocation = 0; |
|
|
|
private long mCurrentLocation = 0; |
|
|
|
private ExecutorService mInfoPool; |
|
|
|
private ExecutorService mInfoPool; |
|
|
|
private ExecutorService mExePool; |
|
|
|
private ExecutorService mExePool; |
|
|
|
private IDownloadListener mListener; |
|
|
|
private IDownloadGroupListener mListener; |
|
|
|
private DownloadGroupTaskEntity mTaskEntity; |
|
|
|
private DownloadGroupTaskEntity mTaskEntity; |
|
|
|
private boolean isRunning = true; |
|
|
|
private boolean isRunning = true; |
|
|
|
private Timer mTimer; |
|
|
|
private Timer mTimer; |
|
|
@ -87,7 +87,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
//实际的下载任务数
|
|
|
|
//实际的下载任务数
|
|
|
|
private int mActualTaskNum = 0; |
|
|
|
private int mActualTaskNum = 0; |
|
|
|
|
|
|
|
|
|
|
|
public DownloadGroupUtil(IDownloadListener listener, DownloadGroupTaskEntity taskEntity) { |
|
|
|
public DownloadGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { |
|
|
|
mListener = listener; |
|
|
|
mListener = listener; |
|
|
|
mTaskEntity = taskEntity; |
|
|
|
mTaskEntity = taskEntity; |
|
|
|
mInfoPool = Executors.newCachedThreadPool(); |
|
|
|
mInfoPool = Executors.newCachedThreadPool(); |
|
|
@ -102,8 +102,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { |
|
|
|
for (DownloadEntity entity : mTaskEntity.entity.getSubTask()) { |
|
|
|
File file = new File(entity.getDownloadPath()); |
|
|
|
File file = new File(entity.getDownloadPath()); |
|
|
|
if (entity.isComplete() && file.exists()) { |
|
|
|
if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) { |
|
|
|
mTotalSize += entity.getFileSize(); |
|
|
|
|
|
|
|
mCompleteNum++; |
|
|
|
mCompleteNum++; |
|
|
|
mInitNum++; |
|
|
|
mInitNum++; |
|
|
|
mCurrentLocation += entity.getFileSize(); |
|
|
|
mCurrentLocation += entity.getFileSize(); |
|
|
@ -111,6 +110,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity)); |
|
|
|
mExeMap.put(entity.getDownloadUrl(), createChildDownloadTask(entity)); |
|
|
|
mCurrentLocation += entity.getCurrentProgress(); |
|
|
|
mCurrentLocation += entity.getCurrentProgress(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
mTotalSize += entity.getFileSize(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -127,8 +127,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void cancelDownload() { |
|
|
|
@Override public void cancelDownload() { |
|
|
|
isRunning = false; |
|
|
|
closeTimer(false); |
|
|
|
closeTimer(); |
|
|
|
|
|
|
|
mListener.onCancel(); |
|
|
|
mListener.onCancel(); |
|
|
|
if (!mInfoPool.isShutdown()) { |
|
|
|
if (!mInfoPool.isShutdown()) { |
|
|
|
mInfoPool.shutdown(); |
|
|
|
mInfoPool.shutdown(); |
|
|
@ -161,8 +160,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void stopDownload() { |
|
|
|
@Override public void stopDownload() { |
|
|
|
isRunning = false; |
|
|
|
closeTimer(false); |
|
|
|
closeTimer(); |
|
|
|
|
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
if (!mInfoPool.isShutdown()) { |
|
|
|
if (!mInfoPool.isShutdown()) { |
|
|
|
mInfoPool.shutdown(); |
|
|
|
mInfoPool.shutdown(); |
|
|
@ -182,15 +180,24 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
|
|
|
|
|
|
|
|
@Override public void startDownload() { |
|
|
|
@Override public void startDownload() { |
|
|
|
isRunning = true; |
|
|
|
isRunning = true; |
|
|
|
|
|
|
|
mFailNum = 0; |
|
|
|
Set<String> keys = mExeMap.keySet(); |
|
|
|
Set<String> keys = mExeMap.keySet(); |
|
|
|
mListener.onPre(); |
|
|
|
mListener.onPre(); |
|
|
|
|
|
|
|
int i = 0; |
|
|
|
for (String key : keys) { |
|
|
|
for (String key : keys) { |
|
|
|
DownloadTaskEntity taskEntity = mExeMap.get(key); |
|
|
|
DownloadTaskEntity taskEntity = mExeMap.get(key); |
|
|
|
if (taskEntity != null) { |
|
|
|
if (taskEntity != null) { |
|
|
|
|
|
|
|
if (taskEntity.getState() != IEntity.STATE_FAIL |
|
|
|
|
|
|
|
&& taskEntity.getState() != IEntity.STATE_WAIT) { |
|
|
|
|
|
|
|
startChildDownload(taskEntity); |
|
|
|
|
|
|
|
i++; |
|
|
|
|
|
|
|
} else { |
|
|
|
mInfoPool.execute(createFileInfoThread(taskEntity)); |
|
|
|
mInfoPool.execute(createFileInfoThread(taskEntity)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (i == mExeMap.size()) startRunningFlow(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void resumeDownload() { |
|
|
|
@Override public void resumeDownload() { |
|
|
|
startDownload(); |
|
|
|
startDownload(); |
|
|
@ -214,7 +221,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
startChildDownload(te); |
|
|
|
startChildDownload(te); |
|
|
|
} |
|
|
|
} |
|
|
|
mInitNum++; |
|
|
|
mInitNum++; |
|
|
|
if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) { |
|
|
|
if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()) { |
|
|
|
startRunningFlow(); |
|
|
|
startRunningFlow(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -235,7 +242,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
if (mActualTaskNum < 0) mActualTaskNum = 0; |
|
|
|
if (mActualTaskNum < 0) mActualTaskNum = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
failNum++; |
|
|
|
failNum++; |
|
|
|
if (mInitNum + mInitFailNum == mTaskEntity.getEntity().getSubTask().size()) { |
|
|
|
if (mInitNum + mInitFailNum >= mTaskEntity.getEntity().getSubTask().size()) { |
|
|
|
startRunningFlow(); |
|
|
|
startRunningFlow(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -244,7 +251,8 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
return new FileInfoThread(taskEntity, callback); |
|
|
|
return new FileInfoThread(taskEntity, callback); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void closeTimer() { |
|
|
|
private void closeTimer(boolean isRunning) { |
|
|
|
|
|
|
|
this.isRunning = isRunning; |
|
|
|
if (mTimer != null) { |
|
|
|
if (mTimer != null) { |
|
|
|
mTimer.purge(); |
|
|
|
mTimer.purge(); |
|
|
|
mTimer.cancel(); |
|
|
|
mTimer.cancel(); |
|
|
@ -255,13 +263,15 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
* 开始进度流程 |
|
|
|
* 开始进度流程 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void startRunningFlow() { |
|
|
|
private void startRunningFlow() { |
|
|
|
closeTimer(); |
|
|
|
closeTimer(true); |
|
|
|
mListener.onPostPre(mTotalSize); |
|
|
|
mListener.onPostPre(mTotalSize); |
|
|
|
mListener.onStart(mCurrentLocation); |
|
|
|
mListener.onStart(mCurrentLocation); |
|
|
|
mTimer = new Timer(true); |
|
|
|
mTimer = new Timer(true); |
|
|
|
mTimer.schedule(new TimerTask() { |
|
|
|
mTimer.schedule(new TimerTask() { |
|
|
|
@Override public void run() { |
|
|
|
@Override public void run() { |
|
|
|
if (mCurrentLocation >= 0) { |
|
|
|
if (!isRunning) { |
|
|
|
|
|
|
|
closeTimer(false); |
|
|
|
|
|
|
|
} else if (mCurrentLocation >= 0) { |
|
|
|
mListener.onProgress(mCurrentLocation); |
|
|
|
mListener.onProgress(mCurrentLocation); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -304,7 +314,7 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 子任务事件监听 |
|
|
|
* 子任务事件监听 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private class ChildDownloadListener extends DownloadListener { |
|
|
|
private class ChildDownloadListener implements IDownloadListener { |
|
|
|
|
|
|
|
|
|
|
|
DownloadTaskEntity taskEntity; |
|
|
|
DownloadTaskEntity taskEntity; |
|
|
|
DownloadEntity entity; |
|
|
|
DownloadEntity entity; |
|
|
@ -314,6 +324,8 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
ChildDownloadListener(DownloadTaskEntity entity) { |
|
|
|
ChildDownloadListener(DownloadTaskEntity entity) { |
|
|
|
this.taskEntity = entity; |
|
|
|
this.taskEntity = entity; |
|
|
|
this.entity = taskEntity.getEntity(); |
|
|
|
this.entity = taskEntity.getEntity(); |
|
|
|
|
|
|
|
lastLen = this.entity.getCurrentProgress(); |
|
|
|
|
|
|
|
this.entity.setFailNum(0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onPre() { |
|
|
|
@Override public void onPre() { |
|
|
@ -347,26 +359,35 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
@Override public void onStop(long stopLocation) { |
|
|
|
@Override public void onStop(long stopLocation) { |
|
|
|
saveData(IEntity.STATE_STOP, stopLocation); |
|
|
|
saveData(IEntity.STATE_STOP, stopLocation); |
|
|
|
handleSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
|
|
|
|
mListener.onSubStop(entity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onCancel() { |
|
|
|
@Override public void onCancel() { |
|
|
|
saveData(IEntity.STATE_CANCEL, -1); |
|
|
|
saveData(IEntity.STATE_CANCEL, -1); |
|
|
|
handleSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
|
|
|
|
mListener.onSubCancel(entity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onComplete() { |
|
|
|
@Override public void onComplete() { |
|
|
|
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); |
|
|
|
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); |
|
|
|
mCompleteNum++; |
|
|
|
mCompleteNum++; |
|
|
|
|
|
|
|
handleSpeed(0); |
|
|
|
|
|
|
|
mListener.onSubComplete(entity); |
|
|
|
|
|
|
|
//如果子任务完成的数量和总任务数一致,表示任务组任务已经完成
|
|
|
|
|
|
|
|
if (mCompleteNum >= mTaskEntity.getEntity().getSubTask().size()){ |
|
|
|
|
|
|
|
closeTimer(false); |
|
|
|
|
|
|
|
mListener.onComplete(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//如果子任务完成数量加上失败的数量和总任务数一致,则任务组停止下载
|
|
|
|
if (mCompleteNum + mFailNum >= mActualTaskNum) { |
|
|
|
if (mCompleteNum + mFailNum >= mActualTaskNum) { |
|
|
|
closeTimer(); |
|
|
|
closeTimer(false); |
|
|
|
mListener.onComplete(); |
|
|
|
mListener.onComplete(); |
|
|
|
} |
|
|
|
} |
|
|
|
handleSpeed(0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onFail() { |
|
|
|
@Override public void onFail() { |
|
|
|
entity.setFailNum(entity.getFailNum() + 1); |
|
|
|
entity.setFailNum(entity.getFailNum() + 1); |
|
|
|
saveData(IEntity.STATE_FAIL, -1); |
|
|
|
saveData(IEntity.STATE_FAIL, lastLen); |
|
|
|
handleSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
reTry(); |
|
|
|
reTry(); |
|
|
|
} |
|
|
|
} |
|
|
@ -376,13 +397,28 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void reTry() { |
|
|
|
private void reTry() { |
|
|
|
if (entity.getFailNum() < 5) { |
|
|
|
if (entity.getFailNum() < 5) { |
|
|
|
Downloader dt = mDownloaderMap.get(entity.getDownloadUrl()); |
|
|
|
reStartTask(); |
|
|
|
dt.startDownload(); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
mFailNum++; |
|
|
|
mFailNum++; |
|
|
|
|
|
|
|
mListener.onSubFail(entity); |
|
|
|
|
|
|
|
//如果失败的任务数大于实际的下载任务数,任务组停止下载
|
|
|
|
|
|
|
|
if (mFailNum >= mActualTaskNum) { |
|
|
|
|
|
|
|
closeTimer(false); |
|
|
|
|
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void reStartTask() { |
|
|
|
|
|
|
|
Timer timer = new Timer(); |
|
|
|
|
|
|
|
timer.schedule(new TimerTask() { |
|
|
|
|
|
|
|
@Override public void run() { |
|
|
|
|
|
|
|
Downloader dt = mDownloaderMap.get(entity.getDownloadUrl()); |
|
|
|
|
|
|
|
dt.startDownload(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, 3000); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void handleSpeed(long speed) { |
|
|
|
private void handleSpeed(long speed) { |
|
|
|
entity.setSpeed(speed); |
|
|
|
entity.setSpeed(speed); |
|
|
|
entity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s"); |
|
|
|
entity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s"); |
|
|
@ -394,10 +430,14 @@ public class DownloadGroupUtil implements IDownloadUtil { |
|
|
|
if (entity.isComplete()) { |
|
|
|
if (entity.isComplete()) { |
|
|
|
entity.setCompleteTime(System.currentTimeMillis()); |
|
|
|
entity.setCompleteTime(System.currentTimeMillis()); |
|
|
|
entity.setCurrentProgress(entity.getFileSize()); |
|
|
|
entity.setCurrentProgress(entity.getFileSize()); |
|
|
|
} else { |
|
|
|
} else if (location > 0) { |
|
|
|
entity.setCurrentProgress(location); |
|
|
|
entity.setCurrentProgress(location); |
|
|
|
} |
|
|
|
} |
|
|
|
entity.update(); |
|
|
|
entity.update(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override public void supportBreakpoint(boolean support) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|