From aca65f13bb94b6c0c4a77a9d426491d3667f4a7d Mon Sep 17 00:00:00 2001 From: Two-Ai <81279822+Two-Ai@users.noreply.github.com> Date: Mon, 30 Jan 2023 17:25:54 -0500 Subject: [PATCH] Misc Service cleanup (#9005) * Simplify DownloadService wake lock handling _isRunning is only modified in onCreate/onDestroy, so the listener job is redundant. * Drop superclass calls to Service.onCreate/onDestroy From https://developer.android.com/guide/components/services > Note: Unlike the activity lifecycle callback methods, you are not > required to call the superclass implementation of these callback > methods. --- .../data/backup/BackupRestoreService.kt | 3 -- .../data/download/DownloadService.kt | 35 +++---------------- .../data/library/LibraryUpdateService.kt | 3 -- .../data/updater/AppUpdateService.kt | 3 -- .../extension/util/ExtensionInstallService.kt | 2 -- 5 files changed, 4 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt index 04be1f069..28b796586 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt @@ -74,8 +74,6 @@ class BackupRestoreService : Service() { private lateinit var notifier: BackupNotifier override fun onCreate() { - super.onCreate() - scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) notifier = BackupNotifier(this) wakeLock = acquireWakeLock(javaClass.name) @@ -90,7 +88,6 @@ class BackupRestoreService : Service() { override fun onDestroy() { destroyJob() - super.onDestroy() } private fun destroyJob() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt index 3c4e7c10f..06bb2de2d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadService.kt @@ -85,21 +85,20 @@ class DownloadService : Service() { private lateinit var scope: CoroutineScope override fun onCreate() { - super.onCreate() scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) startForeground(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS, getPlaceholderNotification()) wakeLock = acquireWakeLock(javaClass.name) _isRunning.value = true - listenDownloaderState() listenNetworkChanges() } override fun onDestroy() { - scope?.cancel() + scope.cancel() _isRunning.value = false downloadManager.stopDownloads() - wakeLock.releaseIfHeld() - super.onDestroy() + if (wakeLock.isHeld) { + wakeLock.release() + } } // Not used @@ -143,32 +142,6 @@ class DownloadService : Service() { .launchIn(scope) } - /** - * Listens to downloader status. Enables or disables the wake lock depending on the status. - */ - private fun listenDownloaderState() { - _isRunning - .onEach { isRunning -> - if (isRunning) { - wakeLock.acquireIfNotHeld() - } else { - wakeLock.releaseIfHeld() - } - } - .catch { - // Ignore errors - } - .launchIn(scope) - } - - private fun PowerManager.WakeLock.releaseIfHeld() { - if (isHeld) release() - } - - private fun PowerManager.WakeLock.acquireIfNotHeld() { - if (!isHeld) acquire() - } - private fun getPlaceholderNotification(): Notification { return notification(Notifications.CHANNEL_DOWNLOADER_PROGRESS) { setContentTitle(getString(R.string.download_notifier_downloader_title)) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 97236a3a4..021bfdddb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -177,8 +177,6 @@ class LibraryUpdateService( * the wake lock. */ override fun onCreate() { - super.onCreate() - notifier = LibraryUpdateNotifier(this) wakeLock = acquireWakeLock(javaClass.name) @@ -198,7 +196,6 @@ class LibraryUpdateService( if (instance == this) { instance = null } - super.onDestroy() } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateService.kt index d67dab7be..0fb92cd43 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateService.kt @@ -45,8 +45,6 @@ class AppUpdateService : Service() { private var runningCall: Call? = null override fun onCreate() { - super.onCreate() - notifier = AppUpdateNotifier(this) wakeLock = acquireWakeLock(javaClass.name) @@ -79,7 +77,6 @@ class AppUpdateService : Service() { override fun onDestroy() { destroyJob() - super.onDestroy() } private fun destroyJob() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstallService.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstallService.kt index 45368c2b4..6e7368c0c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstallService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstallService.kt @@ -22,7 +22,6 @@ class ExtensionInstallService : Service() { private var installer: Installer? = null override fun onCreate() { - super.onCreate() val notification = notificationBuilder(Notifications.CHANNEL_EXTENSIONS_UPDATE) { setSmallIcon(R.drawable.ic_tachi) setAutoCancel(false) @@ -59,7 +58,6 @@ class ExtensionInstallService : Service() { } override fun onDestroy() { - super.onDestroy() installer?.onDestroy() installer = null }