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 <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-12-18 21:54:29 +05:30
parent 70cfdbafd8
commit cd8c4c1364
2 changed files with 15 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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,