Decouple FeedMedia and PlaybackService

This commit is contained in:
ByteHamster 2021-04-22 21:49:24 +02:00
parent 762140bccd
commit c04f36bb9f
6 changed files with 20 additions and 17 deletions

View File

@ -11,6 +11,7 @@ import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.FeedItemUtil;
public abstract class ItemActionButton { public abstract class ItemActionButton {
FeedItem item; FeedItem item;
@ -39,7 +40,7 @@ public abstract class ItemActionButton {
} }
final boolean isDownloadingMedia = DownloadRequester.getInstance().isDownloadingFile(media); final boolean isDownloadingMedia = DownloadRequester.getInstance().isDownloadingFile(media);
if (media.isCurrentlyPlaying()) { if (FeedItemUtil.isCurrentlyPlaying(media)) {
return new PauseActionButton(item); return new PauseActionButton(item);
} else if (item.getFeed().isLocalFeed()) { } else if (item.getFeed().isLocalFeed()) {
return new PlayLocalActionButton(item); return new PlayLocalActionButton(item);

View File

@ -6,6 +6,7 @@ import androidx.annotation.StringRes;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.IntentUtils;
import static de.danoeh.antennapod.core.service.playback.PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE; import static de.danoeh.antennapod.core.service.playback.PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE;
@ -35,7 +36,7 @@ public class PauseActionButton extends ItemActionButton {
return; return;
} }
if (media.isCurrentlyPlaying()) { if (FeedItemUtil.isCurrentlyPlaying(media)) {
IntentUtils.sendLocalBroadcast(context, ACTION_PAUSE_PLAY_CURRENT_EPISODE); IntentUtils.sendLocalBroadcast(context, ACTION_PAUSE_PLAY_CURRENT_EPISODE);
} }
} }

View File

@ -56,6 +56,7 @@ import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.DateUtils; 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.ui.common.ThemeUtils;
import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.danoeh.antennapod.core.util.playback.Timeline; import de.danoeh.antennapod.core.util.playback.Timeline;
@ -336,7 +337,7 @@ public class ItemFragment extends Fragment {
txtvDuration.setContentDescription( txtvDuration.setContentDescription(
Converter.getDurationStringLocalized(getContext(), media.getDuration())); Converter.getDurationStringLocalized(getContext(), media.getDuration()));
} }
if (media.isCurrentlyPlaying()) { if (FeedItemUtil.isCurrentlyPlaying(media)) {
actionButton1 = new PauseActionButton(item); actionButton1 = new PauseActionButton(item);
} else if (item.getFeed().isLocalFeed()) { } else if (item.getFeed().isLocalFeed()) {
actionButton1 = new PlayLocalActionButton(item); actionButton1 = new PlayLocalActionButton(item);

View File

@ -29,6 +29,7 @@ import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.DateUtils; 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.core.util.NetworkUtils;
import de.danoeh.antennapod.ui.common.ThemeUtils; import de.danoeh.antennapod.ui.common.ThemeUtils;
import de.danoeh.antennapod.ui.common.CircularProgressBar; 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); isVideo.setVisibility(media.getMediaType() == MediaType.VIDEO ? View.VISIBLE : View.GONE);
duration.setVisibility(media.getDuration() > 0 ? 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)); itemView.setBackgroundColor(ThemeUtils.getColorFromAttr(activity, R.attr.currently_playing_background));
} else { } else {
itemView.setBackgroundResource(ThemeUtils.getDrawableFromAttr(activity, R.attr.selectableItemBackground)); itemView.setBackgroundResource(ThemeUtils.getDrawableFromAttr(activity, R.attr.selectableItemBackground));
@ -213,7 +214,7 @@ public class EpisodeItemViewHolder extends RecyclerView.ViewHolder {
} }
public boolean isCurrentlyPlayingItem() { public boolean isCurrentlyPlayingItem() {
return item.getMedia() != null && item.getMedia().isCurrentlyPlaying(); return item.getMedia() != null && FeedItemUtil.isCurrentlyPlaying(item.getMedia());
} }
public void notifyPlaybackPositionUpdated(PlaybackPositionEvent event) { public void notifyPlaybackPositionUpdated(PlaybackPositionEvent event) {

View File

@ -15,13 +15,10 @@ import java.util.Date;
import java.util.List; import java.util.List;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences; 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.storage.DBWriter;
import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.util.playback.Playable;
public class FeedMedia extends FeedFile implements 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 FEEDFILETYPE_FEEDMEDIA = 2;
public static final int PLAYABLE_TYPE_FEEDMEDIA = 1; public static final int PLAYABLE_TYPE_FEEDMEDIA = 1;
public static final String FILENAME_PREFIX_EMBEDDED_COVER = "metadata-retriever:"; public static final String FILENAME_PREFIX_EMBEDDED_COVER = "metadata-retriever:";
@ -162,15 +159,6 @@ public class FeedMedia extends FeedFile implements Playable {
&& PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == id; && 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 @Override
public int getTypeAsInt() { public int getTypeAsInt() {
return FEEDFILETYPE_FEEDMEDIA; return FEEDFILETYPE_FEEDMEDIA;

View File

@ -4,6 +4,8 @@ import androidx.annotation.NonNull;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences; 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 org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -73,4 +75,13 @@ public class FeedItemUtil {
int smartMarkAsPlayedSecs = UserPreferences.getSmartMarkAsPlayedSecs(); int smartMarkAsPlayedSecs = UserPreferences.getSmartMarkAsPlayedSecs();
return media.getDuration() > 0 && media.getPosition() >= media.getDuration() - smartMarkAsPlayedSecs * 1000; 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));
}
} }