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..e6d4ba6c 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 @@ -28,6 +28,8 @@ import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.util.Properties; +import java.util.Timer; +import java.util.TimerTask; /** * Created by lyy on 2017/1/18. @@ -35,6 +37,14 @@ import java.util.Properties; */ public abstract class AbsThreadTask> implements Runnable { + /** + * 线程重试次数 + */ + private final int RETRY_NUM = 5; + /** + * 线程重试间隔 + */ + private final int RETRY_INTERVAL = 5000; private final String TAG = "AbsThreadTask"; protected long mChildCurrentLocation = 0, mSleepTime = 0; protected int mBufSize; @@ -48,6 +58,8 @@ public abstract class AbsThreadTask info) { @@ -168,11 +180,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/README.md b/README.md index 399d90b4..9ca1f4b0 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,8 @@ Aria有以下特点: [![Download](https://api.bintray.com/packages/arialyy/maven/AriaApi/images/download.svg)](https://bintray.com/arialyy/maven/AriaApi/_latestVersion) [![Download](https://api.bintray.com/packages/arialyy/maven/AriaCompiler/images/download.svg)](https://bintray.com/arialyy/maven/AriaCompiler/_latestVersion) ```java -compile 'com.arialyy.aria:aria-core:3.2.19' -annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.19' +compile 'com.arialyy.aria:aria-core:3.2.20' +annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.20' ``` ## 示例 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()); diff --git a/build.gradle b/build.gradle index 2b6a3686..70aa0750 100644 --- a/build.gradle +++ b/build.gradle @@ -37,7 +37,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.2.19' + publishVersion = '3.2.20' repoName='maven' desc = 'android 下载框架' website = 'https://github.com/AriaLyy/Aria'