From 3a777628dce43d0b95279a2d12fa187552783363 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 11 Sep 2013 18:46:25 +0200 Subject: [PATCH] Added FeedPreferences class --- .../antennapod/feed/FeedPreferences.java | 21 ++++++++++ .../danoeh/antennapod/storage/DBReader.java | 40 ++++++++++++++----- .../danoeh/antennapod/storage/DBWriter.java | 20 +++++++++- .../antennapod/storage/PodDBAdapter.java | 39 +++++++++++++----- 4 files changed, 98 insertions(+), 22 deletions(-) create mode 100644 src/de/danoeh/antennapod/feed/FeedPreferences.java diff --git a/src/de/danoeh/antennapod/feed/FeedPreferences.java b/src/de/danoeh/antennapod/feed/FeedPreferences.java new file mode 100644 index 000000000..084aa5691 --- /dev/null +++ b/src/de/danoeh/antennapod/feed/FeedPreferences.java @@ -0,0 +1,21 @@ +package de.danoeh.antennapod.feed; + +/** + * Contains preferences for a single feed. + */ +public class FeedPreferences { + + private long feedID; + + public FeedPreferences(long feedID) { + this.feedID = feedID; + } + + public long getFeedID() { + return feedID; + } + + public void setFeedID(long feedID) { + this.feedID = feedID; + } +} diff --git a/src/de/danoeh/antennapod/storage/DBReader.java b/src/de/danoeh/antennapod/storage/DBReader.java index be38aa3eb..feebd2c34 100644 --- a/src/de/danoeh/antennapod/storage/DBReader.java +++ b/src/de/danoeh/antennapod/storage/DBReader.java @@ -10,14 +10,7 @@ import android.database.Cursor; import android.database.SQLException; import android.util.Log; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.feed.Chapter; -import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.feed.FeedImage; -import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.feed.FeedMedia; -import de.danoeh.antennapod.feed.ID3Chapter; -import de.danoeh.antennapod.feed.SimpleChapter; -import de.danoeh.antennapod.feed.VorbisCommentChapter; +import de.danoeh.antennapod.feed.*; import de.danoeh.antennapod.service.download.*; import de.danoeh.antennapod.util.DownloadError; import de.danoeh.antennapod.util.comparator.DownloadStatusComparator; @@ -28,7 +21,6 @@ import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator; * In general, all database calls in DBReader-methods are executed on the caller's thread. * This means that the caller should make sure that DBReader-methods are not executed on the GUI-thread. * This class will use the {@link de.danoeh.antennapod.feed.EventDistributor} to notify listeners about changes in the database. - */ public final class DBReader { private static final String TAG = "DBReader"; @@ -77,9 +69,10 @@ public final class DBReader { /** * Returns a list with the download URLs of all feeds. + * * @param context A context that is used for opening the database connection. * @return A list of Strings with the download URLs of all feeds. - * */ + */ public static List getFeedListDownloadUrls(final Context context) { PodDBAdapter adapter = new PodDBAdapter(context); List result = new ArrayList(); @@ -337,7 +330,7 @@ public final class DBReader { cursor.getString(PodDBAdapter.IDX_FEED_SEL_STD_DOWNLOAD_URL), cursor.getInt(PodDBAdapter.IDX_FEED_SEL_STD_DOWNLOADED) > 0); - if (image != null) { + if (image != null) { image.setFeed(feed); } return feed; @@ -775,4 +768,29 @@ public final class DBReader { return media; } + + private static FeedPreferences extractFeedPreferencesFromCursorRow(final Cursor cursor) { + return new FeedPreferences(cursor.getLong(PodDBAdapter.IDX_FEED_SEL_PREFERENCES_ID)); + } + + /** + * Loads the FeedPreferences-object of a specific Feed from the database. + * + * @param context A context that is used for opening a database connection. + * @param feedID ID of the Feed. + * @return The FeedPreferences of the Feed with the given ID or null if the no Feed could be found. + */ + public static FeedPreferences getFeedPreferencesOfFeed(final Context context, final long feedID) { + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + + Cursor prefCursor = adapter.getFeedPreferenceCursor(feedID); + if (prefCursor.moveToFirst()) { + FeedPreferences result = extractFeedPreferencesFromCursorRow(prefCursor); + prefCursor.close(); + return result; + } else { + return null; + } + } } diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index b5cab8b5a..f4b47e5fc 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -729,7 +729,7 @@ public class DBWriter { /** * Updates download URLs of feeds from a given Map. The key of the Map is the original URL of the feed * and the value is the updated URL - * */ + */ public static Future updateFeedDownloadURLs(final Context context, final Map urls) { return dbExec.submit(new Runnable() { @Override @@ -746,6 +746,24 @@ public class DBWriter { }); } + /** + * Saves a FeedPreferences object in the database. The Feed ID of the FeedPreferences-object MUST NOT be 0. + * + * @param context Used for opening a database connection. + * @param preferences The FeedPreferences object. + */ + public static Future setFeedPreferences(final Context context, final FeedPreferences preferences) { + return dbExec.submit(new Runnable() { + @Override + public void run() { + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + adapter.setFeedPreferences(preferences); + adapter.close(); + } + }); + } + private static boolean itemListContains(List items, long itemId) { for (FeedItem item : items) { if (item.getId() == itemId) { diff --git a/src/de/danoeh/antennapod/storage/PodDBAdapter.java b/src/de/danoeh/antennapod/storage/PodDBAdapter.java index 5f58e9302..bebbc7ff6 100644 --- a/src/de/danoeh/antennapod/storage/PodDBAdapter.java +++ b/src/de/danoeh/antennapod/storage/PodDBAdapter.java @@ -14,11 +14,7 @@ import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.feed.Chapter; -import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.feed.FeedImage; -import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.feed.*; import de.danoeh.antennapod.service.download.DownloadStatus; // TODO Remove media column from feeditem table @@ -195,9 +191,9 @@ public class PodDBAdapter { /** * Select all columns from the feed-table except feed preferences. - * */ + */ private static final String[] FEED_SEL_STD = { - TABLE_NAME_FEEDS + "." + KEY_ID, + TABLE_NAME_FEEDS + "." + KEY_ID, TABLE_NAME_FEEDS + "." + KEY_TITLE, TABLE_NAME_FEEDS + "." + KEY_FILE_URL, TABLE_NAME_FEEDS + "." + KEY_DOWNLOAD_URL, @@ -229,6 +225,16 @@ public class PodDBAdapter { public static final int IDX_FEED_SEL_STD_TYPE = 12; public static final int IDX_FEED_SEL_STD_FEED_IDENTIFIER = 13; + /** + * Select all preference-columns from the feed table + */ + private static final String[] FEED_SEL_PREFERENCES = { + TABLE_NAME_FEEDS + "." + KEY_ID, + // enter preferences here + }; + + // column indices for FEED_SEL_PREFERENCES + public static final int IDX_FEED_SEL_PREFERENCES_ID = 0; /** * Select all columns from the feeditems-table except description and @@ -362,6 +368,14 @@ public class PodDBAdapter { return feed.getId(); } + public void setFeedPreferences(FeedPreferences prefs) { + if (prefs.getFeedID() == 0) { + throw new IllegalArgumentException("Feed ID of preference must not be null"); + } + ContentValues values = new ContentValues(); + db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", new String[]{String.valueOf(prefs.getFeedID())}); + } + /** * Inserts or updates an image entry * @@ -490,7 +504,8 @@ public class PodDBAdapter { /** * 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 @@ -521,7 +536,7 @@ public class PodDBAdapter { new String[]{String.valueOf(item.getId())}); } if (item.getMedia() != null) { - setMedia(item.getMedia()); + setMedia(item.getMedia()); } if (item.getChapters() != null) { setChapters(item); @@ -797,7 +812,7 @@ public class PodDBAdapter { "SELECT %s FROM %s INNER JOIN %s ON %s=%s ORDER BY %s", args); Cursor c = db.rawQuery(query, null); /* - * Cursor c = db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, + * Cursor c = db.query(TABLE_NAME_FEED_ITEMS, FEEDITEM_SEL_FI_SMALL, * "INNER JOIN ? ON ?=?", new String[] { TABLE_NAME_QUEUE, * TABLE_NAME_FEED_ITEMS + "." + KEY_ID, TABLE_NAME_QUEUE + "." + * KEY_FEEDITEM }, null, null, TABLE_NAME_QUEUE + "." + KEY_FEEDITEM); @@ -915,6 +930,10 @@ public class PodDBAdapter { return c; } + public final Cursor getFeedPreferenceCursor(final long feedID) { + return db.query(TABLE_NAME_FEEDS, FEED_SEL_PREFERENCES, KEY_ID + "=" + feedID, null, null, null, null); + } + public final Cursor getFeedItemCursor(final String... ids) { if (ids.length > IN_OPERATOR_MAXIMUM) { throw new IllegalArgumentException(