Skip to content

Commit

Permalink
Merge pull request #9746 from NyanCatTW1/issue9745
Browse files Browse the repository at this point in the history
Add an option to Ignore hardware media button events
  • Loading branch information
Stypox authored Feb 26, 2023
2 parents 2ee4c6e + a4767fc commit 3cb76e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
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 @@ -45,14 +52,25 @@ public void initPlayer() {
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));
sessionConnector.setPlayer(getForwardingPlayer());

// 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());
}

@Override
public void destroyPlayer() {
super.destroyPlayer();
player.getPrefs().unregisterOnSharedPreferenceChangeListener(this);
if (sessionConnector != null) {
sessionConnector.setMediaButtonEventHandler(null);
sessionConnector.setPlayer(null);
sessionConnector.setQueueNavigator(null);
sessionConnector = null;
Expand All @@ -74,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
1 change: 1 addition & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<string name="screen_brightness_key">screen_brightness_key</string>
<string name="screen_brightness_timestamp_key">screen_brightness_timestamp_key</string>
<string name="clear_queue_confirmation_key">clear_queue_confirmation_key</string>
<string name="ignore_hardware_media_buttons_key">ignore_hardware_media_buttons_key</string>

<string name="popup_saved_width_key">popup_saved_width</string>
<string name="popup_saved_x_key">popup_saved_x</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
<string name="clear_queue_confirmation_title">Ask for confirmation before clearing a queue</string>
<string name="clear_queue_confirmation_summary">Switching from one player to another may replace your queue</string>
<string name="clear_queue_confirmation_description">The active player queue will be replaced</string>
<string name="ignore_hardware_media_buttons_title">Ignore hardware media button events</string>
<string name="ignore_hardware_media_buttons_summary">Useful, for instance, if you are using a headset with broken physical buttons</string>
<string name="download_thumbnail_title">Load thumbnails</string>
<string name="download_thumbnail_summary">Turn off to prevent loading thumbnails, saving data and memory usage. Changes clear both in-memory and on-disk image cache</string>
<string name="show_comments_title">Show comments</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/video_audio_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,12 @@
app:singleLineTitle="false"
app:iconSpaceReserved="false" />

<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/ignore_hardware_media_buttons_key"
android:summary="@string/ignore_hardware_media_buttons_summary"
android:title="@string/ignore_hardware_media_buttons_title"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>

0 comments on commit 3cb76e4

Please sign in to comment.