diff options
author | Simon Rutishauser <simon.rutishauser@web.de> | 2013-09-29 17:08:26 +0200 |
---|---|---|
committer | Simon Rutishauser <simon.rutishauser@web.de> | 2013-09-29 17:19:11 +0200 |
commit | 8fbd1ecf9ceec0523447346c1ae93e0cb612a96a (patch) | |
tree | 47d2ba041a28d44578ab7e72d2efe2fdbbaa751e /src/de/danoeh/antennapod/storage | |
parent | 83bb0a608440c21ce61af68df0a8be0c387f1c0c (diff) | |
download | AntennaPod-8fbd1ecf9ceec0523447346c1ae93e0cb612a96a.zip |
this consolidates the code from pull request #253 (flattr queue and auto flattr) into a single commit, rebased onto the current development branch
Diffstat (limited to 'src/de/danoeh/antennapod/storage')
-rw-r--r-- | src/de/danoeh/antennapod/storage/DBReader.java | 50 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/storage/DBTasks.java | 8 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/storage/DBWriter.java | 102 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/storage/PodDBAdapter.java | 39 |
4 files changed, 191 insertions, 8 deletions
diff --git a/src/de/danoeh/antennapod/storage/DBReader.java b/src/de/danoeh/antennapod/storage/DBReader.java index a5a4c8cd4..404952039 100644 --- a/src/de/danoeh/antennapod/storage/DBReader.java +++ b/src/de/danoeh/antennapod/storage/DBReader.java @@ -4,6 +4,7 @@ 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; @@ -22,6 +23,8 @@ import de.danoeh.antennapod.service.download.*; 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; /** * Provides methods for reading data from the AntennaPod database. @@ -216,6 +219,8 @@ public final class DBReader { .getInt(PodDBAdapter.IDX_FI_SMALL_READ) > 0)); item.setItemIdentifier(itemlistCursor .getString(PodDBAdapter.IDX_FI_SMALL_ITEM_IDENTIFIER)); + item.setFlattrStatus(new FlattrStatus(itemlistCursor + .getLong(PodDBAdapter.IDX_FI_SMALL_FLATTR_STATUS))); // extract chapters boolean hasSimpleChapters = itemlistCursor @@ -307,7 +312,8 @@ public final class DBReader { cursor.getString(PodDBAdapter.KEY_FILE_URL_INDEX), cursor.getString(PodDBAdapter.KEY_DOWNLOAD_URL_INDEX), cursor.getInt(PodDBAdapter.KEY_DOWNLOADED_INDEX) > 0, - playbackCompletionDate); + playbackCompletionDate, + cursor.getInt(PodDBAdapter.KEY_PLAYED_DURATION_INDEX)); } private static Feed extractFeedFromCursorRow(PodDBAdapter adapter, @@ -335,7 +341,9 @@ public final class DBReader { image, cursor.getString(PodDBAdapter.KEY_FILE_URL_INDEX), cursor.getString(PodDBAdapter.KEY_DOWNLOAD_URL_INDEX), - cursor.getInt(PodDBAdapter.KEY_DOWNLOADED_INDEX) > 0); + cursor.getInt(PodDBAdapter.KEY_DOWNLOADED_INDEX) > 0, + new FlattrStatus(cursor.getLong(PodDBAdapter.KEY_FEED_FLATTR_STATUS_INDEX)) + ); if (image != null) { image.setFeed(feed); @@ -775,4 +783,42 @@ public final class DBReader { return media; } + + + public static List<FlattrThing> getFlattrQueue(Context context) { + List<Feed> feeds = getFeedList(context); + List<FlattrThing> l = new LinkedList<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); + } + + Log.d(TAG, "Returning flattrQueueIterator for queue with " + l.size() + " items."); + return l; + } + + + 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; + } } diff --git a/src/de/danoeh/antennapod/storage/DBTasks.java b/src/de/danoeh/antennapod/storage/DBTasks.java index b9a1fd002..efc40a7bb 100644 --- a/src/de/danoeh/antennapod/storage/DBTasks.java +++ b/src/de/danoeh/antennapod/storage/DBTasks.java @@ -16,6 +16,8 @@ import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.util.Log; +import de.danoeh.antennapod.asynctask.FlattrClickWorker; +import de.danoeh.antennapod.asynctask.FlattrStatusFetcher; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.Feed; @@ -149,6 +151,12 @@ public final class DBTasks { GpodnetSyncService.sendSyncIntent(context); } }.start(); + + if (AppConfig.DEBUG) Log.d(TAG, "Flattring all pending things."); + new FlattrClickWorker(context).executeAsync(); // flattr pending things + + if (AppConfig.DEBUG) Log.d(TAG, "Fetching flattr status."); + new FlattrStatusFetcher(context).executeAsync(); } else { if (AppConfig.DEBUG) Log.d(TAG, diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index 9eb0ab643..cdebd5436 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -2,20 +2,26 @@ package de.danoeh.antennapod.storage; import java.io.File; import java.util.Date; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import java.net.URI; +import java.net.URISyntaxException; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; +import org.shredzone.flattr4j.model.Flattr; + import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.preference.PreferenceManager; import android.util.Log; +import android.net.Uri; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.feed.*; import de.danoeh.antennapod.preferences.GpodnetPreferences; @@ -24,6 +30,7 @@ import de.danoeh.antennapod.service.GpodnetSyncService; import de.danoeh.antennapod.service.PlaybackService; import de.danoeh.antennapod.service.download.DownloadStatus; import de.danoeh.antennapod.util.QueueAccess; +import de.danoeh.antennapod.util.flattr.*; /** * Provides methods for writing data to AntennaPod's database. @@ -825,4 +832,99 @@ public class DBWriter { } return false; } + + private static String normalizeURI(String uri) { + String normalizedURI = null; + if (uri != null) { + try { + normalizedURI = (new URI(uri)).normalize().toString(); + if (! normalizedURI.endsWith("/")) + normalizedURI = normalizedURI + "/"; + } + catch (URISyntaxException e) { + } + } + return normalizedURI; + } + + + // Set flattr status of the passed thing (either a FeedItem or a Feed) + public static void setFlattredStatus(Context context, FlattrThing thing) { + // must propagate this to back db + if (thing instanceof FeedItem) + DBWriter.setFeedItem(context, (FeedItem) thing); + else if (thing instanceof Feed) + DBWriter.setCompleteFeed(context, (Feed) thing); + else + Log.e(TAG, "flattrQueue processing - thing is neither FeedItem nor Feed"); + } + + /* + * 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 + */ + public static void setFlattredStatus(Context context, List<Flattr> flattrList) { + class FlattrLinkTime { + public String paymentLink; + public long time; + + FlattrLinkTime(String paymentLink, long time) { + this.paymentLink = paymentLink; + this.time = time; + } + } + + // build list with flattred things having normalized URLs + ArrayList<FlattrLinkTime> flattrLinkTime = new ArrayList<FlattrLinkTime>(flattrList.size()); + for (Flattr flattr: flattrList) { + flattrLinkTime.add(new FlattrLinkTime(normalizeURI(flattr.getThing().getUrl()), flattr.getCreated().getTime())); + if (AppConfig.DEBUG) + Log.d(TAG, "FlattredUrl: " + flattr.getThing().getUrl()); + } + + + String paymentLink; + List<Feed> feeds = DBReader.getFeedList(context); + for (Feed feed: feeds) { + // check if the feed has been flattred + paymentLink = feed.getPaymentLink(); + if (paymentLink != null) { + String feedThingUrl = normalizeURI(Uri.parse(paymentLink).getQueryParameter("url")); + + feed.getFlattrStatus().setUnflattred(); // reset our offline status tracking + + if (AppConfig.DEBUG) + Log.d(TAG, "Feed: Trying to match " + feedThingUrl); + for (FlattrLinkTime flattr: flattrLinkTime) { + if (flattr.paymentLink.equals(feedThingUrl)) { + feed.setFlattrStatus(new FlattrStatus(flattr.time)); + setCompleteFeed(context, feed); + break; + } + } + } + + // check if any of the feeditems have been flattred + for (FeedItem item: DBReader.getFeedItemList(context, feed)) { + paymentLink = item.getPaymentLink(); + + if (paymentLink != null) { + String feedItemThingUrl = normalizeURI(Uri.parse(paymentLink).getQueryParameter("url")); + + item.getFlattrStatus().setUnflattred(); // reset our offline status tracking + + if (AppConfig.DEBUG) + Log.d(TAG, "FeedItem: Trying to match " + feedItemThingUrl); + for (FlattrLinkTime flattr: flattrLinkTime) { + if (flattr.paymentLink.equals(feedItemThingUrl)) { + item.setFlattrStatus(new FlattrStatus(flattr.time)); + setFeedItem(context, item); + break; + } + } + } + } + } + } + } diff --git a/src/de/danoeh/antennapod/storage/PodDBAdapter.java b/src/de/danoeh/antennapod/storage/PodDBAdapter.java index e22fe56c9..71c0f787e 100644 --- a/src/de/danoeh/antennapod/storage/PodDBAdapter.java +++ b/src/de/danoeh/antennapod/storage/PodDBAdapter.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.storage; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import android.content.ContentValues; @@ -20,6 +21,7 @@ import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.service.download.DownloadStatus; +import de.danoeh.antennapod.util.flattr.FlattrStatus; // TODO Remove media column from feeditem table @@ -28,7 +30,7 @@ import de.danoeh.antennapod.service.download.DownloadStatus; */ public class PodDBAdapter { private static final String TAG = "PodDBAdapter"; - private static final int DATABASE_VERSION = 9; + private static final int DATABASE_VERSION = 10; public static final String DATABASE_NAME = "Antennapod.db"; /** @@ -58,6 +60,7 @@ public class PodDBAdapter { public static final int KEY_IMAGE_INDEX = 11; public static final int KEY_TYPE_INDEX = 12; public static final int KEY_FEED_IDENTIFIER_INDEX = 13; + public static final int KEY_FEED_FLATTR_STATUS_INDEX = 14; // ----------- FeedItem indices public static final int KEY_CONTENT_ENCODED_INDEX = 2; public static final int KEY_PUBDATE_INDEX = 3; @@ -66,6 +69,7 @@ public class PodDBAdapter { public static final int KEY_FEED_INDEX = 9; public static final int KEY_HAS_SIMPLECHAPTERS_INDEX = 10; public static final int KEY_ITEM_IDENTIFIER_INDEX = 11; + public static final int KEY_ITEM_FLATTR_STATUS_INDEX = 12; // ---------- FeedMedia indices public static final int KEY_DURATION_INDEX = 1; public static final int KEY_POSITION_INDEX = 5; @@ -73,6 +77,7 @@ public class PodDBAdapter { public static final int KEY_MIME_TYPE_INDEX = 7; public static final int KEY_PLAYBACK_COMPLETION_DATE_INDEX = 8; public static final int KEY_MEDIA_FEEDITEM_INDEX = 9; + public static final int KEY_PLAYED_DURATION_INDEX = 10; // --------- Download log indices public static final int KEY_FEEDFILE_INDEX = 1; public static final int KEY_FEEDFILETYPE_INDEX = 2; @@ -123,11 +128,13 @@ 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_FEED_IDENTIFIER = "feed_identifier"; public static final String KEY_REASON_DETAILED = "reason_detailed"; public static final String KEY_DOWNLOADSTATUS_TITLE = "title"; public static final String KEY_CHAPTER_TYPE = "type"; public static final String KEY_PLAYBACK_COMPLETION_DATE = "playback_completion_date"; + public static final String KEY_PLAYED_DURATION = "played_duration"; // Table names public static final String TABLE_NAME_FEEDS = "Feeds"; @@ -149,8 +156,8 @@ public class PodDBAdapter { + KEY_DESCRIPTION + " TEXT," + KEY_PAYMENT_LINK + " TEXT," + KEY_LASTUPDATE + " TEXT," + KEY_LANGUAGE + " TEXT," + KEY_AUTHOR + " TEXT," + KEY_IMAGE + " INTEGER," + KEY_TYPE + " TEXT," - + KEY_FEED_IDENTIFIER + " TEXT)"; - ; + + KEY_FEED_IDENTIFIER + " TEXT," + + KEY_FLATTR_STATUS + " LONG)"; private static final String CREATE_TABLE_FEED_ITEMS = "CREATE TABLE " + TABLE_NAME_FEED_ITEMS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE @@ -158,7 +165,8 @@ public class PodDBAdapter { + " INTEGER," + KEY_READ + " INTEGER," + KEY_LINK + " TEXT," + KEY_DESCRIPTION + " TEXT," + KEY_PAYMENT_LINK + " TEXT," + KEY_MEDIA + " INTEGER," + KEY_FEED + " INTEGER," - + KEY_HAS_CHAPTERS + " INTEGER," + KEY_ITEM_IDENTIFIER + " TEXT)"; + + KEY_HAS_CHAPTERS + " INTEGER," + KEY_ITEM_IDENTIFIER + " TEXT," + + KEY_FLATTR_STATUS + " LONG)"; private static final String CREATE_TABLE_FEED_IMAGES = "CREATE TABLE " + TABLE_NAME_FEED_IMAGES + " (" + TABLE_PRIMARY_KEY + KEY_TITLE @@ -171,7 +179,8 @@ public class PodDBAdapter { + " TEXT," + KEY_DOWNLOADED + " INTEGER," + KEY_POSITION + " INTEGER," + KEY_SIZE + " INTEGER," + KEY_MIME_TYPE + " TEXT," + KEY_PLAYBACK_COMPLETION_DATE + " INTEGER," - + KEY_FEEDITEM + " INTEGER)"; + + KEY_FEEDITEM + " INTEGER," + + KEY_PLAYED_DURATION + " INTEGER)"; private static final String CREATE_TABLE_DOWNLOAD_LOG = "CREATE TABLE " + TABLE_NAME_DOWNLOAD_LOG + " (" + TABLE_PRIMARY_KEY + KEY_FEEDFILE @@ -206,7 +215,8 @@ public class PodDBAdapter { TABLE_NAME_FEED_ITEMS + "." + KEY_PAYMENT_LINK, KEY_MEDIA, TABLE_NAME_FEED_ITEMS + "." + KEY_FEED, TABLE_NAME_FEED_ITEMS + "." + KEY_HAS_CHAPTERS, - TABLE_NAME_FEED_ITEMS + "." + KEY_ITEM_IDENTIFIER}; + TABLE_NAME_FEED_ITEMS + "." + KEY_ITEM_IDENTIFIER, + TABLE_NAME_FEED_ITEMS + "." + KEY_FLATTR_STATUS}; /** * Contains SEL_FI_SMALL as comma-separated list. Useful for raw queries. @@ -230,6 +240,7 @@ public class PodDBAdapter { public static final int IDX_FI_SMALL_FEED = 7; public static final int IDX_FI_SMALL_HAS_CHAPTERS = 8; public static final int IDX_FI_SMALL_ITEM_IDENTIFIER = 9; + public static final int IDX_FI_SMALL_FLATTR_STATUS = 10; /** * Select id, description and content-encoded column from feeditems. @@ -311,6 +322,7 @@ public class PodDBAdapter { values.put(KEY_LASTUPDATE, feed.getLastUpdate().getTime()); values.put(KEY_TYPE, feed.getType()); values.put(KEY_FEED_IDENTIFIER, feed.getFeedIdentifier()); + values.put(KEY_FLATTR_STATUS, feed.getFlattrStatus().toLong()); if (feed.getId() == 0) { // Create new entry if (AppConfig.DEBUG) @@ -391,6 +403,7 @@ public class PodDBAdapter { ContentValues values = new ContentValues(); values.put(KEY_POSITION, media.getPosition()); values.put(KEY_DURATION, media.getDuration()); + values.put(KEY_PLAYED_DURATION, media.getPlayedDuration()); db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID + "=?", new String[]{String.valueOf(media.getId())}); } else { @@ -477,6 +490,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()); if (item.getId() == 0) { item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values)); } else { @@ -1118,6 +1132,19 @@ public class PodDBAdapter { } feeditemCursor.close(); } + if (oldVersion <= 9) { + db.execSQL("ALTER TABLE " + TABLE_NAME_FEEDS + + " ADD COLUMN " + KEY_FLATTR_STATUS + + " LONG"); + db.execSQL("ALTER TABLE " + TABLE_NAME_FEED_ITEMS + + " ADD COLUMN " + KEY_FLATTR_STATUS + + " LONG"); + } + if (oldVersion <= 10) { + db.execSQL("ALTER TABLE " + TABLE_NAME_FEED_MEDIA + + " ADD COLUMN " + KEY_PLAYED_DURATION + + " INTEGER"); + } } } } |