Show notification whilst ads are playing but hide playback actions.

Issue:#4535

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=205722036
This commit is contained in:
bachinger 2018-07-23 14:12:32 -07:00 committed by Oliver Woodman
parent 9c337c8806
commit 9d0ec37ead

View File

@ -745,7 +745,6 @@ public class PlayerNotificationManager {
* @return The {@link Notification} which has been built. * @return The {@link Notification} which has been built.
*/ */
protected Notification createNotification(Player player, @Nullable Bitmap largeIcon) { protected Notification createNotification(Player player, @Nullable Bitmap largeIcon) {
boolean isPlayingAd = player.isPlayingAd();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
List<String> actionNames = getActions(player); List<String> actionNames = getActions(player);
for (int i = 0; i < actionNames.size(); i++) { for (int i = 0; i < actionNames.size(); i++) {
@ -760,18 +759,18 @@ public class PlayerNotificationManager {
} }
// Create a media style notification. // Create a media style notification.
MediaStyle mediaStyle = new MediaStyle(); MediaStyle mediaStyle = new MediaStyle();
builder.setStyle(mediaStyle);
if (mediaSessionToken != null) { if (mediaSessionToken != null) {
mediaStyle.setMediaSession(mediaSessionToken); mediaStyle.setMediaSession(mediaSessionToken);
} }
mediaStyle.setShowActionsInCompactView(getActionIndicesForCompactView(player)); mediaStyle.setShowActionsInCompactView(getActionIndicesForCompactView(actionNames, player));
// Configure stop action (eg. when user dismisses the notification when !isOngoing). // Configure stop action (eg. when user dismisses the notification when !isOngoing).
boolean useStopAction = stopAction != null && !isPlayingAd; boolean useStopAction = stopAction != null;
mediaStyle.setShowCancelButton(useStopAction); mediaStyle.setShowCancelButton(useStopAction);
if (useStopAction && stopPendingIntent != null) { if (useStopAction && stopPendingIntent != null) {
builder.setDeleteIntent(stopPendingIntent); builder.setDeleteIntent(stopPendingIntent);
mediaStyle.setCancelButtonIntent(stopPendingIntent); mediaStyle.setCancelButtonIntent(stopPendingIntent);
} }
builder.setStyle(mediaStyle);
// Set notification properties from getters. // Set notification properties from getters.
builder builder
.setBadgeIconType(badgeIconType) .setBadgeIconType(badgeIconType)
@ -783,6 +782,7 @@ public class PlayerNotificationManager {
.setPriority(priority) .setPriority(priority)
.setDefaults(defaults); .setDefaults(defaults);
if (useChronometer if (useChronometer
&& !player.isPlayingAd()
&& !player.isCurrentWindowDynamic() && !player.isCurrentWindowDynamic()
&& player.getPlayWhenReady() && player.getPlayWhenReady()
&& player.getPlaybackState() == Player.STATE_READY) { && player.getPlaybackState() == Player.STATE_READY) {
@ -831,33 +831,36 @@ public class PlayerNotificationManager {
* name is ignored. * name is ignored.
*/ */
protected List<String> getActions(Player player) { protected List<String> getActions(Player player) {
boolean isPlayingAd = player.isPlayingAd();
List<String> stringActions = new ArrayList<>(); List<String> stringActions = new ArrayList<>();
if (!player.isPlayingAd()) { if (!isPlayingAd) {
if (useNavigationActions) { if (useNavigationActions) {
stringActions.add(ACTION_PREVIOUS); stringActions.add(ACTION_PREVIOUS);
} }
if (rewindMs > 0) { if (rewindMs > 0) {
stringActions.add(ACTION_REWIND); stringActions.add(ACTION_REWIND);
} }
if (usePlayPauseActions) { }
if (player.getPlayWhenReady()) { if (usePlayPauseActions) {
stringActions.add(ACTION_PAUSE); if (player.getPlayWhenReady()) {
} else { stringActions.add(ACTION_PAUSE);
stringActions.add(ACTION_PLAY); } else {
} stringActions.add(ACTION_PLAY);
} }
}
if (!isPlayingAd) {
if (fastForwardMs > 0) { if (fastForwardMs > 0) {
stringActions.add(ACTION_FAST_FORWARD); stringActions.add(ACTION_FAST_FORWARD);
} }
if (useNavigationActions && player.getNextWindowIndex() != C.INDEX_UNSET) { if (useNavigationActions && player.getNextWindowIndex() != C.INDEX_UNSET) {
stringActions.add(ACTION_NEXT); stringActions.add(ACTION_NEXT);
} }
if (customActionReceiver != null) { }
stringActions.addAll(customActionReceiver.getCustomActions(player)); if (customActionReceiver != null) {
} stringActions.addAll(customActionReceiver.getCustomActions(player));
if (ACTION_STOP.equals(stopAction)) { }
stringActions.add(stopAction); if (ACTION_STOP.equals(stopAction)) {
} stringActions.add(stopAction);
} }
return stringActions; return stringActions;
} }
@ -865,18 +868,18 @@ public class PlayerNotificationManager {
/** /**
* Gets an array with the indices of the buttons to be shown in compact mode. * Gets an array with the indices of the buttons to be shown in compact mode.
* *
* <p>This method can be overridden. The indices must refer to the list of actions returned by * <p>This method can be overridden. The indices must refer to the list of actions passed as the
* {@link #getActions(Player)}. * first parameter.
* *
* @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 state to build a notification.
*/ */
protected int[] getActionIndicesForCompactView(Player player) { protected int[] getActionIndicesForCompactView(List<String> actionNames, Player player) {
if (!usePlayPauseActions) { int pauseActionIndex = actionNames.indexOf(ACTION_PAUSE);
return new int[0]; int playActionIndex = actionNames.indexOf(ACTION_PLAY);
} return pauseActionIndex != -1
int actionIndex = useNavigationActions ? 1 : 0; ? new int[] {pauseActionIndex}
actionIndex += fastForwardMs > 0 ? 1 : 0; : (playActionIndex != -1 ? new int[] {playActionIndex} : new int[0]);
return new int[] {actionIndex};
} }
private static Map<String, NotificationCompat.Action> createPlaybackActions(Context context) { private static Map<String, NotificationCompat.Action> createPlaybackActions(Context context) {