mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-21 20:47:03 -05:00
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:
parent
7bf30a094a
commit
aca65f13bb
5 changed files with 4 additions and 42 deletions
|
@ -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() {
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue