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