From a7e238e1d1bd3674d5178e8ff9e77b4af1881081 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 22 Jun 2018 20:55:33 +0800 Subject: [PATCH] fix bug https://github.com/AriaLyy/Aria/issues/262 https://github.com/AriaLyy/Aria/issues/261 --- .../arialyy/aria/core/common/AbsFileer.java | 31 ++++++++- .../aria/core/common/AbsThreadTask.java | 13 +--- .../core/delegate/HttpHeaderDelegate.java | 65 +++++++++++-------- .../core/download/DownloadGroupTarget.java | 32 ++++++++- .../aria/core/download/DownloadGroupTask.java | 2 + .../aria/core/download/DownloadTask.java | 2 + .../download/downloader/AbsGroupUtil.java | 11 ++-- .../com/arialyy/aria/core/inf/AbsTask.java | 27 ++++++++ .../arialyy/aria/core/queue/AbsTaskQueue.java | 13 +++- .../aria/core/scheduler/AbsSchedulers.java | 4 +- app/src/main/AndroidManifest.xml | 4 +- .../arialyy/aria/core/upload/UploadTask.java | 2 + build.gradle | 2 +- 13 files changed, 156 insertions(+), 52 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 7693bfd8..82e3f9c1 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 @@ -113,6 +113,9 @@ public abstract class AbsFileer 0) { mListener.onResume(mConstance.CURRENT_LOCATION); } else { @@ -516,6 +522,10 @@ public abstract class AbsFileer config); + + /** + * 任务是否已经中断 + * + * @return {@code true}中断 + */ + public boolean isBreak() { + if (mConstance.isCancel || mConstance.isStop) { + closeTimer(); + ALog.d(TAG, String.format("任务【%s】已停止或取消了", mEntity.getFileName())); + return true; + } + return false; + } } 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 0665575c..b170baf9 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 @@ -83,7 +83,7 @@ public abstract class AbsThreadTask= Build.VERSION_CODES.LOLLIPOP; } @@ -291,8 +284,8 @@ public abstract class AbsThreadTask headers) { + /* 两个map比较逻辑 1、比对key是否相同 2、如果key相同,比对value是否相同 3、只有当上面两个步骤中key 和 value都相同时才能任务两个map数据一致 */ boolean mapEquals = false; - if (mTaskEntity.getHeaders().size() == headers.size()) { + if (taskEntity.getHeaders().size() == headers.size()) { int i = 0; - Set keys = mTaskEntity.getHeaders().keySet(); + Set keys = taskEntity.getHeaders().keySet(); for (String key : keys) { if (headers.containsKey(key)) { i++; @@ -98,9 +123,9 @@ public class HttpHeaderDelegate values = mTaskEntity.getHeaders().values(); + Collection values = taskEntity.getHeaders().values(); for (String value : values) { if (headers.containsValue(value)) { j++; @@ -108,32 +133,18 @@ public class HttpHeaderDelegate keys = headers.keySet(); for (String key : keys) { - mTaskEntity.getHeaders().put(key, headers.get(key)); + taskEntity.getHeaders().put(key, headers.get(key)); } } - - return mTarget; - } - - /** - * 设置请求类型,POST或GET,默认为在GET - * 只试用于HTTP请求 - * - * @param requestEnum {@link RequestEnum} - */ - @Override - public TARGET setRequestMode(RequestEnum requestEnum) { - mTaskEntity.setRequestEnum(requestEnum); - return mTarget; } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java index 086c6e69..85100747 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java @@ -16,7 +16,11 @@ package com.arialyy.aria.core.download; import android.support.annotation.CheckResult; +import android.support.annotation.NonNull; import android.text.TextUtils; +import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.delegate.HttpHeaderDelegate; +import com.arialyy.aria.core.inf.IHttpHeaderTarget; import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -24,13 +28,17 @@ import java.io.File; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; /** * Created by AriaL on 2017/6/29. * 下载任务组 */ -public class DownloadGroupTarget extends BaseGroupTarget { +public class DownloadGroupTarget extends BaseGroupTarget implements + IHttpHeaderTarget { + private HttpHeaderDelegate + mDelegate; /** * 子任务下载地址, */ @@ -63,6 +71,7 @@ public class DownloadGroupTarget extends BaseGroupTarget { if (mEntity != null) { mDirPathTemp = mEntity.getDirPath(); } + mDelegate = new HttpHeaderDelegate<>(this, mTaskEntity); } /** @@ -253,4 +262,25 @@ public class DownloadGroupTarget extends BaseGroupTarget { return true; } + + @Override public DownloadGroupTarget addHeader(@NonNull String key, @NonNull String value) { + for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) { + mDelegate.addHeader(subTask, key, value); + } + return mDelegate.addHeader(key, value); + } + + @Override public DownloadGroupTarget addHeaders(Map headers) { + for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) { + mDelegate.addHeaders(subTask, headers); + } + return mDelegate.addHeaders(headers); + } + + @Override public DownloadGroupTarget setRequestMode(RequestEnum requestEnum) { + for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) { + subTask.setRequestEnum(requestEnum); + } + return mDelegate.setRequestMode(requestEnum); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java index 6977202a..78cd6e7b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java @@ -69,6 +69,7 @@ public class DownloadGroupTask extends AbsGroupTask { } @Override public void stop() { + super.stop(); if (!mUtil.isRunning()) { mListener.onStop(getCurrentProgress()); } else { @@ -77,6 +78,7 @@ public class DownloadGroupTask extends AbsGroupTask { } @Override public void cancel() { + super.cancel(); if (!mUtil.isRunning()) { mListener.onCancel(); } else { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index ba93dbcc..56181f2f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -121,6 +121,7 @@ public class DownloadTask extends AbsNormalTask { * 停止下载 */ @Override public void stop() { + super.stop(); stop(false); } @@ -137,6 +138,7 @@ public class DownloadTask extends AbsNormalTask { * 取消下载 */ @Override public void cancel() { + super.cancel(); if (mUtil.isRunning()) { mUtil.cancel(); } else { 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 2a398625..99bdff45 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 @@ -15,7 +15,6 @@ */ package com.arialyy.aria.core.download.downloader; -import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.core.download.DownloadEntity; @@ -491,10 +490,11 @@ public abstract class AbsGroupUtil implements IUtil { * 重试下载,只有全部都下载失败才会执行任务组的整体重试,否则只会执行单个子任务的重试 */ private void reTry(boolean needRetry) { + Downloader dt = mDownloaderMap.get(subEntity.getUrl()); synchronized (AbsGroupUtil.LOCK) { - if (subEntity.getFailNum() < 5 - && needRetry && (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)) { - reStartTask(); + if (dt != null && !dt.isBreak() && needRetry && subEntity.getFailNum() < 5 + && (NetUtils.isConnected(AriaManager.APP) || isNotNetRetry)) { + reStartTask(dt); } else { mFailMap.put(subTaskEntity.getUrl(), subTaskEntity); mListener.onSubFail(subEntity); @@ -510,7 +510,7 @@ public abstract class AbsGroupUtil implements IUtil { } } - private void reStartTask() { + private void reStartTask(final Downloader dt) { if (timer != null) { timer.purge(); timer.cancel(); @@ -518,7 +518,6 @@ public abstract class AbsGroupUtil implements IUtil { timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { - Downloader dt = mDownloaderMap.get(subEntity.getUrl()); if (dt != null) { dt.retryThreadTask(); } 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 838b0eb0..bb5a1ba6 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 @@ -37,6 +37,7 @@ public abstract class AbsTask implements ITas private String mTargetName; protected Context mContext; protected boolean isHeighestTask = false; + private boolean isCancel = false, isStop = false; public Handler getOutHandler() { return mOutHandler; @@ -121,6 +122,32 @@ public abstract class AbsTask implements ITas return mTaskEntity.getEntity() == null ? null : mTaskEntity.getEntity().getStr(); } + @Override public void stop() { + isStop = true; + } + + @Override public void cancel() { + isCancel = true; + } + + /** + * 任务是否取消了 + * + * @return {@code true}任务已经取消 + */ + public boolean isCancel() { + return isCancel; + } + + /** + * 任务是否停止了 + * + * @return {@code true}任务已经停止 + */ + public boolean isStop() { + return isStop; + } + /** * @return 返回原始byte速度,需要你在配置文件中配置 *
diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
index 5744258d..5fa95d79 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
@@ -67,11 +67,20 @@ abstract class AbsTaskQueue
     
-    
     
     
         
+    
     
       
         
diff --git a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
index 1cadfa4a..f2c91750 100644
--- a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
+++ b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
@@ -60,6 +60,7 @@ public class UploadTask extends AbsNormalTask {
   }
 
   @Override public void stop() {
+    super.stop();
     if (mUtil.isRunning()) {
       mUtil.stop();
     } else {
@@ -68,6 +69,7 @@ public class UploadTask extends AbsNormalTask {
   }
 
   @Override public void cancel() {
+    super.cancel();
     if (mUtil.isRunning()) {
       mUtil.cancel();
     } else {
diff --git a/build.gradle b/build.gradle
index f8b3a4c0..c639d1e8 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.3'
+  publishVersion = '3.4.4_dev2'
 //  publishVersion = '1.0.3'  //FTP插件
   repoName='maven'
   desc = 'android 下载框架'