Search in sources :

Example 11 with Chapter

use of de.danoeh.antennapod.model.feed.Chapter in project AntennaPod by AntennaPod.

the class ChapterUtils method loadChapters.

public static void loadChapters(Playable playable, Context context) {
    if (playable.getChapters() != null) {
        // Already loaded
        return;
    }
    List<Chapter> chaptersFromDatabase = null;
    if (playable instanceof FeedMedia) {
        FeedMedia feedMedia = (FeedMedia) playable;
        if (feedMedia.getItem() == null) {
            feedMedia.setItem(DBReader.getFeedItem(feedMedia.getItemId()));
        }
        if (feedMedia.getItem().hasChapters()) {
            chaptersFromDatabase = DBReader.loadChaptersOfFeedItem(feedMedia.getItem());
        }
    }
    List<Chapter> chaptersFromMediaFile = ChapterUtils.loadChaptersFromMediaFile(playable, context);
    List<Chapter> chapters = ChapterMerger.merge(chaptersFromDatabase, chaptersFromMediaFile);
    if (chapters == null) {
        // Do not try loading again. There are no chapters.
        playable.setChapters(Collections.emptyList());
    } else {
        playable.setChapters(chapters);
    }
}
Also used : FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) Chapter(de.danoeh.antennapod.model.feed.Chapter)

Example 12 with Chapter

use of de.danoeh.antennapod.model.feed.Chapter in project AntennaPod by AntennaPod.

the class ChapterUtils method readId3ChaptersFrom.

@NonNull
private static List<Chapter> readId3ChaptersFrom(CountingInputStream in) throws IOException, ID3ReaderException {
    ChapterReader reader = new ChapterReader(in);
    reader.readInputStream();
    List<Chapter> chapters = reader.getChapters();
    Collections.sort(chapters, new ChapterStartTimeComparator());
    enumerateEmptyChapterTitles(chapters);
    if (!chaptersValid(chapters)) {
        Log.e(TAG, "Chapter data was invalid");
        return Collections.emptyList();
    }
    return chapters;
}
Also used : ChapterStartTimeComparator(de.danoeh.antennapod.core.util.comparator.ChapterStartTimeComparator) ChapterReader(de.danoeh.antennapod.parser.media.id3.ChapterReader) VorbisCommentChapterReader(de.danoeh.antennapod.parser.media.vorbis.VorbisCommentChapterReader) Chapter(de.danoeh.antennapod.model.feed.Chapter) NonNull(androidx.annotation.NonNull)

Example 13 with Chapter

use of de.danoeh.antennapod.model.feed.Chapter in project AntennaPod by AntennaPod.

the class DbTestUtils method saveFeedlist.

/**
 * Use this method when tests involve chapters.
 */
public static List<Feed> saveFeedlist(int numFeeds, int numItems, boolean withMedia, boolean withChapters, int numChapters) {
    if (numFeeds <= 0) {
        throw new IllegalArgumentException("numFeeds<=0");
    }
    if (numItems < 0) {
        throw new IllegalArgumentException("numItems<0");
    }
    List<Feed> feeds = new ArrayList<>();
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    for (int i = 0; i < numFeeds; i++) {
        Feed f = new Feed(0, null, "feed " + i, "link" + i, "descr", null, null, null, null, "id" + i, null, null, "url" + i, false);
        f.setItems(new ArrayList<>());
        for (int j = 0; j < numItems; j++) {
            FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(), FeedItem.PLAYED, f, withChapters);
            if (withMedia) {
                FeedMedia media = new FeedMedia(item, "url" + j, 1, "audio/mp3");
                item.setMedia(media);
            }
            if (withChapters) {
                List<Chapter> chapters = new ArrayList<>();
                item.setChapters(chapters);
                for (int k = 0; k < numChapters; k++) {
                    chapters.add(new SimpleChapter(k, "item " + j + " chapter " + k, "http://example.com", "http://example.com/image.png"));
                }
            }
            f.getItems().add(item);
        }
        Collections.sort(f.getItems(), new FeedItemPubdateComparator());
        adapter.setCompleteFeed(f);
        assertTrue(f.getId() != 0);
        for (FeedItem item : f.getItems()) {
            assertTrue(item.getId() != 0);
        }
        feeds.add(f);
    }
    adapter.close();
    return feeds;
}
Also used : ArrayList(java.util.ArrayList) SimpleChapter(de.danoeh.antennapod.parser.feed.element.SimpleChapter) Chapter(de.danoeh.antennapod.model.feed.Chapter) Date(java.util.Date) SimpleChapter(de.danoeh.antennapod.parser.feed.element.SimpleChapter) FeedItemPubdateComparator(de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator) FeedItem(de.danoeh.antennapod.model.feed.FeedItem) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) Feed(de.danoeh.antennapod.model.feed.Feed)

