Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow play/pausing from notification when buffering
Browse files Browse the repository at this point in the history
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.
Stypox committed Dec 29, 2023
1 parent 302afa0 commit b7c574e
Showing 3 changed files with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -40,12 +40,8 @@ public void onCustomAction(@NonNull final Player player,
@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();
}
}
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
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;
@@ -20,24 +21,22 @@
import org.schabi.newpipe.R;
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;
}
@@ -55,11 +54,12 @@ public int icon() {
@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;
}

@SuppressLint("PrivateResource") // we currently use Exoplayer's internal strings and icons
@Nullable
public static NotificationActionData fromNotificationActionEnum(
@NonNull final Player player,
@@ -113,8 +113,7 @@ public static NotificationActionData fromNotificationActionEnum(
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);
}
Original file line number Diff line number Diff line change
@@ -244,14 +244,8 @@ private void addAction(final NotificationCompat.Builder builder,
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));
}

0 comments on commit b7c574e

Please sign in to comment.