Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Light Weight, Fast 🚀 , Easy to Use, Reliable Download client for android.

License

NotificationsYou must be signed in to change notification settings

tanoDxyz/GDownload

Repository files navigation


ScreenShot

Overview

GDownload is a simple, powerful, easy to use, customizable file download client library for Android.

ScreenShotScreenShotScreenShot

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.

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"android:maxSdkVersion="28" />

Also, as you are going to use Internet to download files. We need to add the Internet access permissionsin the Manifest.

<uses-permissionandroid:name="android.permission.INTERNET"/>

How to use GDownload

Using GDownload is easy! Just add the following to your application's root build.gradle file.

allprojects {repositories {        ...maven {url'https://jitpack.io' }    } }

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.

GDownload.singleDownload(ActivityorFragmentorContext) {url ="url"name ="fileName or FilePath"downloadProgressListener =object :DownloadProgressListener {overridefunonConnectionEstablished(downloadInfo:DownloadInfo?) {}overridefunonDownloadProgress(downloadInfo:DownloadInfo?) {}overridefunonDownloadFailed(downloadInfo:DownloadInfo,ex:String) {}overridefunonDownloadSuccess(downloadInfo:DownloadInfo) {}overridefunonDownloadIsMultiConnection(downloadInfo:DownloadInfo,multiConnection:Boolean                ) {}overridefunonPause(downloadInfo:DownloadInfo,paused:Boolean,reason:String) {}overridefunonRestart(downloadInfo:DownloadInfo,restarted:Boolean,reason:String                ) {}overridefunonResume(downloadInfo:DownloadInfo,resume:Boolean,reason:String) {}overridefunonStop(downloadInfo:DownloadInfo,stopped:Boolean,reason:String) {}            }        }

This methodGDownload.singleDownload supports the following attributes.

PropertyDefinition
url*resource address
name*file name or absolute filePath
downloadCallbacksOnMainThreadFlag indicating which thread to use for download progress callbacks
connectionFactoryconnection Factory used to make connections to the remote server
downloadFilesRootoptional root path that will be used for saving files. ifname argument is absolute path it always take precedence if both are present.
networkTypenetwork type to use for downloading.
connectionRetryCountnumber of times connection factory will try to establish a connection in case of failures.
maxNumberOfConnectionsnumber of concurrent connections(threads). max 32
progressUpdateTimeMilliSecinterval difference between consective calls of onDownloadProgress method in DownloadProgressListener
getDownloader()retrieve the download manager associated with the download
downloadProgressListenerDownload progress Listener.

Second Way

Make sure you calledGDownload.init(Lifecycle) and then call the following method.

GDownload.freeDownloader(Context) {downloader ->downloader.download("url","name",NetworkType.ALL,object :DownloadProgressListener {overridefunonDownloadSuccess(downloadInfo:DownloadInfo) {}overridefunonDownloadFailed(downloadInfo:DownloadInfo,ex:String) {}                })}

Third Way

For this way of downloading you don't need toinitalize anything.
Simply call the following

valdownloader =GDownload.freeDownloader(Context,ScheduledBackgroundExecutor,DownloadCallbacksHandler,FileStorageHelper,ConnectionManager,DownloadDatabaseManager,NetworkInfoProvider   )downloader.download("url","name",NetworkType.ALL,object :DownloadProgressListener {})

Pause Download

To pause a download is pretty simple.

downloader.freezeDownload {frozen,msg ->if(frozen) {//download froze/paused            }else {// $msg indicates the reason            }        }

Remember when this method is calledDownloadProgressListeneronPause() is also invoked.

Stop Download

To stop a download is pretty simple.

downloader.stopDownload {stopped,msg ->if(stopped) {//download stopped            }else {// $msg indicates the reason            }        }

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.

Resume Download

Download that ispaused can be resumed like this.

downloader.resumeDownload {resumed,msg->if(resumed) {// download successfully resumed    }else {// check the $msg    }}

Restart Download

Download that is 'Stopped' orFailed can berestarted via

downloader.restart {restarted,msg->if(restarted) {// download successfully restarted    }else {// check the $msg    }}

Load Failed Downloads From Database

First way

Make sure you calledGDownload.init() and then call use the following method

GDownload.loadAllInCompleteDownloadsFromDatabase(Context) {downloadList->// process downloadsdownloadList.forEach {download ->GDownload.freeDownloader(Context) {it.download(download,object :DownloadProgressListener {})      }   }}
Second way
valdbManager =SQLiteManager.getInstance(Context)dbManager.findDownloadByDownloadId(downloadId_int)valallInCompleteDownloads =dbManager.findAllInCompleteDownloads()dbManager.findDownloadByFilePath("filePath")dbManager.close()
Third way

If you had reference to the downloader then you can directly load the faileddownload from database.

downloader.loadDownloadFromDatabase(DownloadID) {loaded->}downloader.loadDownloadFromDatabase("filePath") {loaded->}

Fourth way

if you had reference to the GroupProcessor(Group) then you can directly load the failed download/s from database into the group processor.

group.loadDownloadsFromDatabase {downloadsEnqueued -> }

Shutdown Downloader

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.

PropertyDefinition
groupLoopTimeMilliSecsAs each group has dedicated thread for processing Downloads.it is the time difference in milliseconds b/w two consecutive loops.
concurrentDownloadsRunningCapacitynumber of concurrent downloads that group will run. Although! there is no limit on enqueuing downloads.
progressCallbacksOnMainThreadFlag indicating which thread to use for download progress callbacks
urlConnectionFactoryconnection Factory used to make connections to the remote server
filesSaveRootPathoptional root path that will be used for saving files. ifname argument is absolute path it always take precedence if both are present.
networkTypenetwork type to use for downloading.
connectionRetryCountnumber of times connection factory will try to establish a connection in case of failures.
maxConnectionPerDownloadnumber of concurrent connections(threads). max 32
progressUpdateTimeMilliSecsinterval difference between consective calls of onDownloadProgress method in DownloadProgressListener
getGroup()retrieve the group download manager associated with the downloads list.
progressCallbackLifeCycleLifecycle to associate the progress callbacks for individual downloads.
databaseManagerDatabase manager used for loading or accessing Downloads data
networkInfoProvidernetwork 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.

group.addGroupProgressListener(object :GroupListener {overridefunonAdded(groupId:Long,download:DownloadInfo,groupState:GroupState) {}overridefunonDownloading(groupId:Long,download:DownloadInfo,groupState:GroupState            ) {}overridefunonEnqueued(groupId:Long,download:DownloadInfo,groupState:GroupState) {}overridefunonFailure(groupId:Long,errorMessage:String?,download:DownloadInfo,groupState:GroupState            ) {}overridefunonPaused(groupId:Long,download:DownloadInfo,groupState:GroupState) {}overridefunonStarting(groupId:Long,download:DownloadInfo,groupState:GroupState) {}overridefunonStopped(groupId:Long,download:DownloadInfo,groupState:GroupState) {}overridefunonSuccess(groupId:Long,download:DownloadInfo,groupState:GroupState) {}overridefunonWaitingForTurn(groupId:Long,download:DownloadInfo,groupState:GroupState            ) {}        })

Connection Handler

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.

[8]ページ先頭

©2009-2025 Movatter.jp