├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── xml
│ │ │ │ └── provider_path.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── input_dilaog.xml
│ │ │ │ └── download_row.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── furthergrow
│ │ │ │ └── android_download_manager
│ │ │ │ ├── ItemClickListener.java
│ │ │ │ ├── MyApplication.java
│ │ │ │ ├── DownloadModel.java
│ │ │ │ ├── PathUtil.java
│ │ │ │ ├── DownloadAdapter.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── furthergrow
│ │ │ └── android_download_manager
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── furthergrow
│ │ └── android_download_manager
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── vcs.xml
├── misc.xml
├── runConfigurations.xml
├── gradle.xml
├── jarRepositories.xml
└── codeStyles
│ └── Project.xml
├── Screenshot_2020-05-21-16-30-12-760_com.furthergrow.android_download_manager.jpg
├── .gitignore
├── README.md
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name='Android_Download_Manager'
2 | include ':app'
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Android_Download_Manager
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Screenshot_2020-05-21-16-30-12-760_com.furthergrow.android_download_manager.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackstarsj/Android_Download_Manager_Part_2/HEAD/Screenshot_2020-05-21-16-30-12-760_com.furthergrow.android_download_manager.jpg
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/java/com/furthergrow/android_download_manager/ItemClickListener.java:
--------------------------------------------------------------------------------
1 | package com.furthergrow.android_download_manager;
2 |
3 | public interface ItemClickListener {
4 | void onCLickItem(String file_path);
5 | void onShareClick(DownloadModel downloadModel);
6 | }
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu May 21 07:15:00 IST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android_Download_Manager_Part_2
2 | Android Downloader Manager With Recyclerview and Realm
3 |
4 | Screenshot
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/java/com/furthergrow/android_download_manager/MyApplication.java:
--------------------------------------------------------------------------------
1 | package com.furthergrow.android_download_manager;
2 |
3 | import android.app.Application;
4 |
5 | import io.realm.Realm;
6 |
7 | public class MyApplication extends Application {
8 | @Override
9 | public void onCreate() {
10 | super.onCreate();
11 | Realm.init(this);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/furthergrow/android_download_manager/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.furthergrow.android_download_manager;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/provider_path.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
9 |
12 |
15 |
18 |
19 |
22 |
25 |
28 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
16 |
17 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/input_dilaog.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
10 |
15 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/furthergrow/android_download_manager/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.furthergrow.android_download_manager;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.furthergrow.android_download_manager", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'realm-android'
3 |
4 | android {
5 | compileSdkVersion 29
6 | buildToolsVersion "29.0.2"
7 |
8 | defaultConfig {
9 | applicationId "com.furthergrow.android_download_manager"
10 | minSdkVersion 16
11 | targetSdkVersion 29
12 | versionCode 1
13 | versionName "1.0"
14 |
15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(dir: 'libs', include: ['*.jar'])
29 |
30 | implementation 'androidx.appcompat:appcompat:1.1.0'
31 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
32 | testImplementation 'junit:junit:4.12'
33 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
35 | implementation 'androidx.recyclerview:recyclerview:1.1.0'
36 | implementation 'androidx.cardview:cardview:1.0.0'
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/furthergrow/android_download_manager/DownloadModel.java:
--------------------------------------------------------------------------------
1 | package com.furthergrow.android_download_manager;
2 |
3 | import io.realm.RealmObject;
4 | import io.realm.annotations.PrimaryKey;
5 |
6 | public class DownloadModel extends RealmObject {
7 |
8 | @PrimaryKey
9 | long id;
10 | long downloadId;
11 | String title;
12 | String file_path;
13 | String progress;
14 | String status;
15 | String file_size;
16 | boolean is_paused;
17 |
18 |
19 |
20 | public long getId() {
21 | return id;
22 | }
23 |
24 | public void setId(long id) {
25 | this.id = id;
26 | }
27 |
28 | public long getDownloadId() {
29 | return downloadId;
30 | }
31 |
32 | public void setDownloadId(long downloadId) {
33 | this.downloadId = downloadId;
34 | }
35 |
36 | public String getTitle() {
37 | return title;
38 | }
39 |
40 | public void setTitle(String title) {
41 | this.title = title;
42 | }
43 |
44 | public String getFile_path() {
45 | return file_path;
46 | }
47 |
48 | public void setFile_path(String file_path) {
49 | this.file_path = file_path;
50 | }
51 |
52 | public String getProgress() {
53 | return progress;
54 | }
55 |
56 | public void setProgress(String progress) {
57 | this.progress = progress;
58 | }
59 |
60 | public String getStatus() {
61 | return status;
62 | }
63 |
64 | public void setStatus(String status) {
65 | this.status = status;
66 | }
67 |
68 | public String getFile_size() {
69 | return file_size;
70 | }
71 |
72 | public void setFile_size(String file_size) {
73 | this.file_size = file_size;
74 | }
75 |
76 | public boolean isIs_paused() {
77 | return is_paused;
78 | }
79 |
80 | public void setIs_paused(boolean is_paused) {
81 | this.is_paused = is_paused;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
49 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | xmlns:android
14 |
15 | ^$
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | xmlns:.*
25 |
26 | ^$
27 |
28 |
29 | BY_NAME
30 |
31 |
32 |
33 |
34 |
35 |
36 | .*:id
37 |
38 | http://schemas.android.com/apk/res/android
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | .*:name
48 |
49 | http://schemas.android.com/apk/res/android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | name
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | style
70 |
71 | ^$
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | .*
81 |
82 | ^$
83 |
84 |
85 | BY_NAME
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 | ANDROID_ATTRIBUTE_ORDER
98 |
99 |
100 |
101 |
102 |
103 |
104 | .*
105 |
106 | .*
107 |
108 |
109 | BY_NAME
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/download_row.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
10 |
15 |
21 |
27 |
34 |
40 |
44 |
51 |
59 |
60 |
61 |
67 |
74 |
81 |
82 |
83 |
89 |
90 |
--------------------------------------------------------------------------------
/app/src/main/java/com/furthergrow/android_download_manager/PathUtil.java:
--------------------------------------------------------------------------------
1 | package com.furthergrow.android_download_manager;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.ContentUris;
5 | import android.content.Context;
6 | import android.database.Cursor;
7 | import android.net.Uri;
8 | import android.os.Build;
9 | import android.os.Environment;
10 | import android.provider.DocumentsContract;
11 | import android.provider.MediaStore;
12 |
13 | import java.net.URISyntaxException;
14 |
15 | public class PathUtil {
16 | @SuppressLint("NewApi")
17 | public static String getPath(Context context, Uri uri) throws URISyntaxException {
18 | final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
19 | String selection = null;
20 | String[] selectionArgs = null;
21 |
22 | // Uri is different in versions after KITKAT (Android 4.4), we need to
23 | // deal with different Uris.
24 |
25 | if (needToCheckUri && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri))
26 | {
27 | if (isExternalStorageDocument(uri)) {
28 | final String docId = DocumentsContract.getDocumentId(uri);
29 | final String[] split = docId.split(":");
30 | return Environment.getExternalStorageDirectory() + "/" + split[1];
31 | } else if (isDownloadsDocument(uri)) {
32 | final String id = DocumentsContract.getDocumentId(uri);
33 | try{
34 | uri = ContentUris.withAppendedId(
35 | Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
36 | }
37 | catch (NumberFormatException n){
38 | uri = ContentUris.withAppendedId(
39 | Uri.parse("content://downloads/public_downloads"),0);
40 | }
41 | } else if (isMediaDocument(uri)) {
42 | final String docId = DocumentsContract.getDocumentId(uri);
43 | final String[] split = docId.split(":");
44 | final String type = split[0];
45 | if ("image".equals(type)) {
46 | uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
47 | } else if ("video".equals(type)) {
48 | uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
49 | } else if ("audio".equals(type)) {
50 | uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
51 | }
52 | selection = "_id=?";
53 | selectionArgs = new String[]{ split[1] };
54 | }
55 | }
56 | if ("content".equalsIgnoreCase(uri.getScheme())) {
57 | String[] projection = { MediaStore.Images.Media.DATA };
58 | Cursor cursor = null;
59 | try {
60 | cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
61 | int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
62 | if (cursor.moveToFirst()) {
63 | return cursor.getString(column_index);
64 | }
65 | } catch (Exception e) {
66 | }
67 | } else if ("file".equalsIgnoreCase(uri.getScheme())) {
68 | return uri.getPath();
69 | }
70 | return null;
71 | }
72 |
73 |
74 | /**
75 | * @param uri The Uri to check.
76 | * @return Whether the Uri authority is ExternalStorageProvider.
77 | */
78 | public static boolean isExternalStorageDocument(Uri uri) {
79 | return "com.android.externalstorage.documents".equals(uri.getAuthority());
80 | }
81 |
82 |
83 |
84 | /**
85 | * @param uri The Uri to check.
86 | * @return Whether the Uri authority is DownloadsProvider.
87 | */
88 | public static boolean isDownloadsDocument(Uri uri) {
89 | return "com.android.providers.downloads.documents".equals(uri.getAuthority());
90 | }
91 |
92 | /**
93 | * @param uri The Uri to check.
94 | * @return Whether the Uri authority is MediaProvider.
95 | */
96 | public static boolean isMediaDocument(Uri uri) {
97 | return "com.android.providers.media.documents".equals(uri.getAuthority());
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/furthergrow/android_download_manager/DownloadAdapter.java:
--------------------------------------------------------------------------------
1 | package com.furthergrow.android_download_manager;
2 |
3 | import android.content.ContentValues;
4 | import android.content.Context;
5 | import android.net.Uri;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.Button;
10 | import android.widget.ProgressBar;
11 | import android.widget.RelativeLayout;
12 | import android.widget.TextView;
13 | import android.widget.Toast;
14 |
15 | import androidx.annotation.NonNull;
16 | import androidx.recyclerview.widget.RecyclerView;
17 |
18 | import org.w3c.dom.Text;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | import io.realm.Realm;
24 |
25 | public class DownloadAdapter extends RecyclerView.Adapter {
26 |
27 | Context context;
28 | List downloadModels=new ArrayList<>();
29 | ItemClickListener clickListener;
30 |
31 | public DownloadAdapter(Context context,List downloadModels,ItemClickListener itemClickListener){
32 | this.context=context;
33 | this.clickListener=itemClickListener;
34 | this.downloadModels=downloadModels;
35 | }
36 |
37 | public class DownloadViewHolder extends RecyclerView.ViewHolder{
38 |
39 | TextView file_title;
40 | TextView file_size;
41 | ProgressBar file_progress;
42 | Button pause_resume,sharefile;
43 | TextView file_status;
44 | RelativeLayout main_rel;
45 |
46 | public DownloadViewHolder(@NonNull View itemView) {
47 | super(itemView);
48 | file_title=itemView.findViewById(R.id.file_title);
49 | file_size=itemView.findViewById(R.id.file_size);
50 | file_status=itemView.findViewById(R.id.file_status);
51 | file_progress=itemView.findViewById(R.id.file_progress);
52 | pause_resume=itemView.findViewById(R.id.pause_resume);
53 | main_rel=itemView.findViewById(R.id.main_rel);
54 | sharefile=itemView.findViewById(R.id.sharefile);
55 |
56 | }
57 | }
58 |
59 |
60 | @NonNull
61 | @Override
62 | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
63 | RecyclerView.ViewHolder vh;
64 | View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.download_row,parent,false);
65 | vh=new DownloadViewHolder(view);
66 | return vh;
67 | }
68 |
69 | @Override
70 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
71 | final DownloadModel downloadModel=downloadModels.get(position);
72 | final DownloadViewHolder downloadViewHolder= (DownloadViewHolder) holder;
73 |
74 | downloadViewHolder.file_title.setText(downloadModel.getTitle());
75 | downloadViewHolder.file_status.setText(downloadModel.getStatus());
76 | downloadViewHolder.file_progress.setProgress(Integer.parseInt(downloadModel.getProgress()));
77 | downloadViewHolder.file_size.setText("Downloaded : "+downloadModel.getFile_size());
78 |
79 | if(downloadModel.isIs_paused()){
80 | downloadViewHolder.pause_resume.setText("RESUME");
81 | }
82 | else{
83 | downloadViewHolder.pause_resume.setText("PAUSE");
84 | }
85 |
86 | if(downloadModel.getStatus().equalsIgnoreCase("RESUME")){
87 | downloadViewHolder.file_status.setText("Running");
88 | }
89 |
90 |
91 | downloadViewHolder.pause_resume.setOnClickListener(new View.OnClickListener() {
92 | @Override
93 | public void onClick(View v) {
94 | if(downloadModel.isIs_paused()){
95 | downloadModel.setIs_paused(false);
96 | downloadViewHolder.pause_resume.setText("PAUSE");
97 | downloadModel.setStatus("RESUME");
98 | downloadViewHolder.file_status.setText("Running");
99 | if(!resumeDownload(downloadModel)){
100 | Toast.makeText(context, "Failed to Resume", Toast.LENGTH_SHORT).show();
101 | }
102 | notifyItemChanged(position);
103 | }
104 | else {
105 | downloadModel.setIs_paused(true);
106 | downloadViewHolder.pause_resume.setText("RESUME");
107 | downloadModel.setStatus("PAUSE");
108 | downloadViewHolder.file_status.setText("PAUSE");
109 | if(!pauseDownload(downloadModel)){
110 | Toast.makeText(context, "Failed to Pause", Toast.LENGTH_SHORT).show();
111 | }
112 | notifyItemChanged(position);
113 | }
114 | }
115 | });
116 |
117 | downloadViewHolder.main_rel.setOnClickListener(new View.OnClickListener() {
118 | @Override
119 | public void onClick(View v) {
120 | clickListener.onCLickItem(downloadModel.getFile_path());
121 | }
122 | });
123 |
124 | downloadViewHolder.sharefile.setOnClickListener(new View.OnClickListener() {
125 | @Override
126 | public void onClick(View v) {
127 | clickListener.onShareClick(downloadModel);
128 | }
129 | });
130 |
131 | }
132 |
133 | private boolean pauseDownload(DownloadModel downloadModel) {
134 | int updatedRow=0;
135 | ContentValues contentValues=new ContentValues();
136 | contentValues.put("control",1);
137 |
138 | try{
139 | updatedRow=context.getContentResolver().update(Uri.parse("content://downloads/my_downloads"),contentValues,"title=?",new String[]{downloadModel.getTitle()});
140 | }
141 | catch (Exception e){
142 | e.printStackTrace();
143 | }
144 | return 0 downloadModels=new ArrayList<>();
50 | Realm realm;
51 |
52 | @Override
53 | protected void onCreate(Bundle savedInstanceState) {
54 | super.onCreate(savedInstanceState);
55 | setContentView(R.layout.activity_main);
56 | realm=Realm.getDefaultInstance();
57 | registerReceiver(onComplete,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
58 | Button add_download_list=findViewById(R.id.add_download_list);
59 | RecyclerView data_list=findViewById(R.id.data_list);
60 |
61 | add_download_list.setOnClickListener(new View.OnClickListener() {
62 | @Override
63 | public void onClick(View v) {
64 | showInputDialog();
65 | }
66 | });
67 |
68 | List downloadModelsLocal=getAllDownloads();
69 | if(downloadModelsLocal!=null){
70 | if(downloadModelsLocal.size()>0){
71 | downloadModels.addAll(downloadModelsLocal);
72 | for(int i=0;i imageList=intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
132 | if(imageList!=null) {
133 | for (Uri uri : imageList) {
134 | Log.d("Path ",""+uri.getPath());
135 | }
136 | }
137 | }
138 |
139 |
140 | private void showInputDialog(){
141 | AlertDialog.Builder al=new AlertDialog.Builder(MainActivity.this);
142 | View view=getLayoutInflater().inflate(R.layout.input_dilaog,null);
143 | al.setView(view);
144 |
145 |
146 | final EditText editText=view.findViewById(R.id.input);
147 | Button paste=view.findViewById(R.id.paste);
148 |
149 | paste.setOnClickListener(new View.OnClickListener() {
150 | @Override
151 | public void onClick(View v) {
152 | ClipboardManager clipboardManager= (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
153 | try{
154 | CharSequence charSequence=clipboardManager.getPrimaryClip().getItemAt(0).getText();
155 | editText.setText(charSequence);
156 | }
157 | catch (Exception e){
158 | e.printStackTrace();
159 | }
160 | }
161 | });
162 |
163 | al.setPositiveButton("Download", new DialogInterface.OnClickListener() {
164 | @Override
165 | public void onClick(DialogInterface dialog, int which) {
166 | downloadFile(editText.getText().toString());
167 | }
168 | });
169 |
170 | al.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
171 | @Override
172 | public void onClick(DialogInterface dialog, int which) {
173 |
174 | }
175 | });
176 | al.show();
177 |
178 | }
179 |
180 | private void downloadFile(String url) {
181 | String filename= URLUtil.guessFileName(url,null,null);
182 | String downloadPath= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
183 |
184 | File file=new File(downloadPath,filename);
185 |
186 | DownloadManager.Request request=null;
187 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
188 | request=new DownloadManager.Request(Uri.parse(url))
189 | .setTitle(filename)
190 | .setDescription("Downloading")
191 | .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
192 | .setDestinationUri(Uri.fromFile(file))
193 | .setRequiresCharging(false)
194 | .setAllowedOverMetered(true)
195 | .setAllowedOverRoaming(true);
196 | }
197 | else{
198 | request=new DownloadManager.Request(Uri.parse(url))
199 | .setTitle(filename)
200 | .setDescription("Downloading")
201 | .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
202 | .setDestinationUri(Uri.fromFile(file))
203 | .setAllowedOverMetered(true)
204 | .setAllowedOverRoaming(true);
205 | }
206 |
207 | DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
208 | long downloadId=downloadManager.enqueue(request);
209 |
210 | Number currentnum=realm.where(DownloadModel.class).max("id");
211 | int nextId;
212 |
213 | if(currentnum==null){
214 | nextId=1;
215 | }
216 | else{
217 | nextId=currentnum.intValue()+1;
218 | }
219 | final DownloadModel downloadModel=new DownloadModel();
220 | downloadModel.setId(nextId);
221 | downloadModel.setStatus("Downloading");
222 | downloadModel.setTitle(filename);
223 | downloadModel.setFile_size("0");
224 | downloadModel.setProgress("0");
225 | downloadModel.setIs_paused(false);
226 | downloadModel.setDownloadId(downloadId);
227 | downloadModel.setFile_path("");
228 |
229 | downloadModels.add(downloadModel);
230 | downloadAdapter.notifyItemInserted(downloadModels.size()-1);
231 |
232 | realm.executeTransaction(new Realm.Transaction() {
233 | @Override
234 | public void execute(Realm realm) {
235 | realm.copyToRealm(downloadModel);
236 | }
237 | });
238 |
239 |
240 | DownloadStatusTask downloadStatusTask=new DownloadStatusTask(downloadModel);
241 | runTask(downloadStatusTask,""+downloadId);
242 |
243 |
244 | }
245 |
246 | @Override
247 | public void onCLickItem(String file_path) {
248 | Log.d("File Path : ",""+file_path);
249 | openFile(file_path);
250 | }
251 |
252 | @Override
253 | public void onShareClick(DownloadModel downloadModel) {
254 | File file=new File(downloadModel.getFile_path().replaceAll("file:///",""));
255 | Log.d("File Path",""+file.getAbsolutePath());
256 | MimeTypeMap mimeTypeMap=MimeTypeMap.getSingleton();
257 | String ext=MimeTypeMap.getFileExtensionFromUrl(file.getName());
258 | String type=mimeTypeMap.getExtensionFromMimeType(ext);
259 |
260 | if(type==null){
261 | type="*/*";
262 | }
263 |
264 | try{
265 | Intent intent=new Intent(Intent.ACTION_SEND);
266 | intent.putExtra(Intent.EXTRA_TEXT,"Sharing File from File Downloader");
267 |
268 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
269 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
270 | Uri path=FileProvider.getUriForFile(MainActivity.this,"com.furthergrow.android_download_manager",file);
271 | intent.putExtra(Intent.EXTRA_STREAM,path);
272 | }
273 | else{
274 | intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
275 | }
276 | intent.setType("*/*");
277 | startActivity(intent);
278 | }
279 | catch (Exception e){
280 | e.printStackTrace();
281 | Toast.makeText(this, "No Activity Availabe to Handle File", Toast.LENGTH_SHORT).show();
282 | }
283 |
284 | }
285 |
286 | public class DownloadStatusTask extends AsyncTask{
287 |
288 | DownloadModel downloadModel;
289 | public DownloadStatusTask(DownloadModel downloadModel){
290 | this.downloadModel=downloadModel;
291 | }
292 |
293 | @Override
294 | protected String doInBackground(String... strings) {
295 | downloadFileProcess(strings[0]);
296 | return null;
297 | }
298 |
299 | private void downloadFileProcess(String downloadId) {
300 | DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
301 | boolean downloading=true;
302 | while (downloading){
303 | DownloadManager.Query query=new DownloadManager.Query();
304 | query.setFilterById(Long.parseLong(downloadId));
305 | Cursor cursor=downloadManager.query(query);
306 | cursor.moveToFirst();
307 |
308 | int bytes_downloaded=cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
309 | int total_size=cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
310 |
311 | if(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))==DownloadManager.STATUS_SUCCESSFUL){
312 | downloading=false;
313 | }
314 |
315 |
316 | int progress= (int) ((bytes_downloaded*100L)/total_size);
317 | String status=getStatusMessage(cursor);
318 | publishProgress(new String[]{String.valueOf(progress), String.valueOf(bytes_downloaded),status});
319 | cursor.close();
320 | }
321 |
322 | }
323 |
324 | @Override
325 | protected void onProgressUpdate(final String... values) {
326 | super.onProgressUpdate(values);
327 | realm.executeTransaction(new Realm.Transaction() {
328 | @Override
329 | public void execute(Realm realm) {
330 |
331 | downloadModel.setFile_size(bytesIntoHumanReadable(Long.parseLong(values[1])));
332 | downloadModel.setProgress(values[0]);
333 | if (!downloadModel.getStatus().equalsIgnoreCase("PAUSE") && !downloadModel.getStatus().equalsIgnoreCase("RESUME")) {
334 | downloadModel.setStatus(values[2]);
335 | }
336 | downloadAdapter.changeItem(downloadModel.getDownloadId());
337 |
338 | }
339 | });
340 |
341 | }
342 | }
343 |
344 | private String getStatusMessage(Cursor cursor) {
345 | String msg="-";
346 | switch (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))){
347 | case DownloadManager.STATUS_FAILED:
348 | msg="Failed";
349 | break;
350 | case DownloadManager.STATUS_PAUSED:
351 | msg= "Paused";
352 | break;
353 | case DownloadManager.STATUS_RUNNING:
354 | msg= "Running";
355 | break;
356 | case DownloadManager.STATUS_SUCCESSFUL:
357 | msg= "Completed";
358 | break;
359 | case DownloadManager.STATUS_PENDING:
360 | msg= "Pending";
361 | break;
362 | default:
363 | msg="Unknown";
364 | break;
365 | }
366 | return msg;
367 | }
368 |
369 | BroadcastReceiver onComplete=new BroadcastReceiver() {
370 | @Override
371 | public void onReceive(Context context, Intent intent) {
372 | long id=intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
373 | boolean comp=downloadAdapter.ChangeItemWithStatus("Completed",id);
374 |
375 | if(comp){
376 | DownloadManager.Query query=new DownloadManager.Query();
377 | query.setFilterById(id);
378 | DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
379 | Cursor cursor=downloadManager.query(new DownloadManager.Query().setFilterById(id));
380 | cursor.moveToFirst();
381 |
382 | String downloaded_path=cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
383 | downloadAdapter.setChangeItemFilePath(downloaded_path,id);
384 | }
385 | }
386 | };
387 |
388 | @Override
389 | protected void onDestroy() {
390 | super.onDestroy();
391 | unregisterReceiver(onComplete);
392 | }
393 |
394 | public void runTask(DownloadStatusTask downloadStatusTask,String id){
395 | try{
396 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
397 | downloadStatusTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,new String[]{id});
398 | }
399 | else{
400 | downloadStatusTask.execute(new String[]{id});
401 | }
402 | }
403 | catch (Exception e){
404 | e.printStackTrace();
405 | }
406 | }
407 |
408 | private String bytesIntoHumanReadable(long bytes) {
409 | long kilobyte = 1024;
410 | long megabyte = kilobyte * 1024;
411 | long gigabyte = megabyte * 1024;
412 | long terabyte = gigabyte * 1024;
413 |
414 | if ((bytes >= 0) && (bytes < kilobyte)) {
415 | return bytes + " B";
416 |
417 | } else if ((bytes >= kilobyte) && (bytes < megabyte)) {
418 | return (bytes / kilobyte) + " KB";
419 |
420 | } else if ((bytes >= megabyte) && (bytes < gigabyte)) {
421 | return (bytes / megabyte) + " MB";
422 |
423 | } else if ((bytes >= gigabyte) && (bytes < terabyte)) {
424 | return (bytes / gigabyte) + " GB";
425 |
426 | } else if (bytes >= terabyte) {
427 | return (bytes / terabyte) + " TB";
428 |
429 | } else {
430 | return bytes + " Bytes";
431 | }
432 | }
433 |
434 | private RealmResults getAllDownloads(){
435 | Realm realm=Realm.getDefaultInstance();
436 | return realm.where(DownloadModel.class).findAll();
437 | }
438 |
439 | private void requestPermission(){
440 | if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
441 | Toast.makeText(MainActivity.this, "Please Give Permission to Upload File", Toast.LENGTH_SHORT).show();
442 | }
443 | else{
444 | ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE);
445 | }
446 | }
447 |
448 | private boolean checkPermission(){
449 | int result= ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
450 | if(result== PackageManager.PERMISSION_GRANTED){
451 | return true;
452 | }
453 | else{
454 | return false;
455 | }
456 | }
457 |
458 | @Override
459 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
460 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
461 | switch (requestCode){
462 | case PERMISSION_REQUEST_CODE:
463 | if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
464 | Toast.makeText(MainActivity.this, "Permission Successfull", Toast.LENGTH_SHORT).show();
465 | }
466 | else{
467 | Toast.makeText(MainActivity.this, "Permission Failed", Toast.LENGTH_SHORT).show();
468 | }
469 | }
470 | }
471 |
472 | private void openFile(String fileurl){
473 | if(Build.VERSION.SDK_INT>=23){
474 | if(!checkPermission()){
475 | requestPermission();
476 | Toast.makeText(this, "Please Allow Permission to Open File", Toast.LENGTH_SHORT).show();
477 | return;
478 | }
479 |
480 | }
481 |
482 | try{
483 | fileurl=PathUtil.getPath(MainActivity.this,Uri.parse(fileurl));
484 |
485 | File file=new File(fileurl);
486 | MimeTypeMap mimeTypeMap=MimeTypeMap.getSingleton();
487 | String ext=MimeTypeMap.getFileExtensionFromUrl(file.getName());
488 | String type=mimeTypeMap.getMimeTypeFromExtension(ext);
489 |
490 | if(type==null){
491 | type="*/*";
492 | }
493 |
494 | Intent intent=new Intent(Intent.ACTION_VIEW);
495 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
496 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
497 | Uri contne= FileProvider.getUriForFile(MainActivity.this,"com.furthergrow.android_download_manager",file);
498 | intent.setDataAndType(contne,type);
499 | }
500 | else{
501 | intent.setDataAndType(Uri.fromFile(file),type);
502 | }
503 | startActivity(intent);
504 | }
505 | catch (Exception e){
506 | Toast.makeText(this, "Unable to Open File", Toast.LENGTH_SHORT).show();
507 | e.printStackTrace();
508 | }
509 |
510 | }
511 |
512 | }
513 |
--------------------------------------------------------------------------------