diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 805956094..309bfc7bd 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -72,7 +72,6 @@ import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; import de.danoeh.antennapod.core.storage.DBReader; -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.sync.queue.SynchronizationQueueSink; @@ -997,13 +996,7 @@ public class PlaybackService extends MediaBrowserServiceCompat { return null; } FeedItem nextItem; - try { - final List queue = taskManager.getQueue(); - nextItem = DBTasks.getQueueSuccessorOfItem(item.getId(), queue); - } catch (InterruptedException e) { - Log.e(TAG, "Error handling the queue in order to retrieve the next item", e); - return null; - } + nextItem = DBReader.getNextInQueue(item); if (nextItem == null || nextItem.getMedia() == null) { PlaybackPreferences.writeNoMediaPlaying(); @@ -1021,16 +1014,15 @@ public class PlaybackService extends MediaBrowserServiceCompat { && UserPreferences.isFollowQueue() && !nextItem.getFeed().isLocalFeed()) { displayStreamingNotAllowedNotification( new PlaybackServiceStarter(this, nextItem.getMedia()) - .prepareImmediately(true) - .startWhenPrepared(true) - .shouldStream(true) - .getIntent()); + .prepareImmediately(true) + .startWhenPrepared(true) + .shouldStream(true) + .getIntent()); PlaybackPreferences.writeNoMediaPlaying(); stateManager.stopService(); return null; } return nextItem.getMedia(); - } /** 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 f776fe111..da6987910 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 @@ -564,6 +564,34 @@ public final class DBReader { } } + /** + * Get next feed item in queue following a particular feeditem + * + * @param item The FeedItem + * @return The FeedItem next in queue or null if the FeedItem could not be found. + */ + @Nullable + public static FeedItem getNextInQueue(FeedItem item) { + Log.d(TAG, "getNextInQueue() called with: " + "itemId = [" + item.getId() + "]"); + PodDBAdapter adapter = PodDBAdapter.getInstance(); + adapter.open(); + try { + FeedItem nextItem = null; + try (Cursor cursor = adapter.getNextInQueue(item)) { + List list = extractItemlistFromCursor(adapter, cursor); + if (!list.isEmpty()) { + nextItem = list.get(0); + loadAdditionalFeedItemListData(list); + } + return nextItem; + } catch (Exception e) { + return null; + } + } finally { + adapter.close(); + } + } + /** * Loads a specific FeedItem from the database. * 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 b7e221a33..43d9c7f11 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 @@ -982,6 +982,18 @@ public class PodDBAdapter { return db.query(TABLE_NAME_QUEUE, new String[]{KEY_FEEDITEM}, null, null, null, null, KEY_ID + " ASC", null); } + public Cursor getNextInQueue(final FeedItem item) { + final String query = SELECT_FEED_ITEMS_AND_MEDIA + + "INNER JOIN " + TABLE_NAME_QUEUE + + " ON " + SELECT_KEY_ITEM_ID + " = " + TABLE_NAME_QUEUE + "." + KEY_FEEDITEM + + " WHERE Queue.ID > (SELECT Queue.ID FROM Queue WHERE Queue.FeedItem = " + + item.getId() + + ")" + + " ORDER BY Queue.ID" + + " LIMIT 1"; + return db.rawQuery(query, null); + } + public final Cursor getFavoritesCursor(int offset, int limit) { final String query = SELECT_FEED_ITEMS_AND_MEDIA + " INNER JOIN " + TABLE_NAME_FAVORITES