Search in sources :

Example 41 with FeedItem

use of de.danoeh.antennapod.core.feed.FeedItem 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 42 with FeedItem

use of de.danoeh.antennapod.core.feed.FeedItem in project AntennaPod by AntennaPod.

the class DBReader method getNavDrawerData.

/**
     * Returns data necessary for displaying the navigation drawer. This includes
     * the list of subscriptions, the number of items in the queue and the number of unread
     * items.
     *
     */
public static NavDrawerData getNavDrawerData() {
    Log.d(TAG, "getNavDrawerData() called with: " + "");
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    List<Feed> feeds = getFeedList(adapter);
    long[] feedIds = new long[feeds.size()];
    for (int i = 0; i < feeds.size(); i++) {
        feedIds[i] = feeds.get(i).getId();
    }
    final LongIntMap feedCounters = adapter.getFeedCounters(feedIds);
    Comparator<Feed> comparator;
    int feedOrder = UserPreferences.getFeedOrder();
    if (feedOrder == UserPreferences.FEED_ORDER_COUNTER) {
        comparator = (lhs, rhs) -> {
            long counterLhs = feedCounters.get(lhs.getId());
            long counterRhs = feedCounters.get(rhs.getId());
            if (counterLhs > counterRhs) {
                // reverse natural order: podcast with most unplayed episodes first
                return -1;
            } else if (counterLhs == counterRhs) {
                return lhs.getTitle().compareToIgnoreCase(rhs.getTitle());
            } else {
                return 1;
            }
        };
    } else if (feedOrder == UserPreferences.FEED_ORDER_ALPHABETICAL) {
        comparator = (lhs, rhs) -> {
            String t1 = lhs.getTitle();
            String t2 = rhs.getTitle();
            if (t1 == null) {
                return 1;
            } else if (t2 == null) {
                return -1;
            } else {
                return t1.compareToIgnoreCase(t2);
            }
        };
    } else {
        comparator = (lhs, rhs) -> {
            if (lhs.getItems() == null || lhs.getItems().size() == 0) {
                List<FeedItem> items = DBReader.getFeedItemList(lhs);
                lhs.setItems(items);
            }
            if (rhs.getItems() == null || rhs.getItems().size() == 0) {
                List<FeedItem> items = DBReader.getFeedItemList(rhs);
                rhs.setItems(items);
            }
            if (lhs.getMostRecentItem() == null) {
                return 1;
            } else if (rhs.getMostRecentItem() == null) {
                return -1;
            } else {
                Date d1 = lhs.getMostRecentItem().getPubDate();
                Date d2 = rhs.getMostRecentItem().getPubDate();
                return d2.compareTo(d1);
            }
        };
    }
    Collections.sort(feeds, comparator);
    int queueSize = adapter.getQueueSize();
    int numNewItems = adapter.getNumberOfNewItems();
    int numDownloadedItems = adapter.getNumberOfDownloadedEpisodes();
    NavDrawerData result = new NavDrawerData(feeds, queueSize, numNewItems, numDownloadedItems, feedCounters, UserPreferences.getEpisodeCleanupAlgorithm().getReclaimableItems());
    adapter.close();
    return result;
}
Also used : FeedItemPubdateComparator(de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator) Arrays(java.util.Arrays) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) Date(java.util.Date) DownloadStatusComparator(de.danoeh.antennapod.core.util.comparator.DownloadStatusComparator) VorbisCommentChapter(de.danoeh.antennapod.core.feed.VorbisCommentChapter) LongList(de.danoeh.antennapod.core.util.LongList) ArrayList(java.util.ArrayList) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) FeedImage(de.danoeh.antennapod.core.feed.FeedImage) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) Feed(de.danoeh.antennapod.core.feed.Feed) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) Log(android.util.Log) PlaybackCompletionDateComparator(de.danoeh.antennapod.core.util.comparator.PlaybackCompletionDateComparator) Cursor(android.database.Cursor) LongIntMap(de.danoeh.antennapod.core.util.LongIntMap) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) ID3Chapter(de.danoeh.antennapod.core.feed.ID3Chapter) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedPreferences(de.danoeh.antennapod.core.feed.FeedPreferences) List(java.util.List) Chapter(de.danoeh.antennapod.core.feed.Chapter) Comparator(java.util.Comparator) Collections(java.util.Collections) FlattrThing(de.danoeh.antennapod.core.util.flattr.FlattrThing) Date(java.util.Date) LongList(de.danoeh.antennapod.core.util.LongList) ArrayList(java.util.ArrayList) List(java.util.List) LongIntMap(de.danoeh.antennapod.core.util.LongIntMap) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 43 with FeedItem

