Merge pull request #4279 from kiwix/Fixes#4278

Fixed: Application crashes on `FetchDownloadNotificationManager.getCancelNotification` method.
This commit is contained in:
Kelson 2025-04-10 09:21:00 +02:00 committed by GitHub
commit 563e06e302
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -73,6 +73,8 @@ class FetchDownloadNotificationManager @Inject constructor(
val context: Context, val context: Context,
private val downloadRoomDao: DownloadRoomDao private val downloadRoomDao: DownloadRoomDao
) : DefaultFetchNotificationManager(context) { ) : DefaultFetchNotificationManager(context) {
private val notificationBuilderLock = Any()
private val downloadNotificationManager: NotificationManager by lazy { private val downloadNotificationManager: NotificationManager by lazy {
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
} }
@ -258,34 +260,36 @@ class FetchDownloadNotificationManager @Inject constructor(
download: Download, download: Download,
notificationBuilder: Builder notificationBuilder: Builder
): Notification { ): Notification {
val downloadTitle = getDownloadNotificationTitle(download) synchronized(notificationBuilderLock) {
val notificationTitle = val downloadTitle = getDownloadNotificationTitle(download)
runBlocking(Dispatchers.IO) { val notificationTitle =
downloadRoomDao.getEntityForFileName(downloadTitle)?.title runBlocking(Dispatchers.IO) {
?: downloadTitle downloadRoomDao.getEntityForFileName(downloadTitle)?.title
} ?: downloadTitle
return notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT) }
.setSmallIcon(android.R.drawable.stat_sys_download_done) return notificationBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentTitle(notificationTitle) .setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentText(context.getString(R.string.paused_state)) .setContentTitle(notificationTitle)
// Set the ongoing true so that could not cancel the pause notification. .setContentText(context.getString(R.string.paused_state))
// However, on Android 14 and above user can cancel the notification by swipe right so we // Set the ongoing true so that could not cancel the pause notification.
// can't control that see https://developer.android.com/about/versions/14/behavior-changes-all#non-dismissable-notifications // However, on Android 14 and above user can cancel the notification by swipe right so we
.setOngoing(true) // can't control that see https://developer.android.com/about/versions/14/behavior-changes-all#non-dismissable-notifications
.setGroup(download.id.toString()) .setOngoing(true)
.setGroupSummary(false) .setGroup(download.id.toString())
.setProgress(HUNDERED, download.progress, false) .setGroupSummary(false)
.addAction( .setProgress(HUNDERED, download.progress, false)
drawable.fetch_notification_cancel, .addAction(
context.getString(R.string.cancel), drawable.fetch_notification_cancel,
getActionPendingIntent(fetch, download, DownloadNotification.ActionType.DELETE) context.getString(R.string.cancel),
) getActionPendingIntent(fetch, download, DownloadNotification.ActionType.DELETE)
.addAction( )
drawable.fetch_notification_resume, .addAction(
context.getString(R.string.notification_resume_button_text), drawable.fetch_notification_resume,
getActionPendingIntent(fetch, download, DownloadNotification.ActionType.RESUME) context.getString(R.string.notification_resume_button_text),
) getActionPendingIntent(fetch, download, DownloadNotification.ActionType.RESUME)
.build() )
.build()
}
} }
private fun getActionPendingIntent( private fun getActionPendingIntent(