From 4b1824e8c16aed9725ba970ca00b8cae37171cf6 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 29 Dec 2023 15:13:18 +0100 Subject: [PATCH] Allow play/pausing from notification when buffering This change is in line with a recent change in how the play/pause button behaves in the player ui: if the buffering indicator is shown, it's still possible to toggle play/pause, to allow e.g. pausing videos before they even start. This change was needed because on Android 13+ notification actions can't be null, and thus the buffering hourglass action wasn't shown. --- .../SessionConnectorActionProvider.java | 10 +++------- .../notification/NotificationActionData.java | 16 ++++++++++------ .../player/notification/NotificationUtil.java | 10 ++-------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java index 7b109c149..a5c9fccc9 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java @@ -40,12 +40,8 @@ public class SessionConnectorActionProvider implements MediaSessionConnector.Cus @Nullable @Override public PlaybackStateCompat.CustomAction getCustomAction(@NonNull final Player player) { - if (data.action() == null) { - return null; - } else { - return new PlaybackStateCompat.CustomAction.Builder( - data.action(), data.name(), data.icon() - ).build(); - } + return new PlaybackStateCompat.CustomAction.Builder( + data.action(), data.name(), data.icon() + ).build(); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java index 98ee3d7b8..b3abcd0b5 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java @@ -11,6 +11,7 @@ import static org.schabi.newpipe.player.notification.NotificationConstants.ACTIO import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT; import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE; +import android.annotation.SuppressLint; import android.content.Context; import androidx.annotation.DrawableRes; @@ -23,21 +24,23 @@ import org.schabi.newpipe.player.Player; import java.util.Objects; public final class NotificationActionData { - @Nullable + + @NonNull private final String action; @NonNull private final String name; @DrawableRes private final int icon; - public NotificationActionData(@Nullable final String action, @NonNull final String name, + + public NotificationActionData(@NonNull final String action, @NonNull final String name, @DrawableRes final int icon) { this.action = action; this.name = name; this.icon = icon; } - @Nullable + @NonNull public String action() { return action; } @@ -52,6 +55,8 @@ public final class NotificationActionData { return icon; } + + @SuppressLint("PrivateResource") // we currently use Exoplayer's internal strings and icons @Nullable public static NotificationActionData fromNotificationActionEnum( @NonNull final Player player, @@ -105,8 +110,7 @@ public final class NotificationActionData { if (player.getCurrentState() == Player.STATE_PREFLIGHT || player.getCurrentState() == Player.STATE_BLOCKED || player.getCurrentState() == Player.STATE_BUFFERING) { - // null intent action -> show hourglass icon that does nothing when clicked - return new NotificationActionData(null, + return new NotificationActionData(ACTION_PLAY_PAUSE, ctx.getString(R.string.notification_action_buffering), R.drawable.ic_hourglass_top); } @@ -171,7 +175,7 @@ public final class NotificationActionData { @Override public boolean equals(@Nullable final Object obj) { return (obj instanceof NotificationActionData other) - && Objects.equals(this.action, other.action) + && this.action.equals(other.action) && this.name.equals(other.name) && this.icon == other.icon; } diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index b3cfed1ce..30420b0c7 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -244,14 +244,8 @@ public final class NotificationUtil { return; } - final PendingIntent intent; - if (data.action() == null) { - intent = null; - } else { - intent = PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID, - new Intent(data.action()), FLAG_UPDATE_CURRENT, false); - } - + final PendingIntent intent = PendingIntentCompat.getBroadcast(player.getContext(), + NOTIFICATION_ID, new Intent(data.action()), FLAG_UPDATE_CURRENT, false); builder.addAction(new NotificationCompat.Action(data.icon(), data.name(), intent)); }