You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
GDownload is a simple, powerful, easy to use, customizable file download client library for Android.
Features
Simple and easy to use API.
Continuous downloading in the background.
Concurrent downloading support.
Ability to pause and resume downloads.
Set the priority of a download.
Network-specific downloading support.
Ability to retry failed downloads.
Ability to group downloads.
Easy progress and status tracking.
Download remaining time reporting (ETA).
Download speed reporting.
Download Elapsed time reporting.
Save and Retrieve download information anytime.
Scope Storage support.
And more...
Prerequisites
If you are saving downloads outside of your application's sandbox, you will need toadd the following storage permissions to your application's manifest. For Android SDK version23(M) and above, you will also need to explicitly request these permissions from the user.
And then add the dependency to the module level build.gradle file.
implementation'com.github.tanoDxyz:GDownload:1.2'
Initalization
Although! library can be used independently without initalization but initalization gives us two benefits. First, it will reuse the existing downloaders from the pool if available. Second, if the app is closed and the user wants to stop all the downloads running this will help out.
GDownload.init(lifecycle)//activity.lifecycle or any other lifecycle // could be null too
The above line will initalize the library and if the non null Lifecycle is passed the library will terminate all the running or paused downloads when the lifecycle is destroyed.
Single Download
First Way
Make sure you calledGDownload.init(Lifecycle) and thenInsideActivity orFragment call the following method. Remember if the argument passed isActivity orFragment. DownloadProgressListener callbacks will be called only when theActivity orFragment is visible.
Remember when this method is calledDownloadProgressListeneronStop() is also invoked.
Difference b/w Pause and Stop
Pause and Stop does the same job - juststop the download. The difference lies in a fact thatPausePark Threads after download is stopped and later whenresume is called, thesethreads will be used.Whilestop don't park threads.
If you want to shutdown downloader and it's best practice. just call
downloader.shutdown()
Group Download
In Order to Download Multiple files and monitor them as a single entity,GroupDownloader can be used. There are various ways to create one.Make sure you calledGDownload.init(Lifecycle) and then
valdownloadList =mutableListOf<Download>()GDownload.freeGroup(Context) {groupLoopTimeMilliSecs =5_00getGroup()?.apply {start()// start group thread. as group has it's dedicated threadaddAll(downloadList) {// enqueue downloads in the group but don't issue the start commandstartDownloads(it)// start group downloads }addGroupProgressListener(GroupListener) }}
see sample app for more details.The following attributes are supported for group downloader.
Property
Definition
groupLoopTimeMilliSecs
As each group has dedicated thread for processing Downloads.it is the time difference in milliseconds b/w two consecutive loops.
concurrentDownloadsRunningCapacity
number of concurrent downloads that group will run. Although! there is no limit on enqueuing downloads.
progressCallbacksOnMainThread
Flag indicating which thread to use for download progress callbacks
urlConnectionFactory
connection Factory used to make connections to the remote server
filesSaveRootPath
optional root path that will be used for saving files. ifname argument is absolute path it always take precedence if both are present.
networkType
network type to use for downloading.
connectionRetryCount
number of times connection factory will try to establish a connection in case of failures.
maxConnectionPerDownload
number of concurrent connections(threads). max 32
progressUpdateTimeMilliSecs
interval difference between consective calls of onDownloadProgress method in DownloadProgressListener
getGroup()
retrieve the group download manager associated with the downloads list.
progressCallbackLifeCycle
Lifecycle to associate the progress callbacks for individual downloads.
databaseManager
Database manager used for loading or accessing Downloads data
networkInfoProvider
network information provider
Shutdown Group Downloader
group.shutDown()
Pause Group Download
To pause or freeze download running inside group processor.
group.freezeDownload(DownloadID)
Resume Group Download
To resume download running inside group processor.
group.resumeDownload(DownloadID)
Stop Group Download
To stop download running inside group processor.
group.stopDownload(DownloadID)
Restart Group Download
To restart download running inside group processor.
group.restartDownload(DownloadID)
Group Progress Listener
In order to track group processor progress.attach the listener as below.
By default GDownload uses the HttpUrlConnection for connecting and downloading.In order to change or use custom Connection Handler.you need to check outURLConnectionHandler andDefaultURLConnectionHandler.
⚠️Remember⚠️
Library is lifecycle aware. it has its benefits but alsosome pitfalls. Imagine if you init the library by passing the lifecycle which is associated with fragment and app stays for too long in the background and for some reasons your activity,fragment orlifecycle is destroyed but app is not.in such case you might not be able to schedule further downloadsas the background executors are killed.Either reinitialize the library or be careful while passingthe lifecycle.Second scenario will be if you attach lifecycle to single orgroup download manager and that lifecycle is destroyed or that component is destroyed. it is likely that you will not recieve progress callbacks. so be careful there is an activity named asSingleDownloadLifecycleSurvivalActivitywhich shows how to handle lifecycle events while keeping the downloads running. passing lifecycle is not necessary
Contribute
GDownload can only get better if you make code contributions. Found a bug? Report it.Have a feature idea you'd love to see in GDownload? Contribute to the project!
License
Copyright (C) 2022 Tanveer Hussain.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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.
About
Light Weight, Fast 🚀 , Easy to Use, Reliable Download client for android.