diff options
author | ByteHamster <info@bytehamster.com> | 2020-03-28 12:51:20 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2020-03-30 13:58:44 +0200 |
commit | a4409c7c5c57bf935851a9923a497d9440fff9f2 (patch) | |
tree | 8868125bce131330de73f161d06bbfa7968d1ecc /core/src | |
parent | ef121892f7705e97bb0060b243a72258fe7cd887 (diff) | |
download | AntennaPod-a4409c7c5c57bf935851a9923a497d9440fff9f2.zip |
Upload all played episodes on full sync
Diffstat (limited to 'core/src')
4 files changed, 70 insertions, 27 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index d31f5d688..534f7ba4c 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -21,6 +21,7 @@ import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.DownloadStatus; +import de.danoeh.antennapod.core.sync.model.EpisodeAction; import de.danoeh.antennapod.core.util.LongIntMap; import de.danoeh.antennapod.core.util.LongList; import de.danoeh.antennapod.core.util.comparator.DownloadStatusComparator; @@ -348,6 +349,32 @@ public final class DBReader { } /** + * Loads a list of FeedItems whose episode has been played. + * + * @return A list of FeedItems whose episdoe has been played. + */ + @NonNull + public static List<FeedItem> getPlayedItems() { + Log.d(TAG, "getPlayedItems() called"); + + PodDBAdapter adapter = PodDBAdapter.getInstance(); + adapter.open(); + Cursor cursor = null; + try { + cursor = adapter.getPlayedItemsCursor(); + List<FeedItem> items = extractItemlistFromCursor(adapter, cursor); + loadAdditionalFeedItemListData(items); + Collections.sort(items, new FeedItemPubdateComparator()); + return items; + } finally { + if (cursor != null) { + cursor.close(); + } + adapter.close(); + } + } + + /** * Loads a list of FeedItems that are considered new. * Excludes items from feeds that do not have keep updated enabled. * diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java index af6a8ef81..81a1833ec 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java @@ -1023,6 +1023,15 @@ public class PodDBAdapter { return db.rawQuery(query, null); } + public Cursor getPlayedItemsCursor() { + final String query = "SELECT " + SEL_FI_SMALL_STR + + " FROM " + TABLE_NAME_FEED_ITEMS + + " INNER JOIN " + TABLE_NAME_FEED_MEDIA + + " ON " + TABLE_NAME_FEED_ITEMS + "." + KEY_ID + "=" + TABLE_NAME_FEED_MEDIA + "." + KEY_FEEDITEM + + " WHERE " + TABLE_NAME_FEED_ITEMS + "." + KEY_READ + "=" + FeedItem.PLAYED; + return db.rawQuery(query, null); + } + /** * Returns a cursor which contains feed media objects with a playback * completion date in ascending order. diff --git a/core/src/main/java/de/danoeh/antennapod/core/sync/SyncService.java b/core/src/main/java/de/danoeh/antennapod/core/sync/SyncService.java index 0f19d2633..6ca3d3422 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/sync/SyncService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/sync/SyncService.java @@ -58,7 +58,6 @@ public class SyncService extends Worker { private static final String TAG = "SyncService"; private static final String WORK_ID_SYNC = "SyncServiceWorkId"; private static final Object lock = new Object(); - private static boolean syncPending = false; private ISyncService syncServiceImpl; @@ -70,15 +69,6 @@ public class SyncService extends Worker { @NonNull public Result doWork() { syncServiceImpl = new GpodnetService(AntennapodHttpClient.getHttpClient(), GpodnetService.DEFAULT_BASE_HOST); - - try { - // Leave some time, so other actions can be queued - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - syncPending = false; try { syncServiceImpl.login(); syncSubscriptions(); @@ -155,24 +145,18 @@ public class SyncService extends Worker { } public static void sync(Context context) { - if (!syncPending) { - syncPending = true; - - Constraints.Builder constraints = new Constraints.Builder(); - if (UserPreferences.isAllowMobileFeedRefresh()) { - constraints.setRequiredNetworkType(NetworkType.CONNECTED); - } else { - constraints.setRequiredNetworkType(NetworkType.UNMETERED); - } - - OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SyncService.class) - .setConstraints(constraints.build()) - .setInitialDelay(0L, TimeUnit.MILLISECONDS) - .build(); - WorkManager.getInstance().enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest); + Constraints.Builder constraints = new Constraints.Builder(); + if (UserPreferences.isAllowMobileFeedRefresh()) { + constraints.setRequiredNetworkType(NetworkType.CONNECTED); } else { - Log.d(TAG, "Ignored sync: Job already enqueued"); + constraints.setRequiredNetworkType(NetworkType.UNMETERED); } + + OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(SyncService.class) + .setConstraints(constraints.build()) + .setInitialDelay(5L, TimeUnit.SECONDS) // Give it some time, so other actions can be queued + .build(); + WorkManager.getInstance().enqueueUniqueWork(WORK_ID_SYNC, ExistingWorkPolicy.REPLACE, workRequest); } public static void fullSync(Context context) { @@ -296,6 +280,24 @@ public class SyncService extends Worker { // upload local actions List<EpisodeAction> queuedEpisodeActions = getQueuedEpisodeActions(); + if (lastSync == 0) { + List<FeedItem> readItems = DBReader.getPlayedItems(); + Log.d(TAG, "First sync. Upload state for all " + readItems.size() + " played episodes"); + for (FeedItem item : readItems) { + FeedMedia media = item.getMedia(); + if (media == null) { + continue; + } + EpisodeAction played = new EpisodeAction.Builder(item, EpisodeAction.PLAY) + .currentTimestamp() + .started(media.getDuration() / 1000) + .position(media.getDuration() / 1000) + .total(media.getDuration() / 1000) + .build(); + Log.d(TAG, "Played state: " + played.toString()); + queuedEpisodeActions.add(played); + } + } if (queuedEpisodeActions.size() > 0) { synchronized (lock) { Log.d(TAG, "Uploading actions: " + StringUtils.join(queuedEpisodeActions, ", ")); @@ -312,9 +314,11 @@ public class SyncService extends Worker { private synchronized void processEpisodeActions(List<EpisodeAction> remoteActions) { + Log.d(TAG, "Processing " + remoteActions.size() + " actions"); if (remoteActions.size() == 0) { return; } + Map<Pair<String, String>, EpisodeAction> localMostRecentPlayAction = new ArrayMap<>(); for (EpisodeAction action : getQueuedEpisodeActions()) { Pair<String, String> key = new Pair<>(action.getPodcast(), action.getEpisode()); @@ -329,6 +333,7 @@ public class SyncService extends Worker { // make sure more recent local actions are not overwritten by older remote actions Map<Pair<String, String>, EpisodeAction> mostRecentPlayAction = new ArrayMap<>(); for (EpisodeAction action : remoteActions) { + Log.d(TAG, "Processing action: " + action.toString()); switch (action.getAction()) { case NEW: FeedItem newItem = DBReader.getFeedItem(action.getPodcast(), action.getEpisode()); @@ -364,11 +369,13 @@ public class SyncService extends Worker { for (EpisodeAction action : mostRecentPlayAction.values()) { FeedItem playItem = DBReader.getFeedItem(action.getPodcast(), action.getEpisode()); + Log.d(TAG, "Most recent play action: " + action.toString()); if (playItem != null) { FeedMedia media = playItem.getMedia(); media.setPosition(action.getPosition() * 1000); DBWriter.setFeedMedia(media); if (playItem.getMedia().hasAlmostEnded()) { + Log.d(TAG, "Marking as played"); DBWriter.markItemPlayed(playItem, FeedItem.PLAYED, true); DBWriter.addItemToPlaybackHistory(playItem.getMedia()); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/sync/model/EpisodeAction.java b/core/src/main/java/de/danoeh/antennapod/core/sync/model/EpisodeAction.java index c05e06752..a8eeedd03 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/sync/model/EpisodeAction.java +++ b/core/src/main/java/de/danoeh/antennapod/core/sync/model/EpisodeAction.java @@ -183,7 +183,7 @@ public class EpisodeAction { @Override public String toString() { - return "GpodnetEpisodeAction{" + + return "EpisodeAction{" + "podcast='" + podcast + '\'' + ", episode='" + episode + '\'' + ", action=" + action + |