Skip to content

Add notification update reason API and allow manual notification updates #2454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ public final MediaNotification createNotification(
MediaSession mediaSession,
ImmutableList<CommandButton> mediaButtonPreferences,
MediaNotification.ActionFactory actionFactory,
Callback onNotificationChangedCallback) {
Callback onNotificationChangedCallback,
@MediaSessionService.NotificationUpdate int reason) {
ensureNotificationChannel();

ImmutableList.Builder<CommandButton> mediaButtonPreferencesWithEnabledCommandButtonsOnly =
Expand Down Expand Up @@ -354,7 +355,7 @@ public final MediaNotification createNotification(
} else {
pendingOnBitmapLoadedFutureCallback =
new OnBitmapLoadedFutureCallback(
notificationId, builder, onNotificationChangedCallback);
notificationId, builder, onNotificationChangedCallback, reason);
Futures.addCallback(
bitmapFuture,
pendingOnBitmapLoadedFutureCallback,
Expand Down Expand Up @@ -387,7 +388,7 @@ public final MediaNotification createNotification(
.setOngoing(false)
.setGroup(GROUP_KEY)
.build();
return new MediaNotification(notificationId, notification);
return createMediaNotification(notificationId, notification, reason, false);
}

@Override
Expand All @@ -408,6 +409,32 @@ public final void setSmallIcon(@DrawableRes int smallIconResourceId) {
this.smallIconResourceId = smallIconResourceId;
}

/**
* Create a {@link MediaNotification} instance.
*
* <p>This method is called each time a new notification is built, and when the large icon
* finishes loading.
*
* <p>Subclasses may override this method to intercept the created {@link Notification} object or
* its ID.
*
* @param notificationId Constructor parameter of {@link MediaNotification}
* @param notification Constructor parameter of {@link MediaNotification}
* @param reason Reason why the notification is being (re)created, useful for metrics or advanced
* customization.
* @param isRebuild true if the notification is being recreated because the album art finished
* loading
* @see MediaNotification#MediaNotification(int, Notification)
* @return The created instance.
*/
protected MediaNotification createMediaNotification(
int notificationId,
Notification notification,
@MediaSessionService.NotificationUpdate int reason,
boolean isRebuild) {
return new MediaNotification(notificationId, notification);
}

/**
* Returns the ordered list of {@linkplain CommandButton command buttons} to be used to build the
* notification.
Expand Down Expand Up @@ -640,20 +667,23 @@ private static long getPlaybackStartTimeEpochMs(Player player) {
}
}

private static class OnBitmapLoadedFutureCallback implements FutureCallback<Bitmap> {
private class OnBitmapLoadedFutureCallback implements FutureCallback<Bitmap> {
private final int notificationId;
private final NotificationCompat.Builder builder;
private final Callback onNotificationChangedCallback;
private final @MediaSessionService.NotificationUpdate int reason;

private boolean discarded;

public OnBitmapLoadedFutureCallback(
int notificationId,
NotificationCompat.Builder builder,
Callback onNotificationChangedCallback) {
Callback onNotificationChangedCallback,
@MediaSessionService.NotificationUpdate int reason) {
this.notificationId = notificationId;
this.builder = builder;
this.onNotificationChangedCallback = onNotificationChangedCallback;
this.reason = reason;
}

public void discardIfPending() {
Expand All @@ -665,7 +695,7 @@ public void onSuccess(Bitmap result) {
if (!discarded) {
builder.setLargeIcon(result);
onNotificationChangedCallback.onNotificationChanged(
new MediaNotification(notificationId, builder.build()));
createMediaNotification(notificationId, builder.build(), reason, true));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,15 @@ interface Callback {
* @param onNotificationChangedCallback A callback that the provider needs to notify when the
* notification has changed and needs to be posted again, for example after a bitmap has
* been loaded asynchronously.
* @param reason Reason why the notification is being (re)created, useful for metrics or
* advanced customization.
*/
MediaNotification createNotification(
MediaSession mediaSession,
ImmutableList<CommandButton> mediaButtonPreferences,
ActionFactory actionFactory,
Callback onNotificationChangedCallback);
Callback onNotificationChangedCallback,
@MediaSessionService.NotificationUpdate int reason);

/**
* Handles a notification's custom command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ public void setMediaNotificationProvider(MediaNotification.Provider mediaNotific
* @param session A session that needs notification update.
* @param startInForegroundRequired Whether the service is required to start in the foreground.
*/
public void updateNotification(MediaSession session, boolean startInForegroundRequired) {
public void updateNotification(
MediaSession session,
boolean startInForegroundRequired,
@MediaSessionService.NotificationUpdate int reason) {
if (!mediaSessionService.isSessionAdded(session) || !shouldShowNotification(session)) {
removeNotification();
return;
Expand All @@ -185,7 +188,7 @@ public void updateNotification(MediaSession session, boolean startInForegroundRe
() -> {
MediaNotification mediaNotification =
this.mediaNotificationProvider.createNotification(
session, mediaButtonPreferences, actionFactory, callback);
session, mediaButtonPreferences, actionFactory, callback, reason);
mainExecutor.execute(
() ->
updateNotificationInternal(
Expand All @@ -207,7 +210,8 @@ public void setShowNotificationForIdlePlayer(
List<MediaSession> sessions = mediaSessionService.getSessions();
for (int i = 0; i < sessions.size(); i++) {
mediaSessionService.onUpdateNotificationInternal(
sessions.get(i), /* startInForegroundWhenPaused= */ false);
sessions.get(i), /* startInForegroundWhenPaused= */ false,
MediaSessionService.NOTIFICATION_UPDATE_IDLE_PLAYER_SETTING_CHANGED);
}
}

Expand All @@ -217,7 +221,9 @@ public boolean handleMessage(Message msg) {
List<MediaSession> sessions = mediaSessionService.getSessions();
for (int i = 0; i < sessions.size(); i++) {
mediaSessionService.onUpdateNotificationInternal(
sessions.get(i), /* startInForegroundWhenPaused= */ false);
sessions.get(i),
/* startInForegroundWhenPaused= */ false,
MediaSessionService.NOTIFICATION_UPDATE_ENGAGED_TIMEOUT);
}
return true;
}
Expand Down Expand Up @@ -262,7 +268,9 @@ private boolean isAnySessionUserEngaged(boolean startInForegroundWhenPaused) {
List<MediaSession> sessions = mediaSessionService.getSessions();
for (int i = 0; i < sessions.size(); i++) {
mediaSessionService.onUpdateNotificationInternal(
sessions.get(i), /* startInForegroundWhenPaused= */ false);
sessions.get(i),
/* startInForegroundWhenPaused= */ false,
MediaSessionService.NOTIFICATION_UPDATE_ENGAGED_TIMEOUT_DISABLED);
}
}
}
Expand Down Expand Up @@ -402,22 +410,28 @@ public MediaControllerListener(MediaSessionService mediaSessionService, MediaSes
public void onConnected(boolean shouldShowNotification) {
if (shouldShowNotification) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
session,
/* startInForegroundWhenPaused= */ false,
MediaSessionService.NOTIFICATION_UPDATE_CONNECTED);
}
}

@Override
public void onMediaButtonPreferencesChanged(
MediaController controller, List<CommandButton> mediaButtonPreferences) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
session,
/* startInForegroundWhenPaused= */ false,
MediaSessionService.NOTIFICATION_UPDATE_BUTTON_PREFERENCES_CHANGED);
}

@Override
public void onAvailableSessionCommandsChanged(
MediaController controller, SessionCommands commands) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
session,
/* startInForegroundWhenPaused= */ false,
MediaSessionService.NOTIFICATION_UPDATE_SESSION_COMMANDS_CHANGED);
}

@Override
Expand All @@ -438,7 +452,9 @@ public void onDisconnected(MediaController controller) {
}
// We may need to hide the notification.
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
session,
/* startInForegroundWhenPaused= */ false,
MediaSessionService.NOTIFICATION_UPDATE_DISCONNECTED);
}

@Override
Expand All @@ -451,7 +467,9 @@ public void onEvents(Player player, Player.Events events) {
Player.EVENT_MEDIA_METADATA_CHANGED,
Player.EVENT_TIMELINE_CHANGED)) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
session,
/* startInForegroundWhenPaused= */ false,
MediaSessionService.NOTIFICATION_UPDATE_PLAYER_EVENT);
}
}
}
Expand Down
Loading