From fc32f0ded3b6304f7ba52866befefd0729888e18 Mon Sep 17 00:00:00 2001 From: Marc Baechinger Date: Thu, 20 Oct 2022 03:11:46 +0000 Subject: [PATCH] Merge pull request #183 from jasper-apps:bugfix/make-download-notification-appear-immediately PiperOrigin-RevId: 482165983 (cherry picked from commit 405455b06c6d596552c695670664309319f5570c) --- .../ui/DownloadNotificationHelper.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationHelper.java index 9790baf33a..6ace069ef8 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ui/DownloadNotificationHelper.java @@ -15,17 +15,23 @@ */ package com.google.android.exoplayer2.ui; +import static androidx.core.app.NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE; + +import android.annotation.SuppressLint; import android.app.Notification; import android.app.PendingIntent; import android.content.Context; +import androidx.annotation.DoNotInline; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; import androidx.annotation.StringRes; import androidx.core.app.NotificationCompat; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.core.R; import com.google.android.exoplayer2.offline.Download; import com.google.android.exoplayer2.scheduler.Requirements; +import com.google.android.exoplayer2.util.Util; import java.util.List; /** Helper for creating download notifications. */ @@ -236,6 +242,19 @@ private Notification buildNotification( notificationBuilder.setProgress(maxProgress, currentProgress, indeterminateProgress); notificationBuilder.setOngoing(ongoing); notificationBuilder.setShowWhen(showWhen); + if (Util.SDK_INT >= 31) { + Api31.setForegroundServiceBehavior(notificationBuilder); + } return notificationBuilder.build(); } + + @RequiresApi(31) + private static final class Api31 { + @SuppressLint("WrongConstant") // TODO(b/254277605): remove lint suppression + @DoNotInline + public static void setForegroundServiceBehavior( + NotificationCompat.Builder notificationBuilder) { + notificationBuilder.setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE); + } + } }