Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lib able to insert headers to each download request #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.aspsine.multithreaddownload"
minSdkVersion 11
targetSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
Expand All @@ -33,9 +33,9 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile project(':library')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public class DataSource {
"微信电话本",
"淘宝",
"聚美优品",
"搜房网"
"搜房网",
"teste quickblox",
"teste quickblox video"
};

private static final String[] IMAGES = {
Expand All @@ -38,7 +40,9 @@ public class DataSource {
"http://img.wdjimg.com/mms/icon/v1/b/fe/718d7c213ce633fd4e25c278c19acfeb_512_512.png",
"http://img.wdjimg.com/mms/icon/v1/f/29/cf90d1294ac84da3b49561a6f304029f_512_512.png",
"http://img.wdjimg.com/mms/icon/v1/4/43/0318ce32731600bfa66cbb5018e1a434_512_512.png",
"http://img.wdjimg.com/mms/icon/v1/7/08/2b3858e31efdee8a7f28b06bdb83a087_512_512.png"
"http://img.wdjimg.com/mms/icon/v1/7/08/2b3858e31efdee8a7f28b06bdb83a087_512_512.png",
"http://img.wdjimg.com/mms/icon/v1/7/08/2b3858e31efdee8a7f28b06bdb83a087_512_512.png",
"http://img.wdjimg.com/mms/icon/v1/e/d0/03a49009c73496fb8ba6f779fec99d0e_512_512.png"
};

private static final String[] URLS = {
Expand All @@ -52,7 +56,9 @@ public class DataSource {
"http://dldir1.qq.com/qqcontacts/100001_phonebook_4.0.0_3148.apk",
"http://download.alicdn.com/wireless/taobao4android/latest/702757.apk",
"http://apps.wandoujia.com/apps/com.jm.android.jumei/download",
"http://download.3g.fang.com/soufun_android_30001_7.9.0.apk"
"http://download.3g.fang.com/soufun_android_30001_7.9.0.apk",
"https://api.quickblox.com/blobs/3125095/download.xml",
"https://api.quickblox.com/blobs/3125089/download.xml"
};

public static DataSource getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

import java.io.File;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
Expand Down Expand Up @@ -88,6 +90,8 @@ public void onPause() {
*/
private final File mDownloadDir = new File(Environment.getExternalStorageDirectory(), "Download");

private Map<String, String> headers;

@Override
public void onItemClick(View v, int position, AppInfo appInfo) {
if (appInfo.getStatus() == AppInfo.STATUS_DOWNLOADING || appInfo.getStatus() == AppInfo.STATUS_CONNECTING) {
Expand All @@ -102,10 +106,14 @@ public void onItemClick(View v, int position, AppInfo appInfo) {
}

private void download(final int position, String tag, final AppInfo appInfo) {
headers = new HashMap<>(1);
headers.put("QB-Token","76e88913e9b5f7e496b7b9e654474695485ac51d");

final DownloadRequest request = new DownloadRequest.Builder()
.setTitle(appInfo.getName() + ".apk")
.setTitle(appInfo.getName()+ ".png")
.setUri(appInfo.getUrl())
.setFolder(mDownloadDir)
.setHeaders(headers)
.build();

DownloadManager.getInstance().download(request, tag, new DownloadCallback(position, appInfo));
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion '23.0.0'
buildToolsVersion '23.0.2'

defaultConfig {
minSdkVersion 10
targetSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
Expand All @@ -20,5 +20,5 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:appcompat-v7:23.1.1'
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.aspsine.multithreaddownload;

import android.support.annotation.Nullable;

import java.io.File;
import java.io.Serializable;
import java.util.Map;

/**
* Created by aspsine on 15-4-19.
Expand All @@ -14,16 +17,18 @@ public class DownloadInfo {
private long length;
private long finished;
private boolean acceptRanges;
private Map<String,String> headers;

private int status;

public DownloadInfo() {
}

public DownloadInfo(String name, String uri, File dir) {
public DownloadInfo(String name, String uri, File dir, @Nullable Map<String,String> headers) {
this.name = name;
this.uri = uri;
this.dir = dir;
this.headers = headers;
}

public String getName() {
Expand Down Expand Up @@ -81,4 +86,12 @@ public boolean isAcceptRanges() {
public void setAcceptRanges(boolean acceptRanges) {
this.acceptRanges = acceptRanges;
}

public Map<String, String> getHeaders() {
return headers;
}

public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.aspsine.multithreaddownload;


import android.support.annotation.Nullable;

import java.io.File;
import java.util.Map;

/**
* Created by Aspsine on 2015/4/20.
Expand All @@ -17,15 +20,18 @@ public class DownloadRequest {

private boolean mScannable;

private Map<String, String> headers;

private DownloadRequest() {
}

private DownloadRequest(String uri, File folder, CharSequence title, CharSequence description, boolean scannable) {
this.mUri = uri;
this.mFolder = folder;
this.mTitle = title;
this.mDescription = description;
this.mScannable = scannable;
public DownloadRequest(String mUri, File mFolder, CharSequence mTitle, CharSequence mDescription, boolean mScannable,@Nullable Map<String, String> headers) {
this.mUri = mUri;
this.mFolder = mFolder;
this.mTitle = mTitle;
this.mDescription = mDescription;
this.mScannable = mScannable;
this.headers = headers;
}

public String getUri() {
Expand All @@ -48,6 +54,10 @@ public boolean isScannable() {
return mScannable;
}

public Map<String, String> getHeaders() {
return headers;
}

public static class Builder {

private String mUri;
Expand All @@ -60,6 +70,8 @@ public static class Builder {

private boolean mScannable;

private Map<String, String> headers;

public Builder() {
}

Expand Down Expand Up @@ -88,8 +100,13 @@ public Builder setScannable(boolean scannable) {
return this;
}

public Builder setHeaders (Map<String, String> headers) {
this.headers = headers;
return this;
}

public DownloadRequest build() {
DownloadRequest request = new DownloadRequest(mUri, mFolder, mTitle, mDescription, mScannable);
DownloadRequest request = new DownloadRequest(mUri, mFolder, mTitle, mDescription, mScannable, headers);
return request;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.aspsine.multithreaddownload.core;

import android.os.Process;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import com.aspsine.multithreaddownload.Constants;
Expand All @@ -13,6 +14,9 @@
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
* Created by Aspsine on 2015/7/20.
Expand All @@ -25,9 +29,12 @@ public class ConnectTaskImpl implements ConnectTask {

private volatile long mStartTime;

public ConnectTaskImpl(String uri, OnConnectListener listener) {
private volatile Map<String, String> headers;

public ConnectTaskImpl(String uri, Map<String, String> headers, OnConnectListener listener) {
this.mUri = uri;
this.mOnConnectListener = listener;
this.headers = headers;
}

public void cancel() {
Expand Down Expand Up @@ -81,6 +88,8 @@ private void executeConnection() throws DownloadException {
httpConnection.setReadTimeout(Constants.HTTP.READ_TIME_OUT);
httpConnection.setRequestMethod(Constants.HTTP.GET);
httpConnection.setRequestProperty("Range", "bytes=" + 0 + "-");
setHttpHeader(headers, httpConnection);

final int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
parseResponse(httpConnection, false);
Expand All @@ -100,6 +109,15 @@ private void executeConnection() throws DownloadException {
}
}


private void setHttpHeader(Map<String, String> headers, URLConnection connection) {
if (headers != null) {
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
}
}

private void parseResponse(HttpURLConnection httpConnection, boolean isAcceptRanges) throws DownloadException {

final long length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,19 @@ private void executeDownload() throws DownloadException {
}

private void setHttpHeader(Map<String, String> headers, URLConnection connection) {
if (headers != null) {
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
if (mDownloadInfo.getHeaders() != null) {
if(headers != null) {
mDownloadInfo.getHeaders().putAll(headers);
}

for (String key : mDownloadInfo.getHeaders().keySet()) {
connection.setRequestProperty(key, mDownloadInfo.getHeaders().get(key));
}
}else{
if(headers != null){
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public DownloaderImpl(DownloadRequest request, DownloadResponse response, Execut
}

private void init() {
mDownloadInfo = new DownloadInfo(mRequest.getTitle().toString(), mRequest.getUri(), mRequest.getFolder());
mDownloadInfo = new DownloadInfo(mRequest.getTitle().toString(), mRequest.getUri(), mRequest.getFolder(), mRequest.getHeaders());
mDownloadTasks = new LinkedList<>();
}

Expand Down Expand Up @@ -182,7 +182,7 @@ public void onDownloadFailed(DownloadException de) {
}

private void connect() {
mConnectTask = new ConnectTaskImpl(mRequest.getUri(), this);
mConnectTask = new ConnectTaskImpl(mRequest.getUri(), mRequest.getHeaders(), this);
mExecutor.execute(mConnectTask);
}

Expand Down