Skip to content

Commit 17e88f1

Browse files
committed
Do not update notification actions if nothing changed
This should avoid costly updates of the media session.
1 parent 5edafca commit 17e88f1

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import org.schabi.newpipe.player.ui.VideoPlayerUi;
2929
import org.schabi.newpipe.util.StreamTypeUtil;
3030

31-
import java.util.ArrayList;
3231
import java.util.List;
32+
import java.util.Objects;
3333
import java.util.Optional;
34+
import java.util.stream.Collectors;
35+
import java.util.stream.IntStream;
3436

3537
public class MediaSessionPlayerUi extends PlayerUi
3638
implements SharedPreferences.OnSharedPreferenceChangeListener {
@@ -42,6 +44,10 @@ public class MediaSessionPlayerUi extends PlayerUi
4244
private final String ignoreHardwareMediaButtonsKey;
4345
private boolean shouldIgnoreHardwareMediaButtons = false;
4446

47+
// used to check whether any notification action changed, before sending costly updates
48+
private List<NotificationActionData> prevNotificationActions = List.of();
49+
50+
4551
public MediaSessionPlayerUi(@NonNull final Player player) {
4652
super(player);
4753
ignoreHardwareMediaButtonsKey =
@@ -71,6 +77,10 @@ public void initPlayer() {
7177

7278
sessionConnector.setMetadataDeduplicationEnabled(true);
7379
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
80+
81+
// force updating media session actions by resetting the previous ones
82+
prevNotificationActions = List.of();
83+
updateMediaSessionActions();
7484
}
7585

7686
@Override
@@ -88,6 +98,7 @@ public void destroyPlayer() {
8898
mediaSession.release();
8999
mediaSession = null;
90100
}
101+
prevNotificationActions = List.of();
91102
}
92103

93104
@Override
@@ -187,23 +198,25 @@ private void updateMediaSessionActions() {
187198
return;
188199
}
189200

190-
final List<SessionConnectorActionProvider> actions = new ArrayList<>(2);
191-
for (int i = 3; i < 5; ++i) {
192-
// only use the fourth and fifth actions (the settings page also shows only the last 2)
193-
final int action = player.getPrefs().getInt(
194-
player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]),
195-
NotificationConstants.SLOT_DEFAULTS[i]);
196-
197-
@Nullable final NotificationActionData data =
198-
NotificationActionData.fromNotificationActionEnum(player, action);
199-
200-
if (data != null) {
201-
actions.add(new SessionConnectorActionProvider(data, context));
202-
}
201+
// only use the fourth and fifth actions (the settings page also shows only the last 2 on
202+
// Android 13+)
203+
final List<NotificationActionData> newNotificationActions = IntStream.of(3, 4)
204+
.map(i -> player.getPrefs().getInt(
205+
player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]),
206+
NotificationConstants.SLOT_DEFAULTS[i]))
207+
.mapToObj(action -> NotificationActionData
208+
.fromNotificationActionEnum(player, action))
209+
.filter(Objects::nonNull)
210+
.collect(Collectors.toList());
211+
212+
// avoid costly notification actions update, if nothing changed from last time
213+
if (!newNotificationActions.equals(prevNotificationActions)) {
214+
prevNotificationActions = newNotificationActions;
215+
sessionConnector.setCustomActionProviders(
216+
newNotificationActions.stream()
217+
.map(data -> new SessionConnectorActionProvider(data, context))
218+
.toArray(SessionConnectorActionProvider[]::new));
203219
}
204-
205-
sessionConnector.setCustomActionProviders(
206-
actions.toArray(new MediaSessionConnector.CustomActionProvider[0]));
207220
}
208221

209222
@Override

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.schabi.newpipe.R;
2121
import org.schabi.newpipe.player.Player;
2222

23+
import java.util.Objects;
24+
2325
public final class NotificationActionData {
2426
@Nullable
2527
private final String action;
@@ -50,7 +52,6 @@ public int icon() {
5052
return icon;
5153
}
5254

53-
5455
@Nullable
5556
public static NotificationActionData fromNotificationActionEnum(
5657
@NonNull final Player player,
@@ -165,4 +166,18 @@ public static NotificationActionData fromNotificationActionEnum(
165166
return null;
166167
}
167168
}
169+
170+
171+
@Override
172+
public boolean equals(@Nullable final Object obj) {
173+
return (obj instanceof NotificationActionData other)
174+
&& Objects.equals(this.action, other.action)
175+
&& this.name.equals(other.name)
176+
&& this.icon == other.icon;
177+
}
178+
179+
@Override
180+
public int hashCode() {
181+
return Objects.hash(action, name, icon);
182+
}
168183
}

0 commit comments

Comments
 (0)