#3248 Merge FeedVolumeReduction into VolumeReductionSetting

This commit is contained in:
Max Bechtold 2019-10-17 11:54:11 +02:00
parent 3bc55a09b1
commit 14db72ac35
5 changed files with 30 additions and 62 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<Integer, Integer> 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;
}

View File

@ -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);
}
}

View File

@ -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);
}
}