From 59253db2e4ec8cced94541d97111429760c72a3d Mon Sep 17 00:00:00 2001 From: Andrew Booze <42682253+awbooze@users.noreply.github.com> Date: Fri, 24 Feb 2023 10:10:44 -0600 Subject: Add back skip buttons and more custom actions on Android Auto (#6050) --- .../core/service/playback/PlaybackService.java | 99 +++++++++++++++------- .../core/util/playback/PlaybackController.java | 7 -- 2 files changed, 68 insertions(+), 38 deletions(-) (limited to 'core/src/main/java/de/danoeh') diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 344421cd8..50517c303 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -57,6 +57,7 @@ import de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer; import de.danoeh.antennapod.playback.base.PlayerStatus; import de.danoeh.antennapod.playback.cast.CastPsmp; import de.danoeh.antennapod.playback.cast.CastStateListener; +import de.danoeh.antennapod.storage.preferences.UserPreferences; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -74,7 +75,6 @@ import de.danoeh.antennapod.event.settings.SpeedPresetChangedEvent; import de.danoeh.antennapod.event.settings.VolumeAdaptionChangedEvent; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; -import de.danoeh.antennapod.storage.preferences.UserPreferences; import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; @@ -115,10 +115,13 @@ public class PlaybackService extends MediaBrowserServiceCompat { private static final String AVRCP_ACTION_META_CHANGED = "com.android.music.metachanged"; /** - * Custom action used by Android Wear, Android Auto + * Custom actions used by Android Wear, Android Auto, and Android (API 33+ only) */ + private static final String CUSTOM_ACTION_SKIP_TO_NEXT = "action.de.danoeh.antennapod.core.service.skipToNext"; private static final String CUSTOM_ACTION_FAST_FORWARD = "action.de.danoeh.antennapod.core.service.fastForward"; private static final String CUSTOM_ACTION_REWIND = "action.de.danoeh.antennapod.core.service.rewind"; + private static final String CUSTOM_ACTION_CHANGE_PLAYBACK_SPEED = + "action.de.danoeh.antennapod.core.service.changePlaybackSpeed"; /** * Set a max number of episodes to load for Android Auto, otherwise there could be performance issues @@ -1173,45 +1176,52 @@ public class PlaybackService extends MediaBrowserServiceCompat { } else { state = PlaybackStateCompat.STATE_NONE; } + sessionState.setState(state, getCurrentPosition(), getCurrentPlaybackSpeed()); - long capabilities = PlaybackStateCompat.ACTION_PLAY_PAUSE + long capabilities = PlaybackStateCompat.ACTION_PLAY + | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_REWIND | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_FAST_FORWARD - | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SEEK_TO | PlaybackStateCompat.ACTION_SET_PLAYBACK_SPEED; - UiModeManager uiModeManager = (UiModeManager) getApplicationContext() - .getSystemService(Context.UI_MODE_SERVICE); - if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR) { - sessionState.addCustomAction( + sessionState.setActions(capabilities); + + // On Android Auto, custom actions are added in the following order around the play button, if no default + // actions are present: Near left, near right, far left, far right, additional actions panel + PlaybackStateCompat.CustomAction.Builder rewindBuilder = new PlaybackStateCompat.CustomAction.Builder( + CUSTOM_ACTION_REWIND, + getString(R.string.rewind_label), + R.drawable.ic_notification_fast_rewind + ); + WearMediaSession.addWearExtrasToAction(rewindBuilder); + sessionState.addCustomAction(rewindBuilder.build()); + + PlaybackStateCompat.CustomAction.Builder fastForwardBuilder = new PlaybackStateCompat.CustomAction.Builder( + CUSTOM_ACTION_FAST_FORWARD, + getString(R.string.fast_forward_label), + R.drawable.ic_notification_fast_forward + ); + WearMediaSession.addWearExtrasToAction(fastForwardBuilder); + sessionState.addCustomAction(fastForwardBuilder.build()); + + sessionState.addCustomAction( new PlaybackStateCompat.CustomAction.Builder( - CUSTOM_ACTION_REWIND, - getString(R.string.rewind_label), R.drawable.ic_notification_fast_rewind) - .build()); - sessionState.addCustomAction( + CUSTOM_ACTION_CHANGE_PLAYBACK_SPEED, + getString(R.string.playback_speed), + R.drawable.ic_notification_playback_speed + ).build() + ); + sessionState.addCustomAction( new PlaybackStateCompat.CustomAction.Builder( - CUSTOM_ACTION_FAST_FORWARD, - getString(R.string.fast_forward_label), R.drawable.ic_notification_fast_forward) - .build()); - } else { - // This would give the PIP of videos a play button - capabilities = capabilities | PlaybackStateCompat.ACTION_PLAY; - if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_WATCH) { - WearMediaSession.sessionStateAddActionForWear(sessionState, - CUSTOM_ACTION_REWIND, - getString(R.string.rewind_label), - android.R.drawable.ic_media_rew); - WearMediaSession.sessionStateAddActionForWear(sessionState, - CUSTOM_ACTION_FAST_FORWARD, - getString(R.string.fast_forward_label), - android.R.drawable.ic_media_ff); - WearMediaSession.mediaSessionSetExtraForWear(mediaSession); - } - } + CUSTOM_ACTION_SKIP_TO_NEXT, + getString(R.string.skip_episode_label), + R.drawable.ic_notification_skip + ).build() + ); - sessionState.setActions(capabilities); + WearMediaSession.mediaSessionSetExtraForWear(mediaSession); mediaSession.setPlaybackState(sessionState.build()); } @@ -1548,6 +1558,13 @@ public class PlaybackService extends MediaBrowserServiceCompat { } public void setSpeed(float speed) { + PlaybackPreferences.setCurrentlyPlayingTemporaryPlaybackSpeed(speed); + if (currentMediaType == MediaType.VIDEO) { + UserPreferences.setVideoPlaybackSpeed(speed); + } else { + UserPreferences.setPlaybackSpeed(speed); + } + mediaPlayer.setPlaybackParams(speed, UserPreferences.isSkipSilence()); } @@ -1787,6 +1804,26 @@ public class PlaybackService extends MediaBrowserServiceCompat { onFastForward(); } else if (CUSTOM_ACTION_REWIND.equals(action)) { onRewind(); + } else if (CUSTOM_ACTION_SKIP_TO_NEXT.equals(action)) { + mediaPlayer.skip(); + } else if (CUSTOM_ACTION_CHANGE_PLAYBACK_SPEED.equals(action)) { + List selectedSpeeds = UserPreferences.getPlaybackSpeedArray(); + + // If the list has zero or one element, there's nothing we can do to change the playback speed. + if (selectedSpeeds.size() > 1) { + int speedPosition = selectedSpeeds.indexOf(mediaPlayer.getPlaybackSpeed()); + float newSpeed; + + if (speedPosition == selectedSpeeds.size() - 1) { + // This is the last element. Wrap instead of going over the size of the list. + newSpeed = selectedSpeeds.get(0); + } else { + // If speedPosition is still -1 (the user isn't using a preset), use the first preset in the + // list. + newSpeed = selectedSpeeds.get(speedPosition + 1); + } + onSetPlaybackSpeed(newSpeed); + } } } }; diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java index 7a0fcdc4c..c3b103501 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java @@ -399,13 +399,6 @@ public abstract class PlaybackController { } public void setPlaybackSpeed(float speed) { - PlaybackPreferences.setCurrentlyPlayingTemporaryPlaybackSpeed(speed); - if (getMedia() != null && getMedia().getMediaType() == MediaType.VIDEO) { - UserPreferences.setVideoPlaybackSpeed(speed); - } else { - UserPreferences.setPlaybackSpeed(speed); - } - if (playbackService != null) { playbackService.setSpeed(speed); } else { -- cgit v1.2.3