diff --git a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java index c17324750..d4af79645 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java @@ -3,7 +3,8 @@ package de.test.antennapod.service.playback; import android.content.Context; import androidx.test.filters.MediumTest; -import androidx.test.platform.app.InstrumentationRegistry; + +import de.danoeh.antennapod.core.feed.VolumeAdaptionSetting; import de.test.antennapod.EspressoTestUtils; import junit.framework.AssertionFailedError; @@ -125,7 +126,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, null, null); + FeedPreferences prefs = new FeedPreferences(f.getId(), false, FeedPreferences.AutoDeleteAction.NO, VolumeAdaptionSetting.OFF, null, null); f.setPreferences(prefs); f.setItems(new ArrayList<>()); FeedItem i = new FeedItem(0, "t", "i", "l", new Date(), FeedItem.UNPLAYED, f); diff --git a/app/src/main/java/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java index 50a8d0965..07f48f65d 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/OnlineFeedViewActivity.java @@ -14,7 +14,6 @@ import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; -import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; @@ -48,6 +47,7 @@ import de.danoeh.antennapod.core.event.DownloadEvent; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedPreferences; +import de.danoeh.antennapod.core.feed.VolumeAdaptionSetting; import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.glide.FastBlurTransformation; import de.danoeh.antennapod.core.preferences.UserPreferences; @@ -230,7 +230,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, username, password)); + feed.setPreferences(new FeedPreferences(0, false, FeedPreferences.AutoDeleteAction.GLOBAL, VolumeAdaptionSetting.OFF, username, password)); } String fileUrl = new File(getExternalCacheDir(), FileNameGenerator.generateFileName(feed.getDownload_url())).toString(); 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..bd5381df6 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java @@ -2,6 +2,7 @@ package de.danoeh.antennapod.fragment; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.os.Bundle; import androidx.preference.SwitchPreference; import androidx.preference.ListPreference; @@ -10,10 +11,13 @@ import android.util.Log; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.core.dialog.ConfirmationDialog; +import de.danoeh.antennapod.core.event.settings.VolumeAdaptionChangedEvent; 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.VolumeAdaptionSetting; import de.danoeh.antennapod.core.preferences.UserPreferences; +import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.dialog.AuthenticationDialog; @@ -23,6 +27,8 @@ import io.reactivex.MaybeOnSubscribe; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; +import org.greenrobot.eventbus.EventBus; + import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; @@ -73,11 +79,13 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat { setupAutoDownloadPreference(); setupKeepUpdatedPreference(); setupAutoDeletePreference(); + setupVolumeReductionPreferences(); setupAuthentificationPreference(); setupEpisodeFilterPreference(); setupPlaybackSpeedPreference(); updateAutoDeleteSummary(); + updateVolumeReductionValue(); updateAutoDownloadEnabled(); updatePlaybackSpeedPreference(); }, error -> Log.d(TAG, Log.getStackTraceString(error)), () -> { }); @@ -206,6 +214,49 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat { } } + private void setupVolumeReductionPreferences() { + ListPreference volumeReductionPreference = (ListPreference) findPreference("volumeReduction"); + volumeReductionPreference.setOnPreferenceChangeListener((preference, newValue) -> { + switch ((String) newValue) { + case "off": + feedPreferences.setVolumeAdaptionSetting(VolumeAdaptionSetting.OFF); + break; + case "light": + feedPreferences.setVolumeAdaptionSetting(VolumeAdaptionSetting.LIGHT_REDUCTION); + break; + case "heavy": + feedPreferences.setVolumeAdaptionSetting(VolumeAdaptionSetting.HEAVY_REDUCTION); + break; + } + feed.savePreferences(); + updateVolumeReductionValue(); + sendVolumeAdaptionChangedIntent(); + + return false; + }); + } + + private void sendVolumeAdaptionChangedIntent() { + EventBus.getDefault().post( + new VolumeAdaptionChangedEvent(feedPreferences.getVolumeAdaptionSetting(), feed.getId())); + } + + private void updateVolumeReductionValue() { + ListPreference volumeReductionPreference = (ListPreference) findPreference("volumeReduction"); + + switch (feedPreferences.getVolumeAdaptionSetting()) { + case OFF: + volumeReductionPreference.setValue("off"); + break; + case LIGHT_REDUCTION: + volumeReductionPreference.setValue("light"); + break; + case HEAVY_REDUCTION: + volumeReductionPreference.setValue("heavy"); + break; + } + } + private void setupKeepUpdatedPreference() { SwitchPreference pref = (SwitchPreference) findPreference("keepUpdated"); diff --git a/app/src/main/res/xml/feed_settings.xml b/app/src/main/res/xml/feed_settings.xml index 978f66299..08e7572af 100644 --- a/app/src/main/res/xml/feed_settings.xml +++ b/app/src/main/res/xml/feed_settings.xml @@ -28,6 +28,15 @@ android:summary="@string/feed_auto_download_global" android:key="autoDelete"/> + + 0; int autoDeleteActionIndex = cursor.getInt(indexAutoDeleteAction); AutoDeleteAction autoDeleteAction = AutoDeleteAction.values()[autoDeleteActionIndex]; + int volumeAdaptionValue = cursor.getInt(indexVolumeAdaption); + VolumeAdaptionSetting volumeAdaptionSetting = VolumeAdaptionSetting.fromInteger(volumeAdaptionValue); String username = cursor.getString(indexUsername); String password = cursor.getString(indexPassword); String includeFilter = cursor.getString(indexIncludeFilter); String excludeFilter = cursor.getString(indexExcludeFilter); float feedPlaybackSpeed = cursor.getFloat(indexFeedPlaybackSpeed); - return new FeedPreferences(feedId, autoDownload, autoRefresh, autoDeleteAction, username, password, new FeedFilter(includeFilter, excludeFilter), feedPlaybackSpeed); + return new FeedPreferences(feedId, autoDownload, autoRefresh, autoDeleteAction, volumeAdaptionSetting, username, password, new FeedFilter(includeFilter, excludeFilter), feedPlaybackSpeed); } /** @@ -144,10 +151,18 @@ public class FeedPreferences { return auto_delete_action; } + public VolumeAdaptionSetting getVolumeAdaptionSetting() { + return volumeAdaptionSetting; + } + public void setAutoDeleteAction(AutoDeleteAction auto_delete_action) { this.auto_delete_action = auto_delete_action; } + public void setVolumeAdaptionSetting(VolumeAdaptionSetting volumeAdaptionSetting) { + this.volumeAdaptionSetting = volumeAdaptionSetting; + } + public boolean getCurrentAutoDelete() { switch (auto_delete_action) { case GLOBAL: diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSetting.java b/core/src/main/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSetting.java new file mode 100644 index 000000000..bf4fc582a --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSetting.java @@ -0,0 +1,32 @@ +package de.danoeh.antennapod.core.feed; + +public enum VolumeAdaptionSetting { + OFF(0, 1.0f), + LIGHT_REDUCTION(1, 0.5f), + HEAVY_REDUCTION(2, 0.2f); + + private final int value; + private float adaptionFactor; + + VolumeAdaptionSetting(int value, float adaptionFactor) { + this.value = value; + this.adaptionFactor = adaptionFactor; + } + + public static VolumeAdaptionSetting fromInteger(int value) { + for (VolumeAdaptionSetting setting : values()) { + if (setting.value == value) { + return setting; + } + } + throw new IllegalArgumentException("Cannot map value to VolumeAdaptionSetting: " + value); + } + + public int toInteger() { + return value; + } + + public float getAdaptionFactor() { + return adaptionFactor; + } +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/FeedParserTask.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/FeedParserTask.java index 10d5bfa15..5900f84fc 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/FeedParserTask.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/handler/FeedParserTask.java @@ -4,6 +4,7 @@ import android.util.Log; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedPreferences; +import de.danoeh.antennapod.core.feed.VolumeAdaptionSetting; import de.danoeh.antennapod.core.service.download.DownloadRequest; import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.storage.DownloadRequester; @@ -37,7 +38,7 @@ public class FeedParserTask implements Callable { feed.setId(request.getFeedfileId()); feed.setDownloaded(true); feed.setPreferences(new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, - request.getUsername(), request.getPassword())); + VolumeAdaptionSetting.OFF, request.getUsername(), request.getPassword())); feed.setPageNr(request.getArguments().getInt(DownloadRequester.REQUEST_ARG_PAGE_NR, 0)); DownloadError reason = null; 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 485349cb1..9c32e42e0 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 @@ -12,11 +12,11 @@ import android.util.Log; import android.util.Pair; import android.view.SurfaceHolder; -import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter; import org.antennapod.audio.MediaPlayer; import java.io.File; import java.io.IOException; +import java.util.EnumSet; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; @@ -26,13 +26,17 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; 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.VolumeAdaptionSetting; import de.danoeh.antennapod.core.feed.util.PlaybackSpeedUtils; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.util.RewindAfterPauseUtils; import de.danoeh.antennapod.core.util.playback.AudioPlayer; import de.danoeh.antennapod.core.util.playback.IPlayer; import de.danoeh.antennapod.core.util.playback.Playable; +import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter; import de.danoeh.antennapod.core.util.playback.VideoPlayer; /** @@ -308,7 +312,10 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { acquireWifiLockIfNecessary(); setPlaybackParams(PlaybackSpeedUtils.getCurrentPlaybackSpeed(media), UserPreferences.isSkipSilence()); - setVolume(UserPreferences.getLeftVolume(), UserPreferences.getRightVolume()); + + float leftVolume = UserPreferences.getLeftVolume(); + float rightVolume = UserPreferences.getRightVolume(); + setVolume(leftVolume, rightVolume); if (playerStatus == PlayerStatus.PREPARED && media.getPosition() > 0) { int newPosition = RewindAfterPauseUtils.calculatePositionWithRewind( @@ -661,6 +668,15 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { */ private void setVolumeSync(float volumeLeft, float volumeRight) { playerLock.lock(); + Playable playable = getPlayable(); + if (playable instanceof FeedMedia) { + FeedMedia feedMedia = (FeedMedia) playable; + FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences(); + VolumeAdaptionSetting volumeAdaptionSetting = preferences.getVolumeAdaptionSetting(); + float adaptionFactor = volumeAdaptionSetting.getAdaptionFactor(); + volumeLeft *= adaptionFactor; + volumeRight *= adaptionFactor; + } mediaPlayer.setVolume(volumeLeft, volumeRight); Log.d(TAG, "Media player volume was set to " + volumeLeft + " " + volumeRight); playerLock.unlock(); diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index d53f7d669..2fb37cc05 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -1,6 +1,5 @@ package de.danoeh.antennapod.core.service.playback; -import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -51,6 +50,7 @@ import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.event.MessageEvent; import de.danoeh.antennapod.core.event.PlaybackPositionEvent; import de.danoeh.antennapod.core.event.ServiceEvent; +import de.danoeh.antennapod.core.event.settings.VolumeAdaptionChangedEvent; import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedItem; @@ -70,7 +70,6 @@ import de.danoeh.antennapod.core.storage.FeedSearcher; import de.danoeh.antennapod.core.feed.util.ImageResourceUtils; import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.NetworkUtils; -import de.danoeh.antennapod.core.util.QueueAccess; import de.danoeh.antennapod.core.util.gui.NotificationUtils; import de.danoeh.antennapod.core.util.playback.ExternalMedia; import de.danoeh.antennapod.core.util.playback.Playable; @@ -79,6 +78,7 @@ import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; /** * Controls the MediaPlayer that plays a FeedMedia-file @@ -277,6 +277,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { registerReceiver(audioBecomingNoisy, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); registerReceiver(skipCurrentEpisodeReceiver, new IntentFilter(ACTION_SKIP_CURRENT_EPISODE)); registerReceiver(pausePlayCurrentEpisodeReceiver, new IntentFilter(ACTION_PAUSE_PLAY_CURRENT_EPISODE)); + EventBus.getDefault().register(this); taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback); flavorHelper = new PlaybackServiceFlavorHelper(PlaybackService.this, flavorHelperCallback); @@ -1436,6 +1437,12 @@ public class PlaybackService extends MediaBrowserServiceCompat { } }; + @Subscribe + public void volumeAdaptionChanged(VolumeAdaptionChangedEvent event) { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, event.getFeedId(), event.getVolumeAdaptionSetting()); + } + public static MediaType getCurrentMediaType() { return currentMediaType; } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeUpdater.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeUpdater.java new file mode 100644 index 000000000..d03830387 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeUpdater.java @@ -0,0 +1,36 @@ +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.VolumeAdaptionSetting; +import de.danoeh.antennapod.core.util.playback.Playable; + +class PlaybackVolumeUpdater { + + public void updateVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, long feedId, + VolumeAdaptionSetting volumeAdaptionSetting) { + Playable playable = mediaPlayer.getPlayable(); + + if (playable instanceof FeedMedia) { + updateFeedMediaVolumeIfNecessary(mediaPlayer, feedId, volumeAdaptionSetting, (FeedMedia) playable); + } + } + + private void updateFeedMediaVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, long feedId, + VolumeAdaptionSetting volumeAdaptionSetting, FeedMedia feedMedia) { + if (feedMedia.getItem().getFeed().getId() == feedId) { + FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences(); + preferences.setVolumeAdaptionSetting(volumeAdaptionSetting); + + if (mediaPlayer.getPlayerStatus() == PlayerStatus.PLAYING) { + forceUpdateVolume(mediaPlayer); + } + } + } + + private void forceUpdateVolume(PlaybackServiceMediaPlayer mediaPlayer) { + mediaPlayer.pause(false, false); + mediaPlayer.resume(); + } + +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBUpgrader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBUpgrader.java index 0c8f89348..234ce8367 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBUpgrader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBUpgrader.java @@ -294,11 +294,14 @@ class DBUpgrader { db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS + " ADD COLUMN " + PodDBAdapter.KEY_FEED_PLAYBACK_SPEED + " REAL DEFAULT " + SPEED_USE_GLOBAL); } - if (oldVersion < 1070401) { db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS + " ADD COLUMN " + PodDBAdapter.KEY_SORT_ORDER + " TEXT"); } + if (oldVersion < 1090000) { + db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS + + " ADD COLUMN " + PodDBAdapter.KEY_FEED_VOLUME_ADAPTION + " INTEGER DEFAULT 0"); + } } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java index a7b9e44e6..17b79a3da 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java @@ -12,7 +12,6 @@ import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; -import android.os.Build; import android.text.TextUtils; import android.util.Log; @@ -97,6 +96,7 @@ public class PodDBAdapter { public static final String KEY_AUTO_DOWNLOAD = "auto_download"; public static final String KEY_KEEP_UPDATED = "keep_updated"; public static final String KEY_AUTO_DELETE_ACTION = "auto_delete_action"; + public static final String KEY_FEED_VOLUME_ADAPTION = "feed_volume_adaption"; public static final String KEY_PLAYED_DURATION = "played_duration"; public static final String KEY_USERNAME = "username"; public static final String KEY_PASSWORD = "password"; @@ -144,7 +144,8 @@ public class PodDBAdapter { + KEY_SORT_ORDER + " TEXT," + KEY_LAST_UPDATE_FAILED + " INTEGER DEFAULT 0," + KEY_AUTO_DELETE_ACTION + " INTEGER DEFAULT 0," - + KEY_FEED_PLAYBACK_SPEED + " REAL DEFAULT " + SPEED_USE_GLOBAL + ")"; + + KEY_FEED_PLAYBACK_SPEED + " REAL DEFAULT " + SPEED_USE_GLOBAL + "," + + KEY_FEED_VOLUME_ADAPTION + " INTEGER DEFAULT 0)"; private static final String CREATE_TABLE_FEED_ITEMS = "CREATE TABLE " + TABLE_NAME_FEED_ITEMS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE @@ -241,6 +242,7 @@ public class PodDBAdapter { TABLE_NAME_FEEDS + "." + KEY_SORT_ORDER, TABLE_NAME_FEEDS + "." + KEY_LAST_UPDATE_FAILED, TABLE_NAME_FEEDS + "." + KEY_AUTO_DELETE_ACTION, + TABLE_NAME_FEEDS + "." + KEY_FEED_VOLUME_ADAPTION, TABLE_NAME_FEEDS + "." + KEY_INCLUDE_FILTER, TABLE_NAME_FEEDS + "." + KEY_EXCLUDE_FILTER, TABLE_NAME_FEEDS + "." + KEY_FEED_PLAYBACK_SPEED @@ -403,6 +405,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_ADAPTION, prefs.getVolumeAdaptionSetting().toInteger()); values.put(KEY_USERNAME, prefs.getUsername()); values.put(KEY_PASSWORD, prefs.getPassword()); values.put(KEY_INCLUDE_FILTER, prefs.getFilter().getIncludeFilter()); @@ -1333,10 +1336,7 @@ public class PodDBAdapter { * Helper class for opening the Antennapod database. */ private static class PodDBHelper extends SQLiteOpenHelper { - - private static final int VERSION = 1070401; - - private final Context context; + private static final int VERSION = 1090000; /** * Constructor. @@ -1348,7 +1348,6 @@ public class PodDBAdapter { public PodDBHelper(final Context context, final String name, final CursorFactory factory) { super(context, name, factory, VERSION, new PodDbErrorHandler()); - this.context = context; } @Override @@ -1367,7 +1366,6 @@ public class PodDBAdapter { db.execSQL(CREATE_INDEX_FEEDMEDIA_FEEDITEM); db.execSQL(CREATE_INDEX_QUEUE_FEEDITEM); db.execSQL(CREATE_INDEX_SIMPLECHAPTERS_FEEDITEM); - } @Override diff --git a/core/src/main/res/drawable/ic_volume_adaption_grey.xml b/core/src/main/res/drawable/ic_volume_adaption_grey.xml new file mode 100644 index 000000000..fe39e1c70 --- /dev/null +++ b/core/src/main/res/drawable/ic_volume_adaption_grey.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/core/src/main/res/drawable/ic_volume_adaption_white.xml b/core/src/main/res/drawable/ic_volume_adaption_white.xml new file mode 100644 index 000000000..27d7c6e7b --- /dev/null +++ b/core/src/main/res/drawable/ic_volume_adaption_white.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/core/src/main/res/values/arrays.xml b/core/src/main/res/values/arrays.xml index facde2c59..dc79905cd 100644 --- a/core/src/main/res/values/arrays.xml +++ b/core/src/main/res/values/arrays.xml @@ -13,6 +13,18 @@ never + + @string/feed_volume_reduction_off + @string/feed_volume_reduction_light + @string/feed_volume_reduction_heavy + + + + off + light + heavy + + 0 15 diff --git a/core/src/main/res/values/attrs.xml b/core/src/main/res/values/attrs.xml index 755bbac58..53cf7b211 100644 --- a/core/src/main/res/values/attrs.xml +++ b/core/src/main/res/values/attrs.xml @@ -52,6 +52,7 @@ + diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index e0b1e206c..88fa07049 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -96,6 +96,11 @@ Apply to Previous Episodes The new Auto Download setting will automatically be applied to new episodes.\nDo you also want to apply it to previously published episodes? Auto Delete Episode + Volume Reduction + Turn down volume for episodes of this feed: \%s + Off + Light + Heavy \u0020parallel downloads Global default Always diff --git a/core/src/main/res/values/styles.xml b/core/src/main/res/values/styles.xml index 2ccd48353..f5b43629c 100644 --- a/core/src/main/res/values/styles.xml +++ b/core/src/main/res/values/styles.xml @@ -76,6 +76,7 @@ @drawable/ic_bookmark_grey600_24dp @drawable/ic_fab_edit_white @drawable/ic_key_grey600 + @drawable/ic_volume_adaption_grey @color/master_switch_background_light @color/highlight_light @@ -164,6 +165,7 @@ @drawable/ic_bookmark_white_24dp @drawable/ic_fab_edit_white @drawable/ic_key_white + @drawable/ic_volume_adaption_white @color/master_switch_background_dark @color/highlight_dark @style/PreferenceThemeOverlay.v14.Material diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java new file mode 100644 index 000000000..45c86cb83 --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java @@ -0,0 +1,64 @@ +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 VolumeAdaptionSettingTest { + + @Test + public void mapOffToInteger() { + VolumeAdaptionSetting setting = VolumeAdaptionSetting.OFF; + assertThat(setting.toInteger(), is(equalTo(0))); + } + + @Test + public void mapLightReductionToInteger() { + VolumeAdaptionSetting setting = VolumeAdaptionSetting.LIGHT_REDUCTION; + + assertThat(setting.toInteger(), is(equalTo(1))); + } + + @Test + public void mapHeavyReductionToInteger() { + VolumeAdaptionSetting setting = VolumeAdaptionSetting.HEAVY_REDUCTION; + + assertThat(setting.toInteger(), is(equalTo(2))); + } + + @Test + public void mapIntegerToVolumeAdaptionSetting() { + assertThat(VolumeAdaptionSetting.fromInteger(0), is(equalTo(VolumeAdaptionSetting.OFF))); + assertThat(VolumeAdaptionSetting.fromInteger(1), is(equalTo(VolumeAdaptionSetting.LIGHT_REDUCTION))); + assertThat(VolumeAdaptionSetting.fromInteger(2), is(equalTo(VolumeAdaptionSetting.HEAVY_REDUCTION))); + } + + @Test(expected = IllegalArgumentException.class) + public void cannotMapNegativeValues() { + VolumeAdaptionSetting.fromInteger(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void cannotMapValuesOutOfRange() { + VolumeAdaptionSetting.fromInteger(3); + } + + @Test + public void noAdaptionIfTurnedOff() { + float adaptionFactor = VolumeAdaptionSetting.OFF.getAdaptionFactor(); + assertEquals(1.0f, adaptionFactor, 0.01f); + } + + @Test + public void lightReductionYieldsHigherValueThanHeavyReduction() { + float lightReductionFactor = VolumeAdaptionSetting.LIGHT_REDUCTION.getAdaptionFactor(); + + float heavyReductionFactor = VolumeAdaptionSetting.HEAVY_REDUCTION.getAdaptionFactor(); + + 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/PlaybackVolumeUpdaterTest.java b/core/src/test/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeUpdaterTest.java new file mode 100644 index 000000000..22f67933f --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/service/playback/PlaybackVolumeUpdaterTest.java @@ -0,0 +1,225 @@ +package de.danoeh.antennapod.core.service.playback; + +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.VolumeAdaptionSetting; +import de.danoeh.antennapod.core.util.playback.Playable; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.Mockito.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class PlaybackVolumeUpdaterTest { + + private static final long FEED_ID = 42; + + private PlaybackServiceMediaPlayer mediaPlayer; + + @Before + public void setUp() { + mediaPlayer = mock(PlaybackServiceMediaPlayer.class); + } + + @Test + public void noChangeIfNoFeedMediaPlaying() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PAUSED); + + Playable noFeedMedia = mock(Playable.class); + when(mediaPlayer.getPlayable()).thenReturn(noFeedMedia); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void noChangeIfPlayerStatusIsError() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.ERROR); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void noChangeIfPlayerStatusIsIndeterminate() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.INDETERMINATE); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void noChangeIfPlayerStatusIsStopped() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.STOPPED); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void noChangeIfPlayableIsNoItemOfAffectedFeed() { + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PLAYING); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + when(feedMedia.getItem().getFeed().getId()).thenReturn(FEED_ID + 1); + + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.OFF); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void updatesPreferencesForLoadedFeedMediaIfPlayerStatusIsPaused() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PAUSED); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = feedMedia.getItem().getFeed().getPreferences(); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(feedPreferences, times(1)).setVolumeAdaptionSetting(VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void updatesPreferencesForLoadedFeedMediaIfPlayerStatusIsPrepared() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PREPARED); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = feedMedia.getItem().getFeed().getPreferences(); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(feedPreferences, times(1)).setVolumeAdaptionSetting(VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void updatesPreferencesForLoadedFeedMediaIfPlayerStatusIsInitializing() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.INITIALIZING); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = feedMedia.getItem().getFeed().getPreferences(); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(feedPreferences, times(1)).setVolumeAdaptionSetting(VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void updatesPreferencesForLoadedFeedMediaIfPlayerStatusIsPreparing() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PREPARING); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = feedMedia.getItem().getFeed().getPreferences(); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(feedPreferences, times(1)).setVolumeAdaptionSetting(VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void updatesPreferencesForLoadedFeedMediaIfPlayerStatusIsSeeking() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.SEEKING); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = feedMedia.getItem().getFeed().getPreferences(); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(feedPreferences, times(1)).setVolumeAdaptionSetting(VolumeAdaptionSetting.LIGHT_REDUCTION); + + verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); + verify(mediaPlayer, never()).resume(); + } + + @Test + public void updatesPreferencesAndForcesVolumeChangeForLoadedFeedMediaIfPlayerStatusIsPlaying() { + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); + + when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PLAYING); + + FeedMedia feedMedia = mockFeedMedia(); + when(mediaPlayer.getPlayable()).thenReturn(feedMedia); + FeedPreferences feedPreferences = feedMedia.getItem().getFeed().getPreferences(); + + playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.HEAVY_REDUCTION); + + verify(feedPreferences, times(1)).setVolumeAdaptionSetting(VolumeAdaptionSetting.HEAVY_REDUCTION); + + verify(mediaPlayer, times(1)).pause(false, false); + verify(mediaPlayer, times(1)).resume(); + } + + private FeedMedia mockFeedMedia() { + FeedMedia feedMedia = mock(FeedMedia.class); + FeedItem feedItem = mock(FeedItem.class); + Feed feed = mock(Feed.class); + FeedPreferences feedPreferences = mock(FeedPreferences.class); + + when(feedMedia.getItem()).thenReturn(feedItem); + when(feedItem.getFeed()).thenReturn(feed); + when(feed.getId()).thenReturn(FEED_ID); + when(feed.getPreferences()).thenReturn(feedPreferences); + return feedMedia; + } +}