Converted to EventBus instead of broadcast

This commit is contained in:
ByteHamster 2020-01-25 22:52:00 +01:00
parent e2ec07f607
commit 190b54ad70
5 changed files with 54 additions and 55 deletions

View File

@ -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() {

View File

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

View File

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

View File

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

View File

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