From 91bcf4b4009603944378d3dc5e6ee0628e5b80b9 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 15 Apr 2024 19:23:26 +0200 Subject: Work around race condition where position reset might be undone (#7102) When the position saver ticks while the service is just about to be stopped, it might happen that we first reset the position and then set it to the end again. This works around this. --- .../antennapod/playback/service/PlaybackService.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/playback/service/src/main/java/de/danoeh/antennapod/playback/service/PlaybackService.java b/playback/service/src/main/java/de/danoeh/antennapod/playback/service/PlaybackService.java index 7f213a598..02a6c9500 100644 --- a/playback/service/src/main/java/de/danoeh/antennapod/playback/service/PlaybackService.java +++ b/playback/service/src/main/java/de/danoeh/antennapod/playback/service/PlaybackService.java @@ -162,6 +162,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { private CastStateListener castStateListener; private String autoSkippedFeedMediaId = null; + private String positionJustResetAfterPlayback = null; private int clickCount = 0; private final Handler clickHandler = new Handler(Looper.getMainLooper()); @@ -874,6 +875,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { (ctx) -> disableSleepTimer(), getString(R.string.undo))); } loadQueueForMediaSession(); + positionJustResetAfterPlayback = null; break; case ERROR: PlaybackPreferences.writeNoMediaPlaying(); @@ -935,15 +937,17 @@ public class PlaybackService extends MediaBrowserServiceCompat { public void onPlaybackPause(Playable playable, int position) { taskManager.cancelPositionSaver(); cancelPositionObserver(); - saveCurrentPosition(position == Playable.INVALID_TIME || playable == null, playable, position); taskManager.cancelWidgetUpdater(); - if (playable != null) { - if (playable instanceof FeedMedia) { - SynchronizationQueueSink.enqueueEpisodePlayedIfSynchronizationIsActive(getApplicationContext(), - (FeedMedia) playable, false); + if (playable instanceof FeedMedia) { + FeedMedia media = (FeedMedia) playable; + if (!media.getItem().getIdentifyingValue().equals(positionJustResetAfterPlayback)) { + // Don't store position after position is already reset + saveCurrentPosition(position == Playable.INVALID_TIME, playable, position); } - playable.onPlaybackPause(getApplicationContext()); + SynchronizationQueueSink.enqueueEpisodePlayedIfSynchronizationIsActive(getApplicationContext(), + media, false); } + playable.onPlaybackPause(getApplicationContext()); } @Override @@ -1152,6 +1156,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { || autoSkipped || (skipped && !UserPreferences.shouldSkipKeepEpisode())) { // only mark the item as played if we're not keeping it anyways + positionJustResetAfterPlayback = item.getIdentifyingValue(); DBWriter.markItemPlayed(item, FeedItem.PLAYED, ended || (skipped && almostEnded)); // don't know if it actually matters to not autodownload when smart mark as played is triggered DBWriter.removeQueueItem(PlaybackService.this, ended, item); -- cgit v1.2.3