├── .gitignore
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── ISSUE_TEMPLATE.md
├── LICENSE
├── PULL_REQUEST_TEMPLATE.md
├── README.md
├── RUSSIAN_README.md
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── loader
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── zerobranch
│ │ └── loader
│ │ ├── callbacks
│ │ ├── lifecycle
│ │ │ ├── OnCompleted.java
│ │ │ ├── OnError.java
│ │ │ ├── OnProgress.java
│ │ │ └── OnStart.java
│ │ └── receiving
│ │ │ ├── ReceivedFile.java
│ │ │ └── ReceivedFileSource.java
│ │ ├── core
│ │ ├── Core.java
│ │ ├── DownloadReceiver.java
│ │ ├── LoadManager.java
│ │ └── Loader.java
│ │ ├── exception
│ │ ├── BadResponseException.java
│ │ └── FileIsExistException.java
│ │ ├── service
│ │ ├── HideNotificationService.java
│ │ └── LoaderService.java
│ │ └── utils
│ │ ├── BundleConst.java
│ │ ├── Logger.java
│ │ └── Validator.java
│ └── res
│ └── values
│ └── strings.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | /.idea/workspace.xml
6 | /.idea/libraries
7 | .DS_Store
8 | /build
9 | /captures
10 | *.apk
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Code of Conduct
2 |
3 | ## 1. Purpose
4 |
5 | A primary goal of Fileloader is to be inclusive to the largest number of contributors, with the most varied and diverse backgrounds possible. As such, we are committed to providing a friendly, safe and welcoming environment for all, regardless of gender, sexual orientation, ability, ethnicity, socioeconomic status, and religion (or lack thereof).
6 |
7 | This code of conduct outlines our expectations for all those who participate in our community, as well as the consequences for unacceptable behavior.
8 |
9 | We invite all those who participate in Fileloader to help us create safe and positive experiences for everyone.
10 |
11 | ## 2. Open Source Citizenship
12 |
13 | A supplemental goal of this Code of Conduct is to increase open source citizenship by encouraging participants to recognize and strengthen the relationships between our actions and their effects on our community.
14 |
15 | Communities mirror the societies in which they exist and positive action is essential to counteract the many forms of inequality and abuses of power that exist in society.
16 |
17 | If you see someone who is making an extra effort to ensure our community is welcoming, friendly, and encourages all participants to contribute to the fullest extent, we want to know.
18 |
19 | ## 3. Expected Behavior
20 |
21 | The following behaviors are expected and requested of all community members:
22 |
23 | * Participate in an authentic and active way. In doing so, you contribute to the health and longevity of this community.
24 | * Exercise consideration and respect in your speech and actions.
25 | * Attempt collaboration before conflict.
26 | * Refrain from demeaning, discriminatory, or harassing behavior and speech.
27 | * Be mindful of your surroundings and of your fellow participants. Alert community leaders if you notice a dangerous situation, someone in distress, or violations of this Code of Conduct, even if they seem inconsequential.
28 | * Remember that community event venues may be shared with members of the public; please be respectful to all patrons of these locations.
29 |
30 | ## 4. Unacceptable Behavior
31 |
32 | The following behaviors are considered harassment and are unacceptable within our community:
33 |
34 | * Violence, threats of violence or violent language directed against another person.
35 | * Sexist, racist, homophobic, transphobic, ableist or otherwise discriminatory jokes and language.
36 | * Posting or displaying sexually explicit or violent material.
37 | * Posting or threatening to post other people’s personally identifying information ("doxing").
38 | * Personal insults, particularly those related to gender, sexual orientation, race, religion, or disability.
39 | * Inappropriate photography or recording.
40 | * Inappropriate physical contact. You should have someone’s consent before touching them.
41 | * Unwelcome sexual attention. This includes, sexualized comments or jokes; inappropriate touching, groping, and unwelcomed sexual advances.
42 | * Deliberate intimidation, stalking or following (online or in person).
43 | * Advocating for, or encouraging, any of the above behavior.
44 | * Sustained disruption of community events, including talks and presentations.
45 |
46 | ## 5. Consequences of Unacceptable Behavior
47 |
48 | Unacceptable behavior from any community member, including sponsors and those with decision-making authority, will not be tolerated.
49 |
50 | Anyone asked to stop unacceptable behavior is expected to comply immediately.
51 |
52 | If a community member engages in unacceptable behavior, the community organizers may take any action they deem appropriate, up to and including a temporary ban or permanent expulsion from the community without warning (and without refund in the case of a paid event).
53 |
54 | ## 6. Reporting Guidelines
55 |
56 | If you are subject to or witness unacceptable behavior, or have any other concerns, please notify a community organizer as soon as possible. vulturegray@gmail.com.
57 |
58 |
59 |
60 | Additionally, community organizers are available to help community members engage with local law enforcement or to otherwise help those experiencing unacceptable behavior feel safe. In the context of in-person events, organizers will also provide escorts as desired by the person experiencing distress.
61 |
62 | ## 7. Addressing Grievances
63 |
64 | If you feel you have been falsely or unfairly accused of violating this Code of Conduct, you should notify Arman Sargsyan with a concise description of your grievance. Your grievance will be handled in accordance with our existing governing policies.
65 |
66 |
67 |
68 | ## 8. Scope
69 |
70 | We expect all community participants (contributors, paid or otherwise; sponsors; and other guests) to abide by this Code of Conduct in all community venues–online and in-person–as well as in all one-on-one communications pertaining to community business.
71 |
72 | This code of conduct and its related procedures also applies to unacceptable behavior occurring outside the scope of community activities when such behavior has the potential to adversely affect the safety and well-being of community members.
73 |
74 | ## 9. Contact info
75 |
76 | vulturegray@gmail.com
77 |
78 | ## 10. License and attribution
79 |
80 | This Code of Conduct is distributed under a [Creative Commons Attribution-ShareAlike license](http://creativecommons.org/licenses/by-sa/3.0/).
81 |
82 | Portions of text derived from the [Django Code of Conduct](https://www.djangoproject.com/conduct/) and the [Geek Feminism Anti-Harassment Policy](http://geekfeminism.wikia.com/wiki/Conference_anti-harassment/Policy).
83 |
84 | Retrieved on November 22, 2016 from [http://citizencodeofconduct.org/](http://citizencodeofconduct.org/)
85 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | Contributing
2 | ------------
3 |
4 | fileloader is an open source.
5 |
6 | If you want to contribute to the development of the project, you can do this by sending a pull request (on a branch other than the master).
7 |
8 | When sending the code, please try to follow the existing conventions and style of writing the code so that the code is as readable as possible.
9 |
--------------------------------------------------------------------------------
/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | # Issue template
2 |
3 | Before submitting your issue, please use this template
4 |
5 | Description
6 | ------------
7 |
8 | [REQUIRED] Explain what was done, what was expected, and what happened
9 |
10 | Reproduction
11 | ------------
12 |
13 | [REQUIRED] How to reproduce this error. It is desirable to attach URLs to images or videos
14 |
15 | Solution
16 | ------------
17 |
18 | [OPTIONAL] Do you have a solution to fix this problem? You can send a pull request to fix this issue.
19 |
20 |
21 | Additional Information
22 | ------------
23 | [REQUIRED] Specify the version of File Loader
24 |
25 | [REQUIRED] Specify your device and version of android
26 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2017 Arman
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | # Pull request template
2 |
3 | Thank you for taking the time of our project. Please read the following instructions before sending pull request.
4 |
5 | - Explain the reason for making changes
6 | - Provide a **test cases** demonstrating that the code works
7 | - Your code must match **Code Conventions** for Java
8 | - All comments must be in English
9 | - Target the `develop` branch
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # File Loader
2 | [](https://jitpack.io/#zerobranch/Fileloader)
3 | [](https://github.com/zerobranch/Fileloader/blob/master/LICENSE) [](https://android-arsenal.com/details/1/7216)
4 |
5 | Library for managing file downloads on the Android platform
6 |
7 | ##### Choose language
8 | [English](https://github.com/zerobranch/Fileloader/blob/master/README.md)
9 |
10 | [Русский](https://github.com/zerobranch/Fileloader/blob/master/RUSSIAN_README.md)
11 |
12 | ## Descripton
13 | File Loader - is a library for the android platform. It allows you to download any files without much effort and get the result in the thread you specified.
14 |
15 | ## Capabilities
16 | - Download any files by their link
17 | - Save files in the specified folder
18 | - Get the result as bytes
19 | - Add files to the queue for their further download
20 | - Track all stages of downloading files
21 |
22 | ## How to use ?
23 |
24 | ```java
25 | Loader.with(Context)
26 | .fromUrl("YOUR_URL")
27 | .load();
28 | ```
29 |
30 | ## Description of methods
31 | **Required**
32 | ```
33 | - with(Cntext) - the main method with the context
34 | - addInQueue("YOUR_URL") - add the file to the download queue
35 | - load() - start downloading files
36 | ```
37 |
38 | **Optional**
39 | ```
40 | - to("YOUR_PATH") - the path to which the file will be uploaded
41 | - addInQueue("YOUR_URL") - add file to the download queue
42 | - skipIfFileExist() - the download will be terminated if the file already exists (by default, the file is overwritten)
43 | - abortNextIfError() - interrupt the remaining downloads waiting in the queue, if an error occurred during the download
44 | - makeImmortal() - the files will be downloaded in the foreground. Download does not stop even if it is unloaded from memory
45 | - notification(Notification) - connect your notification, which will be displayed at the time of downloading files
46 | - redownloadAttemptCount(4) - the number of attempts to download the file, in case there was an error
47 | - skipCache() - do not save the file in the device memory
48 | - viewNotificationOnFinish() - do not close the notification after downloading files
49 | - hideDefaultNotification() - hide notification when loading files (not recommended)
50 | - downloadReceiver() - setting DownloadReceiver will allow you to set events to get feedback from the loader
51 | - onStart(OnStart) - the event notifies the start of the download of the next file
52 | - onError(OnError) - the event notifies you when an error occurred while loading the file
53 | - onCompleted(OnCompleted) - the event notifies when the next file is being downloaded
54 | - onProgress(OnProgress) - event to track the progress of the download
55 | - receivedFile(ReceivedFile) - after the download is complete, get the path to the downloaded file
56 | - receivedFileSource(ReceivedFileSource) - after the download is complete, get the source file as a byte array
57 | - enableLogging() - enable logging while downloading files
58 | ```
59 |
60 | **Additional Methods**
61 | ```
62 | loader.cancel() - abort all downloads and delete the underloaded file
63 | loader.unsubscribe - unsubscribe from events (download will not be interrupted)
64 | ```
65 |
66 | **Note**
67 | ```
68 | - if you do not specify to ("YOUR_PATH"), then by default the files will be saved in the application cache - Context.getCacheDir()
69 | - notification(Notification), hideDefaultNotification(), viewNotificationOnFinish() - work only when you set makeImmortal()
70 | - to set the events (onStart, onError, etc.), you must set DownloadReceiver
71 | - if you set the receivedFileSource event, the data will go into this method WITHOUT CONSERVATION on the device
72 | ```
73 |
74 |
75 | ## Integration
76 | Add it in your root build.gradle at the end of repositories:
77 | ```groovy
78 | allprojects {
79 | repositories {
80 | ...
81 | maven { url 'https://jitpack.io' }
82 | }
83 | }
84 | ```
85 |
86 | Add the following dependency to your module's build.gradle:
87 | ```groovy
88 | dependencies {
89 | implementation 'com.github.zerobranch:Fileloader:1.0.0'
90 | }
91 | ```
92 |
93 | ## License
94 |
95 | ```
96 | The MIT License (MIT)
97 |
98 | Copyright (c) 2017 Arman Sargsyan
99 |
100 | Permission is hereby granted, free of charge, to any person obtaining a copy
101 | of this software and associated documentation files (the "Software"), to deal
102 | in the Software without restriction, including without limitation the rights
103 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
104 | copies of the Software, and to permit persons to whom the Software is
105 | furnished to do so, subject to the following conditions:
106 |
107 | The above copyright notice and this permission notice shall be included in all
108 | copies or substantial portions of the Software.
109 |
110 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
111 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
112 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
113 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
114 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
115 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
116 | SOFTWARE.
117 | ```
118 |
--------------------------------------------------------------------------------
/RUSSIAN_README.md:
--------------------------------------------------------------------------------
1 | # File Loader
2 | [](https://jitpack.io/#zerobranch/Fileloader)
3 | [](https://github.com/zerobranch/Fileloader/blob/master/LICENSE) [](https://android-arsenal.com/details/1/7216)
4 |
5 | Библиотека для управления загрузками файлов на платформе Android
6 |
7 | ##### Выберите язык
8 | [English](https://github.com/zerobranch/Fileloader/blob/master/README.md)
9 |
10 | [Русский](https://github.com/zerobranch/Fileloader/blob/master/RUSSIAN_README.md)
11 |
12 | ## Описание
13 | File Loader - это библиотека для платформы android. Она позволяет скачивать любые файлы без особых услилий и получать результат в указанном вами потоке.
14 |
15 | ## Возможности
16 | - Скачивать любые файлы по их ссылке
17 | - Сохранять файлы в указанной папке
18 | - Получать результат в виде байтов
19 | - Добавлять файлы в очередь для их дальнейшего скачивания
20 | - Отслеживать все этапы скачивания файлов
21 |
22 | ## Пример использования
23 |
24 | ```java
25 | Loader.with(Context)
26 | .fromUrl("YOUR_URL")
27 | .load();
28 | ```
29 |
30 | ## Описание методов
31 | **Обязательные**
32 | ```
33 | - with(Cntext) - основной метод, в него передается объкт Context
34 | - addInQueue("YOUR_URL") - добавить файл в очередь для загрузки
35 | - load() - начать загрузку файлов
36 | ```
37 |
38 | **Необязательные**
39 | ```
40 | - to("YOUR_PATH") - путь, куда будет загружен файл
41 | - addInQueue("YOUR_URL") - добавить файл в очередь для загрузки
42 | - skipIfFileExist() - загрузка будет прекращена, если файл уже существует (по умолчанию файл перезаписывается)
43 | - abortNextIfError() - если произошла ошибка во время скачивания - прервать остальные загрузки ожидающие в очереди
44 | - makeImmortal() - загрузка файлов будет осуществляться на переднем плане. Загрузка не прекратится даже если её выгрузить из памяти
45 | - notification(Notification) - подключить свое уведомление, которое будет отображаться во время загрузки файлов
46 | - redownloadAttemptCount(4) - количество попыток загрузить файл, в случае если произошла ошибка
47 | - skipCache() - не сохранять файл в памяти устройства
48 | - viewNotificationOnFinish() - не закрывать уведомление после загрузки файлов
49 | - hideDefaultNotification() - скрывать уведомление при загрузке файлов (не рекомендуется)
50 | - downloadReceiver() - установка DownloadReceiver позволит установить события для получения обратной связь с загрузчика
51 | - onStart(OnStart) - событие начала загрузки очередного файла
52 | - onError(OnError) - событие возникнования ошибки при загрузке файла
53 | - onCompleted(OnCompleted) - событие завершения загрузки очередного файла
54 | - onProgress(OnProgress) - событие для отслеживания хода загрузки
55 | - receivedFile(ReceivedFile) - после завершения загрузки получить путь к загруженному файлу
56 | - receivedFileSource(ReceivedFileSource) - после завершения загрузки получить исходный файл в виде массива байтов
57 | - enableLogging() - включить логирование во время загрузки файлов
58 | ```
59 |
60 | **Дополнительные методы**
61 | ```
62 | loader.cancel() - завершить все загрузки и удалить недозагруженный файл
63 | loader.unsubscribe - отписаться от событий (загрузка не будет прервана)
64 | ```
65 |
66 | **Примечание**
67 | ```
68 | - если не указать to("YOUR_PATH"), то по умолчанию файлы будут сохраняется в кэше приложения - Context.getCacheDir()
69 | - notification(Notification), hideDefaultNotification(), viewNotificationOnFinish() - работают только при установке makeImmortal()
70 | - для установки событий (onStart, onError и т.п.) необходимо установить DownloadReceiver
71 | - при установке собятия receivedFileSource - данные перейдут в этот метод БЕЗ СОХРАНЕНИЯ на устройстве
72 | ```
73 |
74 |
75 | ## Интеграция
76 | Добавьте в корневой build.gradle следующий репозиторий:
77 | ```groovy
78 | allprojects {
79 | repositories {
80 | ...
81 | maven { url 'https://jitpack.io' }
82 | }
83 | }
84 | ```
85 |
86 | Добавьте в build.gradle вашего модуля следующую зависимость:
87 | ```groovy
88 | dependencies {
89 | implementation 'com.github.zerobranch:Fileloader:1.0.0'
90 | }
91 | ```
92 |
93 | ## Лицензия
94 |
95 | ```
96 | The MIT License (MIT)
97 |
98 | Copyright (c) 2017 Arman Sargsyan
99 |
100 | Permission is hereby granted, free of charge, to any person obtaining a copy
101 | of this software and associated documentation files (the "Software"), to deal
102 | in the Software without restriction, including without limitation the rights
103 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
104 | copies of the Software, and to permit persons to whom the Software is
105 | furnished to do so, subject to the following conditions:
106 |
107 | The above copyright notice and this permission notice shall be included in all
108 | copies or substantial portions of the Software.
109 |
110 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
111 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
112 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
113 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
114 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
115 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
116 | SOFTWARE.
117 | ```
118 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.3.3'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zerobranch/Fileloader/47683c02a3048db142f22680c0540d78ae60c656/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Sep 19 13:27:54 SAMT 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 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/loader/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | /.idea/workspace.xml
6 | /.idea/libraries
7 | .DS_Store
8 | /build
9 | /captures
10 | *.apk
--------------------------------------------------------------------------------
/loader/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion "26.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 16
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 |
--------------------------------------------------------------------------------
/loader/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 /Users/arman/Library/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 |
--------------------------------------------------------------------------------
/loader/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
15 |
16 |
19 |
20 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/callbacks/lifecycle/OnCompleted.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.callbacks.lifecycle;
2 |
3 | public interface OnCompleted {
4 | void apply();
5 | }
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/callbacks/lifecycle/OnError.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.callbacks.lifecycle;
2 |
3 | public interface OnError {
4 | void apply(String fileName, Throwable throwable);
5 | }
6 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/callbacks/lifecycle/OnProgress.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.callbacks.lifecycle;
2 |
3 | public interface OnProgress {
4 | void apply(int progress);
5 | }
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/callbacks/lifecycle/OnStart.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.callbacks.lifecycle;
2 |
3 | public interface OnStart {
4 | void apply();
5 | }
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/callbacks/receiving/ReceivedFile.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.callbacks.receiving;
2 |
3 | import java.io.File;
4 |
5 | public interface ReceivedFile {
6 | void set(File file);
7 | }
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/callbacks/receiving/ReceivedFileSource.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.callbacks.receiving;
2 |
3 | public interface ReceivedFileSource {
4 | void set(byte[] source, String fileName);
5 | }
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/core/Core.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.core;
2 |
3 | import android.app.Notification;
4 | import android.content.Context;
5 | import android.content.Intent;
6 |
7 | import com.zerobranch.loader.core.Loader.Configurator;
8 | import com.zerobranch.loader.service.LoaderService;
9 | import com.zerobranch.loader.utils.BundleConst;
10 | import com.zerobranch.loader.utils.Logger;
11 | import com.zerobranch.loader.utils.Validator;
12 |
13 | import java.util.ArrayList;
14 |
15 | final class Core {
16 | private Context context;
17 | private ArrayList urls;
18 | private String path;
19 | private DownloadReceiver receiver;
20 | private Loader.ReceivedConfig receivedConfig;
21 | private Notification notification;
22 | private boolean isHideDefaultNotification;
23 | private boolean isViewNotificationOnFinish;
24 | private boolean isEnableLogging;
25 | private boolean isImmortal;
26 | private boolean isSkipCache;
27 | private boolean isSkipIfFileExist;
28 | private boolean isBreakNextIfError;
29 | private int redownloadAttemptCount;
30 |
31 | void build(Configurator configurator, Context context) {
32 | this.context = context;
33 | urls = configurator.getUrls();
34 | path = configurator.getPath();
35 | notification = configurator.getNotification();
36 | receiver = configurator.getDownloadReceiver();
37 | receivedConfig = configurator.getReceivedConfig();
38 | isHideDefaultNotification = configurator.isHideDefaultNotification();
39 | isViewNotificationOnFinish = configurator.isViewNotificationOnFinish();
40 | isEnableLogging = configurator.isEnableLogging();
41 | isImmortal = configurator.isImmortal();
42 | isSkipCache = configurator.isSkipCache();
43 | isSkipIfFileExist = configurator.isSkipIfFileExist();
44 | isBreakNextIfError = configurator.isAbortNextIfError();
45 | redownloadAttemptCount = configurator.getRedownloadAttemptCount();
46 | load();
47 | }
48 |
49 | private void load() {
50 | configure();
51 | validate();
52 | context.startService(getPreparedIntent());
53 | }
54 |
55 | private void configure() {
56 | if (isEnableLogging) {
57 | Logger.enableLogging();
58 | }
59 | if (receiver != null) {
60 | setReceivers();
61 | if (receiver.getReceivedFileSource() != null) {
62 | isSkipCache = true;
63 | }
64 | }
65 | if (isSkipCache) {
66 | path = null;
67 | }
68 | }
69 |
70 | private Intent getPreparedIntent() {
71 | return new Intent()
72 | .setClass(context, LoaderService.class)
73 | .putStringArrayListExtra(BundleConst.URL, urls)
74 | .putExtra(BundleConst.PATH, path)
75 | .putExtra(BundleConst.IMMORTAL, isImmortal)
76 | .putExtra(BundleConst.DEFAULT_NOTIFICATION, isHideDefaultNotification)
77 | .putExtra(BundleConst.VIEW_NOTIFICATION_ON_FINISH, isViewNotificationOnFinish)
78 | .putExtra(BundleConst.NOTIFICATION, notification)
79 | .putExtra(BundleConst.RECEIVER, receiver)
80 | .putExtra(BundleConst.SKIP_IF_EXIST, isSkipIfFileExist)
81 | .putExtra(BundleConst.ABORT_IF_ERROR, isBreakNextIfError)
82 | .putExtra(BundleConst.REDOWNLOAD_COUNT, redownloadAttemptCount);
83 | }
84 |
85 | private void setReceivers() {
86 | receiver.setReceivedFile(receivedConfig.getReceivedFile());
87 | receiver.setReceivedFileSource(receivedConfig.getReceivedFileSource());
88 | receiver.setOnStart(receivedConfig.getOnStart());
89 | receiver.setOnCompleted(receivedConfig.getOnCompleted());
90 | receiver.setOnProgress(receivedConfig.getOnProgress());
91 | receiver.setOnError(receivedConfig.getOnError());
92 | }
93 |
94 | private void validate() {
95 | if (path != null) {
96 | Validator.getNonEmptyValue(path, "Argument 'path' should not be empty");
97 | }
98 | Validator.getNonEmptyValue(urls, "List 'urls' should not be empty");
99 | Validator.getNonNull(context, "Context should not be null");
100 | Validator.getNotNegative(redownloadAttemptCount, "Argument 'redownloadAttemptCount' should not be null");
101 | }
102 |
103 | void cancel() {
104 | destroyService();
105 | context = null;
106 | }
107 |
108 | private void destroyService() {
109 | unsubscribe();
110 | if (context != null) {
111 | context.stopService(new Intent(context, LoaderService.class));
112 | }
113 | }
114 |
115 | void unsubscribe() {
116 | if (receiver != null) {
117 | receiver.unsubscribe();
118 | }
119 | }
120 | }
121 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/core/DownloadReceiver.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.core;
2 |
3 | import android.os.Bundle;
4 | import android.os.Handler;
5 | import android.os.ResultReceiver;
6 |
7 | import java.io.File;
8 |
9 | import com.zerobranch.loader.callbacks.lifecycle.OnCompleted;
10 | import com.zerobranch.loader.callbacks.lifecycle.OnError;
11 | import com.zerobranch.loader.callbacks.lifecycle.OnProgress;
12 | import com.zerobranch.loader.callbacks.lifecycle.OnStart;
13 | import com.zerobranch.loader.callbacks.receiving.ReceivedFile;
14 | import com.zerobranch.loader.callbacks.receiving.ReceivedFileSource;
15 | import com.zerobranch.loader.utils.BundleConst;
16 |
17 | final class DownloadReceiver extends ResultReceiver {
18 | private ReceivedFile receivedFile;
19 | private ReceivedFileSource receivedFileSource;
20 | private OnStart onStart;
21 | private OnCompleted onCompleted;
22 | private OnProgress onProgress;
23 | private OnError onError;
24 | private boolean isSubscribed;
25 |
26 | DownloadReceiver() {
27 | this(new Handler());
28 | }
29 |
30 | DownloadReceiver(Handler handler) {
31 | super(handler);
32 | }
33 |
34 | void unsubscribe() {
35 | setSubscribed(false);
36 | }
37 |
38 | @Override
39 | protected void onReceiveResult(int resultCode, Bundle resultData) {
40 | super.onReceiveResult(resultCode, resultData);
41 | if (!isSubscribed) {
42 | return;
43 | }
44 |
45 | final ReceiveCode receiveCode = ReceiveCode.get(resultCode);
46 | switch (receiveCode) {
47 | case RECEIVED_FILE:
48 | if (receivedFile != null) {
49 | receivedFile.set((File) resultData.getSerializable(BundleConst.FILE));
50 | }
51 | break;
52 | case RECEIVED_FILE_SOURCE:
53 | if (receivedFileSource != null) {
54 | receivedFileSource.set(resultData.getByteArray(BundleConst.BYTES),
55 | resultData.getString(BundleConst.FILE_NAME));
56 | }
57 | break;
58 | case ON_START:
59 | if (onStart != null) {
60 | onStart.apply();
61 | }
62 | break;
63 | case ON_COMPLETED:
64 | if (onCompleted != null) {
65 | onCompleted.apply();
66 | }
67 | break;
68 | case ON_ERROR:
69 | if (onError != null) {
70 | onError.apply(resultData.getString(BundleConst.FILE_NAME),
71 | (Throwable) resultData.getSerializable(BundleConst.THROWABLE));
72 | }
73 | break;
74 | case ON_PROGRESS:
75 | if (onProgress != null) {
76 | onProgress.apply(resultData.getInt(BundleConst.PROGRESS));
77 | }
78 | break;
79 | }
80 | }
81 |
82 | enum ReceiveCode {
83 | EMPTY(-1),
84 | RECEIVED_FILE(0),
85 | RECEIVED_FILE_SOURCE(1),
86 | ON_START(2),
87 | ON_COMPLETED(3),
88 | ON_ERROR(4),
89 | ON_PROGRESS(5);
90 |
91 | private final int code;
92 |
93 | ReceiveCode(int code) {
94 | this.code = code;
95 | }
96 |
97 | private static ReceiveCode get(int code) {
98 | for (ReceiveCode receiveCode : ReceiveCode.values()) {
99 | if (receiveCode.code == code) {
100 | return receiveCode;
101 | }
102 | }
103 | return EMPTY;
104 | }
105 |
106 | int getCode() {
107 | return code;
108 | }
109 | }
110 |
111 | void setReceivedFile(ReceivedFile receivedFile) {
112 | if (receivedFile != null) {
113 | this.receivedFile = receivedFile;
114 | setSubscribed(true);
115 | }
116 | }
117 |
118 | void setReceivedFileSource(ReceivedFileSource receivedFileSource) {
119 | if (receivedFileSource != null) {
120 | this.receivedFileSource = receivedFileSource;
121 | setSubscribed(true);
122 | }
123 | }
124 |
125 | void setOnStart(OnStart onStart) {
126 | if (onStart != null) {
127 | this.onStart = onStart;
128 | setSubscribed(true);
129 | }
130 | }
131 |
132 | void setOnCompleted(OnCompleted onCompleted) {
133 | if (onCompleted != null) {
134 | this.onCompleted = onCompleted;
135 | setSubscribed(true);
136 | }
137 | }
138 |
139 | void setOnProgress(OnProgress onProgress) {
140 | if (onProgress != null) {
141 | this.onProgress = onProgress;
142 | setSubscribed(true);
143 | }
144 | }
145 |
146 | void setOnError(OnError onError) {
147 | if (onError != null) {
148 | this.onError = onError;
149 | setSubscribed(true);
150 | }
151 | }
152 |
153 | private void setSubscribed(boolean subscribed) {
154 | isSubscribed = subscribed;
155 | }
156 |
157 | ReceivedFileSource getReceivedFileSource() {
158 | return receivedFileSource;
159 | }
160 | }
161 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/core/LoadManager.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.core;
2 |
3 | import android.os.Bundle;
4 | import android.os.ResultReceiver;
5 | import android.webkit.URLUtil;
6 |
7 | import java.io.BufferedInputStream;
8 | import java.io.ByteArrayOutputStream;
9 | import java.io.Closeable;
10 | import java.io.File;
11 | import java.io.FileOutputStream;
12 | import java.io.IOException;
13 | import java.io.InputStream;
14 | import java.io.InterruptedIOException;
15 | import java.io.OutputStream;
16 | import java.net.HttpURLConnection;
17 | import java.net.URL;
18 | import java.net.URLConnection;
19 |
20 | import com.zerobranch.loader.core.DownloadReceiver.ReceiveCode;
21 | import com.zerobranch.loader.exception.BadResponseException;
22 | import com.zerobranch.loader.exception.FileIsExistException;
23 | import com.zerobranch.loader.utils.BundleConst;
24 | import com.zerobranch.loader.utils.Logger;
25 |
26 | public class LoadManager {
27 | private ResultReceiver receiver;
28 | private File currentFile;
29 | private String fileName;
30 | private boolean isSkipIfFileExist;
31 | private boolean isAbortNextIfError;
32 | private int redownloadAttemptCount = 1;
33 | private int currentRedownloadAttempt = 1;
34 | private static volatile boolean isErrorPreviousDownload;
35 |
36 | public void loadFile(String path, String url, ResultReceiver resultReceiver) {
37 | loadFile(path, url, resultReceiver, true);
38 | }
39 |
40 | private synchronized void loadFile(String path, String url, ResultReceiver resultReceiver, boolean isFirstAttempt) {
41 | if (isAbortNextIfError && isErrorPreviousDownload) {
42 | Logger.debug("All next downloads were interrupted");
43 | return;
44 | }
45 | this.receiver = resultReceiver;
46 | if (isFirstAttempt) {
47 | sendTrail(ReceiveCode.ON_START, null);
48 | }
49 | fileName = URLUtil.guessFileName(url, null, URLConnection.guessContentTypeFromName(url));
50 | Logger.debug("Start downloading", fileName, "file. Attempt", currentRedownloadAttempt, "from", redownloadAttemptCount);
51 |
52 | try {
53 | tryDownloading(path, url);
54 | } catch (FileIsExistException e) {
55 | Logger.debug("File", fileName, "is exist in", path);
56 | sendTrail(ReceiveCode.ON_COMPLETED, null);
57 | } catch (InterruptedIOException e) {
58 | if (currentFile.delete()) {
59 | Logger.debug("All downloads were interrupted and file", currentFile.getName(), "was removed");
60 | return;
61 | }
62 | Logger.debug("All downloads were interrupted");
63 | return;
64 | } catch (Throwable throwable) {
65 | Logger.error("Downloading file", fileName, "is falled!");
66 | Logger.error(throwable.getMessage(), throwable);
67 | if (redownloadAttemptCount > currentRedownloadAttempt) {
68 | currentRedownloadAttempt++;
69 | loadFile(path, url, resultReceiver, false);
70 | } else {
71 | isErrorPreviousDownload = true;
72 | final Bundle bundle = new Bundle();
73 | bundle.putSerializable(BundleConst.THROWABLE, throwable);
74 | bundle.putString(BundleConst.FILE_NAME, fileName);
75 | sendTrail(ReceiveCode.ON_ERROR, bundle);
76 | }
77 | return;
78 | }
79 | Logger.debug("Download of", fileName, "file completed");
80 | sendTrail(ReceiveCode.ON_COMPLETED, null);
81 | }
82 |
83 | private void tryDownloading(String path, String url) throws IOException{
84 | if (path == null || path.isEmpty()) {
85 | downloading(url, null);
86 | } else {
87 | downloading(url, getOutputStream(path, fileName));
88 | }
89 | }
90 |
91 | private OutputStream getOutputStream(String path, String fileName) throws IOException {
92 | currentFile = new File(path, fileName);
93 | if (isSkipIfFileExist && currentFile.exists()) {
94 | throw new FileIsExistException("File " + currentFile.getName() + " is exist");
95 | }
96 |
97 | if (!currentFile.createNewFile() && !currentFile.exists()) {
98 | throw new IOException("Could not create " + currentFile.getName() + " file");
99 | }
100 | return new FileOutputStream(currentFile);
101 | }
102 |
103 | private void downloading(String url, OutputStream output) throws IOException {
104 | InputStream input = null;
105 | if (output == null) {
106 | output = new ByteArrayOutputStream();
107 | }
108 | try {
109 | final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
110 | connection.connect();
111 | if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
112 | throw new BadResponseException("Server return " + connection.getResponseCode() + " " + connection.getResponseMessage());
113 | }
114 | final int fileLength = connection.getContentLength();
115 | input = new BufferedInputStream(connection.getInputStream());
116 | final byte[] data = new byte[1024];
117 | long total = 0;
118 | int count;
119 | int progress;
120 | int lastProgress = 0;
121 | while ((count = input.read(data)) != -1) {
122 | total += count;
123 | progress = (int) (total * 100 / fileLength);
124 | if (progress - lastProgress != 0 && progress != 100) {
125 | final Bundle resultData = new Bundle();
126 | lastProgress = progress;
127 | resultData.putInt(BundleConst.PROGRESS, progress);
128 | sendTrail(ReceiveCode.ON_PROGRESS, resultData);
129 | }
130 | Logger.debug("Uploaded", total, "from", fileLength, "== progress", progress, "%");
131 | output.write(data, 0, count);
132 | }
133 | output.flush();
134 | } finally {
135 | closeStream(input);
136 | closeStream(output);
137 | }
138 |
139 | final Bundle resultData = new Bundle();
140 | resultData.putInt(BundleConst.PROGRESS, 100);
141 | sendTrail(ReceiveCode.ON_PROGRESS, resultData);
142 |
143 | if (output instanceof ByteArrayOutputStream) {
144 | Logger.debug("Sources of", fileName, "are redirected to bytes");
145 | resultData.putByteArray(BundleConst.BYTES, ((ByteArrayOutputStream) output).toByteArray());
146 | resultData.putString(BundleConst.FILE_NAME, fileName);
147 | sendTrail(ReceiveCode.RECEIVED_FILE_SOURCE, resultData);
148 | } else {
149 | Logger.debug("Sources of", fileName, "are redirected to file");
150 | resultData.putSerializable(BundleConst.FILE, currentFile);
151 | sendTrail(ReceiveCode.RECEIVED_FILE, resultData);
152 | }
153 | }
154 |
155 | private void sendTrail(ReceiveCode receiveCode, Bundle resultData) {
156 | if (receiver != null) {
157 | receiver.send(receiveCode.getCode(), resultData);
158 | }
159 | }
160 |
161 | private void closeStream(Closeable closeable) {
162 | if (closeable != null) {
163 | try {
164 | closeable.close();
165 | } catch (IOException e) {
166 | e.printStackTrace();
167 | }
168 | }
169 | }
170 |
171 | public LoadManager skipIfFileExist(boolean isSkipIfFileExist) {
172 | this.isSkipIfFileExist = isSkipIfFileExist;
173 | return this;
174 | }
175 |
176 | public LoadManager abortNextIfError(boolean isAbortNextIfError) {
177 | this.isAbortNextIfError = isAbortNextIfError;
178 | return this;
179 | }
180 |
181 | public LoadManager redownloadAttemptCount(int redownloadAttemptCount) {
182 | if (redownloadAttemptCount > 0) {
183 | this.redownloadAttemptCount = redownloadAttemptCount;
184 | }
185 | return this;
186 | }
187 | }
188 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/core/Loader.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.core;
2 |
3 | import android.app.Notification;
4 | import android.content.Context;
5 | import android.os.Handler;
6 |
7 | import java.io.File;
8 | import java.util.ArrayList;
9 |
10 | import com.zerobranch.loader.callbacks.lifecycle.OnCompleted;
11 | import com.zerobranch.loader.callbacks.lifecycle.OnError;
12 | import com.zerobranch.loader.callbacks.lifecycle.OnProgress;
13 | import com.zerobranch.loader.callbacks.lifecycle.OnStart;
14 | import com.zerobranch.loader.callbacks.receiving.ReceivedFile;
15 | import com.zerobranch.loader.callbacks.receiving.ReceivedFileSource;
16 | import com.zerobranch.loader.utils.Validator;
17 |
18 | public final class Loader {
19 | private final String DEFAULT_PATH;
20 | private Context context;
21 | private Core core;
22 | private Configurator configurator;
23 |
24 | private Loader(Context context) {
25 | this.context = context;
26 | Validator.getNonNull(context, "Context should not be null");
27 | DEFAULT_PATH = context.getCacheDir().getAbsolutePath();
28 | }
29 |
30 | public static Loader with(Context context) {
31 | return new Loader(context);
32 | }
33 |
34 | private Loader build(Configurator configurator) {
35 | core = new Core();
36 | core.build(configurator, context);
37 | return this;
38 | }
39 |
40 | public Configurator addInQueue(String url) {
41 | configurator = new Configurator(url);
42 | return configurator;
43 | }
44 |
45 | public void cancel() {
46 | core.cancel();
47 | context = null;
48 | }
49 |
50 | public void unsubscribe() {
51 | core.unsubscribe();
52 | }
53 |
54 | public final class ReceivedConfig {
55 | private ReceivedFile receivedFile;
56 | private ReceivedFileSource receivedFileSource;
57 | private OnStart onStart;
58 | private OnCompleted onCompleted;
59 | private OnProgress onProgress;
60 | private OnError onError;
61 |
62 | public ReceivedConfig receivedFile(ReceivedFile receivedFile) {
63 | this.receivedFile = receivedFile;
64 | return this;
65 | }
66 |
67 | public ReceivedConfig receivedFileSource(ReceivedFileSource receivedFileSource) {
68 | this.receivedFileSource = receivedFileSource;
69 | return this;
70 | }
71 |
72 | public ReceivedConfig onStart(OnStart onStart) {
73 | this.onStart = onStart;
74 | return this;
75 | }
76 |
77 | public ReceivedConfig onCompleted(OnCompleted onCompleted) {
78 | this.onCompleted = onCompleted;
79 | return this;
80 | }
81 |
82 | public ReceivedConfig onProgress(OnProgress onProgress) {
83 | this.onProgress = onProgress;
84 | return this;
85 | }
86 |
87 | public ReceivedConfig onError(OnError onError) {
88 | this.onError = onError;
89 | return this;
90 | }
91 |
92 | public Loader load() {
93 | return configurator.load();
94 | }
95 |
96 | ReceivedFile getReceivedFile() {
97 | return receivedFile;
98 | }
99 |
100 | ReceivedFileSource getReceivedFileSource() {
101 | return receivedFileSource;
102 | }
103 |
104 | OnStart getOnStart() {
105 | return onStart;
106 | }
107 |
108 | OnCompleted getOnCompleted() {
109 | return onCompleted;
110 | }
111 |
112 | OnProgress getOnProgress() {
113 | return onProgress;
114 | }
115 |
116 | OnError getOnError() {
117 | return onError;
118 | }
119 | }
120 |
121 | public final class Configurator {
122 | private final ArrayList urls;
123 | private String path = DEFAULT_PATH;
124 | private Notification notification;
125 | private DownloadReceiver downloadReceiver;
126 | private ReceivedConfig receivedConfig;
127 | private boolean isHideDefaultNotification;
128 | private boolean isEnableLogging;
129 | private boolean isViewNotificationOnFinish;
130 | private boolean isImmortal;
131 | private boolean isSkipCache;
132 | private boolean isSkipIfFileExist;
133 | private boolean isAbortNextIfError;
134 | private int redownloadAttemptCount;
135 |
136 | private Configurator(String url) {
137 | urls = new ArrayList<>();
138 | addInQueue(url);
139 | }
140 |
141 | public Configurator to(String path) {
142 | this.path = path;
143 | return this;
144 | }
145 |
146 | public Configurator to(File file) {
147 | this.path = file.getAbsolutePath();
148 | return this;
149 | }
150 |
151 | public Configurator skipCache() {
152 | this.isSkipCache = true;
153 | return this;
154 | }
155 |
156 | public Configurator enableLogging() {
157 | this.isEnableLogging = true;
158 | return this;
159 | }
160 |
161 | public Configurator makeImmortal() {
162 | isImmortal = true;
163 | return this;
164 | }
165 |
166 | @Deprecated
167 | public Configurator hideDefaultNotification() {
168 | isHideDefaultNotification = true;
169 | return this;
170 | }
171 |
172 | public Configurator viewNotificationOnFinish() {
173 | this.isViewNotificationOnFinish = true;
174 | return this;
175 | }
176 |
177 | public Configurator skipIfFileExist() {
178 | isSkipIfFileExist = true;
179 | return this;
180 | }
181 |
182 | public Configurator redownloadAttemptCount(int redownloadAttemptCount) {
183 | this.redownloadAttemptCount = redownloadAttemptCount;
184 | return this;
185 | }
186 |
187 | public Configurator abortNextIfError() {
188 | isAbortNextIfError = true;
189 | return this;
190 | }
191 |
192 | public Configurator notification(Notification notification) {
193 | this.notification = notification;
194 | return this;
195 | }
196 |
197 | public Configurator addInQueue(String url) {
198 | urls.add(url.trim());
199 | return this;
200 | }
201 |
202 | public ReceivedConfig downloadReceiver() {
203 | this.downloadReceiver = new DownloadReceiver();
204 | receivedConfig = new ReceivedConfig();
205 | return receivedConfig;
206 | }
207 |
208 | public ReceivedConfig downloadReceiver(Handler handler) {
209 | this.downloadReceiver = new DownloadReceiver(handler);
210 | receivedConfig = new ReceivedConfig();
211 | return receivedConfig;
212 | }
213 |
214 | DownloadReceiver getDownloadReceiver() {
215 | return downloadReceiver;
216 | }
217 |
218 | ReceivedConfig getReceivedConfig() {
219 | return receivedConfig;
220 | }
221 |
222 | ArrayList getUrls() {
223 | return urls;
224 | }
225 |
226 | String getPath() {
227 | return path;
228 | }
229 |
230 | Notification getNotification() {
231 | return notification;
232 | }
233 |
234 | boolean isHideDefaultNotification() {
235 | return isHideDefaultNotification;
236 | }
237 |
238 | boolean isViewNotificationOnFinish() {
239 | return isViewNotificationOnFinish;
240 | }
241 |
242 | boolean isImmortal() {
243 | return isImmortal;
244 | }
245 |
246 | boolean isSkipCache() {
247 | return isSkipCache;
248 | }
249 |
250 | boolean isEnableLogging() {
251 | return isEnableLogging;
252 | }
253 |
254 | boolean isAbortNextIfError() {
255 | return isAbortNextIfError;
256 | }
257 |
258 | int getRedownloadAttemptCount() {
259 | return redownloadAttemptCount;
260 | }
261 |
262 | boolean isSkipIfFileExist() {
263 | return isSkipIfFileExist;
264 | }
265 |
266 | public Loader load() {
267 | return Loader.this.build(this);
268 | }
269 | }
270 | }
271 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/exception/BadResponseException.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.exception;
2 |
3 | import java.io.IOException;
4 |
5 | public class BadResponseException extends IOException {
6 | public BadResponseException(String message) {
7 | super(message);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/exception/FileIsExistException.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.exception;
2 |
3 | import java.io.IOException;
4 |
5 | public class FileIsExistException extends IOException {
6 | public FileIsExistException(String message) {
7 | super(message);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/service/HideNotificationService.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.service;
2 |
3 | import android.app.Notification;
4 | import android.app.Service;
5 | import android.content.Intent;
6 | import android.os.IBinder;
7 |
8 | public class HideNotificationService extends Service {
9 | @Override
10 | public IBinder onBind(Intent intent) {
11 | return null;
12 | }
13 |
14 | @Override
15 | @SuppressWarnings("deprecation")
16 | public void onCreate() {
17 | startForeground(LoaderService.DEFAULT_NOTIFICATION_ID, new Notification.Builder(this).build());
18 | stopForeground(true);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/service/LoaderService.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.service;
2 |
3 | import android.app.Notification;
4 | import android.app.Service;
5 | import android.content.Intent;
6 | import android.os.Handler;
7 | import android.os.HandlerThread;
8 | import android.os.IBinder;
9 | import android.os.Looper;
10 | import android.os.Message;
11 | import android.os.ResultReceiver;
12 |
13 | import java.util.ArrayList;
14 |
15 | import com.zerobranch.loader.core.LoadManager;
16 | import com.zerobranch.loader.utils.BundleConst;
17 |
18 | public class LoaderService extends Service {
19 | private volatile Looper serviceLooper;
20 | private volatile Handler handler;
21 | public static final int DEFAULT_NOTIFICATION_ID = 43534;
22 | private ArrayList urls;
23 |
24 | @Override
25 | public void onCreate() {
26 | super.onCreate();
27 | final HandlerThread thread = new HandlerThread(LoaderService.class.getName());
28 | thread.start();
29 | serviceLooper = thread.getLooper();
30 | handler = new Handler(serviceLooper, handlerCallback);
31 | }
32 |
33 | @Override
34 | public int onStartCommand(Intent intent, int flags, int startId) {
35 | urls = intent.getStringArrayListExtra(BundleConst.URL);
36 | for (int i = 0; i < urls.size(); i++) {
37 | final Message msg = handler.obtainMessage();
38 | msg.arg1 = startId;
39 | msg.arg2 = i;
40 | msg.obj = intent;
41 | handler.sendMessage(msg);
42 | }
43 | return START_NOT_STICKY;
44 | }
45 |
46 | @SuppressWarnings("deprecation")
47 | private void setImmortal(Intent intent) {
48 | final boolean isHideNotification = intent.getBooleanExtra(BundleConst.DEFAULT_NOTIFICATION, false);
49 | final boolean isImmortal = intent.getBooleanExtra(BundleConst.IMMORTAL, false);
50 | final Notification notification = intent.getParcelableExtra(BundleConst.NOTIFICATION);
51 | if (isImmortal) {
52 | if (notification != null) {
53 | startForeground(DEFAULT_NOTIFICATION_ID, notification);
54 | } else {
55 | startForeground(DEFAULT_NOTIFICATION_ID, new Notification.Builder(this).build());
56 | if (isHideNotification) {
57 | startService(new Intent(this, HideNotificationService.class));
58 | }
59 | }
60 | }
61 | }
62 |
63 | @Override
64 | public void onDestroy() {
65 | serviceLooper.quit();
66 | serviceLooper.getThread().interrupt();
67 | }
68 |
69 | @Override
70 | public IBinder onBind(Intent intent) {
71 | return null;
72 | }
73 |
74 | private final Handler.Callback handlerCallback = new Handler.Callback() {
75 | @Override
76 | public boolean handleMessage(Message msg) {
77 | final Intent intent = (Intent) msg.obj;
78 | final int urlIndex = msg.arg2;
79 | setImmortal(intent);
80 | onHandleIntent(intent, urls.get(urlIndex));
81 | stopForeground(intent.getBooleanExtra(BundleConst.VIEW_NOTIFICATION_ON_FINISH, false));
82 | if (urlIndex == urls.size() - 1) {
83 | stopSelf(msg.arg1);
84 | }
85 | return true;
86 | }
87 | };
88 |
89 | private void onHandleIntent(Intent intent, String url) {
90 | final String path = intent.getStringExtra(BundleConst.PATH);
91 | final ResultReceiver receiver = intent.getParcelableExtra(BundleConst.RECEIVER);
92 | new LoadManager().skipIfFileExist(intent.getBooleanExtra(BundleConst.SKIP_IF_EXIST, false))
93 | .abortNextIfError(intent.getBooleanExtra(BundleConst.ABORT_IF_ERROR, false))
94 | .redownloadAttemptCount(intent.getIntExtra(BundleConst.REDOWNLOAD_COUNT, 0))
95 | .loadFile(path, url, receiver);
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/utils/BundleConst.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.utils;
2 |
3 | public interface BundleConst {
4 | String URL = "url";
5 | String PATH = "path";
6 | String RECEIVER = "receiver";
7 | String IMMORTAL = "immortal";
8 | String DEFAULT_NOTIFICATION = "default_notification";
9 | String VIEW_NOTIFICATION_ON_FINISH = "view_notification_on_finish";
10 | String NOTIFICATION = "notification";
11 | String THROWABLE = "throwable";
12 | String FILE_NAME = "file_name";
13 | String PROGRESS = "progress";
14 | String FILE = "file";
15 | String BYTES = "bytes";
16 | String SKIP_IF_EXIST = "skit_if_exist";
17 | String ABORT_IF_ERROR = "break_if_error";
18 | String REDOWNLOAD_COUNT = "redownload_count";
19 | }
20 |
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/utils/Logger.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.utils;
2 |
3 | import android.util.Log;
4 |
5 | public class Logger {
6 | private static boolean isEnableLogging;
7 | private static final String tag = "Loader";
8 |
9 | public static void disableLogging() {
10 | isEnableLogging = false;
11 | }
12 |
13 | public static void enableLogging() {
14 | isEnableLogging = true;
15 | }
16 |
17 | public static void debug(Object... args) {
18 | if (args == null) return;
19 | final StringBuilder text = new StringBuilder();
20 | for (Object arg : args) {
21 | text.append(arg);
22 | text.append(" ");
23 | }
24 | d(text.toString());
25 | }
26 |
27 | public static void error(Object... args) {
28 | if (args == null) return;
29 | final StringBuilder text = new StringBuilder();
30 | for (Object arg : args) {
31 | text.append(arg);
32 | text.append(" ");
33 | }
34 | e(text.toString());
35 | }
36 |
37 | public static void error(String text, Throwable throwable) {
38 | e(text, throwable);
39 | }
40 |
41 | private static void d(String text) {
42 | if (isEnableLogging) Log.d(tag, text);
43 | }
44 |
45 | private static void e(String text) {
46 | if (isEnableLogging) Log.e(tag, text);
47 | }
48 |
49 | private static void e(String text, Throwable throwable) {
50 | if (isEnableLogging) Log.e(tag, text, throwable);
51 | }
52 | }
--------------------------------------------------------------------------------
/loader/src/main/java/com/zerobranch/loader/utils/Validator.java:
--------------------------------------------------------------------------------
1 | package com.zerobranch.loader.utils;
2 |
3 | import java.util.List;
4 |
5 | public class Validator {
6 |
7 | public static T getNonNull(T o, String throwMessage) {
8 | if (o == null)
9 | throw new NullPointerException(throwMessage);
10 | return o;
11 | }
12 |
13 | public static String getNonEmptyValue(String s, String throwMessage) {
14 | getNonNull(s, throwMessage);
15 | if (s.isEmpty())
16 | throw new IllegalArgumentException(throwMessage);
17 | return s;
18 | }
19 |
20 | public static int getNotNegative(int val, String throwMessage) {
21 | if (val < 0)
22 | throw new IllegalArgumentException(throwMessage);
23 | return val;
24 | }
25 |
26 | public static List> getNonEmptyValue(List> list, String throwMessage) {
27 | getNonNull(list, throwMessage);
28 | if (list.isEmpty())
29 | throw new IllegalArgumentException(throwMessage);
30 | return list;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/loader/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | loader
3 |
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':loader'
2 |
--------------------------------------------------------------------------------