add flutter

This commit is contained in:
Ariska
2026-03-11 15:29:37 +07:00
parent c253e1a370
commit 619d758027
9490 changed files with 135801 additions and 1353 deletions

View File

@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
/.idea

View File

@@ -0,0 +1,9 @@
# Contributing
1. Fork it!
2. Checkout the development branch: `git checkout development`
3. Create your feature branch: `git checkout -b my-new-feature`
4. Add your changes to the index: `git add .`
5. Commit your changes: `git commit -m 'Add some feature'`
6. Push to the branch: `git push origin my-new-feature`
7. Submit a pull request against the `development` branch

View File

@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,190 @@
<p align="center">
<img alt="PRDownloader" src=https://raw.githubusercontent.com/amitshekhariitbhu/PRDownloader/master/assets/prdownloader.png />
</p>
# PRDownloader - A file downloader library for Android with pause and resume support
## Sample Download
<img src=https://raw.githubusercontent.com/amitshekhariitbhu/PRDownloader/master/assets/sample_download.png width=360 height=640 />
### Overview of PRDownloader library
* PRDownloader can be used to download any type of files like image, video, pdf, apk and etc.
* This file downloader library supports pause and resume while downloading a file.
* Supports large file download.
* This downloader library has a simple interface to make download request.
* We can check if the status of downloading with the given download Id.
* PRDownloader gives callbacks for everything like onProgress, onCancel, onStart, onError and etc while downloading a file.
* Supports proper request canceling.
* Many requests can be made in parallel.
* All types of customization are possible.
## About me
Hi, I am Amit Shekhar, Founder @ [Outcome School](https://outcomeschool.com) • IIT 2010-14 • I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
### Follow Amit Shekhar
- [X/Twitter](https://twitter.com/amitiitbhu)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)
### Follow Outcome School
- [YouTube](https://youtube.com/@OutcomeSchool)
- [X/Twitter](https://x.com/outcome_school)
- [LinkedIn](https://www.linkedin.com/company/outcomeschool)
- [GitHub](http://github.com/OutcomeSchool)
## I teach at Outcome School
- AI and Machine Learning
- Android
Join Outcome School and get a high-paying tech job: [Outcome School](https://outcomeschool.com)
## Using PRDownloader Library in your Android application
Add this in your `settings.gradle`:
```groovy
maven { url 'https://jitpack.io' }
```
If you are using `settings.gradle.kts`, add the following:
```kotlin
maven { setUrl("https://jitpack.io") }
```
Add this in your `build.gradle`
```groovy
implementation 'com.github.amitshekhariitbhu:PRDownloader:1.0.2'
```
If you are using `build.gradle.kts`, add the following:
```kotlin
implementation("com.github.amitshekhariitbhu:PRDownloader:1.0.2")
```
Do not forget to add internet permission in manifest if already not present
```xml
<uses-permission android:name="android.permission.INTERNET" />
```
Then initialize it in onCreate() Method of application class :
```java
PRDownloader.initialize(getApplicationContext());
```
Initializing it with some customization
```java
// Enabling database for resume support even after the application is killed:
PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
.setDatabaseEnabled(true)
.build();
PRDownloader.initialize(getApplicationContext(), config);
// Setting timeout globally for the download network requests:
PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
.setReadTimeout(30_000)
.setConnectTimeout(30_000)
.build();
PRDownloader.initialize(getApplicationContext(), config);
```
### Make a download request
```java
int downloadId = PRDownloader.download(url, dirPath, fileName)
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
@Override
public void onStartOrResume() {
}
})
.setOnPauseListener(new OnPauseListener() {
@Override
public void onPause() {
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel() {
}
})
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(Progress progress) {
}
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
}
@Override
public void onError(Error error) {
}
});
```
### Pause a download request
```java
PRDownloader.pause(downloadId);
```
### Resume a download request
```java
PRDownloader.resume(downloadId);
```
### Cancel a download request
```java
// Cancel with the download id
PRDownloader.cancel(downloadId);
// The tag can be set to any request and then can be used to cancel the request
PRDownloader.cancel(TAG);
// Cancel all the requests
PRDownloader.cancelAll();
```
### Status of a download request
```java
Status status = PRDownloader.getStatus(downloadId);
```
### Clean up resumed files if database enabled
```java
// Method to clean up temporary resumed files which is older than the given day
PRDownloader.cleanUp(days);
```
### TODO
* Integration with other libraries like OkHttp, RxJava
* Test Cases
* And of course many many features and bug fixes
## [Outcome School Blog](https://outcomeschool.com/blog) - High-quality content to learn Android concepts.
## If this library helps you in anyway, show your love :heart: by putting a :star: on this project :v:
### License
```
Copyright (C) 2024 Amit Shekhar
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
### Contributing to PRDownloader
All pull requests are welcome, make sure to follow the [contribution guidelines](CONTRIBUTING.md)
when you submit pull request.

View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,31 @@
apply plugin: 'com.android.application'
android {
namespace 'com.sample'
defaultConfig {
applicationId "com.sample"
minSdkVersion 14
targetSdkVersion 35
compileSdk 35
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
debugImplementation 'com.github.amitshekhariitbhu.Android-Debug-Database:debug-db:1.0.7'
implementation project(':prdownloader')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,26 @@
package com.sample;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.sample", appContext.getPackageName());
}
}

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".SampleApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,23 @@
package com.sample;
import android.app.Application;
import com.downloader.PRDownloader;
import com.downloader.PRDownloaderConfig;
/**
* Created by amitshekhar on 13/11/17.
*/
public class SampleApp extends Application {
@Override
public void onCreate() {
super.onCreate();
PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
.setDatabaseEnabled(true)
.build();
PRDownloader.initialize(this, config);
}
}

View File

@@ -0,0 +1,38 @@
package com.sample.utils;
import android.content.Context;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import java.io.File;
import java.util.Locale;
/**
* Created by amitshekhar on 13/11/17.
*/
public final class Utils {
private Utils() {
// no instance
}
public static String getRootDirPath(Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(),
null)[0];
return file.getAbsolutePath();
} else {
return context.getApplicationContext().getFilesDir().getAbsolutePath();
}
}
public static String getProgressDisplayLine(long currentBytes, long totalBytes) {
return getBytesToMBString(currentBytes) + "/" + getBytesToMBString(totalBytes);
}
private static String getBytesToMBString(long bytes) {
return String.format(Locale.ENGLISH, "%.2fMb", bytes / (1024.00 * 1024.00));
}
}

View File

@@ -0,0 +1,34 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeColor="#00000000"
android:strokeWidth="1">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeColor="#00000000"
android:strokeWidth="1" />
</vector>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
<stroke android:width="1dp"
android:color="@android:color/background_dark"/>
<solid android:color="@android:color/transparent" />
</shape>

View File

@@ -0,0 +1,171 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportHeight="108"
android:viewportWidth="108">
<path
android:fillColor="#26A69A"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,59L108,59"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,69L108,69"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,79L108,79"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,89L108,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,99L108,99"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,29L89,29"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,39L89,39"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,49L89,49"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,59L89,59"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,69L89,69"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,79L89,79"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M29,19L29,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M39,19L39,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M49,19L49,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M59,19L59,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M69,19L69,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M79,19L79,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
</vector>

View File

@@ -0,0 +1,765 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sample.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarOne"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarOne"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarTwo"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarTwo"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarThree"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarThree"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressThree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelThree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonThree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarFour"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarFour"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressFour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelFour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonFour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarFive"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarFive"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressFive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelFive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonFive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarSix"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarSix"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressSix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelSix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonSix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarSeven"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarSeven"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressSeven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelSeven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonSeven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarEight"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarEight"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressEight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelEight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonEight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarNine"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarNine"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressNine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelNine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonNine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarTen"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarTen"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressTen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelTen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonTen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarEleven"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarEleven"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressEleven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelEleven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonEleven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarTwelve"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarTwelve"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressTwelve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelTwelve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonTwelve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarThirteen"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarThirteen"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressThirteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelThirteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonThirteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarFourteen"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarFourteen"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressFourteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelFourteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonFourteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border_black">
<ProgressBar
android:id="@+id/progressBarFifteen"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:progressTint="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBarFifteen"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewProgressFifteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAlignment="center" />
<Button
android:id="@+id/buttonCancelFifteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:enabled="false"
android:text="@string/cancel" />
<Button
android:id="@+id/buttonFifteen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:text="@string/start" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>

View File

@@ -0,0 +1,10 @@
<resources>
<string name="app_name">PRDownloader</string>
<string name="some_error_occurred">Some error occurred</string>
<string name="start">start</string>
<string name="completed">completed</string>
<string name="pause">pause</string>
<string name="cancel">cancel</string>
<string name="resume">resume</string>
<string name="restart">restart</string>
</resources>

View File

@@ -0,0 +1,11 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

View File

@@ -0,0 +1,17 @@
package com.sample;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@@ -0,0 +1,28 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:8.7.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
tasks.register('clean', Delete) {
delete rootProject.layout.buildDirectory
}

View File

@@ -0,0 +1,18 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.useAndroidX=true

View File

@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

252
OnTime_Driver_live/lib/PRDownloader/gradlew vendored Executable file
View File

@@ -0,0 +1,252 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

View File

@@ -0,0 +1,94 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@@ -0,0 +1,2 @@
jdk:
- openjdk17

View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,28 @@
apply plugin: 'com.android.library'
android {
namespace 'com.downloader'
compileSdk 33
defaultConfig {
minSdkVersion 21
targetSdkVersion 33
versionCode 1
versionName "1.0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,26 @@
package com.downloader;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.downloader.test", appContext.getPackageName());
}
}

View File

@@ -0,0 +1,26 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public final class Constants {
private Constants() {
// no instance
}
public static final int UPDATE = 0x01;
public static final String RANGE = "Range";
public static final String ETAG = "ETag";
public static final String USER_AGENT = "User-Agent";
public static final String DEFAULT_USER_AGENT = "PRDownloader";
public static final int DEFAULT_READ_TIMEOUT_IN_MILLS = 20_000;
public static final int DEFAULT_CONNECT_TIMEOUT_IN_MILLS = 20_000;
public static final int HTTP_RANGE_NOT_SATISFIABLE = 416;
public static final int HTTP_TEMPORARY_REDIRECT = 307;
public static final int HTTP_PERMANENT_REDIRECT = 308;
}

View File

@@ -0,0 +1,66 @@
package com.downloader;
import java.util.List;
import java.util.Map;
/**
* Created by amitshekhar on 13/11/17.
*/
public class Error {
private boolean isServerError;
private boolean isConnectionError;
private String serverErrorMessage;
private Map<String, List<String>> headerFields;
private Throwable connectionException;
private int responseCode;
public boolean isServerError() {
return isServerError;
}
public void setServerError(boolean serverError) {
isServerError = serverError;
}
public boolean isConnectionError() {
return isConnectionError;
}
public void setConnectionError(boolean connectionError) {
isConnectionError = connectionError;
}
public void setServerErrorMessage(String serverErrorMessage) {
this.serverErrorMessage = serverErrorMessage;
}
public String getServerErrorMessage() {
return serverErrorMessage;
}
public void setHeaderFields(Map<String, List<String>> headerFields) {
this.headerFields = headerFields;
}
public Map<String, List<String>> getHeaderFields() {
return headerFields;
}
public void setConnectionException(Throwable connectionException) {
this.connectionException = connectionException;
}
public Throwable getConnectionException() {
return connectionException;
}
public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
}
public int getResponseCode() {
return responseCode;
}
}

View File

@@ -0,0 +1,11 @@
package com.downloader;
/**
* Created by amitshekhar on 15/11/17.
*/
public interface OnCancelListener {
void onCancel();
}

View File

@@ -0,0 +1,13 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface OnDownloadListener {
void onDownloadComplete();
void onError(Error error);
}

View File

@@ -0,0 +1,11 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface OnPauseListener {
void onPause();
}

View File

@@ -0,0 +1,11 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface OnProgressListener {
void onProgress(Progress progress);
}

View File

@@ -0,0 +1,11 @@
package com.downloader;
/**
* Created by amitshekhar on 15/11/17.
*/
public interface OnStartOrResumeListener {
void onStartOrResume();
}

View File

@@ -0,0 +1,129 @@
package com.downloader;
/**
* Created by amitshekhar on 12/11/17.
*/
import android.content.Context;
import com.downloader.core.Core;
import com.downloader.internal.ComponentHolder;
import com.downloader.internal.DownloadRequestQueue;
import com.downloader.request.DownloadRequestBuilder;
import com.downloader.utils.Utils;
/**
* PRDownloader entry point.
* You must initialize this class before use. The simplest way is to just do
* {#code PRDownloader.initialize(context)}.
*/
public class PRDownloader {
/**
* private constructor to prevent instantiation of this class
*/
private PRDownloader() {
}
/**
* Initializes PRDownloader with the default config.
*
* @param context The context
*/
public static void initialize(Context context) {
initialize(context, PRDownloaderConfig.newBuilder().build());
}
/**
* Initializes PRDownloader with the custom config.
*
* @param context The context
* @param config The PRDownloaderConfig
*/
public static void initialize(Context context, PRDownloaderConfig config) {
ComponentHolder.getInstance().init(context, config);
DownloadRequestQueue.initialize();
}
/**
* Method to make download request
*
* @param url The url on which request is to be made
* @param dirPath The directory path on which file is to be saved
* @param fileName The file name with which file is to be saved
* @return the DownloadRequestBuilder
*/
public static DownloadRequestBuilder download(String url, String dirPath, String fileName) {
return new DownloadRequestBuilder(url, dirPath, fileName);
}
/**
* Method to pause request with the given downloadId
*
* @param downloadId The downloadId with which request is to be paused
*/
public static void pause(int downloadId) {
DownloadRequestQueue.getInstance().pause(downloadId);
}
/**
* Method to resume request with the given downloadId
*
* @param downloadId The downloadId with which request is to be resumed
*/
public static void resume(int downloadId) {
DownloadRequestQueue.getInstance().resume(downloadId);
}
/**
* Method to cancel request with the given downloadId
*
* @param downloadId The downloadId with which request is to be cancelled
*/
public static void cancel(int downloadId) {
DownloadRequestQueue.getInstance().cancel(downloadId);
}
/**
* Method to cancel requests with the given tag
*
* @param tag The tag with which requests are to be cancelled
*/
public static void cancel(Object tag) {
DownloadRequestQueue.getInstance().cancel(tag);
}
/**
* Method to cancel all requests
*/
public static void cancelAll() {
DownloadRequestQueue.getInstance().cancelAll();
}
/**
* Method to check the request with the given downloadId is running or not
*
* @param downloadId The downloadId with which request status is to be checked
* @return the running status
*/
public static Status getStatus(int downloadId) {
return DownloadRequestQueue.getInstance().getStatus(downloadId);
}
/**
* Method to clean up temporary resumed files which is older than the given day
*
* @param days the days
*/
public static void cleanUp(int days) {
Utils.deleteUnwantedModelsAndTempFiles(days);
}
/**
* Shuts PRDownloader down
*/
public static void shutDown() {
Core.shutDown();
}
}

View File

@@ -0,0 +1,107 @@
package com.downloader;
import com.downloader.httpclient.DefaultHttpClient;
import com.downloader.httpclient.HttpClient;
/**
* Created by amitshekhar on 13/11/17.
*/
public class PRDownloaderConfig {
private int readTimeout;
private int connectTimeout;
private String userAgent;
private HttpClient httpClient;
private boolean databaseEnabled;
private PRDownloaderConfig(Builder builder) {
this.readTimeout = builder.readTimeout;
this.connectTimeout = builder.connectTimeout;
this.userAgent = builder.userAgent;
this.httpClient = builder.httpClient;
this.databaseEnabled = builder.databaseEnabled;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public HttpClient getHttpClient() {
return httpClient;
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
public boolean isDatabaseEnabled() {
return databaseEnabled;
}
public void setDatabaseEnabled(boolean databaseEnabled) {
this.databaseEnabled = databaseEnabled;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
int readTimeout = Constants.DEFAULT_READ_TIMEOUT_IN_MILLS;
int connectTimeout = Constants.DEFAULT_CONNECT_TIMEOUT_IN_MILLS;
String userAgent = Constants.DEFAULT_USER_AGENT;
HttpClient httpClient = new DefaultHttpClient();
boolean databaseEnabled = false;
public Builder setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public Builder setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public Builder setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
public Builder setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}
public Builder setDatabaseEnabled(boolean databaseEnabled) {
this.databaseEnabled = databaseEnabled;
return this;
}
public PRDownloaderConfig build() {
return new PRDownloaderConfig(this);
}
}
}

View File

@@ -0,0 +1,32 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
/**
* Priority levels recognized by the request server.
*/
public enum Priority {
/**
* Lowest priority level. Used for prefetches of data.
*/
LOW,
/**
* Medium priority level. Used for warming of data that might soon get visible.
*/
MEDIUM,
/**
* Highest priority level. Used for data that are currently visible on screen.
*/
HIGH,
/**
* Highest priority level. Used for data that are required instantly(mainly for emergency).
*/
IMMEDIATE
}

View File

@@ -0,0 +1,26 @@
package com.downloader;
import java.io.Serializable;
/**
* Created by amitshekhar on 13/11/17.
*/
public class Progress implements Serializable {
public long currentBytes;
public long totalBytes;
public Progress(long currentBytes, long totalBytes) {
this.currentBytes = currentBytes;
this.totalBytes = totalBytes;
}
@Override
public String toString() {
return "Progress{" +
"currentBytes=" + currentBytes +
", totalBytes=" + totalBytes +
'}';
}
}

View File

@@ -0,0 +1,46 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public class Response {
private Error error;
private boolean isSuccessful;
private boolean isPaused;
private boolean isCancelled;
public Error getError() {
return error;
}
public void setError(Error error) {
this.error = error;
}
public boolean isSuccessful() {
return isSuccessful;
}
public void setSuccessful(boolean successful) {
isSuccessful = successful;
}
public boolean isPaused() {
return isPaused;
}
public void setPaused(boolean paused) {
isPaused = paused;
}
public boolean isCancelled() {
return isCancelled;
}
public void setCancelled(boolean cancelled) {
isCancelled = cancelled;
}
}

View File

@@ -0,0 +1,23 @@
package com.downloader;
/**
* Created by amitshekhar on 15/11/17.
*/
public enum Status {
QUEUED,
RUNNING,
PAUSED,
COMPLETED,
CANCELLED,
FAILED,
UNKNOWN
}

View File

@@ -0,0 +1,36 @@
package com.downloader.core;
/**
* Created by amitshekhar on 13/11/17.
*/
public class Core {
private static Core instance = null;
private final ExecutorSupplier executorSupplier;
private Core() {
this.executorSupplier = new DefaultExecutorSupplier();
}
public static Core getInstance() {
if (instance == null) {
synchronized (Core.class) {
if (instance == null) {
instance = new Core();
}
}
}
return instance;
}
public ExecutorSupplier getExecutorSupplier() {
return executorSupplier;
}
public static void shutDown() {
if (instance != null) {
instance = null;
}
}
}

View File

@@ -0,0 +1,41 @@
package com.downloader.core;
import android.os.Process;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DefaultExecutorSupplier implements ExecutorSupplier {
private static final int DEFAULT_MAX_NUM_THREADS = 2 * Runtime.getRuntime().availableProcessors() + 1;
private final DownloadExecutor networkExecutor;
private final Executor backgroundExecutor;
private final Executor mainThreadExecutor;
DefaultExecutorSupplier() {
ThreadFactory backgroundPriorityThreadFactory = new PriorityThreadFactory(Process.THREAD_PRIORITY_BACKGROUND);
networkExecutor = new DownloadExecutor(DEFAULT_MAX_NUM_THREADS, backgroundPriorityThreadFactory);
backgroundExecutor = Executors.newSingleThreadExecutor();
mainThreadExecutor = new MainThreadExecutor();
}
@Override
public DownloadExecutor forDownloadTasks() {
return networkExecutor;
}
@Override
public Executor forBackgroundTasks() {
return backgroundExecutor;
}
@Override
public Executor forMainThreadTasks() {
return mainThreadExecutor;
}
}

View File

@@ -0,0 +1,27 @@
package com.downloader.core;
import com.downloader.internal.DownloadRunnable;
import java.util.concurrent.Future;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadExecutor extends ThreadPoolExecutor {
DownloadExecutor(int maxNumThreads, ThreadFactory threadFactory) {
super(maxNumThreads, maxNumThreads, 0, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(), threadFactory);
}
@Override
public Future<?> submit(Runnable task) {
DownloadFutureTask futureTask = new DownloadFutureTask((DownloadRunnable) task);
execute(futureTask);
return futureTask;
}
}

View File

@@ -0,0 +1,27 @@
package com.downloader.core;
import com.downloader.Priority;
import com.downloader.internal.DownloadRunnable;
import java.util.concurrent.FutureTask;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadFutureTask extends FutureTask<DownloadRunnable> implements Comparable<DownloadFutureTask> {
private final DownloadRunnable runnable;
DownloadFutureTask(DownloadRunnable downloadRunnable) {
super(downloadRunnable, null);
this.runnable = downloadRunnable;
}
@Override
public int compareTo(DownloadFutureTask other) {
Priority p1 = runnable.priority;
Priority p2 = other.runnable.priority;
return (p1 == p2 ? runnable.sequence - other.runnable.sequence : p2.ordinal() - p1.ordinal());
}
}

View File

@@ -0,0 +1,17 @@
package com.downloader.core;
import java.util.concurrent.Executor;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface ExecutorSupplier {
DownloadExecutor forDownloadTasks();
Executor forBackgroundTasks();
Executor forMainThreadTasks();
}

View File

@@ -0,0 +1,20 @@
package com.downloader.core;
import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Executor;
/**
* Created by amitshekhar on 13/11/17.
*/
public class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void execute(Runnable runnable) {
handler.post(runnable);
}
}

View File

@@ -0,0 +1,34 @@
package com.downloader.core;
import android.os.Process;
import java.util.concurrent.ThreadFactory;
/**
* Created by amitshekhar on 13/11/17.
*/
public class PriorityThreadFactory implements ThreadFactory {
private final int mThreadPriority;
PriorityThreadFactory(int threadPriority) {
mThreadPriority = threadPriority;
}
@Override
public Thread newThread(final Runnable runnable) {
Runnable wrapperRunnable = new Runnable() {
@Override
public void run() {
try {
Process.setThreadPriority(mThreadPriority);
} catch (Throwable ignored) {
}
runnable.run();
}
};
return new Thread(wrapperRunnable);
}
}

View File

@@ -0,0 +1,154 @@
package com.downloader.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by anandgaurav on 14-11-2017.
*/
public class AppDbHelper implements DbHelper {
public static final String TABLE_NAME = "prdownloader";
private final SQLiteDatabase db;
public AppDbHelper(Context context) {
DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(context);
db = databaseOpenHelper.getWritableDatabase();
}
@Override
public DownloadModel find(int id) {
Cursor cursor = null;
DownloadModel model = null;
try {
cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " +
DownloadModel.ID + " = " + id, null);
if (cursor != null && cursor.moveToFirst()) {
model = new DownloadModel();
model.setId(id);
model.setUrl(cursor.getString(cursor.getColumnIndex(DownloadModel.URL)));
model.setETag(cursor.getString(cursor.getColumnIndex(DownloadModel.ETAG)));
model.setDirPath(cursor.getString(cursor.getColumnIndex(DownloadModel.DIR_PATH)));
model.setFileName(cursor.getString(cursor.getColumnIndex(DownloadModel.FILE_NAME)));
model.setTotalBytes(cursor.getLong(cursor.getColumnIndex(DownloadModel.TOTAL_BYTES)));
model.setDownloadedBytes(cursor.getLong(cursor.getColumnIndex(DownloadModel.DOWNLOADED_BYTES)));
model.setLastModifiedAt(cursor.getLong(cursor.getColumnIndex(DownloadModel.LAST_MODIFIED_AT)));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return model;
}
@Override
public void insert(DownloadModel model) {
try {
ContentValues values = new ContentValues();
values.put(DownloadModel.ID, model.getId());
values.put(DownloadModel.URL, model.getUrl());
values.put(DownloadModel.ETAG, model.getETag());
values.put(DownloadModel.DIR_PATH, model.getDirPath());
values.put(DownloadModel.FILE_NAME, model.getFileName());
values.put(DownloadModel.TOTAL_BYTES, model.getTotalBytes());
values.put(DownloadModel.DOWNLOADED_BYTES, model.getDownloadedBytes());
values.put(DownloadModel.LAST_MODIFIED_AT, model.getLastModifiedAt());
db.insert(TABLE_NAME, null, values);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void update(DownloadModel model) {
try {
ContentValues values = new ContentValues();
values.put(DownloadModel.URL, model.getUrl());
values.put(DownloadModel.ETAG, model.getETag());
values.put(DownloadModel.DIR_PATH, model.getDirPath());
values.put(DownloadModel.FILE_NAME, model.getFileName());
values.put(DownloadModel.TOTAL_BYTES, model.getTotalBytes());
values.put(DownloadModel.DOWNLOADED_BYTES, model.getDownloadedBytes());
values.put(DownloadModel.LAST_MODIFIED_AT, model.getLastModifiedAt());
db.update(TABLE_NAME, values, DownloadModel.ID + " = ? ",
new String[]{String.valueOf(model.getId())});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void updateProgress(int id, long downloadedBytes, long lastModifiedAt) {
try {
ContentValues values = new ContentValues();
values.put(DownloadModel.DOWNLOADED_BYTES, downloadedBytes);
values.put(DownloadModel.LAST_MODIFIED_AT, lastModifiedAt);
db.update(TABLE_NAME, values, DownloadModel.ID + " = ? ",
new String[]{String.valueOf(id)});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void remove(int id) {
try {
db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " +
DownloadModel.ID + " = " + id);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public List<DownloadModel> getUnwantedModels(int days) {
List<DownloadModel> models = new ArrayList<>();
Cursor cursor = null;
try {
final long daysInMillis = days * 24 * 60 * 60 * 1000L;
final long beforeTimeInMillis = System.currentTimeMillis() - daysInMillis;
cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " +
DownloadModel.LAST_MODIFIED_AT + " <= " + beforeTimeInMillis, null);
if (cursor != null && cursor.moveToFirst()) {
do {
DownloadModel model = new DownloadModel();
model.setId(cursor.getInt(cursor.getColumnIndex(DownloadModel.ID)));
model.setUrl(cursor.getString(cursor.getColumnIndex(DownloadModel.URL)));
model.setETag(cursor.getString(cursor.getColumnIndex(DownloadModel.ETAG)));
model.setDirPath(cursor.getString(cursor.getColumnIndex(DownloadModel.DIR_PATH)));
model.setFileName(cursor.getString(cursor.getColumnIndex(DownloadModel.FILE_NAME)));
model.setTotalBytes(cursor.getLong(cursor.getColumnIndex(DownloadModel.TOTAL_BYTES)));
model.setDownloadedBytes(cursor.getLong(cursor.getColumnIndex(DownloadModel.DOWNLOADED_BYTES)));
model.setLastModifiedAt(cursor.getLong(cursor.getColumnIndex(DownloadModel.LAST_MODIFIED_AT)));
models.add(model);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return models;
}
@Override
public void clear() {
try {
db.delete(TABLE_NAME, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,40 @@
package com.downloader.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by anandgaurav on 14-11-2017.
*/
public class DatabaseOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "prdownloader.db";
private static final int DATABASE_VERSION = 1;
DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " +
AppDbHelper.TABLE_NAME + "( " +
DownloadModel.ID + " INTEGER PRIMARY KEY, " +
DownloadModel.URL + " VARCHAR, " +
DownloadModel.ETAG + " VARCHAR, " +
DownloadModel.DIR_PATH + " VARCHAR, " +
DownloadModel.FILE_NAME + " VARCHAR, " +
DownloadModel.TOTAL_BYTES + " INTEGER, " +
DownloadModel.DOWNLOADED_BYTES + " INTEGER, " +
DownloadModel.LAST_MODIFIED_AT + " INTEGER " +
")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
}

View File

@@ -0,0 +1,25 @@
package com.downloader.database;
import java.util.List;
/**
* Created by anandgaurav on 14-11-2017.
*/
public interface DbHelper {
DownloadModel find(int id);
void insert(DownloadModel model);
void update(DownloadModel model);
void updateProgress(int id, long downloadedBytes, long lastModifiedAt);
void remove(int id);
List<DownloadModel> getUnwantedModels(int days);
void clear();
}

View File

@@ -0,0 +1,91 @@
package com.downloader.database;
/**
* Created by anandgaurav on 14-11-2017.
*/
public class DownloadModel {
static final String ID = "id";
static final String URL = "url";
static final String ETAG = "etag";
static final String DIR_PATH = "dir_path";
static final String FILE_NAME = "file_name";
static final String TOTAL_BYTES = "total_bytes";
static final String DOWNLOADED_BYTES = "downloaded_bytes";
static final String LAST_MODIFIED_AT = "last_modified_at";
private int id;
private String url;
private String eTag;
private String dirPath;
private String fileName;
private long totalBytes;
private long downloadedBytes;
private long lastModifiedAt;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getETag() {
return eTag;
}
public void setETag(String eTag) {
this.eTag = eTag;
}
public String getDirPath() {
return dirPath;
}
public void setDirPath(String dirPath) {
this.dirPath = dirPath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public long getTotalBytes() {
return totalBytes;
}
public void setTotalBytes(long totalBytes) {
this.totalBytes = totalBytes;
}
public long getDownloadedBytes() {
return downloadedBytes;
}
public void setDownloadedBytes(long downloadedBytes) {
this.downloadedBytes = downloadedBytes;
}
public long getLastModifiedAt() {
return lastModifiedAt;
}
public void setLastModifiedAt(long lastModifiedAt) {
this.lastModifiedAt = lastModifiedAt;
}
}

View File

@@ -0,0 +1,49 @@
package com.downloader.database;
import java.util.List;
/**
* Created by anandgaurav on 14-11-2017.
*/
public class NoOpsDbHelper implements DbHelper {
public NoOpsDbHelper() {
}
@Override
public DownloadModel find(int id) {
return null;
}
@Override
public void insert(DownloadModel model) {
}
@Override
public void update(DownloadModel model) {
}
@Override
public void updateProgress(int id, long downloadedBytes, long lastModifiedAt) {
}
@Override
public void remove(int id) {
}
@Override
public List<DownloadModel> getUnwantedModels(int days) {
return null;
}
@Override
public void clear() {
}
}

View File

@@ -0,0 +1,38 @@
package com.downloader.handler;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.downloader.Constants;
import com.downloader.Progress;
import com.downloader.OnProgressListener;
/**
* Created by amitshekhar on 13/11/17.
*/
public class ProgressHandler extends Handler {
private final OnProgressListener listener;
public ProgressHandler(OnProgressListener listener) {
super(Looper.getMainLooper());
this.listener = listener;
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Constants.UPDATE:
if (listener != null) {
final Progress progress = (Progress) msg.obj;
listener.onProgress(progress);
}
break;
default:
super.handleMessage(msg);
break;
}
}
}

View File

@@ -0,0 +1,111 @@
package com.downloader.httpclient;
import com.downloader.Constants;
import com.downloader.request.DownloadRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DefaultHttpClient implements HttpClient {
private URLConnection connection;
public DefaultHttpClient() {
}
@SuppressWarnings("CloneDoesntCallSuperClone")
@Override
public HttpClient clone() {
return new DefaultHttpClient();
}
@Override
public void connect(DownloadRequest request) throws IOException {
connection = new URL(request.getUrl()).openConnection();
connection.setReadTimeout(request.getReadTimeout());
connection.setConnectTimeout(request.getConnectTimeout());
final String range = String.format(Locale.ENGLISH,
"bytes=%d-", request.getDownloadedBytes());
connection.addRequestProperty(Constants.RANGE, range);
connection.addRequestProperty(Constants.USER_AGENT, request.getUserAgent());
addHeaders(request);
connection.connect();
}
@Override
public int getResponseCode() throws IOException {
int responseCode = 0;
if (connection instanceof HttpURLConnection) {
responseCode = ((HttpURLConnection) connection).getResponseCode();
}
return responseCode;
}
@Override
public InputStream getInputStream() throws IOException {
return connection.getInputStream();
}
@Override
public long getContentLength() {
String length = connection.getHeaderField("Content-Length");
try {
return Long.parseLong(length);
} catch (NumberFormatException e) {
return -1;
}
}
@Override
public String getResponseHeader(String name) {
return connection.getHeaderField(name);
}
@Override
public void close() {
// no operation
}
@Override
public Map<String, List<String>> getHeaderFields() {
return connection.getHeaderFields();
}
@Override
public InputStream getErrorStream() {
if (connection instanceof HttpURLConnection) {
return ((HttpURLConnection) connection).getErrorStream();
}
return null;
}
private void addHeaders(DownloadRequest request) {
final HashMap<String, List<String>> headers = request.getHeaders();
if (headers != null) {
Set<Map.Entry<String, List<String>>> entries = headers.entrySet();
for (Map.Entry<String, List<String>> entry : entries) {
String name = entry.getKey();
List<String> list = entry.getValue();
if (list != null) {
for (String value : list) {
connection.addRequestProperty(name, value);
}
}
}
}
}
}

View File

@@ -0,0 +1,34 @@
package com.downloader.httpclient;
import com.downloader.request.DownloadRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface HttpClient extends Cloneable {
HttpClient clone();
void connect(DownloadRequest request) throws IOException;
int getResponseCode() throws IOException;
InputStream getInputStream() throws IOException;
long getContentLength();
String getResponseHeader(String name);
void close();
Map<String, List<String>> getHeaderFields();
InputStream getErrorStream() throws IOException;
}

View File

@@ -0,0 +1,97 @@
package com.downloader.internal;
import android.content.Context;
import com.downloader.Constants;
import com.downloader.PRDownloader;
import com.downloader.PRDownloaderConfig;
import com.downloader.database.AppDbHelper;
import com.downloader.database.DbHelper;
import com.downloader.database.NoOpsDbHelper;
import com.downloader.httpclient.DefaultHttpClient;
import com.downloader.httpclient.HttpClient;
/**
* Created by amitshekhar on 14/11/17.
*/
public class ComponentHolder {
private final static ComponentHolder INSTANCE = new ComponentHolder();
private int readTimeout;
private int connectTimeout;
private String userAgent;
private HttpClient httpClient;
private DbHelper dbHelper;
public static ComponentHolder getInstance() {
return INSTANCE;
}
public void init(Context context, PRDownloaderConfig config) {
this.readTimeout = config.getReadTimeout();
this.connectTimeout = config.getConnectTimeout();
this.userAgent = config.getUserAgent();
this.httpClient = config.getHttpClient();
this.dbHelper = config.isDatabaseEnabled() ? new AppDbHelper(context) : new NoOpsDbHelper();
if (config.isDatabaseEnabled()) {
PRDownloader.cleanUp(30);
}
}
public int getReadTimeout() {
if (readTimeout == 0) {
synchronized (ComponentHolder.class) {
if (readTimeout == 0) {
readTimeout = Constants.DEFAULT_READ_TIMEOUT_IN_MILLS;
}
}
}
return readTimeout;
}
public int getConnectTimeout() {
if (connectTimeout == 0) {
synchronized (ComponentHolder.class) {
if (connectTimeout == 0) {
connectTimeout = Constants.DEFAULT_CONNECT_TIMEOUT_IN_MILLS;
}
}
}
return connectTimeout;
}
public String getUserAgent() {
if (userAgent == null) {
synchronized (ComponentHolder.class) {
if (userAgent == null) {
userAgent = Constants.DEFAULT_USER_AGENT;
}
}
}
return userAgent;
}
public DbHelper getDbHelper() {
if (dbHelper == null) {
synchronized (ComponentHolder.class) {
if (dbHelper == null) {
dbHelper = new NoOpsDbHelper();
}
}
}
return dbHelper;
}
public HttpClient getHttpClient() {
if (httpClient == null) {
synchronized (ComponentHolder.class) {
if (httpClient == null) {
httpClient = new DefaultHttpClient();
}
}
}
return httpClient.clone();
}
}

View File

@@ -0,0 +1,118 @@
package com.downloader.internal;
import com.downloader.Status;
import com.downloader.core.Core;
import com.downloader.request.DownloadRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadRequestQueue {
private static DownloadRequestQueue instance;
private final Map<Integer, DownloadRequest> currentRequestMap;
private final AtomicInteger sequenceGenerator;
private DownloadRequestQueue() {
currentRequestMap = new ConcurrentHashMap<>();
sequenceGenerator = new AtomicInteger();
}
public static void initialize() {
getInstance();
}
public static DownloadRequestQueue getInstance() {
if (instance == null) {
synchronized (DownloadRequestQueue.class) {
if (instance == null) {
instance = new DownloadRequestQueue();
}
}
}
return instance;
}
private int getSequenceNumber() {
return sequenceGenerator.incrementAndGet();
}
public void pause(int downloadId) {
DownloadRequest request = currentRequestMap.get(downloadId);
if (request != null) {
request.setStatus(Status.PAUSED);
}
}
public void resume(int downloadId) {
DownloadRequest request = currentRequestMap.get(downloadId);
if (request != null) {
request.setStatus(Status.QUEUED);
request.setFuture(Core.getInstance()
.getExecutorSupplier()
.forDownloadTasks()
.submit(new DownloadRunnable(request)));
}
}
private void cancelAndRemoveFromMap(DownloadRequest request) {
if (request != null) {
request.cancel();
currentRequestMap.remove(request.getDownloadId());
}
}
public void cancel(int downloadId) {
DownloadRequest request = currentRequestMap.get(downloadId);
cancelAndRemoveFromMap(request);
}
public void cancel(Object tag) {
for (Map.Entry<Integer, DownloadRequest> currentRequestMapEntry : currentRequestMap.entrySet()) {
DownloadRequest request = currentRequestMapEntry.getValue();
if (request.getTag() instanceof String && tag instanceof String) {
final String tempRequestTag = (String) request.getTag();
final String tempTag = (String) tag;
if (tempRequestTag.equals(tempTag)) {
cancelAndRemoveFromMap(request);
}
} else if (request.getTag().equals(tag)) {
cancelAndRemoveFromMap(request);
}
}
}
public void cancelAll() {
for (Map.Entry<Integer, DownloadRequest> currentRequestMapEntry : currentRequestMap.entrySet()) {
DownloadRequest request = currentRequestMapEntry.getValue();
cancelAndRemoveFromMap(request);
}
}
public Status getStatus(int downloadId) {
DownloadRequest request = currentRequestMap.get(downloadId);
if (request != null) {
return request.getStatus();
}
return Status.UNKNOWN;
}
public void addRequest(DownloadRequest request) {
currentRequestMap.put(request.getDownloadId(), request);
request.setStatus(Status.QUEUED);
request.setSequenceNumber(getSequenceNumber());
request.setFuture(Core.getInstance()
.getExecutorSupplier()
.forDownloadTasks()
.submit(new DownloadRunnable(request)));
}
public void finish(DownloadRequest request) {
currentRequestMap.remove(request.getDownloadId());
}
}

View File

@@ -0,0 +1,41 @@
package com.downloader.internal;
import com.downloader.Error;
import com.downloader.Priority;
import com.downloader.Response;
import com.downloader.Status;
import com.downloader.request.DownloadRequest;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadRunnable implements Runnable {
public final Priority priority;
public final int sequence;
public final DownloadRequest request;
DownloadRunnable(DownloadRequest request) {
this.request = request;
this.priority = request.getPriority();
this.sequence = request.getSequenceNumber();
}
@Override
public void run() {
request.setStatus(Status.RUNNING);
DownloadTask downloadTask = DownloadTask.create(request);
Response response = downloadTask.run();
if (response.isSuccessful()) {
request.deliverSuccess();
} else if (response.isPaused()) {
request.deliverPauseEvent();
} else if (response.getError() != null) {
request.deliverError(response.getError());
} else if (!response.isCancelled()) {
request.deliverError(new Error());
}
}
}

View File

@@ -0,0 +1,390 @@
package com.downloader.internal;
import com.downloader.Constants;
import com.downloader.Error;
import com.downloader.Progress;
import com.downloader.Response;
import com.downloader.Status;
import com.downloader.database.DownloadModel;
import com.downloader.handler.ProgressHandler;
import com.downloader.httpclient.HttpClient;
import com.downloader.internal.stream.FileDownloadOutputStream;
import com.downloader.internal.stream.FileDownloadRandomAccessFile;
import com.downloader.request.DownloadRequest;
import com.downloader.utils.Utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadTask {
private static final int BUFFER_SIZE = 1024 * 4;
private static final long TIME_GAP_FOR_SYNC = 2000;
private static final long MIN_BYTES_FOR_SYNC = 65536;
private final DownloadRequest request;
private ProgressHandler progressHandler;
private long lastSyncTime;
private long lastSyncBytes;
private InputStream inputStream;
private FileDownloadOutputStream outputStream;
private HttpClient httpClient;
private long totalBytes;
private int responseCode;
private String eTag;
private boolean isResumeSupported;
private String tempPath;
private DownloadTask(DownloadRequest request) {
this.request = request;
}
static DownloadTask create(DownloadRequest request) {
return new DownloadTask(request);
}
Response run() {
Response response = new Response();
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
response.setPaused(true);
return response;
}
try {
if (request.getOnProgressListener() != null) {
progressHandler = new ProgressHandler(request.getOnProgressListener());
}
tempPath = Utils.getTempPath(request.getDirPath(), request.getFileName());
File file = new File(tempPath);
DownloadModel model = getDownloadModelIfAlreadyPresentInDatabase();
if (model != null) {
if (file.exists()) {
request.setTotalBytes(model.getTotalBytes());
request.setDownloadedBytes(model.getDownloadedBytes());
} else {
removeNoMoreNeededModelFromDatabase();
request.setDownloadedBytes(0);
request.setTotalBytes(0);
model = null;
}
}
httpClient = ComponentHolder.getInstance().getHttpClient();
httpClient.connect(request);
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
response.setPaused(true);
return response;
}
httpClient = Utils.getRedirectedConnectionIfAny(httpClient, request);
responseCode = httpClient.getResponseCode();
eTag = httpClient.getResponseHeader(Constants.ETAG);
if (checkIfFreshStartRequiredAndStart(model)) {
model = null;
}
if (!isSuccessful()) {
Error error = new Error();
error.setServerError(true);
error.setServerErrorMessage(convertStreamToString(httpClient.getErrorStream()));
error.setHeaderFields(httpClient.getHeaderFields());
error.setResponseCode(responseCode);
response.setError(error);
return response;
}
setResumeSupportedOrNot();
totalBytes = request.getTotalBytes();
if (!isResumeSupported) {
deleteTempFile();
}
if (totalBytes == 0) {
totalBytes = httpClient.getContentLength();
request.setTotalBytes(totalBytes);
}
if (isResumeSupported && model == null) {
createAndInsertNewModel();
}
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
response.setPaused(true);
return response;
}
request.deliverStartEvent();
inputStream = httpClient.getInputStream();
byte[] buff = new byte[BUFFER_SIZE];
if (!file.exists()) {
if (file.getParentFile() != null && !file.getParentFile().exists()) {
if (file.getParentFile().mkdirs()) {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
}
} else {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
}
}
this.outputStream = FileDownloadRandomAccessFile.create(file);
if (isResumeSupported && request.getDownloadedBytes() != 0) {
outputStream.seek(request.getDownloadedBytes());
}
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
response.setPaused(true);
return response;
}
do {
final int byteCount = inputStream.read(buff, 0, BUFFER_SIZE);
if (byteCount == -1) {
break;
}
outputStream.write(buff, 0, byteCount);
request.setDownloadedBytes(request.getDownloadedBytes() + byteCount);
sendProgress();
syncIfRequired(outputStream);
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
sync(outputStream);
response.setPaused(true);
return response;
}
} while (true);
final String path = Utils.getPath(request.getDirPath(), request.getFileName());
Utils.renameFileName(tempPath, path);
response.setSuccessful(true);
if (isResumeSupported) {
removeNoMoreNeededModelFromDatabase();
}
} catch (IOException | IllegalAccessException e) {
if (!isResumeSupported) {
deleteTempFile();
}
Error error = new Error();
error.setConnectionError(true);
error.setConnectionException(e);
response.setError(error);
} finally {
closeAllSafely(outputStream);
}
return response;
}
private void deleteTempFile() {
File file = new File(tempPath);
if (file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.delete();
}
}
private boolean isSuccessful() {
return responseCode >= HttpURLConnection.HTTP_OK
&& responseCode < HttpURLConnection.HTTP_MULT_CHOICE;
}
private void setResumeSupportedOrNot() {
isResumeSupported = (responseCode == HttpURLConnection.HTTP_PARTIAL);
}
private boolean checkIfFreshStartRequiredAndStart(DownloadModel model) throws IOException,
IllegalAccessException {
if (responseCode == Constants.HTTP_RANGE_NOT_SATISFIABLE || isETagChanged(model)) {
if (model != null) {
removeNoMoreNeededModelFromDatabase();
}
deleteTempFile();
request.setDownloadedBytes(0);
request.setTotalBytes(0);
httpClient = ComponentHolder.getInstance().getHttpClient();
httpClient.connect(request);
httpClient = Utils.getRedirectedConnectionIfAny(httpClient, request);
responseCode = httpClient.getResponseCode();
return true;
}
return false;
}
private boolean isETagChanged(DownloadModel model) {
return !(eTag == null || model == null || model.getETag() == null)
&& !model.getETag().equals(eTag);
}
private DownloadModel getDownloadModelIfAlreadyPresentInDatabase() {
return ComponentHolder.getInstance().getDbHelper().find(request.getDownloadId());
}
private void createAndInsertNewModel() {
DownloadModel model = new DownloadModel();
model.setId(request.getDownloadId());
model.setUrl(request.getUrl());
model.setETag(eTag);
model.setDirPath(request.getDirPath());
model.setFileName(request.getFileName());
model.setDownloadedBytes(request.getDownloadedBytes());
model.setTotalBytes(totalBytes);
model.setLastModifiedAt(System.currentTimeMillis());
ComponentHolder.getInstance().getDbHelper().insert(model);
}
private void removeNoMoreNeededModelFromDatabase() {
ComponentHolder.getInstance().getDbHelper().remove(request.getDownloadId());
}
private void sendProgress() {
if (request.getStatus() != Status.CANCELLED) {
if (progressHandler != null) {
progressHandler
.obtainMessage(Constants.UPDATE,
new Progress(request.getDownloadedBytes(),
totalBytes)).sendToTarget();
}
}
}
private void syncIfRequired(FileDownloadOutputStream outputStream) {
final long currentBytes = request.getDownloadedBytes();
final long currentTime = System.currentTimeMillis();
final long bytesDelta = currentBytes - lastSyncBytes;
final long timeDelta = currentTime - lastSyncTime;
if (bytesDelta > MIN_BYTES_FOR_SYNC && timeDelta > TIME_GAP_FOR_SYNC) {
sync(outputStream);
lastSyncBytes = currentBytes;
lastSyncTime = currentTime;
}
}
private void sync(FileDownloadOutputStream outputStream) {
boolean success;
try {
outputStream.flushAndSync();
success = true;
} catch (IOException e) {
success = false;
e.printStackTrace();
}
if (success && isResumeSupported) {
ComponentHolder.getInstance().getDbHelper()
.updateProgress(request.getDownloadId(),
request.getDownloadedBytes(),
System.currentTimeMillis());
}
}
private void closeAllSafely(FileDownloadOutputStream outputStream) {
if (httpClient != null) {
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if (outputStream != null) {
try {
sync(outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
} finally {
if (outputStream != null)
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String convertStreamToString(InputStream stream) {
StringBuilder stringBuilder = new StringBuilder();
if (stream != null) {
String line;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(stream));
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException ignored) {
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (NullPointerException | IOException ignored) {
}
}
}
return stringBuilder.toString();
}
}

View File

@@ -0,0 +1,19 @@
package com.downloader.internal;
import com.downloader.Response;
import com.downloader.request.DownloadRequest;
public class SynchronousCall {
public final DownloadRequest request;
public SynchronousCall(DownloadRequest request) {
this.request = request;
}
public Response execute() {
DownloadTask downloadTask = DownloadTask.create(request);
return downloadTask.run();
}
}

View File

@@ -0,0 +1,38 @@
package com.downloader.internal.stream;
import java.io.IOException;
public interface FileDownloadOutputStream {
/**
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this file.
*/
void write(byte b[], int off, int len) throws IOException;
/**
* Flush all buffer to system and force all system buffers to synchronize with the underlying
* device.
*/
void flushAndSync() throws IOException;
/**
* Closes this output stream and releases any system resources associated with this stream. The
* general contract of <code>close</code> is that it closes the output stream. A closed stream
* cannot perform output operations and cannot be reopened.
*/
void close() throws IOException;
/**
* Sets the file-pointer offset, measured from the beginning of this file, at which the next
* read or write occurs. The offset may be set beyond the end of the file.
*/
void seek(long offset) throws IOException, IllegalAccessException;
/**
* Sets the length of this file.
*/
void setLength(final long newLength) throws IOException, IllegalAccessException;
}

View File

@@ -0,0 +1,53 @@
package com.downloader.internal.stream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FileDownloadRandomAccessFile implements FileDownloadOutputStream {
private final BufferedOutputStream out;
private final FileDescriptor fd;
private final RandomAccessFile randomAccess;
private FileDownloadRandomAccessFile(File file) throws IOException {
randomAccess = new RandomAccessFile(file, "rw");
fd = randomAccess.getFD();
out = new BufferedOutputStream(new FileOutputStream(randomAccess.getFD()));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}
@Override
public void flushAndSync() throws IOException {
out.flush();
fd.sync();
}
@Override
public void close() throws IOException {
out.close();
randomAccess.close();
}
@Override
public void seek(long offset) throws IOException {
randomAccess.seek(offset);
}
@Override
public void setLength(long totalBytes) throws IOException {
randomAccess.setLength(totalBytes);
}
public static FileDownloadOutputStream create(File file) throws IOException {
return new FileDownloadRandomAccessFile(file);
}
}

View File

@@ -0,0 +1,319 @@
package com.downloader.request;
import com.downloader.Error;
import com.downloader.OnCancelListener;
import com.downloader.OnDownloadListener;
import com.downloader.OnPauseListener;
import com.downloader.OnProgressListener;
import com.downloader.OnStartOrResumeListener;
import com.downloader.Priority;
import com.downloader.Response;
import com.downloader.Status;
import com.downloader.core.Core;
import com.downloader.internal.ComponentHolder;
import com.downloader.internal.DownloadRequestQueue;
import com.downloader.internal.SynchronousCall;
import com.downloader.utils.Utils;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Future;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadRequest {
private Priority priority;
private Object tag;
private String url;
private String dirPath;
private String fileName;
private int sequenceNumber;
private Future future;
private long downloadedBytes;
private long totalBytes;
private int readTimeout;
private int connectTimeout;
private String userAgent;
private OnProgressListener onProgressListener;
private OnDownloadListener onDownloadListener;
private OnStartOrResumeListener onStartOrResumeListener;
private OnPauseListener onPauseListener;
private OnCancelListener onCancelListener;
private int downloadId;
private HashMap<String, List<String>> headerMap;
private Status status;
DownloadRequest(DownloadRequestBuilder builder) {
this.url = builder.url;
this.dirPath = builder.dirPath;
this.fileName = builder.fileName;
this.headerMap = builder.headerMap;
this.priority = builder.priority;
this.tag = builder.tag;
this.readTimeout =
builder.readTimeout != 0 ?
builder.readTimeout :
getReadTimeoutFromConfig();
this.connectTimeout =
builder.connectTimeout != 0 ?
builder.connectTimeout :
getConnectTimeoutFromConfig();
this.userAgent = builder.userAgent;
}
public Priority getPriority() {
return priority;
}
public void setPriority(Priority priority) {
this.priority = priority;
}
public Object getTag() {
return tag;
}
public void setTag(Object tag) {
this.tag = tag;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDirPath() {
return dirPath;
}
public void setDirPath(String dirPath) {
this.dirPath = dirPath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getSequenceNumber() {
return sequenceNumber;
}
public void setSequenceNumber(int sequenceNumber) {
this.sequenceNumber = sequenceNumber;
}
public HashMap<String, List<String>> getHeaders() {
return headerMap;
}
public Future getFuture() {
return future;
}
public void setFuture(Future future) {
this.future = future;
}
public long getDownloadedBytes() {
return downloadedBytes;
}
public void setDownloadedBytes(long downloadedBytes) {
this.downloadedBytes = downloadedBytes;
}
public long getTotalBytes() {
return totalBytes;
}
public void setTotalBytes(long totalBytes) {
this.totalBytes = totalBytes;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public String getUserAgent() {
if (userAgent == null) {
userAgent = ComponentHolder.getInstance().getUserAgent();
}
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public int getDownloadId() {
return downloadId;
}
public void setDownloadId(int downloadId) {
this.downloadId = downloadId;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public OnProgressListener getOnProgressListener() {
return onProgressListener;
}
public DownloadRequest setOnStartOrResumeListener(OnStartOrResumeListener onStartOrResumeListener) {
this.onStartOrResumeListener = onStartOrResumeListener;
return this;
}
public DownloadRequest setOnProgressListener(OnProgressListener onProgressListener) {
this.onProgressListener = onProgressListener;
return this;
}
public DownloadRequest setOnPauseListener(OnPauseListener onPauseListener) {
this.onPauseListener = onPauseListener;
return this;
}
public DownloadRequest setOnCancelListener(OnCancelListener onCancelListener) {
this.onCancelListener = onCancelListener;
return this;
}
public int start(OnDownloadListener onDownloadListener) {
this.onDownloadListener = onDownloadListener;
downloadId = Utils.getUniqueId(url, dirPath, fileName);
DownloadRequestQueue.getInstance().addRequest(this);
return downloadId;
}
public Response executeSync() {
downloadId = Utils.getUniqueId(url, dirPath, fileName);
return new SynchronousCall(this).execute();
}
public void deliverError(final Error error) {
if (status != Status.CANCELLED) {
setStatus(Status.FAILED);
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onDownloadListener != null) {
onDownloadListener.onError(error);
}
finish();
}
});
}
}
public void deliverSuccess() {
if (status != Status.CANCELLED) {
setStatus(Status.COMPLETED);
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onDownloadListener != null) {
onDownloadListener.onDownloadComplete();
}
finish();
}
});
}
}
public void deliverStartEvent() {
if (status != Status.CANCELLED) {
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onStartOrResumeListener != null) {
onStartOrResumeListener.onStartOrResume();
}
}
});
}
}
public void deliverPauseEvent() {
if (status != Status.CANCELLED) {
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onPauseListener != null) {
onPauseListener.onPause();
}
}
});
}
}
private void deliverCancelEvent() {
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onCancelListener != null) {
onCancelListener.onCancel();
}
}
});
}
public void cancel() {
status = Status.CANCELLED;
if (future != null) {
future.cancel(true);
}
deliverCancelEvent();
Utils.deleteTempFileAndDatabaseEntryInBackground(Utils.getTempPath(dirPath, fileName), downloadId);
}
private void finish() {
destroy();
DownloadRequestQueue.getInstance().finish(this);
}
private void destroy() {
this.onProgressListener = null;
this.onDownloadListener = null;
this.onStartOrResumeListener = null;
this.onPauseListener = null;
this.onCancelListener = null;
}
private int getReadTimeoutFromConfig() {
return ComponentHolder.getInstance().getReadTimeout();
}
private int getConnectTimeoutFromConfig() {
return ComponentHolder.getInstance().getConnectTimeout();
}
}

View File

@@ -0,0 +1,81 @@
package com.downloader.request;
import com.downloader.Priority;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadRequestBuilder implements RequestBuilder {
String url;
String dirPath;
String fileName;
Priority priority = Priority.MEDIUM;
Object tag;
int readTimeout;
int connectTimeout;
String userAgent;
HashMap<String, List<String>> headerMap;
public DownloadRequestBuilder(String url, String dirPath, String fileName) {
this.url = url;
this.dirPath = dirPath;
this.fileName = fileName;
}
@Override
public DownloadRequestBuilder setHeader(String name, String value) {
if (headerMap == null) {
headerMap = new HashMap<>();
}
List<String> list = headerMap.get(name);
if (list == null) {
list = new ArrayList<>();
headerMap.put(name, list);
}
if (!list.contains(value)) {
list.add(value);
}
return this;
}
@Override
public DownloadRequestBuilder setPriority(Priority priority) {
this.priority = priority;
return this;
}
@Override
public DownloadRequestBuilder setTag(Object tag) {
this.tag = tag;
return this;
}
@Override
public DownloadRequestBuilder setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
@Override
public DownloadRequestBuilder setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
@Override
public DownloadRequestBuilder setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
public DownloadRequest build() {
return new DownloadRequest(this);
}
}

View File

@@ -0,0 +1,23 @@
package com.downloader.request;
import com.downloader.Priority;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface RequestBuilder {
RequestBuilder setHeader(String name, String value);
RequestBuilder setPriority(Priority priority);
RequestBuilder setTag(Object tag);
RequestBuilder setReadTimeout(int readTimeout);
RequestBuilder setConnectTimeout(int connectTimeout);
RequestBuilder setUserAgent(String userAgent);
}

View File

@@ -0,0 +1,157 @@
package com.downloader.utils;
import com.downloader.Constants;
import com.downloader.core.Core;
import com.downloader.database.DownloadModel;
import com.downloader.httpclient.HttpClient;
import com.downloader.internal.ComponentHolder;
import com.downloader.request.DownloadRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
/**
* Created by amitshekhar on 13/11/17.
*/
public final class Utils {
private final static int MAX_REDIRECTION = 10;
private Utils() {
// no instance
}
public static String getPath(String dirPath, String fileName) {
return dirPath + File.separator + fileName;
}
public static String getTempPath(String dirPath, String fileName) {
return getPath(dirPath, fileName) + ".temp";
}
public static void renameFileName(String oldPath, String newPath) throws IOException {
final File oldFile = new File(oldPath);
try {
final File newFile = new File(newPath);
if (newFile.exists()) {
if (!newFile.delete()) {
throw new IOException("Deletion Failed");
}
}
if (!oldFile.renameTo(newFile)) {
throw new IOException("Rename Failed");
}
} finally {
if (oldFile.exists()) {
//noinspection ResultOfMethodCallIgnored
oldFile.delete();
}
}
}
public static void deleteTempFileAndDatabaseEntryInBackground(final String path, final int downloadId) {
Core.getInstance().getExecutorSupplier().forBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
ComponentHolder.getInstance().getDbHelper().remove(downloadId);
File file = new File(path);
if (file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.delete();
}
}
});
}
public static void deleteUnwantedModelsAndTempFiles(final int days) {
Core.getInstance().getExecutorSupplier().forBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
List<DownloadModel> models = ComponentHolder.getInstance()
.getDbHelper()
.getUnwantedModels(days);
if (models != null) {
for (DownloadModel model : models) {
final String tempPath = getTempPath(model.getDirPath(), model.getFileName());
ComponentHolder.getInstance().getDbHelper().remove(model.getId());
File file = new File(tempPath);
if (file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.delete();
}
}
}
}
});
}
public static int getUniqueId(String url, String dirPath, String fileName) {
String string = url + File.separator + dirPath + File.separator + fileName;
byte[] hash;
try {
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("NoSuchAlgorithmException", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UnsupportedEncodingException", e);
}
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString().hashCode();
}
public static HttpClient getRedirectedConnectionIfAny(HttpClient httpClient,
DownloadRequest request)
throws IOException, IllegalAccessException {
int redirectTimes = 0;
int code = httpClient.getResponseCode();
String location = httpClient.getResponseHeader("Location");
while (isRedirection(code)) {
if (location == null) {
throw new IllegalAccessException("Location is null");
}
httpClient.close();
request.setUrl(location);
httpClient = ComponentHolder.getInstance().getHttpClient();
httpClient.connect(request);
code = httpClient.getResponseCode();
location = httpClient.getResponseHeader("Location");
redirectTimes++;
if (redirectTimes >= MAX_REDIRECTION) {
throw new IllegalAccessException("Max redirection done");
}
}
return httpClient;
}
private static boolean isRedirection(int code) {
return code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_MOVED_TEMP
|| code == HttpURLConnection.HTTP_SEE_OTHER
|| code == HttpURLConnection.HTTP_MULT_CHOICE
|| code == Constants.HTTP_TEMPORARY_REDIRECT
|| code == Constants.HTTP_PERMANENT_REDIRECT;
}
}

View File

@@ -0,0 +1,3 @@
<resources>
<string name="app_name">downloader</string>
</resources>

View File

@@ -0,0 +1,17 @@
package com.downloader;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}

View File

@@ -0,0 +1 @@
include ':app', ':prdownloader'