Skip to content

Commit

Permalink
优化taskExists方法
Browse files Browse the repository at this point in the history
添加`post`参数请求支持
  • Loading branch information
AriaLyy committed Sep 30, 2018
1 parent ff81d43 commit 21bad6b
Show file tree
Hide file tree
Showing 25 changed files with 359 additions and 86 deletions.
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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比较逻辑
Expand Down
@@ -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();
}
}
Expand Up @@ -40,6 +40,11 @@ abstract class AbsDownloadTarget<TARGET extends AbsTarget, ENTITY extends AbsEnt
*/
String mTempFilePath;

/**
* {@code true}强制下载,不考虑文件路径是否被占用
*/
boolean forceDownload = false;

/**
* 将任务设置为最高优先级任务,最高优先级任务有以下特点:
* 1、在下载队列中,有且只有一个最高优先级任务
Expand Down
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;
Expand All @@ -38,7 +39,7 @@
* 下载任务组
*/
public class DownloadGroupTarget extends BaseGroupTarget<DownloadGroupTarget> implements
IHttpHeaderTarget<DownloadGroupTarget> {
IHttpHeaderDelegate<DownloadGroupTarget> {
private HttpHeaderDelegate<DownloadGroupTarget> mDelegate;
/**
* 子任务下载地址,
Expand Down Expand Up @@ -74,6 +75,13 @@ private void init() {
mDelegate = new HttpHeaderDelegate<>(this);
}

/**
* Post处理
*/
public PostDelegate asPost() {
return new PostDelegate<>(this);
}

/**
* 更新组合任务下载地址
*
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
Expand Up @@ -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;

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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中获取文件描述信息
*/
Expand All @@ -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);
}
}
Expand Up @@ -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.
Expand Down Expand Up @@ -60,8 +61,6 @@ public class DownloadTaskEntity extends AbsNormalTaskEntity<DownloadEntity> {
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
private String key;



public DownloadTaskEntity() {
}

Expand Down Expand Up @@ -112,6 +111,4 @@ public void setChunked(boolean chunked) {
public void setGroupTask(boolean groupTask) {
isGroupTask = groupTask;
}


}
Expand Up @@ -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) {
Expand All @@ -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");
}
Expand All @@ -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, ")
Expand Down

0 comments on commit 21bad6b

Please sign in to comment.