Decouple FeedMedia and sync service (#5128)

This commit is contained in:
ByteHamster 2021-04-22 22:06:11 +02:00 committed by GitHub
parent 0081d232d0
commit 762140bccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 19 deletions

View File

@ -14,13 +14,10 @@ import android.support.v4.media.MediaDescriptionCompat;
import java.util.Date;
import java.util.List;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
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.util.playback.Playable;
import de.danoeh.antennapod.core.sync.SyncService;
import de.danoeh.antennapod.core.sync.model.EpisodeAction;
public class FeedMedia extends FeedFile implements Playable {
private static final String TAG = "FeedMedia";
@ -373,6 +370,10 @@ public class FeedMedia extends FeedFile implements Playable {
return download_url;
}
public int getStartPosition() {
return startPosition;
}
@Override
public Date getPubDate() {
if (item == null) {
@ -419,30 +420,14 @@ public class FeedMedia extends FeedFile implements Playable {
played_duration = playedDurationWhenStarted + position - startPosition;
playedDurationWhenStarted = played_duration;
}
postPlaybackTasks(context, false);
startPosition = position;
}
@Override
public void onPlaybackCompleted(Context context) {
postPlaybackTasks(context, true);
startPosition = -1;
}
private void postPlaybackTasks(Context context, boolean completed) {
if (item != null) {
if (startPosition >= 0 && (completed || startPosition < position) && GpodnetPreferences.loggedIn()) {
EpisodeAction action = new EpisodeAction.Builder(item, EpisodeAction.PLAY)
.currentTimestamp()
.started(startPosition / 1000)
.position((completed ? duration : position) / 1000)
.total(duration / 1000)
.build();
SyncService.enqueueEpisodeAction(context, action);
}
}
}
@Override
public int getPlayableType() {
return PLAYABLE_TYPE_FEEDMEDIA;

View File

@ -73,6 +73,7 @@ import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.FeedSearcher;
import de.danoeh.antennapod.core.feed.util.ImageResourceUtils;
import de.danoeh.antennapod.core.sync.SyncService;
import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.NetworkUtils;
@ -975,6 +976,9 @@ public class PlaybackService extends MediaBrowserServiceCompat {
playable, position);
taskManager.cancelWidgetUpdater();
if (playable != null) {
if (playable instanceof FeedMedia) {
SyncService.enqueueEpisodePlayed(getApplicationContext(), (FeedMedia) playable, true);
}
playable.onPlaybackPause(getApplicationContext());
}
}
@ -1108,8 +1112,10 @@ public class PlaybackService extends MediaBrowserServiceCompat {
}
if (ended || smartMarkAsPlayed) {
SyncService.enqueueEpisodePlayed(getApplicationContext(), media, true);
media.onPlaybackCompleted(getApplicationContext());
} else {
SyncService.enqueueEpisodePlayed(getApplicationContext(), media, false);
media.onPlaybackPause(getApplicationContext());
}

View File

@ -173,6 +173,25 @@ public class SyncService extends Worker {
});
}
public static void enqueueEpisodePlayed(Context context, FeedMedia media, boolean completed) {
if (!GpodnetPreferences.loggedIn()) {
return;
}
if (media.getItem() == null) {
return;
}
if (media.getStartPosition() < 0 || (!completed && media.getStartPosition() >= media.getPosition())) {
return;
}
EpisodeAction action = new EpisodeAction.Builder(media.getItem(), EpisodeAction.PLAY)
.currentTimestamp()
.started(media.getStartPosition() / 1000)
.position((completed ? media.getDuration() : media.getPosition()) / 1000)
.total(media.getDuration() / 1000)
.build();
SyncService.enqueueEpisodeAction(context, action);
}
public static void sync(Context context) {
OneTimeWorkRequest workRequest = getWorkRequest().build();
WorkManager.getInstance(context).enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest);