Permalink
Browse files

优化`taskExists`方法

添加`post`参数请求支持
  • Loading branch information...
AriaLyy committed Sep 30, 2018
1 parent ff81d43 commit 21bad6b9c1fea40e3b995b2ec782035f009cc9b2
Showing with 359 additions and 86 deletions.
  1. +2 −19 Aria/src/main/java/com/arialyy/aria/core/common/http/HttpHeaderDelegate.java
  2. +86 −0 Aria/src/main/java/com/arialyy/aria/core/common/http/PostDelegate.java
  3. +5 −0 Aria/src/main/java/com/arialyy/aria/core/download/AbsDownloadTarget.java
  4. +1 −1 Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java
  5. +18 −11 Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTarget.java
  6. +2 −3 Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  7. +30 −13 Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  8. +1 −4 Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  9. +6 −2 Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java
  10. +26 −3 Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpFileInfoThread.java
  11. +22 −0 Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  12. +1 −0 Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  13. +13 −0 Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  14. +1 −9 Aria/src/main/java/com/arialyy/aria/core/inf/{IHttpHeaderTarget.java → IHttpHeaderDelegate.java}
  15. +31 −0 Aria/src/main/java/com/arialyy/aria/core/inf/IPostDelegate.java
  16. +1 −1 Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  17. +10 −8 Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
  18. +0 −7 Aria/src/main/java/com/arialyy/aria/orm/DelegateWrapper.java
  19. +12 −0 DEV_LOG.md
  20. +6 −2 app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  21. +0 −1 app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java
  22. +1 −1 app/src/main/java/com/arialyy/simple/upload/HttpUploadActivity.java
  23. +1 −1 build.gradle
  24. +21 −0 py/download.py
  25. +62 −0 py/upload.py
