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 7f064fff3..068669af9 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 @@ -4,7 +4,6 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.database.Cursor; import android.media.MediaMetadataRetriever; -import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.Nullable; @@ -321,12 +320,10 @@ public class FeedMedia extends FeedFile implements Playable { } public boolean hasEmbeddedPicture() { - return false; - // TODO: reenable! - //if(hasEmbeddedPicture == null) { - // checkEmbeddedPicture(); - //} - //return hasEmbeddedPicture; + if(hasEmbeddedPicture == null) { + checkEmbeddedPicture(); + } + return hasEmbeddedPicture; } @Override @@ -514,20 +511,11 @@ public class FeedMedia extends FeedFile implements Playable { }; @Override - public Uri getImageUri() { + public String getImageLocation() { if (hasEmbeddedPicture()) { - Uri.Builder builder = new Uri.Builder(); - builder.scheme(SCHEME_MEDIA).encodedPath(getLocalMediaUrl()); - - if (item != null && item.getFeed() != null) { - final Uri feedImgUri = item.getFeed().getImageUri(); - if (feedImgUri != null) { - builder.appendQueryParameter(PARAM_FALLBACK, feedImgUri.toString()); - } - } - return builder.build(); + return getLocalMediaUrl(); } else if(item != null) { - return item.getImageUri(); + return item.getImageLocation(); } else { return null; } @@ -550,7 +538,7 @@ public class FeedMedia extends FeedFile implements Playable { super.setFile_url(file_url); } - private void checkEmbeddedPicture() { + public void checkEmbeddedPicture() { if (!localFileAvailable()) { hasEmbeddedPicture = Boolean.FALSE; return; 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 0baff9723..835dee735 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 @@ -6,7 +6,6 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.GlideBuilder; import com.bumptech.glide.load.DecodeFormat; import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory; -import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.module.GlideModule; import java.io.InputStream; @@ -27,7 +26,7 @@ public class ApGlideModule implements GlideModule { @Override public void registerComponents(Context context, Glide glide) { - glide.register(GlideUrl.class, InputStream.class, new ApOkHttpUrlLoader.Factory()); + glide.register(String.class, InputStream.class, new ApOkHttpUrlLoader.Factory()); } -} \ No newline at end of file +} 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 513264a05..d67db198d 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 @@ -26,14 +26,14 @@ import de.danoeh.antennapod.core.util.NetworkUtils; /** * @see com.bumptech.glide.integration.okhttp.OkHttpUrlLoader */ -public class ApOkHttpUrlLoader implements ModelLoader { +public class ApOkHttpUrlLoader implements ModelLoader { private static final String TAG = ApOkHttpUrlLoader.class.getSimpleName(); /** * The default factory for {@link ApOkHttpUrlLoader}s. */ - public static class Factory implements ModelLoaderFactory { + public static class Factory implements ModelLoaderFactory { private static volatile OkHttpClient internalClient; private OkHttpClient client; @@ -66,7 +66,7 @@ public class ApOkHttpUrlLoader implements ModelLoader { } @Override - public ModelLoader build(Context context, GenericLoaderFactory factories) { + public ModelLoader build(Context context, GenericLoaderFactory factories) { return new ApOkHttpUrlLoader(client); } @@ -83,8 +83,15 @@ public class ApOkHttpUrlLoader implements ModelLoader { } @Override - public DataFetcher getResourceFetcher(GlideUrl model, int width, int height) { - return new OkHttpStreamFetcher(client, model); + public DataFetcher getResourceFetcher(String model, int width, int height) { + Log.d(TAG, "getResourceFetcher() called with: " + "model = [" + model + "], width = [" + + width + "], height = [" + height + "]"); + if(model.startsWith("/")) { + return new AudioCoverFetcher(model); + } else { + GlideUrl url = new GlideUrl(model); + return new OkHttpStreamFetcher(client, url); + } } private static class NetworkAllowanceInterceptor implements Interceptor { 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 new file mode 100644 index 000000000..48dadc492 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/glide/AudioCoverFetcher.java @@ -0,0 +1,48 @@ +package de.danoeh.antennapod.core.glide; + +import android.media.MediaMetadataRetriever; +import android.util.Log; + +import com.bumptech.glide.Priority; +import com.bumptech.glide.load.data.DataFetcher; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +// see https://github.com/bumptech/glide/issues/699 +class AudioCoverFetcher implements DataFetcher { + + private static final String TAG = "AudioCoverFetcher"; + + private final String path; + + public AudioCoverFetcher(String path) { + this.path = path; + } + + @Override public String getId() { + return path; + } + + @Override public InputStream loadData(Priority priority) throws Exception { + MediaMetadataRetriever retriever = new MediaMetadataRetriever(); + try { + retriever.setDataSource(path); + byte[] picture = retriever.getEmbeddedPicture(); + if (picture != null) { + return new ByteArrayInputStream(picture); + } + } finally { + retriever.release(); + } + throw new IOException("Loading embedded cover did not work"); + } + + @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/service/download/DownloadService.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java index 00b0e6db3..06a89ca64 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java @@ -1009,7 +1009,7 @@ public class DownloadService extends Service { } media.setDownloaded(true); media.setFile_url(request.getDestination()); - media.setHasEmbeddedPicture(null); + media.checkEmbeddedPicture(); // enforce check // check if file has chapters ChapterUtils.loadChaptersFromFileUrl(media);