diff options
8 files changed, 74 insertions, 136 deletions
diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java index d37e0d7e1..17e023da5 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java @@ -153,7 +153,8 @@ public class DbReaderTest { Feed feed = saveFeedlist(numFeeds, numItems, false).get(0); List<FeedItem> items = feed.getItems(); feed.setItems(null); - List<FeedItem> savedItems = DBReader.getFeedItemList(feed); + List<FeedItem> savedItems = DBReader.getFeedItemList(feed, + FeedItemFilter.unfiltered(), SortOrder.DATE_NEW_OLD); assertNotNull(savedItems); assertEquals(items.size(), savedItems.size()); for (int i = 0; i < savedItems.size(); i++) { diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbWriterTest.java index 90876cc11..5aa81ea35 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DbWriterTest.java @@ -9,6 +9,8 @@ import androidx.core.util.Consumer; import androidx.preference.PreferenceManager; import androidx.test.platform.app.InstrumentationRegistry; +import de.danoeh.antennapod.model.feed.FeedItemFilter; +import de.danoeh.antennapod.model.feed.SortOrder; import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface; import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterfaceStub; import de.danoeh.antennapod.storage.database.DBReader; @@ -774,7 +776,8 @@ public class DbWriterTest { } DBWriter.removeAllNewFlags().get(); - List<FeedItem> loadedItems = DBReader.getFeedItemList(feed); + List<FeedItem> loadedItems = DBReader.getFeedItemList(feed, + FeedItemFilter.unfiltered(), SortOrder.DATE_NEW_OLD); for (FeedItem item : loadedItems) { assertFalse(item.isNew()); } diff --git a/net/download/service/src/test/java/de/danoeh/antennapod/net/download/service/feed/local/LocalFeedUpdaterTest.java b/net/download/service/src/test/java/de/danoeh/antennapod/net/download/service/feed/local/LocalFeedUpdaterTest.java index 484929136..4fb686ca5 100644 --- a/net/download/service/src/test/java/de/danoeh/antennapod/net/download/service/feed/local/LocalFeedUpdaterTest.java +++ b/net/download/service/src/test/java/de/danoeh/antennapod/net/download/service/feed/local/LocalFeedUpdaterTest.java @@ -8,6 +8,8 @@ import android.webkit.MimeTypeMap; import androidx.annotation.NonNull; import androidx.test.platform.app.InstrumentationRegistry; +import de.danoeh.antennapod.model.feed.FeedItemFilter; +import de.danoeh.antennapod.model.feed.SortOrder; import de.danoeh.antennapod.storage.preferences.PlaybackPreferences; import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface; import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterfaceStub; @@ -167,7 +169,7 @@ public class LocalFeedUpdaterTest { callUpdateFeed(LOCAL_FEED_DIR1); Feed feed = verifySingleFeedInDatabase(); - List<FeedItem> feedItems = DBReader.getFeedItemList(feed); + List<FeedItem> feedItems = DBReader.getFeedItemList(feed, FeedItemFilter.unfiltered(), SortOrder.DATE_NEW_OLD); assertEquals("track1.mp3", feedItems.get(0).getTitle()); } @@ -281,7 +283,7 @@ public class LocalFeedUpdaterTest { */ private static void verifySingleFeedInDatabaseAndItemCount(int expectedItemCount) { Feed feed = verifySingleFeedInDatabase(); - List<FeedItem> feedItems = DBReader.getFeedItemList(feed); + List<FeedItem> feedItems = DBReader.getFeedItemList(feed, FeedItemFilter.unfiltered(), SortOrder.DATE_NEW_OLD); assertEquals(expectedItemCount, feedItems.size()); } diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBReader.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBReader.java index 41bb75b8b..7335e056d 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBReader.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBReader.java @@ -52,9 +52,9 @@ public final class DBReader { /** * Returns a list of Feeds, sorted alphabetically by their title. * - * @return A list of Feeds, sorted alphabetically by their title. A Feed-object - * of the returned list does NOT have its list of FeedItems yet. The FeedItem-list - * can be loaded separately with {@link #getFeedItemList(Feed)}. + * @return A list of Feeds, sorted alphabetically by their title. + * A Feed-object of the returned list does NOT have its list of FeedItems yet. + * The FeedItem-list can be loaded separately with getFeedItemList(). */ @NonNull public static List<Feed> getFeedList() { @@ -62,21 +62,14 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - try { - return getFeedList(adapter); - } finally { - adapter.close(); - } - } - - @NonNull - private static List<Feed> getFeedList(PodDBAdapter adapter) { try (FeedCursor cursor = new FeedCursor(adapter.getAllFeedsCursor())) { List<Feed> feeds = new ArrayList<>(cursor.getCount()); while (cursor.moveToNext()) { feeds.add(cursor.getFeed()); } return feeds; + } finally { + adapter.close(); } } @@ -156,24 +149,14 @@ public final class DBReader { * * @param feed The Feed whose items should be loaded * @return A list with the FeedItems of the Feed. The Feed-attribute of the FeedItems will already be set correctly. - * The method does NOT change the items-attribute of the feed. */ - public static List<FeedItem> getFeedItemList(final Feed feed) { - return getFeedItemList(feed, FeedItemFilter.unfiltered()); - } - - public static List<FeedItem> getFeedItemList(final Feed feed, final FeedItemFilter filter) { - return getFeedItemList(feed, filter, SortOrder.DATE_NEW_OLD); - } - public static List<FeedItem> getFeedItemList(final Feed feed, final FeedItemFilter filter, SortOrder sortOrder) { Log.d(TAG, "getFeedItemList() called with: " + "feed = [" + feed + "]"); PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - try (FeedItemCursor cursor = new FeedItemCursor(adapter.getItemsOfFeedCursor(feed, filter))) { + try (FeedItemCursor cursor = new FeedItemCursor(adapter.getItemsOfFeedCursor(feed, filter, sortOrder))) { List<FeedItem> items = extractItemlistFromCursor(cursor); - FeedItemPermutors.getPermutor(sortOrder).reorder(items); feed.setItems(items); for (FeedItem item : items) { item.setFeed(feed); @@ -193,16 +176,6 @@ public final class DBReader { return result; } - @NonNull - public static List<FeedItem> getQueue(PodDBAdapter adapter) { - Log.d(TAG, "getQueue()"); - try (FeedItemCursor cursor = new FeedItemCursor(adapter.getQueueCursor())) { - List<FeedItem> items = extractItemlistFromCursor(cursor); - loadAdditionalFeedItemListData(items); - return items; - } - } - /** * Loads the IDs of the FeedItems in the queue. This method should be preferred over * {@link #getQueue()} if the FeedItems of the queue are not needed. @@ -213,20 +186,14 @@ public final class DBReader { Log.d(TAG, "getQueueIDList() called"); PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - try { - return getQueueIDList(adapter); - } finally { - adapter.close(); - } - } - - private static LongList getQueueIDList(PodDBAdapter adapter) { try (Cursor cursor = adapter.getQueueIDCursor()) { LongList queueIds = new LongList(cursor.getCount()); while (cursor.moveToNext()) { queueIds.add(cursor.getLong(0)); } return queueIds; + } finally { + adapter.close(); } } @@ -242,8 +209,10 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - try { - return getQueue(adapter); + try (FeedItemCursor cursor = new FeedItemCursor(adapter.getQueueCursor())) { + List<FeedItem> items = extractItemlistFromCursor(cursor); + loadAdditionalFeedItemListData(items); + return items; } finally { adapter.close(); } @@ -385,11 +354,8 @@ public final class DBReader { try (FeedCursor cursor = new FeedCursor(adapter.getFeedCursor(feedId))) { if (cursor.moveToNext()) { feed = cursor.getFeed(); - if (filtered) { - feed.setItems(getFeedItemList(feed, feed.getItemFilter())); - } else { - feed.setItems(getFeedItemList(feed)); - } + FeedItemFilter filter = filtered ? feed.getItemFilter() : FeedItemFilter.unfiltered(); + feed.setItems(getFeedItemList(feed, filter, feed.getSortOrder())); } else { Log.e(TAG, "getFeed could not find feed with id " + feedId); } @@ -399,21 +365,6 @@ public final class DBReader { } } - @Nullable - private static FeedItem getFeedItem(final long itemId, PodDBAdapter adapter) { - Log.d(TAG, "Loading feeditem with id " + itemId); - - FeedItem item = null; - try (FeedItemCursor cursor = new FeedItemCursor(adapter.getFeedItemCursor(Long.toString(itemId)))) { - List<FeedItem> list = extractItemlistFromCursor(cursor); - if (!list.isEmpty()) { - item = list.get(0); - loadAdditionalFeedItemListData(list); - } - return item; - } - } - /** * Loads a specific FeedItem from the database. This method should not be used for loading more * than one FeedItem because this method might query the database several times for each item. @@ -427,11 +378,17 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - try { - return getFeedItem(itemId, adapter); + try (FeedItemCursor cursor = new FeedItemCursor(adapter.getFeedItemCursor(Long.toString(itemId)))) { + List<FeedItem> list = extractItemlistFromCursor(cursor); + if (!list.isEmpty()) { + FeedItem item = list.get(0); + loadAdditionalFeedItemListData(list); + return item; + } } finally { adapter.close(); } + return null; } /** @@ -481,31 +438,15 @@ public final class DBReader { * @return The FeedItem or null if the FeedItem could not be found. * Does NOT load additional attributes like feed or queue state. */ - @Nullable - private static FeedItem getFeedItemByGuidOrEpisodeUrl(final String guid, final String episodeUrl, - PodDBAdapter adapter) { + public static FeedItem getFeedItemByGuidOrEpisodeUrl(final String guid, final String episodeUrl) { + PodDBAdapter adapter = PodDBAdapter.getInstance(); + adapter.open(); try (FeedItemCursor cursor = new FeedItemCursor(adapter.getFeedItemCursor(guid, episodeUrl))) { List<FeedItem> list = extractItemlistFromCursor(cursor); if (!list.isEmpty()) { return list.get(0); } return null; - } - } - - /** - * Loads a specific FeedItem from the database. - * - * @param guid feed item guid - * @param episodeUrl the feed item's url - * @return The FeedItem or null if the FeedItem could not be found. - * Does NOT load additional attributes like feed or queue state. - */ - public static FeedItem getFeedItemByGuidOrEpisodeUrl(final String guid, final String episodeUrl) { - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - try { - return getFeedItemByGuidOrEpisodeUrl(guid, episodeUrl, adapter); } finally { adapter.close(); } @@ -543,14 +484,6 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - try { - return loadChaptersOfFeedItem(adapter, item); - } finally { - adapter.close(); - } - } - - private static List<Chapter> loadChaptersOfFeedItem(PodDBAdapter adapter, FeedItem item) { try (ChapterCursor cursor = new ChapterCursor(adapter.getSimpleChaptersOfFeedItemCursor(item))) { int chaptersCount = cursor.getCount(); if (chaptersCount == 0) { @@ -562,6 +495,8 @@ public final class DBReader { chapters.add(cursor.getChapter()); } return chapters; + } finally { + adapter.close(); } } @@ -731,7 +666,7 @@ public final class DBReader { adapter.open(); final Map<Long, Integer> feedCounters = adapter.getFeedCounters(feedCounter); - List<Feed> feeds = getFeedList(adapter); + List<Feed> feeds = getFeedList(); if (subscriptionsFilter != null) { feeds = subscriptionsFilter.filter(feeds, feedCounters); @@ -827,17 +762,20 @@ public final class DBReader { } public static List<FeedItem> searchFeedItems(final long feedId, final String query) { - PodDBAdapter adapter = PodDBAdapter.getInstance().open(); - FeedItemCursor searchResult = new FeedItemCursor(adapter.searchItems(feedId, query)); - List<FeedItem> items = extractItemlistFromCursor(searchResult); - loadAdditionalFeedItemListData(items); - searchResult.close(); - adapter.close(); - return items; + PodDBAdapter adapter = PodDBAdapter.getInstance(); + adapter.open(); + try (FeedItemCursor searchResult = new FeedItemCursor(adapter.searchItems(feedId, query))) { + List<FeedItem> items = extractItemlistFromCursor(searchResult); + loadAdditionalFeedItemListData(items); + return items; + } finally { + adapter.close(); + } } public static List<Feed> searchFeeds(final String query) { PodDBAdapter adapter = PodDBAdapter.getInstance(); + adapter.open(); try (FeedCursor cursor = new FeedCursor(adapter.searchFeeds(query))) { List<Feed> items = new ArrayList<>(); while (cursor.moveToNext()) { diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBWriter.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBWriter.java index 0d7cf5cb1..3866b7ec1 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBWriter.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBWriter.java @@ -337,7 +337,7 @@ public class DBWriter { return runOnDbThread(() -> { final PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - final List<FeedItem> queue = DBReader.getQueue(adapter); + final List<FeedItem> queue = DBReader.getQueue(); FeedItem item; if (queue != null) { @@ -412,7 +412,7 @@ public class DBWriter { final PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - final List<FeedItem> queue = DBReader.getQueue(adapter); + final List<FeedItem> queue = DBReader.getQueue(); boolean queueModified = false; LongList markAsUnplayedIds = new LongList(); @@ -523,7 +523,7 @@ public class DBWriter { } final PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - final List<FeedItem> queue = DBReader.getQueue(adapter); + final List<FeedItem> queue = DBReader.getQueue(); if (queue != null) { boolean queueModified = false; @@ -662,7 +662,7 @@ public class DBWriter { final int to, final boolean broadcastUpdate) { final PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - final List<FeedItem> queue = DBReader.getQueue(adapter); + final List<FeedItem> queue = DBReader.getQueue(); if (queue != null) { if (from >= 0 && from < queue.size() && to >= 0 && to < queue.size()) { @@ -948,7 +948,7 @@ public class DBWriter { return runOnDbThread(() -> { final PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - final List<FeedItem> queue = DBReader.getQueue(adapter); + final List<FeedItem> queue = DBReader.getQueue(); if (queue != null) { permutor.reorder(queue); diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/FeedDatabaseWriter.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/FeedDatabaseWriter.java index eb33f5705..6429c3cb5 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/FeedDatabaseWriter.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/FeedDatabaseWriter.java @@ -8,7 +8,9 @@ import de.danoeh.antennapod.model.download.DownloadError; import de.danoeh.antennapod.model.download.DownloadResult; import de.danoeh.antennapod.model.feed.Feed; import de.danoeh.antennapod.model.feed.FeedItem; +import de.danoeh.antennapod.model.feed.FeedItemFilter; import de.danoeh.antennapod.model.feed.FeedPreferences; +import de.danoeh.antennapod.model.feed.SortOrder; import de.danoeh.antennapod.net.sync.model.EpisodeAction; import de.danoeh.antennapod.net.sync.serviceinterface.SynchronizationQueueSink; import de.danoeh.antennapod.storage.preferences.UserPreferences; @@ -34,7 +36,7 @@ public abstract class FeedDatabaseWriter { List<Feed> feeds = DBReader.getFeedList(); for (Feed f : feeds) { if (f.getIdentifyingValue().equals(feed.getIdentifyingValue())) { - f.setItems(DBReader.getFeedItemList(f)); + f.setItems(DBReader.getFeedItemList(f, FeedItemFilter.unfiltered(), SortOrder.DATE_NEW_OLD)); return f; } } diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java index 9ae07d61e..4e8d8133f 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java @@ -954,12 +954,14 @@ public class PodDBAdapter { * @param feed The feed you want to get the FeedItems from. * @return The cursor of the query */ - public final Cursor getItemsOfFeedCursor(final Feed feed, FeedItemFilter filter) { + public final Cursor getItemsOfFeedCursor(final Feed feed, FeedItemFilter filter, SortOrder sortOrder) { + String orderByQuery = FeedItemSortQuery.generateFrom(sortOrder); String filterQuery = FeedItemFilterQuery.generateFrom(filter); String whereClauseAnd = "".equals(filterQuery) ? "" : " AND " + filterQuery; final String query = SELECT_FEED_ITEMS_AND_MEDIA + " WHERE " + TABLE_NAME_FEED_ITEMS + "." + KEY_FEED + "=" + feed.getId() - + whereClauseAnd; + + whereClauseAnd + + " ORDER BY " + orderByQuery; return db.rawQuery(query, null); } diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/mapper/FeedItemSortQuery.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/mapper/FeedItemSortQuery.java index 644402079..7db9a0f56 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/mapper/FeedItemSortQuery.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/mapper/FeedItemSortQuery.java @@ -5,40 +5,30 @@ import de.danoeh.antennapod.storage.database.PodDBAdapter; public class FeedItemSortQuery { public static String generateFrom(SortOrder sortOrder) { - String sortQuery = ""; + if (sortOrder == null) { + sortOrder = SortOrder.DATE_NEW_OLD; + } switch (sortOrder) { case EPISODE_TITLE_A_Z: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_TITLE + " " + "ASC"; - break; + return PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_TITLE + " " + "ASC"; case EPISODE_TITLE_Z_A: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_TITLE + " " + "DESC"; - break; - case DATE_OLD_NEW: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_PUBDATE + " " + "ASC"; - break; - case DATE_NEW_OLD: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_PUBDATE + " " + "DESC"; - break; + return PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_TITLE + " " + "DESC"; case DURATION_SHORT_LONG: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_DURATION + " " + "ASC"; - break; + return PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_DURATION + " " + "ASC"; case DURATION_LONG_SHORT: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_DURATION + " " + "DESC"; - break; + return PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_DURATION + " " + "DESC"; case SIZE_SMALL_LARGE: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_SIZE + " " + "ASC"; - break; + return PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_SIZE + " " + "ASC"; case SIZE_LARGE_SMALL: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_SIZE + " " + "DESC"; - break; + return PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_SIZE + " " + "DESC"; case COMPLETION_DATE_NEW_OLD: - sortQuery = PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + return PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_PLAYBACK_COMPLETION_DATE + " " + "DESC"; - break; + case DATE_OLD_NEW: + return PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_PUBDATE + " " + "ASC"; + case DATE_NEW_OLD: default: - sortQuery = ""; - break; + return PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_PUBDATE + " " + "DESC"; } - return sortQuery; } }
\ No newline at end of file |