Skip to content

Commit

Permalink
Listen to ignore hardware buttons pref changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Feb 26, 2023
1 parent 42d8616 commit a4767fc
Showing 1 changed file with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.schabi.newpipe.MainActivity.DEBUG;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
Expand All @@ -23,14 +24,20 @@

import java.util.Optional;

public class MediaSessionPlayerUi extends PlayerUi {
public class MediaSessionPlayerUi extends PlayerUi
implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "MediaSessUi";

private MediaSessionCompat mediaSession;
private MediaSessionConnector sessionConnector;

private final String ignoreHardwareMediaButtonsKey;
private boolean shouldIgnoreHardwareMediaButtons = false;

public MediaSessionPlayerUi(@NonNull final Player player) {
super(player);
ignoreHardwareMediaButtonsKey =
context.getString(R.string.ignore_hardware_media_buttons_key);
}

@Override
Expand All @@ -44,15 +51,15 @@ public void initPlayer() {
sessionConnector = new MediaSessionConnector(mediaSession);
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));
sessionConnector.setPlayer(getForwardingPlayer());
sessionConnector.setMediaButtonEventHandler((p, i) -> {
// It seems like events from the Media Control UI
// in the notification area don't go through this function,
// so it's safe to just ignore all events in case we want to
// ignore the hardware media buttons.
// Returning true stops all further event processing of the system
return player.getPrefs().getBoolean(
context.getString(R.string.ignore_hardware_media_buttons_key), false);
});

// It seems like events from the Media Control UI in the notification area don't go through
// this function, so it's safe to just ignore all events in case we want to ignore the
// hardware media buttons. Returning true stops all further event processing of the system.
sessionConnector.setMediaButtonEventHandler((p, i) -> shouldIgnoreHardwareMediaButtons);

// listen to changes to ignore_hardware_media_buttons_key
updateShouldIgnoreHardwareMediaButtons(player.getPrefs());
player.getPrefs().registerOnSharedPreferenceChangeListener(this);

sessionConnector.setMetadataDeduplicationEnabled(true);
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
Expand All @@ -61,6 +68,7 @@ public void initPlayer() {
@Override
public void destroyPlayer() {
super.destroyPlayer();
player.getPrefs().unregisterOnSharedPreferenceChangeListener(this);
if (sessionConnector != null) {
sessionConnector.setMediaButtonEventHandler(null);
sessionConnector.setPlayer(null);
Expand All @@ -84,6 +92,20 @@ public void onThumbnailLoaded(@Nullable final Bitmap bitmap) {
}


@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) {
MediaButtonReceiver.handleIntent(mediaSession, intent);
}
Expand Down

0 comments on commit a4767fc

Please sign in to comment.