├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.xml
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── drawable-hdpi
│ │ │ └── ic_launcher.9.png
│ │ ├── drawable-mdpi
│ │ │ └── ic_launcher.9.png
│ │ ├── drawable-xhdpi
│ │ │ └── ic_launcher.9.png
│ │ ├── drawable-xxhdpi
│ │ │ └── ic_launcher.9.png
│ │ ├── drawable-xxxhdpi
│ │ │ └── ic_launcher.9.png
│ │ └── layout
│ │ │ ├── activity_main.xml
│ │ │ ├── include_down1.xml
│ │ │ └── include_down2.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── less
│ │ └── downloadmanager
│ │ └── MainActivity.java
├── build.gradle
└── proguard-rules.pro
├── lib_java
├── .gitignore
├── libs
│ └── sqlite-jdbc-3.8.11.1.jar
├── build.gradle
└── src
│ └── main
│ └── java
│ └── com
│ └── less
│ └── downloadmanager
│ ├── lib
│ ├── Downloader.java
│ ├── util
│ │ ├── TextUtils.java
│ │ ├── L.java
│ │ ├── Platform.java
│ │ ├── IOUtils.java
│ │ └── FileUtils.java
│ ├── db
│ │ ├── BaseDaoImpl.java
│ │ ├── BaseDao.java
│ │ ├── DatabaseManager.java
│ │ ├── ThreadInfoDao.java
│ │ └── SqlHelper.java
│ ├── Config.java
│ ├── Constants.java
│ ├── request
│ │ ├── PostBuilder.java
│ │ ├── RequestCall.java
│ │ ├── GetBuilder.java
│ │ ├── GetRequest.java
│ │ ├── RequestBuilder.java
│ │ ├── FileCallBack.java
│ │ ├── StringCallback.java
│ │ └── Callback.java
│ ├── task
│ │ ├── ConnectTask.java
│ │ ├── DownloadTask.java
│ │ ├── SingleDownloadTask.java
│ │ ├── MultiDownloadTask.java
│ │ ├── ConnectTaskImpl.java
│ │ └── DownloadTaskImpl.java
│ ├── listenter
│ │ ├── OnConnectListener.java
│ │ └── OnDownloadListener.java
│ ├── DownloadException.java
│ ├── bean
│ │ ├── DownloadInfo.java
│ │ ├── ThreadInfo.java
│ │ └── DownloadStatus.java
│ ├── DownloadManager.java
│ └── DownloaderImpl.java
│ └── App.java
├── lib_android
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── less
│ │ │ │ └── downloadmanager
│ │ │ │ └── lib
│ │ │ │ ├── Downloader.java
│ │ │ │ ├── request
│ │ │ │ ├── PostBuilder.java
│ │ │ │ ├── RequestCall.java
│ │ │ │ ├── GetBuilder.java
│ │ │ │ ├── GetRequest.java
│ │ │ │ ├── FileCallBack.java
│ │ │ │ ├── RequestBuilder.java
│ │ │ │ ├── StringCallback.java
│ │ │ │ └── Callback.java
│ │ │ │ ├── task
│ │ │ │ ├── ConnectTask.java
│ │ │ │ ├── DownloadTask.java
│ │ │ │ ├── SingleDownloadTask.java
│ │ │ │ ├── MultiDownloadTask.java
│ │ │ │ ├── ConnectTaskImpl.java
│ │ │ │ └── DownloadTaskImpl.java
│ │ │ │ ├── db
│ │ │ │ ├── BaseDao.java
│ │ │ │ ├── BaseDaoImpl.java
│ │ │ │ ├── SqlHelper.java
│ │ │ │ ├── DatabaseManager.java
│ │ │ │ └── ThreadInfoDao.java
│ │ │ │ ├── listenter
│ │ │ │ ├── OnConnectListener.java
│ │ │ │ └── OnDownloadListener.java
│ │ │ │ ├── Constants.java
│ │ │ │ ├── util
│ │ │ │ ├── IOUtils.java
│ │ │ │ ├── L.java
│ │ │ │ ├── Platform.java
│ │ │ │ └── FileUtils.java
│ │ │ │ ├── DownloadException.java
│ │ │ │ ├── bean
│ │ │ │ ├── DownloadInfo.java
│ │ │ │ ├── ThreadInfo.java
│ │ │ │ └── DownloadStatus.java
│ │ │ │ ├── DownloadManager.java
│ │ │ │ └── DownloaderImpl.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── less
│ │ │ └── lib_android
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── less
│ │ └── lib_android
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── download.db
├── screenshots
└── show.png
├── libs
├── downloader-java-1.1.jar
└── downloader-android-1.1.jar
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lib_java/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lib_android/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':lib_android', ':lib_java'
2 |
--------------------------------------------------------------------------------
/download.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/download.db
--------------------------------------------------------------------------------
/screenshots/show.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/screenshots/show.png
--------------------------------------------------------------------------------
/libs/downloader-java-1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/libs/downloader-java-1.1.jar
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DownloadManager
3 |
4 |
--------------------------------------------------------------------------------
/libs/downloader-android-1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/libs/downloader-android-1.1.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/lib_android/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | lib_android
3 |
4 |
--------------------------------------------------------------------------------
/lib_java/libs/sqlite-jdbc-3.8.11.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/lib_java/libs/sqlite-jdbc-3.8.11.1.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/ic_launcher.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/drawable-hdpi/ic_launcher.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/ic_launcher.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/drawable-mdpi/ic_launcher.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/ic_launcher.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/drawable-xhdpi/ic_launcher.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_launcher.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/drawable-xxhdpi/ic_launcher.9.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxxhdpi/ic_launcher.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/drawable-xxxhdpi/ic_launcher.9.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/coding-dream/DownloadManager/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 | .idea
11 |
--------------------------------------------------------------------------------
/lib_java/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | dependencies {
4 | compile fileTree(dir: 'libs', include: ['*.jar'])
5 | }
6 |
7 | sourceCompatibility = "1.7"
8 | targetCompatibility = "1.7"
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Sep 13 16:26:50 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/Downloader.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib;
2 |
3 | /** 下载执行类 */
4 | public interface Downloader {
5 | boolean isRunning();
6 |
7 | void start();
8 |
9 | void pause();
10 |
11 | void cancel();
12 | }
13 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/Downloader.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib;
2 |
3 | /** 下载执行类 */
4 | public interface Downloader {
5 | boolean isRunning();
6 |
7 | void start();
8 |
9 | void pause();
10 |
11 | void cancel();
12 | }
13 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/util/TextUtils.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | public class TextUtils {
4 |
5 | public static boolean isEmpty(String text){
6 | if(text == null || text.length() == 0){
7 | return true;
8 | }
9 | return false;
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/lib_android/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/db/BaseDaoImpl.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.db;
2 |
3 | /**
4 | * Created by Administrator on 2017/9/16.
5 | */
6 |
7 | public abstract class BaseDaoImpl implements BaseDao{
8 | protected String TAG = this.getClass().getSimpleName();// 获取真实子类的TAG名称
9 | }
10 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/Config.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib;
2 |
3 | /**
4 | * Created by Administrator on 2017/9/16.
5 | */
6 |
7 | public class Config {
8 | public static final int DEFAULT_MAX_THREAD_NUMBER = 10;// 线程池的最大线程数量
9 |
10 | public static final int DEFAULT_THREAD_NUMBER = 3;// 每个DownLoader下载器的 线程数量
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/util/L.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | import com.less.downloadmanager.lib.Constants;
4 |
5 | public class L {
6 | public static void d(String msg) {
7 | if(Constants.CONFIG.DEBUG){
8 | System.out.println(msg);
9 | }
10 | }
11 |
12 | public static void e(String msg) {
13 | if (Constants.CONFIG.DEBUG) {
14 | System.err.println(msg);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/Constants.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib;
2 |
3 | public class Constants {
4 |
5 | public static final class CONFIG {
6 | public static final boolean DEBUG = true;
7 | }
8 |
9 | public static final class HTTP {
10 | public static final int CONNECT_TIME_OUT = 10 * 1000;
11 | public static final int READ_TIME_OUT = 10 * 1000;
12 | public static final String GET = "GET";
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/db/BaseDao.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.db;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/16.
7 | */
8 |
9 | public interface BaseDao {
10 | void add(T t);
11 | void delete(String tag);
12 | void update(String tag, int threadId, long finished);
13 | List find(String tag);
14 | List findAll();
15 | boolean exists(String tag, int threadId);
16 | }
17 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/request/PostBuilder.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | /**
4 | * Created by Administrator on 2017/9/16.
5 | */
6 |
7 | public class PostBuilder extends RequestBuilder{
8 |
9 | @Override
10 | protected RequestBuilder addParams(String key, String val) {
11 | return null;
12 | }
13 |
14 | @Override
15 | public RequestCall build() {
16 | return null;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/request/PostBuilder.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | /**
4 | * Created by Administrator on 2017/9/16.
5 | */
6 |
7 | public class PostBuilder extends RequestBuilder{
8 |
9 | @Override
10 | protected RequestBuilder addParams(String key, String val) {
11 | return null;
12 | }
13 |
14 | @Override
15 | public RequestCall build() {
16 | return null;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/task/ConnectTask.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.task;
2 |
3 | /**
4 | * Created by Limitless on 2017/9/22.
5 | * 首次连接获取下载文件length的Task
6 | */
7 |
8 | public interface ConnectTask extends Runnable{
9 | void pause();
10 |
11 | void cancel();
12 |
13 | boolean isConnecting();
14 |
15 | boolean isConnected();
16 |
17 | boolean isPaused();
18 |
19 | boolean isCanceled();
20 |
21 | boolean isFailed();
22 | }
23 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/task/DownloadTask.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.task;
2 |
3 | /**
4 | * Created by Limitless on 2017/9/22.
5 | * 连接Task完成之后-> 下载Task
6 | */
7 |
8 | public interface DownloadTask extends Runnable{
9 | void pause();
10 |
11 | void cancel();
12 |
13 | boolean isDownloading();
14 |
15 | boolean isComplete();
16 |
17 | boolean isPaused();
18 |
19 | boolean isCanceled();
20 |
21 | boolean isFailed();
22 | }
23 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/task/ConnectTask.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.task;
2 |
3 | /**
4 | * Created by Limitless on 2017/9/22.
5 | * 首次连接获取下载文件length的Task
6 | */
7 |
8 | public interface ConnectTask extends Runnable{
9 | void pause();
10 |
11 | void cancel();
12 |
13 | boolean isConnecting();
14 |
15 | boolean isConnected();
16 |
17 | boolean isPaused();
18 |
19 | boolean isCanceled();
20 |
21 | boolean isFailed();
22 | }
23 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/task/DownloadTask.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.task;
2 |
3 | /**
4 | * Created by Limitless on 2017/9/22.
5 | * 连接Task完成之后-> 下载Task
6 | */
7 |
8 | public interface DownloadTask extends Runnable{
9 | void pause();
10 |
11 | void cancel();
12 |
13 | boolean isDownloading();
14 |
15 | boolean isComplete();
16 |
17 | boolean isPaused();
18 |
19 | boolean isCanceled();
20 |
21 | boolean isFailed();
22 | }
23 |
--------------------------------------------------------------------------------
/lib_android/src/test/java/com/less/lib_android/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.less.lib_android;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/db/BaseDao.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.db;
2 |
3 | import android.database.sqlite.SQLiteDatabase;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * Created by Administrator on 2017/9/16.
9 | */
10 |
11 | public interface BaseDao {
12 | void add(T t);
13 | void delete(String tag);
14 | void update(String tag, int threadId, long finished);
15 | List find(String tag);
16 | List findAll();
17 | boolean exists(String tag, int threadId);
18 | }
19 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/listenter/OnConnectListener.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.listenter;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/22.
7 | */
8 |
9 | public interface OnConnectListener {
10 | void onConnecting();
11 |
12 | void onConnected(long length, boolean isAcceptRanges);
13 |
14 | void onConnectPaused();
15 |
16 | void onConnectCanceled();
17 |
18 | void onConnectFailed(DownloadException de);
19 | }
20 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/listenter/OnConnectListener.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.listenter;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/22.
7 | */
8 |
9 | public interface OnConnectListener {
10 | void onConnecting();
11 |
12 | void onConnected(long length, boolean isAcceptRanges);
13 |
14 | void onConnectPaused();
15 |
16 | void onConnectCanceled();
17 |
18 | void onConnectFailed(DownloadException de);
19 | }
20 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/listenter/OnDownloadListener.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.listenter;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/22.
7 | */
8 |
9 | public interface OnDownloadListener {
10 | void onDownloadConnecting();
11 |
12 | void onDownloadProgress(long finished, long length);
13 |
14 | void onDownloadCompleted();
15 |
16 | void onDownloadPaused();
17 |
18 | void onDownloadCanceled();
19 |
20 | void onDownloadFailed(DownloadException de);
21 | }
22 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/listenter/OnDownloadListener.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.listenter;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/22.
7 | */
8 |
9 | public interface OnDownloadListener {
10 | void onDownloadConnecting();
11 |
12 | void onDownloadProgress(long finished, long length);
13 |
14 | void onDownloadCompleted();
15 |
16 | void onDownloadPaused();
17 |
18 | void onDownloadCanceled();
19 |
20 | void onDownloadFailed(DownloadException de);
21 | }
22 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/request/RequestCall.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import com.less.downloadmanager.lib.DownloadManager;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/18.
7 | */
8 |
9 | public class RequestCall {
10 | public GetRequest getRequest;
11 |
12 | public RequestCall(GetRequest getRequest) {
13 | this.getRequest = getRequest;
14 | }
15 |
16 | public void execute(Callback callback) {
17 | DownloadManager.getInstance().start(this,callback);// 仍然交给DownLoadManager调用,有点EventBus的味道
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/Constants.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib;
2 |
3 | public class Constants {
4 |
5 | public static final class CONFIG {
6 | public static final boolean DEBUG = true;
7 | public static final int DEFAULT_MAX_THREAD_NUMBER = 10;// 线程池的最大线程数量
8 | public static final int DEFAULT_THREAD_NUMBER = 3;// 每个DownLoader下载器的 线程数量
9 | }
10 |
11 | public static final class HTTP {
12 | public static final int CONNECT_TIME_OUT = 10 * 1000;
13 | public static final int READ_TIME_OUT = 10 * 1000;
14 | public static final String GET = "GET";
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/request/RequestCall.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import android.content.Context;
4 |
5 | import com.less.downloadmanager.lib.DownloadManager;
6 |
7 | /**
8 | * Created by Administrator on 2017/9/18.
9 | */
10 |
11 | public class RequestCall {
12 | public GetRequest getRequest;
13 |
14 | public RequestCall(GetRequest getRequest) {
15 | this.getRequest = getRequest;
16 | }
17 |
18 | public void execute(Context context,Callback callback) {
19 | DownloadManager.getInstance(context).start(this,callback);// 仍然交给DownLoadManager调用,有点EventBus的味道
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/request/GetBuilder.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import java.util.HashMap;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/16.
7 | */
8 |
9 | public class GetBuilder extends RequestBuilder{
10 |
11 | @Override
12 | protected RequestBuilder addParams(String key, String val) {
13 | if (mParams == null) {
14 | mParams = new HashMap();
15 | }
16 | mParams.put(key,val);
17 | return this;
18 | }
19 |
20 | @Override
21 | public RequestCall build() {
22 |
23 | return new GetRequest(mUri,mFolder,mName,mTag).build();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/request/GetBuilder.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import java.util.HashMap;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/16.
7 | */
8 |
9 | public class GetBuilder extends RequestBuilder{
10 |
11 | @Override
12 | protected RequestBuilder addParams(String key, String val) {
13 | if (mParams == null) {
14 | mParams = new HashMap();
15 | }
16 | mParams.put(key,val);
17 | return this;
18 | }
19 |
20 | @Override
21 | public RequestCall build() {
22 |
23 | return new GetRequest(mUri,mFolder,mName,mTag).build();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/request/GetRequest.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import java.io.File;
4 | import java.util.Map;
5 |
6 | /**
7 | * Created by Administrator on 2017/9/18.
8 | */
9 |
10 | public class GetRequest { // Builder模式分离为两个类
11 | public String mUri;
12 |
13 | public File mFolder;
14 |
15 | public String mName;
16 |
17 | public String mTag;
18 |
19 | protected Map mParams;
20 |
21 | public GetRequest(String uri, File folder, String name,String tag) {
22 | this.mUri = uri;
23 | this.mFolder = folder;
24 | this.mName = name;
25 | this.mTag = tag;
26 | }
27 |
28 | public RequestCall build() {
29 | return new RequestCall(this);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/request/GetRequest.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import java.io.File;
4 | import java.util.Map;
5 |
6 | /**
7 | * Created by Administrator on 2017/9/18.
8 | */
9 |
10 | public class GetRequest { // Builder模式分离为两个类
11 | public String mUri;
12 |
13 | public File mFolder;
14 |
15 | public String mName;
16 |
17 | public String mTag;
18 |
19 | protected Map mParams;
20 |
21 | public GetRequest(String uri, File folder, String name,String tag) {
22 | this.mUri = uri;
23 | this.mFolder = folder;
24 | this.mName = name;
25 | this.mTag = tag;
26 | }
27 |
28 | public RequestCall build() {
29 | return new RequestCall(this);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/util/Platform.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | import java.util.concurrent.Executor;
4 | import java.util.concurrent.Executors;
5 |
6 | public class Platform {
7 | private static final Platform PLATFORM = findPlatform();
8 | private static Executor executor;
9 | public static Platform get() {
10 | return PLATFORM;
11 | }
12 |
13 | private static Platform findPlatform() {
14 | return new Platform();// 创建Java平台的线程池
15 | }
16 |
17 | public Executor makeCallbackExecutor() {
18 | if (executor == null) {
19 | executor = Executors.newCachedThreadPool();
20 | }
21 | return executor;
22 | }
23 |
24 | /** execute 线程 */
25 | public void execute(Runnable runnable) {
26 | makeCallbackExecutor().execute(runnable);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/util/IOUtils.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | import java.io.Closeable;
4 | import java.io.Flushable;
5 | import java.io.IOException;
6 |
7 | public class IOUtils {
8 |
9 | public static final void closeQuietly(Closeable closeable) {
10 | if (closeable != null) {
11 | synchronized (IOUtils.class) {
12 | try {
13 | closeable.close();
14 | } catch (IOException e) {
15 | e.printStackTrace();
16 | }
17 | }
18 | }
19 | }
20 |
21 | public static void flushQuietly(Flushable flushable) {
22 | if (flushable == null) return;
23 | try {
24 | flushable.flush();
25 | } catch (Exception e) {
26 | e.printStackTrace();
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/util/IOUtils.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | import java.io.Closeable;
4 | import java.io.Flushable;
5 | import java.io.IOException;
6 |
7 | public class IOUtils {
8 |
9 | public static final void closeQuietly(Closeable closeable) {
10 | if (closeable != null) {
11 | synchronized (IOUtils.class) {
12 | try {
13 | closeable.close();
14 | } catch (IOException e) {
15 | e.printStackTrace();
16 | }
17 | }
18 | }
19 | }
20 |
21 | public static void flushQuietly(Flushable flushable) {
22 | if (flushable == null) return;
23 | try {
24 | flushable.flush();
25 | } catch (Exception e) {
26 | e.printStackTrace();
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/db/BaseDaoImpl.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.db;
2 |
3 | import android.content.Context;
4 | import android.database.sqlite.SQLiteDatabase;
5 |
6 | /**
7 | * Created by Administrator on 2017/9/16.
8 | */
9 |
10 | public abstract class BaseDaoImpl implements BaseDao{
11 | protected String TAG = this.getClass().getSimpleName();// 获取真实子类的TAG名称
12 | public BaseDaoImpl(Context context){
13 | mHelper = new SqlHelper(context);
14 | }
15 | private SqlHelper mHelper;
16 |
17 | protected SQLiteDatabase getWritableDatabase() {
18 | return mHelper.getWritableDatabase();
19 | }
20 |
21 | protected SQLiteDatabase getReadableDatabase() {
22 | return mHelper.getReadableDatabase();
23 | }
24 |
25 | public void closeDB() {
26 | mHelper.close();
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/db/SqlHelper.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.db;
2 |
3 | import android.content.Context;
4 | import android.database.sqlite.SQLiteDatabase;
5 | import android.database.sqlite.SQLiteOpenHelper;
6 |
7 | /**
8 | * Created by Administrator on 2017/9/16.
9 | */
10 |
11 | public class SqlHelper extends SQLiteOpenHelper {
12 | private static final String DB_NAME = "download.db";
13 | private static final int DB_VERSION = 1;
14 | public SqlHelper(Context context) {
15 | super(context, DB_NAME, null, DB_VERSION);
16 | }
17 |
18 | @Override
19 | public void onCreate(SQLiteDatabase db) {
20 | ThreadInfoDao.createTable(db);
21 | }
22 |
23 | @Override
24 | public void onUpgrade(SQLiteDatabase db, int i, int i1) {
25 | ThreadInfoDao.dropTable(db);
26 | ThreadInfoDao.createTable(db);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/lib_android/src/androidTest/java/com/less/lib_android/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.less.lib_android;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.less.lib_android.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion "26.0.0"
6 | defaultConfig {
7 | applicationId "com.less.downloadmanager"
8 | minSdkVersion 14
9 | targetSdkVersion 15
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:26.+'
28 | compile project(':lib_android')
29 | }
30 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in E:\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/lib_android/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in E:\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/request/FileCallBack.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 | import com.less.downloadmanager.lib.DownloadManager;
5 |
6 | import java.io.File;
7 |
8 | /**
9 | * Created by Administrator on 2017/9/18.
10 | */
11 |
12 | public abstract class FileCallBack extends Callback{
13 |
14 | @Override
15 | public abstract void onStart(String tag) ;
16 |
17 | @Override
18 | public abstract void onDownloadProgress(String tag,long finished, long totalLength, int percent);
19 |
20 | @Override
21 | public abstract void onDownloadPaused();
22 |
23 | @Override
24 | public abstract void onDownloadCanceled();
25 |
26 | @Override
27 | public abstract void onDownloadFailed(DownloadException e);
28 |
29 | @Override
30 | public File parseResponse(File file) {
31 | // 对原始文件进行操作(暂时不作修改)
32 | // ...
33 | return file;
34 | }
35 |
36 | @Override
37 | public abstract void onDownloadCompleted(File file);
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/request/RequestBuilder.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import java.io.File;
4 | import java.util.Map;
5 |
6 | /**
7 | * Created by Administrator on 2017/9/16.
8 | */
9 |
10 | public abstract class RequestBuilder {
11 | protected String mUri;
12 |
13 | protected File mFolder;
14 |
15 | protected String mName;
16 |
17 | protected String mTag;
18 |
19 | protected Map mParams;
20 |
21 | public T uri(String uri){
22 | this.mUri = uri;
23 | return (T) this;
24 | }
25 |
26 | public T folder(File folder){
27 | this.mFolder = folder;
28 | return (T) this;
29 | }
30 |
31 | public T name(String name) {
32 | this.mName = name;
33 | return (T) this;
34 | }
35 |
36 | public T tag(String tag) {
37 | this.mTag = tag;
38 | return (T) this;
39 | }
40 |
41 | protected abstract RequestBuilder addParams(String key, String val);
42 |
43 | public abstract RequestCall build();
44 | }
45 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/util/L.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | import android.util.Log;
4 |
5 | import com.less.downloadmanager.lib.Constants;
6 |
7 | public class L {
8 | private static final String TAG = "DownloadManager";
9 |
10 | public static void d(String msg) {
11 | d(TAG, msg);
12 | }
13 |
14 | public static void d(String tag, String msg) {
15 | if (Constants.CONFIG.DEBUG) {
16 | Log.d(tag, msg);
17 | }
18 | }
19 |
20 | public static void i(String msg) {
21 | i(TAG, msg);
22 | }
23 |
24 | public static void i(String tag, String msg) {
25 | if (Constants.CONFIG.DEBUG) {
26 | Log.i(tag, msg);
27 | }
28 | }
29 |
30 | public static void e(String tag, String msg) {
31 | if (Constants.CONFIG.DEBUG) {
32 | Log.e(tag, msg);
33 | }
34 | }
35 |
36 | public static void w(String msg) {
37 | w(TAG, msg);
38 | }
39 |
40 | public static void w(String tag, String msg) {
41 | if (Constants.CONFIG.DEBUG) {
42 | Log.w(tag, msg);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/request/RequestBuilder.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import android.content.Context;
4 |
5 | import java.io.File;
6 | import java.util.Map;
7 |
8 | /**
9 | * Created by Administrator on 2017/9/16.
10 | */
11 |
12 | public abstract class RequestBuilder {
13 | protected String mUri;
14 |
15 | protected File mFolder;
16 |
17 | protected String mName;
18 |
19 | protected String mTag;
20 |
21 | protected Map mParams;
22 |
23 | public T uri(String uri){
24 | this.mUri = uri;
25 | return (T) this;
26 | }
27 |
28 | public T folder(File folder){
29 | this.mFolder = folder;
30 | return (T) this;
31 | }
32 |
33 | public T name(String name) {
34 | this.mName = name;
35 | return (T) this;
36 | }
37 |
38 | public T tag(String tag) {
39 | this.mTag = tag;
40 | return (T) this;
41 | }
42 |
43 | protected abstract RequestBuilder addParams(String key, String val);
44 |
45 | public abstract RequestCall build();
46 | }
47 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/request/FileCallBack.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 | import com.less.downloadmanager.lib.DownloadManager;
5 |
6 | import java.io.File;
7 |
8 | /**
9 | * Created by Administrator on 2017/9/18.
10 | */
11 |
12 | public abstract class FileCallBack extends Callback{
13 |
14 | @Override
15 | public abstract void onStart(String tag) ;
16 |
17 | @Override
18 | public abstract void onDownloadProgress(String tag,long finished, long totalLength, int percent);
19 |
20 | @Override
21 | public void onDownloadPaused() {
22 | // nothing to do
23 | }
24 |
25 | @Override
26 | public void onDownloadCanceled() {
27 | // nothing to do
28 | }
29 |
30 | @Override
31 | public abstract void onDownloadFailed(DownloadException e);
32 |
33 | @Override
34 | public File parseResponse(File file) {
35 | // 对原始文件进行操作(暂时不作修改)
36 | // ...
37 | return file;
38 | }
39 |
40 | @Override
41 | public abstract void onDownloadCompleted(File file);
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/DownloadException.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib;
2 |
3 | public class DownloadException extends Exception {
4 | private String errorMessage;
5 | private int errorCode;
6 |
7 | public DownloadException() {
8 | }
9 |
10 | public DownloadException(String detailMessage) {
11 | super(detailMessage);
12 | this.errorMessage = detailMessage;
13 | }
14 |
15 | public DownloadException(int errorCode, String detailMessage) {
16 | super(detailMessage);
17 | this.errorCode = errorCode;
18 | this.errorMessage = detailMessage;
19 | }
20 |
21 | public DownloadException(int errorCode, Throwable throwable) {
22 | super(throwable);
23 | this.errorCode = errorCode;
24 | }
25 |
26 | public DownloadException(String detailMessage, Throwable throwable) {
27 | super(detailMessage, throwable);
28 | this.errorMessage = detailMessage;
29 | }
30 |
31 | public DownloadException(int errorCode, String detailMessage, Throwable throwable) {
32 | super(detailMessage, throwable);
33 | this.errorCode = errorCode;
34 | this.errorMessage = detailMessage;
35 | }
36 |
37 | public DownloadException(Throwable throwable) {
38 | super(throwable);
39 | }
40 |
41 | public String getErrorMessage() {
42 | return errorMessage;
43 | }
44 |
45 | public void setErrorMessage(String errorMessage) {
46 | this.errorMessage = errorMessage;
47 | }
48 |
49 | public int getErrorCode() {
50 | return errorCode;
51 | }
52 |
53 | public void setErrorCode(int errorCode) {
54 | this.errorCode = errorCode;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/DownloadException.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib;
2 |
3 | public class DownloadException extends Exception {
4 | private String errorMessage;
5 | private int errorCode;
6 |
7 | public DownloadException() {
8 | }
9 |
10 | public DownloadException(String detailMessage) {
11 | super(detailMessage);
12 | this.errorMessage = detailMessage;
13 | }
14 |
15 | public DownloadException(int errorCode, String detailMessage) {
16 | super(detailMessage);
17 | this.errorCode = errorCode;
18 | this.errorMessage = detailMessage;
19 | }
20 |
21 | public DownloadException(int errorCode, Throwable throwable) {
22 | super(throwable);
23 | this.errorCode = errorCode;
24 | }
25 |
26 | public DownloadException(String detailMessage, Throwable throwable) {
27 | super(detailMessage, throwable);
28 | this.errorMessage = detailMessage;
29 | }
30 |
31 | public DownloadException(int errorCode, String detailMessage, Throwable throwable) {
32 | super(detailMessage, throwable);
33 | this.errorCode = errorCode;
34 | this.errorMessage = detailMessage;
35 | }
36 |
37 | public DownloadException(Throwable throwable) {
38 | super(throwable);
39 | }
40 |
41 | public String getErrorMessage() {
42 | return errorMessage;
43 | }
44 |
45 | public void setErrorMessage(String errorMessage) {
46 | this.errorMessage = errorMessage;
47 | }
48 |
49 | public int getErrorCode() {
50 | return errorCode;
51 | }
52 |
53 | public void setErrorCode(int errorCode) {
54 | this.errorCode = errorCode;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/include_down1.xml:
--------------------------------------------------------------------------------
1 |
6 |
15 |
16 |
20 |
21 |
27 |
28 |
34 |
35 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/include_down2.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
16 |
17 |
21 |
22 |
28 |
29 |
35 |
36 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/task/SingleDownloadTask.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.task;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 | import java.io.RandomAccessFile;
6 | import java.net.HttpURLConnection;
7 | import java.util.Map;
8 |
9 | import com.less.downloadmanager.lib.bean.DownloadInfo;
10 | import com.less.downloadmanager.lib.bean.ThreadInfo;
11 | import com.less.downloadmanager.lib.listenter.OnDownloadListener;
12 | import com.less.downloadmanager.lib.util.L;
13 |
14 | /**
15 | * Created by Administrator on 2017/9/16.
16 | */
17 |
18 | public class SingleDownloadTask extends DownloadTaskImpl {
19 |
20 | public SingleDownloadTask(DownloadInfo downloadInfo, ThreadInfo threadInfo, OnDownloadListener listener) {
21 | super(downloadInfo, threadInfo, listener);
22 | }
23 |
24 | @Override
25 | protected void insertIntoDB(ThreadInfo info) {
26 | // don't support
27 | L.d("SingleDownloadTask: insertIntoDB-> not implement");
28 | }
29 |
30 | @Override
31 | protected int getResponseCode() {
32 | return HttpURLConnection.HTTP_OK;
33 | }
34 |
35 | @Override
36 | protected void updateDB(ThreadInfo info) {
37 | // needn't Override this
38 | }
39 |
40 | @Override
41 | protected Map getHttpHeaders(ThreadInfo info) {
42 | // not have header
43 | return null;
44 | }
45 |
46 | @Override
47 | protected RandomAccessFile getRandomAccessFile(File dir, String name, long offset) throws IOException {
48 | File file = new File(dir, name);
49 | RandomAccessFile raf = new RandomAccessFile(file, "rwd");
50 | raf.seek(0);
51 | return raf;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/task/SingleDownloadTask.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.task;
2 |
3 | import com.less.downloadmanager.lib.bean.DownloadInfo;
4 | import com.less.downloadmanager.lib.bean.ThreadInfo;
5 | import com.less.downloadmanager.lib.listenter.OnDownloadListener;
6 | import com.less.downloadmanager.lib.util.L;
7 |
8 | import java.io.File;
9 | import java.io.IOException;
10 | import java.io.RandomAccessFile;
11 | import java.net.HttpURLConnection;
12 | import java.util.Map;
13 |
14 | /**
15 | * Created by Administrator on 2017/9/16.
16 | */
17 |
18 | public class SingleDownloadTask extends DownloadTaskImpl {
19 |
20 | public SingleDownloadTask(DownloadInfo downloadInfo, ThreadInfo threadInfo, OnDownloadListener listener) {
21 | super(downloadInfo, threadInfo, listener);
22 | }
23 |
24 | @Override
25 | protected void insertIntoDB(ThreadInfo info) {
26 | // don't support
27 | L.d("SingleDownloadTask: insertIntoDB-> not implement");
28 | }
29 |
30 | @Override
31 | protected int getResponseCode() {
32 | return HttpURLConnection.HTTP_OK;
33 | }
34 |
35 | @Override
36 | protected void updateDB(ThreadInfo info) {
37 | // needn't Override this
38 | }
39 |
40 | @Override
41 | protected Map getHttpHeaders(ThreadInfo info) {
42 | // not have header
43 | return null;
44 | }
45 |
46 | @Override
47 | protected RandomAccessFile getRandomAccessFile(File dir, String name, long offset) throws IOException {
48 | File file = new File(dir, name);
49 | RandomAccessFile raf = new RandomAccessFile(file, "rwd");
50 | raf.seek(0);
51 | return raf;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/request/StringCallback.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 | import com.less.downloadmanager.lib.util.IOUtils;
5 |
6 | import java.io.BufferedReader;
7 | import java.io.File;
8 | import java.io.FileNotFoundException;
9 | import java.io.FileReader;
10 |
11 | /**
12 | * Created by Administrator on 2017/9/18.
13 | */
14 |
15 | public abstract class StringCallback extends Callback{
16 |
17 | @Override
18 | public abstract void onStart(String tag);
19 |
20 | @Override
21 | public void onDownloadProgress(String tag,long finished, long totalLength, int percent){}
22 |
23 | @Override
24 | public void onDownloadPaused() {}
25 |
26 | @Override
27 | public void onDownloadCanceled() {}
28 |
29 | @Override
30 | public abstract void onDownloadFailed(DownloadException e);
31 |
32 |
33 | @Override
34 | public String parseResponse(File file) {
35 | try {
36 | FileReader fileReader = new FileReader(file);
37 | BufferedReader bufferedReader = new BufferedReader(fileReader);
38 | String line = null;
39 | StringBuffer buffer = new StringBuffer();
40 | while ((line = bufferedReader.readLine()) != null) {
41 | buffer.append(line + "\r\n");
42 | }
43 | bufferedReader.close();
44 | fileReader.close();
45 | return buffer.toString();
46 | } catch (Exception e) {
47 | e.printStackTrace();
48 | }
49 |
50 | return null;
51 | }
52 |
53 | @Override
54 | public abstract void onDownloadCompleted(String s); // 此形参是parseResponse方法的返回值。
55 | }
56 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/request/StringCallback.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 | import com.less.downloadmanager.lib.util.IOUtils;
5 |
6 | import java.io.BufferedReader;
7 | import java.io.File;
8 | import java.io.FileNotFoundException;
9 | import java.io.FileReader;
10 |
11 | /**
12 | * Created by Administrator on 2017/9/18.
13 | */
14 |
15 | public abstract class StringCallback extends Callback{
16 |
17 | @Override
18 | public abstract void onStart(String tag);
19 |
20 | @Override
21 | public void onDownloadProgress(String tag,long finished, long totalLength, int percent){}
22 |
23 | @Override
24 | public void onDownloadPaused() {}
25 |
26 | @Override
27 | public void onDownloadCanceled() {}
28 |
29 | @Override
30 | public abstract void onDownloadFailed(DownloadException e);
31 |
32 |
33 | @Override
34 | public String parseResponse(File file) {
35 | try {
36 | FileReader fileReader = new FileReader(file);
37 | BufferedReader bufferedReader = new BufferedReader(fileReader);
38 | String line = null;
39 | StringBuffer buffer = new StringBuffer();
40 | while ((line = bufferedReader.readLine()) != null) {
41 | buffer.append(line + "\r\n");
42 | }
43 | bufferedReader.close();
44 | fileReader.close();
45 | return buffer.toString();
46 | } catch (Exception e) {
47 | e.printStackTrace();
48 | }
49 |
50 | return null;
51 | }
52 |
53 | @Override
54 | public abstract void onDownloadCompleted(String s); // 此形参是parseResponse方法的返回值。
55 | }
56 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/db/DatabaseManager.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.db;
2 |
3 | import java.util.List;
4 |
5 | import com.less.downloadmanager.lib.bean.ThreadInfo;
6 |
7 | /** 相当于Service -> Dao -> Database */
8 | public class DatabaseManager {
9 | private static DatabaseManager sDataBaseManager;
10 | private ThreadInfoDao threadInfoDao;
11 |
12 | public DatabaseManager(){
13 | threadInfoDao = new ThreadInfoDao();
14 |
15 | createTable();
16 | }
17 |
18 | private synchronized void createTable() {
19 | threadInfoDao.createTable();
20 | }
21 |
22 | public static DatabaseManager getInstance() {
23 | if (sDataBaseManager == null) {
24 | synchronized (DatabaseManager.class) {
25 | if (sDataBaseManager == null) {
26 | sDataBaseManager = new DatabaseManager();
27 | }
28 | }
29 | }
30 | return sDataBaseManager;
31 | }
32 |
33 | public synchronized void add(ThreadInfo threadInfo){
34 | threadInfoDao.add(threadInfo);
35 | }
36 |
37 | public synchronized void delete(String tag) {
38 | threadInfoDao.delete(tag);
39 | }
40 |
41 | public synchronized void update(String tag, int threadId, long finished) {
42 | threadInfoDao.update(tag, threadId, finished);
43 | }
44 |
45 | public synchronized List getThreadInfos(String tag) {
46 | return threadInfoDao.find(tag);
47 | }
48 |
49 | /**
50 | * 使用2个条件找到 相应的ThreadInfo,因为每个文件下载所对应的线程数组(字段threadId 【非主键】都是1,2,3,4,5等),
51 | * 而字段tag标识url,准确找到某个ThreadInfo
52 | */
53 | public synchronized boolean exists(String tag, int threadId) {
54 | return threadInfoDao.exists(tag, threadId);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/util/Platform.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | import android.os.Build;
4 | import android.os.Handler;
5 | import android.os.Looper;
6 | import android.support.annotation.NonNull;
7 |
8 | import java.util.concurrent.Executor;
9 | import java.util.concurrent.Executors;
10 |
11 | public class Platform {
12 | private static final Platform PLATFORM = findPlatform();
13 | private static Executor executor;
14 | public static Platform get() {
15 | return PLATFORM;
16 | }
17 |
18 | private static Platform findPlatform() {
19 | try {
20 | Class.forName("android.os.Build");
21 | if (Build.VERSION.SDK_INT != 0) {
22 | return new Android(); // 创建Android平台的UI线程池
23 | }
24 | } catch (ClassNotFoundException ignored) {
25 | }
26 | return new Platform();// 创建Java平台的线程池
27 | }
28 |
29 | public Executor makeCallbackExecutor() {
30 | if (executor == null) {
31 | executor = Executors.newCachedThreadPool();
32 | }
33 | return executor;
34 | }
35 |
36 | /** execute 线程 */
37 | public void execute(Runnable runnable) {
38 | makeCallbackExecutor().execute(runnable);
39 | }
40 |
41 | static class Android extends Platform {
42 | @Override
43 | public Executor makeCallbackExecutor() {
44 | if (executor != null) {
45 | return executor;
46 | }
47 | return executor = new Executor() {
48 | private Handler handler = new Handler(Looper.getMainLooper());
49 |
50 | @Override
51 | public void execute(Runnable runnable) {
52 | handler.post(runnable);
53 | }
54 | };
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/db/DatabaseManager.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.db;
2 |
3 | /**
4 | * Created by Administrator on 2017/9/16.
5 | */
6 |
7 | import android.content.Context;
8 |
9 | import com.less.downloadmanager.lib.bean.ThreadInfo;
10 |
11 | import java.util.List;
12 |
13 | /** 相当于Service -> Dao -> Database */
14 | public class DatabaseManager {
15 | private static DatabaseManager sDataBaseManager;
16 | private ThreadInfoDao threadInfoDao;
17 |
18 | public DatabaseManager(Context context){
19 | threadInfoDao = new ThreadInfoDao(context);
20 | }
21 |
22 | public static DatabaseManager getInstance(Context context) {
23 | if (sDataBaseManager == null) {
24 | synchronized (DatabaseManager.class) {
25 | if (sDataBaseManager == null) {
26 | sDataBaseManager = new DatabaseManager(context);
27 | }
28 | }
29 | }
30 | return sDataBaseManager;
31 | }
32 |
33 | public synchronized void add(ThreadInfo threadInfo){
34 | threadInfoDao.add(threadInfo);
35 | }
36 |
37 | public synchronized void delete(String tag) {
38 | threadInfoDao.delete(tag);
39 | }
40 |
41 | public synchronized void update(String tag, int threadId, long finished) {
42 | threadInfoDao.update(tag, threadId, finished);
43 | }
44 |
45 | public synchronized List getThreadInfos(String tag) {
46 | return threadInfoDao.find(tag);
47 | }
48 |
49 | /**
50 | * 使用2个条件找到 相应的ThreadInfo,因为每个文件下载所对应的线程数组(字段threadId 【非主键】都是1,2,3,4,5等),
51 | * 而字段tag标识url,准确找到某个ThreadInfo
52 | */
53 | public synchronized boolean exists(String tag, int threadId) {
54 | return threadInfoDao.exists(tag, threadId);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/bean/DownloadInfo.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.bean;
2 |
3 | import java.io.File;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/22.
7 | */
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 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/bean/DownloadInfo.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.bean;
2 |
3 | import java.io.File;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/22.
7 | */
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 |
--------------------------------------------------------------------------------
/lib_android/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion "26.0.0"
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 26
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | task buildJar_android(type: Jar, dependsOn: ['compileReleaseJavaWithJavac']) { // task buildJar(type: Jar, dependsOn: ['build'])
25 | archiveName = "downloader-android-1.1.jar"// 导出的jar文件的名称
26 | from('build/intermediates/classes/release')// 需打包的资源所在的路径集
27 | destinationDir = file('../libs') // 导出的jar文件的存放目录(未指定则默认存放在build/libs下)
28 |
29 | exclude "com/less/downloadmanager/lib_android/BuildConfig.class"
30 | exclude('android/**')
31 | exclude('**/R.class')
32 | exclude('**/R\$*.class')
33 | include "**/*.*"
34 | }
35 |
36 | task buildJar_java(type: Jar, dependsOn: ['compileReleaseJavaWithJavac']) { // task buildJar(type: Jar, dependsOn: ['build'])
37 | archiveName = "downloader-java-1.1.jar"// 导出的jar文件的名称
38 | from('../lib_java/build/classes/main')// 需打包的资源所在的路径集
39 | destinationDir = file('../libs') // 导出的jar文件的存放目录(未指定则默认存放在build/libs下)
40 |
41 | exclude "com/less/downloadmanager/App.class"
42 | exclude('**/R.class')
43 | exclude('**/R\$*.class')
44 | include "**/*.*"
45 | }
46 |
47 | dependencies {
48 | compile fileTree(dir: 'libs', include: ['*.jar'])
49 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
50 | exclude group: 'com.android.support', module: 'support-annotations'
51 | })
52 | compile 'com.android.support:appcompat-v7:26.+'
53 | testCompile 'junit:junit:4.12'
54 | }
55 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/bean/ThreadInfo.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.bean;
2 |
3 | import java.io.RandomAccessFile;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/16.
7 | */
8 |
9 | public class ThreadInfo {
10 | private int id;
11 | private String tag;// 下载文件的标识,多线程下载共用此tag.
12 | private String uri;// 文件url
13 | private long start;// RandomAccessFile 下载开始位置
14 | private long end;// RandomAccessFile 下载结束位置
15 | private long finished;// 本线程已下载
16 |
17 | public ThreadInfo() {
18 | }
19 |
20 | public ThreadInfo(int id, String tag, String uri, long finished) {
21 | this.id = id;
22 | this.tag = tag;
23 | this.uri = uri;
24 | this.finished = finished;
25 | }
26 |
27 | public ThreadInfo(int id, String tag, String uri, long start, long end, long finished) {
28 | this.id = id;
29 | this.tag = tag;
30 | this.uri = uri;
31 | this.start = start;
32 | this.end = end;
33 | this.finished = finished;
34 | }
35 |
36 | public int getId() {
37 | return id;
38 | }
39 |
40 | public void setId(int id) {
41 | this.id = id;
42 | }
43 |
44 | public String getTag() {
45 | return tag;
46 | }
47 |
48 | public void setTag(String tag) {
49 | this.tag = tag;
50 | }
51 |
52 | public String getUri() {
53 | return uri;
54 | }
55 |
56 | public void setUri(String uri) {
57 | this.uri = uri;
58 | }
59 |
60 | public long getStart() {
61 | return start;
62 | }
63 |
64 | public void setStart(long start) {
65 | this.start = start;
66 | }
67 |
68 | public long getEnd() {
69 | return end;
70 | }
71 |
72 | public void setEnd(long end) {
73 | this.end = end;
74 | }
75 |
76 | public long getFinished() {
77 | return finished;
78 | }
79 |
80 | public void setFinished(long finished) {
81 | this.finished = finished;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/bean/ThreadInfo.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.bean;
2 |
3 | import java.io.RandomAccessFile;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/16.
7 | */
8 |
9 | public class ThreadInfo {
10 | private int id;
11 | private String tag;// 下载文件的标识,多线程下载共用此tag.
12 | private String uri;// 文件url
13 | private long start;// RandomAccessFile 下载开始位置
14 | private long end;// RandomAccessFile 下载结束位置
15 | private long finished;// 本线程已下载
16 |
17 | public ThreadInfo() {
18 | }
19 |
20 | public ThreadInfo(int id, String tag, String uri, long finished) {
21 | this.id = id;
22 | this.tag = tag;
23 | this.uri = uri;
24 | this.finished = finished;
25 | }
26 |
27 | public ThreadInfo(int id, String tag, String uri, long start, long end, long finished) {
28 | this.id = id;
29 | this.tag = tag;
30 | this.uri = uri;
31 | this.start = start;
32 | this.end = end;
33 | this.finished = finished;
34 | }
35 |
36 | public int getId() {
37 | return id;
38 | }
39 |
40 | public void setId(int id) {
41 | this.id = id;
42 | }
43 |
44 | public String getTag() {
45 | return tag;
46 | }
47 |
48 | public void setTag(String tag) {
49 | this.tag = tag;
50 | }
51 |
52 | public String getUri() {
53 | return uri;
54 | }
55 |
56 | public void setUri(String uri) {
57 | this.uri = uri;
58 | }
59 |
60 | public long getStart() {
61 | return start;
62 | }
63 |
64 | public void setStart(long start) {
65 | this.start = start;
66 | }
67 |
68 | public long getEnd() {
69 | return end;
70 | }
71 |
72 | public void setEnd(long end) {
73 | this.end = end;
74 | }
75 |
76 | public long getFinished() {
77 | return finished;
78 | }
79 |
80 | public void setFinished(long finished) {
81 | this.finished = finished;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/util/FileUtils.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | import java.net.HttpURLConnection;
4 | import java.util.Locale;
5 | import java.util.UUID;
6 | import java.util.regex.Matcher;
7 | import java.util.regex.Pattern;
8 |
9 | public class FileUtils {
10 |
11 | public static final String getPrefix(String fileName) {
12 | return fileName.substring(0, fileName.lastIndexOf("."));
13 | }
14 |
15 | public static final String getSuffix(String fileName) {
16 | return fileName.substring(fileName.lastIndexOf(".") + 1);
17 | }
18 |
19 | /**
20 | * Get file name
21 | * @param url 根据url获取文件名
22 | * @param conn 根据HttpConnection 获取文件名
23 | * @return
24 | */
25 | private String getFileName(HttpURLConnection conn,String url) {
26 | String filename = url.substring(url.lastIndexOf("/") + 1);
27 |
28 | if (null == filename || filename.length() < 1) {// Get file name failed.
29 | for (int i = 0;; i++) { // Get file name from http header.
30 | String mine = conn.getHeaderField(i);
31 | if (mine == null)
32 | break; // Exit the loop when go through all http header.
33 | if ("content-disposition".equals(conn.getHeaderFieldKey(i).toLowerCase(Locale.ENGLISH))) { // Get content-disposition header field returns, which may contain a file name
34 | Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase(Locale.ENGLISH)); // Using regular expressions query file name
35 | if (m.find()) {
36 | return m.group(1); // If there is compliance with the rules of the regular expression string
37 | }
38 | }
39 | }
40 | filename = UUID.randomUUID() + ".tmp";// A 16-byte binary digits generated by a unique identification number
41 | // (each card has a unique identification number)
42 | // on the card and the CPU clock as the file name
43 | }
44 | return filename;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/bean/DownloadStatus.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.bean;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/22.
7 | */
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 length;
21 | private long finished;
22 | private int percent;
23 | private boolean acceptRanges;
24 | private DownloadException exception;
25 |
26 | public int getStatus() {
27 | return status;
28 | }
29 |
30 | public void setStatus(int status) {
31 | this.status = status;
32 | }
33 |
34 | public long getLength() {
35 | return length;
36 | }
37 |
38 | public void setLength(long length) {
39 | this.length = length;
40 | }
41 |
42 | public long getFinished() {
43 | return finished;
44 | }
45 |
46 | public void setFinished(long finished) {
47 | this.finished = finished;
48 | }
49 |
50 | public int getPercent() {
51 | return percent;
52 | }
53 |
54 | public void setPercent(int percent) {
55 | this.percent = percent;
56 | }
57 |
58 | public boolean isAcceptRanges() {
59 | return acceptRanges;
60 | }
61 |
62 | public void setAcceptRanges(boolean acceptRanges) {
63 | this.acceptRanges = acceptRanges;
64 | }
65 |
66 | public Exception getException() {
67 | return exception;
68 | }
69 |
70 | public void setException(DownloadException exception) {
71 | this.exception = exception;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/bean/DownloadStatus.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.bean;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 |
5 | /**
6 | * Created by Administrator on 2017/9/22.
7 | */
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 length;
21 | private long finished;
22 | private int percent;
23 | private boolean acceptRanges;
24 | private DownloadException exception;
25 |
26 | public int getStatus() {
27 | return status;
28 | }
29 |
30 | public void setStatus(int status) {
31 | this.status = status;
32 | }
33 |
34 | public long getLength() {
35 | return length;
36 | }
37 |
38 | public void setLength(long length) {
39 | this.length = length;
40 | }
41 |
42 | public long getFinished() {
43 | return finished;
44 | }
45 |
46 | public void setFinished(long finished) {
47 | this.finished = finished;
48 | }
49 |
50 | public int getPercent() {
51 | return percent;
52 | }
53 |
54 | public void setPercent(int percent) {
55 | this.percent = percent;
56 | }
57 |
58 | public boolean isAcceptRanges() {
59 | return acceptRanges;
60 | }
61 |
62 | public void setAcceptRanges(boolean acceptRanges) {
63 | this.acceptRanges = acceptRanges;
64 | }
65 |
66 | public Exception getException() {
67 | return exception;
68 | }
69 |
70 | public void setException(DownloadException exception) {
71 | this.exception = exception;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/task/MultiDownloadTask.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.task;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 | import java.io.RandomAccessFile;
6 | import java.net.HttpURLConnection;
7 | import java.util.HashMap;
8 | import java.util.Map;
9 |
10 | import com.less.downloadmanager.lib.bean.DownloadInfo;
11 | import com.less.downloadmanager.lib.bean.ThreadInfo;
12 | import com.less.downloadmanager.lib.db.DatabaseManager;
13 | import com.less.downloadmanager.lib.listenter.OnDownloadListener;
14 | import com.less.downloadmanager.lib.util.L;
15 |
16 | /**
17 | * Created by Administrator on 2017/9/16.
18 | */
19 |
20 | public class MultiDownloadTask extends DownloadTaskImpl{
21 | private DatabaseManager mDBManager;
22 |
23 | public MultiDownloadTask(DownloadInfo mDownloadInfo, ThreadInfo info, DatabaseManager databaseManager, OnDownloadListener onDownloadListener) {
24 | super(mDownloadInfo, info, onDownloadListener);
25 | this.mDBManager = databaseManager;
26 | }
27 |
28 |
29 | @Override
30 | protected void insertIntoDB(ThreadInfo info) {
31 | if (!mDBManager.exists(info.getTag(), info.getId())) {
32 | L.d("MultiDownloadTask: insertIntoDB->" + info.getId() + " | " + info.getTag());
33 | mDBManager.add(info);
34 | }
35 | }
36 |
37 | @Override
38 | protected int getResponseCode() {
39 | return HttpURLConnection.HTTP_PARTIAL;
40 | }
41 |
42 | @Override
43 | protected void updateDB(ThreadInfo info) {
44 | L.d("MultiDownloadTask(pause): updateDB-> " + info.getId() + " | " + info.getTag());
45 | mDBManager.update(info.getTag(), info.getId(), info.getFinished());
46 | }
47 |
48 | @Override
49 | protected Map getHttpHeaders(ThreadInfo info) {
50 | Map headers = new HashMap();
51 | long start = info.getStart() + info.getFinished();
52 | long end = info.getEnd();
53 | headers.put("Range", "bytes=" + start + "-" + end);
54 | return headers;
55 | }
56 |
57 | @Override
58 | protected RandomAccessFile getRandomAccessFile(File dir, String name, long offset) throws IOException {
59 | File file = new File(dir, name);
60 | RandomAccessFile raf = new RandomAccessFile(file, "rwd");
61 | raf.seek(offset);
62 | return raf;
63 | }
64 | }
65 |
66 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/task/MultiDownloadTask.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.task;
2 |
3 | import com.less.downloadmanager.lib.bean.DownloadInfo;
4 | import com.less.downloadmanager.lib.bean.ThreadInfo;
5 | import com.less.downloadmanager.lib.db.DatabaseManager;
6 | import com.less.downloadmanager.lib.listenter.OnDownloadListener;
7 | import com.less.downloadmanager.lib.util.L;
8 |
9 | import java.io.File;
10 | import java.io.IOException;
11 | import java.io.RandomAccessFile;
12 | import java.net.HttpURLConnection;
13 | import java.util.HashMap;
14 | import java.util.Map;
15 |
16 | /**
17 | * Created by Administrator on 2017/9/16.
18 | */
19 |
20 | public class MultiDownloadTask extends DownloadTaskImpl{
21 | private DatabaseManager mDBManager;
22 |
23 | public MultiDownloadTask(DownloadInfo mDownloadInfo, ThreadInfo info, DatabaseManager databaseManager, OnDownloadListener onDownloadListener) {
24 | super(mDownloadInfo, info, onDownloadListener);
25 | this.mDBManager = databaseManager;
26 | }
27 |
28 |
29 | @Override
30 | protected void insertIntoDB(ThreadInfo info) {
31 | if (!mDBManager.exists(info.getTag(), info.getId())) {
32 | L.d("MultiDownloadTask: insertIntoDB->" + info.getId() + " | " + info.getTag());
33 | mDBManager.add(info);
34 | }
35 | }
36 |
37 | @Override
38 | protected int getResponseCode() {
39 | return HttpURLConnection.HTTP_PARTIAL;
40 | }
41 |
42 | @Override
43 | protected void updateDB(ThreadInfo info) {
44 | L.d("MultiDownloadTask(pause): updateDB-> " + info.getId() + " | " + info.getTag());
45 | mDBManager.update(info.getTag(), info.getId(), info.getFinished());
46 | }
47 |
48 | @Override
49 | protected Map getHttpHeaders(ThreadInfo info) {
50 | Map headers = new HashMap();
51 | long start = info.getStart() + info.getFinished();
52 | long end = info.getEnd();
53 | headers.put("Range", "bytes=" + start + "-" + end);
54 | return headers;
55 | }
56 |
57 | @Override
58 | protected RandomAccessFile getRandomAccessFile(File dir, String name, long offset) throws IOException {
59 | File file = new File(dir, name);
60 | RandomAccessFile raf = new RandomAccessFile(file, "rwd");
61 | raf.seek(offset);
62 | return raf;
63 | }
64 | }
65 |
66 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/request/Callback.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 |
5 | import java.io.File;
6 |
7 | /**
8 | * Created by Administrator on 2017/9/18.
9 | */
10 |
11 | public abstract class Callback {
12 | /**
13 | * UI Thread
14 | */
15 | public abstract void onStart(String tag);
16 |
17 | /** UI Thread */
18 | public void onConnecting() {}
19 |
20 | /** UI Thread */
21 | public void onConnected(long length,boolean isAcceptRanges){};
22 |
23 | /** UI Thread */
24 | public void onConnectPaused() {
25 | // nothing to do(because downloaderImpl.pause() callback.onDownloadPaused() )
26 | }
27 |
28 | /** UI Thread */
29 | public void onConnectCanceled(){
30 | onDownloadCanceled();// 把onConnectCanceled 当做 onDownloadCanceled 处理
31 | };
32 |
33 | /** UI Thread */
34 | public void onConnectFailed(DownloadException e){
35 | onDownloadFailed(e);// 把onConnectFailed 当做 onDownloadFailed 处理
36 | };
37 |
38 | /** UI Thread
39 | * @param finished 已下载
40 | * @param totalLength 文件总长度
41 | * @param percent 百分比
42 | */
43 | public abstract void onDownloadProgress(String tag,long finished, long totalLength , int percent);
44 |
45 | /** UI Thread */
46 | public abstract void onDownloadPaused();
47 |
48 | public abstract void onDownloadCanceled();
49 |
50 | public abstract void onDownloadFailed(DownloadException e);
51 |
52 | public abstract T parseResponse(File file);
53 |
54 | public abstract void onDownloadCompleted(T t);
55 |
56 | public static Callback CALLBACK_DEFAULT = new Callback() {
57 |
58 | @Override
59 | public void onStart(String tag) {
60 |
61 | }
62 |
63 | @Override
64 | public void onDownloadProgress(String tag,long finished, long totalLength, int percent) {
65 |
66 | }
67 |
68 | @Override
69 | public void onDownloadPaused() {
70 |
71 | }
72 |
73 | @Override
74 | public void onDownloadCompleted(Object object) {
75 |
76 | }
77 |
78 | @Override
79 | public void onDownloadCanceled() {
80 |
81 | }
82 |
83 | @Override
84 | public void onDownloadFailed(DownloadException e) {
85 |
86 | }
87 |
88 | @Override
89 | public Object parseResponse(File file) {
90 |
91 | return null;
92 | }
93 | };
94 | }
95 |
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/request/Callback.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.request;
2 |
3 | import com.less.downloadmanager.lib.DownloadException;
4 |
5 | import java.io.File;
6 |
7 | /**
8 | * Created by Administrator on 2017/9/18.
9 | */
10 |
11 | public abstract class Callback {
12 | /**
13 | * UI Thread
14 | */
15 | public abstract void onStart(String tag);
16 |
17 | /** UI Thread */
18 | public void onConnecting() {}
19 |
20 | /** UI Thread */
21 | public void onConnected(long length,boolean isAcceptRanges){};
22 |
23 | /** UI Thread */
24 | public void onConnectCanceled(){
25 | onDownloadCanceled();// 把onConnectCanceled 当做 onDownloadCanceled 处理
26 | };
27 |
28 | /** UI Thread */
29 | public void onConnectPaused() {
30 | // nothing to do(because downloaderImpl.pause() callback.onDownloadPaused() )
31 | }
32 |
33 | /** UI Thread */
34 | public void onConnectFailed(DownloadException e){
35 | onDownloadFailed(e);// 把onConnectFailed 当做 onDownloadFailed 处理
36 | };
37 |
38 | /** UI Thread
39 | * @param finished 已下载
40 | * @param totalLength 文件总长度
41 | * @param percent 百分比
42 | */
43 | public abstract void onDownloadProgress(String tag,long finished, long totalLength , int percent);
44 |
45 | /** UI Thread */
46 | public abstract void onDownloadPaused();
47 |
48 | public abstract void onDownloadCanceled();
49 |
50 | public abstract void onDownloadFailed(DownloadException e);
51 |
52 | public abstract T parseResponse(File file);
53 |
54 | public abstract void onDownloadCompleted(T t);
55 |
56 | public static Callback CALLBACK_DEFAULT = new Callback() {
57 |
58 | @Override
59 | public void onStart(String tag) {
60 |
61 | }
62 |
63 | @Override
64 | public void onDownloadProgress(String tag,long finished, long totalLength, int percent) {
65 |
66 | }
67 |
68 | @Override
69 | public void onDownloadPaused() {
70 |
71 | }
72 |
73 | @Override
74 | public void onDownloadCompleted(Object object) {
75 |
76 | }
77 |
78 | @Override
79 | public void onDownloadCanceled() {
80 |
81 | }
82 |
83 | @Override
84 | public void onDownloadFailed(DownloadException e) {
85 |
86 | }
87 |
88 | @Override
89 | public Object parseResponse(File file) {
90 |
91 | return null;
92 | }
93 | };
94 | }
95 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/lib_android/src/main/java/com/less/downloadmanager/lib/util/FileUtils.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.util;
2 |
3 | import android.content.Context;
4 | import android.os.Environment;
5 | import android.support.annotation.NonNull;
6 |
7 | import java.io.File;
8 | import java.net.HttpURLConnection;
9 | import java.util.Locale;
10 | import java.util.UUID;
11 | import java.util.regex.Matcher;
12 | import java.util.regex.Pattern;
13 |
14 | public class FileUtils {
15 | private static final String DOWNLOAD_DIR = "download";
16 |
17 | public static final File getDefaultDownloadDir(Context context) {
18 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
19 | return new File(context.getExternalCacheDir(), DOWNLOAD_DIR);
20 | }
21 | return new File(context.getCacheDir(), DOWNLOAD_DIR);
22 | }
23 |
24 | public static boolean isSDMounted(){
25 | return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
26 | }
27 |
28 | public static final String getPrefix(String fileName) {
29 | return fileName.substring(0, fileName.lastIndexOf("."));
30 | }
31 |
32 | public static final String getSuffix(String fileName) {
33 | return fileName.substring(fileName.lastIndexOf(".") + 1);
34 | }
35 |
36 | /**
37 | * Get file name
38 | * @param url 根据url获取文件名
39 | * @param conn 根据HttpConnection 获取文件名
40 | * @return
41 | */
42 | private String getFileName(HttpURLConnection conn,String url) {
43 | String filename = url.substring(url.lastIndexOf("/") + 1);
44 |
45 | if (null == filename || filename.length() < 1) {// Get file name failed.
46 | for (int i = 0;; i++) { // Get file name from http header.
47 | String mine = conn.getHeaderField(i);
48 | if (mine == null)
49 | break; // Exit the loop when go through all http header.
50 | if ("content-disposition".equals(conn.getHeaderFieldKey(i).toLowerCase(Locale.ENGLISH))) { // Get content-disposition header field returns, which may contain a file name
51 | Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase(Locale.ENGLISH)); // Using regular expressions query file name
52 | if (m.find()) {
53 | return m.group(1); // If there is compliance with the rules of the regular expression string
54 | }
55 | }
56 | }
57 | filename = UUID.randomUUID() + ".tmp";// A 16-byte binary digits generated by a unique identification number
58 | // (each card has a unique identification number)
59 | // on the card and the CPU clock as the file name
60 | }
61 | return filename;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## DownloadManager 文件下载器
2 | 多线程下载,支持断点续传,分为Android和Java两个版本。
3 |
4 | 
5 |
6 | ## 用法
7 |
8 | ```
9 | RequestCall call = new GetBuilder()
10 | .name(name)
11 | .folder(folder)
12 | .uri(url)
13 | .tag(tag)
14 | .build();
15 | DownloadManager.getInstance(this).start(call, new FileCallBack() {
16 | @Override
17 | public void onStart(String tag) {
18 | L.d("=====> onStart " + tag);
19 | }
20 |
21 | @Override
22 | public void onDownloadProgress(String tag,long finished, long totalLength, int percent) {
23 | L.d("=====> onDownloadProgress: " + percent);
24 | }
25 |
26 | @Override
27 | public void onDownloadPaused() {
28 | L.d("=====> onDownloadPaused: " );
29 | }
30 |
31 | @Override
32 | public void onDownloadCanceled() {
33 | L.d("=====> onDownloadCanceled: " );
34 | }
35 |
36 | @Override
37 | public void onDownloadFailed(DownloadException e) {
38 | L.d("=====> onDownloadFailed: " + e.getErrorMessage());
39 | }
40 |
41 | @Override
42 | public void onDownloadCompleted(File file) {
43 | L.d("=====> onDownloadCompleted: " + file.getAbsolutePath());
44 | }
45 | });
46 |
47 | ```
48 |
49 | ## 简单调用
50 | 如果只想获取下载进度和结果,还可以这样调用。
51 | ```
52 | new GetBuilder()
53 | .name("JOKER_山本彩.mp4")
54 | .folder(new File("F:/"))
55 | .uri(url)
56 | .tag(tag)
57 | .build()
58 | .execute(new FileCallBack() {
59 | @Override
60 | public void onStart(String tag) {
61 | // UI Thread
62 | }
63 |
64 | @Override
65 | public void onDownloadProgress(String tag, long finished, long totalLength, int percent) {
66 | // UI Thread
67 | }
68 |
69 | @Override
70 | public void onDownloadFailed(DownloadException e) {
71 | // UI Thread
72 | }
73 |
74 | @Override
75 | public void onDownloadCompleted(File file) {
76 | // UI Thread
77 | }
78 | });
79 | ```
80 |
81 | ## 其他API
82 | 1. 取消一个下载任务
83 | ```
84 | DownloadManager.getInstance(this).cancel(tag);`
85 | ```
86 |
87 | 2. 暂停一个下载任务
88 | ```
89 | DownloadManager.getInstance(this).pause(tag);
90 | ```
91 | 3. 取消所有下载任务
92 | ```
93 | DownloadManager.getInstance(this).pauseAll();
94 | ```
95 | 4. 暂停所有下载任务
96 | ```
97 | DownloadManager.getInstance(this).cancelAll();
98 | ```
99 |
100 | ## 依赖
101 | Android: 拷贝libs目录`downloader-android-1.0.jar`到项目引用即可。
102 |
103 | Java: 拷贝libs目录`downloader-java-1.0.jar`到项目中,该jar需要额外引入`sqlite-jdbc.jar`,本项目使用的版本是`sqlite-jdbc-3.8.11.1.jar`。
104 |
105 |
106 | ## License
107 | Copyright 2017 wangli
108 |
109 | Licensed under the Apache License, Version 2.0 (the "License");
110 | you may not use this file except in compliance with the License.
111 | You may obtain a copy of the License at
112 |
113 | http://www.apache.org/licenses/LICENSE-2.0
114 |
115 | Unless required by applicable law or agreed to in writing, software
116 | distributed under the License is distributed on an "AS IS" BASIS,
117 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
118 | See the License for the specific language governing permissions and
119 | limitations under the License.
--------------------------------------------------------------------------------
/lib_java/src/main/java/com/less/downloadmanager/lib/db/ThreadInfoDao.java:
--------------------------------------------------------------------------------
1 | package com.less.downloadmanager.lib.db;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import java.util.Map;
6 |
7 | import com.less.downloadmanager.lib.bean.ThreadInfo;
8 |
9 | /**
10 | * Created by Administrator on 2017/9/16.
11 | */
12 |
13 | public class ThreadInfoDao extends BaseDaoImpl{
14 | private static final String TABLE_NAME = ThreadInfo.class.getSimpleName();
15 | private static ThreadInfoDao instance;
16 |
17 | /** 这里Dao如果用单例,注意context对象的释放,建议使用ApplicationContext对象 */
18 | public static ThreadInfoDao getInstance(){
19 | if (instance == null) {
20 | synchronized (ThreadInfo.class) {
21 | if (instance == null) {
22 | instance = new ThreadInfoDao();
23 | }
24 | }
25 | }
26 | return instance;
27 | }
28 |
29 | public void createTable() {
30 | String sql = String.format("create table if not exists %s(_id integer primary key autoincrement, id integer, tag text, uri text, start long, end long, finished long)", TABLE_NAME);
31 | SqlHelper.execSQL(sql, null);
32 | }
33 |
34 | public void dropTable() {
35 | String sql = String.format("drop table if exists %s", TABLE_NAME);
36 | SqlHelper.execSQL(sql, null);
37 | }
38 |
39 | @Override
40 | public void add(ThreadInfo info) {
41 | String [] params = new String[]{info.getId()+"", info.getTag(), info.getUri(), info.getStart()+"", info.getEnd()+"", info.getFinished()+""};
42 | SqlHelper.execSQL("insert into "
43 | + TABLE_NAME
44 | + "(id, tag, uri, start, end, finished) values(?, ?, ?, ?, ?, ?)", params);
45 | }
46 |
47 | @Override
48 | public void delete(String tag) {
49 | SqlHelper.execSQL("delete from "
50 | + TABLE_NAME
51 | + " where tag = ?", new String[]{tag});
52 | }
53 |
54 | @Override
55 | public void update(String tag, int threadId, long finished) {
56 | SqlHelper.execSQL("update "
57 | + TABLE_NAME
58 | + " set finished = ?"
59 | + " where tag = ? and id = ? ", new String[]{finished+"", tag, threadId+""});
60 | }
61 |
62 | @Override
63 | public List find(String tag) {
64 | List list = new ArrayList();
65 |
66 | List