#3248 Refactor enum mapping for more refactoring safety

This commit is contained in:
Max Bechtold 2019-08-26 11:09:39 +02:00
parent 5f43acbffe
commit b6fc27fe12
14 changed files with 102 additions and 27 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import de.danoeh.antennapod.core.feed.VolumeReductionSetting;
import de.test.antennapod.EspressoTestUtils;
import junit.framework.AssertionFailedError;
@ -129,7 +130,7 @@ public class PlaybackServiceMediaPlayerTest {
private Playable writeTestPlayable(String downloadUrl, String fileUrl) {
final Context c = getInstrumentation().getTargetContext();
Feed f = new Feed(0, null, "f", "l", "d", null, null, null, null, "i", null, null, "l", false);
FeedPreferences prefs = new FeedPreferences(f.getId(), false, FeedPreferences.AutoDeleteAction.NO, FeedPreferences.VolumeReductionSetting.OFF, null, null);
FeedPreferences prefs = new FeedPreferences(f.getId(), false, FeedPreferences.AutoDeleteAction.NO, VolumeReductionSetting.OFF, null, null);
f.setPreferences(prefs);
f.setItems(new ArrayList<>());
FeedItem i = new FeedItem(0, "t", "i", "l", new Date(), FeedItem.UNPLAYED, f);

View File

@ -32,6 +32,7 @@ import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import de.danoeh.antennapod.core.feed.VolumeReductionSetting;
import de.danoeh.antennapod.core.glide.FastBlurTransformation;
import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;
@ -270,7 +271,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
url = URLChecker.prepareURL(url);
feed = new Feed(url, null);
if (username != null && password != null) {
feed.setPreferences(new FeedPreferences(0, false, FeedPreferences.AutoDeleteAction.GLOBAL, FeedPreferences.VolumeReductionSetting.OFF, username, password));
feed.setPreferences(new FeedPreferences(0, false, FeedPreferences.AutoDeleteAction.GLOBAL, VolumeReductionSetting.OFF, username, password));
}
String fileUrl = new File(getExternalCacheDir(),
FileNameGenerator.generateFileName(feed.getDownload_url())).toString();

View File

@ -13,6 +13,7 @@ import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedFilter;
import de.danoeh.antennapod.core.feed.FeedPreferences;
import de.danoeh.antennapod.core.feed.VolumeReductionSetting;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.storage.DBWriter;
@ -123,13 +124,13 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat {
volumeReductionPreference.setOnPreferenceChangeListener((preference, newValue) -> {
switch ((String) newValue) {
case "off":
feedPreferences.setVolumeReductionSetting(FeedPreferences.VolumeReductionSetting.OFF);
feedPreferences.setVolumeReductionSetting(VolumeReductionSetting.OFF);
break;
case "light":
feedPreferences.setVolumeReductionSetting(FeedPreferences.VolumeReductionSetting.LIGHT);
feedPreferences.setVolumeReductionSetting(VolumeReductionSetting.LIGHT);
break;
case "heavy":
feedPreferences.setVolumeReductionSetting(FeedPreferences.VolumeReductionSetting.HEAVY);
feedPreferences.setVolumeReductionSetting(VolumeReductionSetting.HEAVY);
break;
}
feed.savePreferences();

View File

@ -160,7 +160,7 @@ public class Feed extends FeedFile implements ImageResource {
*/
public Feed(String url, String lastUpdate, String title, String username, String password) {
this(url, lastUpdate, title);
preferences = new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, FeedPreferences.VolumeReductionSetting.OFF, username, password);
preferences = new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, VolumeReductionSetting.OFF, username, password);
}
public static Feed fromCursor(Cursor cursor) {

View File

@ -27,11 +27,6 @@ public class FeedPreferences {
}
private AutoDeleteAction auto_delete_action;
public enum VolumeReductionSetting {
OFF,
LIGHT,
HEAVY
}
private VolumeReductionSetting volumeReductionSetting;
private String username;
@ -68,8 +63,8 @@ public class FeedPreferences {
boolean autoRefresh = cursor.getInt(indexAutoRefresh) > 0;
int autoDeleteActionIndex = cursor.getInt(indexAutoDeleteAction);
AutoDeleteAction autoDeleteAction = AutoDeleteAction.values()[autoDeleteActionIndex];
int volumeReductionIndex = cursor.getInt(indexVolumeReduction);
VolumeReductionSetting volumeReductionSetting = VolumeReductionSetting.values()[volumeReductionIndex];
int volumeReductionValue = cursor.getInt(indexVolumeReduction);
VolumeReductionSetting volumeReductionSetting = VolumeReductionSetting.fromInteger(volumeReductionValue);
String username = cursor.getString(indexUsername);
String password = cursor.getString(indexPassword);
String includeFilter = cursor.getString(indexIncludeFilter);

View File

@ -0,0 +1,26 @@
package de.danoeh.antennapod.core.feed;
public enum VolumeReductionSetting {
OFF(0),
LIGHT(1),
HEAVY(2);
private final int value;
VolumeReductionSetting(int value) {
this.value = value;
}
public static VolumeReductionSetting fromInteger(int value) {
for (VolumeReductionSetting setting : values()) {
if (setting.value == value) {
return setting;
}
}
throw new IllegalArgumentException("Cannot map value to VolumeReductionSetting: " + value);
}
public int toInteger() {
return value;
}
}

View File

@ -19,6 +19,7 @@ import android.util.Log;
import android.util.Pair;
import android.webkit.URLUtil;
import de.danoeh.antennapod.core.feed.VolumeReductionSetting;
import org.apache.commons.io.FileUtils;
import org.greenrobot.eventbus.EventBus;
import org.xml.sax.SAXException;
@ -764,7 +765,7 @@ public class DownloadService extends Service {
feed.setId(request.getFeedfileId());
feed.setDownloaded(true);
feed.setPreferences(new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL,
FeedPreferences.VolumeReductionSetting.OFF, request.getUsername(), request.getPassword()));
VolumeReductionSetting.OFF, request.getUsername(), request.getPassword()));
feed.setPageNr(request.getArguments().getInt(DownloadRequester.REQUEST_ARG_PAGE_NR, 0));
DownloadError reason = null;

View File

@ -1,14 +1,14 @@
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) {
FeedPreferences.VolumeReductionSetting volumeReductionSetting = preferences.getVolumeReductionSetting();
// TODO maxbechtold These numbers should be tested
if (volumeReductionSetting == FeedPreferences.VolumeReductionSetting.LIGHT) {
VolumeReductionSetting volumeReductionSetting = preferences.getVolumeReductionSetting();
if (volumeReductionSetting == VolumeReductionSetting.LIGHT) {
return 0.5f;
} else if (volumeReductionSetting == FeedPreferences.VolumeReductionSetting.HEAVY) {
} else if (volumeReductionSetting == VolumeReductionSetting.HEAVY) {
return 0.2f;
}
return 1.0f;

View File

@ -54,9 +54,9 @@ import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
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.SearchResult;
import de.danoeh.antennapod.core.feed.VolumeReductionSetting;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
@ -1533,7 +1533,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
String affectedFeed = intent.getStringExtra(EXTRA_VOLUME_REDUCTION_AFFECTED_FEED);
Serializable volumeReductionExtra = intent.getSerializableExtra(EXTRA_VOLUME_REDUCTION_SETTING);
FeedPreferences.VolumeReductionSetting volumeReductionSetting = (FeedPreferences.VolumeReductionSetting) volumeReductionExtra;
VolumeReductionSetting volumeReductionSetting = (VolumeReductionSetting) volumeReductionExtra;
PlaybackVolumeAdaptor playbackVolumeAdaptor = new PlaybackVolumeAdaptor();
playbackVolumeAdaptor.adaptVolumeIfNecessary(mediaPlayer, affectedFeed, volumeReductionSetting);

View File

@ -2,11 +2,12 @@ package de.danoeh.antennapod.core.service.playback;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.FeedPreferences;
import de.danoeh.antennapod.core.feed.VolumeReductionSetting;
import de.danoeh.antennapod.core.util.playback.Playable;
class PlaybackVolumeAdaptor {
public void adaptVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, String affectedFeedIdentifier, FeedPreferences.VolumeReductionSetting volumeReductionSetting) {
public void adaptVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, String affectedFeedIdentifier, VolumeReductionSetting volumeReductionSetting) {
Playable playable = mediaPlayer.getPlayable();
boolean isFeedMedia = playable instanceof FeedMedia;
boolean isPlayableLoaded = isPlayableLoaded(mediaPlayer.getPlayerStatus());
@ -16,7 +17,7 @@ class PlaybackVolumeAdaptor {
}
}
private void adaptFeedMediaVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, String affectedFeedIdentifier, FeedPreferences.VolumeReductionSetting volumeReductionSetting, FeedMedia feedMedia) {
private void adaptFeedMediaVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, String affectedFeedIdentifier, VolumeReductionSetting volumeReductionSetting, FeedMedia feedMedia) {
if (mediaBelongsToAffectedFeed(feedMedia, affectedFeedIdentifier)) {
FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences();
preferences.setVolumeReductionSetting(volumeReductionSetting);

View File

@ -407,7 +407,7 @@ public class PodDBAdapter {
values.put(KEY_AUTO_DOWNLOAD, prefs.getAutoDownload());
values.put(KEY_KEEP_UPDATED, prefs.getKeepUpdated());
values.put(KEY_AUTO_DELETE_ACTION, prefs.getAutoDeleteAction().ordinal());
values.put(KEY_FEED_VOLUME_REDUCTION, prefs.getVolumeReductionSetting().ordinal());
values.put(KEY_FEED_VOLUME_REDUCTION, prefs.getVolumeReductionSetting().toInteger());
values.put(KEY_USERNAME, prefs.getUsername());
values.put(KEY_PASSWORD, prefs.getPassword());
values.put(KEY_INCLUDE_FILTER, prefs.getFilter().getIncludeFilter());

View File

@ -0,0 +1,48 @@
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.assertThat;
public class VolumeReductionSettingTest {
@Test
public void mapOffToInteger() {
VolumeReductionSetting setting = VolumeReductionSetting.OFF;
assertThat(setting.toInteger(), is(equalTo(0)));
}
@Test
public void mapLightToInteger() {
VolumeReductionSetting setting = VolumeReductionSetting.LIGHT;
assertThat(setting.toInteger(), is(equalTo(1)));
}
@Test
public void mapHeavyToInteger() {
VolumeReductionSetting setting = VolumeReductionSetting.HEAVY;
assertThat(setting.toInteger(), is(equalTo(2)));
}
@Test
public void mapIntegerToVolumeReductionSetting() {
assertThat(VolumeReductionSetting.fromInteger(0), is(equalTo(VolumeReductionSetting.OFF)));
assertThat(VolumeReductionSetting.fromInteger(1), is(equalTo(VolumeReductionSetting.LIGHT)));
assertThat(VolumeReductionSetting.fromInteger(2), is(equalTo(VolumeReductionSetting.HEAVY)));
}
@Test(expected = IllegalArgumentException.class)
public void cannotMapNegativeValues() {
VolumeReductionSetting.fromInteger(-1);
}
@Test(expected = IllegalArgumentException.class)
public void cannotMapValuesOutOfRange() {
VolumeReductionSetting.fromInteger(3);
}
}

View File

@ -1,6 +1,7 @@
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;
@ -13,7 +14,7 @@ public class FeedVolumeReductionTest {
@Test
public void noReductionIfTurnedOff() {
FeedPreferences feedPreferences = mock(FeedPreferences.class);
when(feedPreferences.getVolumeReductionSetting()).thenReturn(FeedPreferences.VolumeReductionSetting.OFF);
when(feedPreferences.getVolumeReductionSetting()).thenReturn(VolumeReductionSetting.OFF);
FeedVolumeReduction feedVolumeReduction = new FeedVolumeReduction();
float reductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences);
@ -25,10 +26,10 @@ public class FeedVolumeReductionTest {
FeedPreferences feedPreferences = mock(FeedPreferences.class);
FeedVolumeReduction feedVolumeReduction = new FeedVolumeReduction();
when(feedPreferences.getVolumeReductionSetting()).thenReturn(FeedPreferences.VolumeReductionSetting.LIGHT);
when(feedPreferences.getVolumeReductionSetting()).thenReturn(VolumeReductionSetting.LIGHT);
float lightReductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences);
when(feedPreferences.getVolumeReductionSetting()).thenReturn(FeedPreferences.VolumeReductionSetting.HEAVY);
when(feedPreferences.getVolumeReductionSetting()).thenReturn(VolumeReductionSetting.HEAVY);
float heavyReductionFactor = feedVolumeReduction.getReductionFactor(feedPreferences);
assertTrue("Light reduction must have higher factor than heavy reduction", lightReductionFactor > heavyReductionFactor);

View File

@ -4,7 +4,7 @@ import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.FeedPreferences;
import de.danoeh.antennapod.core.feed.FeedPreferences.VolumeReductionSetting;
import de.danoeh.antennapod.core.feed.VolumeReductionSetting;
import de.danoeh.antennapod.core.util.playback.Playable;
import org.junit.Before;
import org.junit.Test;