From 8053d1f3bc10f80ee1a3ec0bbb2685dc7ca35016 Mon Sep 17 00:00:00 2001 From: Rahul Patel Date: Fri, 7 Mar 2025 20:23:45 +0530 Subject: [PATCH] Downloadworker: fix notification issue --- .../aurora/store/data/work/DownloadWorker.kt | 18 ++++++++++-------- .../com/aurora/store/util/NotificationUtil.kt | 5 +++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt b/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt index 647d898ca..b1503dd6e 100644 --- a/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt +++ b/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt @@ -76,13 +76,13 @@ class DownloadWorker @AssistedInject constructor( private val notificationManager = appContext.getSystemService()!! - private var notificationId: Int = 200 private var icon: Bitmap? = null private var totalBytes by Delegates.notNull() private var totalProgress = 0 private var downloadedBytes = 0L private val TAG = DownloadWorker::class.java.simpleName + private val NOTIFICATION_ID: Int = 200 object Exceptions { class InvalidAuthDataException : Exception("AuthData is invalid") @@ -107,7 +107,6 @@ class DownloadWorker @AssistedInject constructor( // Bail out if no package name is provided if (packageName.isNullOrBlank()) return onFailure(Exceptions.NoPackageNameException()) - notificationId = packageName.hashCode() download = downloadDao.getDownload(packageName) val response = (httpClient as HttpClient).call(download.iconURL).body @@ -261,7 +260,7 @@ class DownloadWorker @AssistedInject constructor( } // Remove all notifications - notificationManager.cancel(notificationId) + notificationManager.cancel(NOTIFICATION_ID) return@withContext Result.failure() } @@ -391,7 +390,7 @@ class DownloadWorker @AssistedInject constructor( download.timeRemaining ) - notifyStatus(DownloadStatus.DOWNLOADING) + notifyStatus(DownloadStatus.DOWNLOADING, true) totalProgress = progress } } @@ -405,9 +404,9 @@ class DownloadWorker @AssistedInject constructor( } return if (isQAndAbove) { - ForegroundInfo(notificationId, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC) + ForegroundInfo(NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC) } else { - ForegroundInfo(notificationId, notification) + ForegroundInfo(NOTIFICATION_ID, notification) } } @@ -415,7 +414,7 @@ class DownloadWorker @AssistedInject constructor( * Notifies the user of the current status of the download. * @param status Current [DownloadStatus] */ - private suspend fun notifyStatus(status: DownloadStatus) { + private suspend fun notifyStatus(status: DownloadStatus, isProgress: Boolean = false) { // Update status in database download.downloadStatus = status downloadDao.updateStatus(download.packageName, status) @@ -434,7 +433,10 @@ class DownloadWorker @AssistedInject constructor( } val notification = NotificationUtil.getDownloadNotification(appContext, download, icon) - notificationManager.notify(notificationId, notification) + notificationManager.notify( + if (isProgress) NOTIFICATION_ID else download.packageName.hashCode(), + notification + ) } /** diff --git a/app/src/main/java/com/aurora/store/util/NotificationUtil.kt b/app/src/main/java/com/aurora/store/util/NotificationUtil.kt index 21533141e..2d76d4249 100644 --- a/app/src/main/java/com/aurora/store/util/NotificationUtil.kt +++ b/app/src/main/java/com/aurora/store/util/NotificationUtil.kt @@ -93,6 +93,7 @@ object NotificationUtil { largeIcon: Bitmap? = null ): Notification { val builder = NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_DOWNLOADS) + builder.setSmallIcon(R.drawable.ic_notification_outlined) builder.setContentTitle(download.displayName) builder.setContentIntent(getContentIntentForDownloads(context)) builder.setLargeIcon(largeIcon) @@ -151,7 +152,7 @@ object NotificationUtil { DownloadStatus.DOWNLOADING, DownloadStatus.QUEUED -> { builder.setSmallIcon(android.R.drawable.stat_sys_download) builder.setContentText( - if (download.progress == 0) { + if (download.progress <= 0) { context.getString(R.string.download_queued) } else { context.getString( @@ -164,7 +165,7 @@ object NotificationUtil { ) builder.setOngoing(true) builder.setCategory(Notification.CATEGORY_PROGRESS) - builder.setProgress(100, download.progress, download.progress == 0) + builder.setProgress(100, download.progress, download.progress <= 0) builder.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE builder.addAction( NotificationCompat.Action.Builder(