From 666ecd02b52ece491d0b5eb7256b87f73c67ead2 Mon Sep 17 00:00:00 2001 From: Tonyo Francis Date: Sun, 3 Mar 2019 09:07:44 -0500 Subject: [PATCH] Added new method hasActiveDownloads(includeAddedDownloads: Boolean, func: Func) --- CHANGELOG | 4 ++++ README.md | 12 ++++++------ .../src/main/java/com/tonyodev/fetch2/Fetch.kt | 9 +++++++++ .../fetch2/database/FetchDatabaseManager.kt | 4 +++- .../fetch2/database/FetchDatabaseManagerImpl.kt | 10 ++++++++-- .../com/tonyodev/fetch2/fetch/FetchHandler.kt | 2 +- .../tonyodev/fetch2/fetch/FetchHandlerImpl.kt | 6 +++--- .../java/com/tonyodev/fetch2/fetch/FetchImpl.kt | 15 ++++++++++++++- .../main/java/com/tonyodev/fetch2rx/RxFetch.kt | 8 ++++++++ .../java/com/tonyodev/fetch2rx/RxFetchImpl.kt | 17 ++++++++++++++++- versions.gradle | 4 ++-- 11 files changed, 74 insertions(+), 17 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 44599866..3c3fdf06 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +Version 3.0.1 +- Added new method hasActiveDownloads(includeAddedDownloads: Boolean, func: Func) + + Version 3.0.0 *Version 3 comes with a lot of enhancements and fixes* diff --git a/README.md b/README.md index 27f62cc8..84679ebc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![Build Status](https://travis-ci.org/tonyofrancis/Fetch.svg?branch=v2)](https://travis-ci.org/tonyofrancis/Fetch) -[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=3.0.0) ](https://bintray.com/tonyofrancis/maven/fetch2/3.0.0/link) +[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=3.0.1) ](https://bintray.com/tonyofrancis/maven/fetch2/3.0.1/link) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Android%20Networking-blue.svg?style=flat)](https://android-arsenal.com/details/1/5196) [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/tonyofrancis/Fetch/blob/master/LICENSE) @@ -50,7 +50,7 @@ How to use Fetch Using Fetch is easy! Just add the Gradle dependency to your application's build.gradle file. ```java -implementation "com.tonyodev.fetch2:fetch2:3.0.0" +implementation "com.tonyodev.fetch2:fetch2:3.0.1" ``` Next, get an instance of Fetch and request a download. @@ -216,7 +216,7 @@ to use the OkHttp Downloader instead. You can create your own custom downloaders if necessary. See the Java docs for details. ```java -implementation "com.tonyodev.fetch2okhttp:fetch2okhttp:3.0.0" +implementation "com.tonyodev.fetch2okhttp:fetch2okhttp:3.0.1" ``` Set the OkHttp Downloader for Fetch to use. ```java @@ -237,7 +237,7 @@ If you would like to take advantage of RxJava2 features when using Fetch, add the following gradle dependency to your application's build.gradle file. ```java -implementation "com.tonyodev.fetch2rx:fetch2rx:3.0.0" +implementation "com.tonyodev.fetch2rx:fetch2rx:3.0.1" ``` RxFetch makes it super easy to enqueue download requests and query downloads using rxJava2 functional methods. @@ -273,7 +273,7 @@ added in the coming days. Start using FetchFileServer by adding the gradle dependency to your application's build.gradle file. ```java -implementation "com.tonyodev.fetch2fileserver:fetch2fileserver:3.0.0" +implementation "com.tonyodev.fetch2fileserver:fetch2fileserver:3.0.1" ``` Start a FetchFileServer instance and add resource files that it can server to connected clients. @@ -382,7 +382,7 @@ Fetch1 Migration Migrate downloads from Fetch1 to Fetch2 using the migration assistant. Add the following gradle dependency to your application's build.gradle file. ```java -implementation "com.tonyodev.fetchmigrator:fetchmigrator:3.0.0" +implementation "com.tonyodev.fetchmigrator:fetchmigrator:3.0.1" ``` Then run the Migrator. diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt b/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt index e8c578ff..5af1ef41 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt @@ -877,6 +877,15 @@ interface Fetch { * */ fun removeFetchObserversForDownload(downloadId: Int, vararg fetchObservers: FetchObserver): Fetch + /** Indicates if this fetch namespace has active(Queued or Downloading) downloads. You can use this value to + * keep a background service ongoing until the callback function returns false. + * @param includeAddedDownloads To include downloads with a status of Added. Added downloads are not considered active. + * @param func the callback function + * @throws FetchException if accessed on ui thread + * @return instance + * */ + fun hasActiveDownloads(includeAddedDownloads: Boolean, func: Func): Fetch + /** * Fetch implementation class. Use this Singleton to get instances of Fetch. * */ diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/database/FetchDatabaseManager.kt b/fetch2/src/main/java/com/tonyodev/fetch2/database/FetchDatabaseManager.kt index a017f078..bf259dba 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/database/FetchDatabaseManager.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/database/FetchDatabaseManager.kt @@ -154,9 +154,11 @@ interface FetchDatabaseManager : Closeable { /** * Gets the count/sum of all downloads with the status of Queued and Downloading combined. + * @param includeAddedDownloads if to include downloads with the status of Added. + * Added downloads are not considered pending by default. * @return the pending download count. * */ - fun getPendingCount(): Long + fun getPendingCount(includeAddedDownloads: Boolean): Long /** * Interface used for the DownloadManager's delegate. diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/database/FetchDatabaseManagerImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/database/FetchDatabaseManagerImpl.kt index 091fc2dc..43f5e162 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/database/FetchDatabaseManagerImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/database/FetchDatabaseManagerImpl.kt @@ -226,10 +226,16 @@ class FetchDatabaseManagerImpl constructor(context: Context, " WHERE ${DownloadDatabase.COLUMN_STATUS} = '${Status.QUEUED.value}'" + " OR ${DownloadDatabase.COLUMN_STATUS} = '${Status.DOWNLOADING}'" - override fun getPendingCount(): Long { + private val pendingCountIncludeAddedQuery = "SELECT ${DownloadDatabase.COLUMN_ID} FROM ${DownloadDatabase.TABLE_NAME}" + + " WHERE ${DownloadDatabase.COLUMN_STATUS} = '${Status.QUEUED.value}'" + + " OR ${DownloadDatabase.COLUMN_STATUS} = '${Status.DOWNLOADING}'" + + " OR ${DownloadDatabase.COLUMN_STATUS} = '${Status.ADDED}'" + + override fun getPendingCount(includeAddedDownloads: Boolean): Long { synchronized(lock) { return try { - val cursor: Cursor? = database.query(pendingCountQuery) + val query = if (includeAddedDownloads) pendingCountIncludeAddedQuery else pendingCountQuery + val cursor: Cursor? = database.query(query) val count = cursor?.count?.toLong() ?: -1L cursor?.close() count diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt index 1d86b027..c87ab579 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt @@ -52,7 +52,7 @@ interface FetchHandler : Closeable { fun getFetchFileServerCatalog(request: Request): List fun setDownloadConcurrentLimit(downloadConcurrentLimit: Int) fun replaceExtras(id: Int, extras: Extras): Download - fun hasActiveDownloads(): Boolean + fun hasActiveDownloads(includeAddedDownloads: Boolean): Boolean fun getListenerSet(): Set fun getPendingCount(): Long fun renameCompletedDownloadFile(id: Int, newFileName: String): Download diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt index 08f01792..8f230803 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt @@ -674,15 +674,15 @@ class FetchHandlerImpl(private val namespace: String, } } - override fun hasActiveDownloads(): Boolean { + override fun hasActiveDownloads(includeAddedDownloads: Boolean): Boolean { if (Thread.currentThread() == Looper.getMainLooper().thread) { throw FetchException(BLOCKING_CALL_ON_UI_THREAD) } - return fetchDatabaseManager.getPendingCount() > 0 + return fetchDatabaseManager.getPendingCount(includeAddedDownloads) > 0 } override fun getPendingCount(): Long { - return fetchDatabaseManager.getPendingCount() + return fetchDatabaseManager.getPendingCount(false) } private fun cancelDownloadsIfDownloading(downloads: List) { diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt index c0111016..f54b5f49 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt @@ -32,7 +32,7 @@ open class FetchImpl constructor(override val namespace: String, override val hasActiveDownloads: Boolean get() { return try { - fetchHandler.hasActiveDownloads() + fetchHandler.hasActiveDownloads(false) } catch (e: Exception) { false } @@ -856,6 +856,19 @@ open class FetchImpl constructor(override val namespace: String, return this } + override fun hasActiveDownloads(includeAddedDownloads: Boolean, func: Func): Fetch { + synchronized(lock) { + throwExceptionIfClosed() + handlerWrapper.post { + val hasActiveDownloads = fetchHandler.hasActiveDownloads(includeAddedDownloads) + uiHandler.post { + func.call(hasActiveDownloads) + } + } + } + return this + } + override fun addListener(listener: FetchListener): Fetch { return addListener(listener, DEFAULT_ENABLE_LISTENER_NOTIFY_ON_ATTACHED) } diff --git a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt index 802910fa..28519bbe 100644 --- a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt +++ b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt @@ -574,6 +574,14 @@ interface RxFetch { * */ fun removeFetchObserversForDownload(downloadId: Int, vararg fetchObservers: FetchObserver): RxFetch + /** Indicates if this fetch namespace has active(Queued or Downloading) downloads. You can use this value to + * keep a background service ongoing until the results returns false. + * @param includeAddedDownloads To include downloads with a status of Added. Added downloads are not considered active. + * @throws FetchException if accessed on ui thread + * @return Convertible with results. + * */ + fun hasActiveDownloads(includeAddedDownloads: Boolean): Convertible + /** * RX Fetch implementation class. Use this Singleton to get instances of RxFetch or Fetch. * */ diff --git a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt index 5486522a..ed9e2fcf 100644 --- a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt +++ b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt @@ -38,7 +38,7 @@ open class RxFetchImpl(override val namespace: String, override val hasActiveDownloads: Boolean get() { return try { - fetchHandler.hasActiveDownloads() + fetchHandler.hasActiveDownloads(false) } catch (e: Exception) { false } @@ -917,6 +917,21 @@ open class RxFetchImpl(override val namespace: String, } } + override fun hasActiveDownloads(includeAddedDownloads: Boolean): Convertible { + return synchronized(lock) { + throwExceptionIfClosed() + Flowable.just(includeAddedDownloads) + .subscribeOn(scheduler) + .flatMap { + throwExceptionIfClosed() + val hasActiveDownloads = fetchHandler.hasActiveDownloads(it) + Flowable.just(hasActiveDownloads) + } + .observeOn(uiScheduler) + .toConvertible() + } + } + override fun getContentLengthForRequest(request: Request, fromServer: Boolean): Convertible { return synchronized(lock) { throwExceptionIfClosed() diff --git a/versions.gradle b/versions.gradle index a7ccf366..875ff8c0 100644 --- a/versions.gradle +++ b/versions.gradle @@ -17,6 +17,6 @@ ext { rxAndroid2_version = "2.1.1" timber_version = "4.7.1" novoda_bintray_version = "0.9" - library_version = "3.0.0" - library_version_code = 62 + library_version = "3.0.1" + library_version_code = 63 } \ No newline at end of file