mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
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:
parent
e39826a8db
commit
601eaba7a6
1 changed files with 21 additions and 4 deletions
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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 {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue