From cd8c4c1364b22d930b5a612c6b81db3ff165b573 Mon Sep 17 00:00:00 2001 From: Aayush Gupta Date: Mon, 18 Dec 2023 21:54:29 +0530 Subject: [PATCH] DownloadWorker: Use a generic download notification if required On old android versions, work manager runs expedited jobs using FGS which seem to require a work notification as soon as the service starts by automatically calling getForegroundInfo method. This causes an exception as we haven't parsed the data yet. Use a generic download notification for such cases. Signed-off-by: Aayush Gupta --- .../java/com/aurora/store/data/work/DownloadWorker.kt | 6 +++++- .../java/com/aurora/store/util/NotificationUtil.kt | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 0ccc1f451..504baa2b7 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 @@ -224,7 +224,11 @@ class DownloadWorker @AssistedInject constructor( } override suspend fun getForegroundInfo(): ForegroundInfo { - val notification = NotificationUtil.getDownloadNotification(appContext, download, id) + val notification = if (this::download.isInitialized) { + NotificationUtil.getDownloadNotification(appContext, download, id) + } else { + NotificationUtil.getDownloadNotification(appContext) + } return if (isQAndAbove()) { ForegroundInfo(NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_DATA_SYNC) } else { 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 c31f3fba2..a08d97b46 100644 --- a/app/src/main/java/com/aurora/store/util/NotificationUtil.kt +++ b/app/src/main/java/com/aurora/store/util/NotificationUtil.kt @@ -79,6 +79,16 @@ object NotificationUtil { } } + fun getDownloadNotification(context: Context): Notification { + return NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_GENERAL) + .setSmallIcon(android.R.drawable.stat_sys_download) + .setColor(ContextCompat.getColor(context, R.color.colorAccent)) + .setContentTitle(context.getString(R.string.app_updater_service_notif_title)) + .setContentText(context.getString(R.string.app_updater_service_notif_text)) + .setOngoing(true) + .build() + } + fun getDownloadNotification( context: Context, app: App,