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 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);