Initial database support for chapter images

This commit is contained in:
ByteHamster 2020-02-07 00:41:44 +01:00
parent cf5a876dc4
commit 5caaaa9e5a
9 changed files with 194 additions and 217 deletions

View File

@ -58,7 +58,8 @@ class DBTestUtils {
List<Chapter> chapters = new ArrayList<>();
item.setChapters(chapters);
for (int k = 0; k < numChapters; k++) {
chapters.add(new SimpleChapter(k, "item " + j + " chapter " + k, item, "http://example.com"));
chapters.add(new SimpleChapter(k, "item " + j + " chapter " + k,
"http://example.com", "http://example.com/image.png"));
}
}
f.getItems().add(item);

View File

@ -2,14 +2,18 @@ package de.danoeh.antennapod.core.feed;
import android.database.Cursor;
import android.text.TextUtils;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import java.util.Objects;
public abstract class Chapter extends FeedComponent {
/** Defines starting point in milliseconds. */
long start;
String title;
String link;
String imageUrl;
Chapter() {
}
@ -19,43 +23,45 @@ public abstract class Chapter extends FeedComponent {
this.start = start;
}
Chapter(long start, String title, FeedItem item, String link) {
Chapter(long start, String title, String link, String imageUrl) {
super();
this.start = start;
this.title = title;
this.link = link;
this.imageUrl = imageUrl;
}
public static Chapter fromCursor(Cursor cursor, FeedItem item) {
public static Chapter fromCursor(Cursor cursor) {
int indexId = cursor.getColumnIndex(PodDBAdapter.KEY_ID);
int indexTitle = cursor.getColumnIndex(PodDBAdapter.KEY_TITLE);
int indexStart = cursor.getColumnIndex(PodDBAdapter.KEY_START);
int indexLink = cursor.getColumnIndex(PodDBAdapter.KEY_LINK);
int indexImage = cursor.getColumnIndex(PodDBAdapter.KEY_IMAGE_URL);
int indexChapterType = cursor.getColumnIndex(PodDBAdapter.KEY_CHAPTER_TYPE);
long id = cursor.getLong(indexId);
String title = cursor.getString(indexTitle);
long start = cursor.getLong(indexStart);
String link = cursor.getString(indexLink);
String imageUrl = cursor.getString(indexImage);
int chapterType = cursor.getInt(indexChapterType);
Chapter chapter = null;
switch (chapterType) {
case SimpleChapter.CHAPTERTYPE_SIMPLECHAPTER:
chapter = new SimpleChapter(start, title, item, link);
chapter = new SimpleChapter(start, title, link, imageUrl);
break;
case ID3Chapter.CHAPTERTYPE_ID3CHAPTER:
chapter = new ID3Chapter(start, title, item, link);
chapter = new ID3Chapter(start, title, link, imageUrl);
break;
case VorbisCommentChapter.CHAPTERTYPE_VORBISCOMMENT_CHAPTER:
chapter = new VorbisCommentChapter(start, title, item, link);
chapter = new VorbisCommentChapter(start, title, link, imageUrl);
break;
}
chapter.setId(id);
return chapter;
}
public abstract int getChapterType();
public long getStart() {
@ -82,6 +88,14 @@ public abstract class Chapter extends FeedComponent {
this.link = link;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
@Override
public String getHumanReadableIdentifier() {
return title;

View File

@ -14,8 +14,8 @@ public class ID3Chapter extends Chapter {
this.id3ID = id3ID;
}
public ID3Chapter(long start, String title, FeedItem item, String link) {
super(start, title, item, link);
public ID3Chapter(long start, String title, String link, String imageUrl) {
super(start, title, link, imageUrl);
}
@Override

View File

@ -3,23 +3,12 @@ package de.danoeh.antennapod.core.feed;
public class SimpleChapter extends Chapter {
public static final int CHAPTERTYPE_SIMPLECHAPTER = 0;
public SimpleChapter(long start, String title, FeedItem item, String link) {
super(start, title, item, link);
public SimpleChapter(long start, String title, String link, String imageUrl) {
super(start, title, link, imageUrl);
}
@Override
public int getChapterType() {
return CHAPTERTYPE_SIMPLECHAPTER;
}
public void updateFromOther(SimpleChapter other) {
super.updateFromOther(other);
start = other.start;
if (other.title != null) {
title = other.title;
}
if (other.link != null) {
link = other.link;
}
}
}

View File

@ -15,9 +15,8 @@ public class VorbisCommentChapter extends Chapter {
this.vorbisCommentId = vorbisCommentId;
}
public VorbisCommentChapter(long start, String title, FeedItem item,
String link) {
super(start, title, item, link);
public VorbisCommentChapter(long start, String title, String link, String imageUrl) {
super(start, title, link, imageUrl);
}
@Override
@ -55,8 +54,7 @@ public class VorbisCommentChapter extends Chapter {
* @return the id of the chapter key or -1 if the id couldn't be read.
* @throws VorbisCommentReaderException
* */
public static int getIDFromKey(String key)
throws VorbisCommentReaderException {
public static int getIDFromKey(String key) throws VorbisCommentReaderException {
if (key.length() >= CHAPTERXXX_LENGTH) { // >= CHAPTERxxx
try {
String strId = key.substring(8, 10);
@ -74,7 +72,7 @@ public class VorbisCommentChapter extends Chapter {
*/
public static String getAttributeTypeFromKey(String key) {
if (key.length() > CHAPTERXXX_LENGTH) {
return key.substring(CHAPTERXXX_LENGTH, key.length());
return key.substring(CHAPTERXXX_LENGTH);
}
return null;
}
@ -84,26 +82,7 @@ public class VorbisCommentChapter extends Chapter {
return CHAPTERTYPE_VORBISCOMMENT_CHAPTER;
}
public void setTitle(String title) {
this.title = title;
}
public void setLink(String link) {
this.link = link;
}
public void setStart(long start) {
this.start = start;
}
public int getVorbisCommentId() {
return vorbisCommentId;
}
public void setVorbisCommentId(int vorbisCommentId) {
this.vorbisCommentId = vorbisCommentId;
}
}

View File

@ -795,9 +795,7 @@ public final class DBReader {
}
private static void loadChaptersOfFeedItem(PodDBAdapter adapter, FeedItem item) {
Cursor cursor = null;
try {
cursor = adapter.getSimpleChaptersOfFeedItemCursor(item);
try (Cursor cursor = adapter.getSimpleChaptersOfFeedItemCursor(item)) {
int chaptersCount = cursor.getCount();
if (chaptersCount == 0) {
item.setChapters(null);
@ -805,14 +803,7 @@ public final class DBReader {
}
item.setChapters(new ArrayList<>(chaptersCount));
while (cursor.moveToNext()) {
Chapter chapter = Chapter.fromCursor(cursor, item);
if (chapter != null) {
item.getChapters().add(chapter);
}
}
} finally {
if (cursor != null) {
cursor.close();
item.getChapters().add(Chapter.fromCursor(cursor));
}
}
}

View File

@ -95,7 +95,7 @@ class DBUpgrader {
+ " ADD COLUMN " + PodDBAdapter.KEY_PASSWORD
+ " TEXT");
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEED_ITEMS
+ " ADD COLUMN " + PodDBAdapter.KEY_IMAGE
+ " ADD COLUMN image"
+ " INTEGER");
}
if (oldVersion <= 12) {
@ -280,13 +280,13 @@ class DBUpgrader {
+ " SELECT " + PodDBAdapter.KEY_DOWNLOAD_URL
+ " FROM " + PodDBAdapter.TABLE_NAME_FEED_IMAGES
+ " WHERE " + PodDBAdapter.TABLE_NAME_FEED_IMAGES + "." + PodDBAdapter.KEY_ID
+ " = " + PodDBAdapter.TABLE_NAME_FEED_ITEMS + "." + PodDBAdapter.KEY_IMAGE + ")");
+ " = " + PodDBAdapter.TABLE_NAME_FEED_ITEMS + ".image)");
db.execSQL("UPDATE " + PodDBAdapter.TABLE_NAME_FEEDS + " SET " + PodDBAdapter.KEY_IMAGE_URL + " = ("
+ " SELECT " + PodDBAdapter.KEY_DOWNLOAD_URL
+ " FROM " + PodDBAdapter.TABLE_NAME_FEED_IMAGES
+ " WHERE " + PodDBAdapter.TABLE_NAME_FEED_IMAGES + "." + PodDBAdapter.KEY_ID
+ " = " + PodDBAdapter.TABLE_NAME_FEEDS + "." + PodDBAdapter.KEY_IMAGE + ")");
+ " = " + PodDBAdapter.TABLE_NAME_FEEDS + ".image)");
db.execSQL("DROP TABLE " + PodDBAdapter.TABLE_NAME_FEED_IMAGES);
}
@ -301,6 +301,8 @@ class DBUpgrader {
if (oldVersion < 1090000) {
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS
+ " ADD COLUMN " + PodDBAdapter.KEY_FEED_VOLUME_ADAPTION + " INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_SIMPLECHAPTERS
+ " ADD COLUMN " + PodDBAdapter.KEY_IMAGE_URL + " TEXT DEFAULT NULL");
}
}

View File

@ -69,7 +69,6 @@ public class PodDBAdapter {
public static final String KEY_POSITION = "position";
public static final String KEY_SIZE = "filesize";
public static final String KEY_MIME_TYPE = "mime_type";
public static final String KEY_IMAGE = "image";
public static final String KEY_IMAGE_URL = "image_url";
public static final String KEY_FEED = "feed";
public static final String KEY_MEDIA = "media";
@ -183,7 +182,7 @@ public class PodDBAdapter {
private static final String CREATE_TABLE_SIMPLECHAPTERS = "CREATE TABLE "
+ TABLE_NAME_SIMPLECHAPTERS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE
+ " TEXT," + KEY_START + " INTEGER," + KEY_FEEDITEM + " INTEGER,"
+ KEY_LINK + " TEXT," + KEY_CHAPTER_TYPE + " INTEGER)";
+ KEY_LINK + " TEXT," + KEY_IMAGE_URL + " TEXT," + KEY_CHAPTER_TYPE + " INTEGER)";
// SQL Statements for creating indexes
static final String CREATE_INDEX_FEEDITEMS_FEED = "CREATE INDEX "
@ -674,6 +673,7 @@ public class PodDBAdapter {
values.put(KEY_START, chapter.getStart());
values.put(KEY_FEEDITEM, item.getId());
values.put(KEY_LINK, chapter.getLink());
values.put(KEY_IMAGE_URL, chapter.getImageUrl());
values.put(KEY_CHAPTER_TYPE, chapter.getChapterType());
if (chapter.getId() == 0) {
chapter.setId(db.insert(TABLE_NAME_SIMPLECHAPTERS, null, values));

View File

@ -22,12 +22,12 @@ public class NSSimpleChapters extends Namespace {
private static final String START = "start";
private static final String TITLE = "title";
private static final String HREF = "href";
private static final String IMAGE = "image";
@Override
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) {
FeedItem currentItem = state.getCurrentItem();
if(currentItem != null) {
if (currentItem != null) {
if (localName.equals(CHAPTERS)) {
currentItem.setChapters(new ArrayList<>());
} else if (localName.equals(CHAPTER)) {
@ -35,7 +35,8 @@ public class NSSimpleChapters extends Namespace {
long start = DateUtils.parseTimeString(attributes.getValue(START));
String title = attributes.getValue(TITLE);
String link = attributes.getValue(HREF);
SimpleChapter chapter = new SimpleChapter(start, title, currentItem, link);
String imageUrl = attributes.getValue(IMAGE);
SimpleChapter chapter = new SimpleChapter(start, title, link, imageUrl);
currentItem.getChapters().add(chapter);
} catch (NumberFormatException e) {
Log.e(TAG, "Unable to read chapter", e);