Listen to ignore hardware buttons pref changes

This commit is contained in:
Stypox 2023-02-26 14:27:48 +01:00
parent 42d861688e
commit a4767fc48a
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23

View file

@ -3,6 +3,7 @@ package org.schabi.newpipe.player.mediasession;
import static org.schabi.newpipe.MainActivity.DEBUG; import static org.schabi.newpipe.MainActivity.DEBUG;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
@ -23,14 +24,20 @@ import org.schabi.newpipe.util.StreamTypeUtil;
import java.util.Optional; import java.util.Optional;
public class MediaSessionPlayerUi extends PlayerUi { public class MediaSessionPlayerUi extends PlayerUi
implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "MediaSessUi"; private static final String TAG = "MediaSessUi";
private MediaSessionCompat mediaSession; private MediaSessionCompat mediaSession;
private MediaSessionConnector sessionConnector; private MediaSessionConnector sessionConnector;
private final String ignoreHardwareMediaButtonsKey;
private boolean shouldIgnoreHardwareMediaButtons = false;
public MediaSessionPlayerUi(@NonNull final Player player) { public MediaSessionPlayerUi(@NonNull final Player player) {
super(player); super(player);
ignoreHardwareMediaButtonsKey =
context.getString(R.string.ignore_hardware_media_buttons_key);
} }
@Override @Override
@ -44,15 +51,15 @@ public class MediaSessionPlayerUi extends PlayerUi {
sessionConnector = new MediaSessionConnector(mediaSession); sessionConnector = new MediaSessionConnector(mediaSession);
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player)); sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));
sessionConnector.setPlayer(getForwardingPlayer()); sessionConnector.setPlayer(getForwardingPlayer());
sessionConnector.setMediaButtonEventHandler((p, i) -> {
// It seems like events from the Media Control UI // It seems like events from the Media Control UI in the notification area don't go through
// in the notification area don't go through this function, // this function, so it's safe to just ignore all events in case we want to ignore the
// so it's safe to just ignore all events in case we want to // hardware media buttons. Returning true stops all further event processing of the system.
// ignore the hardware media buttons. sessionConnector.setMediaButtonEventHandler((p, i) -> shouldIgnoreHardwareMediaButtons);
// Returning true stops all further event processing of the system
return player.getPrefs().getBoolean( // listen to changes to ignore_hardware_media_buttons_key
context.getString(R.string.ignore_hardware_media_buttons_key), false); updateShouldIgnoreHardwareMediaButtons(player.getPrefs());
}); player.getPrefs().registerOnSharedPreferenceChangeListener(this);
sessionConnector.setMetadataDeduplicationEnabled(true); sessionConnector.setMetadataDeduplicationEnabled(true);
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata()); sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
@ -61,6 +68,7 @@ public class MediaSessionPlayerUi extends PlayerUi {
@Override @Override
public void destroyPlayer() { public void destroyPlayer() {
super.destroyPlayer(); super.destroyPlayer();
player.getPrefs().unregisterOnSharedPreferenceChangeListener(this);
if (sessionConnector != null) { if (sessionConnector != null) {
sessionConnector.setMediaButtonEventHandler(null); sessionConnector.setMediaButtonEventHandler(null);
sessionConnector.setPlayer(null); sessionConnector.setPlayer(null);
@ -84,6 +92,20 @@ public class MediaSessionPlayerUi extends PlayerUi {
} }
@Override
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences,
final String key) {
if (key == null || key.equals(ignoreHardwareMediaButtonsKey)) {
updateShouldIgnoreHardwareMediaButtons(sharedPreferences);
}
}
public void updateShouldIgnoreHardwareMediaButtons(final SharedPreferences sharedPreferences) {
shouldIgnoreHardwareMediaButtons =
sharedPreferences.getBoolean(ignoreHardwareMediaButtonsKey, false);
}
public void handleMediaButtonIntent(final Intent intent) { public void handleMediaButtonIntent(final Intent intent) {
MediaButtonReceiver.handleIntent(mediaSession, intent); MediaButtonReceiver.handleIntent(mediaSession, intent);
} }