Re-enable embedded images

This commit is contained in:
Martin Fietz 2016-04-27 17:49:27 +02:00
parent 92d63a6570
commit 26cd3faffc
5 changed files with 71 additions and 29 deletions

View File

@ -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;

View File

@ -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());
}
}

View File

@ -26,14 +26,14 @@ import de.danoeh.antennapod.core.util.NetworkUtils;
/**
* @see com.bumptech.glide.integration.okhttp.OkHttpUrlLoader
*/
public class ApOkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
public class ApOkHttpUrlLoader implements ModelLoader<String, InputStream> {
private static final String TAG = ApOkHttpUrlLoader.class.getSimpleName();
/**
* The default factory for {@link ApOkHttpUrlLoader}s.
*/
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
public static class Factory implements ModelLoaderFactory<String, InputStream> {
private static volatile OkHttpClient internalClient;
private OkHttpClient client;
@ -66,7 +66,7 @@ public class ApOkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
}
@Override
public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) {
public ModelLoader<String, InputStream> build(Context context, GenericLoaderFactory factories) {
return new ApOkHttpUrlLoader(client);
}
@ -83,8 +83,15 @@ public class ApOkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
}
@Override
public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) {
return new OkHttpStreamFetcher(client, model);
public DataFetcher<InputStream> 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 {

View File

@ -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<InputStream> {
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
}
}

View File

@ -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);