Commit 7553854a authored by Geoffrey Métais's avatar Geoffrey Métais

Refactor Http image loader

Less databinder specific
parent c0e1bef0
......@@ -41,7 +41,7 @@ import org.videolan.vlc.media.MediaDatabase;
import org.videolan.vlc.media.MediaUtils;
import org.videolan.vlc.media.MediaWrapper;
import org.videolan.vlc.util.CustomDirectories;
import org.videolan.vlc.util.HttpImageFetcher;
import org.videolan.vlc.util.HttpImageLoader;
import java.io.File;
import java.util.ArrayList;
......@@ -111,7 +111,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
vh.binding.dviIcon.setBackgroundResource(getIconResId(media));
if (!TextUtils.isEmpty(media.getArtworkURL()) && media.getArtworkURL().startsWith("http"))
AsyncImageLoader.LoadImage(new HttpImageFetcher(vh.binding, media.getArtworkURL()), null);
AsyncImageLoader.LoadImage(new HttpImageLoader(media.getArtworkURL(), vh.binding), null);
vh.setContextMenuListener();
}
......
......@@ -18,11 +18,9 @@ import org.videolan.vlc.extensions.api.VLCExtensionItem;
import org.videolan.vlc.gui.helpers.AsyncImageLoader;
import org.videolan.vlc.media.MediaUtils;
import org.videolan.vlc.media.MediaWrapper;
import org.videolan.vlc.util.HttpImageFetcher;
import org.videolan.vlc.util.HttpImageLoader;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ExtensionAdapter extends RecyclerView.Adapter<ExtensionAdapter.ViewHolder> {
......@@ -93,7 +91,7 @@ public class ExtensionAdapter extends RecyclerView.Adapter<ExtensionAdapter.View
vh.binding.setImage(new BitmapDrawable(res, BitmapFactory.decodeResource(res, getIconResId(item))));
if (item.imageUri != null) {
if (TextUtils.equals("http", item.imageUri.getScheme()))
AsyncImageLoader.LoadImage(new HttpImageFetcher(holder.binding, item.getImageUri().toString()), null);
AsyncImageLoader.LoadImage(new HttpImageLoader(item.getImageUri().toString(), holder.binding), null);
}
}
......
/*
* ************************************************************************
* HttpImageFetcher.java
* HttpImageLoader.java
* *************************************************************************
* Copyright © 2016 VLC authors and VideoLAN
* Author: Geoffrey Métais
......@@ -24,17 +24,20 @@
package org.videolan.vlc.util;
import android.databinding.OnRebindCallback;
import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v17.leanback.widget.ImageCardView;
import android.support.v4.util.SimpleArrayMap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.videolan.vlc.BR;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.gui.helpers.AsyncImageLoader;
import org.videolan.vlc.gui.helpers.AsyncImageLoader.Callbacks;
import java.io.BufferedInputStream;
import java.io.IOException;
......@@ -44,32 +47,61 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpImageFetcher extends AsyncImageLoader.CoverFetcher {
static SimpleArrayMap<String, SoftReference<Bitmap>> iconsMap = new SimpleArrayMap<>();
final String imageLink;
public class HttpImageLoader implements Callbacks {
public HttpImageFetcher(ViewDataBinding binding, String imageLink) {
super(binding);
this.imageLink = imageLink;
private static SimpleArrayMap<String, SoftReference<Bitmap>> iconsMap = new SimpleArrayMap<>();
private String mImageLink;
private ViewDataBinding mBinding;
private boolean bindChanged = false;
final OnRebindCallback<ViewDataBinding> rebindCallbacks;
public HttpImageLoader(String imageLink) {
mImageLink = imageLink;
rebindCallbacks = null;
}
public HttpImageLoader(String imageLink, ViewDataBinding binding) {
mImageLink = imageLink;
mBinding = binding;
mBinding = binding;
mBinding.executePendingBindings();
rebindCallbacks = new OnRebindCallback<ViewDataBinding>() {
@Override
public boolean onPreBind(ViewDataBinding binding) {
bindChanged = true;
return super.onPreBind(binding);
}
@Override
public void onCanceled(ViewDataBinding binding) {
super.onCanceled(binding);
}
@Override
public void onBound(ViewDataBinding binding) {
super.onBound(binding);
}
};
mBinding.addOnRebindCallback(rebindCallbacks);
}
@Override
public Bitmap getImage() {
if (iconsMap.containsKey(imageLink)) {
Bitmap bd = iconsMap.get(imageLink).get();
if (iconsMap.containsKey(mImageLink)) {
Bitmap bd = iconsMap.get(mImageLink).get();
if (bd != null) {
return bd;
} else
iconsMap.remove(imageLink);
iconsMap.remove(mImageLink);
}
HttpURLConnection urlConnection = null;
Bitmap icon = null;
try {
URL url = new URL(imageLink);
URL url = new URL(mImageLink);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
icon = BitmapFactory.decodeStream(in);
iconsMap.put(imageLink, new SoftReference<>(icon));
iconsMap.put(mImageLink, new SoftReference<>(icon));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
......@@ -82,10 +114,30 @@ public class HttpImageFetcher extends AsyncImageLoader.CoverFetcher {
}
@Override
public void updateBindImage(Bitmap bitmap, View target) {
if (bitmap != null && (bitmap.getWidth() != 1 && bitmap.getHeight() != 1)) {
binding.setVariable(BR.scaleType, ImageView.ScaleType.FIT_CENTER);
binding.setVariable(BR.image, new BitmapDrawable(VLCApplication.getAppResources(), bitmap));
public void updateImage(final Bitmap bitmap, final View target) {
if (mBinding != null) {
mBinding.removeOnRebindCallback(rebindCallbacks);
if (bitmap != null && (bitmap.getWidth() != 1 && bitmap.getHeight() != 1)) {
if (mBinding != null) {
if (bindChanged)
return;
mBinding.setVariable(BR.scaleType, ImageView.ScaleType.FIT_CENTER);
mBinding.setVariable(BR.image, new BitmapDrawable(VLCApplication.getAppResources(), bitmap));
}
}
} else if (bitmap != null && (bitmap.getWidth() != 1 && bitmap.getHeight() != 1)) {
target.post(new Runnable() {
@Override
public void run() {
if (target instanceof ImageCardView)
((ImageCardView) target).setMainImage(new BitmapDrawable(target.getResources(), bitmap));
else if (target instanceof ImageView)
((ImageView) target).setImageBitmap(bitmap);
else if (target instanceof TextView)
target.setBackgroundDrawable(new BitmapDrawable(target.getResources(), bitmap));
}
});
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment