Do not update notification actions if nothing changed
This should avoid costly updates of the media session.
This commit is contained in:
parent
5edafca05a
commit
17e88f1749
|
@ -28,9 +28,11 @@ import org.schabi.newpipe.player.ui.PlayerUi;
|
||||||
import org.schabi.newpipe.player.ui.VideoPlayerUi;
|
import org.schabi.newpipe.player.ui.VideoPlayerUi;
|
||||||
import org.schabi.newpipe.util.StreamTypeUtil;
|
import org.schabi.newpipe.util.StreamTypeUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class MediaSessionPlayerUi extends PlayerUi
|
public class MediaSessionPlayerUi extends PlayerUi
|
||||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
@ -42,6 +44,10 @@ public class MediaSessionPlayerUi extends PlayerUi
|
||||||
private final String ignoreHardwareMediaButtonsKey;
|
private final String ignoreHardwareMediaButtonsKey;
|
||||||
private boolean shouldIgnoreHardwareMediaButtons = false;
|
private boolean shouldIgnoreHardwareMediaButtons = false;
|
||||||
|
|
||||||
|
// used to check whether any notification action changed, before sending costly updates
|
||||||
|
private List<NotificationActionData> prevNotificationActions = List.of();
|
||||||
|
|
||||||
|
|
||||||
public MediaSessionPlayerUi(@NonNull final Player player) {
|
public MediaSessionPlayerUi(@NonNull final Player player) {
|
||||||
super(player);
|
super(player);
|
||||||
ignoreHardwareMediaButtonsKey =
|
ignoreHardwareMediaButtonsKey =
|
||||||
|
@ -71,6 +77,10 @@ public class MediaSessionPlayerUi extends PlayerUi
|
||||||
|
|
||||||
sessionConnector.setMetadataDeduplicationEnabled(true);
|
sessionConnector.setMetadataDeduplicationEnabled(true);
|
||||||
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
|
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
|
||||||
|
|
||||||
|
// force updating media session actions by resetting the previous ones
|
||||||
|
prevNotificationActions = List.of();
|
||||||
|
updateMediaSessionActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -88,6 +98,7 @@ public class MediaSessionPlayerUi extends PlayerUi
|
||||||
mediaSession.release();
|
mediaSession.release();
|
||||||
mediaSession = null;
|
mediaSession = null;
|
||||||
}
|
}
|
||||||
|
prevNotificationActions = List.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -187,23 +198,25 @@ public class MediaSessionPlayerUi extends PlayerUi
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<SessionConnectorActionProvider> actions = new ArrayList<>(2);
|
// only use the fourth and fifth actions (the settings page also shows only the last 2 on
|
||||||
for (int i = 3; i < 5; ++i) {
|
// Android 13+)
|
||||||
// only use the fourth and fifth actions (the settings page also shows only the last 2)
|
final List<NotificationActionData> newNotificationActions = IntStream.of(3, 4)
|
||||||
final int action = player.getPrefs().getInt(
|
.map(i -> player.getPrefs().getInt(
|
||||||
player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]),
|
player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]),
|
||||||
NotificationConstants.SLOT_DEFAULTS[i]);
|
NotificationConstants.SLOT_DEFAULTS[i]))
|
||||||
|
.mapToObj(action -> NotificationActionData
|
||||||
|
.fromNotificationActionEnum(player, action))
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
@Nullable final NotificationActionData data =
|
// avoid costly notification actions update, if nothing changed from last time
|
||||||
NotificationActionData.fromNotificationActionEnum(player, action);
|
if (!newNotificationActions.equals(prevNotificationActions)) {
|
||||||
|
prevNotificationActions = newNotificationActions;
|
||||||
if (data != null) {
|
sessionConnector.setCustomActionProviders(
|
||||||
actions.add(new SessionConnectorActionProvider(data, context));
|
newNotificationActions.stream()
|
||||||
}
|
.map(data -> new SessionConnectorActionProvider(data, context))
|
||||||
|
.toArray(SessionConnectorActionProvider[]::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionConnector.setCustomActionProviders(
|
|
||||||
actions.toArray(new MediaSessionConnector.CustomActionProvider[0]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -20,6 +20,8 @@ import androidx.annotation.Nullable;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
import org.schabi.newpipe.player.Player;
|
import org.schabi.newpipe.player.Player;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public final class NotificationActionData {
|
public final class NotificationActionData {
|
||||||
@Nullable
|
@Nullable
|
||||||
private final String action;
|
private final String action;
|
||||||
|
@ -50,7 +52,6 @@ public final class NotificationActionData {
|
||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static NotificationActionData fromNotificationActionEnum(
|
public static NotificationActionData fromNotificationActionEnum(
|
||||||
@NonNull final Player player,
|
@NonNull final Player player,
|
||||||
|
@ -165,4 +166,18 @@ public final class NotificationActionData {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(@Nullable final Object obj) {
|
||||||
|
return (obj instanceof NotificationActionData other)
|
||||||
|
&& Objects.equals(this.action, other.action)
|
||||||
|
&& this.name.equals(other.name)
|
||||||
|
&& this.icon == other.icon;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(action, name, icon);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue