|
15 | 15 | */ |
16 | 16 | package com.google.android.exoplayer2.ui; |
17 | 17 |
|
| 18 | +import static androidx.core.app.NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE; |
| 19 | + |
| 20 | +import android.annotation.SuppressLint; |
18 | 21 | import android.app.Notification; |
19 | 22 | import android.app.PendingIntent; |
20 | 23 | import android.content.Context; |
| 24 | +import androidx.annotation.DoNotInline; |
21 | 25 | import androidx.annotation.DrawableRes; |
22 | 26 | import androidx.annotation.Nullable; |
| 27 | +import androidx.annotation.RequiresApi; |
23 | 28 | import androidx.annotation.StringRes; |
24 | 29 | import androidx.core.app.NotificationCompat; |
25 | 30 | import com.google.android.exoplayer2.C; |
26 | 31 | import com.google.android.exoplayer2.core.R; |
27 | 32 | import com.google.android.exoplayer2.offline.Download; |
28 | 33 | import com.google.android.exoplayer2.scheduler.Requirements; |
| 34 | +import com.google.android.exoplayer2.util.Util; |
29 | 35 | import java.util.List; |
30 | 36 |
|
31 | 37 | /** Helper for creating download notifications. */ |
@@ -236,6 +242,19 @@ private Notification buildNotification( |
236 | 242 | notificationBuilder.setProgress(maxProgress, currentProgress, indeterminateProgress); |
237 | 243 | notificationBuilder.setOngoing(ongoing); |
238 | 244 | notificationBuilder.setShowWhen(showWhen); |
| 245 | + if (Util.SDK_INT >= 31) { |
| 246 | + Api31.setForegroundServiceBehavior(notificationBuilder); |
| 247 | + } |
239 | 248 | return notificationBuilder.build(); |
240 | 249 | } |
| 250 | + |
| 251 | + @RequiresApi(31) |
| 252 | + private static final class Api31 { |
| 253 | + @SuppressLint("WrongConstant") // TODO(b/254277605): remove lint suppression |
| 254 | + @DoNotInline |
| 255 | + public static void setForegroundServiceBehavior( |
| 256 | + NotificationCompat.Builder notificationBuilder) { |
| 257 | + notificationBuilder.setForegroundServiceBehavior(FOREGROUND_SERVICE_IMMEDIATE); |
| 258 | + } |
| 259 | + } |
241 | 260 | } |
0 commit comments