Skip to content

An Android downloader library supported with notifications.

License

Notifications You must be signed in to change notification settings

mutkuensert/Downloader

Repository files navigation

Downloader

Table of Contents

About The Library

With this library you can easily download the files in urls and show the download progress in notifications. For the example implementation of this library, you can look at my Pixabay Search Engine project on Github.

Adding The Dependency

Add jitpack into the repositories

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

Add the dependency in build.gradle file.

implementation 'com.github.mutkuensert:downloader:v2.3.1'

You can also add the library locally. To do that:

Clone the project.

Run in the terminal:

./gradlew build

Find generated aar file under build folder and copy & paste it into your project. You can create a directory called "libs" under your app module and paste the file into there. Then you can add the dependency:

implementation files('libs/Downloader.aar')

How to Use

If you use Hilt in your project, you can initialize Downloader class like below. You should also implement a CoroutineScope and inject it. You mustn't provide it as a singleton.

@Module
@InstallIn(SingletonComponent::class)
object DownloaderModule {

    @Provides
    fun providesDownloader(scope: CoroutineScope, @ApplicationContext context: Context): Downloader {
        return Downloader.Builder(context, scope)
            .setNotificationsActive(true) //Default is false.
            .build()
    }
}

The notifications can be customized:

val downloader = Downloader.Builder()
    .context(context)
    .scope(scope)
    .setNotificationsActive(true)
    .build()

downloader.notificationBuilder.setAutoCancel(false)

return downloader

Inject the downloader somewhere, for example a ViewModel:

@HiltViewModel
class MyViewModel @Inject constructor(
    ...,
    val downloader: Downloader,
) : ViewModel() { }

Initialize activity result launcher in a Fragment or an Activity:

viewModel.downloader.initActivityResultLauncher {
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                if (result.resultCode == Activity.RESULT_OK) {
                    val uri = result.data?.data

                    if (uri != null) this.writeToFile(uri)
                }
            }
        }

The library uses functions of its own to extract the file name and file format from the url. If a custom extractor is needed different from these default ones, you must set them:

downloader.setFileFormatExtractor { url ->
            url.substringAfterLast(".").substringBefore("?")
        }
downloader.setFileNameExtractor { url ->
    url.substringAfterLast("/").substringBefore(".")
}

Finally, you can download the file on the url:

downloader.downloadUrl(url)

Notification and internet permissions have to be added in the manifest file.

License

Copyright 2022 Mustafa Utku Ensert

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.

Contact

ensertyazilim@gmail.com