@@ -17,10 +17,9 @@
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IHttpHeaderTarget;
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
import com.arialyy.aria.util.ALog;
import java.net.Proxy;
import java.util.Collection;
@@ -32,7 +31,7 @@
* HTTP header参数设置委托类
*/
public class HttpHeaderDelegate<TARGET extends AbsTarget>
implements IHttpHeaderTarget<TARGET> {
implements IHttpHeaderDelegate<TARGET> {
private static final String TAG = "HttpHeaderDelegate";
private TARGET mTarget;
@@ -76,18 +75,6 @@ public TARGET addHeaders(@NonNull Map<String, String> headers) {
return mTarget;
}
/**
* 设置请求类型,POST或GET,默认为在GET
* 只试用于HTTP请求
*
* @param requestEnum {@link RequestEnum}
*/
@Override
public TARGET setRequestMode(RequestEnum requestEnum) {
setRequestMode(mTarget.getTaskEntity(), requestEnum);
return mTarget;
}
@Override public TARGET setUrlProxy(Proxy proxy) {
mTarget.getTaskEntity().setProxy(proxy);
return mTarget;
@@ -101,10 +88,6 @@ public void addHeader(AbsTaskEntity taskEntity, String key, String value) {
}
}
public void setRequestMode(AbsTaskEntity taskEntity, RequestEnum requestEnum) {
taskEntity.setRequestEnum(requestEnum);
}
public void addHeaders(AbsTaskEntity taskEntity, Map<String, String> headers) {
/*
两个map比较逻辑
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.common.http;
import android.text.TextUtils;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.download.DownloadGroupTarget;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.inf.IPostDelegate;
import com.arialyy.aria.core.inf.ITarget;
import com.arialyy.aria.util.ALog;
import java.util.HashMap;
import java.util.Map;
/**
* post处理委托类
*/
public class PostDelegate<TARGET extends AbsTarget> implements IPostDelegate<TARGET>, ITarget {
private static final String TAG = "PostDelegate";
private TARGET mTarget;
public PostDelegate(TARGET target) {
mTarget = target;
mTarget.getTaskEntity().setRequestEnum(RequestEnum.POST);
}
@Override public TARGET setParams(Map<String, String> params) {
mTarget.getTaskEntity().setParams(params);
if (mTarget instanceof DownloadGroupTarget) {
for (DownloadTaskEntity subTask : ((DownloadGroupTaskEntity) mTarget.getTaskEntity()).getSubTaskEntities()) {
subTask.setParams(params);
}
}
return mTarget;
}
@Override public TARGET setParam(String key, String value) {
if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
ALog.d(TAG, "key 或value 为空");
return mTarget;
}
Map<String, String> params = mTarget.getTaskEntity().getParams();
if (params == null) {
params = new HashMap<>();
mTarget.getTaskEntity().setParams(params);
}
params.put(key, value);
if (mTarget instanceof DownloadGroupTarget) {
for (DownloadTaskEntity subTask : ((DownloadGroupTaskEntity) mTarget.getTaskEntity()).getSubTaskEntities()) {
subTask.setParams(params);
}
}
return mTarget;
}
@Override public void start() {
mTarget.start();
}
@Override public void stop() {
mTarget.stop();
}
@Override public void resume() {
mTarget.resume();
}
@Override public void cancel() {
mTarget.cancel();
}
}
@@ -40,6 +40,11 @@
*/
String mTempFilePath;
/**
* {@code true}强制下载,不考虑文件路径是否被占用
*/
boolean forceDownload = false;
/**
* 将任务设置为最高优先级任务,最高优先级任务有以下特点:
* 1、在下载队列中,有且只有一个最高优先级任务
@@ -164,7 +164,7 @@ private boolean checkFilePath() {
//设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径
if (!filePath.equals(mEntity.getDownloadPath())) {
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) {
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath) && !forceDownload) {
ALog.e(TAG, "下载失败,保存路径【" + filePath + "】已经被其它任务占用,请设置其它保存路径");
return false;
}
@@ -20,7 +20,8 @@
import android.text.TextUtils;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.common.http.HttpHeaderDelegate;
import com.arialyy.aria.core.inf.IHttpHeaderTarget;
import com.arialyy.aria.core.common.http.PostDelegate;
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
import com.arialyy.aria.core.manager.TEManager;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.ALog;
@@ -38,7 +39,7 @@
* 下载任务组
*/
public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> implements
IHttpHeaderTarget<DownloadGroupTarget> {
IHttpHeaderDelegate<DownloadGroupTarget> {
private HttpHeaderDelegate<DownloadGroupTarget> mDelegate;
/**
* 子任务下载地址,
@@ -74,6 +75,13 @@ private void init() {
mDelegate = new HttpHeaderDelegate<>(this);
}
/**
* Post处理
*/
public PostDelegate asPost() {
return new PostDelegate<>(this);
}
/**
* 更新组合任务下载地址
*
@@ -178,6 +186,12 @@ public DownloadGroupTarget setSubFileName(List<String> subTaskFileName) {
return false;
}
if (mTaskEntity.getRequestEnum() == RequestEnum.POST) {
for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) {
subTask.setRequestEnum(RequestEnum.POST);
}
}
mEntity.save();
mTaskEntity.save();
@@ -262,7 +276,8 @@ private void updateSingleSubFileName(DownloadTaskEntity taskEntity, String newNa
if (!newName.equals(entity.getFileName())) {
String oldPath = mEntity.getDirPath() + "/" + entity.getFileName();
String newPath = mEntity.getDirPath() + "/" + newName;
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=? or isComplete='true'", newPath)) {
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=? or isComplete='true'",
newPath)) {
ALog.w(TAG, String.format("更新文件名失败,路径【%s】已存在或文件已下载", newPath));
return;
}
@@ -313,14 +328,6 @@ private boolean checkSubName() {
return mDelegate.addHeaders(headers);
}
@CheckResult
@Override public DownloadGroupTarget setRequestMode(RequestEnum requestEnum) {
for (DownloadTaskEntity subTask : mTaskEntity.getSubTaskEntities()) {
subTask.setRequestEnum(requestEnum);
}
return mDelegate.setRequestMode(requestEnum);
}
@CheckResult
@Override public DownloadGroupTarget setUrlProxy(Proxy proxy) {
return mDelegate.setUrlProxy(proxy);
@@ -288,7 +288,7 @@ public DownloadGroupTaskEntity getFtpDirTask(String dirUrl) {
* @return {@code true}存在,{@code false} 不存在
*/
public boolean taskExists(String downloadUrl) {
return DownloadEntity.findFirst(DownloadEntity.class, "url=?", downloadUrl) != null;
return DbEntity.checkDataExist(DownloadTaskEntity.class, "url=?", downloadUrl);
}
/**
@@ -301,8 +301,7 @@ public boolean taskExists(List<String> urls) {
return false;
}
String groupName = CommonUtil.getMd5Code(urls);
return DownloadGroupEntity.findFirst(DownloadGroupEntity.class, "groupName=?", groupName)
!= null;
return DbEntity.checkDataExist(DownloadGroupEntity.class, "groupName=?", groupName);
}
/**
@@ -19,7 +19,8 @@
import android.support.annotation.NonNull;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.common.http.HttpHeaderDelegate;
import com.arialyy.aria.core.inf.IHttpHeaderTarget;
import com.arialyy.aria.core.common.http.PostDelegate;
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
import java.net.Proxy;
import java.util.Map;
@@ -28,17 +29,23 @@
* https://github.com/AriaLyy/Aria
*/
public class DownloadTarget extends BaseNormalTarget<DownloadTarget>
implements IHttpHeaderTarget<DownloadTarget> {
private HttpHeaderDelegate<DownloadTarget> mDelegate;
implements IHttpHeaderDelegate<DownloadTarget> {
private HttpHeaderDelegate<DownloadTarget> mHeaderDelegate;
DownloadTarget(DownloadEntity entity, String targetName) {
this(entity.getUrl(), targetName);
}
DownloadTarget(String url, String targetName) {
initTarget(url, targetName);
mDelegate = new HttpHeaderDelegate<>(this);
mHeaderDelegate = new HttpHeaderDelegate<>(this);
}
/**
* Post处理
*/
public PostDelegate asPost() {
return new PostDelegate<>(this);
}
/**
@@ -78,6 +85,21 @@ public DownloadTarget setFilePath(@NonNull String filePath) {
return this;
}
/**
* 设置文件存储路径,如果需要修改新的文件名,修改路径便可。
* 如:原文件路径 /mnt/sdcard/test.zip
* 如果需要将test.zip改为game.zip,只需要重新设置文件路径为:/mnt/sdcard/game.zip
*
* @param filePath 路径必须为文件路径,不能为文件夹路径
* @param forceDownload {@code true}强制下载,不考虑未见路径是否被占用
*/
@CheckResult
public DownloadTarget setFilePath(@NonNull String filePath, boolean forceDownload) {
mTempFilePath = filePath;
this.forceDownload = forceDownload;
return this;
}
/**
* 从header中获取文件描述信息
*/
@@ -89,29 +111,24 @@ public String getContentDisposition() {
return HTTP;
}
/**
* 设置URL的代理
*
* @param proxy {@link Proxy}
*/
@CheckResult
@Override public DownloadTarget setUrlProxy(Proxy proxy) {
return mDelegate.setUrlProxy(proxy);
return mHeaderDelegate.setUrlProxy(proxy);
}
@CheckResult
@Override public DownloadTarget addHeader(@NonNull String key, @NonNull String value) {
return mDelegate.addHeader(key, value);
return mHeaderDelegate.addHeader(key, value);
}
@CheckResult
@Override public DownloadTarget addHeaders(Map<String, String> headers) {
return mDelegate.addHeaders(headers);
return mHeaderDelegate.addHeaders(headers);
}
@CheckResult
@Override public DownloadTarget setRequestMode(RequestEnum requestEnum) {
return mDelegate.setRequestMode(requestEnum);
}
}
@@ -21,6 +21,7 @@
import com.arialyy.aria.orm.annotation.Ignore;
import com.arialyy.aria.orm.annotation.NoNull;
import com.arialyy.aria.orm.annotation.Primary;
import java.util.Map;
/**
* Created by lyy on 2017/1/23.
@@ -60,8 +61,6 @@
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
private String key;
public DownloadTaskEntity() {
}
@@ -112,6 +111,4 @@ public void setChunked(boolean chunked) {
public void setGroupTask(boolean groupTask) {
isGroupTask = groupTask;
}
}
@@ -100,6 +100,8 @@ static HttpURLConnection handleConnection(URL url, AbsTaskEntity taskEntity) thr
static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConnection conn) {
if (entity.getRequestEnum() == RequestEnum.POST) {
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
}
Set<String> keys = null;
if (entity.getHeaders() != null && entity.getHeaders().size() > 0) {
@@ -108,7 +110,9 @@ static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConne
conn.setRequestProperty(key, entity.getHeaders().get(key));
}
}
if (conn.getRequestProperty("Accept-Language") == null) {
conn.setRequestProperty("Accept-Language", "UTF-8");
}
if (conn.getRequestProperty("Accept-Encoding") == null) {
conn.setRequestProperty("Accept-Encoding", "identity");
}
@@ -126,7 +130,7 @@ static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConne
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
}
if (conn.getRequestProperty("Accept") == null) {
StringBuilder accept = new StringBuilder();
//StringBuilder accept = new StringBuilder();
//accept
//.append("image/gif, ")
//.append("image/jpeg, ")
Oops, something went wrong.

0 comments on commit 21bad6b

Please sign in to comment.