From 42d861688eb00fae428476ef990066c8bcfee793 Mon Sep 17 00:00:00 2001
From: Nyan Cat <17372086+e666666@users.noreply.github.com>
Date: Sat, 4 Feb 2023 15:31:08 +0800
Subject: [PATCH 1/2] Implement Ignore hardware media button events option
---
.../player/mediasession/MediaSessionPlayerUi.java | 10 ++++++++++
app/src/main/res/values/settings_keys.xml | 1 +
app/src/main/res/values/strings.xml | 2 ++
app/src/main/res/xml/video_audio_settings.xml | 7 +++++++
4 files changed, 20 insertions(+)
diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java
index e9541ab06..3d19e0784 100644
--- a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java
+++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java
@@ -44,6 +44,15 @@ public class MediaSessionPlayerUi extends PlayerUi {
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);
+ });
sessionConnector.setMetadataDeduplicationEnabled(true);
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
@@ -53,6 +62,7 @@ public class MediaSessionPlayerUi extends PlayerUi {
public void destroyPlayer() {
super.destroyPlayer();
if (sessionConnector != null) {
+ sessionConnector.setMediaButtonEventHandler(null);
sessionConnector.setPlayer(null);
sessionConnector.setQueueNavigator(null);
sessionConnector = null;
diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml
index 126818969..0b2e58d04 100644
--- a/app/src/main/res/values/settings_keys.xml
+++ b/app/src/main/res/values/settings_keys.xml
@@ -25,6 +25,7 @@
screen_brightness_key
screen_brightness_timestamp_key
clear_queue_confirmation_key
+ ignore_hardware_media_buttons_key
popup_saved_width
popup_saved_x
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index a8d527ffb..06f8cf4f1 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -83,6 +83,8 @@
Ask for confirmation before clearing a queue
Switching from one player to another may replace your queue
The active player queue will be replaced
+ Ignore hardware media button events
+ Useful, for instance, if you are using a headset with broken physical buttons
Load thumbnails
Turn off to prevent loading thumbnails, saving data and memory usage. Changes clear both in-memory and on-disk image cache
Show comments
diff --git a/app/src/main/res/xml/video_audio_settings.xml b/app/src/main/res/xml/video_audio_settings.xml
index 8ff91548d..117ee8703 100644
--- a/app/src/main/res/xml/video_audio_settings.xml
+++ b/app/src/main/res/xml/video_audio_settings.xml
@@ -224,5 +224,12 @@
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
+
From a4767fc48aa5c33774038e8427effd41ee7f0e72 Mon Sep 17 00:00:00 2001
From: Stypox
Date: Sun, 26 Feb 2023 14:27:48 +0100
Subject: [PATCH 2/2] Listen to ignore hardware buttons pref changes
---
.../mediasession/MediaSessionPlayerUi.java | 42 ++++++++++++++-----
1 file changed, 32 insertions(+), 10 deletions(-)
diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java
index 3d19e0784..6f76a91d1 100644
--- a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java
+++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java
@@ -3,6 +3,7 @@ package org.schabi.newpipe.player.mediasession;
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;
@@ -23,14 +24,20 @@ import org.schabi.newpipe.util.StreamTypeUtil;
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
@@ -44,15 +51,15 @@ public class MediaSessionPlayerUi extends PlayerUi {
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());
@@ -61,6 +68,7 @@ public class MediaSessionPlayerUi extends PlayerUi {
@Override
public void destroyPlayer() {
super.destroyPlayer();
+ player.getPrefs().unregisterOnSharedPreferenceChangeListener(this);
if (sessionConnector != null) {
sessionConnector.setMediaButtonEventHandler(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) {
MediaButtonReceiver.handleIntent(mediaSession, intent);
}