mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-14 13:08:56 +01:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import eu.kanade.domain.track.model.toDomainTrack
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.cache.CoverCache
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
||||
import eu.kanade.tachiyomi.data.preference.DEVICE_BATTERY_NOT_LOW
|
||||
import eu.kanade.tachiyomi.data.preference.DEVICE_CHARGING
|
||||
@@ -309,7 +308,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
|
||||
if (newUpdates.isNotEmpty()) {
|
||||
notifier.showUpdateNotifications(newUpdates)
|
||||
if (hasDownloads.get()) {
|
||||
DownloadService.start(context)
|
||||
downloadManager.startDownloads()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ import eu.kanade.domain.download.service.DownloadPreferences
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.backup.BackupRestoreService
|
||||
import eu.kanade.tachiyomi.data.download.DownloadManager
|
||||
import eu.kanade.tachiyomi.data.download.DownloadService
|
||||
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
|
||||
import eu.kanade.tachiyomi.data.updater.AppUpdateService
|
||||
import eu.kanade.tachiyomi.source.SourceManager
|
||||
@@ -56,12 +55,9 @@ class NotificationReceiver : BroadcastReceiver() {
|
||||
// Dismiss notification
|
||||
ACTION_DISMISS_NOTIFICATION -> dismissNotification(context, intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1))
|
||||
// Resume the download service
|
||||
ACTION_RESUME_DOWNLOADS -> DownloadService.start(context)
|
||||
ACTION_RESUME_DOWNLOADS -> downloadManager.startDownloads()
|
||||
// Pause the download service
|
||||
ACTION_PAUSE_DOWNLOADS -> {
|
||||
DownloadService.stop(context)
|
||||
downloadManager.pauseDownloads()
|
||||
}
|
||||
ACTION_PAUSE_DOWNLOADS -> downloadManager.pauseDownloads()
|
||||
// Clear the download queue
|
||||
ACTION_CLEAR_DOWNLOADS -> downloadManager.clearQueue()
|
||||
// Launch share activity and dismiss notification
|
||||
|
||||
Reference in New Issue
Block a user