Search in sources :

Example 31 with FeedMedia

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);
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) ArrayList(java.util.ArrayList) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 32 with FeedMedia

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());
            }
        }
    }
}
Also used : GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) ArrayMap(android.support.v4.util.ArrayMap) Pair(android.util.Pair)

Example 33 with FeedMedia

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;
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) Cursor(android.database.Cursor)

Example 34 with FeedMedia

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;
}
Also used : FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) ArrayMap(android.support.v4.util.ArrayMap) Cursor(android.database.Cursor)

Example 35 with FeedMedia

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;
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Aggregations

FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)53 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)39 Feed (de.danoeh.antennapod.core.feed.Feed)20 Date (java.util.Date)19 PodDBAdapter (de.danoeh.antennapod.core.storage.PodDBAdapter)16 ArrayList (java.util.ArrayList)13 FeedImage (de.danoeh.antennapod.core.feed.FeedImage)10 File (java.io.File)9 Intent (android.content.Intent)7 Playable (de.danoeh.antennapod.core.util.playback.Playable)7 Cursor (android.database.Cursor)6 SharedPreferences (android.content.SharedPreferences)4 TypedArray (android.content.res.TypedArray)3 Uri (android.net.Uri)3 View (android.view.View)3 Chapter (de.danoeh.antennapod.core.feed.Chapter)3 GpodnetEpisodeAction (de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction)3 Context (android.content.Context)2 ArrayMap (android.support.v4.util.ArrayMap)2 FlakyTest (android.test.FlakyTest)2