diff options
author | ByteHamster <info@bytehamster.com> | 2020-07-13 00:21:20 +0200 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2020-07-13 00:21:33 +0200 |
commit | 9cd1c9490600402276e7216ee009b91d5b4ec485 (patch) | |
tree | 3c12c75baea926e0295562c2db59c7f882d93bdf /core | |
parent | d90b2b37bce50dae741841f68971d1637e12f5fa (diff) | |
download | AntennaPod-9cd1c9490600402276e7216ee009b91d5b4ec485.zip |
Load embedded covers of local feed items
Diffstat (limited to 'core')
7 files changed, 79 insertions, 22 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java index 20ed402fc..892592a4b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItem.java @@ -378,7 +378,7 @@ public class FeedItem extends FeedComponent implements ShownotesProvider, ImageR if (imageUrl != null) { return imageUrl; } else if (media != null && media.hasEmbeddedPicture()) { - return media.getLocalMediaUrl(); + return FeedMedia.FILENAME_PREFIX_EMBEDDED_COVER + media.getLocalMediaUrl(); } else if (feed != null) { return feed.getImageLocation(); } else { diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java index df1de201b..083e8c500 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java @@ -32,6 +32,7 @@ public class FeedMedia extends FeedFile implements Playable { public static final int FEEDFILETYPE_FEEDMEDIA = 2; public static final int PLAYABLE_TYPE_FEEDMEDIA = 1; + public static final String FILENAME_PREFIX_EMBEDDED_COVER = "metadata-retriever:"; public static final String PREF_MEDIA_ID = "FeedMedia.PrefMediaId"; private static final String PREF_FEED_ID = "FeedMedia.PrefFeedId"; @@ -557,7 +558,7 @@ public class FeedMedia extends FeedFile implements Playable { if (item != null) { return item.getImageLocation(); } else if (hasEmbeddedPicture()) { - return getLocalMediaUrl(); + return FILENAME_PREFIX_EMBEDDED_COVER + getLocalMediaUrl(); } else { return null; } diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java b/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java index 2d58b7b52..f9863b0a6 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/LocalFeedUpdater.java @@ -118,6 +118,7 @@ public class LocalFeedUpdater { long size = file.length(); FeedMedia media = new FeedMedia(0, item, (int) duration, 0, size, file.getType(), file.getUri().toString(), file.getUri().toString(), false, null, 0, 0); + media.setHasEmbeddedPicture(mediaMetadataRetriever.getEmbeddedPicture() != null); item.setMedia(media); return item; diff --git a/core/src/main/java/de/danoeh/antennapod/core/glide/ApGlideModule.java b/core/src/main/java/de/danoeh/antennapod/core/glide/ApGlideModule.java index 3ffa92df0..ab4247cef 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/glide/ApGlideModule.java +++ b/core/src/main/java/de/danoeh/antennapod/core/glide/ApGlideModule.java @@ -35,8 +35,10 @@ public class ApGlideModule extends AppGlideModule { @Override public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { - registry.replace(String.class, InputStream.class, new ApOkHttpUrlLoader.Factory()); - registry.append(EmbeddedChapterImage.class, ByteBuffer.class, new ChapterImageModelLoader.Factory()); + registry.replace(String.class, InputStream.class, new MetadataRetrieverLoader.Factory(context)); + registry.append(String.class, InputStream.class, new ApOkHttpUrlLoader.Factory()); registry.append(String.class, InputStream.class, new StringLoader.StreamFactory()); + + registry.append(EmbeddedChapterImage.class, ByteBuffer.class, new ChapterImageModelLoader.Factory()); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java b/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java index 8c9155bc9..963ce7596 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/glide/ApOkHttpUrlLoader.java @@ -53,14 +53,7 @@ class ApOkHttpUrlLoader implements ModelLoader<String, InputStream> { * Constructor for a new Factory that runs requests using a static singleton client. */ Factory() { - this(getInternalClient()); - } - - /** - * Constructor for a new Factory that runs requests using given client. - */ - Factory(OkHttpClient client) { - this.client = client; + this.client = getInternalClient(); } @NonNull @@ -84,14 +77,7 @@ class ApOkHttpUrlLoader implements ModelLoader<String, InputStream> { @Nullable @Override public LoadData<InputStream> buildLoadData(@NonNull String model, int width, int height, @NonNull Options options) { - if (TextUtils.isEmpty(model)) { - return null; - } else if (model.startsWith("/")) { - return new LoadData<>(new ObjectKey(model), new AudioCoverFetcher(model)); - } else { - GlideUrl url = new GlideUrl(model); - return new LoadData<>(new ObjectKey(model), new OkHttpStreamFetcher(client, url)); - } + return new LoadData<>(new ObjectKey(model), new OkHttpStreamFetcher(client, new GlideUrl(model))); } @Override diff --git a/core/src/main/java/de/danoeh/antennapod/core/glide/AudioCoverFetcher.java b/core/src/main/java/de/danoeh/antennapod/core/glide/AudioCoverFetcher.java index 6a237573b..b6b607904 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/glide/AudioCoverFetcher.java +++ b/core/src/main/java/de/danoeh/antennapod/core/glide/AudioCoverFetcher.java @@ -1,7 +1,10 @@ package de.danoeh.antennapod.core.glide; +import android.content.ContentResolver; +import android.content.Context; import android.media.MediaMetadataRetriever; +import android.net.Uri; import androidx.annotation.NonNull; import com.bumptech.glide.Priority; import com.bumptech.glide.load.DataSource; @@ -17,16 +20,22 @@ class AudioCoverFetcher implements DataFetcher<InputStream> { private static final String TAG = "AudioCoverFetcher"; private final String path; + private final Context context; - public AudioCoverFetcher(String path) { + public AudioCoverFetcher(String path, Context context) { this.path = path; + this.context = context; } @Override public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super InputStream> callback) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { - retriever.setDataSource(path); + if (path.startsWith(ContentResolver.SCHEME_CONTENT)) { + retriever.setDataSource(context, Uri.parse(path)); + } else { + retriever.setDataSource(path); + } byte[] picture = retriever.getEmbeddedPicture(); if (picture != null) { callback.onDataReady(new ByteArrayInputStream(picture)); @@ -41,6 +50,7 @@ class AudioCoverFetcher implements DataFetcher<InputStream> { @Override public void cleanup() { // nothing to clean up } + @Override public void cancel() { // cannot cancel } diff --git a/core/src/main/java/de/danoeh/antennapod/core/glide/MetadataRetrieverLoader.java b/core/src/main/java/de/danoeh/antennapod/core/glide/MetadataRetrieverLoader.java new file mode 100644 index 000000000..baa06e722 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/glide/MetadataRetrieverLoader.java @@ -0,0 +1,57 @@ +package de.danoeh.antennapod.core.glide; + +import android.content.Context; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.bumptech.glide.load.Options; +import com.bumptech.glide.load.model.ModelLoader; +import com.bumptech.glide.load.model.ModelLoaderFactory; +import com.bumptech.glide.load.model.MultiModelLoaderFactory; +import com.bumptech.glide.signature.ObjectKey; +import de.danoeh.antennapod.core.feed.FeedMedia; + +import java.io.InputStream; + +class MetadataRetrieverLoader implements ModelLoader<String, InputStream> { + + /** + * The default factory for {@link MetadataRetrieverLoader}s. + */ + public static class Factory implements ModelLoaderFactory<String, InputStream> { + private final Context context; + + Factory(Context context) { + this.context = context; + } + + @NonNull + @Override + public ModelLoader<String, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) { + return new MetadataRetrieverLoader(context); + } + + @Override + public void teardown() { + // Do nothing, this instance doesn't own the client. + } + } + + private final Context context; + + private MetadataRetrieverLoader(Context context) { + this.context = context; + } + + @Nullable + @Override + public LoadData<InputStream> buildLoadData(@NonNull String model, + int width, int height, @NonNull Options options) { + return new LoadData<>(new ObjectKey(model), + new AudioCoverFetcher(model.replace(FeedMedia.FILENAME_PREFIX_EMBEDDED_COVER, ""), context)); + } + + @Override + public boolean handles(@NonNull String model) { + return model.startsWith(FeedMedia.FILENAME_PREFIX_EMBEDDED_COVER); + } +} |