use of de.danoeh.antennapod.core.feed.FeedMedia in project AntennaPod by AntennaPod.
the class DBReader method getStatistics.
/**
* Searches the DB for statistics
*
* @return The StatisticsInfo object
*/
public static StatisticsData getStatistics() {
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
long totalTime = 0;
List<StatisticsItem> feedTime = new ArrayList<>();
List<Feed> feeds = getFeedList();
for (Feed feed : feeds) {
long feedPlayedTime = 0;
long feedTotalTime = 0;
long episodes = 0;
long episodesStarted = 0;
List<FeedItem> items = getFeed(feed.getId()).getItems();
for (FeedItem item : items) {
FeedMedia media = item.getMedia();
if (media == null) {
continue;
}
// played duration used to be reset when the item is added to the playback history
if (media.getPlaybackCompletionDate() != null) {
feedPlayedTime += media.getDuration() / 1000;
}
feedPlayedTime += media.getPlayedDuration() / 1000;
if (media.getPlaybackCompletionDate() != null || media.getPlayedDuration() > 0) {
episodesStarted++;
}
feedTotalTime += media.getDuration() / 1000;
episodes++;
}
feedTime.add(new StatisticsItem(feed, feedTotalTime, feedPlayedTime, episodes, episodesStarted));
totalTime += feedPlayedTime;
}
Collections.sort(feedTime, (item1, item2) -> {
if (item1.timePlayed > item2.timePlayed) {
return -1;
} else if (item1.timePlayed < item2.timePlayed) {
return 1;
} else {
return 0;
}
});
adapter.close();
return new StatisticsData(totalTime, feedTime);
}
use of de.danoeh.antennapod.core.feed.FeedMedia in project AntennaPod by AntennaPod.
the class GpodnetSyncService method processEpisodeActions.
private synchronized void processEpisodeActions(List<GpodnetEpisodeAction> localActions, List<GpodnetEpisodeAction> remoteActions) throws DownloadRequestException {
if (remoteActions.size() == 0) {
return;
}
Map<Pair<String, String>, GpodnetEpisodeAction> localMostRecentPlayAction = new ArrayMap<>();
for (GpodnetEpisodeAction action : localActions) {
Pair<String, String> key = new Pair<>(action.getPodcast(), action.getEpisode());
GpodnetEpisodeAction mostRecent = localMostRecentPlayAction.get(key);
if (mostRecent == null || mostRecent.getTimestamp() == null) {
localMostRecentPlayAction.put(key, action);
} else if (mostRecent.getTimestamp().before(action.getTimestamp())) {
localMostRecentPlayAction.put(key, action);
}
}
// make sure more recent local actions are not overwritten by older remote actions
Map<Pair<String, String>, GpodnetEpisodeAction> mostRecentPlayAction = new ArrayMap<>();
for (GpodnetEpisodeAction action : remoteActions) {
switch(action.getAction()) {
case NEW:
FeedItem newItem = DBReader.getFeedItem(action.getPodcast(), action.getEpisode());
if (newItem != null) {
DBWriter.markItemPlayed(newItem, FeedItem.UNPLAYED, true);
} else {
Log.i(TAG, "Unknown feed item: " + action);
}
break;
case DOWNLOAD:
break;
case PLAY:
Pair<String, String> key = new Pair<>(action.getPodcast(), action.getEpisode());
GpodnetEpisodeAction localMostRecent = localMostRecentPlayAction.get(key);
if (localMostRecent == null || localMostRecent.getTimestamp() == null || localMostRecent.getTimestamp().before(action.getTimestamp())) {
GpodnetEpisodeAction mostRecent = mostRecentPlayAction.get(key);
if (mostRecent == null || mostRecent.getTimestamp() == null) {
mostRecentPlayAction.put(key, action);
} else if (action.getTimestamp() != null && mostRecent.getTimestamp().before(action.getTimestamp())) {
mostRecentPlayAction.put(key, action);
} else {
Log.d(TAG, "No date information in action, skipping it");
}
}
break;
case DELETE:
// NEVER EVER call DBWriter.deleteFeedMediaOfItem() here, leads to an infinite loop
break;
}
}
for (GpodnetEpisodeAction action : mostRecentPlayAction.values()) {
FeedItem playItem = DBReader.getFeedItem(action.getPodcast(), action.getEpisode());
if (playItem != null) {
FeedMedia media = playItem.getMedia();
media.setPosition(action.getPosition() * 1000);
DBWriter.setFeedMedia(media);
if (playItem.getMedia().hasAlmostEnded()) {
DBWriter.markItemPlayed(playItem, FeedItem.PLAYED, true);
DBWriter.addItemToPlaybackHistory(playItem.getMedia());
}
}
}
}
use of de.danoeh.antennapod.core.feed.FeedMedia in project AntennaPod by AntennaPod.
the class DBReader method getFeedMedia.
/**
* Searches the DB for a FeedMedia of the given id.
*
* @param mediaId The id of the object
* @return The found object
*/
public static FeedMedia getFeedMedia(final long mediaId) {
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
Cursor mediaCursor = adapter.getSingleFeedMediaCursor(mediaId);
if (!mediaCursor.moveToFirst()) {
mediaCursor.close();
return null;
}
int indexFeedItem = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM);
long itemId = mediaCursor.getLong(indexFeedItem);
FeedMedia media = FeedMedia.fromCursor(mediaCursor);
mediaCursor.close();
if (media != null) {
FeedItem item = getFeedItem(itemId);
if (item != null) {
media.setItem(item);
item.setMedia(media);
}
}
adapter.close();
return media;
}
use of de.danoeh.antennapod.core.feed.FeedMedia in project AntennaPod by AntennaPod.
the class DBReader method getFeedMedia.
private static Map<Long, FeedMedia> getFeedMedia(PodDBAdapter adapter, long... itemIds) {
String[] ids = new String[itemIds.length];
for (int i = 0, len = itemIds.length; i < len; i++) {
ids[i] = String.valueOf(itemIds[i]);
}
Map<Long, FeedMedia> result = new ArrayMap<>(itemIds.length);
Cursor cursor = adapter.getFeedMediaCursor(ids);
try {
if (cursor.moveToFirst()) {
do {
int index = cursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM);
long itemId = cursor.getLong(index);
FeedMedia media = FeedMedia.fromCursor(cursor);
result.put(itemId, media);
} while (cursor.moveToNext());
}
} finally {
cursor.close();
}
return result;
}
use of de.danoeh.antennapod.core.feed.FeedMedia in project AntennaPod by AntennaPod.
the class FeedHandlerTest method createTestFeed.
private Feed createTestFeed(int numItems, boolean withImage, boolean withFeedMedia, boolean withChapters) {
FeedImage image = null;
if (withImage) {
image = new FeedImage(0, "image", null, "http://example.com/picture", false);
}
Feed feed = new Feed(0, null, "title", "http://example.com", "This is the description", "http://example.com/payment", "Daniel", "en", null, "http://example.com/feed", image, file.getAbsolutePath(), "http://example.com/feed", true);
feed.setItems(new ArrayList<FeedItem>());
for (int i = 0; i < numItems; i++) {
FeedItem item = new FeedItem(0, "item-" + i, "http://example.com/item-" + i, "http://example.com/items/" + i, new Date(i * 60000), FeedItem.UNPLAYED, feed);
feed.getItems().add(item);
if (withFeedMedia) {
item.setMedia(new FeedMedia(0, item, 4711, 0, 1024 * 1024, "audio/mp3", null, "http://example.com/media-" + i, false, null, 0, 0));
}
}
return feed;
}
Aggregations