this will be the the first method called by 16 | * {@link ConnectTaskImpl}. 17 | */ 18 | void onConnecting(); 19 | 20 | /** 21 | *
if {@link ConnectTaskImpl} is successfully 22 | * connected with the http/https server this method will be invoke. If not method 23 | * {@link #onFailed(DownloadException)} will be invoke. 24 | * 25 | * @param total The length of the file. See {@link HttpURLConnection#getContentLength()} 26 | * @param isRangeSupport indicate whether download can be resumed from pause. 27 | * See {@link ConnectTaskImpl#run()}. If the value of http header field 28 | * {@code Accept-Ranges} is {@code bytes} the value of isRangeSupport is 29 | * {@code true} else {@code false} 30 | */ 31 | void onConnected(long total, boolean isRangeSupport); 32 | 33 | /** 34 | *
progress callback. 35 | * 36 | * @param finished the downloaded length of the file 37 | * @param total the total length of the file same value with method {@link } 38 | * @param progress the percent of progress (finished/total)*100 39 | */ 40 | void onProgress(long finished, long total, int progress); 41 | 42 | /** 43 | *
download complete 44 | */ 45 | void onCompleted(); 46 | 47 | /** 48 | *
if you invoke {@link DownloadManager#pause(String)} or {@link DownloadManager#pauseAll()} 49 | * this method will be invoke if the downloading task is successfully paused. 50 | */ 51 | void onDownloadPaused(); 52 | 53 | /** 54 | *
if you invoke {@link DownloadManager#cancel(String)} or {@link DownloadManager#cancelAll()} 55 | * this method will be invoke if the downloading task is successfully canceled. 56 | */ 57 | void onDownloadCanceled(); 58 | 59 | /** 60 | *
download fail or exception callback
61 | *
62 | * @param e download exception
63 | */
64 | void onFailed(DownloadException e);
65 | }
66 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/Constants.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload;
2 |
3 | /**
4 | * Created by Aspsine on 2015/7/14.
5 | */
6 | public class Constants {
7 |
8 | public static final class CONFIG {
9 | public static final boolean DEBUG = false;
10 | }
11 |
12 | public static final class HTTP {
13 | public static final int CONNECT_TIME_OUT = 10 * 1000;
14 | public static final int READ_TIME_OUT = 10*1000;
15 | public static final String GET = "GET";
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/DownloadConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload;
2 |
3 | /**
4 | * Created by Aspsine on 2015/7/14.
5 | */
6 | public class DownloadConfiguration {
7 |
8 | public static final int DEFAULT_MAX_THREAD_NUMBER = 10;
9 |
10 | public static final int DEFAULT_THREAD_NUMBER = 1;
11 |
12 | /**
13 | * thread number in the pool
14 | */
15 | private int maxThreadNum;
16 |
17 | /**
18 | * thread number for each download
19 | */
20 | private int threadNum;
21 |
22 |
23 | /**
24 | * init with default value
25 | */
26 | public DownloadConfiguration() {
27 | maxThreadNum = DEFAULT_MAX_THREAD_NUMBER;
28 | threadNum = DEFAULT_THREAD_NUMBER;
29 | }
30 |
31 | public int getMaxThreadNum() {
32 | return maxThreadNum;
33 | }
34 |
35 | public void setMaxThreadNum(int maxThreadNum) {
36 | this.maxThreadNum = maxThreadNum;
37 | }
38 |
39 | public int getThreadNum() {
40 | return threadNum;
41 | }
42 |
43 | public void setThreadNum(int threadNum) {
44 | this.threadNum = threadNum;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/DownloadException.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload;
2 |
3 | /**
4 | * Created by Aspsine on 2015/7/15.
5 | */
6 | public class DownloadException extends Exception {
7 | private String errorMessage;
8 | private int errorCode;
9 |
10 |
11 | public DownloadException() {
12 | }
13 |
14 | public DownloadException(String detailMessage) {
15 | super(detailMessage);
16 | errorMessage = detailMessage;
17 | }
18 |
19 | public DownloadException(int errorCode, String detailMessage) {
20 | this(detailMessage);
21 | this.errorCode = errorCode;
22 | }
23 |
24 | public DownloadException(String detailMessage, Throwable throwable) {
25 | super(detailMessage, throwable);
26 | errorMessage = detailMessage;
27 | }
28 |
29 | public DownloadException(int errorCode, String detailMessage, Throwable throwable) {
30 | this(detailMessage, throwable);
31 | this.errorCode = errorCode;
32 | }
33 |
34 | public DownloadException(Throwable throwable) {
35 | super(throwable);
36 | }
37 |
38 | public String getErrorMessage() {
39 | return errorMessage;
40 | }
41 |
42 | public void setErrorMessage(String errorMessage) {
43 | this.errorMessage = errorMessage;
44 | }
45 |
46 | public int getErrorCode() {
47 | return errorCode;
48 | }
49 |
50 | public void setErrorCode(int errorCode) {
51 | this.errorCode = errorCode;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/DownloadInfo.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload;
2 |
3 | import java.io.File;
4 | import java.io.Serializable;
5 |
6 | /**
7 | * Created by aspsine on 15-4-19.
8 | */
9 | public class DownloadInfo {
10 | private String name;
11 | private String uri;
12 | private File dir;
13 | private int progress;
14 | private long length;
15 | private long finished;
16 | private boolean acceptRanges;
17 |
18 | private int status;
19 |
20 | public DownloadInfo() {
21 | }
22 |
23 | public DownloadInfo(String name, String uri, File dir) {
24 | this.name = name;
25 | this.uri = uri;
26 | this.dir = dir;
27 | }
28 |
29 | public String getName() {
30 | return name;
31 | }
32 |
33 | public void setName(String name) {
34 | this.name = name;
35 | }
36 |
37 | public String getUri() {
38 | return uri;
39 | }
40 |
41 | public void setUri(String uri) {
42 | this.uri = uri;
43 | }
44 |
45 | public File getDir() {
46 | return dir;
47 | }
48 |
49 | public void setDir(File dir) {
50 | this.dir = dir;
51 | }
52 |
53 | public int getProgress() {
54 | return progress;
55 | }
56 |
57 | public void setProgress(int progress) {
58 | this.progress = progress;
59 | }
60 |
61 | public long getLength() {
62 | return length;
63 | }
64 |
65 | public void setLength(long length) {
66 | this.length = length;
67 | }
68 |
69 | public long getFinished() {
70 | return finished;
71 | }
72 |
73 | public void setFinished(long finished) {
74 | this.finished = finished;
75 | }
76 |
77 | public boolean isAcceptRanges() {
78 | return acceptRanges;
79 | }
80 |
81 | public void setAcceptRanges(boolean acceptRanges) {
82 | this.acceptRanges = acceptRanges;
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/DownloadRequest.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload;
2 |
3 |
4 | import java.io.File;
5 |
6 | /**
7 | * Created by Aspsine on 2015/4/20.
8 | */
9 | public class DownloadRequest {
10 | private String mUri;
11 |
12 | private File mFolder;
13 |
14 | private CharSequence mTitle;
15 |
16 | private CharSequence mDescription;
17 |
18 | private boolean mScannable;
19 |
20 | private DownloadRequest() {
21 | }
22 |
23 | private DownloadRequest(String uri, File folder, CharSequence title, CharSequence description, boolean scannable) {
24 | this.mUri = uri;
25 | this.mFolder = folder;
26 | this.mTitle = title;
27 | this.mDescription = description;
28 | this.mScannable = scannable;
29 | }
30 |
31 | public String getUri() {
32 | return mUri;
33 | }
34 |
35 | public File getFolder() {
36 | return mFolder;
37 | }
38 |
39 | public CharSequence getTitle() {
40 | return mTitle;
41 | }
42 |
43 | public CharSequence getDescription() {
44 | return mDescription;
45 | }
46 |
47 | public boolean isScannable() {
48 | return mScannable;
49 | }
50 |
51 | public static class Builder {
52 |
53 | private String mUri;
54 |
55 | private File mFolder;
56 |
57 | private CharSequence mTitle;
58 |
59 | private CharSequence mDescription;
60 |
61 | private boolean mScannable;
62 |
63 | public Builder() {
64 | }
65 |
66 | public Builder setUri(String uri) {
67 | this.mUri = uri;
68 | return this;
69 | }
70 |
71 | public Builder setFolder(File folder) {
72 | this.mFolder = folder;
73 | return this;
74 | }
75 |
76 | public Builder setTitle(CharSequence title) {
77 | this.mTitle = title;
78 | return this;
79 | }
80 |
81 | public Builder setDescription(CharSequence description) {
82 | this.mDescription = description;
83 | return this;
84 | }
85 |
86 | public Builder setScannable(boolean scannable) {
87 | this.mScannable = scannable;
88 | return this;
89 | }
90 |
91 | public DownloadRequest build() {
92 | DownloadRequest request = new DownloadRequest(mUri, mFolder, mTitle, mDescription, mScannable);
93 | return request;
94 | }
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/architecture/ConnectTask.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload.architecture;
2 |
3 | import com.aspsine.multithreaddownload.DownloadException;
4 | import com.aspsine.multithreaddownload.DownloadInfo;
5 |
6 | /**
7 | * Created by Aspsine on 2015/10/29.
8 | */
9 | public interface ConnectTask extends Runnable {
10 |
11 | public interface OnConnectListener {
12 | void onConnecting();
13 |
14 | void onConnected(long time, long length, boolean isAcceptRanges);
15 |
16 | void onConnectCanceled();
17 |
18 | void onConnectFailed(DownloadException de);
19 | }
20 |
21 | void cancel();
22 |
23 | boolean isConnecting();
24 |
25 | boolean isConnected();
26 |
27 | boolean isCanceled();
28 |
29 | boolean isFailed();
30 |
31 | @Override
32 | void run();
33 | }
34 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/architecture/DownloadResponse.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload.architecture;
2 |
3 | import com.aspsine.multithreaddownload.DownloadException;
4 |
5 | /**
6 | * Created by Aspsine on 2015/10/28.
7 | */
8 | public interface DownloadResponse {
9 |
10 | void onStarted();
11 |
12 | void onConnecting();
13 |
14 | void onConnected(long time, long length, boolean acceptRanges);
15 |
16 | void onConnectFailed(DownloadException e);
17 |
18 | void onConnectCanceled();
19 |
20 | void onDownloadProgress(long finished, long length, int percent);
21 |
22 | void onDownloadCompleted();
23 |
24 | void onDownloadPaused();
25 |
26 | void onDownloadCanceled();
27 |
28 | void onDownloadFailed(DownloadException e);
29 | }
30 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/architecture/DownloadStatus.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload.architecture;
2 |
3 | import com.aspsine.multithreaddownload.CallBack;
4 | import com.aspsine.multithreaddownload.DownloadException;
5 |
6 | /**
7 | * Created by Aspsine on 2015/7/15.
8 | */
9 | public class DownloadStatus {
10 | public static final int STATUS_STARTED = 101;
11 | public static final int STATUS_CONNECTING = 102;
12 | public static final int STATUS_CONNECTED = 103;
13 | public static final int STATUS_PROGRESS = 104;
14 | public static final int STATUS_COMPLETED = 105;
15 | public static final int STATUS_PAUSED = 106;
16 | public static final int STATUS_CANCELED = 107;
17 | public static final int STATUS_FAILED = 108;
18 |
19 | private int status;
20 | private long time;
21 | private long length;
22 | private long finished;
23 | private int percent;
24 | private boolean acceptRanges;
25 | private DownloadException exception;
26 |
27 | private CallBack callBack;
28 |
29 | public int getStatus() {
30 | return status;
31 | }
32 |
33 | public void setStatus(int status) {
34 | this.status = status;
35 | }
36 |
37 | public long getTime() {
38 | return time;
39 | }
40 |
41 | public void setTime(long time) {
42 | this.time = time;
43 | }
44 |
45 | public long getLength() {
46 | return length;
47 | }
48 |
49 | public void setLength(long length) {
50 | this.length = length;
51 | }
52 |
53 | public long getFinished() {
54 | return finished;
55 | }
56 |
57 | public void setFinished(long finished) {
58 | this.finished = finished;
59 | }
60 |
61 | public int getPercent() {
62 | return percent;
63 | }
64 |
65 | public void setPercent(int percent) {
66 | this.percent = percent;
67 | }
68 |
69 | public boolean isAcceptRanges() {
70 | return acceptRanges;
71 | }
72 |
73 | public void setAcceptRanges(boolean acceptRanges) {
74 | this.acceptRanges = acceptRanges;
75 | }
76 |
77 | public Exception getException() {
78 | return exception;
79 | }
80 |
81 | public void setException(DownloadException exception) {
82 | this.exception = exception;
83 | }
84 |
85 | public CallBack getCallBack() {
86 | return callBack;
87 | }
88 |
89 | public void setCallBack(CallBack callBack) {
90 | this.callBack = callBack;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/architecture/DownloadStatusDelivery.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload.architecture;
2 |
3 | /**
4 | * Created by Aspsine on 2015/7/15.
5 | */
6 | public interface DownloadStatusDelivery {
7 |
8 | void post(DownloadStatus status);
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/architecture/DownloadTask.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload.architecture;
2 |
3 | import com.aspsine.multithreaddownload.DownloadException;
4 |
5 | /**
6 | * Created by Aspsine on 2015/7/22.
7 | */
8 | public interface DownloadTask extends Runnable {
9 |
10 | interface OnDownloadListener {
11 | void onDownloadConnecting();
12 |
13 | void onDownloadProgress(long finished, long length);
14 |
15 | void onDownloadCompleted();
16 |
17 | void onDownloadPaused();
18 |
19 | void onDownloadCanceled();
20 |
21 | void onDownloadFailed(DownloadException de);
22 | }
23 |
24 | void cancel();
25 |
26 | void pause();
27 |
28 | boolean isDownloading();
29 |
30 | boolean isComplete();
31 |
32 | boolean isPaused();
33 |
34 | boolean isCanceled();
35 |
36 | boolean isFailed();
37 |
38 | @Override
39 | void run();
40 | }
41 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/architecture/Downloader.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload.architecture;
2 |
3 | /**
4 | * Created by Aspsine on 2015/10/29.
5 | */
6 | public interface Downloader {
7 |
8 | public interface OnDownloaderDestroyedListener {
9 | void onDestroyed(String key, Downloader downloader);
10 | }
11 |
12 | boolean isRunning();
13 |
14 | void start();
15 |
16 | void pause();
17 |
18 | void cancel();
19 |
20 | void onDestroy();
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/core/DownloadStatusDeliveryImpl.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload.core;
2 |
3 | import android.os.Handler;
4 |
5 | import com.aspsine.multithreaddownload.CallBack;
6 | import com.aspsine.multithreaddownload.DownloadException;
7 | import com.aspsine.multithreaddownload.architecture.DownloadStatus;
8 | import com.aspsine.multithreaddownload.architecture.DownloadStatusDelivery;
9 |
10 | import java.util.concurrent.Executor;
11 |
12 | /**
13 | * Created by Aspsine on 2015/7/15.
14 | */
15 | public class DownloadStatusDeliveryImpl implements DownloadStatusDelivery {
16 | private Executor mDownloadStatusPoster;
17 |
18 | public DownloadStatusDeliveryImpl(final Handler handler) {
19 | mDownloadStatusPoster = new Executor() {
20 | @Override
21 | public void execute(Runnable command) {
22 | handler.post(command);
23 | }
24 | };
25 | }
26 |
27 | @Override
28 | public void post(DownloadStatus status) {
29 | mDownloadStatusPoster.execute(new DownloadStatusDeliveryRunnable(status));
30 | }
31 |
32 | private static class DownloadStatusDeliveryRunnable implements Runnable {
33 | private final DownloadStatus mDownloadStatus;
34 | private final CallBack mCallBack;
35 |
36 | public DownloadStatusDeliveryRunnable(DownloadStatus downloadStatus) {
37 | this.mDownloadStatus = downloadStatus;
38 | this.mCallBack = mDownloadStatus.getCallBack();
39 | }
40 |
41 | @Override
42 | public void run() {
43 | switch (mDownloadStatus.getStatus()) {
44 | case DownloadStatus.STATUS_CONNECTING:
45 | mCallBack.onConnecting();
46 | break;
47 | case DownloadStatus.STATUS_CONNECTED:
48 | mCallBack.onConnected(mDownloadStatus.getLength(), mDownloadStatus.isAcceptRanges());
49 | break;
50 | case DownloadStatus.STATUS_PROGRESS:
51 | mCallBack.onProgress(mDownloadStatus.getFinished(), mDownloadStatus.getLength(), mDownloadStatus.getPercent());
52 | break;
53 | case DownloadStatus.STATUS_COMPLETED:
54 | mCallBack.onCompleted();
55 | break;
56 | case DownloadStatus.STATUS_PAUSED:
57 | mCallBack.onDownloadPaused();
58 | break;
59 | case DownloadStatus.STATUS_CANCELED:
60 | mCallBack.onDownloadCanceled();
61 | break;
62 | case DownloadStatus.STATUS_FAILED:
63 | mCallBack.onFailed((DownloadException) mDownloadStatus.getException());
64 | break;
65 | }
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/library/src/main/java/com/aspsine/multithreaddownload/core/MultiDownloadTask.java:
--------------------------------------------------------------------------------
1 | package com.aspsine.multithreaddownload.core;
2 |
3 | /**
4 | * Created by Aspsine on 2015/7/20.
5 | */
6 |
7 | import com.aspsine.multithreaddownload.DownloadInfo;
8 | import com.aspsine.multithreaddownload.db.DataBaseManager;
9 | import com.aspsine.multithreaddownload.db.ThreadInfo;
10 |
11 | import java.io.File;
12 | import java.io.IOException;
13 | import java.io.RandomAccessFile;
14 | import java.net.HttpURLConnection;
15 | import java.util.HashMap;
16 | import java.util.Map;
17 |
18 | /**
19 | * download thread
20 | */
21 | public class MultiDownloadTask extends DownloadTaskImpl {
22 |
23 | private DataBaseManager mDBManager;
24 |
25 | public MultiDownloadTask(DownloadInfo downloadInfo, ThreadInfo threadInfo, DataBaseManager dbManager, OnDownloadListener listener) {
26 |
27 | super(downloadInfo, threadInfo, listener);
28 | this.mDBManager = dbManager;
29 | }
30 |
31 |
32 | @Override
33 | protected void insertIntoDB(ThreadInfo info) {
34 | if (!mDBManager.exists(info.getTag(), info.getId())) {
35 | mDBManager.insert(info);
36 | }
37 | }
38 |
39 | @Override
40 | protected int getResponseCode() {
41 | return HttpURLConnection.HTTP_PARTIAL;
42 | }
43 |
44 | @Override
45 | protected void updateDB(ThreadInfo info) {
46 | mDBManager.update(info.getTag(), info.getId(), info.getFinished());
47 | }
48 |
49 | @Override
50 | protected Map