From 14db72ac3532f2258df301074bc781d691d68658 Mon Sep 17 00:00:00 2001 From: Max Bechtold Date: Thu, 17 Oct 2019 11:54:11 +0200 Subject: [PATCH] #3248 Merge FeedVolumeReduction into VolumeReductionSetting --- .../core/feed/VolumeReductionSetting.java | 14 +++++-- .../service/playback/FeedVolumeReduction.java | 16 -------- .../core/service/playback/LocalPSMP.java | 7 ++-- .../core/feed/VolumeReductionSettingTest.java | 18 ++++++++- .../playback/FeedVolumeReductionTest.java | 37 ------------------- 5 files changed, 30 insertions(+), 62 deletions(-) delete mode 100644 core/src/main/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReduction.java delete mode 100644 core/src/test/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReductionTest.java diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/VolumeReductionSetting.java b/core/src/main/java/de/danoeh/antennapod/core/feed/VolumeReductionSetting.java index ec6db4872..53808580a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/VolumeReductionSetting.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/VolumeReductionSetting.java @@ -1,14 +1,16 @@ package de.danoeh.antennapod.core.feed; public enum VolumeReductionSetting { - OFF(0), - LIGHT(1), - HEAVY(2); + OFF(0, 1.0f), + LIGHT(1, 0.5f), + HEAVY(2, 0.2f); private final int value; + private float reductionFactor; - VolumeReductionSetting(int value) { + VolumeReductionSetting(int value, float reductionFactor) { this.value = value; + this.reductionFactor = reductionFactor; } public static VolumeReductionSetting fromInteger(int value) { @@ -23,4 +25,8 @@ public enum VolumeReductionSetting { public int toInteger() { return value; } + + public float getReductionFactor() { + return reductionFactor; + } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReduction.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReduction.java deleted file mode 100644 index 608e8375b..000000000 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReduction.java +++ /dev/null @@ -1,16 +0,0 @@ -package de.danoeh.antennapod.core.service.playback; - -import de.danoeh.antennapod.core.feed.FeedPreferences; -import de.danoeh.antennapod.core.feed.VolumeReductionSetting; - -public class FeedVolumeReduction { - float getReductionFactor(FeedPreferences preferences) { - VolumeReductionSetting volumeReductionSetting = preferences.getVolumeReductionSetting(); - if (volumeReductionSetting == VolumeReductionSetting.LIGHT) { - return 0.5f; - } else if (volumeReductionSetting == VolumeReductionSetting.HEAVY) { - return 0.2f; - } - return 1.0f; - } -} diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java index a09d6cb19..e5dd292f0 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java @@ -29,6 +29,7 @@ import java.util.concurrent.locks.ReentrantLock; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedPreferences; import de.danoeh.antennapod.core.feed.MediaType; +import de.danoeh.antennapod.core.feed.VolumeReductionSetting; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.util.RewindAfterPauseUtils; import de.danoeh.antennapod.core.util.playback.AudioPlayer; @@ -55,8 +56,6 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { private volatile boolean pausedBecauseOfTransientAudiofocusLoss; private volatile Pair videoSize; - private final FeedVolumeReduction feedVolumeReduction; - /** * Some asynchronous calls might change the state of the MediaPlayer object. Therefore calls in other threads * have to wait until these operations have finished. @@ -142,7 +141,6 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { super(context, callback); this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); this.playerLock = new PlayerLock(); - this.feedVolumeReduction = new FeedVolumeReduction(); this.startWhenPrepared = new AtomicBoolean(false); executor = new PlayerExecutor(); @@ -676,7 +674,8 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { if (playable instanceof FeedMedia) { FeedMedia feedMedia = (FeedMedia) playable; FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences(); - float reductionFactor = feedVolumeReduction.getReductionFactor(preferences); + VolumeReductionSetting volumeReductionSetting = preferences.getVolumeReductionSetting(); + float reductionFactor = volumeReductionSetting.getReductionFactor(); volumeLeft *= reductionFactor; volumeRight *= reductionFactor; } diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeReductionSettingTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeReductionSettingTest.java index 26c2132fe..a97fb6f20 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeReductionSettingTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeReductionSettingTest.java @@ -1,11 +1,12 @@ package de.danoeh.antennapod.core.feed; - import org.junit.Test; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; public class VolumeReductionSettingTest { @@ -45,4 +46,19 @@ public class VolumeReductionSettingTest { public void cannotMapValuesOutOfRange() { VolumeReductionSetting.fromInteger(3); } + + @Test + public void noReductionIfTurnedOff() { + float reductionFactor = VolumeReductionSetting.OFF.getReductionFactor(); + assertEquals(1.0f, reductionFactor, 0.01f); + } + + @Test + public void lightReductionYieldsHigherValueThanHeavyReduction() { + float lightReductionFactor = VolumeReductionSetting.LIGHT.getReductionFactor(); + + float heavyReductionFactor = VolumeReductionSetting.HEAVY.getReductionFactor(); + + assertTrue("Light reduction must have higher factor than heavy reduction", lightReductionFactor > heavyReductionFactor); + } } \ No newline at end of file diff --git a/core/src/test/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReductionTest.java b/core/src/test/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReductionTest.java deleted file mode 100644 index 109b361fe..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/service/playback/FeedVolumeReductionTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package de.danoeh.antennapod.core.service.playback; - -import de.danoeh.antennapod.core.feed.FeedPreferences; -import de.danoeh.antennapod.core.feed.VolumeReductionSetting; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class FeedVolumeReductionTest { - - @Test - public void noReductionIfTurnedOff() { - FeedPreferences feedPreferences = mock(FeedPreferences.class); - when(feedPreferences.getVolumeReductionSetting()).thenReturn(VolumeReductionSetting.OFF); - - FeedVolumeReduction feedVolumeReduction = new FeedVolumeReduction(); - float reductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences); - assertEquals(1.0f, reductionFactor, 0.01f ); - } - - @Test - public void lightReductionYieldsHigherValueThanHeavyReduction() { - FeedPreferences feedPreferences = mock(FeedPreferences.class); - FeedVolumeReduction feedVolumeReduction = new FeedVolumeReduction(); - - when(feedPreferences.getVolumeReductionSetting()).thenReturn(VolumeReductionSetting.LIGHT); - float lightReductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences); - - when(feedPreferences.getVolumeReductionSetting()).thenReturn(VolumeReductionSetting.HEAVY); - float heavyReductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences); - - assertTrue("Light reduction must have higher factor than heavy reduction", lightReductionFactor > heavyReductionFactor); - } -} \ No newline at end of file