Make DownloadManager the sole entry point for DownloadService (#9140)

* Rename functions for DownloadService internal use

* Call DownloadService.start via DownloadManager

* Inline DownloadService.stop into pauseDownloads

* Inline DownloadService.stop into clearQueue

NotificationReceiver will now also stop the DownloadService when
receiving ACTION_CLEAR_DOWNLOADS.

* Provide DownloadService.isRunning via DownloadManager
This commit is contained in:
Two-Ai
2023-02-24 22:07:30 -05:00
committed by GitHub
parent 7ec87e76db
commit 86b9262a7e
9 changed files with 34 additions and 44 deletions
@@ -48,22 +48,18 @@ class DownloadManager(
val queue: DownloadQueue
get() = downloader.queue
/**
* Tells the downloader to begin downloads.
*
* @return true if it's started, false otherwise (empty queue).
*/
fun startDownloads(): Boolean {
return downloader.start()
}
// For use by DownloadService only
fun downloaderStart() = downloader.start()
fun downloaderStop(reason: String? = null) = downloader.stop(reason)
val isDownloaderRunning
get() = DownloadService.isRunning
/**
* Tells the downloader to stop downloads.
*
* @param reason an optional reason for being stopped, used to notify the user.
* Tells the downloader to begin downloads.
*/
fun stopDownloads(reason: String? = null) {
downloader.stop(reason)
fun startDownloads() {
DownloadService.start(context)
}
/**
@@ -71,6 +67,7 @@ class DownloadManager(
*/
fun pauseDownloads() {
downloader.pause()
DownloadService.stop(context)
}
/**
@@ -78,6 +75,7 @@ class DownloadManager(
*/
fun clearQueue() {
downloader.clearQueue()
DownloadService.stop(context)
}
/**
@@ -95,7 +95,7 @@ class DownloadService : Service() {
override fun onDestroy() {
scope.cancel()
_isRunning.value = false
downloadManager.stopDownloads()
downloadManager.downloaderStop()
if (wakeLock.isHeld) {
wakeLock.release()
}
@@ -111,8 +111,8 @@ class DownloadService : Service() {
return null
}
private fun stopDownloads(@StringRes string: Int) {
downloadManager.stopDownloads(getString(string))
private fun downloaderStop(@StringRes string: Int) {
downloadManager.downloaderStop(getString(string))
}
private fun listenNetworkChanges() {
@@ -122,13 +122,13 @@ class DownloadService : Service() {
withUIContext {
if (isOnline()) {
if (downloadPreferences.downloadOnlyOverWifi().get() && !isConnectedToWifi()) {
stopDownloads(R.string.download_notifier_text_only_wifi)
downloaderStop(R.string.download_notifier_text_only_wifi)
} else {
val started = downloadManager.startDownloads()
val started = downloadManager.downloaderStart()
if (!started) stopSelf()
}
} else {
stopDownloads(R.string.download_notifier_no_network)
downloaderStop(R.string.download_notifier_no_network)
}
}
}