Skip to content

Commit 4b1824e

Browse files
committed
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.
1 parent 17e88f1 commit 4b1824e

3 files changed

Lines changed: 15 additions & 21 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ public void onCustomAction(@NonNull final Player player,
4040
@Nullable
4141
@Override
4242
public PlaybackStateCompat.CustomAction getCustomAction(@NonNull final Player player) {
43-
if (data.action() == null) {
44-
return null;
45-
} else {
46-
return new PlaybackStateCompat.CustomAction.Builder(
47-
data.action(), data.name(), data.icon()
48-
).build();
49-
}
43+
return new PlaybackStateCompat.CustomAction.Builder(
44+
data.action(), data.name(), data.icon()
45+
).build();
5046
}
5147
}

app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT;
1212
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE;
1313

14+
import android.annotation.SuppressLint;
1415
import android.content.Context;
1516

1617
import androidx.annotation.DrawableRes;
@@ -23,21 +24,23 @@
2324
import java.util.Objects;
2425

2526
public final class NotificationActionData {
26-
@Nullable
27+
28+
@NonNull
2729
private final String action;
2830
@NonNull
2931
private final String name;
3032
@DrawableRes
3133
private final int icon;
3234

33-
public NotificationActionData(@Nullable final String action, @NonNull final String name,
35+
36+
public NotificationActionData(@NonNull final String action, @NonNull final String name,
3437
@DrawableRes final int icon) {
3538
this.action = action;
3639
this.name = name;
3740
this.icon = icon;
3841
}
3942

40-
@Nullable
43+
@NonNull
4144
public String action() {
4245
return action;
4346
}
@@ -52,6 +55,8 @@ public int icon() {
5255
return icon;
5356
}
5457

58+
59+
@SuppressLint("PrivateResource") // we currently use Exoplayer's internal strings and icons
5560
@Nullable
5661
public static NotificationActionData fromNotificationActionEnum(
5762
@NonNull final Player player,
@@ -105,8 +110,7 @@ public static NotificationActionData fromNotificationActionEnum(
105110
if (player.getCurrentState() == Player.STATE_PREFLIGHT
106111
|| player.getCurrentState() == Player.STATE_BLOCKED
107112
|| player.getCurrentState() == Player.STATE_BUFFERING) {
108-
// null intent action -> show hourglass icon that does nothing when clicked
109-
return new NotificationActionData(null,
113+
return new NotificationActionData(ACTION_PLAY_PAUSE,
110114
ctx.getString(R.string.notification_action_buffering),
111115
R.drawable.ic_hourglass_top);
112116
}
@@ -171,7 +175,7 @@ public static NotificationActionData fromNotificationActionEnum(
171175
@Override
172176
public boolean equals(@Nullable final Object obj) {
173177
return (obj instanceof NotificationActionData other)
174-
&& Objects.equals(this.action, other.action)
178+
&& this.action.equals(other.action)
175179
&& this.name.equals(other.name)
176180
&& this.icon == other.icon;
177181
}

app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,8 @@ private void addAction(final NotificationCompat.Builder builder,
244244
return;
245245
}
246246

247-
final PendingIntent intent;
248-
if (data.action() == null) {
249-
intent = null;
250-
} else {
251-
intent = PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID,
252-
new Intent(data.action()), FLAG_UPDATE_CURRENT, false);
253-
}
254-
247+
final PendingIntent intent = PendingIntentCompat.getBroadcast(player.getContext(),
248+
NOTIFICATION_ID, new Intent(data.action()), FLAG_UPDATE_CURRENT, false);
255249
builder.addAction(new NotificationCompat.Action(data.icon(), data.name(), intent));
256250
}
257251

0 commit comments

Comments
 (0)