diff options
author | ByteHamster <info@bytehamster.com> | 2020-01-20 00:34:12 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2020-01-20 15:33:24 +0100 |
commit | 7702c9ee8c6ef23afd71270ad4126de37eeb7dfb (patch) | |
tree | b168540929bb2cf8909d9b81159820bd7392c05c /app | |
parent | e0236361a4b6b7b55ff958a16948398268f2a1ad (diff) | |
download | AntennaPod-7702c9ee8c6ef23afd71270ad4126de37eeb7dfb.zip |
Convert playback speeds to float
Advantage: When setting the speed to a value that is not available for the button
using the audio controls dialog, we no longer jump to the lowest value. Instead,
we jump to the next bigger one.
Diffstat (limited to 'app')
5 files changed, 51 insertions, 59 deletions
diff --git a/app/src/androidTest/java/de/test/antennapod/ui/SpeedChangeTest.java b/app/src/androidTest/java/de/test/antennapod/ui/SpeedChangeTest.java index df32ed3c0..194d51a3c 100644 --- a/app/src/androidTest/java/de/test/antennapod/ui/SpeedChangeTest.java +++ b/app/src/androidTest/java/de/test/antennapod/ui/SpeedChangeTest.java @@ -66,8 +66,8 @@ public class SpeedChangeTest { List<FeedItem> queue = DBReader.getQueue(); PlaybackPreferences.writeMediaPlaying(queue.get(0).getMedia(), PlayerStatus.PAUSED, false); - UserPreferences.setPlaybackSpeedArray(new String[] {"1.00", "2.00", "3.00"}); - availableSpeeds = UserPreferences.getPlaybackSpeedArray(); + availableSpeeds = new String[] {"1.00", "2.00", "3.00"}; + UserPreferences.setPlaybackSpeedArray(availableSpeeds); EspressoTestUtils.tryKillPlaybackService(); activityRule.launchActivity(new Intent()); diff --git a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java index 7f8c14b03..5906cd214 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/AudioplayerActivity.java @@ -1,28 +1,25 @@ package de.danoeh.antennapod.activity; import android.content.Intent; -import androidx.core.view.ViewCompat; import android.text.TextUtils; import android.util.Log; import android.view.View; - -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.util.Locale; -import java.util.concurrent.atomic.AtomicBoolean; - import de.danoeh.antennapod.core.feed.MediaType; -import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.core.feed.util.PlaybackSpeedUtils; +import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.dialog.VariableSpeedDialog; +import java.text.DecimalFormat; +import java.util.concurrent.atomic.AtomicBoolean; + /** * Activity for playing audio files. */ public class AudioplayerActivity extends MediaplayerInfoActivity { private static final String TAG = "AudioPlayerActivity"; + private static final float EPSILON = 0.001f; private final AtomicBoolean isSetup = new AtomicBoolean(false); @@ -33,8 +30,8 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { playExternalMedia(getIntent(), MediaType.AUDIO); } else if (PlaybackService.isCasting()) { Intent intent = PlaybackService.getPlayerActivityIntent(this); - if (intent.getComponent() != null && - !intent.getComponent().getClassName().equals(AudioplayerActivity.class.getName())) { + if (intent.getComponent() != null + && !intent.getComponent().getClassName().equals(AudioplayerActivity.class.getName())) { saveCurrentFragment(); finish(); startActivity(intent); @@ -57,7 +54,7 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { @Override protected void updatePlaybackSpeedButton() { - if(butPlaybackSpeed == null) { + if (butPlaybackSpeed == null) { return; } if (controller == null) { @@ -66,14 +63,14 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { return; } updatePlaybackSpeedButtonText(); - ViewCompat.setAlpha(butPlaybackSpeed, controller.canSetPlaybackSpeed() ? 1.0f : 0.5f); + butPlaybackSpeed.setAlpha(controller.canSetPlaybackSpeed() ? 1.0f : 0.5f); butPlaybackSpeed.setVisibility(View.VISIBLE); txtvPlaybackSpeed.setVisibility(View.VISIBLE); } @Override protected void updatePlaybackSpeedButtonText() { - if(butPlaybackSpeed == null) { + if (butPlaybackSpeed == null) { return; } if (controller == null) { @@ -82,7 +79,7 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { return; } float speed = 1.0f; - if(controller.canSetPlaybackSpeed()) { + if (controller.canSetPlaybackSpeed()) { speed = PlaybackSpeedUtils.getCurrentPlaybackSpeed(controller.getMedia()); } String speedStr = new DecimalFormat("0.00").format(speed); @@ -91,54 +88,40 @@ public class AudioplayerActivity extends MediaplayerInfoActivity { @Override protected void setupGUI() { - if(isSetup.getAndSet(true)) { + if (isSetup.getAndSet(true)) { return; } super.setupGUI(); - if(butCastDisconnect != null) { + if (butCastDisconnect != null) { butCastDisconnect.setVisibility(View.GONE); } - if(butPlaybackSpeed != null) { + if (butPlaybackSpeed != null) { butPlaybackSpeed.setOnClickListener(v -> { if (controller == null) { return; } if (controller.canSetPlaybackSpeed()) { - String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray(); - DecimalFormatSymbols format = new DecimalFormatSymbols(Locale.US); - format.setDecimalSeparator('.'); + float[] availableSpeeds = UserPreferences.getPlaybackSpeedArray(); + float currentSpeed = controller.getCurrentPlaybackSpeedMultiplier(); - float currentSpeedValue = controller.getCurrentPlaybackSpeedMultiplier(); - String currentSpeed = new DecimalFormat("0.00", format).format(currentSpeedValue); + int newSpeedIndex = 0; + while (newSpeedIndex < availableSpeeds.length + && availableSpeeds[newSpeedIndex] < currentSpeed + EPSILON) { + newSpeedIndex++; + } - // Provide initial value in case the speed list has changed - // out from under us - // and our current speed isn't in the new list - String newSpeed; - if (availableSpeeds.length > 0) { + float newSpeed; + if (availableSpeeds.length == 0) { + newSpeed = 1.0f; + } else if (newSpeedIndex == availableSpeeds.length) { newSpeed = availableSpeeds[0]; } else { - newSpeed = "1.00"; + newSpeed = availableSpeeds[newSpeedIndex]; } - for (int i = 0; i < availableSpeeds.length; i++) { - if (availableSpeeds[i].equals(currentSpeed)) { - if (i == availableSpeeds.length - 1) { - newSpeed = availableSpeeds[0]; - } else { - newSpeed = availableSpeeds[i + 1]; - } - break; - } - } - - try { - PlaybackPreferences.setCurrentlyPlayingTemporaryPlaybackSpeed(Float.parseFloat(newSpeed)); - } catch (NumberFormatException e) { - // Well this was awkward... - } + PlaybackPreferences.setCurrentlyPlayingTemporaryPlaybackSpeed(newSpeed); UserPreferences.setPlaybackSpeed(newSpeed); - controller.setPlaybackSpeed(Float.parseFloat(newSpeed)); + controller.setPlaybackSpeed(newSpeed); onPositionObserverUpdate(); } else { VariableSpeedDialog.showGetPluginDialog(this); diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/PlaybackControlsDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/PlaybackControlsDialog.java index 3e4e40a5b..5581a7140 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/PlaybackControlsDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/PlaybackControlsDialog.java @@ -98,11 +98,11 @@ public class PlaybackControlsDialog extends DialogFragment { final TextView txtvPlaybackSpeed = (TextView) dialog.findViewById(R.id.txtvPlaybackSpeed); float currentSpeed = getCurrentSpeed(); - String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray(); + float[] availableSpeeds = UserPreferences.getPlaybackSpeedArray(); final float minPlaybackSpeed = availableSpeeds.length > 1 ? - Float.valueOf(availableSpeeds[0]) : DEFAULT_MIN_PLAYBACK_SPEED; + availableSpeeds[0] : DEFAULT_MIN_PLAYBACK_SPEED; float maxPlaybackSpeed = availableSpeeds.length > 1 ? - Float.valueOf(availableSpeeds[availableSpeeds.length - 1]) : DEFAULT_MAX_PLAYBACK_SPEED; + availableSpeeds[availableSpeeds.length - 1] : DEFAULT_MAX_PLAYBACK_SPEED; int progressMax = (int) ((maxPlaybackSpeed - minPlaybackSpeed) / PLAYBACK_SPEED_STEP); barPlaybackSpeed.setMax(progressMax); @@ -113,13 +113,12 @@ public class PlaybackControlsDialog extends DialogFragment { if (controller != null && controller.canSetPlaybackSpeed()) { float playbackSpeed = progress * PLAYBACK_SPEED_STEP + minPlaybackSpeed; controller.setPlaybackSpeed(playbackSpeed); - String speedPref = String.format(Locale.US, "%.2f", playbackSpeed); PlaybackPreferences.setCurrentlyPlayingTemporaryPlaybackSpeed(playbackSpeed); if (isPlayingVideo) { - UserPreferences.setVideoPlaybackSpeed(speedPref); + UserPreferences.setVideoPlaybackSpeed(playbackSpeed); } else { - UserPreferences.setPlaybackSpeed(speedPref); + UserPreferences.setPlaybackSpeed(playbackSpeed); } String speedStr = String.format("%.2fx", playbackSpeed); diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/VariableSpeedDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/VariableSpeedDialog.java index ef624ebe6..3a6ba183f 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/VariableSpeedDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/VariableSpeedDialog.java @@ -6,8 +6,12 @@ import androidx.appcompat.app.AlertDialog; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.preferences.UserPreferences; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Locale; public class VariableSpeedDialog { @@ -43,15 +47,22 @@ public class VariableSpeedDialog { } private static void showSpeedSelectorDialog(final Context context) { + DecimalFormatSymbols format = new DecimalFormatSymbols(Locale.US); + format.setDecimalSeparator('.'); + DecimalFormat speedFormat = new DecimalFormat("0.00", format); + final String[] speedValues = context.getResources().getStringArray( R.array.playback_speed_values); // According to Java spec these get initialized to false on creation final boolean[] speedChecked = new boolean[speedValues.length]; - // Build the "isChecked" array so that multiChoice dialog is - // populated correctly - List<String> selectedSpeedList = Arrays.asList(UserPreferences - .getPlaybackSpeedArray()); + // Build the "isChecked" array so that multiChoice dialog is populated correctly + List<String> selectedSpeedList = new ArrayList<>(); + float[] selectedSpeeds = UserPreferences.getPlaybackSpeedArray(); + for (float speed : selectedSpeeds) { + selectedSpeedList.add(speedFormat.format(speed)); + } + for (int i = 0; i < speedValues.length; i++) { speedChecked[i] = selectedSpeedList.contains(speedValues[i]); } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java index bb1f8f8e9..38e9a285f 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java @@ -109,8 +109,7 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat { private void setupPlaybackSpeedPreference() { ListPreference feedPlaybackSpeedPreference = findPreference(PREF_FEED_PLAYBACK_SPEED); - String[] speeds = UserPreferences.getPlaybackSpeedArray(); - + final String[] speeds = getResources().getStringArray(R.array.playback_speed_values); String[] values = new String[speeds.length + 1]; values[0] = decimalFormat.format(SPEED_USE_GLOBAL); |