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 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 selectedSpeedList = Arrays.asList(UserPreferences - .getPlaybackSpeedArray()); + // Build the "isChecked" array so that multiChoice dialog is populated correctly + List 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); diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index 4f871e83b..1ba981d27 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -389,7 +389,7 @@ public class UserPreferences { return Float.parseFloat(prefs.getString(PREF_PLAYBACK_SPEED, "1.00")); } catch (NumberFormatException e) { Log.e(TAG, Log.getStackTraceString(e)); - UserPreferences.setPlaybackSpeed("1.00"); + UserPreferences.setPlaybackSpeed(1.0f); return 1.0f; } } @@ -399,7 +399,7 @@ public class UserPreferences { return Float.parseFloat(prefs.getString(PREF_VIDEO_PLAYBACK_SPEED, "1.00")); } catch (NumberFormatException e) { Log.e(TAG, Log.getStackTraceString(e)); - UserPreferences.setVideoPlaybackSpeed("1.00"); + UserPreferences.setVideoPlaybackSpeed(1.0f); return 1.0f; } } @@ -408,7 +408,7 @@ public class UserPreferences { return prefs.getBoolean(PREF_PLAYBACK_SKIP_SILENCE, false); } - public static String[] getPlaybackSpeedArray() { + public static float[] getPlaybackSpeedArray() { return readPlaybackSpeedArray(prefs.getString(PREF_PLAYBACK_SPEED_ARRAY, null)); } @@ -638,15 +638,15 @@ public class UserPreferences { .apply(); } - public static void setPlaybackSpeed(String speed) { + public static void setPlaybackSpeed(float speed) { prefs.edit() - .putString(PREF_PLAYBACK_SPEED, speed) + .putString(PREF_PLAYBACK_SPEED, String.valueOf(speed)) .apply(); } - public static void setVideoPlaybackSpeed(String speed) { + public static void setVideoPlaybackSpeed(float speed) { prefs.edit() - .putString(PREF_VIDEO_PLAYBACK_SPEED, speed) + .putString(PREF_VIDEO_PLAYBACK_SPEED, String.valueOf(speed)) .apply(); } @@ -769,24 +769,22 @@ public class UserPreferences { } } - private static String[] readPlaybackSpeedArray(String valueFromPrefs) { - String[] selectedSpeeds = null; - // If this preference hasn't been set yet, return the default options - if (valueFromPrefs == null) { - selectedSpeeds = new String[] { "0.75", "1.00", "1.25", "1.50", "1.75", "2.00" }; - } else { + private static float[] readPlaybackSpeedArray(String valueFromPrefs) { + if (valueFromPrefs != null) { try { JSONArray jsonArray = new JSONArray(valueFromPrefs); - selectedSpeeds = new String[jsonArray.length()]; + float[] selectedSpeeds = new float[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i++) { - selectedSpeeds[i] = jsonArray.getString(i); + selectedSpeeds[i] = (float) jsonArray.getDouble(i); } + return selectedSpeeds; } catch (JSONException e) { Log.e(TAG, "Got JSON error when trying to get speeds from JSONArray"); e.printStackTrace(); } } - return selectedSpeeds; + // If this preference hasn't been set yet, return the default options + return new float[] { 0.75f, 1.0f, 1.25f, 1.5f, 1.75f, 2.0f }; } public static String getMediaPlayer() {