diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index df094f64..6b817f2d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -109,6 +109,28 @@ public abstract class AbsFileer tr.endLocation) { - ALog.w(TAG, String.format("分块【%s】错误,将重新开始该分块", temp.getPath())); - temp.delete(); - tr.startLocation = i * blockLen; - } - mStartThreadNum++; - } - } - } - i++; - } - mTotalThreadNum = mRecord.threadRecords.size(); - mTaskEntity.setNewTask(false); + handleBlockRecord(); } else { - File file = new File(mRecord.filePath); - if (!file.exists()) { - ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath())); - mRecord.deleteData(); - initRecord(true); - return; - } - for (ThreadRecord tr : mRecord.threadRecords) { - if (tr.isComplete) { - mCompleteThreadNum++; - } else { - mStartThreadNum++; + handleNormalRecord(); + } + } + } + } + + /** + * 处理普通任务的记录 + */ + private void handleNormalRecord() { + File file = new File(mRecord.filePath); + if (!file.exists()) { + ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath())); + mRecord.deleteData(); + initRecord(true); + return; + } + + if (mRecord.isOpenDynamicFile) { + ThreadRecord tr = mRecord.threadRecords.get(0); + if (tr == null) { + mTaskEntity.setNewTask(true); + mTotalThreadNum = 1; + return; + } + if (file.length() > mEntity.getFileSize()) { + ALog.i(TAG, String.format("文件【%s】错误,任务重新开始", file.getPath())); + file.delete(); + tr.startLocation = 0; + tr.isComplete = false; + tr.endLocation = mEntity.getFileSize(); + mStartThreadNum++; + } else if (file.length() == mEntity.getFileSize()) { + tr.isComplete = true; + mCompleteThreadNum++; + } else { + if (file.length() != tr.startLocation) { + ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length())); + tr.startLocation = file.length(); + tr.isComplete = false; + mStartThreadNum++; + } + } + } else { + for (ThreadRecord tr : mRecord.threadRecords) { + if (tr.isComplete) { + mCompleteThreadNum++; + } else { + mStartThreadNum++; + } + } + } + mTotalThreadNum = mRecord.threadRecords.size(); + mTaskEntity.setNewTask(false); + } + + /** + * 处理分块任务的记录 + */ + private void handleBlockRecord() { + final int threadNum = mRecord.threadRecords.size(); + final long blockLen = mEntity.getFileSize() / threadNum; + int i = 0; + for (ThreadRecord tr : mRecord.threadRecords) { + File temp = new File(String.format(SUB_PATH, mRecord.filePath, tr.threadId)); + if (!temp.exists()) { + ALog.i(TAG, String.format("分块文件【%s】不存在,该分块将重新开始", temp.getPath())); + tr.isComplete = false; + tr.startLocation = -1; + mStartThreadNum++; + } else { + if (tr.isComplete) { + mCompleteThreadNum++; + } else { + long realLocation = i * blockLen + temp.length(); + ALog.i(TAG, String.format( + "startLocation = %s; endLocation = %s; block = %s; tempLen = %s; i = %s", + tr.startLocation, tr.endLocation, blockLen, temp.length(), i)); + if (tr.endLocation == realLocation) { + ALog.d(TAG, String.format("分块【%s】已完成,更新记录", temp.getPath())); + tr.startLocation = realLocation; + tr.isComplete = true; + mCompleteThreadNum++; + } else { + tr.isComplete = false; + if (realLocation < tr.startLocation) { + ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation)); + tr.startLocation = realLocation; + } else if (realLocation > tr.endLocation) { + ALog.i(TAG, String.format("分块【%s】错误,将重新开始该分块", temp.getPath())); + temp.delete(); + tr.startLocation = i * blockLen; } + mStartThreadNum++; } - mTotalThreadNum = mRecord.threadRecords.size(); - mTaskEntity.setNewTask(false); } } + i++; } + mTotalThreadNum = mRecord.threadRecords.size(); + mTaskEntity.setNewTask(false); } /** @@ -463,7 +529,7 @@ public abstract class AbsFileer config = new SubThreadConfig<>(); - config.FILE_SIZE = fileLength; + config.TOTAL_FILE_SIZE = fileLength; config.URL = mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getUrl(); config.TEMP_FILE = mRecord.isBlock ? new File(String.format(SUB_PATH, mTempFile.getPath(), i)) : mTempFile; @@ -566,34 +632,36 @@ public abstract class AbsFileer keys = mTask.keySet(); - for (Integer key : keys) { - AbsThreadTask task = mTask.get(key); - if (task != null && !task.isThreadComplete()) { - task.getConfig().START_LOCATION = task.getConfig().THREAD_RECORD.startLocation; - mConstance.resetState(); - startTimer(); - ALog.d(TAG, String.format("任务【%s】开始重试,线程__%s__【开始位置:%s,结束位置:%s】", mEntity.getFileName(), - key, task.getConfig().START_LOCATION, task.getConfig().END_LOCATION)); - if (!mFixedThreadPool.isShutdown()) { - mFixedThreadPool.execute(task); - } else { - ALog.w(TAG, "线程池已关闭"); - mListener.onFail(true); - } - } - } + public void retryTask() { + ALog.w(TAG, String.format("任务【%s】开始重试", mEntity.getFileName())); + startFlow(); + //if (isBreak()) { + // return; + //} + //if (mTask == null || mTask.size() == 0) { + // ALog.w(TAG, "没有线程任务"); + // return; + //} + //Set keys = mTask.keySet(); + //for (Integer key : keys) { + // AbsThreadTask task = mTask.get(key); + // if (task != null && !task.isThreadComplete()) { + // + // task.getConfig().START_LOCATION = task.getConfig().THREAD_RECORD.startLocation; + // mConstance.resetState(); + // startTimer(); + // ALog.d(TAG, String.format("任务【%s】开始重试,线程__%s__【开始位置:%s,结束位置:%s】", mEntity.getFileName(), + // key, task.getConfig().START_LOCATION, task.getConfig().END_LOCATION)); + // if (!mFixedThreadPool.isShutdown()) { + // mFixedThreadPool.execute(task); + // } else { + // ALog.w(TAG, "线程池已关闭"); + // mListener.onFail(true); + // } + // } + //} } /** @@ -608,17 +676,17 @@ public abstract class AbsFileer config = new SubThreadConfig<>(); - config.FILE_SIZE = mEntity.getFileSize(); + config.TOTAL_FILE_SIZE = mEntity.getFileSize(); config.URL = mEntity.isRedirect() ? mEntity.getRedirectUrl() : mEntity.getUrl(); config.TEMP_FILE = mTempFile; config.THREAD_ID = 0; config.START_LOCATION = 0; - config.END_LOCATION = config.FILE_SIZE; + config.END_LOCATION = config.TOTAL_FILE_SIZE; config.SUPPORT_BP = mTaskEntity.isSupportBP(); config.TASK_ENTITY = mTaskEntity; ThreadRecord record = new ThreadRecord(); record.startLocation = 0; - record.endLocation = config.FILE_SIZE; + record.endLocation = config.TOTAL_FILE_SIZE; record.key = mTempFile.getPath(); config.THREAD_RECORD = record; AbsThreadTask task = selectThreadTask(config); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java index c776b047..a8f6fe24 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java @@ -104,6 +104,52 @@ public abstract class AbsThreadTask tr.endLocation) { + ALog.i(TAG, String.format("分块【%s】错误,将重新下载该分块", file.getPath())); + boolean b = file.delete(); + ALog.w(TAG, "删除:" + b); + tr.startLocation = block * tr.threadId; + tr.isComplete = false; + mConfig.START_LOCATION = tr.startLocation; + } else if (file.length() == tr.endLocation) { + STATE.COMPLETE_THREAD_NUM++; + tr.isComplete = true; + } else { + tr.startLocation = file.length(); + mConfig.START_LOCATION = file.length(); + tr.isComplete = false; + long size = 0; + for (int i = 0, len = getTaskRecord().threadRecords.size(); i < len; i++) { + File temp = new File(String.format(AbsFileer.SUB_PATH, getTaskRecord().filePath, i)); + if (temp.exists()) { + size += file.length(); + } + } + STATE.CURRENT_LOCATION = size; + ALog.i(TAG, String.format("修正分块【%s】进度,开始位置:%s", file.getPath(), tr.startLocation)); + } + } else { + mConfig.START_LOCATION = mChildCurrentLocation == 0 ? mConfig.START_LOCATION + : mConfig.THREAD_RECORD.startLocation; + } + } + /** * 处理失败状态 * @@ -328,12 +411,17 @@ public abstract class AbsThreadTask { //线程Id public int THREAD_ID; - //下载文件大小 - public long FILE_SIZE; + //文件总长度 + public long TOTAL_FILE_SIZE; //子线程启动下载位置 public long START_LOCATION; //子线程结束下载位置 diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index 686ec364..a151a1d0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -170,7 +170,7 @@ public class DownloadReceiver extends AbsReceiver { } } } else { - ALog.i(TAG, "没有Aria的注解方法"); + ALog.w(TAG, "没有Aria的注解方法"); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index c4745cc1..5abb73ab 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -275,6 +275,7 @@ public abstract class AbsGroupUtil implements IUtil { */ protected void onPre() { mListener.onPre(); + isRunning = true; mGroupSize = mGTEntity.getSubTaskEntities().size(); mTotalLen = mGTEntity.getEntity().getFileSize(); isNeedLoadFileSize = mTotalLen <= 10; @@ -492,20 +493,22 @@ public abstract class AbsGroupUtil implements IUtil { subEntity.setFailNum(subEntity.getFailNum() + 1); saveData(IEntity.STATE_FAIL, lastLen); handleSpeed(0); - reTry(needRetry); + //reTry(needRetry); + reTry(false); } /** * 重试下载,只有全部都下载失败才会执行任务组的整体重试,否则只会执行单个子任务的重试 */ private void reTry(boolean needRetry) { - Downloader dt = mDownloaderMap.get(subEntity.getUrl()); synchronized (AbsGroupUtil.LOCK) { + Downloader dt = mDownloaderMap.get(subEntity.getUrl()); if (!isCancel && !isStop && dt != null && !dt.isBreak() && needRetry && subEntity.getFailNum() < 3 && (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)) { + ALog.d(TAG, "downloader retry"); reStartTask(dt); } else { mFailMap.put(subTaskEntity.getUrl(), subTaskEntity); @@ -531,7 +534,7 @@ public abstract class AbsGroupUtil implements IUtil { timer.schedule(new TimerTask() { @Override public void run() { if (dt != null) { - dt.retryThreadTask(); + dt.retryTask(); } } }, 5000); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index f01a6436..ae0c0fdc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -163,6 +163,10 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { */ private void checkStartFlow() { synchronized (DownloadGroupUtil.class) { + if (isStop) { + closeTimer(); + return; + } if (mInitFailNum == mExeNum) { closeTimer(); mListener.onFail(true); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index 23caeacd..58d56049 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -60,7 +60,7 @@ final class HttpThreadTask extends AbsThreadTask - + diff --git a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java index dc705a90..1e936757 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/group/DownloadGroupActivity.java @@ -46,7 +46,7 @@ public class DownloadGroupActivity extends BaseActivityhttps://res5.d.cn/5a6a3384c1b2be1a52034c72752e8475414630ebc69318b84ef584115ebf5eaaab945ae07b7fe3596afc72a7940ff328d4a9553f6ae92d6c09ba4bfb533137f6.apk--> http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk - http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk + @@ -87,7 +87,7 @@ 战斗吧剑灵.apk - 天魔幻想.apk + diff --git a/build.gradle b/build.gradle index 1056598a..0882d5ad 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.4.6_dev2' + publishVersion = '3.4.6_dev3' // publishVersion = '1.0.3' //FTP插件 repoName='maven' desc = 'android 下载框架'