2828import org .schabi .newpipe .player .ui .VideoPlayerUi ;
2929import org .schabi .newpipe .util .StreamTypeUtil ;
3030
31- import java .util .ArrayList ;
3231import java .util .List ;
32+ import java .util .Objects ;
3333import java .util .Optional ;
34+ import java .util .stream .Collectors ;
35+ import java .util .stream .IntStream ;
3436
3537public 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
0 commit comments