Merge pull request #4177 from kiwix/improvement_in_showing_download_notification

Improved handling of completed and canceled download notifications(When app is in background or closed).
This commit is contained in:
Kelson 2025-01-18 08:15:45 +01:00 committed by GitHub
commit c069a129e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -101,10 +101,16 @@ class DownloadMonitorService : Service() {
* *
* The method checks for any active downloads and, if found, updates the notification * The method checks for any active downloads and, if found, updates the notification
* with the latest download progress. If there are no active downloads, * with the latest download progress. If there are no active downloads,
* the service is stopped and removed from the foreground. * the service is stopped and removed from the foreground. Additionally, if the user cancels a
* download, the corresponding notification is immediately removed to reflect the cancellation.
*
* @param downloadId Optional parameter representing the ID of the download whose notification
* should be canceled if the user cancels the download.
*/ */
private fun setForegroundNotification() { private fun setForegroundNotification(downloadId: Int? = null) {
updater.onNext { updater.onNext {
// Cancel the ongoing download notification if the user cancels the download.
downloadId?.let(::cancelNotificationForId)
fetch.getDownloads { downloadList -> fetch.getDownloads { downloadList ->
downloadList.firstOrNull { downloadList.firstOrNull {
it.status == Status.NONE || it.status == Status.NONE ||
@ -115,11 +121,20 @@ class DownloadMonitorService : Service() {
val notificationBuilder = val notificationBuilder =
fetchDownloadNotificationManager.getNotificationBuilder(it.id, it.id) fetchDownloadNotificationManager.getNotificationBuilder(it.id, it.id)
startForeground(it.id, notificationBuilder.build()) startForeground(it.id, notificationBuilder.build())
} ?: kotlin.run(::stopForegroundServiceForDownloads) } ?: kotlin.run {
stopForegroundServiceForDownloads()
// Cancel the last ongoing notification after detaching it from
// the foreground service if no active downloads are found.
downloadId?.let(::cancelNotificationForId)
}
} }
} }
} }
private fun cancelNotificationForId(downloadId: Int) {
notificationManager.cancel(downloadId)
}
private val fetchListener = object : FetchListener { private val fetchListener = object : FetchListener {
override fun onAdded(download: Download) { override fun onAdded(download: Download) {
// Do nothing // Do nothing
@ -199,7 +214,7 @@ class DownloadMonitorService : Service() {
} }
} }
if (shouldSetForegroundNotification) { if (shouldSetForegroundNotification) {
setForegroundNotification() setForegroundNotification(download.id)
} }
} }
} }
@ -207,11 +222,12 @@ class DownloadMonitorService : Service() {
private fun delete(download: Download) { private fun delete(download: Download) {
updater.onNext { updater.onNext {
downloadRoomDao.delete(download) downloadRoomDao.delete(download)
setForegroundNotification() setForegroundNotification(download.id)
} }
} }
} }
@Suppress("MagicNumber")
private fun showDownloadCompletedNotification(download: Download) { private fun showDownloadCompletedNotification(download: Download) {
downloadNotificationChannel() downloadNotificationChannel()
val notificationBuilder = getNotificationBuilder(download.id) val notificationBuilder = getNotificationBuilder(download.id)
@ -229,7 +245,12 @@ class DownloadMonitorService : Service() {
.setTimeoutAfter(DEFAULT_NOTIFICATION_TIMEOUT_AFTER_RESET) .setTimeoutAfter(DEFAULT_NOTIFICATION_TIMEOUT_AFTER_RESET)
.setContentIntent(getPendingIntentForDownloadedNotification(download)) .setContentIntent(getPendingIntentForDownloadedNotification(download))
.setAutoCancel(true) .setAutoCancel(true)
notificationManager.notify(download.id, notificationBuilder.build()) // Assigning a new ID to the notification because the same ID is used for the foreground
// notification. If we use the same ID, changing the foreground notification for another
// ongoing download cancels the previous notification for that id, preventing the download
// complete notification from being displayed.
val downloadCompleteNotificationId = download.id + 33
notificationManager.notify(downloadCompleteNotificationId, notificationBuilder.build())
} }
private fun getPendingIntentForDownloadedNotification(download: Download): PendingIntent { private fun getPendingIntentForDownloadedNotification(download: Download): PendingIntent {