Commit 4182f37b by olly Committed by Oliver Woodman

Make notification dismissible iff player is active

Seems like more useful default behaviour

PiperOrigin-RevId: 230356813
parent de3a749b
...@@ -235,15 +235,15 @@ public class PlayerNotificationManager { ...@@ -235,15 +235,15 @@ public class PlayerNotificationManager {
/** /**
* Called each time after the notification has been posted. * Called each time after the notification has been posted.
* *
* <p>The {@code isPlayerActive} flag indicates whether a service in which the player may run * <p>For a service, the {@code ongoing} flag can be used as an indicator as to whether it
* needs to be in the foreground. * should be in the foreground.
* *
* @param notificationId The id of the notification which has been posted. * @param notificationId The id of the notification which has been posted.
* @param notification The {@link Notification}. * @param notification The {@link Notification}.
* @param isPlayerActive {@code true} if the player is active. * @param ongoing Whether the notification is ongoing.
*/ */
default void onNotificationPosted( default void onNotificationPosted(
int notificationId, Notification notification, boolean isPlayerActive) {} int notificationId, Notification notification, boolean ongoing) {}
} }
/** Receives a {@link Bitmap}. */ /** Receives a {@link Bitmap}. */
...@@ -371,7 +371,6 @@ public class PlayerNotificationManager { ...@@ -371,7 +371,6 @@ public class PlayerNotificationManager {
private @DrawableRes int smallIconResourceId; private @DrawableRes int smallIconResourceId;
private int visibility; private int visibility;
private @Priority int priority; private @Priority int priority;
private boolean ongoing;
private boolean useChronometer; private boolean useChronometer;
private boolean wasPlayWhenReady; private boolean wasPlayWhenReady;
private int lastPlaybackState; private int lastPlaybackState;
...@@ -547,7 +546,6 @@ public class PlayerNotificationManager { ...@@ -547,7 +546,6 @@ public class PlayerNotificationManager {
intentFilter = new IntentFilter(); intentFilter = new IntentFilter();
useNavigationActions = true; useNavigationActions = true;
usePlayPauseActions = true; usePlayPauseActions = true;
ongoing = true;
colorized = true; colorized = true;
useChronometer = true; useChronometer = true;
color = Color.TRANSPARENT; color = Color.TRANSPARENT;
...@@ -790,22 +788,6 @@ public class PlayerNotificationManager { ...@@ -790,22 +788,6 @@ public class PlayerNotificationManager {
} }
/** /**
* Sets whether the notification should be ongoing. If {@code false} the user can dismiss the
* notification by swiping. If in addition the stop action is enabled dismissing the notification
* triggers the stop action.
*
* <p>See {@link NotificationCompat.Builder#setOngoing(boolean)}.
*
* @param ongoing Whether {@code true} the notification is ongoing and not dismissible.
*/
public final void setOngoing(boolean ongoing) {
if (this.ongoing != ongoing) {
this.ongoing = ongoing;
invalidate();
}
}
/**
* Sets the priority of the notification required for API 25 and lower. * Sets the priority of the notification required for API 25 and lower.
* *
* <p>See {@link NotificationCompat.Builder#setPriority(int)}. * <p>See {@link NotificationCompat.Builder#setPriority(int)}.
...@@ -904,7 +886,8 @@ public class PlayerNotificationManager { ...@@ -904,7 +886,8 @@ public class PlayerNotificationManager {
@Nullable @Nullable
private Notification startOrUpdateNotification(@Nullable Bitmap bitmap) { private Notification startOrUpdateNotification(@Nullable Bitmap bitmap) {
Player player = this.player; Player player = this.player;
Notification notification = createNotification(player, bitmap); boolean ongoing = getOngoing(player);
Notification notification = createNotification(player, ongoing, bitmap);
if (notification == null) { if (notification == null) {
stopNotification(/* dismissedByUser= */ false); stopNotification(/* dismissedByUser= */ false);
return null; return null;
...@@ -919,9 +902,7 @@ public class PlayerNotificationManager { ...@@ -919,9 +902,7 @@ public class PlayerNotificationManager {
} }
NotificationListener listener = notificationListener; NotificationListener listener = notificationListener;
if (listener != null) { if (listener != null) {
boolean isPlayerActive = listener.onNotificationPosted(notificationId, notification, ongoing);
player.getPlayWhenReady() && player.getPlaybackState() != Player.STATE_IDLE;
listener.onNotificationPosted(notificationId, notification, isPlayerActive);
} }
return notification; return notification;
} }
...@@ -942,12 +923,14 @@ public class PlayerNotificationManager { ...@@ -942,12 +923,14 @@ public class PlayerNotificationManager {
* Creates the notification given the current player state. * Creates the notification given the current player state.
* *
* @param player The player for which state to build a notification. * @param player The player for which state to build a notification.
* @param ongoing Whether the notification should be ongoing.
* @param largeIcon The large icon to be used. * @param largeIcon The large icon to be used.
* @return The {@link Notification} which has been built, or {@code null} if no notification * @return The {@link Notification} which has been built, or {@code null} if no notification
* should be displayed. * should be displayed.
*/ */
@Nullable @Nullable
protected Notification createNotification(Player player, @Nullable Bitmap largeIcon) { protected Notification createNotification(
Player player, boolean ongoing, @Nullable Bitmap largeIcon) {
if (player.getPlaybackState() == Player.STATE_IDLE) { if (player.getPlaybackState() == Player.STATE_IDLE) {
return null; return null;
} }
...@@ -970,12 +953,11 @@ public class PlayerNotificationManager { ...@@ -970,12 +953,11 @@ public class PlayerNotificationManager {
} }
mediaStyle.setShowActionsInCompactView(getActionIndicesForCompactView(actionNames, player)); mediaStyle.setShowActionsInCompactView(getActionIndicesForCompactView(actionNames, player));
// Configure dismiss action prior to API 21 ('x' button). // Configure dismiss action prior to API 21 ('x' button).
mediaStyle.setShowCancelButton(true); mediaStyle.setShowCancelButton(!ongoing);
mediaStyle.setCancelButtonIntent(dismissPendingIntent); mediaStyle.setCancelButtonIntent(dismissPendingIntent);
// Set intent which is sent if the user selects 'clear all' // Set intent which is sent if the user selects 'clear all'
builder.setDeleteIntent(dismissPendingIntent); builder.setDeleteIntent(dismissPendingIntent);
builder.setStyle(mediaStyle); builder.setStyle(mediaStyle);
// Set notification properties from getters. // Set notification properties from getters.
builder builder
.setBadgeIconType(badgeIconType) .setBadgeIconType(badgeIconType)
...@@ -1086,7 +1068,7 @@ public class PlayerNotificationManager { ...@@ -1086,7 +1068,7 @@ public class PlayerNotificationManager {
* first parameter. * first parameter.
* *
* @param actionNames The names of the actions included in the notification. * @param actionNames The names of the actions included in the notification.
* @param player The player for which state to build a notification. * @param player The player for which a notification is being built.
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
protected int[] getActionIndicesForCompactView(List<String> actionNames, Player player) { protected int[] getActionIndicesForCompactView(List<String> actionNames, Player player) {
...@@ -1097,6 +1079,13 @@ public class PlayerNotificationManager { ...@@ -1097,6 +1079,13 @@ public class PlayerNotificationManager {
: (playActionIndex != -1 ? new int[] {playActionIndex} : new int[0]); : (playActionIndex != -1 ? new int[] {playActionIndex} : new int[0]);
} }
/** Returns whether the generated notification should be ongoing. */
protected boolean getOngoing(Player player) {
int playbackState = player.getPlaybackState();
return (playbackState == Player.STATE_BUFFERING || playbackState == Player.STATE_READY)
&& player.getPlayWhenReady();
}
private static Map<String, NotificationCompat.Action> createPlaybackActions( private static Map<String, NotificationCompat.Action> createPlaybackActions(
Context context, int instanceId) { Context context, int instanceId) {
Map<String, NotificationCompat.Action> actions = new HashMap<>(); Map<String, NotificationCompat.Action> actions = new HashMap<>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment