Added support for Simplechapters

This commit is contained in:
daniel oeh 2012-06-26 19:50:32 +02:00
parent e5e7015df3
commit c3509b1b92
8 changed files with 169 additions and 25 deletions

View File

@ -1,5 +1,6 @@
package de.podfetcher.feed; package de.podfetcher.feed;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -19,6 +20,7 @@ public class FeedItem extends FeedComponent{
private Feed feed; private Feed feed;
protected boolean read; protected boolean read;
private String paymentLink; private String paymentLink;
private ArrayList<SimpleChapter> simpleChapters;
public FeedItem() { public FeedItem() {
this.read = true; this.read = true;
@ -104,5 +106,12 @@ public class FeedItem extends FeedComponent{
this.paymentLink = paymentLink; this.paymentLink = paymentLink;
} }
public ArrayList<SimpleChapter> getSimpleChapters() {
return simpleChapters;
}
public void setSimpleChapters(ArrayList<SimpleChapter> simpleChapters) {
this.simpleChapters = simpleChapters;
}
} }

View File

@ -454,6 +454,19 @@ public class FeedManager {
if (!item.read) { if (!item.read) {
unreadItems.add(item); unreadItems.add(item);
} }
// extract chapters
Cursor chapterCursor = adapter.getSimpleChaptersOfFeedItemCursor(item);
if (chapterCursor.moveToFirst()) {
item.setSimpleChapters(new ArrayList<SimpleChapter>());
do {
SimpleChapter chapter = new SimpleChapter(chapterCursor.getLong(chapterCursor.getColumnIndex(PodDBAdapter.KEY_START)),
chapterCursor.getString(chapterCursor.getColumnIndex(PodDBAdapter.KEY_TITLE)));
item.getSimpleChapters().add(chapter);
} while (chapterCursor.moveToNext());
}
chapterCursor.close();
items.add(item); items.add(item);
} while (itemlistCursor.moveToNext()); } while (itemlistCursor.moveToNext());
} }

View File

@ -0,0 +1,22 @@
package de.podfetcher.feed;
public class SimpleChapter extends FeedComponent {
public long getStart() {
return start;
}
public SimpleChapter(long start, String title) {
super();
this.start = start;
this.title = title;
}
public String getTitle() {
return title;
}
/** Defines starting point in milliseconds. */
private long start;
private String title;
}

View File

@ -8,6 +8,7 @@ import de.podfetcher.feed.FeedCategory;
import de.podfetcher.feed.FeedImage; import de.podfetcher.feed.FeedImage;
import de.podfetcher.feed.FeedItem; import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedMedia; import de.podfetcher.feed.FeedMedia;
import de.podfetcher.feed.SimpleChapter;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
@ -53,6 +54,7 @@ public class PodDBAdapter {
public static final String KEY_FEEDITEM = "feeditem"; public static final String KEY_FEEDITEM = "feeditem";
public static final String KEY_CONTENT_ENCODED = "content_encoded"; public static final String KEY_CONTENT_ENCODED = "content_encoded";
public static final String KEY_PAYMENT_LINK = "payment_link"; public static final String KEY_PAYMENT_LINK = "payment_link";
public static final String KEY_START = "start";
// Table names // Table names
public static final String TABLE_NAME_FEEDS = "Feeds"; public static final String TABLE_NAME_FEEDS = "Feeds";
@ -62,6 +64,7 @@ public class PodDBAdapter {
public static final String TABLE_NAME_FEED_MEDIA = "FeedMedia"; public static final String TABLE_NAME_FEED_MEDIA = "FeedMedia";
public static final String TABLE_NAME_DOWNLOAD_LOG = "DownloadLog"; public static final String TABLE_NAME_DOWNLOAD_LOG = "DownloadLog";
public static final String TABLE_NAME_QUEUE = "Queue"; public static final String TABLE_NAME_QUEUE = "Queue";
public static final String TABLE_NAME_SIMPLECHAPTERS = "SimpleChapters";
// SQL Statements for creating new tables // SQL Statements for creating new tables
private static final String TABLE_PRIMARY_KEY = KEY_ID private static final String TABLE_PRIMARY_KEY = KEY_ID
@ -77,10 +80,9 @@ public class PodDBAdapter {
private static final String CREATE_TABLE_FEED_ITEMS = "CREATE TABLE " private static final String CREATE_TABLE_FEED_ITEMS = "CREATE TABLE "
+ TABLE_NAME_FEED_ITEMS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE + TABLE_NAME_FEED_ITEMS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE
+ " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION + " TEXT," + " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION + " TEXT,"
+ KEY_CONTENT_ENCODED + " TEXT," + KEY_CONTENT_ENCODED + " TEXT," + KEY_PUBDATE + " INTEGER,"
+ KEY_PUBDATE + " INTEGER," + KEY_MEDIA + " INTEGER," + KEY_FEED + KEY_MEDIA + " INTEGER," + KEY_FEED + " INTEGER," + KEY_READ
+ " INTEGER," + KEY_READ + " INTEGER," + " INTEGER," + KEY_PAYMENT_LINK + " TEXT)";
+ KEY_PAYMENT_LINK + " TEXT)";
private static final String CREATE_TABLE_FEED_CATEGORIES = "CREATE TABLE " private static final String CREATE_TABLE_FEED_CATEGORIES = "CREATE TABLE "
+ TABLE_NAME_FEED_CATEGORIES + " (" + TABLE_PRIMARY_KEY + KEY_NAME + TABLE_NAME_FEED_CATEGORIES + " (" + TABLE_PRIMARY_KEY + KEY_NAME
@ -105,8 +107,11 @@ public class PodDBAdapter {
private static final String CREATE_TABLE_QUEUE = "CREATE TABLE " private static final String CREATE_TABLE_QUEUE = "CREATE TABLE "
+ TABLE_NAME_QUEUE + "(" + KEY_ID + " INTEGER PRIMARY KEY," + TABLE_NAME_QUEUE + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_FEEDITEM + " INTEGER," + KEY_FEEDITEM + " INTEGER," + KEY_FEED + " INTEGER)";
+ KEY_FEED + " INTEGER)";
private static final String CREATE_TABLE_SIMPLECHAPTERS = "CREATE TABLE "
+ TABLE_NAME_SIMPLECHAPTERS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE
+ " TEXT," + KEY_START + " INTEGER," + KEY_FEEDITEM + " INTEGER)";
/** /**
* Used for storing download status entries to determine the type of the * Used for storing download status entries to determine the type of the
@ -283,10 +288,29 @@ public class PodDBAdapter {
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?",
new String[] { String.valueOf(item.getId()) }); new String[] { String.valueOf(item.getId()) });
} }
if (item.getSimpleChapters() != null) {
setSimpleChapters(item);
}
close(); close();
return item.getId(); return item.getId();
} }
public void setSimpleChapters(FeedItem item) {
ContentValues values = new ContentValues();
for (SimpleChapter chapter : item.getSimpleChapters()) {
values.put(KEY_TITLE, chapter.getTitle());
values.put(KEY_START, chapter.getStart());
values.put(KEY_FEEDITEM, item.getId());
if (chapter.getId() == 0) {
chapter.setId(db
.insert(TABLE_NAME_SIMPLECHAPTERS, null, values));
} else {
db.update(TABLE_NAME_SIMPLECHAPTERS, values, KEY_ID + "=?",
new String[] { String.valueOf(chapter.getId()) });
}
}
}
/** /**
* Inserts or updates a download status. * Inserts or updates a download status.
* */ * */
@ -324,20 +348,23 @@ public class PodDBAdapter {
values.put(KEY_ID, i); values.put(KEY_ID, i);
values.put(KEY_FEEDITEM, item.getId()); values.put(KEY_FEEDITEM, item.getId());
values.put(KEY_FEED, item.getFeed().getId()); values.put(KEY_FEED, item.getFeed().getId());
db.insertWithOnConflict(TABLE_NAME_QUEUE, null, values, SQLiteDatabase.CONFLICT_REPLACE); db.insertWithOnConflict(TABLE_NAME_QUEUE, null, values,
SQLiteDatabase.CONFLICT_REPLACE);
} }
close(); close();
} }
public void removeFeedMedia(FeedMedia media) { public void removeFeedMedia(FeedMedia media) {
open(); open();
db.delete(TABLE_NAME_FEED_MEDIA, KEY_ID + "=?", new String[] {String.valueOf(media.getId())}); db.delete(TABLE_NAME_FEED_MEDIA, KEY_ID + "=?",
new String[] { String.valueOf(media.getId()) });
close(); close();
} }
public void removeFeedImage(FeedImage image) { public void removeFeedImage(FeedImage image) {
open(); open();
db.delete(TABLE_NAME_FEED_IMAGES, KEY_ID + "=?", new String[] {String.valueOf(image.getId())}); db.delete(TABLE_NAME_FEED_IMAGES, KEY_ID + "=?",
new String[] { String.valueOf(image.getId()) });
close(); close();
} }
@ -347,7 +374,8 @@ public class PodDBAdapter {
removeFeedMedia(item.getMedia()); removeFeedMedia(item.getMedia());
} }
open(); open();
db.delete(TABLE_NAME_FEED_ITEMS, KEY_ID + "=?", new String[] {String.valueOf(item.getId())}); db.delete(TABLE_NAME_FEED_ITEMS, KEY_ID + "=?",
new String[] { String.valueOf(item.getId()) });
close(); close();
} }
@ -360,7 +388,8 @@ public class PodDBAdapter {
removeFeedItem(item); removeFeedItem(item);
} }
open(); open();
db.delete(TABLE_NAME_FEEDS, KEY_ID + "=?", new String[] {String.valueOf(feed.getId())}); db.delete(TABLE_NAME_FEEDS, KEY_ID + "=?",
new String[] { String.valueOf(feed.getId()) });
close(); close();
} }
@ -440,6 +469,15 @@ public class PodDBAdapter {
return c; return c;
} }
public final Cursor getSimpleChaptersOfFeedItemCursor(final FeedItem item) {
open();
Cursor c = db
.query(CREATE_TABLE_SIMPLECHAPTERS, null, KEY_ID + "=?",
new String[] { String.valueOf(item.getId()) }, null,
null, null);
return c;
}
public final Cursor getDownloadLogCursor() { public final Cursor getDownloadLogCursor() {
open(); open();
Cursor c = db.query(TABLE_NAME_DOWNLOAD_LOG, null, null, null, null, Cursor c = db.query(TABLE_NAME_DOWNLOAD_LOG, null, null, null, null,
@ -449,7 +487,8 @@ public class PodDBAdapter {
public final Cursor getQueueCursor() { public final Cursor getQueueCursor() {
open(); open();
Cursor c = db.query(TABLE_NAME_QUEUE, null, null, null, null, null, null); Cursor c = db.query(TABLE_NAME_QUEUE, null, null, null, null, null,
null);
return c; return c;
} }
@ -531,6 +570,7 @@ public class PodDBAdapter {
db.execSQL(CREATE_TABLE_FEED_MEDIA); db.execSQL(CREATE_TABLE_FEED_MEDIA);
db.execSQL(CREATE_TABLE_DOWNLOAD_LOG); db.execSQL(CREATE_TABLE_DOWNLOAD_LOG);
db.execSQL(CREATE_TABLE_QUEUE); db.execSQL(CREATE_TABLE_QUEUE);
db.execSQL(CREATE_TABLE_SIMPLECHAPTERS);
} }
@Override @Override

View File

@ -13,6 +13,7 @@ import de.podfetcher.syndication.namespace.atom.NSAtom;
import de.podfetcher.syndication.namespace.content.NSContent; import de.podfetcher.syndication.namespace.content.NSContent;
import de.podfetcher.syndication.namespace.itunes.NSITunes; import de.podfetcher.syndication.namespace.itunes.NSITunes;
import de.podfetcher.syndication.namespace.rss20.NSRSS20; import de.podfetcher.syndication.namespace.rss20.NSRSS20;
import de.podfetcher.syndication.namespace.simplechapters.NSSimpleChapters;
/** Superclass for all SAX Handlers which process Syndication formats */ /** Superclass for all SAX Handlers which process Syndication formats */
public class SyndHandler extends DefaultHandler { public class SyndHandler extends DefaultHandler {
@ -89,6 +90,9 @@ public class SyndHandler extends DefaultHandler {
} else if (uri.equals(NSITunes.NSURI) && prefix.equals(NSITunes.NSTAG)) { } else if (uri.equals(NSITunes.NSURI) && prefix.equals(NSITunes.NSTAG)) {
state.namespaces.put(uri, new NSITunes()); state.namespaces.put(uri, new NSITunes());
Log.d(TAG, "Recognized ITunes namespace"); Log.d(TAG, "Recognized ITunes namespace");
} else if (uri.equals(NSSimpleChapters.NSURI) && prefix.equals(NSSimpleChapters.NSTAG)) {
state.namespaces.put(uri, new NSSimpleChapters());
Log.d(TAG, "Recognized SimpleChapters namespace");
} }
} }

View File

@ -80,14 +80,12 @@ public class NSAtom extends Namespace {
new FeedMedia(state.getCurrentItem(), download_url, new FeedMedia(state.getCurrentItem(), download_url,
size, type)); size, type));
} else if (rel.equals(LINK_REL_PAYMENT)) { } else if (rel.equals(LINK_REL_PAYMENT)) {
Log.d(TAG, "Found payment item link");
state.getCurrentItem().setPaymentLink(href); state.getCurrentItem().setPaymentLink(href);
} }
} else if (parent.getName().matches(isFeed)) { } else if (parent.getName().matches(isFeed)) {
if (rel == null || rel.equals(LINK_REL_ALTERNATE)) { if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
state.getFeed().setLink(href); state.getFeed().setLink(href);
} else if (rel.equals(LINK_REL_PAYMENT)) { } else if (rel.equals(LINK_REL_PAYMENT)) {
Log.d(TAG, "Found payment link");
state.getFeed().setPaymentLink(href); state.getFeed().setPaymentLink(href);
} }
} }

View File

@ -0,0 +1,43 @@
package de.podfetcher.syndication.namespace.simplechapters;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import de.podfetcher.feed.SimpleChapter;
import de.podfetcher.syndication.handler.HandlerState;
import de.podfetcher.syndication.namespace.Namespace;
import de.podfetcher.syndication.namespace.SyndElement;
import de.podfetcher.syndication.util.SyndDateUtils;
public class NSSimpleChapters extends Namespace {
public static final String NSTAG = "sc";
public static final String NSURI = "http://podlove.org/simple-chapters";
public static final String CHAPTERS = "chapters";
public static final String CHAPTER = "chapter";
public static final String START = "start";
public static final String TITLE = "title";
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
if (localName.equals(CHAPTERS)) {
state.getCurrentItem().setSimpleChapters(
new ArrayList<SimpleChapter>());
} else if (localName.equals(CHAPTER)) {
state.getCurrentItem()
.getSimpleChapters()
.add(new SimpleChapter(SyndDateUtils
.parseTimeString(attributes.getValue(START)),
attributes.getValue(TITLE)));
}
return new SyndElement(localName, this);
}
@Override
public void handleElementEnd(String localName, HandlerState state) {
}
}

View File

@ -84,4 +84,19 @@ public class SyndDateUtils {
return result; return result;
} }
/** Takes a string of the form [HH:]MM:SS[.mmm] and converts it to milliseconds. */
public static long parseTimeString(final String time) {
String[] parts = time.split(":");
long result = 0;
int idx = 0;
if (parts.length == 3) {
// string has hours
result += Integer.valueOf(parts[idx]) * 3600000;
idx++;
}
result += Integer.valueOf(parts[idx]) * 60000;
idx++;
result += ( Float.valueOf(parts[idx])) * 1000;
return result;
}
} }