use of de.danoeh.antennapod.core.feed.FeedItem in project AntennaPod by AntennaPod.

the class DBWriter method addFavoriteItemById.

public static Future<?> addFavoriteItemById(final long itemId) {
    return dbExec.submit(() -> {
        final FeedItem item = DBReader.getFeedItem(itemId);
        if (item == null) {
            Log.d(TAG, "Can't find item for itemId " + itemId);
            return;
        }
        final PodDBAdapter adapter = PodDBAdapter.getInstance().open();
        adapter.addFavoriteItem(item);
        adapter.close();
        item.addTag(FeedItem.TAG_FAVORITE);
        EventBus.getDefault().post(FavoritesEvent.added(item));
        EventBus.getDefault().post(FeedItemEvent.updated(item));
    });
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem)

Example 44 with FeedItem

use of de.danoeh.antennapod.core.feed.FeedItem in project AntennaPod by AntennaPod.

the class DBWriter method deleteFeedMediaOfItem.

/**
     * Deletes a downloaded FeedMedia file from the storage device.
     *
     * @param context A context that is used for opening a database connection.
     * @param mediaId ID of the FeedMedia object whose downloaded file should be deleted.
     */
public static Future<?> deleteFeedMediaOfItem(final Context context, final long mediaId) {
    return dbExec.submit(() -> {
        final FeedMedia media = DBReader.getFeedMedia(mediaId);
        if (media != null) {
            Log.i(TAG, String.format("Requested to delete FeedMedia [id=%d, title=%s, downloaded=%s", media.getId(), media.getEpisodeTitle(), String.valueOf(media.isDownloaded())));
            boolean result = false;
            if (media.isDownloaded()) {
                // delete downloaded media file
                File mediaFile = new File(media.getFile_url());
                if (mediaFile.exists()) {
                    result = mediaFile.delete();
                }
                media.setDownloaded(false);
                media.setFile_url(null);
                media.setHasEmbeddedPicture(false);
                PodDBAdapter adapter = PodDBAdapter.getInstance();
                adapter.open();
                adapter.setMedia(media);
                adapter.close();
                // If media is currently being played, change playback
                // type to 'stream' and shutdown playback service
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                if (PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA) {
                    if (media.getId() == PlaybackPreferences.getCurrentlyPlayingFeedMediaId()) {
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putBoolean(PlaybackPreferences.PREF_CURRENT_EPISODE_IS_STREAM, true);
                        editor.commit();
                    }
                    if (PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == media.getId()) {
                        context.sendBroadcast(new Intent(PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
                    }
                }
                // Gpodder: queue delete action for synchronization
                if (GpodnetPreferences.loggedIn()) {
                    FeedItem item = media.getItem();
                    GpodnetEpisodeAction action = new GpodnetEpisodeAction.Builder(item, GpodnetEpisodeAction.Action.DELETE).currentDeviceId().currentTimestamp().build();
                    GpodnetPreferences.enqueueEpisodeAction(action);
                }
            }
            Log.d(TAG, "Deleting File. Result: " + result);
            EventBus.getDefault().post(FeedItemEvent.deletedMedia(Collections.singletonList(media.getItem())));
            EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast();
        }
    });
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) File(java.io.File)

Example 45 with FeedItem

use of de.danoeh.antennapod.core.feed.FeedItem in project AntennaPod by AntennaPod.

the class DBWriter method addQueueItem.

/**
     * Appends FeedItem objects to the end of the queue. The 'read'-attribute of all items will be set to true.
     * If a FeedItem is already in the queue, the FeedItem will not change its position in the queue.
     *
     * @param context A context that is used for opening a database connection.
     * @param performAutoDownload true if an auto-download process should be started after the operation.
     * @param itemIds IDs of the FeedItem objects that should be added to the queue.
     */
public static Future<?> addQueueItem(final Context context, final boolean performAutoDownload, final long... itemIds) {
    return dbExec.submit(() -> {
        if (itemIds.length > 0) {
            final PodDBAdapter adapter = PodDBAdapter.getInstance();
            adapter.open();
            final List<FeedItem> queue = DBReader.getQueue(adapter);
            if (queue != null) {
                boolean queueModified = false;
                LongList markAsUnplayedIds = new LongList();
                List<QueueEvent> events = new ArrayList<>();
                List<FeedItem> updatedItems = new ArrayList<>();
                for (int i = 0; i < itemIds.length; i++) {
                    if (!itemListContains(queue, itemIds[i])) {
                        final FeedItem item = DBReader.getFeedItem(itemIds[i]);
                        if (item != null) {
                            // add item to either front ot back of queue
                            boolean addToFront = UserPreferences.enqueueAtFront();
                            if (addToFront) {
                                queue.add(0 + i, item);
                                events.add(QueueEvent.added(item, 0 + i));
                            } else {
                                queue.add(item);
                                events.add(QueueEvent.added(item, queue.size() - 1));
                            }
                            item.addTag(FeedItem.TAG_QUEUE);
                            updatedItems.add(item);
                            queueModified = true;
                            if (item.isNew()) {
                                markAsUnplayedIds.add(item.getId());
                            }
                        }
                    }
                }
                if (queueModified) {
                    adapter.setQueue(queue);
                    for (QueueEvent event : events) {
                        EventBus.getDefault().post(event);
                    }
                    EventBus.getDefault().post(FeedItemEvent.updated(updatedItems));
                    if (markAsUnplayedIds.size() > 0) {
                        DBWriter.markItemPlayed(FeedItem.UNPLAYED, markAsUnplayedIds.toArray());
                    }
                }
            }
            adapter.close();
            if (performAutoDownload) {
                DBTasks.autodownloadUndownloadedItems(context);
            }
        }
    });
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) ArrayList(java.util.ArrayList) QueueEvent(de.danoeh.antennapod.core.event.QueueEvent) LongList(de.danoeh.antennapod.core.util.LongList)

Aggregations

FeedItem (de.danoeh.antennapod.core.feed.FeedItem)149 Feed (de.danoeh.antennapod.core.feed.Feed)53 FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)40 Date (java.util.Date)35 ArrayList (java.util.ArrayList)33 PodDBAdapter (de.danoeh.antennapod.core.storage.PodDBAdapter)28 Cursor (android.database.Cursor)23 File (java.io.File)18 FeedImage (de.danoeh.antennapod.core.feed.FeedImage)15 Context (android.content.Context)12 FlakyTest (android.test.FlakyTest)11 View (android.view.View)10 Playable (de.danoeh.antennapod.core.util.playback.Playable)7 Intent (android.content.Intent)6 LayoutInflater (android.view.LayoutInflater)5 ListView (android.widget.ListView)5 SharedPreferences (android.content.SharedPreferences)4 Chapter (de.danoeh.antennapod.core.feed.Chapter)4 FeedFilter (de.danoeh.antennapod.core.feed.FeedFilter)4 LongList (de.danoeh.antennapod.core.util.LongList)4