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
This commit is contained in:
christosts 2022-10-19 13:41:26 +00:00 committed by Rohit Singh
parent e39826a8db
commit 601eaba7a6

View file

@ -15,6 +15,8 @@
*/
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 android.app.Notification;
@ -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)
// and notificationManagerCompat.cancel(notificationId). For pre-L devices, we must also call
// Service.stopForeground(true) anyway as a workaround that prevents the media notification from
// being undismissable.
mediaSessionService.stopForeground(removeNotifications || Util.SDK_INT < 21);
// and notificationManagerCompat.cancel(notificationId).
if (Util.SDK_INT >= 24) {
Api24.stopForeground(mediaSessionService, removeNotifications);
} 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) {
notificationManagerCompat.cancel(mediaNotification.notificationId);
// Update the notification count so that if a pending notification callback arrives (e.g., a
@ -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)
private static class Api29 {