@@ -136,9 +136,7 @@ class DownloadMonitorService : Service() {
136
136
if (downloadRoomDao.downloads().blockingFirst().isNotEmpty()) {
137
137
checkDownloads()
138
138
} else {
139
- // dispose to avoid unnecessary request to downloadManager
140
- // when there is no download ongoing.
141
- stopMonitoringDownloads()
139
+ stopForegroundServiceForDownloads()
142
140
}
143
141
}
144
142
} catch (ignore: Exception ) {
@@ -405,10 +403,11 @@ class DownloadMonitorService : Service() {
405
403
updateNotification(downloadModel, downloadEntity.title, downloadEntity.description)
406
404
return @let
407
405
}
408
- cancelNotification (downloadId)
406
+ cancelNotificationAndAssignNewNotificationToForegroundService (downloadId)
409
407
} ? : run {
410
- // already downloaded/cancelled so cancel the notification if any running.
411
- cancelNotification(downloadId)
408
+ // already downloaded/cancelled so cancel the notification if any running, and
409
+ // assign new notification to foreground service.
410
+ cancelNotificationAndAssignNewNotificationToForegroundService(downloadId)
412
411
}
413
412
}
414
413
}
@@ -465,12 +464,12 @@ class DownloadMonitorService : Service() {
465
464
}
466
465
}
467
466
468
- private fun cancelNotification (downloadId : Long ) {
467
+ private fun cancelNotificationAndAssignNewNotificationToForegroundService (downloadId : Long ) {
469
468
downloadNotificationManager.cancelNotification(downloadId.toInt())
470
- assignNewForegroundNotification ()
469
+ updateForegroundNotificationOrStopService ()
471
470
}
472
471
473
- private fun assignNewForegroundNotification () {
472
+ private fun updateForegroundNotificationOrStopService () {
474
473
val activeDownloads = getActiveDownloads()
475
474
if (activeDownloads.isNotEmpty()) {
476
475
// Promote the first active download to foreground
@@ -487,7 +486,7 @@ class DownloadMonitorService : Service() {
487
486
startForeground(foreGroundServiceInformation.second, notification)
488
487
} else {
489
488
// Stop the service if no active downloads remain
490
- stopMonitoringDownloads ()
489
+ stopForegroundServiceForDownloads ()
491
490
}
492
491
}
493
492
@@ -511,7 +510,7 @@ class DownloadMonitorService : Service() {
511
510
downloadNotificationModel,
512
511
object : AssignNewForegroundServiceNotification {
513
512
override fun assignNewForegroundServiceNotification (downloadId : Long ) {
514
- cancelNotification (downloadId)
513
+ cancelNotificationAndAssignNewNotificationToForegroundService (downloadId)
515
514
}
516
515
}
517
516
)
@@ -605,7 +604,7 @@ class DownloadMonitorService : Service() {
605
604
super .onDestroy()
606
605
}
607
606
608
- private fun stopMonitoringDownloads () {
607
+ private fun stopForegroundServiceForDownloads () {
609
608
foreGroundServiceInformation = true to DEFAULT_INT_VALUE
610
609
monitoringDisposable?.dispose()
611
610
stopForeground(STOP_FOREGROUND_REMOVE )
0 commit comments