Downloadworker: fix notification issue

This commit is contained in:
Rahul Patel
2025-03-07 20:23:45 +05:30
parent 89f45f13d5
commit 8053d1f3bc
2 changed files with 13 additions and 10 deletions

View File

@@ -76,13 +76,13 @@ class DownloadWorker @AssistedInject constructor(
private val notificationManager = appContext.getSystemService<NotificationManager>()!! private val notificationManager = appContext.getSystemService<NotificationManager>()!!
private var notificationId: Int = 200
private var icon: Bitmap? = null private var icon: Bitmap? = null
private var totalBytes by Delegates.notNull<Long>() private var totalBytes by Delegates.notNull<Long>()
private var totalProgress = 0 private var totalProgress = 0
private var downloadedBytes = 0L private var downloadedBytes = 0L
private val TAG = DownloadWorker::class.java.simpleName private val TAG = DownloadWorker::class.java.simpleName
private val NOTIFICATION_ID: Int = 200
object Exceptions { object Exceptions {
class InvalidAuthDataException : Exception("AuthData is invalid") class InvalidAuthDataException : Exception("AuthData is invalid")
@@ -107,7 +107,6 @@ class DownloadWorker @AssistedInject constructor(
// Bail out if no package name is provided // Bail out if no package name is provided
if (packageName.isNullOrBlank()) return onFailure(Exceptions.NoPackageNameException()) if (packageName.isNullOrBlank()) return onFailure(Exceptions.NoPackageNameException())
notificationId = packageName.hashCode()
download = downloadDao.getDownload(packageName) download = downloadDao.getDownload(packageName)
val response = (httpClient as HttpClient).call(download.iconURL).body val response = (httpClient as HttpClient).call(download.iconURL).body
@@ -261,7 +260,7 @@ class DownloadWorker @AssistedInject constructor(
} }
// Remove all notifications // Remove all notifications
notificationManager.cancel(notificationId) notificationManager.cancel(NOTIFICATION_ID)
return@withContext Result.failure() return@withContext Result.failure()
} }
@@ -391,7 +390,7 @@ class DownloadWorker @AssistedInject constructor(
download.timeRemaining download.timeRemaining
) )
notifyStatus(DownloadStatus.DOWNLOADING) notifyStatus(DownloadStatus.DOWNLOADING, true)
totalProgress = progress totalProgress = progress
} }
} }
@@ -405,9 +404,9 @@ class DownloadWorker @AssistedInject constructor(
} }
return if (isQAndAbove) { return if (isQAndAbove) {
ForegroundInfo(notificationId, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC) ForegroundInfo(NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC)
} else { } 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. * Notifies the user of the current status of the download.
* @param status Current [DownloadStatus] * @param status Current [DownloadStatus]
*/ */
private suspend fun notifyStatus(status: DownloadStatus) { private suspend fun notifyStatus(status: DownloadStatus, isProgress: Boolean = false) {
// Update status in database // Update status in database
download.downloadStatus = status download.downloadStatus = status
downloadDao.updateStatus(download.packageName, status) downloadDao.updateStatus(download.packageName, status)
@@ -434,7 +433,10 @@ class DownloadWorker @AssistedInject constructor(
} }
val notification = NotificationUtil.getDownloadNotification(appContext, download, icon) val notification = NotificationUtil.getDownloadNotification(appContext, download, icon)
notificationManager.notify(notificationId, notification) notificationManager.notify(
if (isProgress) NOTIFICATION_ID else download.packageName.hashCode(),
notification
)
} }
/** /**

View File

@@ -93,6 +93,7 @@ object NotificationUtil {
largeIcon: Bitmap? = null largeIcon: Bitmap? = null
): Notification { ): Notification {
val builder = NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_DOWNLOADS) val builder = NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_DOWNLOADS)
builder.setSmallIcon(R.drawable.ic_notification_outlined)
builder.setContentTitle(download.displayName) builder.setContentTitle(download.displayName)
builder.setContentIntent(getContentIntentForDownloads(context)) builder.setContentIntent(getContentIntentForDownloads(context))
builder.setLargeIcon(largeIcon) builder.setLargeIcon(largeIcon)
@@ -151,7 +152,7 @@ object NotificationUtil {
DownloadStatus.DOWNLOADING, DownloadStatus.QUEUED -> { DownloadStatus.DOWNLOADING, DownloadStatus.QUEUED -> {
builder.setSmallIcon(android.R.drawable.stat_sys_download) builder.setSmallIcon(android.R.drawable.stat_sys_download)
builder.setContentText( builder.setContentText(
if (download.progress == 0) { if (download.progress <= 0) {
context.getString(R.string.download_queued) context.getString(R.string.download_queued)
} else { } else {
context.getString( context.getString(
@@ -164,7 +165,7 @@ object NotificationUtil {
) )
builder.setOngoing(true) builder.setOngoing(true)
builder.setCategory(Notification.CATEGORY_PROGRESS) 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.foregroundServiceBehavior = NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
builder.addAction( builder.addAction(
NotificationCompat.Action.Builder( NotificationCompat.Action.Builder(