Search in sources :

Example 1 with FeedItemPubdateComparator

use of de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator in project AntennaPod by AntennaPod.

the class DBReader method getDownloadedItems.

/**
     * Loads a list of FeedItems whose episode has been downloaded.
     *
     * @return A list of FeedItems whose episdoe has been downloaded.
     */
public static List<FeedItem> getDownloadedItems() {
    Log.d(TAG, "getDownloadedItems() called with: " + "");
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    Cursor itemlistCursor = adapter.getDownloadedItemsCursor();
    List<FeedItem> items = extractItemlistFromCursor(adapter, itemlistCursor);
    itemlistCursor.close();
    loadAdditionalFeedItemListData(items);
    adapter.close();
    Collections.sort(items, new FeedItemPubdateComparator());
    return items;
}
Also used : FeedItemPubdateComparator(de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Cursor(android.database.Cursor)

Example 2 with FeedItemPubdateComparator

use of de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator in project AntennaPod by AntennaPod.

the class DBTestUtils method saveFeedlist.

/**
     * Use this method when tests involve chapters.
     */
public static List<Feed> saveFeedlist(int numFeeds, int numItems, boolean withMedia, boolean withChapters, int numChapters) {
    if (numFeeds <= 0) {
        throw new IllegalArgumentException("numFeeds<=0");
    }
    if (numItems < 0) {
        throw new IllegalArgumentException("numItems<0");
    }
    List<Feed> feeds = new ArrayList<>();
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    for (int i = 0; i < numFeeds; i++) {
        Feed f = new Feed(0, null, "feed " + i, null, "link" + i, "descr", null, null, null, null, "id" + i, null, null, "url" + i, false, new FlattrStatus(), false, null, null, false);
        f.setItems(new ArrayList<>());
        for (int j = 0; j < numItems; j++) {
            FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(), FeedItem.PLAYED, f, withChapters);
            if (withMedia) {
                FeedMedia media = new FeedMedia(item, "url" + j, 1, "audio/mp3");
                item.setMedia(media);
            }
            if (withChapters) {
                List<Chapter> chapters = new ArrayList<>();
                item.setChapters(chapters);
                for (int k = 0; k < numChapters; k++) {
                    chapters.add(new SimpleChapter(k, "item " + j + " chapter " + k, item, "http://example.com"));
                }
            }
            f.getItems().add(item);
        }
        Collections.sort(f.getItems(), new FeedItemPubdateComparator());
        adapter.setCompleteFeed(f);
        Assert.assertTrue(f.getId() != 0);
        for (FeedItem item : f.getItems()) {
            Assert.assertTrue(item.getId() != 0);
        }
        feeds.add(f);
    }
    adapter.close();
    return feeds;
}
Also used : ArrayList(java.util.ArrayList) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) Chapter(de.danoeh.antennapod.core.feed.Chapter) Date(java.util.Date) FlattrStatus(de.danoeh.antennapod.core.util.flattr.FlattrStatus) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedItemPubdateComparator(de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 3 with FeedItemPubdateComparator

use of de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator in project AntennaPod by AntennaPod.

the class DBReader method getFeedItemList.

/**
     * Loads the list of FeedItems for a certain Feed-object. This method should NOT be used if the FeedItems are not
     * used. In order to get information ABOUT the list of FeedItems, consider using {@link #getFeedStatisticsList()} instead.
     *
     * @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) {
    Log.d(TAG, "getFeedItemList() called with: " + "feed = [" + feed + "]");
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    Cursor itemlistCursor = adapter.getAllItemsOfFeedCursor(feed);
    List<FeedItem> items = extractItemlistFromCursor(adapter, itemlistCursor);
    itemlistCursor.close();
    adapter.close();
    Collections.sort(items, new FeedItemPubdateComparator());
    for (FeedItem item : items) {
        item.setFeed(feed);
    }
    return items;
}
Also used : FeedItemPubdateComparator(de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Cursor(android.database.Cursor)

Example 4 with FeedItemPubdateComparator

use of de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator in project AntennaPod by AntennaPod.

the class DBTasks method updateFeed.

/**
     * Adds new Feeds to the database or updates the old versions if they already exists. If another Feed with the same
     * identifying value already exists, this method will add new FeedItems from the new Feed to the existing Feed.
     * These FeedItems will be marked as unread with the exception of the most recent FeedItem.
     * <p/>
     * This method can update multiple feeds at once. Submitting a feed twice in the same method call can result in undefined behavior.
     * <p/>
     * This method should NOT be executed on the GUI thread.
     *
     * @param context  Used for accessing the DB.
     * @param newFeeds The new Feed objects.
     * @return The updated Feeds from the database if it already existed, or the new Feed from the parameters otherwise.
     */
public static synchronized Feed[] updateFeed(final Context context, final Feed... newFeeds) {
    List<Feed> newFeedsList = new ArrayList<>();
    List<Feed> updatedFeedsList = new ArrayList<>();
    Feed[] resultFeeds = new Feed[newFeeds.length];
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    for (int feedIdx = 0; feedIdx < newFeeds.length; feedIdx++) {
        final Feed newFeed = newFeeds[feedIdx];
        // Look up feed in the feedslist
        final Feed savedFeed = searchFeedByIdentifyingValueOrID(adapter, newFeed);
        if (savedFeed == null) {
            Log.d(TAG, "Found no existing Feed with title " + newFeed.getTitle() + ". Adding as new one.");
            // Add a new Feed
            // all new feeds will have the most recent item marked as unplayed
            FeedItem mostRecent = newFeed.getMostRecentItem();
            if (mostRecent != null) {
                mostRecent.setNew();
            }
            newFeedsList.add(newFeed);
            resultFeeds[feedIdx] = newFeed;
        } else {
            Log.d(TAG, "Feed with title " + newFeed.getTitle() + " already exists. Syncing new with existing one.");
            Collections.sort(newFeed.getItems(), new FeedItemPubdateComparator());
            if (newFeed.getPageNr() == savedFeed.getPageNr()) {
                if (savedFeed.compareWithOther(newFeed)) {
                    Log.d(TAG, "Feed has updated attribute values. Updating old feed's attributes");
                    savedFeed.updateFromOther(newFeed);
                }
            } else {
                Log.d(TAG, "New feed has a higher page number.");
                savedFeed.setNextPageLink(newFeed.getNextPageLink());
            }
            if (savedFeed.getPreferences().compareWithOther(newFeed.getPreferences())) {
                Log.d(TAG, "Feed has updated preferences. Updating old feed's preferences");
                savedFeed.getPreferences().updateFromOther(newFeed.getPreferences());
            }
            // get the most recent date now, before we start changing the list
            FeedItem priorMostRecent = savedFeed.getMostRecentItem();
            Date priorMostRecentDate = null;
            if (priorMostRecent != null) {
                priorMostRecentDate = priorMostRecent.getPubDate();
            }
            // Look for new or updated Items
            for (int idx = 0; idx < newFeed.getItems().size(); idx++) {
                final FeedItem item = newFeed.getItems().get(idx);
                FeedItem oldItem = searchFeedItemByIdentifyingValue(savedFeed, item.getIdentifyingValue());
                if (oldItem == null) {
                    // item is new
                    item.setFeed(savedFeed);
                    item.setAutoDownload(savedFeed.getPreferences().getAutoDownload());
                    savedFeed.getItems().add(idx, item);
                    // and this is the first, hence 'new')
                    if (priorMostRecentDate == null || priorMostRecentDate.before(item.getPubDate()) || priorMostRecentDate.equals(item.getPubDate())) {
                        Log.d(TAG, "Marking item published on " + item.getPubDate() + " new, prior most recent date = " + priorMostRecentDate);
                        item.setNew();
                    }
                } else {
                    oldItem.updateFromOther(item);
                }
            }
            // update attributes
            savedFeed.setLastUpdate(newFeed.getLastUpdate());
            savedFeed.setType(newFeed.getType());
            savedFeed.setLastUpdateFailed(false);
            updatedFeedsList.add(savedFeed);
            resultFeeds[feedIdx] = savedFeed;
        }
    }
    adapter.close();
    try {
        DBWriter.addNewFeed(context, newFeedsList.toArray(new Feed[newFeedsList.size()])).get();
        DBWriter.setCompleteFeed(updatedFeedsList.toArray(new Feed[updatedFeedsList.size()])).get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    EventDistributor.getInstance().sendFeedUpdateBroadcast();
    return resultFeeds;
}
Also used : FeedItemPubdateComparator(de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Aggregations

FeedItem (de.danoeh.antennapod.core.feed.FeedItem)4 FeedItemPubdateComparator (de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator)4 Cursor (android.database.Cursor)2 Feed (de.danoeh.antennapod.core.feed.Feed)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Chapter (de.danoeh.antennapod.core.feed.Chapter)1 FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)1 SimpleChapter (de.danoeh.antennapod.core.feed.SimpleChapter)1 PodDBAdapter (de.danoeh.antennapod.core.storage.PodDBAdapter)1 FlattrStatus (de.danoeh.antennapod.core.util.flattr.FlattrStatus)1 ExecutionException (java.util.concurrent.ExecutionException)1