From 190b54ad70ccf25f869eb92b9699f0d90552974f Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sat, 25 Jan 2020 22:52:00 +0100 Subject: [PATCH] Converted to EventBus instead of broadcast --- .../fragment/FeedSettingsFragment.java | 10 +++--- .../settings/VolumeAdaptionChangedEvent.java | 21 ++++++++++++ .../service/playback/PlaybackService.java | 34 +++++-------------- .../playback/PlaybackVolumeUpdater.java | 15 ++++---- .../playback/PlaybackVolumeUpdaterTest.java | 29 ++++++++-------- 5 files changed, 54 insertions(+), 55 deletions(-) create mode 100644 core/src/main/java/de/danoeh/antennapod/core/event/settings/VolumeAdaptionChangedEvent.java 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 95352ea28..b58b14a86 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedSettingsFragment.java @@ -11,6 +11,7 @@ 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; @@ -26,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; @@ -234,11 +237,8 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat { } private void sendVolumeAdaptionChangedIntent() { - Context context = getContext(); - Intent intent = new Intent(PlaybackService.ACTION_VOLUME_ADAPTION_CHANGED).setPackage(context.getPackageName()); - intent.putExtra(PlaybackService.EXTRA_VOLUME_ADAPTION_AFFECTED_FEED, feed.getIdentifyingValue()); - intent.putExtra(PlaybackService.EXTRA_VOLUME_ADAPTION_SETTING, feedPreferences.getVolumeAdaptionSetting()); - context.sendBroadcast(intent); + EventBus.getDefault().post( + new VolumeAdaptionChangedEvent(feedPreferences.getVolumeAdaptionSetting(), feed.getId())); } private void updateVolumeReductionValue() { diff --git a/core/src/main/java/de/danoeh/antennapod/core/event/settings/VolumeAdaptionChangedEvent.java b/core/src/main/java/de/danoeh/antennapod/core/event/settings/VolumeAdaptionChangedEvent.java new file mode 100644 index 000000000..3ed84f6a8 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/event/settings/VolumeAdaptionChangedEvent.java @@ -0,0 +1,21 @@ +package de.danoeh.antennapod.core.event.settings; + +import de.danoeh.antennapod.core.feed.VolumeAdaptionSetting; + +public class VolumeAdaptionChangedEvent { + private final VolumeAdaptionSetting volumeAdaptionSetting; + private final long feedId; + + public VolumeAdaptionChangedEvent(VolumeAdaptionSetting volumeAdaptionSetting, long feedId) { + this.volumeAdaptionSetting = volumeAdaptionSetting; + this.feedId = feedId; + } + + public VolumeAdaptionSetting getVolumeAdaptionSetting() { + return volumeAdaptionSetting; + } + + public long getFeedId() { + return feedId; + } +} 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 577e482ec..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 @@ -41,7 +41,6 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; -import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -51,13 +50,13 @@ 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; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.feed.SearchResult; -import de.danoeh.antennapod.core.feed.VolumeAdaptionSetting; import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; @@ -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 @@ -136,13 +136,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { */ public static final String ACTION_PAUSE_PLAY_CURRENT_EPISODE = "action.de.danoeh.antennapod.core.service.pausePlayCurrentEpisode"; - /** - * If the PlaybackService receives this action, it will try to apply the supplied volume adaption setting. - */ - public static final String ACTION_VOLUME_ADAPTION_CHANGED = "action.de.danoeh.antennapod.core.service.volumedAdaptionChanged"; - public static final String EXTRA_VOLUME_ADAPTION_SETTING = "PlaybackService.VolumeAdaptionSettingExtra"; - public static final String EXTRA_VOLUME_ADAPTION_AFFECTED_FEED = "PlaybackService.VolumeAdaptionSettingAffectedFeed"; - /** * Custom action used by Android Wear */ @@ -284,7 +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)); - registerReceiver(volumeAdaptionChangedReceiver, new IntentFilter(ACTION_VOLUME_ADAPTION_CHANGED)); + EventBus.getDefault().register(this); taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback); flavorHelper = new PlaybackServiceFlavorHelper(PlaybackService.this, flavorHelperCallback); @@ -356,7 +349,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { unregisterReceiver(audioBecomingNoisy); unregisterReceiver(skipCurrentEpisodeReceiver); unregisterReceiver(pausePlayCurrentEpisodeReceiver); - unregisterReceiver(volumeAdaptionChangedReceiver); flavorHelper.removeCastConsumer(); flavorHelper.unregisterWifiBroadcastReceiver(); mediaPlayer.shutdown(); @@ -1445,21 +1437,11 @@ public class PlaybackService extends MediaBrowserServiceCompat { } }; - private final BroadcastReceiver volumeAdaptionChangedReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (TextUtils.equals(intent.getAction(), ACTION_VOLUME_ADAPTION_CHANGED)) { - Log.d(TAG, "Received ACTION_VOLUME_ADAPTION_CHANGED intent"); - - String affectedFeed = intent.getStringExtra(EXTRA_VOLUME_ADAPTION_AFFECTED_FEED); - Serializable volumeAdaptionExtra = intent.getSerializableExtra(EXTRA_VOLUME_ADAPTION_SETTING); - VolumeAdaptionSetting volumeAdaptionSetting = (VolumeAdaptionSetting) volumeAdaptionExtra; - - PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); - playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, affectedFeed, volumeAdaptionSetting); - } - } - }; + @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 index 9e8f34de5..0ef7e55bc 100644 --- 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 @@ -7,18 +7,20 @@ import de.danoeh.antennapod.core.util.playback.Playable; class PlaybackVolumeUpdater { - public void updateVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, String affectedFeedIdentifier, VolumeAdaptionSetting volumeAdaptionSetting) { + public void updateVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, long feedId, + VolumeAdaptionSetting volumeAdaptionSetting) { Playable playable = mediaPlayer.getPlayable(); boolean isFeedMedia = playable instanceof FeedMedia; boolean isPlayableLoaded = isPlayableLoaded(mediaPlayer.getPlayerStatus()); if (isFeedMedia && isPlayableLoaded) { - updateFeedMediaVolumeIfNecessary(mediaPlayer, affectedFeedIdentifier, volumeAdaptionSetting, (FeedMedia) playable); + updateFeedMediaVolumeIfNecessary(mediaPlayer, feedId, volumeAdaptionSetting, (FeedMedia) playable); } } - private void updateFeedMediaVolumeIfNecessary(PlaybackServiceMediaPlayer mediaPlayer, String affectedFeedIdentifier, VolumeAdaptionSetting volumeAdaptionSetting, FeedMedia feedMedia) { - if (mediaBelongsToAffectedFeed(feedMedia, affectedFeedIdentifier)) { + 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); @@ -37,11 +39,6 @@ class PlaybackVolumeUpdater { || playerStatus == PlayerStatus.INITIALIZING; } - private static boolean mediaBelongsToAffectedFeed(FeedMedia feedMedia, String affectedFeedIdentifier) { - return affectedFeedIdentifier != null - && affectedFeedIdentifier.equals(feedMedia.getItem().getFeed().getIdentifyingValue()); - } - private void forceUpdateVolume(PlaybackServiceMediaPlayer mediaPlayer) { mediaPlayer.pause(false, false); mediaPlayer.resume(); 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 index 02c3b6efb..a5a4ea760 100644 --- 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 @@ -18,12 +18,12 @@ import static org.mockito.Mockito.when; public class PlaybackVolumeUpdaterTest { - private static final String FEED_ID = "feedId"; + private static final long FEED_ID = 42; private PlaybackServiceMediaPlayer mediaPlayer; @Before - public void setUp() throws Exception { + public void setUp() { mediaPlayer = mock(PlaybackServiceMediaPlayer.class); } @@ -89,15 +89,14 @@ public class PlaybackVolumeUpdaterTest { @Test public void noChangeIfPlayableIsNoItemOfAffectedFeed() { - PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); - when(mediaPlayer.getPlayerStatus()).thenReturn(PlayerStatus.PLAYING); FeedMedia feedMedia = mock(FeedMedia.class); when(mediaPlayer.getPlayable()).thenReturn(feedMedia); - Feed feed = mockFeed(feedMedia, FEED_ID); + Feed feed = mockFeed(feedMedia); when(feed.getIdentifyingValue()).thenReturn("wrongFeedId"); + PlaybackVolumeUpdater playbackVolumeUpdater = new PlaybackVolumeUpdater(); playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.OFF); verify(mediaPlayer, never()).pause(anyBoolean(), anyBoolean()); @@ -112,7 +111,7 @@ public class PlaybackVolumeUpdaterTest { FeedMedia feedMedia = mock(FeedMedia.class); when(mediaPlayer.getPlayable()).thenReturn(feedMedia); - FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia); playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); @@ -130,7 +129,7 @@ public class PlaybackVolumeUpdaterTest { FeedMedia feedMedia = mock(FeedMedia.class); when(mediaPlayer.getPlayable()).thenReturn(feedMedia); - FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia); playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); @@ -148,7 +147,7 @@ public class PlaybackVolumeUpdaterTest { FeedMedia feedMedia = mock(FeedMedia.class); when(mediaPlayer.getPlayable()).thenReturn(feedMedia); - FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia); playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); @@ -166,7 +165,7 @@ public class PlaybackVolumeUpdaterTest { FeedMedia feedMedia = mock(FeedMedia.class); when(mediaPlayer.getPlayable()).thenReturn(feedMedia); - FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia); playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); @@ -184,7 +183,7 @@ public class PlaybackVolumeUpdaterTest { FeedMedia feedMedia = mock(FeedMedia.class); when(mediaPlayer.getPlayable()).thenReturn(feedMedia); - FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia); playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.LIGHT_REDUCTION); @@ -202,7 +201,7 @@ public class PlaybackVolumeUpdaterTest { FeedMedia feedMedia = mock(FeedMedia.class); when(mediaPlayer.getPlayable()).thenReturn(feedMedia); - FeedPreferences feedPreferences = mockFeedPreferences(feedMedia, FEED_ID); + FeedPreferences feedPreferences = mockFeedPreferences(feedMedia); playbackVolumeUpdater.updateVolumeIfNecessary(mediaPlayer, FEED_ID, VolumeAdaptionSetting.HEAVY_REDUCTION); @@ -212,18 +211,18 @@ public class PlaybackVolumeUpdaterTest { verify(mediaPlayer, times(1)).resume(); } - private FeedPreferences mockFeedPreferences(FeedMedia feedMedia, String feedId) { - Feed feed = mockFeed(feedMedia, feedId); + private FeedPreferences mockFeedPreferences(FeedMedia feedMedia) { + Feed feed = mockFeed(feedMedia); FeedPreferences feedPreferences = mock(FeedPreferences.class); when(feed.getPreferences()).thenReturn(feedPreferences); return feedPreferences; } - private Feed mockFeed(FeedMedia feedMedia, String feedId) { + private Feed mockFeed(FeedMedia feedMedia) { FeedItem feedItem = mock(FeedItem.class); when(feedMedia.getItem()).thenReturn(feedItem); Feed feed = mock(Feed.class); - when(feed.getIdentifyingValue()).thenReturn(feedId); + when(feed.getId()).thenReturn(FEED_ID); when(feedItem.getFeed()).thenReturn(feed); return feed; }