Commit 601eaba7 by christosts Committed by Rohit Singh

Use Service.stopForeground(int) on API 24+

The MediaNotficationManager stops the service from the foreground
calling Service.stopForeground(boolean) which is deprecated in API 33.
This change calls Service.stopForeground(int), which was added in API
24.

#minor-release

PiperOrigin-RevId: 482190332
parent e39826a8
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
*/ */
package androidx.media3.session; package androidx.media3.session;
import static android.app.Service.STOP_FOREGROUND_DETACH;
import static android.app.Service.STOP_FOREGROUND_REMOVE;
import static androidx.media3.common.util.Assertions.checkStateNotNull; import static androidx.media3.common.util.Assertions.checkStateNotNull;
import android.app.Notification; import android.app.Notification;
...@@ -229,10 +231,14 @@ import java.util.concurrent.TimeoutException; ...@@ -229,10 +231,14 @@ import java.util.concurrent.TimeoutException;
} }
} }
// To hide the notification on all API levels, we need to call both Service.stopForeground(true) // To hide the notification on all API levels, we need to call both Service.stopForeground(true)
// and notificationManagerCompat.cancel(notificationId). For pre-L devices, we must also call // and notificationManagerCompat.cancel(notificationId).
// Service.stopForeground(true) anyway as a workaround that prevents the media notification from if (Util.SDK_INT >= 24) {
// being undismissable. Api24.stopForeground(mediaSessionService, removeNotifications);
mediaSessionService.stopForeground(removeNotifications || Util.SDK_INT < 21); } else {
// For pre-L devices, we must call Service.stopForeground(true) anyway as a workaround
// that prevents the media notification from being undismissable.
mediaSessionService.stopForeground(removeNotifications || Util.SDK_INT < 21);
}
if (removeNotifications && mediaNotification != null) { if (removeNotifications && mediaNotification != null) {
notificationManagerCompat.cancel(mediaNotification.notificationId); notificationManagerCompat.cancel(mediaNotification.notificationId);
// Update the notification count so that if a pending notification callback arrives (e.g., a // Update the notification count so that if a pending notification callback arrives (e.g., a
...@@ -301,6 +307,17 @@ import java.util.concurrent.TimeoutException; ...@@ -301,6 +307,17 @@ import java.util.concurrent.TimeoutException;
} }
} }
@RequiresApi(24)
private static class Api24 {
@DoNotInline
public static void stopForeground(MediaSessionService service, boolean removeNotification) {
service.stopForeground(removeNotification ? STOP_FOREGROUND_REMOVE : STOP_FOREGROUND_DETACH);
}
private Api24() {}
}
@RequiresApi(29) @RequiresApi(29)
private static class Api29 { private static class Api29 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment