|
|
@ -43,23 +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"; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 任务组所有任务总大小 |
|
|
|
* 任务组所有任务总长度 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
long mTotalSize = 0; |
|
|
|
long mTotalLen = 0; |
|
|
|
long mCurrentLocation = 0; |
|
|
|
long mCurrentLocation = 0; |
|
|
|
private ExecutorService mExePool; |
|
|
|
private ExecutorService mExePool; |
|
|
|
protected IDownloadGroupListener mListener; |
|
|
|
protected IDownloadGroupListener mListener; |
|
|
|
protected DownloadGroupTaskEntity mTaskEntity; |
|
|
|
protected DownloadGroupTaskEntity mTaskEntity; |
|
|
|
private boolean isRunning = false; |
|
|
|
private boolean isRunning = false; |
|
|
|
private Timer mTimer; |
|
|
|
private Timer mTimer; |
|
|
|
/** |
|
|
|
|
|
|
|
* 初始化完成的任务书数 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
int mInitNum = 0; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 初始化失败的任务数 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
int mInitFailNum = 0; |
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 保存所有没有下载完成的任务,key为下载地址 |
|
|
|
* 保存所有没有下载完成的任务,key为下载地址 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -79,18 +71,26 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
* 该任务组对应的所有任务 |
|
|
|
* 该任务组对应的所有任务 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private Map<String, DownloadTaskEntity> mTasksMap = new HashMap<>(); |
|
|
|
private Map<String, DownloadTaskEntity> mTasksMap = new HashMap<>(); |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 是否需要读取文件长度,{@code true}需要 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
boolean isNeedLoadFileSize = true; |
|
|
|
//已经完成的任务数
|
|
|
|
//已经完成的任务数
|
|
|
|
private int mCompleteNum = 0; |
|
|
|
private int mCompleteNum = 0; |
|
|
|
//失败的任务数
|
|
|
|
//失败的任务数
|
|
|
|
private int mFailNum = 0; |
|
|
|
private int mFailNum = 0; |
|
|
|
//停止的任务数
|
|
|
|
//停止的任务数
|
|
|
|
private int mStopNum = 0; |
|
|
|
private int mStopNum = 0; |
|
|
|
|
|
|
|
|
|
|
|
//实际的下载任务数
|
|
|
|
//实际的下载任务数
|
|
|
|
int mActualTaskNum = 0; |
|
|
|
int mActualTaskNum = 0; |
|
|
|
/** |
|
|
|
//初始化完成的任务数
|
|
|
|
* 是否需要读取文件长度,{@code true}需要 |
|
|
|
int mInitNum = 0; |
|
|
|
*/ |
|
|
|
// 初始化失败的任务数
|
|
|
|
boolean isNeedLoadFileSize = true; |
|
|
|
int mInitFailNum = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//任务组大小
|
|
|
|
|
|
|
|
int mGroupSize = 0; |
|
|
|
|
|
|
|
|
|
|
|
AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { |
|
|
|
AbsGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { |
|
|
|
mListener = listener; |
|
|
|
mListener = listener; |
|
|
@ -105,14 +105,13 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
mTasksMap.put(te.getEntity().getUrl(), te); |
|
|
|
mTasksMap.put(te.getEntity().getUrl(), te); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
mTotalSize = taskEntity.getEntity().getFileSize(); |
|
|
|
mGroupSize = mTaskEntity.entity.getSubTask().size(); |
|
|
|
isNeedLoadFileSize = mTotalSize <= 1; |
|
|
|
mTotalLen = taskEntity.getEntity().getFileSize(); |
|
|
|
|
|
|
|
isNeedLoadFileSize = mTotalLen <= 1; |
|
|
|
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.getState() == IEntity.STATE_COMPLETE && file.exists()) { |
|
|
|
if (entity.getState() == IEntity.STATE_COMPLETE && file.exists()) { |
|
|
|
mCompleteNum++; |
|
|
|
mCompleteNum++; |
|
|
|
mInitNum++; |
|
|
|
|
|
|
|
mStopNum++; |
|
|
|
|
|
|
|
mCurrentLocation += entity.getFileSize(); |
|
|
|
mCurrentLocation += entity.getFileSize(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
mExeMap.put(entity.getUrl(), createChildDownloadTask(entity)); |
|
|
|
mExeMap.put(entity.getUrl(), createChildDownloadTask(entity)); |
|
|
@ -120,7 +119,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
mActualTaskNum++; |
|
|
|
mActualTaskNum++; |
|
|
|
} |
|
|
|
} |
|
|
|
if (isNeedLoadFileSize) { |
|
|
|
if (isNeedLoadFileSize) { |
|
|
|
mTotalSize += entity.getFileSize(); |
|
|
|
mTotalLen += entity.getFileSize(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
updateFileSize(); |
|
|
|
updateFileSize(); |
|
|
@ -128,7 +127,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
|
|
|
|
|
|
|
|
void updateFileSize() { |
|
|
|
void updateFileSize() { |
|
|
|
if (isNeedLoadFileSize) { |
|
|
|
if (isNeedLoadFileSize) { |
|
|
|
mTaskEntity.getEntity().setFileSize(mTotalSize); |
|
|
|
mTaskEntity.getEntity().setFileSize(mTotalLen); |
|
|
|
mTaskEntity.getEntity().update(); |
|
|
|
mTaskEntity.getEntity().update(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -212,7 +211,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public long getFileSize() { |
|
|
|
@Override public long getFileSize() { |
|
|
|
return mTotalSize; |
|
|
|
return mTotalLen; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public long getCurrentLocation() { |
|
|
|
@Override public long getCurrentLocation() { |
|
|
@ -272,7 +271,6 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
|
|
|
|
|
|
|
|
@Override public void stop() { |
|
|
|
@Override public void stop() { |
|
|
|
closeTimer(false); |
|
|
|
closeTimer(false); |
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
|
|
|
|
onStop(); |
|
|
|
onStop(); |
|
|
|
if (!mExePool.isShutdown()) { |
|
|
|
if (!mExePool.isShutdown()) { |
|
|
|
mExePool.shutdown(); |
|
|
|
mExePool.shutdown(); |
|
|
@ -325,7 +323,7 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void startRunningFlow() { |
|
|
|
void startRunningFlow() { |
|
|
|
closeTimer(true); |
|
|
|
closeTimer(true); |
|
|
|
mListener.onPostPre(mTotalSize); |
|
|
|
mListener.onPostPre(mTotalLen); |
|
|
|
mListener.onStart(mCurrentLocation); |
|
|
|
mListener.onStart(mCurrentLocation); |
|
|
|
startTimer(); |
|
|
|
startTimer(); |
|
|
|
} |
|
|
|
} |
|
|
@ -413,8 +411,8 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
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); |
|
|
|
this.entity.setFailNum(0); |
|
|
|
|
|
|
|
lastLen = this.entity.getCurrentProgress(); |
|
|
|
mLastSaveTime = System.currentTimeMillis(); |
|
|
|
mLastSaveTime = System.currentTimeMillis(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -458,12 +456,14 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
saveData(IEntity.STATE_STOP, stopLocation); |
|
|
|
saveData(IEntity.STATE_STOP, stopLocation); |
|
|
|
handleSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
mListener.onSubStop(entity); |
|
|
|
mListener.onSubStop(entity); |
|
|
|
|
|
|
|
synchronized (AbsGroupUtil.class) { |
|
|
|
mStopNum++; |
|
|
|
mStopNum++; |
|
|
|
if (mStopNum + mCompleteNum >= mInitNum) { |
|
|
|
if (mStopNum + mCompleteNum + mInitFailNum + mFailNum >= mGroupSize) { |
|
|
|
closeTimer(false); |
|
|
|
closeTimer(false); |
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onCancel() { |
|
|
|
@Override public void onCancel() { |
|
|
|
saveData(IEntity.STATE_CANCEL, -1); |
|
|
|
saveData(IEntity.STATE_CANCEL, -1); |
|
|
@ -473,16 +473,19 @@ public abstract class AbsGroupUtil implements IUtil { |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onComplete() { |
|
|
|
@Override public void onComplete() { |
|
|
|
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); |
|
|
|
saveData(IEntity.STATE_COMPLETE, entity.getFileSize()); |
|
|
|
mCompleteNum++; |
|
|
|
|
|
|
|
handleSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
mListener.onSubComplete(entity); |
|
|
|
mListener.onSubComplete(entity); |
|
|
|
|
|
|
|
synchronized (AbsGroupUtil.class) { |
|
|
|
|
|
|
|
mCompleteNum++; |
|
|
|
//如果子任务完成的数量和总任务数一致,表示任务组任务已经完成
|
|
|
|
//如果子任务完成的数量和总任务数一致,表示任务组任务已经完成
|
|
|
|
if (mCompleteNum >= mTaskEntity.getEntity().getSubTask().size()) { |
|
|
|
if (mCompleteNum >= mGroupSize) { |
|
|
|
closeTimer(false); |
|
|
|
closeTimer(false); |
|
|
|
mListener.onComplete(); |
|
|
|
mListener.onComplete(); |
|
|
|
} else if (mCompleteNum + mFailNum >= mActualTaskNum) { |
|
|
|
} else if (mStopNum + mCompleteNum + mInitFailNum + mFailNum >= mGroupSize) { |
|
|
|
//如果子任务完成数量加上失败的数量和总任务数一致,则任务组停止下载
|
|
|
|
//如果子任务完成数量加上失败的数量和总任务数一致,则任务组停止下载
|
|
|
|
closeTimer(false); |
|
|
|
closeTimer(false); |
|
|
|
|
|
|
|
mListener.onStop(mCurrentLocation); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|