From 8ac8e22251c422d20457f4dd46b5bc1b27f1d5b8 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 16 Aug 2017 17:39:29 +0800 Subject: [PATCH] fix bug https://github.com/AriaLyy/Aria/issues/94 --- .../java/com/arialyy/aria/core/common/AbsFileer.java | 8 +------- .../com/arialyy/aria/core/common/AbsThreadTask.java | 11 +++++++++-- .../com/arialyy/aria/core/download/BaseDListener.java | 5 ++--- .../aria/core/download/downloader/AbsGroupUtil.java | 10 +++++----- .../aria/core/download/downloader/Downloader.java | 10 ++++++++++ .../core/download/downloader/FtpDirDownloadUtil.java | 2 +- .../core/download/downloader/SimpleDownloadUtil.java | 2 +- .../main/java/com/arialyy/aria/core/inf/AbsTask.java | 5 ++++- .../com/arialyy/aria/core/inf/IEventListener.java | 4 +++- .../arialyy/aria/core/scheduler/AbsSchedulers.java | 5 +++++ .../com/arialyy/aria/core/upload/BaseUListener.java | 3 ++- .../com/arialyy/aria/core/upload/UploadListener.java | 2 +- .../aria/core/upload/uploader/HttpThreadTask.java | 2 +- .../simple/download/group/DownloadGroupActivity.java | 11 ++--------- 14 files changed, 47 insertions(+), 33 deletions(-) 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 db24b141..74f7eb8c 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 @@ -130,7 +130,7 @@ public abstract class AbsFileer config); - protected void failDownload(String errorMsg) { - closeTimer(); - Log.e(TAG, errorMsg); - mConstance.isRunning = false; - mListener.onFail(); - } } 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 316d6cbc..60f72971 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 @@ -168,11 +168,11 @@ public abstract class AbsThreadTask> sendInState2Target(ISchedulers.COMPLETE); } - @Override public void onFail() { + @Override public void onFail(boolean needRetry) { mEntity.setFailNum(mEntity.getFailNum() + 1); saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress()); handleSpeed(0); + mTask.needRetry = needRetry; sendInState2Target(ISchedulers.FAIL); } 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 5c698a12..f91f4fc9 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 @@ -103,7 +103,7 @@ abstract class AbsGroupUtil implements IUtil { mCurrentLocation += entity.getFileSize(); } else { mExeMap.put(entity.getUrl(), createChildDownloadTask(entity)); - mCurrentLocation += entity.getCurrentProgress(); + mCurrentLocation += file.exists() ? entity.getCurrentProgress() : 0; mActualTaskNum++; } mTotalSize += entity.getFileSize(); @@ -339,19 +339,19 @@ abstract class AbsGroupUtil implements IUtil { } } - @Override public void onFail() { + @Override public void onFail(boolean needRetry) { entity.setFailNum(entity.getFailNum() + 1); saveData(IEntity.STATE_FAIL, lastLen); handleSpeed(0); - reTry(); + reTry(needRetry); } /** * 失败后重试下载,如果失败次数超过5次,不再重试 */ - private void reTry() { + private void reTry(boolean needRetry) { synchronized (AriaManager.LOCK) { - if (entity.getFailNum() < 5 && isRunning) { + if (entity.getFailNum() < 5 && isRunning && needRetry) { reStartTask(); } else { mFailNum++; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java index 241b324b..616a86b7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java @@ -15,6 +15,7 @@ */ package com.arialyy.aria.core.download.downloader; +import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.AbsFileer; import com.arialyy.aria.core.common.AbsThreadTask; @@ -34,6 +35,7 @@ import java.io.IOException; * 文件下载器 */ class Downloader extends AbsFileer { + private String TAG = "Downloader"; Downloader(IDownloadListener listener, DownloadTaskEntity taskEntity) { super(listener, taskEntity); @@ -100,4 +102,12 @@ class Downloader extends AbsFileer { } return null; } + + + private void failDownload(String errorMsg) { + closeTimer(); + Log.e(TAG, errorMsg); + mConstance.isRunning = false; + mListener.onFail(false); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java index 9c5c7ce8..f8c97c13 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirDownloadUtil.java @@ -47,7 +47,7 @@ public class FtpDirDownloadUtil extends AbsGroupUtil { } @Override public void onFail(String url, String errorMsg) { - mListener.onFail(); + mListener.onFail(true); } }).start(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java index a124bb27..c800f2b0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/SimpleDownloadUtil.java @@ -82,7 +82,7 @@ public class SimpleDownloadUtil implements IUtil, Runnable { } private void failDownload(String msg) { - mListener.onFail(); + mListener.onFail(true); } @Override public void run() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java index 1bbaecc8..5501f695 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java @@ -23,7 +23,10 @@ import com.arialyy.aria.util.CommonUtil; * Created by AriaL on 2017/6/29. */ public abstract class AbsTask implements ITask { - + /** + * 是否需要重试,默认为true + */ + public boolean needRetry = true; protected ENTITY mEntity; protected Handler mOutHandler; diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IEventListener.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IEventListener.java index edce5282..11da04c7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IEventListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IEventListener.java @@ -58,6 +58,8 @@ public interface IEventListener { /** * 下载失败 + * @param needRetry 是否需要重试{@code true} 需要 */ - void onFail(); + void onFail(boolean needRetry); + } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 8e40eacb..281324c9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -225,6 +225,11 @@ abstract class AbsSchedulers> sendInState2Target(ISchedulers.COMPLETE); } - @Override public void onFail() { + @Override public void onFail(boolean needRetry) { mEntity.setFailNum(mEntity.getFailNum() + 1); saveData(IEntity.STATE_FAIL, mEntity.getCurrentProgress()); handleSpeed(0); + mTask.needRetry = needRetry; sendInState2Target(ISchedulers.FAIL); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java index bf43c7bd..17506e92 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java @@ -49,7 +49,7 @@ class UploadListener implements IUploadListener { } - @Override public void onFail() { + @Override public void onFail(boolean needRetry) { } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java index f32a8d69..cca591fa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java @@ -98,7 +98,7 @@ class HttpThreadTask extends AbsThreadTask { private void fail() { try { - mListener.onFail(); + mListener.onFail(true); STATE.isRunning = false; if (mOutputStream != null) { mOutputStream.close(); 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 e1c6bcfb..cc10cb35 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 @@ -81,15 +81,7 @@ public class DownloadGroupActivity extends BaseActivity " + Aria.download(DownloadGroupActivity.this).getTotleTaskList().size()); - } - }, 1000); + Aria.download(this).load(mUrls).cancel(); break; } } @@ -114,6 +106,7 @@ public class DownloadGroupActivity extends BaseActivity " + task.getPercent()); getBinding().setProgress(task.getPercent()); getBinding().setSpeed(task.getConvertSpeed()); mChildList.updateChildProgress(task.getEntity().getSubTask());