Merge pull request #2310 from dklimkin/datareader

Some cleanup on DBReader:
This commit is contained in:
Martin Fietz 2017-04-22 18:38:20 +02:00 committed by GitHub
commit 6a30dd611a
2 changed files with 442 additions and 383 deletions

View File

@ -4,7 +4,6 @@ import android.database.Cursor;
import android.support.v4.util.ArrayMap; import android.support.v4.util.ArrayMap;
import android.util.Log; import android.util.Log;
import java.util.Arrays;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -66,23 +65,28 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
List<Feed> result = getFeedList(adapter); try {
return getFeedList(adapter);
} finally {
adapter.close(); adapter.close();
return result; }
} }
private static List<Feed> getFeedList(PodDBAdapter adapter) { private static List<Feed> getFeedList(PodDBAdapter adapter) {
Cursor feedlistCursor = adapter.getAllFeedsCursor(); Cursor cursor = null;
List<Feed> feeds = new ArrayList<>(feedlistCursor.getCount()); try {
cursor = adapter.getAllFeedsCursor();
if (feedlistCursor.moveToFirst()) { List<Feed> feeds = new ArrayList<>(cursor.getCount());
do { while (cursor.moveToNext()) {
Feed feed = extractFeedFromCursorRow(adapter, feedlistCursor); Feed feed = extractFeedFromCursorRow(adapter, cursor);
feeds.add(feed); feeds.add(feed);
} while (feedlistCursor.moveToNext());
} }
feedlistCursor.close();
return feeds; return feeds;
} finally {
if (cursor != null) {
cursor.close();
}
}
} }
/** /**
@ -92,23 +96,26 @@ public final class DBReader {
*/ */
public static List<String> getFeedListDownloadUrls() { public static List<String> getFeedListDownloadUrls() {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
List<String> result = new ArrayList<>();
adapter.open(); adapter.open();
Cursor feeds = adapter.getFeedCursorDownloadUrls(); Cursor cursor = null;
if (feeds.moveToFirst()) { try {
do { cursor = adapter.getFeedCursorDownloadUrls();
result.add(feeds.getString(1)); List<String> result = new ArrayList<>(cursor.getCount());
} while (feeds.moveToNext()); while (cursor.moveToNext()) {
result.add(cursor.getString(1));
} }
feeds.close();
adapter.close();
return result; return result;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
/** /**
* Loads additional data in to the feed items from other database queries * Loads additional data in to the feed items from other database queries
*
* @param items the FeedItems who should have other data loaded * @param items the FeedItems who should have other data loaded
*/ */
public static void loadAdditionalFeedItemListData(List<FeedItem> items) { public static void loadAdditionalFeedItemListData(List<FeedItem> items) {
@ -139,16 +146,17 @@ public final class DBReader {
*/ */
public static void loadFeedDataOfFeedItemList(List<FeedItem> items) { public static void loadFeedDataOfFeedItemList(List<FeedItem> items) {
List<Feed> feeds = getFeedList(); List<Feed> feeds = getFeedList();
for (FeedItem item : items) {
Map<Long, Feed> feedIndex = new ArrayMap<>(feeds.size());
for (Feed feed : feeds) { for (Feed feed : feeds) {
if (feed.getId() == item.getFeedId()) { feedIndex.put(feed.getId(), feed);
item.setFeed(feed);
break;
} }
} for (FeedItem item : items) {
if (item.getFeed() == null) { Feed feed = feedIndex.get(item.getFeedId());
if (feed == null) {
Log.w(TAG, "No match found for item with ID " + item.getId() + ". Feed ID was " + item.getFeedId()); Log.w(TAG, "No match found for item with ID " + item.getId() + ". Feed ID was " + item.getFeedId());
} }
item.setFeed(feed);
} }
} }
@ -165,33 +173,35 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor cursor = null;
Cursor itemlistCursor = adapter.getAllItemsOfFeedCursor(feed); try {
List<FeedItem> items = extractItemlistFromCursor(adapter, cursor = adapter.getAllItemsOfFeedCursor(feed);
itemlistCursor); List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
itemlistCursor.close();
adapter.close();
Collections.sort(items, new FeedItemPubdateComparator()); Collections.sort(items, new FeedItemPubdateComparator());
for (FeedItem item : items) { for (FeedItem item : items) {
item.setFeed(feed); item.setFeed(feed);
} }
return items; return items;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
public static List<FeedItem> extractItemlistFromCursor(Cursor itemlistCursor) { public static List<FeedItem> extractItemlistFromCursor(Cursor itemlistCursor) {
Log.d(TAG, "extractItemlistFromCursor() called with: " + "itemlistCursor = [" + itemlistCursor + "]"); Log.d(TAG, "extractItemlistFromCursor() called with: " + "itemlistCursor = [" + itemlistCursor + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
List<FeedItem> result = extractItemlistFromCursor(adapter, itemlistCursor); try {
return extractItemlistFromCursor(adapter, itemlistCursor);
} finally {
adapter.close(); adapter.close();
return result; }
} }
private static List<FeedItem> extractItemlistFromCursor(PodDBAdapter adapter, private static List<FeedItem> extractItemlistFromCursor(PodDBAdapter adapter, Cursor cursor) {
Cursor cursor) {
List<FeedItem> result = new ArrayList<>(cursor.getCount()); List<FeedItem> result = new ArrayList<>(cursor.getCount());
LongList imageIds = new LongList(cursor.getCount()); LongList imageIds = new LongList(cursor.getCount());
@ -207,7 +217,7 @@ public final class DBReader {
itemIds.add(item.getId()); itemIds.add(item.getId());
} while (cursor.moveToNext()); } while (cursor.moveToNext());
Map<Long, FeedImage> images = getFeedImages(adapter, imageIds.toArray()); Map<Long, FeedImage> images = getFeedImages(adapter, imageIds.toArray());
Map<Long,FeedMedia> medias = getFeedMedia(adapter, itemIds.toArray()); Map<Long, FeedMedia> medias = getFeedMedia(adapter, itemIds);
for (int i = 0; i < result.size(); i++) { for (int i = 0; i < result.size(); i++) {
FeedItem item = result.get(i); FeedItem item = result.get(i);
long imageId = imageIds.get(i); long imageId = imageIds.get(i);
@ -223,15 +233,14 @@ public final class DBReader {
return result; return result;
} }
private static Map<Long,FeedMedia> getFeedMedia(PodDBAdapter adapter, private static Map<Long, FeedMedia> getFeedMedia(PodDBAdapter adapter, LongList itemIds) {
long... itemIds) { List<String> ids = new ArrayList<>(itemIds.size());
for (long item : itemIds.toArray()) {
String[] ids = new String[itemIds.length]; ids.add(String.valueOf(item));
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); Map<Long, FeedMedia> result = new ArrayMap<>(itemIds.size());
Cursor cursor = adapter.getFeedMediaCursor(ids.toArray(new String[0]));
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
@ -247,8 +256,7 @@ public final class DBReader {
return result; return result;
} }
private static Feed extractFeedFromCursorRow(PodDBAdapter adapter, private static Feed extractFeedFromCursorRow(PodDBAdapter adapter, Cursor cursor) {
Cursor cursor) {
final FeedImage image; final FeedImage image;
int indexImage = cursor.getColumnIndex(PodDBAdapter.KEY_IMAGE); int indexImage = cursor.getColumnIndex(PodDBAdapter.KEY_IMAGE);
long imageId = cursor.getLong(indexImage); long imageId = cursor.getLong(indexImage);
@ -272,11 +280,17 @@ public final class DBReader {
static List<FeedItem> getQueue(PodDBAdapter adapter) { static List<FeedItem> getQueue(PodDBAdapter adapter) {
Log.d(TAG, "getQueue()"); Log.d(TAG, "getQueue()");
Cursor itemlistCursor = adapter.getQueueCursor(); Cursor cursor = null;
List<FeedItem> items = extractItemlistFromCursor(adapter, itemlistCursor); try {
itemlistCursor.close(); cursor = adapter.getQueueCursor();
List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
loadAdditionalFeedItemListData(items); loadAdditionalFeedItemListData(items);
return items; return items;
} finally {
if (cursor != null) {
cursor.close();
}
}
} }
/** /**
@ -290,22 +304,27 @@ public final class DBReader {
Log.d(TAG, "getQueueIDList() called"); Log.d(TAG, "getQueueIDList() called");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
LongList result = getQueueIDList(adapter); try {
return getQueueIDList(adapter);
} finally {
adapter.close(); adapter.close();
return result; }
} }
static LongList getQueueIDList(PodDBAdapter adapter) { private static LongList getQueueIDList(PodDBAdapter adapter) {
Cursor queueCursor = adapter.getQueueIDCursor(); Cursor cursor = null;
try {
LongList queueIds = new LongList(queueCursor.getCount()); cursor = adapter.getQueueIDCursor();
if (queueCursor.moveToFirst()) { LongList queueIds = new LongList(cursor.getCount());
do { while (cursor.moveToNext()) {
queueIds.add(queueCursor.getLong(0)); queueIds.add(cursor.getLong(0));
} while (queueCursor.moveToNext());
} }
queueCursor.close();
return queueIds; return queueIds;
} finally {
if (cursor != null) {
cursor.close();
}
}
} }
/** /**
@ -316,13 +335,15 @@ public final class DBReader {
* list in a {@link de.danoeh.antennapod.core.util.QueueAccess} object for easier access to the queue's properties. * list in a {@link de.danoeh.antennapod.core.util.QueueAccess} object for easier access to the queue's properties.
*/ */
public static List<FeedItem> getQueue() { public static List<FeedItem> getQueue() {
Log.d(TAG, "getQueue() called with: " + ""); Log.d(TAG, "getQueue() called");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
List<FeedItem> items = getQueue(adapter); try {
return getQueue(adapter);
} finally {
adapter.close(); adapter.close();
return items; }
} }
/** /**
@ -331,22 +352,23 @@ public final class DBReader {
* @return A list of FeedItems whose episdoe has been downloaded. * @return A list of FeedItems whose episdoe has been downloaded.
*/ */
public static List<FeedItem> getDownloadedItems() { public static List<FeedItem> getDownloadedItems() {
Log.d(TAG, "getDownloadedItems() called with: " + ""); Log.d(TAG, "getDownloadedItems() called");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor cursor = null;
Cursor itemlistCursor = adapter.getDownloadedItemsCursor(); try {
List<FeedItem> items = extractItemlistFromCursor(adapter, cursor = adapter.getDownloadedItemsCursor();
itemlistCursor); List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
itemlistCursor.close();
loadAdditionalFeedItemListData(items); loadAdditionalFeedItemListData(items);
adapter.close();
Collections.sort(items, new FeedItemPubdateComparator()); Collections.sort(items, new FeedItemPubdateComparator());
return items; return items;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
/** /**
@ -359,20 +381,24 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor itemlistCursor = adapter.getUnreadItemsCursor(); Cursor cursor = null;
List<FeedItem> items = extractItemlistFromCursor(adapter, itemlistCursor); try {
itemlistCursor.close(); cursor = adapter.getUnreadItemsCursor();
List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
loadAdditionalFeedItemListData(items); loadAdditionalFeedItemListData(items);
adapter.close();
return items; return items;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
/** /**
* Loads a list of FeedItems that are considered new. * Loads a list of FeedItems that are considered new.
* Excludes items from feeds that do not have keep updated enabled. * Excludes items from feeds that do not have keep updated enabled.
*
* @return A list of FeedItems that are considered new. * @return A list of FeedItems that are considered new.
*/ */
public static List<FeedItem> getNewItemsList() { public static List<FeedItem> getNewItemsList() {
@ -380,16 +406,18 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor cursor = null;
Cursor itemlistCursor = adapter.getNewItemsCursor(); try {
List<FeedItem> items = extractItemlistFromCursor(adapter, itemlistCursor); cursor = adapter.getNewItemsCursor();
itemlistCursor.close(); List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
loadAdditionalFeedItemListData(items); loadAdditionalFeedItemListData(items);
adapter.close();
return items; return items;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
public static List<FeedItem> getFavoriteItemsList() { public static List<FeedItem> getFavoriteItemsList() {
@ -397,33 +425,39 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor cursor = null;
Cursor itemlistCursor = adapter.getFavoritesCursor(); try {
List<FeedItem> items = extractItemlistFromCursor(adapter, itemlistCursor); cursor = adapter.getFavoritesCursor();
itemlistCursor.close(); List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
loadAdditionalFeedItemListData(items); loadAdditionalFeedItemListData(items);
adapter.close();
return items; return items;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
public static LongList getFavoriteIDList() { public static LongList getFavoriteIDList() {
Log.d(TAG, "getFavoriteIDList() called"); Log.d(TAG, "getFavoriteIDList() called");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor favoritesCursor = adapter.getFavoritesCursor(); Cursor cursor = null;
try {
LongList favoriteIDs = new LongList(favoritesCursor.getCount()); cursor = adapter.getFavoritesCursor();
if (favoritesCursor.moveToFirst()) { LongList favoriteIDs = new LongList(cursor.getCount());
do { while (cursor.moveToNext()) {
favoriteIDs.add(favoritesCursor.getLong(0)); favoriteIDs.add(cursor.getLong(0));
} while (favoritesCursor.moveToNext());
} }
favoritesCursor.close();
adapter.close();
return favoriteIDs; return favoriteIDs;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
/** /**
@ -436,16 +470,18 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor cursor = null;
Cursor itemlistCursor = adapter.getRecentlyPublishedItemsCursor(limit); try {
List<FeedItem> items = extractItemlistFromCursor(adapter, itemlistCursor); cursor = adapter.getRecentlyPublishedItemsCursor(limit);
itemlistCursor.close(); List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
loadAdditionalFeedItemListData(items); loadAdditionalFeedItemListData(items);
adapter.close();
return items; return items;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
/** /**
@ -461,21 +497,29 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor mediaCursor = adapter.getCompletedMediaCursor(PLAYBACK_HISTORY_SIZE); Cursor mediaCursor = null;
Cursor itemCursor = null;
try {
mediaCursor = adapter.getCompletedMediaCursor(PLAYBACK_HISTORY_SIZE);
String[] itemIds = new String[mediaCursor.getCount()]; String[] itemIds = new String[mediaCursor.getCount()];
for (int i = 0; i < itemIds.length && mediaCursor.moveToPosition(i); i++) { for (int i = 0; i < itemIds.length && mediaCursor.moveToPosition(i); i++) {
int index = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM); int index = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM);
itemIds[i] = Long.toString(mediaCursor.getLong(index)); itemIds[i] = Long.toString(mediaCursor.getLong(index));
} }
mediaCursor.close(); itemCursor = adapter.getFeedItemCursor(itemIds);
Cursor itemCursor = adapter.getFeedItemCursor(itemIds);
List<FeedItem> items = extractItemlistFromCursor(adapter, itemCursor); List<FeedItem> items = extractItemlistFromCursor(adapter, itemCursor);
loadAdditionalFeedItemListData(items); loadAdditionalFeedItemListData(items);
itemCursor.close();
adapter.close();
Collections.sort(items, new PlaybackCompletionDateComparator()); Collections.sort(items, new PlaybackCompletionDateComparator());
return items; return items;
} finally {
if (mediaCursor != null) {
mediaCursor.close();
}
if (itemCursor != null) {
itemCursor.close();
}
adapter.close();
}
} }
/** /**
@ -489,19 +533,21 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor logCursor = adapter.getDownloadLogCursor(DOWNLOAD_LOG_SIZE); Cursor cursor = null;
List<DownloadStatus> downloadLog = new ArrayList<>(logCursor.getCount()); try {
cursor = adapter.getDownloadLogCursor(DOWNLOAD_LOG_SIZE);
if (logCursor.moveToFirst()) { List<DownloadStatus> downloadLog = new ArrayList<>(cursor.getCount());
do { while (cursor.moveToNext()) {
DownloadStatus status = DownloadStatus.fromCursor(logCursor); downloadLog.add(DownloadStatus.fromCursor(cursor));
downloadLog.add(status);
} while (logCursor.moveToNext());
} }
logCursor.close();
adapter.close();
Collections.sort(downloadLog, new DownloadStatusComparator()); Collections.sort(downloadLog, new DownloadStatusComparator());
return downloadLog; return downloadLog;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
/** /**
@ -516,19 +562,21 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor cursor = adapter.getDownloadLog(Feed.FEEDFILETYPE_FEED, feed.getId()); Cursor cursor = null;
try {
cursor = adapter.getDownloadLog(Feed.FEEDFILETYPE_FEED, feed.getId());
List<DownloadStatus> downloadLog = new ArrayList<>(cursor.getCount()); List<DownloadStatus> downloadLog = new ArrayList<>(cursor.getCount());
while (cursor.moveToNext()) {
if (cursor.moveToFirst()) { downloadLog.add(DownloadStatus.fromCursor(cursor));
do {
DownloadStatus status = DownloadStatus.fromCursor(cursor);
downloadLog.add(status);
} while (cursor.moveToNext());
} }
cursor.close();
adapter.close();
Collections.sort(downloadLog, new DownloadStatusComparator()); Collections.sort(downloadLog, new DownloadStatusComparator());
return downloadLog; return downloadLog;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
/** /**
@ -542,18 +590,21 @@ public final class DBReader {
Log.d(TAG, "getFeedStatisticsList() called"); Log.d(TAG, "getFeedStatisticsList() called");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
List<FeedItemStatistics> result = new ArrayList<>(); Cursor cursor = null;
Cursor cursor = adapter.getFeedStatisticsCursor(); try {
if (cursor.moveToFirst()) { cursor = adapter.getFeedStatisticsCursor();
do { List<FeedItemStatistics> result = new ArrayList<>(cursor.getCount());
while (cursor.moveToNext()) {
FeedItemStatistics fis = FeedItemStatistics.fromCursor(cursor); FeedItemStatistics fis = FeedItemStatistics.fromCursor(cursor);
result.add(fis); result.add(fis);
} while (cursor.moveToNext());
} }
cursor.close();
adapter.close();
return result; return result;
} finally {
if (cursor != null) {
cursor.close();
}
adapter.close();
}
} }
/** /**
@ -565,73 +616,58 @@ public final class DBReader {
*/ */
public static Feed getFeed(final long feedId) { public static Feed getFeed(final long feedId) {
Log.d(TAG, "getFeed() called with: " + "feedId = [" + feedId + "]"); Log.d(TAG, "getFeed() called with: " + "feedId = [" + feedId + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Feed result = getFeed(feedId, adapter); try {
return getFeed(feedId, adapter);
} finally {
adapter.close(); adapter.close();
return result; }
} }
static Feed getFeed(final long feedId, PodDBAdapter adapter) { static Feed getFeed(final long feedId, PodDBAdapter adapter) {
Feed feed = null; Feed feed = null;
Cursor cursor = null;
Cursor feedCursor = adapter.getFeedCursor(feedId); try {
if (feedCursor.moveToFirst()) { cursor = adapter.getFeedCursor(feedId);
feed = extractFeedFromCursorRow(adapter, feedCursor); if (cursor.moveToNext()) {
feed = extractFeedFromCursorRow(adapter, cursor);
feed.setItems(getFeedItemList(feed)); feed.setItems(getFeedItemList(feed));
} else { } else {
Log.e(TAG, "getFeed could not find feed with id " + feedId); Log.e(TAG, "getFeed could not find feed with id " + feedId);
} }
feedCursor.close();
return feed; return feed;
} finally {
if (cursor != null) {
cursor.close();
}
}
} }
static FeedItem getFeedItem(final long itemId, PodDBAdapter adapter) { private static FeedItem getFeedItem(final long itemId, PodDBAdapter adapter) {
Log.d(TAG, "Loading feeditem with id " + itemId); Log.d(TAG, "Loading feeditem with id " + itemId);
FeedItem item = null;
Cursor itemCursor = adapter.getFeedItemCursor(Long.toString(itemId)); FeedItem item = null;
if (!itemCursor.moveToFirst()) { Cursor cursor = null;
itemCursor.close(); try {
return null; cursor = adapter.getFeedItemCursor(Long.toString(itemId));
} if (cursor.moveToNext()) {
List<FeedItem> list = extractItemlistFromCursor(adapter, itemCursor); List<FeedItem> list = extractItemlistFromCursor(adapter, cursor);
itemCursor.close(); if (!list.isEmpty()) {
if (list.size() > 0) {
item = list.get(0); item = list.get(0);
loadAdditionalFeedItemListData(list); loadAdditionalFeedItemListData(list);
if (item.hasChapters()) { if (item.hasChapters()) {
loadChaptersOfFeedItem(adapter, item); loadChaptersOfFeedItem(adapter, item);
} }
} }
}
return item; return item;
} } finally {
if (cursor != null) {
static List<FeedItem> getFeedItems(PodDBAdapter adapter, final long... itemIds) { cursor.close();
String[] ids = new String[itemIds.length];
for(int i = 0; i < itemIds.length; i++) {
long itemId = itemIds[i];
ids[i] = Long.toString(itemId);
}
List<FeedItem> result;
Cursor itemCursor = adapter.getFeedItemCursor(ids);
if (itemCursor.moveToFirst()) {
result = extractItemlistFromCursor(adapter, itemCursor);
loadAdditionalFeedItemListData(result);
for(FeedItem item : result) {
if (item.hasChapters()) {
loadChaptersOfFeedItem(adapter, item);
} }
} }
} else {
result = Collections.emptyList();
}
itemCursor.close();
return result;
} }
/** /**
@ -647,46 +683,37 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
FeedItem item = getFeedItem(itemId, adapter); try {
return getFeedItem(itemId, adapter);
} finally {
adapter.close(); adapter.close();
return item; }
} }
static FeedItem getFeedItem(final String podcastUrl, final String episodeUrl, PodDBAdapter adapter) { static FeedItem getFeedItem(final String podcastUrl, final String episodeUrl, PodDBAdapter adapter) {
Log.d(TAG, "Loading feeditem with podcast url " + podcastUrl + " and episode url " + episodeUrl); Log.d(TAG, "Loading feeditem with podcast url " + podcastUrl + " and episode url " + episodeUrl);
Cursor cursor = null;
try {
cursor = adapter.getFeedItemCursor(podcastUrl, episodeUrl);
if (!cursor.moveToNext()) {
return null;
}
List<FeedItem> list = extractItemlistFromCursor(adapter, cursor);
FeedItem item = null; FeedItem item = null;
Cursor itemCursor = adapter.getFeedItemCursor(podcastUrl, episodeUrl); if (!list.isEmpty()) {
if (itemCursor.moveToFirst()) {
List<FeedItem> list = extractItemlistFromCursor(adapter, itemCursor);
if (list.size() > 0) {
item = list.get(0); item = list.get(0);
loadAdditionalFeedItemListData(list); loadAdditionalFeedItemListData(list);
if (item.hasChapters()) { if (item.hasChapters()) {
loadChaptersOfFeedItem(adapter, item); loadChaptersOfFeedItem(adapter, item);
} }
} }
}
itemCursor.close();
return item; return item;
} finally {
if (cursor != null) {
cursor.close();
}
} }
/**
* Loads specific FeedItems from the database. This method canbe used for loading more
* than one FeedItem
*
* @param itemIds The IDs of the FeedItems
* @return The FeedItems or an empty list if none of the FeedItems could be found. All FeedComponent-attributes
* as well as chapter marks of the FeedItems will also be loaded from the database.
*/
public static List<FeedItem> getFeedItems(final long... itemIds) {
Log.d(TAG, "getFeedItems() called with: " + "itemIds = [" + Arrays.toString(itemIds) + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
List<FeedItem> items = getFeedItems(adapter, itemIds);
adapter.close();
return items;
} }
/** /**
* Returns credentials based on image URL * Returns credentials based on image URL
@ -699,16 +726,18 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
String credentials = getImageAuthentication(imageUrl, adapter); try {
return getImageAuthentication(imageUrl, adapter);
} finally {
adapter.close(); adapter.close();
return credentials; }
} }
static String getImageAuthentication(final String imageUrl, PodDBAdapter adapter) { private static String getImageAuthentication(final String imageUrl, PodDBAdapter adapter) {
String credentials = null; String credentials = null;
Cursor cursor = adapter.getImageAuthenticationCursor(imageUrl); Cursor cursor = null;
try { try {
cursor = adapter.getImageAuthenticationCursor(imageUrl);
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
String username = cursor.getString(0); String username = cursor.getString(0);
String password = cursor.getString(1); String password = cursor.getString(1);
@ -721,8 +750,10 @@ public final class DBReader {
credentials = ""; credentials = "";
} }
} finally { } finally {
if (cursor != null) {
cursor.close(); cursor.close();
} }
}
return credentials; return credentials;
} }
@ -739,9 +770,11 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
FeedItem item = getFeedItem(podcastUrl, episodeUrl, adapter); try {
return getFeedItem(podcastUrl, episodeUrl, adapter);
} finally {
adapter.close(); adapter.close();
return item; }
} }
/** /**
@ -753,18 +786,24 @@ public final class DBReader {
Log.d(TAG, "loadExtraInformationOfFeedItem() called with: " + "item = [" + item + "]"); Log.d(TAG, "loadExtraInformationOfFeedItem() called with: " + "item = [" + item + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor extraCursor = adapter.getExtraInformationOfItem(item); Cursor cursor = null;
if (extraCursor.moveToFirst()) { try {
int indexDescription = extraCursor.getColumnIndex(PodDBAdapter.KEY_DESCRIPTION); cursor = adapter.getExtraInformationOfItem(item);
String description = extraCursor.getString(indexDescription); if (cursor.moveToFirst()) {
int indexContentEncoded = extraCursor.getColumnIndex(PodDBAdapter.KEY_CONTENT_ENCODED); int indexDescription = cursor.getColumnIndex(PodDBAdapter.KEY_DESCRIPTION);
String contentEncoded = extraCursor.getString(indexContentEncoded); String description = cursor.getString(indexDescription);
int indexContentEncoded = cursor.getColumnIndex(PodDBAdapter.KEY_CONTENT_ENCODED);
String contentEncoded = cursor.getString(indexContentEncoded);
item.setDescription(description); item.setDescription(description);
item.setContentEncoded(contentEncoded); item.setContentEncoded(contentEncoded);
} }
extraCursor.close(); } finally {
if (cursor != null) {
cursor.close();
}
adapter.close(); adapter.close();
} }
}
/** /**
* Loads the list of chapters that belongs to this FeedItem if available. This method overwrites * Loads the list of chapters that belongs to this FeedItem if available. This method overwrites
@ -775,52 +814,60 @@ public final class DBReader {
*/ */
public static void loadChaptersOfFeedItem(final FeedItem item) { public static void loadChaptersOfFeedItem(final FeedItem item) {
Log.d(TAG, "loadChaptersOfFeedItem() called with: " + "item = [" + item + "]"); Log.d(TAG, "loadChaptersOfFeedItem() called with: " + "item = [" + item + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
try {
loadChaptersOfFeedItem(adapter, item); loadChaptersOfFeedItem(adapter, item);
} finally {
adapter.close(); adapter.close();
} }
}
static void loadChaptersOfFeedItem(PodDBAdapter adapter, FeedItem item) { static void loadChaptersOfFeedItem(PodDBAdapter adapter, FeedItem item) {
Cursor chapterCursor = adapter.getSimpleChaptersOfFeedItemCursor(item); Cursor cursor = null;
if (chapterCursor.moveToFirst()) { try {
item.setChapters(new ArrayList<>()); cursor = adapter.getSimpleChaptersOfFeedItemCursor(item);
do { int chaptersCount = cursor.getCount();
int indexType = chapterCursor.getColumnIndex(PodDBAdapter.KEY_CHAPTER_TYPE); if (chaptersCount == 0) {
int indexStart = chapterCursor.getColumnIndex(PodDBAdapter.KEY_START); item.setChapters(null);
int indexTitle = chapterCursor.getColumnIndex(PodDBAdapter.KEY_TITLE); return;
int indexLink = chapterCursor.getColumnIndex(PodDBAdapter.KEY_LINK); }
item.setChapters(new ArrayList<>(chaptersCount));
while (cursor.moveToNext()) {
int indexType = cursor.getColumnIndex(PodDBAdapter.KEY_CHAPTER_TYPE);
int indexStart = cursor.getColumnIndex(PodDBAdapter.KEY_START);
int indexTitle = cursor.getColumnIndex(PodDBAdapter.KEY_TITLE);
int indexLink = cursor.getColumnIndex(PodDBAdapter.KEY_LINK);
int chapterType = chapterCursor.getInt(indexType); int chapterType = cursor.getInt(indexType);
Chapter chapter = null; Chapter chapter = null;
long start = chapterCursor.getLong(indexStart); long start = cursor.getLong(indexStart);
String title = chapterCursor.getString(indexTitle); String title = cursor.getString(indexTitle);
String link = chapterCursor.getString(indexLink); String link = cursor.getString(indexLink);
switch (chapterType) { switch (chapterType) {
case SimpleChapter.CHAPTERTYPE_SIMPLECHAPTER: case SimpleChapter.CHAPTERTYPE_SIMPLECHAPTER:
chapter = new SimpleChapter(start, title, item, chapter = new SimpleChapter(start, title, item, link);
link);
break; break;
case ID3Chapter.CHAPTERTYPE_ID3CHAPTER: case ID3Chapter.CHAPTERTYPE_ID3CHAPTER:
chapter = new ID3Chapter(start, title, item, chapter = new ID3Chapter(start, title, item, link);
link);
break; break;
case VorbisCommentChapter.CHAPTERTYPE_VORBISCOMMENT_CHAPTER: case VorbisCommentChapter.CHAPTERTYPE_VORBISCOMMENT_CHAPTER:
chapter = new VorbisCommentChapter(start, chapter = new VorbisCommentChapter(start, title, item, link);
title, item, link);
break; break;
} }
if (chapter != null) { if (chapter != null) {
int indexId = chapterCursor.getColumnIndex(PodDBAdapter.KEY_ID); int indexId = cursor.getColumnIndex(PodDBAdapter.KEY_ID);
chapter.setId(chapterCursor.getLong(indexId)); chapter.setId(cursor.getLong(indexId));
item.getChapters().add(chapter); item.getChapters().add(chapter);
} }
} while (chapterCursor.moveToNext());
} else {
item.setChapters(null);
} }
chapterCursor.close(); } finally {
if (cursor != null) {
cursor.close();
}
}
} }
/** /**
@ -828,13 +875,17 @@ public final class DBReader {
* *
* @return The number of downloaded episodes. * @return The number of downloaded episodes.
*/ */
public static int getNumberOfDownloadedEpisodes() { public static int getNumberOfDownloadedEpisodes() {
Log.d(TAG, "getNumberOfDownloadedEpisodes() called with: " + ""); Log.d(TAG, "getNumberOfDownloadedEpisodes() called with: " + "");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
final int result = adapter.getNumberOfDownloadedEpisodes(); try {
return adapter.getNumberOfDownloadedEpisodes();
} finally {
adapter.close(); adapter.close();
return result; }
} }
/** /**
@ -847,9 +898,11 @@ public final class DBReader {
Log.d(TAG, "getFeedImage() called with: " + "imageId = [" + imageId + "]"); Log.d(TAG, "getFeedImage() called with: " + "imageId = [" + imageId + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
FeedImage result = getFeedImage(adapter, imageId); try {
return getFeedImage(adapter, imageId);
} finally {
adapter.close(); adapter.close();
return result; }
} }
/** /**
@ -874,15 +927,16 @@ public final class DBReader {
ids[i] = String.valueOf(imageIds[i]); ids[i] = String.valueOf(imageIds[i]);
} }
Cursor cursor = adapter.getImageCursor(ids); Cursor cursor = adapter.getImageCursor(ids);
Map<Long, FeedImage> result = new ArrayMap<>(cursor.getCount()); int imageCount = cursor.getCount();
try { if (imageCount == 0) {
if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
do { Map<Long, FeedImage> result = new ArrayMap<>(imageCount);
try {
while (cursor.moveToNext()) {
FeedImage image = FeedImage.fromCursor(cursor); FeedImage image = FeedImage.fromCursor(cursor);
result.put(image.getId(), image); result.put(image.getId(), image);
} while(cursor.moveToNext()); }
} finally { } finally {
cursor.close(); cursor.close();
} }
@ -899,18 +953,16 @@ public final class DBReader {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
Cursor mediaCursor = adapter.getSingleFeedMediaCursor(mediaId); Cursor mediaCursor = null;
try {
mediaCursor = adapter.getSingleFeedMediaCursor(mediaId);
if (!mediaCursor.moveToFirst()) { if (!mediaCursor.moveToFirst()) {
mediaCursor.close();
return null; return null;
} }
int indexFeedItem = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM); int indexFeedItem = mediaCursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM);
long itemId = mediaCursor.getLong(indexFeedItem); long itemId = mediaCursor.getLong(indexFeedItem);
FeedMedia media = FeedMedia.fromCursor(mediaCursor); FeedMedia media = FeedMedia.fromCursor(mediaCursor);
mediaCursor.close();
if (media != null) { if (media != null) {
FeedItem item = getFeedItem(itemId); FeedItem item = getFeedItem(itemId);
if (item != null) { if (item != null) {
@ -918,10 +970,14 @@ public final class DBReader {
item.setMedia(media); item.setMedia(media);
} }
} }
adapter.close();
return media; return media;
} finally {
if (mediaCursor != null) {
mediaCursor.close();
}
adapter.close();
}
} }
/** /**
@ -1100,7 +1156,6 @@ public final class DBReader {
* Returns data necessary for displaying the navigation drawer. This includes * 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 * the list of subscriptions, the number of items in the queue and the number of unread
* items. * items.
*
*/ */
public static NavDrawerData getNavDrawerData() { public static NavDrawerData getNavDrawerData() {
Log.d(TAG, "getNavDrawerData() called with: " + ""); Log.d(TAG, "getNavDrawerData() called with: " + "");

View File

@ -1156,7 +1156,9 @@ public class PodDBAdapter {
+ TABLE_NAME_FEED_IMAGES + " WHERE " + KEY_ID + " IN " + TABLE_NAME_FEED_IMAGES + " WHERE " + KEY_ID + " IN "
+ buildInOperator(neededLength), parts); + buildInOperator(neededLength), parts);
} }
return new MergeCursor(cursors); Cursor result = new MergeCursor(cursors);
result.moveToFirst();
return result;
} else { } else {
return db.query(TABLE_NAME_FEED_IMAGES, null, KEY_ID + " IN " return db.query(TABLE_NAME_FEED_IMAGES, null, KEY_ID + " IN "
+ buildInOperator(length), imageIds, null, null, null); + buildInOperator(length), imageIds, null, null, null);
@ -1341,7 +1343,9 @@ public class PodDBAdapter {
+ TABLE_NAME_FEED_MEDIA + " WHERE " + KEY_FEEDITEM + " IN " + TABLE_NAME_FEED_MEDIA + " WHERE " + KEY_FEEDITEM + " IN "
+ buildInOperator(neededLength), parts); + buildInOperator(neededLength), parts);
} }
return new MergeCursor(cursors); Cursor result = new MergeCursor(cursors);
result.moveToFirst();
return result;
} else { } else {
return db.query(TABLE_NAME_FEED_MEDIA, null, KEY_FEEDITEM + " IN " return db.query(TABLE_NAME_FEED_MEDIA, null, KEY_FEEDITEM + " IN "
+ buildInOperator(length), itemIds, null, null, null); + buildInOperator(length), itemIds, null, null, null);