From 1297a168507d691a29092c89f46000736a753a04 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Wed, 3 Feb 2021 12:50:01 +0100 Subject: [PATCH] Allow remapping hardware buttons (#4881) --- .../preferences/PreferenceUpgrader.java | 12 ++++++ app/src/main/res/xml/preferences_playback.xml | 29 +++++++++------ .../core/preferences/UserPreferences.java | 15 +++++--- .../service/playback/PlaybackService.java | 37 ++++++++----------- core/src/main/res/values/arrays.xml | 14 +++++++ core/src/main/res/values/keycodes.xml | 9 +++++ core/src/main/res/values/strings.xml | 13 +++++-- 7 files changed, 85 insertions(+), 44 deletions(-) create mode 100644 core/src/main/res/values/keycodes.xml diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceUpgrader.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceUpgrader.java index 311f44881..03a8edbf0 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceUpgrader.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceUpgrader.java @@ -2,6 +2,7 @@ package de.danoeh.antennapod.preferences; import android.content.Context; import android.content.SharedPreferences; +import android.view.KeyEvent; import androidx.preference.PreferenceManager; import de.danoeh.antennapod.BuildConfig; @@ -92,5 +93,16 @@ public class PreferenceUpgrader { if (oldVersion < 1080100) { prefs.edit().putString(UserPreferences.PREF_VIDEO_BEHAVIOR, "pip").apply(); } + if (oldVersion < 2010300) { + // Migrate hardware button preferences + if (prefs.getBoolean("prefHardwareForwardButtonSkips", false)) { + prefs.edit().putString(UserPreferences.PREF_HARDWARE_FORWARD_BUTTON, + String.valueOf(KeyEvent.KEYCODE_MEDIA_NEXT)).apply(); + } + if (prefs.getBoolean("prefHardwarePreviousButtonRestarts", false)) { + prefs.edit().putString(UserPreferences.PREF_HARDWARE_PREVIOUS_BUTTON, + String.valueOf(KeyEvent.KEYCODE_MEDIA_PREVIOUS)).apply(); + } + } } } diff --git a/app/src/main/res/xml/preferences_playback.xml b/app/src/main/res/xml/preferences_playback.xml index d2999c59d..2be8492eb 100644 --- a/app/src/main/res/xml/preferences_playback.xml +++ b/app/src/main/res/xml/preferences_playback.xml @@ -44,18 +44,6 @@ - - + + + + + | skip button should actually skip + if (!notificationButton) { + // Handle remapped button as notification button which is not remapped again. + return handleKeycode(UserPreferences.getHardwareForwardButton(), true); + } else if (getStatus() == PlayerStatus.PLAYING || getStatus() == PlayerStatus.PAUSED) { mediaPlayer.skip(); - } else { - // assume skip command comes from a (bluetooth) media button - // user actually wants to fast-forward - seekDelta(UserPreferences.getFastForwardSecs() * 1000); + return true; } - return true; + return false; case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: if (getStatus() == PlayerStatus.PLAYING || getStatus() == PlayerStatus.PAUSED) { mediaPlayer.seekDelta(UserPreferences.getFastForwardSecs() * 1000); @@ -687,23 +683,20 @@ public class PlaybackService extends MediaBrowserServiceCompat { } return false; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: - if (getStatus() != PlayerStatus.PLAYING && getStatus() != PlayerStatus.PAUSED) { - return false; - } else if (UserPreferences.shouldHardwarePreviousButtonRestart()) { - // user wants to restart current episode + if (!notificationButton) { + // Handle remapped button as notification button which is not remapped again. + return handleKeycode(UserPreferences.getHardwarePreviousButton(), true); + } else if (getStatus() == PlayerStatus.PLAYING || getStatus() == PlayerStatus.PAUSED) { mediaPlayer.seekTo(0); - } else { - // user wants to rewind current episode - mediaPlayer.seekDelta(-UserPreferences.getRewindSecs() * 1000); + return true; } - return true; + return false; case KeyEvent.KEYCODE_MEDIA_REWIND: if (getStatus() == PlayerStatus.PLAYING || getStatus() == PlayerStatus.PAUSED) { mediaPlayer.seekDelta(-UserPreferences.getRewindSecs() * 1000); - } else { - return false; + return true; } - return true; + return false; case KeyEvent.KEYCODE_MEDIA_STOP: if (status == PlayerStatus.PLAYING) { mediaPlayer.pause(true, true); @@ -1911,7 +1904,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { Log.d(TAG, "onSkipToNext()"); UiModeManager uiModeManager = (UiModeManager) getApplicationContext() .getSystemService(Context.UI_MODE_SERVICE); - if (UserPreferences.shouldHardwareButtonSkip() + if (UserPreferences.getHardwareForwardButton() == KeyEvent.KEYCODE_MEDIA_NEXT || uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR) { mediaPlayer.skip(); } else { diff --git a/core/src/main/res/values/arrays.xml b/core/src/main/res/values/arrays.xml index 79ffb10ce..6492c1aa2 100644 --- a/core/src/main/res/values/arrays.xml +++ b/core/src/main/res/values/arrays.xml @@ -105,6 +105,20 @@ @string/episode_cleanup_never + + @string/button_action_fast_forward + @string/button_action_rewind + @string/button_action_skip_episode + @string/button_action_restart_episode + + + + @string/keycode_media_fast_forward + @string/keycode_media_rewind + @string/keycode_media_next + @string/keycode_media_previous + + @string/enqueue_location_back @string/enqueue_location_front diff --git a/core/src/main/res/values/keycodes.xml b/core/src/main/res/values/keycodes.xml new file mode 100644 index 000000000..e0d44ce04 --- /dev/null +++ b/core/src/main/res/values/keycodes.xml @@ -0,0 +1,9 @@ + + + 87 + 88 + 89 + 90 + diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 403f021f7..0cd8bcd54 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -381,6 +381,7 @@ External elements Interruptions Playback control + Reassign hardware buttons Search… No results Clear history @@ -390,10 +391,14 @@ Pause playback when headphones or bluetooth are disconnected Resume playback when the headphones are reconnected Resume playback when bluetooth reconnects - Forward Button Skips - When pressing a forward button on a bluetooth-connected device skip to the next episode instead of fast-forwarding - Previous button restarts - When pressing a hardware previous button restart playing the current episode instead of rewinding + Forward Button + Customize the forward button behavior + Previous Button + Customize the previous button behavior + Fast Forward + Rewind + Skip Episode + Restart Episode Jump to next queue item when playback completes Delete episode when playback completes Auto Delete