From c04f36bb9fd8ba659b7e95f16f3a5cad2521bdb9 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Thu, 22 Apr 2021 21:49:24 +0200 Subject: [PATCH] Decouple FeedMedia and PlaybackService --- .../adapter/actionbutton/ItemActionButton.java | 3 ++- .../adapter/actionbutton/PauseActionButton.java | 3 ++- .../de/danoeh/antennapod/fragment/ItemFragment.java | 3 ++- .../view/viewholder/EpisodeItemViewHolder.java | 5 +++-- .../de/danoeh/antennapod/core/feed/FeedMedia.java | 12 ------------ .../de/danoeh/antennapod/core/util/FeedItemUtil.java | 11 +++++++++++ 6 files changed, 20 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/ItemActionButton.java b/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/ItemActionButton.java index c694c0a0e..dbd5eaedb 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/ItemActionButton.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/ItemActionButton.java @@ -11,6 +11,7 @@ import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.DownloadRequester; +import de.danoeh.antennapod.core.util.FeedItemUtil; public abstract class ItemActionButton { FeedItem item; @@ -39,7 +40,7 @@ public abstract class ItemActionButton { } final boolean isDownloadingMedia = DownloadRequester.getInstance().isDownloadingFile(media); - if (media.isCurrentlyPlaying()) { + if (FeedItemUtil.isCurrentlyPlaying(media)) { return new PauseActionButton(item); } else if (item.getFeed().isLocalFeed()) { return new PlayLocalActionButton(item); diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/PauseActionButton.java b/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/PauseActionButton.java index ec6284e82..12d9eadf4 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/PauseActionButton.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/actionbutton/PauseActionButton.java @@ -6,6 +6,7 @@ import androidx.annotation.StringRes; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.IntentUtils; import static de.danoeh.antennapod.core.service.playback.PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE; @@ -35,7 +36,7 @@ public class PauseActionButton extends ItemActionButton { return; } - if (media.isCurrentlyPlaying()) { + if (FeedItemUtil.isCurrentlyPlaying(media)) { IntentUtils.sendLocalBroadcast(context, ACTION_PAUSE_PLAY_CURRENT_EPISODE); } } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java index 497d6f5b0..eae59eb2b 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java @@ -56,6 +56,7 @@ import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.DateUtils; +import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.ui.common.ThemeUtils; import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.core.util.playback.Timeline; @@ -336,7 +337,7 @@ public class ItemFragment extends Fragment { txtvDuration.setContentDescription( Converter.getDurationStringLocalized(getContext(), media.getDuration())); } - if (media.isCurrentlyPlaying()) { + if (FeedItemUtil.isCurrentlyPlaying(media)) { actionButton1 = new PauseActionButton(item); } else if (item.getFeed().isLocalFeed()) { actionButton1 = new PlayLocalActionButton(item); diff --git a/app/src/main/java/de/danoeh/antennapod/view/viewholder/EpisodeItemViewHolder.java b/app/src/main/java/de/danoeh/antennapod/view/viewholder/EpisodeItemViewHolder.java index 9fd742d5f..e83990d43 100644 --- a/app/src/main/java/de/danoeh/antennapod/view/viewholder/EpisodeItemViewHolder.java +++ b/app/src/main/java/de/danoeh/antennapod/view/viewholder/EpisodeItemViewHolder.java @@ -29,6 +29,7 @@ import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.DateUtils; +import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.NetworkUtils; import de.danoeh.antennapod.ui.common.ThemeUtils; import de.danoeh.antennapod.ui.common.CircularProgressBar; @@ -133,7 +134,7 @@ public class EpisodeItemViewHolder extends RecyclerView.ViewHolder { isVideo.setVisibility(media.getMediaType() == MediaType.VIDEO ? View.VISIBLE : View.GONE); duration.setVisibility(media.getDuration() > 0 ? View.VISIBLE : View.GONE); - if (media.isCurrentlyPlaying()) { + if (FeedItemUtil.isCurrentlyPlaying(media)) { itemView.setBackgroundColor(ThemeUtils.getColorFromAttr(activity, R.attr.currently_playing_background)); } else { itemView.setBackgroundResource(ThemeUtils.getDrawableFromAttr(activity, R.attr.selectableItemBackground)); @@ -213,7 +214,7 @@ public class EpisodeItemViewHolder extends RecyclerView.ViewHolder { } public boolean isCurrentlyPlayingItem() { - return item.getMedia() != null && item.getMedia().isCurrentlyPlaying(); + return item.getMedia() != null && FeedItemUtil.isCurrentlyPlaying(item.getMedia()); } public void notifyPlaybackPositionUpdated(PlaybackPositionEvent event) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java index 2aa6087a2..3fcd258d5 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java @@ -15,13 +15,10 @@ import java.util.Date; import java.util.List; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; -import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.util.playback.Playable; public class FeedMedia extends FeedFile implements Playable { - private static final String TAG = "FeedMedia"; - public static final int FEEDFILETYPE_FEEDMEDIA = 2; public static final int PLAYABLE_TYPE_FEEDMEDIA = 1; public static final String FILENAME_PREFIX_EMBEDDED_COVER = "metadata-retriever:"; @@ -162,15 +159,6 @@ public class FeedMedia extends FeedFile implements Playable { && PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == id; } - /** - * Reads playback preferences to determine whether this FeedMedia object is - * currently being played and the current player status is playing. - */ - public boolean isCurrentlyPlaying() { - return isPlaying() && PlaybackService.isRunning && - ((PlaybackPreferences.getCurrentPlayerStatus() == PlaybackPreferences.PLAYER_STATUS_PLAYING)); - } - @Override public int getTypeAsInt() { return FEEDFILETYPE_FEEDMEDIA; diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/FeedItemUtil.java b/core/src/main/java/de/danoeh/antennapod/core/util/FeedItemUtil.java index 9d3edd544..438739a6f 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/FeedItemUtil.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/FeedItemUtil.java @@ -4,6 +4,8 @@ import androidx.annotation.NonNull; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; +import de.danoeh.antennapod.core.preferences.PlaybackPreferences; +import de.danoeh.antennapod.core.service.playback.PlaybackService; import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; @@ -73,4 +75,13 @@ public class FeedItemUtil { int smartMarkAsPlayedSecs = UserPreferences.getSmartMarkAsPlayedSecs(); return media.getDuration() > 0 && media.getPosition() >= media.getDuration() - smartMarkAsPlayedSecs * 1000; } + + /** + * Reads playback preferences to determine whether this FeedMedia object is + * currently being played and the current player status is playing. + */ + public static boolean isCurrentlyPlaying(FeedMedia item) { + return item.isPlaying() && PlaybackService.isRunning + && ((PlaybackPreferences.getCurrentPlayerStatus() == PlaybackPreferences.PLAYER_STATUS_PLAYING)); + } }