Search in sources :

Example 1 with LongIntMap

use of de.danoeh.antennapod.core.util.LongIntMap 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 2 with LongIntMap

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

the class PodDBAdapter method getFeedCounters.

public final LongIntMap getFeedCounters(long... feedIds) {
    int setting = UserPreferences.getFeedCounterSetting();
    String whereRead;
    switch(setting) {
        case UserPreferences.FEED_COUNTER_SHOW_NEW_UNPLAYED_SUM:
            whereRead = "(" + KEY_READ + "=" + FeedItem.NEW + " OR " + KEY_READ + "=" + FeedItem.UNPLAYED + ")";
            break;
        case UserPreferences.FEED_COUNTER_SHOW_NEW:
            whereRead = KEY_READ + "=" + FeedItem.NEW;
            break;
        case UserPreferences.FEED_COUNTER_SHOW_UNPLAYED:
            whereRead = KEY_READ + "=" + FeedItem.UNPLAYED;
            break;
        case UserPreferences.FEED_COUNTER_SHOW_DOWNLOADED:
            whereRead = KEY_DOWNLOADED + "=1";
            break;
        case UserPreferences.FEED_COUNTER_SHOW_NONE:
        // deliberate fall-through
        default:
            // NONE
            return new LongIntMap(0);
    }
    // work around TextUtils.join wanting only boxed items
    // and StringUtils.join() causing NoSuchMethodErrors on MIUI
    StringBuilder builder = new StringBuilder();
    for (long id : feedIds) {
        builder.append(id);
        builder.append(',');
    }
    if (feedIds.length > 0) {
        // there's an extra ',', get rid of it
        builder.deleteCharAt(builder.length() - 1);
    }
    final String query = "SELECT " + KEY_FEED + ", COUNT(" + TABLE_NAME_FEED_ITEMS + "." + KEY_ID + ") AS count " + " FROM " + TABLE_NAME_FEED_ITEMS + " LEFT JOIN " + TABLE_NAME_FEED_MEDIA + " ON " + TABLE_NAME_FEED_ITEMS + "." + KEY_ID + "=" + TABLE_NAME_FEED_MEDIA + "." + KEY_FEEDITEM + " WHERE " + KEY_FEED + " IN (" + builder.toString() + ") " + " AND " + whereRead + " GROUP BY " + KEY_FEED;
    Cursor c = db.rawQuery(query, null);
    LongIntMap result = new LongIntMap(c.getCount());
    if (c.moveToFirst()) {
        do {
            long feedId = c.getLong(0);
            int count = c.getInt(1);
            result.put(feedId, count);
        } while (c.moveToNext());
    }
    c.close();
    return result;
}
Also used : LongIntMap(de.danoeh.antennapod.core.util.LongIntMap) Cursor(android.database.Cursor) MergeCursor(android.database.MergeCursor)

Example 3 with LongIntMap

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

the class LongLongMapTest method testAddAndDelete.

public void testAddAndDelete() {
    LongIntMap map = new LongIntMap();
    for (int i = 0; i < 100; i++) {
        map.put(i * 17, i * 42);
    }
    assertEquals(100, map.size());
    assertEquals(0, map.get(0));
    assertEquals(42, map.get(17));
    assertEquals(42, map.get(17, -1));
    assertEquals(1, map.indexOfKey(17));
    assertEquals(1, map.indexOfValue(42));
    for (int i = 0; i < 100; i++) {
        assertEquals(true, map.delete(i * 17));
    }
}
Also used : LongIntMap(de.danoeh.antennapod.core.util.LongIntMap)

Example 4 with LongIntMap

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

the class LongLongMapTest method testSingleElement.

public void testSingleElement() {
    LongIntMap map = new LongIntMap();
    map.put(17, 42);
    assertEquals(1, map.size());
    assertEquals("LongLongMap{17=42}", map.toString());
    assertEquals(42, map.get(17));
    assertEquals(42, map.get(17, -1));
    assertEquals(0, map.indexOfKey(17));
    assertEquals(0, map.indexOfValue(42));
    assertEquals(true, map.delete(17));
}
Also used : LongIntMap(de.danoeh.antennapod.core.util.LongIntMap)

Example 5 with LongIntMap

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

the class LongLongMapTest method testEmptyMap.

public void testEmptyMap() {
    LongIntMap map = new LongIntMap();
    assertEquals(0, map.size());
    assertEquals("LongLongMap{}", map.toString());
    assertEquals(0, map.get(42));
    assertEquals(-1, map.get(42, -1));
    assertEquals(false, map.delete(42));
    assertEquals(-1, map.indexOfKey(42));
    assertEquals(-1, map.indexOfValue(42));
    assertEquals(1, map.hashCode());
}
Also used : LongIntMap(de.danoeh.antennapod.core.util.LongIntMap)

Aggregations

LongIntMap (de.danoeh.antennapod.core.util.LongIntMap)6 Cursor (android.database.Cursor)2 MergeCursor (android.database.MergeCursor)1 ArrayMap (android.support.v4.util.ArrayMap)1 Log (android.util.Log)1 Chapter (de.danoeh.antennapod.core.feed.Chapter)1 Feed (de.danoeh.antennapod.core.feed.Feed)1 FeedImage (de.danoeh.antennapod.core.feed.FeedImage)1 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)1 FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)1 FeedPreferences (de.danoeh.antennapod.core.feed.FeedPreferences)1 ID3Chapter (de.danoeh.antennapod.core.feed.ID3Chapter)1 SimpleChapter (de.danoeh.antennapod.core.feed.SimpleChapter)1 VorbisCommentChapter (de.danoeh.antennapod.core.feed.VorbisCommentChapter)1 UserPreferences (de.danoeh.antennapod.core.preferences.UserPreferences)1 DownloadStatus (de.danoeh.antennapod.core.service.download.DownloadStatus)1 LongList (de.danoeh.antennapod.core.util.LongList)1 DownloadStatusComparator (de.danoeh.antennapod.core.util.comparator.DownloadStatusComparator)1 FeedItemPubdateComparator (de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator)1 PlaybackCompletionDateComparator (de.danoeh.antennapod.core.util.comparator.PlaybackCompletionDateComparator)1