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;
}
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;
}
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));
});
}
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();
}
});
}
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);
}
}
});
}
Aggregations