Set notification foreground service behaviour starting with API 31

When the foreground service type is set to `mediaPlayiback` this shouldn't
be required. However, some reports from users say that setting the
foreground service behavor fixes some problems on some devices. Setting
it explicitely with this change makes sure users don't have to do that
workaround themselves.

Issue: androidx/media#167
PiperOrigin-RevId: 557608577
This commit is contained in:
bachinger 2023-08-16 22:48:06 +01:00 committed by Julia Bibik
parent 74fa0ed6bf
commit 6bd3e53e8d
3 changed files with 16 additions and 0 deletions

View file

@ -145,6 +145,9 @@ This release includes the following changes since
* Effect:
* Add `VideoFrameProcessor.queueInputBitmap(Bitmap, Iterator<Long>)`
queuing bitmap input by timestamp.
* Set the notifications foreground service behavior to
`FOREGROUND_SERVICE_IMMEDIATE` in `DefaultMediaNotificationProvider`
([#167](https://github.com/androidx/media/issues/167)).
* UI:
* Add a `Player.Listener` implementation for Wear OS devices that handles
playback suppression due to

View file

@ -43,6 +43,7 @@ dependencies {
compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion
implementation 'androidx.collection:collection:' + androidxCollectionVersion
implementation 'androidx.media:media:' + androidxMediaVersion
implementation 'androidx.core:core:' + androidxCoreVersion
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:' + okhttpVersion
androidTestImplementation project(modulePrefix + 'test-utils')
androidTestImplementation project(modulePrefix + 'test-data')

View file

@ -371,6 +371,10 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
.setShowWhen(displayElapsedTimeWithChronometer)
.setUsesChronometer(displayElapsedTimeWithChronometer);
if (Util.SDK_INT >= 31) {
Api31.setForegroundServiceBehavior(builder);
}
Notification notification =
builder
.setContentIntent(mediaSession.getSessionActivity())
@ -692,6 +696,14 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
}
}
@RequiresApi(31)
private static class Api31 {
@DoNotInline
public static void setForegroundServiceBehavior(NotificationCompat.Builder builder) {
builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
}
}
private static String getBitmapLoadErrorMessage(Throwable throwable) {
return "Failed to load bitmap: " + throwable.getMessage();
}