Search in sources :

Example 1 with Util

use of androidx.media3.common.util.Util in project media by androidx.

the class PlayerNotificationManager method createNotification.

/**
 * Creates the notification given the current session state.
 *
 * @param builder The builder used to build the last notification, or {@code null}. Re-using the
 *     builder when possible can prevent notification flicker when {@code Util#SDK_INT} < 21.
 * @param ongoing Whether the notification should be ongoing.
 * @param largeIcon The large icon to be used.
 * @return The {@link NotificationCompat.Builder} on which to call {@link
 *     NotificationCompat.Builder#build()} to obtain the notification, or {@code null} if no
 *     notification should be displayed.
 */
@Nullable
protected NotificationCompat.Builder createNotification(@Nullable NotificationCompat.Builder builder, boolean ongoing, @Nullable Bitmap largeIcon) {
    Player player = session.getPlayer();
    if (player.getPlaybackState() == Player.STATE_IDLE && player.getCurrentTimeline().isEmpty()) {
        return null;
    }
    if (builder == null) {
        builder = new NotificationCompat.Builder(context, channelId);
    }
    List<CommandButton> actionButtons = getActionButtons();
    for (int i = 0; i < actionButtons.size(); i++) {
        CommandButton button = actionButtons.get(i);
        NotificationCompat.Action action = new NotificationCompat.Action(button.iconResId, button.displayName, createBroadcastIntent(context, button, instanceId));
        builder.addAction(action);
    }
    MediaStyle mediaStyle = new MediaStyle(session);
    mediaStyle.setShowActionsInCompactView(getActionButtonIndicesForCompactView(actionButtons));
    // Configure dismiss action prior to API 21 ('x' button).
    mediaStyle.setShowCancelButton(!ongoing);
    mediaStyle.setCancelButtonIntent(dismissPendingIntent);
    builder.setStyle(mediaStyle);
    // Set intent which is sent if the user selects 'clear all'
    builder.setDeleteIntent(dismissPendingIntent);
    // Set notification properties from getters.
    builder.setBadgeIconType(badgeIconType).setOngoing(ongoing).setColor(color).setColorized(colorized).setSmallIcon(smallIconResourceId).setVisibility(visibility).setPriority(priority).setDefaults(defaults);
    // Changing "showWhen" causes notification flicker if SDK_INT < 21.
    if (Util.SDK_INT >= 21 && useChronometer && player.isPlaying() && !player.isPlayingAd() && !player.isCurrentWindowDynamic() && player.getPlaybackParameters().speed == 1f) {
        builder.setWhen(System.currentTimeMillis() - player.getContentPosition()).setShowWhen(true).setUsesChronometer(true);
    } else {
        builder.setShowWhen(false).setUsesChronometer(false);
    }
    // Set media specific notification properties from MediaDescriptionAdapter.
    builder.setContentTitle(mediaDescriptionAdapter.getCurrentContentTitle(session));
    builder.setContentText(mediaDescriptionAdapter.getCurrentContentText(session));
    builder.setSubText(mediaDescriptionAdapter.getCurrentSubText(session));
    if (largeIcon == null) {
        largeIcon = mediaDescriptionAdapter.getCurrentLargeIcon(session, new BitmapCallback(++currentNotificationTag));
    }
    setLargeIcon(builder, largeIcon);
    MediaController controller = getMediaControllerOrNull();
    if (controller != null) {
        builder.setContentIntent(controller.getSessionActivity());
    }
    if (groupKey != null) {
        builder.setGroup(groupKey);
    }
    builder.setOnlyAlertOnce(true);
    return builder;
}
Also used : Player(androidx.media3.common.Player) MediaStyle(androidx.media3.session.MediaStyleNotificationHelper.MediaStyle) NotificationCompat(androidx.core.app.NotificationCompat) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)1 NotificationCompat (androidx.core.app.NotificationCompat)1 Player (androidx.media3.common.Player)1 MediaStyle (androidx.media3.session.MediaStyleNotificationHelper.MediaStyle)1