Load only Flattrables in flattr queue from the database.

This commit is contained in:
daniel oeh 2014-01-04 01:48:15 +01:00
parent 8c116142b8
commit 0921e49d7c
3 changed files with 94 additions and 48 deletions

View File

@ -1,24 +1,23 @@
package de.danoeh.antennapod.storage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.LinkedList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.feed.*;
import de.danoeh.antennapod.service.download.*;
import de.danoeh.antennapod.service.download.DownloadStatus;
import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.comparator.DownloadStatusComparator;
import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator;
import de.danoeh.antennapod.util.flattr.FlattrStatus;
import de.danoeh.antennapod.util.flattr.FlattrThing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* Provides methods for reading data from the AntennaPod database.
* In general, all database calls in DBReader-methods are executed on the caller's thread.
@ -126,6 +125,7 @@ public final class DBReader {
* Takes a list of FeedItems and loads their corresponding Feed-objects from the database.
* The feedID-attribute of a FeedItem must be set to the ID of its feed or the method will
* not find the correct feed of an item.
*
* @param context A context that is used for opening a database connection.
* @param items The FeedItems whose Feed-objects should be loaded.
*/
@ -783,40 +783,47 @@ public final class DBReader {
}
/**
* Returns the flattr queue as a List of FlattrThings. The list consists of Feeds and FeedItems.
*
* @param context A context that is used for opening a database connection.
* @return The flattr queue as a List.
*/
public static List<FlattrThing> getFlattrQueue(Context context) {
List<Feed> feeds = getFeedList(context);
List<FlattrThing> l = new LinkedList<FlattrThing>();
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
List<FlattrThing> result = new ArrayList<FlattrThing>();
for (Feed feed : feeds) {
if (feed.getFlattrStatus().getFlattrQueue())
l.add(feed);
for (FeedItem item : getFeedItemList(context, feed))
if (item.getFlattrStatus().getFlattrQueue())
l.add(item);
// load feeds
Cursor feedCursor = adapter.getFeedsInFlattrQueueCursor();
if (feedCursor.moveToFirst()) {
do {
result.add(extractFeedFromCursorRow(adapter, feedCursor));
} while (feedCursor.moveToNext());
}
feedCursor.close();
Log.d(TAG, "Returning flattrQueueIterator for queue with " + l.size() + " items.");
return l;
//load feed items
Cursor feedItemCursor = adapter.getFeedItemsInFlattrQueueCursor();
result.addAll(extractItemlistFromCursor(adapter, feedItemCursor));
feedItemCursor.close();
adapter.close();
Log.d(TAG, "Returning flattrQueueIterator for queue with " + result.size() + " items.");
return result;
}
/**
* Returns true if the flattr queue is empty.
*
* @param context A context that is used for opening a database connection.
*/
public static boolean getFlattrQueueEmpty(Context context) {
List<Feed> feeds = getFeedList(context);
for (Feed feed : feeds) {
if (feed.getFlattrStatus().getFlattrQueue())
return false;
}
for (Feed feed : feeds) {
for (FeedItem item : getFeedItemList(context, feed))
if (item.getFlattrStatus().getFlattrQueue())
return false;
}
Log.d(TAG, "getFlattrQueueEmpty() = true");
return true;
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
boolean empty = adapter.getFlattrQueueSize() == 0;
adapter.close();
return empty;
}
}

View File

@ -896,7 +896,7 @@ public class DBWriter {
return null;
}
/*
/**
* Set flattr status of the feeds/feeditems in flattrList to flattred at the given timestamp,
* where the information has been retrieved from the flattr API
*/

View File

@ -1,9 +1,5 @@
package de.danoeh.antennapod.storage;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
@ -19,6 +15,9 @@ import de.danoeh.antennapod.feed.*;
import de.danoeh.antennapod.service.download.DownloadStatus;
import de.danoeh.antennapod.util.flattr.FlattrStatus;
import java.util.Arrays;
import java.util.List;
// TODO Remove media column from feeditem table
/**
@ -124,7 +123,7 @@ public class PodDBAdapter {
public static final String KEY_HAS_CHAPTERS = "has_simple_chapters";
public static final String KEY_TYPE = "type";
public static final String KEY_ITEM_IDENTIFIER = "item_identifier";
public static final String KEY_FLATTR_STATUS = "flattr_status";
public static final String KEY_FLATTR_STATUS = "flattr_status";
public static final String KEY_FEED_IDENTIFIER = "feed_identifier";
public static final String KEY_REASON_DETAILED = "reason_detailed";
public static final String KEY_DOWNLOADSTATUS_TITLE = "title";
@ -363,7 +362,7 @@ public class PodDBAdapter {
Log.d(TAG, "Setting feed with flattr status " + feed.getTitle() + ": " + feed.getFlattrStatus().toLong());
values.put(KEY_FLATTR_STATUS, feed.getFlattrStatus().toLong());
values.put(KEY_FLATTR_STATUS, feed.getFlattrStatus().toLong());
if (feed.getId() == 0) {
// Create new entry
if (AppConfig.DEBUG)
@ -491,13 +490,53 @@ public class PodDBAdapter {
/**
* Update the flattr status of a feed
*/
public void setFeedFlattrStatus(Feed feed)
{
public void setFeedFlattrStatus(Feed feed) {
ContentValues values = new ContentValues();
values.put(KEY_FLATTR_STATUS, feed.getFlattrStatus().toLong());
db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", new String[]{String.valueOf(feed.getId())});
}
/**
* Get all feeds in the flattr queue.
*/
public Cursor getFeedsInFlattrQueueCursor() {
return db.query(TABLE_NAME_FEEDS, FEED_SEL_STD, KEY_FLATTR_STATUS + "=?",
new String[]{String.valueOf(FlattrStatus.STATUS_QUEUE)},null, null, null);
}
/**
* Get all feed items in the flattr queue.
*/
public Cursor getFeedItemsInFlattrQueueCursor() {
return db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, KEY_FLATTR_STATUS + "=?",
new String[]{String.valueOf(FlattrStatus.STATUS_QUEUE)},null, null, null);
}
/**
* Counts feeds and feed items in the flattr queue
*/
public int getFlattrQueueSize() {
int res = 0;
Cursor c = db.rawQuery(String.format("SELECT count(*) FROM %s WHERE %s=%s",
TABLE_NAME_FEEDS, KEY_FLATTR_STATUS, String.valueOf(FlattrStatus.STATUS_QUEUE)), null);
if (c.moveToFirst()) {
res = c.getInt(0);
c.close();
} else {
Log.e(TAG, "Unable to determine size of flattr queue: Could not count number of feeds");
}
c = db.rawQuery(String.format("SELECT count(*) FROM %s WHERE %s=%s",
TABLE_NAME_FEED_ITEMS, KEY_FLATTR_STATUS, String.valueOf(FlattrStatus.STATUS_QUEUE)), null);
if (c.moveToFirst()) {
res += c.getInt(0);
c.close();
} else {
Log.e(TAG, "Unable to determine size of flattr queue: Could not count number of feed items");
}
return res;
}
/**
* Updates the download URL of a Feed.
*/
@ -527,17 +566,17 @@ public class PodDBAdapter {
/**
* Update the flattr status of a FeedItem
*/
public void setFeedItemFlattrStatus(FeedItem feedItem)
{
public void setFeedItemFlattrStatus(FeedItem feedItem) {
ContentValues values = new ContentValues();
values.put(KEY_FLATTR_STATUS, feedItem.getFlattrStatus().toLong());
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", new String[] { String.valueOf(feedItem.getId()) } );
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", new String[]{String.valueOf(feedItem.getId())});
}
/**
* Inserts or updates a feeditem entry
* @param item The FeedItem
*
* @param item The FeedItem
* @param saveFeed true if the Feed of the item should also be saved. This should be set to
* false if the method is executed on a list of FeedItems of the same Feed.
* @return the id of the entry
@ -561,7 +600,7 @@ public class PodDBAdapter {
values.put(KEY_READ, item.isRead());
values.put(KEY_HAS_CHAPTERS, item.getChapters() != null);
values.put(KEY_ITEM_IDENTIFIER, item.getItemIdentifier());
values.put(KEY_FLATTR_STATUS, item.getFlattrStatus().toLong());
values.put(KEY_FLATTR_STATUS, item.getFlattrStatus().toLong());
if (item.getId() == 0) {
item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values));
} else {