Load chapters of local feed items

This commit is contained in:
ByteHamster 2020-05-25 22:28:42 +02:00
parent 643e970a27
commit 122bed841b
7 changed files with 23 additions and 25 deletions

View File

@ -128,7 +128,7 @@ public class ChaptersFragment extends Fragment {
disposable = Maybe.create(emitter -> { disposable = Maybe.create(emitter -> {
Playable media = controller.getMedia(); Playable media = controller.getMedia();
if (media != null) { if (media != null) {
media.loadChapterMarks(); media.loadChapterMarks(getContext());
emitter.onSuccess(media); emitter.onSuccess(media);
} else { } else {
emitter.onComplete(); emitter.onComplete();

View File

@ -375,7 +375,7 @@ public class FeedMedia extends FeedFile implements Playable {
} }
@Override @Override
public void loadChapterMarks() { public void loadChapterMarks(Context context) {
if (item == null && itemID != 0) { if (item == null && itemID != 0) {
item = DBReader.getFeedItem(itemID); item = DBReader.getFeedItem(itemID);
} }
@ -386,10 +386,10 @@ public class FeedMedia extends FeedFile implements Playable {
if (item.hasChapters()) { if (item.hasChapters()) {
DBReader.loadChaptersOfFeedItem(item); DBReader.loadChaptersOfFeedItem(item);
} else { } else {
if(localFileAvailable()) { if (localFileAvailable()) {
ChapterUtils.loadChaptersFromFileUrl(this); ChapterUtils.loadChaptersFromFileUrl(this);
} else { } else {
ChapterUtils.loadChaptersFromStreamUrl(this); ChapterUtils.loadChaptersFromStreamUrl(this, context);
} }
if (item.getChapters() != null) { if (item.getChapters() != null) {
DBWriter.setFeedItem(item); DBWriter.setFeedItem(item);

View File

@ -311,7 +311,7 @@ public class PlaybackServiceTaskManager {
if (media.getChapters() == null) { if (media.getChapters() == null) {
Completable.create(emitter -> { Completable.create(emitter -> {
media.loadChapterMarks(); media.loadChapterMarks(context);
emitter.onComplete(); emitter.onComplete();
}) })
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())

View File

@ -1,10 +1,10 @@
package de.danoeh.antennapod.core.util; package de.danoeh.antennapod.core.util;
import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.Log; import android.util.Log;
import java.util.zip.CheckedOutputStream;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@ -13,7 +13,6 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -49,10 +48,10 @@ public class ChapterUtils {
return chapters.size() - 1; return chapters.size() - 1;
} }
public static void loadChaptersFromStreamUrl(Playable media) { public static void loadChaptersFromStreamUrl(Playable media, Context context) {
ChapterUtils.readID3ChaptersFromPlayableStreamUrl(media); ChapterUtils.readID3ChaptersFromPlayableStreamUrl(media, context);
if (media.getChapters() == null) { if (media.getChapters() == null) {
ChapterUtils.readOggChaptersFromPlayableStreamUrl(media); ChapterUtils.readOggChaptersFromPlayableStreamUrl(media, context);
} }
} }
@ -71,7 +70,7 @@ public class ChapterUtils {
* Uses the download URL of a media object of a feeditem to read its ID3 * Uses the download URL of a media object of a feeditem to read its ID3
* chapters. * chapters.
*/ */
private static void readID3ChaptersFromPlayableStreamUrl(Playable p) { private static void readID3ChaptersFromPlayableStreamUrl(Playable p, Context context) {
if (p == null || p.getStreamUrl() == null) { if (p == null || p.getStreamUrl() == null) {
Log.e(TAG, "Unable to read ID3 chapters: media or download URL was null"); Log.e(TAG, "Unable to read ID3 chapters: media or download URL was null");
return; return;
@ -79,8 +78,8 @@ public class ChapterUtils {
Log.d(TAG, "Reading id3 chapters from item " + p.getEpisodeTitle()); Log.d(TAG, "Reading id3 chapters from item " + p.getEpisodeTitle());
CountingInputStream in = null; CountingInputStream in = null;
try { try {
URL url = new URL(p.getStreamUrl()); Uri uri = Uri.parse(p.getStreamUrl());
in = new CountingInputStream(url.openStream()); in = new CountingInputStream(context.getContentResolver().openInputStream(uri));
List<Chapter> chapters = readChaptersFrom(in); List<Chapter> chapters = readChaptersFrom(in);
if (!chapters.isEmpty()) { if (!chapters.isEmpty()) {
p.setChapters(chapters); p.setChapters(chapters);
@ -142,14 +141,14 @@ public class ChapterUtils {
return chapters; return chapters;
} }
private static void readOggChaptersFromPlayableStreamUrl(Playable media) { private static void readOggChaptersFromPlayableStreamUrl(Playable media, Context context) {
if (media == null || !media.streamAvailable()) { if (media == null || !media.streamAvailable()) {
return; return;
} }
InputStream input = null; InputStream input = null;
try { try {
URL url = new URL(media.getStreamUrl()); Uri uri = Uri.parse(media.getStreamUrl());
input = url.openStream(); input = context.getContentResolver().openInputStream(uri);
if (input != null) { if (input != null) {
readOggChaptersFromInputStream(media, input); readOggChaptersFromInputStream(media, input);
} }

View File

@ -103,7 +103,7 @@ public class ExternalMedia implements Playable {
} }
@Override @Override
public void loadChapterMarks() { public void loadChapterMarks(Context context) {
} }

View File

@ -4,11 +4,8 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Parcelable; import android.os.Parcelable;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import androidx.annotation.Nullable;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable;
import java.util.List;
import de.danoeh.antennapod.core.asynctask.ImageResource; import de.danoeh.antennapod.core.asynctask.ImageResource;
import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
@ -17,6 +14,8 @@ import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.ShownotesProvider; import de.danoeh.antennapod.core.util.ShownotesProvider;
import java.util.List;
/** /**
* Interface for objects that can be played by the PlaybackService. * Interface for objects that can be played by the PlaybackService.
*/ */
@ -44,7 +43,7 @@ public interface Playable extends Parcelable,
* Playable objects should load their chapter marks in this method if no * Playable objects should load their chapter marks in this method if no
* local file was available when loadMetadata() was called. * local file was available when loadMetadata() was called.
*/ */
void loadChapterMarks(); void loadChapterMarks(Context context);
/** /**
* Returns the title of the episode that this playable represents * Returns the title of the episode that this playable represents

View File

@ -129,8 +129,8 @@ public class RemoteMedia implements Playable {
} }
@Override @Override
public void loadChapterMarks() { public void loadChapterMarks(Context context) {
ChapterUtils.loadChaptersFromStreamUrl(this); ChapterUtils.loadChaptersFromStreamUrl(this, context);
} }
@Override @Override