组合任务新增`unknownSize()`,用于处理组合任务大小未知的情况,https://github.com/AriaLyy/Aria/issues/380
parent
8c0ef0f879
commit
0c3e16cad7
@ -1,183 +1,196 @@ |
|||||||
/* |
/* |
||||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
package com.arialyy.aria.core.download; |
package com.arialyy.aria.core.download; |
||||||
|
|
||||||
import android.support.annotation.CheckResult; |
import android.support.annotation.CheckResult; |
||||||
import android.support.annotation.NonNull; |
import android.support.annotation.NonNull; |
||||||
import com.arialyy.aria.core.common.http.HttpHeaderDelegate; |
import com.arialyy.aria.core.common.http.HttpHeaderDelegate; |
||||||
import com.arialyy.aria.core.common.http.PostDelegate; |
import com.arialyy.aria.core.common.http.PostDelegate; |
||||||
import com.arialyy.aria.core.inf.IHttpHeaderDelegate; |
import com.arialyy.aria.core.inf.IHttpHeaderDelegate; |
||||||
import com.arialyy.aria.core.manager.TaskWrapperManager; |
import com.arialyy.aria.core.manager.TaskWrapperManager; |
||||||
import com.arialyy.aria.exception.ParamException; |
import com.arialyy.aria.exception.ParamException; |
||||||
import com.arialyy.aria.util.ALog; |
import com.arialyy.aria.util.ALog; |
||||||
import java.net.Proxy; |
import java.net.Proxy; |
||||||
import java.util.List; |
import java.util.List; |
||||||
import java.util.Map; |
import java.util.Map; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by AriaL on 2017/6/29. |
* Created by AriaL on 2017/6/29. |
||||||
* 下载任务组 |
* 下载任务组 |
||||||
*/ |
*/ |
||||||
public class DownloadGroupTarget extends AbsDGTarget<DownloadGroupTarget> implements |
public class DownloadGroupTarget extends AbsDGTarget<DownloadGroupTarget> implements |
||||||
IHttpHeaderDelegate<DownloadGroupTarget> { |
IHttpHeaderDelegate<DownloadGroupTarget> { |
||||||
private HttpHeaderDelegate<DownloadGroupTarget> mHeaderDelegate; |
private HttpHeaderDelegate<DownloadGroupTarget> mHeaderDelegate; |
||||||
private HttpGroupDelegate mGroupDelegate; |
private HttpGroupDelegate mGroupDelegate; |
||||||
|
|
||||||
DownloadGroupTarget(DownloadGroupEntity groupEntity, String targetName) { |
DownloadGroupTarget(DownloadGroupEntity groupEntity, String targetName) { |
||||||
setTargetName(targetName); |
setTargetName(targetName); |
||||||
if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) { |
if (groupEntity.getUrls() != null && !groupEntity.getUrls().isEmpty()) { |
||||||
init(groupEntity.getUrls()); |
init(groupEntity.getUrls()); |
||||||
} else { |
} else { |
||||||
throw new ParamException("组合任务只任务下载地址为空"); |
throw new ParamException("组合任务只任务下载地址为空"); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
DownloadGroupTarget(List<String> urls, String targetName) { |
DownloadGroupTarget(List<String> urls, String targetName) { |
||||||
setTargetName(targetName); |
setTargetName(targetName); |
||||||
init(urls); |
init(urls); |
||||||
} |
} |
||||||
|
|
||||||
private void init(List<String> urls) { |
private void init(List<String> urls) { |
||||||
mGroupDelegate = new HttpGroupDelegate(this, |
mGroupDelegate = new HttpGroupDelegate(this, |
||||||
TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, urls)); |
TaskWrapperManager.getInstance().getDGTaskWrapper(DGTaskWrapper.class, urls)); |
||||||
mHeaderDelegate = new HttpHeaderDelegate<>(this); |
mHeaderDelegate = new HttpHeaderDelegate<>(this); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Post处理 |
* Post处理 |
||||||
*/ |
*/ |
||||||
@CheckResult |
@CheckResult |
||||||
public PostDelegate asPost() { |
public PostDelegate asPost() { |
||||||
return new PostDelegate<>(this); |
return new PostDelegate<>(this); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 更新组合任务下载地址 |
* 更新组合任务下载地址 |
||||||
* |
* |
||||||
* @param urls 新的组合任务下载地址列表 |
* @param urls 新的组合任务下载地址列表 |
||||||
*/ |
*/ |
||||||
@CheckResult |
@CheckResult |
||||||
public DownloadGroupTarget updateUrls(List<String> urls) { |
public DownloadGroupTarget updateUrls(List<String> urls) { |
||||||
return mGroupDelegate.updateUrls(urls); |
return mGroupDelegate.updateUrls(urls); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 任务组总任务大小,任务组是一个抽象的概念,没有真实的数据实体,任务组的大小是Aria动态获取子任务大小相加而得到的, |
* 任务组总任务大小,任务组是一个抽象的概念,没有真实的数据实体,任务组的大小是Aria动态获取子任务大小相加而得到的, |
||||||
* 如果你知道当前任务组总大小,你也可以调用该方法给任务组设置大小 |
* 如果你知道当前任务组总大小,你也可以调用该方法给任务组设置大小 |
||||||
* |
* |
||||||
* 为了更好的用户体验,组合任务必须设置文件大小 |
* 为了更好的用户体验,组合任务最好设置文件大小,默认需要强制设置文件大小。如果无法获取到总长度,请调用{@link #unknownSize()} |
||||||
* |
* |
||||||
* @param fileSize 任务组总大小 |
* @param fileSize 任务组总大小 |
||||||
*/ |
*/ |
||||||
@CheckResult |
@CheckResult |
||||||
public DownloadGroupTarget setFileSize(long fileSize) { |
public DownloadGroupTarget setFileSize(long fileSize) { |
||||||
if (fileSize <= 0) { |
if (fileSize <= 0) { |
||||||
ALog.e(TAG, "文件大小不能小于 0"); |
ALog.e(TAG, "文件大小不能小于 0"); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
if (getEntity().getFileSize() <= 1 || getEntity().getFileSize() != fileSize) { |
if (getEntity().getFileSize() <= 1 || getEntity().getFileSize() != fileSize) { |
||||||
getEntity().setFileSize(fileSize); |
getEntity().setFileSize(fileSize); |
||||||
} |
} |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 如果你是使用{@link DownloadReceiver#load(DownloadGroupEntity)}进行下载操作,那么你需要设置任务组的下载地址 |
* 如果无法获取到组合任务到总长度,请调用该方法, |
||||||
*/ |
* 请注意: |
||||||
@CheckResult |
* 1、如果组合任务到子任务数过多,请不要使用该标志位,否则Aria将需要消耗大量的时间获取组合任务的总长度。 |
||||||
public DownloadGroupTarget setGroupUrl(List<String> urls) { |
* 2、如果你的知道组合任务的总长度,请使用{@link #setFileSize(long)}设置组合任务的长度。 |
||||||
return mGroupDelegate.setGroupUrl(urls); |
* 3、由于网络或其它原因的存在,这种方式获取的组合任务大小有可能是不准确的。 |
||||||
} |
*/ |
||||||
|
@CheckResult |
||||||
/** |
public DownloadGroupTarget unknownSize() { |
||||||
* 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 |
getTaskWrapper().setUnknownSize(true); |
||||||
* |
return this; |
||||||
* @deprecated {@link #setSubFileName(List)} 请使用该api |
} |
||||||
*/ |
|
||||||
@CheckResult |
/** |
||||||
@Deprecated public DownloadGroupTarget setSubTaskFileName(List<String> subTaskFileName) { |
* 如果你是使用{@link DownloadReceiver#load(DownloadGroupEntity)}进行下载操作,那么你需要设置任务组的下载地址 |
||||||
return setSubFileName(subTaskFileName); |
*/ |
||||||
} |
@CheckResult |
||||||
|
public DownloadGroupTarget setGroupUrl(List<String> urls) { |
||||||
/** |
return mGroupDelegate.setGroupUrl(urls); |
||||||
* 设置任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。 |
} |
||||||
* 如:groupDirPath = "/mnt/sdcard/download/group_test" |
|
||||||
* <pre> |
/** |
||||||
* {@code |
* 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 |
||||||
* + mnt |
* |
||||||
* + sdcard |
* @deprecated {@link #setSubFileName(List)} 请使用该api |
||||||
* + download |
*/ |
||||||
* + group_test |
@CheckResult |
||||||
* - task1.apk |
@Deprecated public DownloadGroupTarget setSubTaskFileName(List<String> subTaskFileName) { |
||||||
* - task2.apk |
return setSubFileName(subTaskFileName); |
||||||
* - task3.apk |
} |
||||||
* .... |
|
||||||
* |
/** |
||||||
* } |
* 设置任务组的文件夹路径,在Aria中,任务组的所有子任务都会下载到以任务组组名的文件夹中。 |
||||||
* </pre> |
* 如:groupDirPath = "/mnt/sdcard/download/group_test" |
||||||
* |
* <pre> |
||||||
* @param dirPath 任务组保存文件夹路径 |
* {@code |
||||||
*/ |
* + mnt |
||||||
@CheckResult |
* + sdcard |
||||||
public DownloadGroupTarget setDirPath(String dirPath) { |
* + download |
||||||
return mGroupDelegate.setDirPath(dirPath); |
* + group_test |
||||||
} |
* - task1.apk |
||||||
|
* - task2.apk |
||||||
/** |
* - task3.apk |
||||||
* 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 |
* .... |
||||||
*/ |
* |
||||||
@CheckResult |
* } |
||||||
public DownloadGroupTarget setSubFileName(List<String> subTaskFileName) { |
* </pre> |
||||||
return mGroupDelegate.setSubFileName(subTaskFileName); |
* |
||||||
} |
* @param dirPath 任务组保存文件夹路径 |
||||||
|
*/ |
||||||
@Override public int getTargetType() { |
@CheckResult |
||||||
return GROUP_HTTP; |
public DownloadGroupTarget setDirPath(String dirPath) { |
||||||
} |
return mGroupDelegate.setDirPath(dirPath); |
||||||
|
} |
||||||
@Override protected boolean checkEntity() { |
|
||||||
return mGroupDelegate.checkEntity(); |
/** |
||||||
} |
* 设置子任务文件名,该方法必须在{@link #setDirPath(String)}之后调用,否则不生效 |
||||||
|
*/ |
||||||
@Override public boolean isRunning() { |
@CheckResult |
||||||
return mGroupDelegate.isRunning(); |
public DownloadGroupTarget setSubFileName(List<String> subTaskFileName) { |
||||||
} |
return mGroupDelegate.setSubFileName(subTaskFileName); |
||||||
|
} |
||||||
@Override public boolean taskExists() { |
|
||||||
return mGroupDelegate.taskExists(); |
@Override public int getTargetType() { |
||||||
} |
return GROUP_HTTP; |
||||||
|
} |
||||||
@CheckResult |
|
||||||
@Override public DownloadGroupTarget addHeader(@NonNull String key, @NonNull String value) { |
@Override protected boolean checkEntity() { |
||||||
for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) { |
return mGroupDelegate.checkEntity(); |
||||||
mHeaderDelegate.addHeader(subTask, key, value); |
} |
||||||
} |
|
||||||
return mHeaderDelegate.addHeader(key, value); |
@Override public boolean isRunning() { |
||||||
} |
return mGroupDelegate.isRunning(); |
||||||
|
} |
||||||
@CheckResult |
|
||||||
@Override public DownloadGroupTarget addHeaders(Map<String, String> headers) { |
@Override public boolean taskExists() { |
||||||
for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) { |
return mGroupDelegate.taskExists(); |
||||||
mHeaderDelegate.addHeaders(subTask, headers); |
} |
||||||
} |
|
||||||
return mHeaderDelegate.addHeaders(headers); |
@CheckResult |
||||||
} |
@Override public DownloadGroupTarget addHeader(@NonNull String key, @NonNull String value) { |
||||||
|
for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) { |
||||||
@CheckResult |
mHeaderDelegate.addHeader(subTask, key, value); |
||||||
@Override public DownloadGroupTarget setUrlProxy(Proxy proxy) { |
} |
||||||
return mHeaderDelegate.setUrlProxy(proxy); |
return mHeaderDelegate.addHeader(key, value); |
||||||
} |
} |
||||||
} |
|
||||||
|
@CheckResult |
||||||
|
@Override public DownloadGroupTarget addHeaders(Map<String, String> headers) { |
||||||
|
for (DTaskWrapper subTask : getTaskWrapper().getSubTaskWrapper()) { |
||||||
|
mHeaderDelegate.addHeaders(subTask, headers); |
||||||
|
} |
||||||
|
return mHeaderDelegate.addHeaders(headers); |
||||||
|
} |
||||||
|
|
||||||
|
@CheckResult |
||||||
|
@Override public DownloadGroupTarget setUrlProxy(Proxy proxy) { |
||||||
|
return mHeaderDelegate.setUrlProxy(proxy); |
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue