From bc252fae1fa6378da46f4d7082be09e4bbdfe3bb Mon Sep 17 00:00:00 2001 From: Aayush Gupta Date: Fri, 29 Dec 2023 19:13:04 +0530 Subject: [PATCH] DownloadWorker: Create and use scaled bitmap for notification icon Old android versions get binder transaction errors otherwise. Also move out the logic to fetch icon into the Worker to avoid fetching it 100 times. Keep the install logic in place as that method is only called once after installation. Signed-off-by: Aayush Gupta --- .../aurora/store/data/work/DownloadWorker.kt | 12 +++++++++--- .../com/aurora/store/util/NotificationUtil.kt | 17 ++++++----------- 2 files changed, 15 insertions(+), 14 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 99fe07b63..61ff988e5 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 @@ -4,6 +4,8 @@ import android.app.NotificationManager import android.app.Service import android.content.Context import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC +import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.util.Log import androidx.hilt.work.HiltWorker import androidx.work.CoroutineWorker @@ -49,10 +51,11 @@ class DownloadWorker @AssistedInject constructor( private lateinit var download: Download private lateinit var notificationManager: NotificationManager - private var downloading = false + private lateinit var icon: Bitmap private val NOTIFICATION_ID = 200 + private var downloading = false private var totalBytes by Delegates.notNull() private var totalProgress = 0 private var downloadedBytes = 0L @@ -64,6 +67,9 @@ class DownloadWorker @AssistedInject constructor( try { val downloadData = inputData.getString(DownloadWorkerUtil.DOWNLOAD_DATA) download = gson.fromJson(downloadData, Download::class.java) + + val bitmap = BitmapFactory.decodeStream(URL(download.iconURL).openStream()) + icon = Bitmap.createScaledBitmap(bitmap, 96, 96, true) } catch (exception: Exception) { Log.e(TAG, "Failed to parse download data", exception) return Result.failure() @@ -267,7 +273,7 @@ class DownloadWorker @AssistedInject constructor( override suspend fun getForegroundInfo(): ForegroundInfo { val notification = if (this::download.isInitialized) { - NotificationUtil.getDownloadNotification(appContext, download, id) + NotificationUtil.getDownloadNotification(appContext, download, id, icon) } else { NotificationUtil.getDownloadNotification(appContext) } @@ -288,7 +294,7 @@ class DownloadWorker @AssistedInject constructor( downloadDao.update(download) } - val notification = NotificationUtil.getDownloadNotification(appContext, download, id) + val notification = NotificationUtil.getDownloadNotification(appContext, download, id, icon) val notificationID = if (dID != -1) dID else download.packageName.hashCode() notificationManager.notify(notificationID, 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 19d2c0955..0644fa966 100644 --- a/app/src/main/java/com/aurora/store/util/NotificationUtil.kt +++ b/app/src/main/java/com/aurora/store/util/NotificationUtil.kt @@ -7,6 +7,7 @@ import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent +import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Color import android.os.Build @@ -86,20 +87,14 @@ object NotificationUtil { fun getDownloadNotification( context: Context, download: AuroraDownload, - workID: UUID + workID: UUID, + largeIcon: Bitmap? = null ): Notification { val builder = NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_GENERAL) builder.setContentTitle(download.displayName) builder.color = ContextCompat.getColor(context, R.color.colorAccent) builder.setContentIntent(getContentIntentForDownloads(context)) - - // Set big icon for download - try { - val bitmap = BitmapFactory.decodeStream(URL(download.iconURL).openStream()) - builder.setLargeIcon(bitmap) - } catch (exception: Exception) { - Log.i(TAG, "Failed to set big icon", exception) - } + builder.setLargeIcon(largeIcon) when (download.status) { DownloadStatus.CANCELLED -> { @@ -166,10 +161,10 @@ object NotificationUtil { fun getInstallNotification(context: Context, download: AuroraDownload): Notification { val builder = NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ALERT) - // Set big icon for download + // Set scaled big icon for download try { val bitmap = BitmapFactory.decodeStream(URL(download.iconURL).openStream()) - builder.setLargeIcon(bitmap) + builder.setLargeIcon(Bitmap.createScaledBitmap(bitmap, 96, 96, true)) } catch (exception: Exception) { Log.i(TAG, "Failed to set big icon", exception) }