Skip to content

Commit 51e8ea8

Browse files
authored
Merge pull request #4496 from kiwix/Fixes#4495
Improved the startup behavior of `DownloadMonitorService`.
2 parents 987aebf + bbaaee2 commit 51e8ea8

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadMonitorService.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,15 @@ class DownloadMonitorService : Service() {
124124
* until there are no active downloads, at which point the service is stopped.
125125
*/
126126
private fun showDownloadServiceForegroundNotification() {
127+
// Start the foreground service immediately before going to background.
128+
downloadNotificationChannel()
129+
startForeground(DOWNLOAD_SERVICE_NOTIFICATION_ID, buildForegroundNotification())
127130
fetch.getDownloadsWithStatus(
128131
listOf(Status.NONE, Status.ADDED, Status.QUEUED, Status.DOWNLOADING, Status.PAUSED)
129132
) { activeDownloads ->
130133
if (activeDownloads.isNotEmpty()) {
131-
downloadNotificationChannel()
132-
startForeground(DOWNLOAD_SERVICE_NOTIFICATION_ID, buildForegroundNotification())
134+
// Update the notification.
135+
notificationManager.notify(DOWNLOAD_SERVICE_NOTIFICATION_ID, buildForegroundNotification())
133136
} else {
134137
// Stop the foreground service if no active downloads.
135138
stopForegroundServiceForDownloads()

core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,14 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
256256
super.onStart()
257257
externalLinkOpener.setAlertDialogShower(alertDialogShower)
258258
rateDialogHandler.setAlertDialogShower(alertDialogShower)
259+
rateDialogHandler.checkForRateDialog(getIconResId())
260+
}
261+
262+
override fun onResume() {
263+
super.onResume()
264+
// Resume in-app download monitoring now that the app is visible again.
259265
downloadMonitor.startMonitoringDownload()
260266
stopDownloadServiceIfRunning()
261-
rateDialogHandler.checkForRateDialog(getIconResId())
262267
}
263268

264269
/**
@@ -276,10 +281,12 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
276281
}
277282
}
278283

279-
override fun onStop() {
284+
override fun onPause() {
285+
// Start the DownloadService as the app is about to enter the background,
286+
// so downloads can continue even if the app is no longer in the foreground.
280287
startMonitoringDownloads()
281288
downloadMonitor.stopListeningDownloads()
282-
super.onStop()
289+
super.onPause()
283290
}
284291

285292
/**

0 commit comments

Comments
 (0)