Permalink
Browse files

增加广播支持,增加get参数

  • Loading branch information...
AriaLyy committed Dec 4, 2018
1 parent ed0c98f commit 44f3b4faf12a3ae0b258a8a7b3f41f4b3d72f840
@@ -14,4 +14,5 @@
.idea
/cache
*.log
uml
uml
*.swp
@@ -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;

/**
* get处理委托类
*/
public class GetDelegate<TARGET extends AbsTarget> implements IPostDelegate<TARGET>, ITarget {
private static final String TAG = "PostDelegate";
private TARGET mTarget;

public GetDelegate(TARGET target) {
mTarget = target;
mTarget.getTaskEntity().setRequestEnum(RequestEnum.GET);
}

@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();
}
}
@@ -57,6 +57,10 @@ public DownloadGroupEntity getEntity() {
? mTaskEntity.getEntity().getGroupName() : mTaskEntity.getEntity().getAlias());
}

@Override public int getTaskType() {
return DOWNLOAD_GROUP;
}

public static class Builder {
DownloadGroupTaskEntity taskEntity;
Handler outHandler;
@@ -18,6 +18,7 @@
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.common.http.GetDelegate;
import com.arialyy.aria.core.common.http.HttpHeaderDelegate;
import com.arialyy.aria.core.common.http.PostDelegate;
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
@@ -48,6 +49,13 @@ public PostDelegate asPost() {
return new PostDelegate<>(this);
}

/**
* get参数传递
*/
public GetDelegate asGet(){
return new GetDelegate<>(this);
}

/**
* 是否使用服务器通过content-disposition传递的文件名,内容格式{@code attachment;filename=***}
* 如果获取不到服务器文件名,则使用用户设置的文件名
@@ -68,6 +68,10 @@ public DownloadEntity getEntity() {
return mEntity.getUrl();
}

@Override public int getTaskType() {
return DOWNLOAD;
}

@Override public String getKey() {
return mEntity.getUrl();
}
@@ -21,16 +21,22 @@
import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.SSLContextUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
@@ -39,10 +45,36 @@
import javax.net.ssl.SSLSocketFactory;

/**
* Created by lyy on 2017/1/18.
* 链接帮助类
* Created by lyy on 2017/1/18. 链接帮助类
*/
class ConnectionHelp {
private static final String TAG = "ConnectionHelp";

/**
* 处理url参数
*
* @throws MalformedURLException
*/
static URL handleUrl(String url, AbsTaskEntity taskEntity) throws MalformedURLException {
Map<String, String> params = taskEntity.getParams();
if (params != null && taskEntity.getRequestEnum() == RequestEnum.GET) {
if (url.contains("?")) {
ALog.e(TAG, String.format("设置参数失败,url中已经有?,url: %s", url));
return new URL(CommonUtil.convertUrl(url));
}
StringBuilder sb = new StringBuilder();
sb.append(url).append("?");
Set<String> keys = params.keySet();
for (String key : keys) {
sb.append(key).append("=").append(URLEncoder.encode(params.get(key))).append("&");
}
String temp = sb.toString();
temp = temp.substring(0, temp.length() - 1);
return new URL(CommonUtil.convertUrl(temp));
} else {
return new URL(CommonUtil.convertUrl(url));
}
}

/**
* 转换HttpUrlConnect的inputStream流
@@ -69,7 +69,7 @@
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
HttpURLConnection conn = null;
try {
URL url = new URL(CommonUtil.convertUrl(mEntity.getUrl()));
URL url = ConnectionHelp.handleUrl(mEntity.getUrl(), mTaskEntity);
conn = ConnectionHelp.handleConnection(url, mTaskEntity);
conn = ConnectionHelp.setConnectParam(mTaskEntity, conn);
conn.setRequestProperty("Range", "bytes=" + 0 + "-");
@@ -275,7 +275,7 @@ private void handleUrlReTurn(HttpURLConnection conn, String newUrl) throws IOExc
mEntity.setRedirect(true);
mEntity.setRedirectUrl(newUrl);
String cookies = conn.getHeaderField("Set-Cookie");
URL url = new URL(CommonUtil.convertUrl(newUrl));
URL url = ConnectionHelp.handleUrl(newUrl, mTaskEntity);
conn = ConnectionHelp.handleConnection(url, mTaskEntity);
conn = ConnectionHelp.setConnectParam(mTaskEntity, conn);
conn.setRequestProperty("Cookie", cookies);
@@ -75,7 +75,7 @@
//当前子线程的下载位置
mChildCurrentLocation = mConfig.START_LOCATION;
try {
URL url = new URL(CommonUtil.convertUrl(mConfig.URL));
URL url = ConnectionHelp.handleUrl(mConfig.URL, mTaskEntity);
conn = ConnectionHelp.handleConnection(url, mTaskEntity);
if (mConfig.SUPPORT_BP) {
ALog.d(TAG,
@@ -17,6 +17,7 @@

import android.content.Context;
import android.os.Handler;
import android.os.Parcelable;
import android.text.TextUtils;
import com.arialyy.aria.core.common.IUtil;
import com.arialyy.aria.util.ALog;
@@ -63,8 +64,7 @@ public Handler getOutHandler() {
}

/**
* 添加扩展数据
* 读取扩展数据{@link #getExpand(String)}
* 添加扩展数据 读取扩展数据{@link #getExpand(String)}
*/
public void putExpand(String key, Object obj) {
if (TextUtils.isEmpty(key)) {
@@ -20,6 +20,34 @@
*/
public interface ITask<TASK_ENTITY extends AbsTaskEntity> {

/**
* 普通下载任务
*/
int DOWNLOAD = 1;
/**
* 上传任务
*/
int UPLOAD = 2;
/**
* 组合任务
*/
int DOWNLOAD_GROUP = 3;
/**
* 组合任务的子任务
*/
int DOWNLOAD_GROUP_SUB = 4;
/**
* 未知
*/
int OTHER = -1;

/**
* 获取任务类型
*
* @return {@link #DOWNLOAD}、{@link #UPLOAD}、{@link #DOWNLOAD_GROUP}
*/
int getTaskType();

/**
* 获取下载状态
*/
@@ -51,6 +79,7 @@

/**
* 停止任务
*
* @param type {@code 0}默认操作,{@code 1}停止任务不自动执行下一任务
*/
void stop(int type);
Oops, something went wrong.

0 comments on commit 44f3b4f

Please sign in to comment.