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.
This commit is contained in:
Two-Ai 2023-01-30 17:25:54 -05:00 committed by GitHub
parent 7bf30a094a
commit aca65f13bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 4 additions and 42 deletions

View file

@ -74,8 +74,6 @@ class BackupRestoreService : Service() {
private lateinit var notifier: BackupNotifier private lateinit var notifier: BackupNotifier
override fun onCreate() { override fun onCreate() {
super.onCreate()
scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
notifier = BackupNotifier(this) notifier = BackupNotifier(this)
wakeLock = acquireWakeLock(javaClass.name) wakeLock = acquireWakeLock(javaClass.name)
@ -90,7 +88,6 @@ class BackupRestoreService : Service() {
override fun onDestroy() { override fun onDestroy() {
destroyJob() destroyJob()
super.onDestroy()
} }
private fun destroyJob() { private fun destroyJob() {

View file

@ -85,21 +85,20 @@ class DownloadService : Service() {
private lateinit var scope: CoroutineScope private lateinit var scope: CoroutineScope
override fun onCreate() { override fun onCreate() {
super.onCreate()
scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
startForeground(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS, getPlaceholderNotification()) startForeground(Notifications.ID_DOWNLOAD_CHAPTER_PROGRESS, getPlaceholderNotification())
wakeLock = acquireWakeLock(javaClass.name) wakeLock = acquireWakeLock(javaClass.name)
_isRunning.value = true _isRunning.value = true
listenDownloaderState()
listenNetworkChanges() listenNetworkChanges()
} }
override fun onDestroy() { override fun onDestroy() {
scope?.cancel() scope.cancel()
_isRunning.value = false _isRunning.value = false
downloadManager.stopDownloads() downloadManager.stopDownloads()
wakeLock.releaseIfHeld() if (wakeLock.isHeld) {
super.onDestroy() wakeLock.release()
}
} }
// Not used // Not used
@ -143,32 +142,6 @@ class DownloadService : Service() {
.launchIn(scope) .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 { private fun getPlaceholderNotification(): Notification {
return notification(Notifications.CHANNEL_DOWNLOADER_PROGRESS) { return notification(Notifications.CHANNEL_DOWNLOADER_PROGRESS) {
setContentTitle(getString(R.string.download_notifier_downloader_title)) setContentTitle(getString(R.string.download_notifier_downloader_title))

View file

@ -177,8 +177,6 @@ class LibraryUpdateService(
* the wake lock. * the wake lock.
*/ */
override fun onCreate() { override fun onCreate() {
super.onCreate()
notifier = LibraryUpdateNotifier(this) notifier = LibraryUpdateNotifier(this)
wakeLock = acquireWakeLock(javaClass.name) wakeLock = acquireWakeLock(javaClass.name)
@ -198,7 +196,6 @@ class LibraryUpdateService(
if (instance == this) { if (instance == this) {
instance = null instance = null
} }
super.onDestroy()
} }
/** /**

View file

@ -45,8 +45,6 @@ class AppUpdateService : Service() {
private var runningCall: Call? = null private var runningCall: Call? = null
override fun onCreate() { override fun onCreate() {
super.onCreate()
notifier = AppUpdateNotifier(this) notifier = AppUpdateNotifier(this)
wakeLock = acquireWakeLock(javaClass.name) wakeLock = acquireWakeLock(javaClass.name)
@ -79,7 +77,6 @@ class AppUpdateService : Service() {
override fun onDestroy() { override fun onDestroy() {
destroyJob() destroyJob()
super.onDestroy()
} }
private fun destroyJob() { private fun destroyJob() {

View file

@ -22,7 +22,6 @@ class ExtensionInstallService : Service() {
private var installer: Installer? = null private var installer: Installer? = null
override fun onCreate() { override fun onCreate() {
super.onCreate()
val notification = notificationBuilder(Notifications.CHANNEL_EXTENSIONS_UPDATE) { val notification = notificationBuilder(Notifications.CHANNEL_EXTENSIONS_UPDATE) {
setSmallIcon(R.drawable.ic_tachi) setSmallIcon(R.drawable.ic_tachi)
setAutoCancel(false) setAutoCancel(false)
@ -59,7 +58,6 @@ class ExtensionInstallService : Service() {
} }
override fun onDestroy() { override fun onDestroy() {
super.onDestroy()
installer?.onDestroy() installer?.onDestroy()
installer = null installer = null
} }