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.
This commit is contained in:
ByteHamster 2024-04-15 19:23:26 +02:00 committed by GitHub
parent 8037bd2239
commit 91bcf4b400
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 6 deletions

View File

@ -162,6 +162,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
private CastStateListener castStateListener; private CastStateListener castStateListener;
private String autoSkippedFeedMediaId = null; private String autoSkippedFeedMediaId = null;
private String positionJustResetAfterPlayback = null;
private int clickCount = 0; private int clickCount = 0;
private final Handler clickHandler = new Handler(Looper.getMainLooper()); private final Handler clickHandler = new Handler(Looper.getMainLooper());
@ -874,6 +875,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
(ctx) -> disableSleepTimer(), getString(R.string.undo))); (ctx) -> disableSleepTimer(), getString(R.string.undo)));
} }
loadQueueForMediaSession(); loadQueueForMediaSession();
positionJustResetAfterPlayback = null;
break; break;
case ERROR: case ERROR:
PlaybackPreferences.writeNoMediaPlaying(); PlaybackPreferences.writeNoMediaPlaying();
@ -935,15 +937,17 @@ public class PlaybackService extends MediaBrowserServiceCompat {
public void onPlaybackPause(Playable playable, int position) { public void onPlaybackPause(Playable playable, int position) {
taskManager.cancelPositionSaver(); taskManager.cancelPositionSaver();
cancelPositionObserver(); cancelPositionObserver();
saveCurrentPosition(position == Playable.INVALID_TIME || playable == null, playable, position);
taskManager.cancelWidgetUpdater(); taskManager.cancelWidgetUpdater();
if (playable != null) { if (playable instanceof FeedMedia) {
if (playable instanceof FeedMedia) { FeedMedia media = (FeedMedia) playable;
SynchronizationQueueSink.enqueueEpisodePlayedIfSynchronizationIsActive(getApplicationContext(), if (!media.getItem().getIdentifyingValue().equals(positionJustResetAfterPlayback)) {
(FeedMedia) playable, false); // 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 @Override
@ -1152,6 +1156,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
|| autoSkipped || autoSkipped
|| (skipped && !UserPreferences.shouldSkipKeepEpisode())) { || (skipped && !UserPreferences.shouldSkipKeepEpisode())) {
// only mark the item as played if we're not keeping it anyways // only mark the item as played if we're not keeping it anyways
positionJustResetAfterPlayback = item.getIdentifyingValue();
DBWriter.markItemPlayed(item, FeedItem.PLAYED, ended || (skipped && almostEnded)); 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 // don't know if it actually matters to not autodownload when smart mark as played is triggered
DBWriter.removeQueueItem(PlaybackService.this, ended, item); DBWriter.removeQueueItem(PlaybackService.this, ended, item);