|
|
@ -169,25 +169,25 @@ 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 = mTaskEntity.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 = |
|
|
|
// DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url);
|
|
|
|
DbEntity.findFirst(DownloadTaskEntity.class, "url=? and isGroupTask='true'", url); |
|
|
|
// if (det != null) {
|
|
|
|
if (det != null) { |
|
|
|
// mTotalLen -= det.getEntity().getFileSize();
|
|
|
|
mTotalLen -= det.getEntity().getFileSize(); |
|
|
|
// mGroupSize--;
|
|
|
|
mGroupSize--; |
|
|
|
// if (mGroupSize == 0) {
|
|
|
|
if (mGroupSize == 0) { |
|
|
|
// closeTimer(false);
|
|
|
|
closeTimer(false); |
|
|
|
// mListener.onCancel();
|
|
|
|
mListener.onCancel(); |
|
|
|
// }
|
|
|
|
} |
|
|
|
// }
|
|
|
|
} |
|
|
|
// mTaskEntity.getEntity().update();
|
|
|
|
mTaskEntity.update(); |
|
|
|
//}
|
|
|
|
} |
|
|
|
//Downloader d = getDownloader(url, false);
|
|
|
|
Downloader d = getDownloader(url, false); |
|
|
|
//if (d != null) {
|
|
|
|
if (d != null) { |
|
|
|
// d.cancel();
|
|
|
|
d.cancel(); |
|
|
|
//}
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -416,19 +416,18 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
* 子任务事件监听 |
|
|
|
* 子任务事件监听 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private class ChildDownloadListener implements IDownloadListener { |
|
|
|
private class ChildDownloadListener implements IDownloadListener { |
|
|
|
|
|
|
|
private DownloadTaskEntity subTaskEntity; |
|
|
|
DownloadTaskEntity taskEntity; |
|
|
|
private DownloadEntity subEntity; |
|
|
|
DownloadEntity entity; |
|
|
|
|
|
|
|
private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
|
|
|
|
private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
|
|
|
|
private long mLastSaveTime; |
|
|
|
private long lastSaveTime; |
|
|
|
long lastLen = 0; |
|
|
|
private long lastLen = 0; |
|
|
|
|
|
|
|
|
|
|
|
ChildDownloadListener(DownloadTaskEntity entity) { |
|
|
|
ChildDownloadListener(DownloadTaskEntity entity) { |
|
|
|
this.taskEntity = entity; |
|
|
|
subTaskEntity = entity; |
|
|
|
this.entity = taskEntity.getEntity(); |
|
|
|
subEntity = subTaskEntity.getEntity(); |
|
|
|
this.entity.setFailNum(0); |
|
|
|
subEntity.setFailNum(0); |
|
|
|
lastLen = this.entity.getCurrentProgress(); |
|
|
|
lastLen = subEntity.getCurrentProgress(); |
|
|
|
mLastSaveTime = System.currentTimeMillis(); |
|
|
|
lastSaveTime = System.currentTimeMillis(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onPre() { |
|
|
|
@Override public void onPre() { |
|
|
@ -436,33 +435,33 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onPostPre(long fileSize) { |
|
|
|
@Override public void onPostPre(long fileSize) { |
|
|
|
entity.setFileSize(fileSize); |
|
|
|
subEntity.setFileSize(fileSize); |
|
|
|
entity.setConvertFileSize(CommonUtil.formatFileSize(fileSize)); |
|
|
|
subEntity.setConvertFileSize(CommonUtil.formatFileSize(fileSize)); |
|
|
|
saveData(IEntity.STATE_POST_PRE, -1); |
|
|
|
saveData(IEntity.STATE_POST_PRE, -1); |
|
|
|
mListener.onSubPre(entity); |
|
|
|
mListener.onSubPre(subEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onResume(long resumeLocation) { |
|
|
|
@Override public void onResume(long resumeLocation) { |
|
|
|
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); |
|
|
|
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); |
|
|
|
lastLen = resumeLocation; |
|
|
|
lastLen = resumeLocation; |
|
|
|
mListener.onSubStart(entity); |
|
|
|
mListener.onSubStart(subEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onStart(long startLocation) { |
|
|
|
@Override public void onStart(long startLocation) { |
|
|
|
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); |
|
|
|
saveData(IEntity.STATE_POST_PRE, IEntity.STATE_RUNNING); |
|
|
|
lastLen = startLocation; |
|
|
|
lastLen = startLocation; |
|
|
|
mListener.onSubStart(entity); |
|
|
|
mListener.onSubStart(subEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onProgress(long currentLocation) { |
|
|
|
@Override public void onProgress(long currentLocation) { |
|
|
|
long speed = currentLocation - lastLen; |
|
|
|
long speed = currentLocation - lastLen; |
|
|
|
mCurrentLocation += speed; |
|
|
|
mCurrentLocation += speed; |
|
|
|
entity.setCurrentProgress(currentLocation); |
|
|
|
subEntity.setCurrentProgress(currentLocation); |
|
|
|
handleSpeed(speed); |
|
|
|
handleSpeed(speed); |
|
|
|
mListener.onSubRunning(entity); |
|
|
|
mListener.onSubRunning(subEntity); |
|
|
|
if (System.currentTimeMillis() - mLastSaveTime >= RUN_SAVE_INTERVAL) { |
|
|
|
if (System.currentTimeMillis() - lastSaveTime >= RUN_SAVE_INTERVAL) { |
|
|
|
saveData(IEntity.STATE_RUNNING, currentLocation); |
|
|
|
saveData(IEntity.STATE_RUNNING, currentLocation); |
|
|
|
mLastSaveTime = System.currentTimeMillis(); |
|
|
|
lastSaveTime = System.currentTimeMillis(); |
|
|
|
} |
|
|
|
} |
|
|
|
lastLen = currentLocation; |
|
|
|
lastLen = currentLocation; |
|
|
|
} |
|
|
|
} |
|
|
@ -470,7 +469,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
@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); |
|
|
|
mListener.onSubStop(subEntity); |
|
|
|
synchronized (AbsGroupUtil.class) { |
|
|
|
synchronized (AbsGroupUtil.class) { |
|
|
|
mStopNum++; |
|
|
|
mStopNum++; |
|
|
|
if (mStopNum + mCompleteNum + mInitFailNum + mFailNum >= mGroupSize) { |
|
|
|
if (mStopNum + mCompleteNum + mInitFailNum + mFailNum >= mGroupSize) { |
|
|
@ -483,13 +482,13 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
@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); |
|
|
|
mListener.onSubCancel(subEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onComplete() { |
|
|
|
@Override public void onComplete() { |
|
|
|
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); |
|
|
|
saveData(IEntity.STATE_COMPLETE, subEntity.getFileSize()); |
|
|
|
handleSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
mListener.onSubComplete(entity); |
|
|
|
mListener.onSubComplete(subEntity); |
|
|
|
synchronized (AbsGroupUtil.class) { |
|
|
|
synchronized (AbsGroupUtil.class) { |
|
|
|
mCompleteNum++; |
|
|
|
mCompleteNum++; |
|
|
|
//如果子任务完成的数量和总任务数一致,表示任务组任务已经完成
|
|
|
|
//如果子任务完成的数量和总任务数一致,表示任务组任务已经完成
|
|
|
@ -505,7 +504,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onFail(boolean needRetry) { |
|
|
|
@Override public void onFail(boolean needRetry) { |
|
|
|
entity.setFailNum(entity.getFailNum() + 1); |
|
|
|
subEntity.setFailNum(subEntity.getFailNum() + 1); |
|
|
|
saveData(IEntity.STATE_FAIL, lastLen); |
|
|
|
saveData(IEntity.STATE_FAIL, lastLen); |
|
|
|
handleSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
reTry(needRetry); |
|
|
|
reTry(needRetry); |
|
|
@ -516,12 +515,12 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void reTry(boolean needRetry) { |
|
|
|
private void reTry(boolean needRetry) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
if (entity.getFailNum() < 5 && isRunning && needRetry && NetUtils.isConnected( |
|
|
|
if (subEntity.getFailNum() < 5 && isRunning && needRetry && NetUtils.isConnected( |
|
|
|
AriaManager.APP)) { |
|
|
|
AriaManager.APP)) { |
|
|
|
reStartTask(); |
|
|
|
reStartTask(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
mFailNum++; |
|
|
|
mFailNum++; |
|
|
|
mListener.onSubFail(entity); |
|
|
|
mListener.onSubFail(subEntity); |
|
|
|
//如果失败的任务数大于实际的下载任务数,任务组停止下载
|
|
|
|
//如果失败的任务数大于实际的下载任务数,任务组停止下载
|
|
|
|
if (mFailNum >= mActualTaskNum) { |
|
|
|
if (mFailNum >= mActualTaskNum) { |
|
|
|
closeTimer(false); |
|
|
|
closeTimer(false); |
|
|
@ -535,28 +534,32 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
Timer timer = new Timer(); |
|
|
|
Timer timer = new Timer(); |
|
|
|
timer.schedule(new TimerTask() { |
|
|
|
timer.schedule(new TimerTask() { |
|
|
|
@Override public void run() { |
|
|
|
@Override public void run() { |
|
|
|
Downloader dt = mDownloaderMap.get(entity.getUrl()); |
|
|
|
Downloader dt = mDownloaderMap.get(subEntity.getUrl()); |
|
|
|
dt.start(); |
|
|
|
dt.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
}, 3000); |
|
|
|
}, 3000); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void handleSpeed(long speed) { |
|
|
|
private void handleSpeed(long speed) { |
|
|
|
entity.setSpeed(speed); |
|
|
|
subEntity.setSpeed(speed); |
|
|
|
entity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s"); |
|
|
|
subEntity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s"); |
|
|
|
entity.setPercent((int) (entity.getCurrentProgress() * 100 / entity.getFileSize())); |
|
|
|
subEntity.setPercent((int) (subEntity.getCurrentProgress() * 100 / subEntity.getFileSize())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void saveData(int state, long location) { |
|
|
|
private void saveData(int state, long location) { |
|
|
|
entity.setState(state); |
|
|
|
subTaskEntity.state = state; |
|
|
|
entity.setComplete(state == IEntity.STATE_COMPLETE); |
|
|
|
subEntity.setState(state); |
|
|
|
if (entity.isComplete()) { |
|
|
|
subEntity.setComplete(state == IEntity.STATE_COMPLETE); |
|
|
|
entity.setCompleteTime(System.currentTimeMillis()); |
|
|
|
if (state == IEntity.STATE_CANCEL) { |
|
|
|
entity.setCurrentProgress(entity.getFileSize()); |
|
|
|
subTaskEntity.deleteData(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} else if (subEntity.isComplete()) { |
|
|
|
|
|
|
|
subEntity.setCompleteTime(System.currentTimeMillis()); |
|
|
|
|
|
|
|
subEntity.setCurrentProgress(subEntity.getFileSize()); |
|
|
|
} else if (location > 0) { |
|
|
|
} else if (location > 0) { |
|
|
|
entity.setCurrentProgress(location); |
|
|
|
subEntity.setCurrentProgress(location); |
|
|
|
} |
|
|
|
} |
|
|
|
entity.update(); |
|
|
|
subTaskEntity.update(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void supportBreakpoint(boolean support) { |
|
|
|
@Override public void supportBreakpoint(boolean support) { |
|
|
|