From a29041cd4d797bf4737d6a0814ce4bb7c6420d01 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Tue, 1 Nov 2022 11:31:09 +0100 Subject: [PATCH] No need to define INVALID_TIME multiple times --- .../activity/VideoplayerActivity.java | 4 ++-- .../fragment/AudioPlayerFragment.java | 3 +-- .../fragment/ExternalPlayerFragment.java | 4 ++-- .../viewholder/EpisodeItemViewHolder.java | 4 ++-- .../service/playback/ExoPlayerWrapper.java | 4 ++-- .../core/service/playback/LocalPSMP.java | 12 +++++------ .../service/playback/PlaybackService.java | 21 +++++++------------ .../util/playback/PlaybackController.java | 9 ++++---- .../base/PlaybackServiceMediaPlayer.java | 9 ++------ .../antennapod/playback/cast/CastPsmp.java | 12 +++++------ 10 files changed, 34 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java index 65a5bf507..6b2da84e9 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java @@ -669,8 +669,8 @@ public class VideoplayerActivity extends CastEnabledActivity implements SeekBar. int remainingTime = converter.convert( controller.getDuration() - controller.getPosition()); Log.d(TAG, "currentPosition " + Converter.getDurationStringLong(currentPosition)); - if (currentPosition == PlaybackService.INVALID_TIME - || duration == PlaybackService.INVALID_TIME) { + if (currentPosition == Playable.INVALID_TIME + || duration == Playable.INVALID_TIME) { Log.w(TAG, "Could not react to position observer update because of invalid time"); return; } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java index 3a92a8fda..08c684a15 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/AudioPlayerFragment.java @@ -28,7 +28,6 @@ import com.google.android.material.elevation.SurfaceColors; import com.google.android.material.snackbar.Snackbar; import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; -import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.event.playback.BufferUpdateEvent; import de.danoeh.antennapod.event.playback.PlaybackServiceEvent; @@ -375,7 +374,7 @@ public class AudioPlayerFragment extends Fragment implements int remainingTime = converter.convert(Math.max(event.getDuration() - event.getPosition(), 0)); currentChapterIndex = ChapterUtils.getCurrentChapterIndex(controller.getMedia(), currentPosition); Log.d(TAG, "currentPosition " + Converter.getDurationStringLong(currentPosition)); - if (currentPosition == PlaybackService.INVALID_TIME || duration == PlaybackService.INVALID_TIME) { + if (currentPosition == Playable.INVALID_TIME || duration == Playable.INVALID_TIME) { Log.w(TAG, "Could not react to position observer update because of invalid time"); return; } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java index 1e24d62f7..2f39070ac 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java @@ -139,8 +139,8 @@ public class ExternalPlayerFragment extends Fragment { public void onPositionObserverUpdate(PlaybackPositionEvent event) { if (controller == null) { return; - } else if (controller.getPosition() == PlaybackService.INVALID_TIME - || controller.getDuration() == PlaybackService.INVALID_TIME) { + } else if (controller.getPosition() == Playable.INVALID_TIME + || controller.getDuration() == Playable.INVALID_TIME) { return; } progressBar.setProgress((int) 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 bb57e4bc0..22b46492f 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 @@ -30,10 +30,10 @@ import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.model.playback.MediaType; import de.danoeh.antennapod.core.feed.util.ImageResourceUtils; import de.danoeh.antennapod.core.preferences.UserPreferences; -import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.NetworkUtils; +import de.danoeh.antennapod.model.playback.Playable; import de.danoeh.antennapod.ui.common.CircularProgressBar; import de.danoeh.antennapod.ui.common.ThemeUtils; @@ -230,7 +230,7 @@ public class EpisodeItemViewHolder extends RecyclerView.ViewHolder { int timeDuration = event.getDuration(); int remainingTime = Math.max(timeDuration - currentPosition, 0); Log.d(TAG, "currentPosition " + Converter.getDurationStringLong(currentPosition)); - if (currentPosition == PlaybackService.INVALID_TIME || timeDuration == PlaybackService.INVALID_TIME) { + if (currentPosition == Playable.INVALID_TIME || timeDuration == Playable.INVALID_TIME) { Log.w(TAG, "Could not react to position observer update because of invalid time"); return; } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java index 37f49a416..cbd552684 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java @@ -41,7 +41,7 @@ import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; import de.danoeh.antennapod.core.service.download.HttpCredentialEncoder; import de.danoeh.antennapod.core.util.NetworkUtils; import de.danoeh.antennapod.core.util.playback.IPlayer; -import de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer; +import de.danoeh.antennapod.model.playback.Playable; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; @@ -154,7 +154,7 @@ public class ExoPlayerWrapper implements IPlayer { @Override public int getDuration() { if (exoPlayer.getDuration() == C.TIME_UNSET) { - return PlaybackServiceMediaPlayer.INVALID_TIME; + return Playable.INVALID_TIME; } return (int) exoPlayer.getDuration(); } 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 f1b9510d9..9525401d6 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 @@ -543,7 +543,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { executor.submit(() -> { playerLock.lock(); int currentPosition = getPosition(); - if (currentPosition != INVALID_TIME) { + if (currentPosition != Playable.INVALID_TIME) { seekToSync(currentPosition + d); } else { Log.e(TAG, "getPosition() returned INVALID_TIME in seekDelta"); @@ -559,10 +559,10 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { @Override public int getDuration() { if (!playerLock.tryLock()) { - return INVALID_TIME; + return Playable.INVALID_TIME; } - int retVal = INVALID_TIME; + int retVal = Playable.INVALID_TIME; if (playerStatus == PlayerStatus.PLAYING || playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PREPARED) { @@ -583,13 +583,13 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { public int getPosition() { try { if (!playerLock.tryLock(50, TimeUnit.MILLISECONDS)) { - return INVALID_TIME; + return Playable.INVALID_TIME; } } catch (InterruptedException e) { - return INVALID_TIME; + return Playable.INVALID_TIME; } - int retVal = INVALID_TIME; + int retVal = Playable.INVALID_TIME; if (playerStatus.isAtLeast(PlayerStatus.PREPARED)) { retVal = mediaPlayer.getCurrentPosition(); } 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 f39466fc4..c1dc5ee2d 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 @@ -156,12 +156,6 @@ public class PlaybackService extends MediaBrowserServiceCompat { */ public static final int NOTIFICATION_TYPE_PLAYBACK_END = 7; - /** - * Returned by getPositionSafe() or getDurationSafe() if the playbackService - * is in an invalid state. - */ - public static final int INVALID_TIME = -1; - /** * Is true if service is running. */ @@ -761,7 +755,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { private final PlaybackServiceTaskManager.PSTMCallback taskManagerCallback = new PlaybackServiceTaskManager.PSTMCallback() { @Override public void positionSaverTick() { - saveCurrentPosition(true, null, PlaybackServiceMediaPlayer.INVALID_TIME); + saveCurrentPosition(true, null, Playable.INVALID_TIME); } @Override @@ -813,7 +807,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { break; case PLAYING: PlaybackPreferences.writePlayerStatus(mediaPlayer.getPlayerStatus()); - saveCurrentPosition(true, null, INVALID_TIME); + saveCurrentPosition(true, null, Playable.INVALID_TIME); updateNotificationAndMediaSession(newInfo.playable); setupPositionObserver(); stateManager.validStartCommandWasReceived(); @@ -868,7 +862,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { @Override public void onPlaybackStart(@NonNull Playable playable, int position) { taskManager.startWidgetUpdater(); - if (position != PlaybackServiceMediaPlayer.INVALID_TIME) { + if (position != Playable.INVALID_TIME) { playable.setPosition(position); } else { skipIntro(playable); @@ -881,8 +875,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { public void onPlaybackPause(Playable playable, int position) { taskManager.cancelPositionSaver(); cancelPositionObserver(); - saveCurrentPosition(position == PlaybackServiceMediaPlayer.INVALID_TIME || playable == null, - playable, position); + saveCurrentPosition(position == Playable.INVALID_TIME || playable == null, playable, position); taskManager.cancelWidgetUpdater(); if (playable != null) { if (playable instanceof FeedMedia) { @@ -1377,7 +1370,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { } else { duration = playable.getDuration(); } - if (position != INVALID_TIME && duration != INVALID_TIME && playable != null) { + if (position != Playable.INVALID_TIME && duration != Playable.INVALID_TIME && playable != null) { Log.d(TAG, "Saving current position to " + position); PlayableUtils.saveCurrentPosition(playable, position, System.currentTimeMillis()); } @@ -1653,7 +1646,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { */ public int getDuration() { if (mediaPlayer == null) { - return INVALID_TIME; + return Playable.INVALID_TIME; } return mediaPlayer.getDuration(); } @@ -1664,7 +1657,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { */ public int getCurrentPosition() { if (mediaPlayer == null) { - return INVALID_TIME; + return Playable.INVALID_TIME; } return mediaPlayer.getPosition(); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java index ea1c737a7..da27dad07 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java @@ -39,7 +39,6 @@ import java.util.List; public abstract class PlaybackController { private static final String TAG = "PlaybackController"; - private static final int INVALID_TIME = -1; private final Activity activity; private PlaybackService playbackService; @@ -363,7 +362,7 @@ public abstract class PlaybackController { } else if (getMedia() != null) { return getMedia().getPosition(); } else { - return PlaybackService.INVALID_TIME; + return Playable.INVALID_TIME; } } @@ -373,7 +372,7 @@ public abstract class PlaybackController { } else if (getMedia() != null) { return getMedia().getDuration(); } else { - return PlaybackService.INVALID_TIME; + return Playable.INVALID_TIME; } } @@ -398,13 +397,13 @@ public abstract class PlaybackController { if (playbackService != null) { return playbackService.getSleepTimerTimeLeft(); } else { - return INVALID_TIME; + return Playable.INVALID_TIME; } } public void extendSleepTimer(long extendTime) { long timeLeft = getSleepTimerTimeLeft(); - if (playbackService != null && timeLeft != INVALID_TIME) { + if (playbackService != null && timeLeft != Playable.INVALID_TIME) { setSleepTimer(timeLeft + extendTime); } } diff --git a/playback/base/src/main/java/de/danoeh/antennapod/playback/base/PlaybackServiceMediaPlayer.java b/playback/base/src/main/java/de/danoeh/antennapod/playback/base/PlaybackServiceMediaPlayer.java index a2ccb3f88..a5b6df41e 100644 --- a/playback/base/src/main/java/de/danoeh/antennapod/playback/base/PlaybackServiceMediaPlayer.java +++ b/playback/base/src/main/java/de/danoeh/antennapod/playback/base/PlaybackServiceMediaPlayer.java @@ -28,11 +28,6 @@ import de.danoeh.antennapod.model.playback.Playable; public abstract class PlaybackServiceMediaPlayer { private static final String TAG = "PlaybackSvcMediaPlayer"; - /** - * Return value of some PSMP methods if the method call failed. - */ - public static final int INVALID_TIME = -1; - private volatile PlayerStatus oldPlayerStatus; protected volatile PlayerStatus playerStatus; @@ -307,7 +302,7 @@ public abstract class PlaybackServiceMediaPlayer { * @param newStatus The new PlayerStatus. This must not be null. * @param newMedia The new playable object of the PSMP object. This can be null. * @param position The position to be set to the current Playable object in case playback started or paused. - * Will be ignored if given the value of {@link #INVALID_TIME}. + * Will be ignored if given the value of {@link Playable#INVALID_TIME}. */ protected final synchronized void setPlayerStatus(@NonNull PlayerStatus newStatus, Playable newMedia, int position) { @@ -337,7 +332,7 @@ public abstract class PlaybackServiceMediaPlayer { * @see #setPlayerStatus(PlayerStatus, Playable, int) */ protected final void setPlayerStatus(@NonNull PlayerStatus newStatus, Playable newMedia) { - setPlayerStatus(newStatus, newMedia, INVALID_TIME); + setPlayerStatus(newStatus, newMedia, Playable.INVALID_TIME); } public interface PSMPCallback { diff --git a/playback/cast/src/play/java/de/danoeh/antennapod/playback/cast/CastPsmp.java b/playback/cast/src/play/java/de/danoeh/antennapod/playback/cast/CastPsmp.java index d3c4f3468..0858d4fc9 100644 --- a/playback/cast/src/play/java/de/danoeh/antennapod/playback/cast/CastPsmp.java +++ b/playback/cast/src/play/java/de/danoeh/antennapod/playback/cast/CastPsmp.java @@ -173,7 +173,7 @@ public class CastPsmp extends PlaybackServiceMediaPlayer { if (mediaChanged && stateChanged && oldState == MediaStatus.PLAYER_STATE_PLAYING && state != MediaStatus.PLAYER_STATE_IDLE) { - callback.onPlaybackPause(null, INVALID_TIME); + callback.onPlaybackPause(null, Playable.INVALID_TIME); // We don't want setPlayerStatus to handle the onPlaybackPause callback setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia); } @@ -197,7 +197,7 @@ public class CastPsmp extends PlaybackServiceMediaPlayer { case MediaStatus.PLAYER_STATE_BUFFERING: setPlayerStatus((mediaChanged || playerStatus == PlayerStatus.PREPARING) ? PlayerStatus.PREPARING : PlayerStatus.SEEKING, currentMedia, - currentMedia != null ? currentMedia.getPosition() : INVALID_TIME); + currentMedia != null ? currentMedia.getPosition() : Playable.INVALID_TIME); break; case MediaStatus.PLAYER_STATE_IDLE: int reason = status.getIdleReason(); @@ -218,7 +218,7 @@ public class CastPsmp extends PlaybackServiceMediaPlayer { // Means that a request to load a different media was sent // Not sure if currentMedia already reflects the to be loaded one if (mediaChanged && oldState == MediaStatus.PLAYER_STATE_PLAYING) { - callback.onPlaybackPause(null, INVALID_TIME); + callback.onPlaybackPause(null, Playable.INVALID_TIME); setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia); } setPlayerStatus(PlayerStatus.PREPARING, currentMedia); @@ -376,7 +376,7 @@ public class CastPsmp extends PlaybackServiceMediaPlayer { @Override public void seekDelta(int d) { int position = getPosition(); - if (position != INVALID_TIME) { + if (position != Playable.INVALID_TIME) { seekTo(position + d); } else { Log.e(TAG, "getPosition() returned INVALID_TIME in seekDelta"); @@ -386,7 +386,7 @@ public class CastPsmp extends PlaybackServiceMediaPlayer { @Override public int getDuration() { int retVal = (int) remoteMediaClient.getStreamDuration(); - if (retVal == INVALID_TIME && media != null && media.getDuration() > 0) { + if (retVal == Playable.INVALID_TIME && media != null && media.getDuration() > 0) { retVal = media.getDuration(); } return retVal; @@ -542,7 +542,7 @@ public class CastPsmp extends PlaybackServiceMediaPlayer { } } else if (isPlaying) { callback.onPlaybackPause(currentMedia, - currentMedia != null ? currentMedia.getPosition() : INVALID_TIME); + currentMedia != null ? currentMedia.getPosition() : Playable.INVALID_TIME); } FutureTask future = new FutureTask<>(() -> { }, null);