Provide methods to override notification fields

`contentTitle` and `contentText` can now be overridden.

Issue: androidx/media#150
PiperOrigin-RevId: 471287701
This commit is contained in:
rohks 2022-08-31 17:23:20 +00:00 committed by Marc Baechinger
parent 6d3cc01d79
commit a45df2fdcb
2 changed files with 85 additions and 1 deletions

View File

@ -281,7 +281,9 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
// Set metadata info in the notification. // Set metadata info in the notification.
MediaMetadata metadata = player.getMediaMetadata(); MediaMetadata metadata = player.getMediaMetadata();
builder.setContentTitle(metadata.title).setContentText(metadata.artist); builder
.setContentTitle(getNotificationContentTitle(metadata))
.setContentText(getNotificationContentText(metadata));
@Nullable ListenableFuture<Bitmap> bitmapFuture = loadArtworkBitmap(metadata); @Nullable ListenableFuture<Bitmap> bitmapFuture = loadArtworkBitmap(metadata);
if (bitmapFuture != null) { if (bitmapFuture != null) {
if (pendingOnBitmapLoadedFutureCallback != null) { if (pendingOnBitmapLoadedFutureCallback != null) {
@ -504,6 +506,42 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
return compactViewIndices; return compactViewIndices;
} }
/**
* Returns the content title to be used to build the notification.
*
* <p>This method is called each time a new notification is built.
*
* <p>Override this method to customize which field of {@link MediaMetadata} is used for content
* title of the notification.
*
* <p>By default, the notification shows {@link MediaMetadata#title} as content title.
*
* @param metadata The media metadata from which content title is fetched.
* @return Notification content title.
*/
@Nullable
protected CharSequence getNotificationContentTitle(MediaMetadata metadata) {
return metadata.title;
}
/**
* Returns the content text to be used to build the notification.
*
* <p>This method is called each time a new notification is built.
*
* <p>Override this method to customize which field of {@link MediaMetadata} is used for content
* text of the notification.
*
* <p>By default, the notification shows {@link MediaMetadata#artist} as content text.
*
* @param metadata The media metadata from which content text is fetched.
* @return Notification content text.
*/
@Nullable
protected CharSequence getNotificationContentText(MediaMetadata metadata) {
return metadata.artist;
}
private void ensureNotificationChannel() { private void ensureNotificationChannel() {
if (Util.SDK_INT < 26 || notificationManager.getNotificationChannel(channelId) != null) { if (Util.SDK_INT < 26 || notificationManager.getNotificationChannel(channelId) != null) {
return; return;

View File

@ -510,6 +510,52 @@ public class DefaultMediaNotificationProviderTest {
.isEqualTo(R.drawable.media3_icon_circular_play); .isEqualTo(R.drawable.media3_icon_circular_play);
} }
@Test
public void setMediaMetadataTitle_notificationUsesItAsContentTitle() {
Context context = ApplicationProvider.getApplicationContext();
DefaultMediaNotificationProvider defaultMediaNotificationProvider =
new DefaultMediaNotificationProvider.Builder(context).build();
DefaultActionFactory defaultActionFactory =
new DefaultActionFactory(Robolectric.setupService(TestService.class));
MediaSession mockMediaSession =
createMockMediaSessionForNotification(
new MediaMetadata.Builder().setTitle("title").build());
MediaNotification notification =
defaultMediaNotificationProvider.createNotification(
mockMediaSession,
ImmutableList.of(),
defaultActionFactory,
mock(MediaNotification.Provider.Callback.class));
boolean isMediaMetadataTitleEqualToNotificationContentTitle =
"title".contentEquals(NotificationCompat.getContentTitle(notification.notification));
assertThat(isMediaMetadataTitleEqualToNotificationContentTitle).isTrue();
}
@Test
public void setMediaMetadataArtist_notificationUsesItAsContentText() {
Context context = ApplicationProvider.getApplicationContext();
DefaultMediaNotificationProvider defaultMediaNotificationProvider =
new DefaultMediaNotificationProvider.Builder(context).build();
DefaultActionFactory defaultActionFactory =
new DefaultActionFactory(Robolectric.setupService(TestService.class));
MediaSession mockMediaSession =
createMockMediaSessionForNotification(
new MediaMetadata.Builder().setArtist("artist").build());
MediaNotification notification =
defaultMediaNotificationProvider.createNotification(
mockMediaSession,
ImmutableList.of(),
defaultActionFactory,
mock(MediaNotification.Provider.Callback.class));
boolean isMediaMetadataArtistEqualToNotificationContentText =
"artist".contentEquals(NotificationCompat.getContentText(notification.notification));
assertThat(isMediaMetadataArtistEqualToNotificationContentText).isTrue();
}
private static void assertHasNotificationChannel( private static void assertHasNotificationChannel(
List<Object> notificationChannels, String channelId, String channelName) { List<Object> notificationChannels, String channelId, String channelName) {
boolean found = false; boolean found = false;