Example 14 with Chapter

use of de.danoeh.antennapod.model.feed.Chapter in project AntennaPod by AntennaPod.

the class ChapterReaderTest method testRealFileAuphonic.

@Test
public void testRealFileAuphonic() throws IOException, ID3ReaderException {
    CountingInputStream inputStream = new CountingInputStream(getClass().getClassLoader().getResource("auphonic.mp3").openStream());
    ChapterReader reader = new ChapterReader(inputStream);
    reader.readInputStream();
    List<Chapter> chapters = reader.getChapters();
    assertEquals(4, chapters.size());
    assertEquals(0, chapters.get(0).getStart());
    assertEquals(3000, chapters.get(1).getStart());
    assertEquals(6000, chapters.get(2).getStart());
    assertEquals(9000, chapters.get(3).getStart());
    assertEquals("Chapter 1 - ❤️😊", chapters.get(0).getTitle());
    assertEquals("Chapter 2 - ßöÄ", chapters.get(1).getTitle());
    assertEquals("Chapter 3 - 爱", chapters.get(2).getTitle());
    assertEquals("Chapter 4", chapters.get(3).getTitle());
    assertEquals("https://example.com", chapters.get(0).getLink());
    assertEquals("https://example.com", chapters.get(1).getLink());
    assertEquals("https://example.com", chapters.get(2).getLink());
    assertEquals("https://example.com", chapters.get(3).getLink());
    assertEquals(EmbeddedChapterImage.makeUrl(765, 308), chapters.get(0).getImageUrl());
    assertEquals(EmbeddedChapterImage.makeUrl(1271, 308), chapters.get(1).getImageUrl());
    assertEquals(EmbeddedChapterImage.makeUrl(1771, 308), chapters.get(2).getImageUrl());
    assertEquals(EmbeddedChapterImage.makeUrl(2259, 308), chapters.get(3).getImageUrl());
}
Also used : CountingInputStream(org.apache.commons.io.input.CountingInputStream) Chapter(de.danoeh.antennapod.model.feed.Chapter) Test(org.junit.Test)

Example 15 with Chapter

use of de.danoeh.antennapod.model.feed.Chapter in project AntennaPod by AntennaPod.

the class ChapterReaderTest method testReadTitleWithGarbage.

@Test
public void testReadTitleWithGarbage() throws IOException, ID3ReaderException {
    byte[] titleSubframeContent = { ID3Reader.ENCODING_ISO, // Title
    'A', // Null-terminated
    0, // Garbage, should be ignored
    42, // Garbage, should be ignored
    42, // Garbage, should be ignored
    42, // Garbage, should be ignored
    42 };
    FrameHeader header = new FrameHeader(ChapterReader.FRAME_ID_TITLE, titleSubframeContent.length, (short) 0);
    CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(titleSubframeContent));
    ChapterReader reader = new ChapterReader(inputStream);
    Chapter chapter = new ID3Chapter("", 0);
    reader.readChapterSubFrame(header, chapter);
    assertEquals("A", chapter.getTitle());
    // Should skip the garbage and point to the next frame
    assertEquals(titleSubframeContent.length, reader.getPosition());
}
Also used : FrameHeader(de.danoeh.antennapod.parser.media.id3.model.FrameHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) CountingInputStream(org.apache.commons.io.input.CountingInputStream) Chapter(de.danoeh.antennapod.model.feed.Chapter) Test(org.junit.Test)

Aggregations

Chapter (de.danoeh.antennapod.model.feed.Chapter)20 CountingInputStream (org.apache.commons.io.input.CountingInputStream)7 Test (org.junit.Test)7 NonNull (androidx.annotation.NonNull)4 FrameHeader (de.danoeh.antennapod.parser.media.id3.model.FrameHeader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ChapterStartTimeComparator (de.danoeh.antennapod.core.util.comparator.ChapterStartTimeComparator)2 FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)2 SimpleChapter (de.danoeh.antennapod.parser.feed.element.SimpleChapter)2 VorbisCommentChapterReader (de.danoeh.antennapod.parser.media.vorbis.VorbisCommentChapterReader)2 Dialog (android.app.Dialog)1 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 Bundle (android.os.Bundle)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 ProgressBar (android.widget.ProgressBar)1 RelativeLayout (android.widget.RelativeLayout)1