Commit 74cfd0a8 authored by Geoffrey Métais's avatar Geoffrey Métais

Export HttpImageFetcher in a standalone class

parent bd9edb62
......@@ -2,7 +2,6 @@ package org.videolan.vlc.gui.browser;
import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
......@@ -12,24 +11,16 @@ import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import org.videolan.vlc.BR;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.ExtensionItemViewBinding;
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 java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -38,7 +29,6 @@ public class ExtensionAdapter extends RecyclerView.Adapter<ExtensionAdapter.View
ExtensionBrowser mFragment;
List<VLCExtensionItem> mItemsList = new ArrayList<>();
static HashMap<String, SoftReference<Bitmap>> iconsMap = new HashMap<>();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
......@@ -107,52 +97,6 @@ public class ExtensionAdapter extends RecyclerView.Adapter<ExtensionAdapter.View
}
}
private static class HttpImageFetcher extends AsyncImageLoader.CoverFetcher {
final String imageLink;
HttpImageFetcher(ViewDataBinding binding, String imageLink) {
super(binding);
this.imageLink = imageLink;
}
@Override
public Bitmap getImage() {
if (iconsMap.containsKey(imageLink)) {
Bitmap bd = iconsMap.get(imageLink).get();
if (bd != null) {
return bd;
} else
iconsMap.remove(imageLink);
}
HttpURLConnection urlConnection = null;
Bitmap icon = null;
try {
URL url = new URL(imageLink);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
icon = BitmapFactory.decodeStream(in);
iconsMap.put(imageLink, new SoftReference<>(icon));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
return icon;
}
@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));
}
}
}
private int getIconResId(VLCExtensionItem item) {
switch (item.type){
case VLCExtensionItem.TYPE_AUDIO:
......
/*
* ************************************************************************
* HttpImageFetcher.java
* *************************************************************************
* Copyright © 2016 VLC authors and VideoLAN
* Author: Geoffrey Métais
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*
* *************************************************************************
*/
package org.videolan.vlc.util;
import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.util.SimpleArrayMap;
import android.view.View;
import android.widget.ImageView;
import org.videolan.vlc.BR;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.gui.helpers.AsyncImageLoader;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
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 HttpImageFetcher(ViewDataBinding binding, String imageLink) {
super(binding);
this.imageLink = imageLink;
}
@Override
public Bitmap getImage() {
if (iconsMap.containsKey(imageLink)) {
Bitmap bd = iconsMap.get(imageLink).get();
if (bd != null) {
return bd;
} else
iconsMap.remove(imageLink);
}
HttpURLConnection urlConnection = null;
Bitmap icon = null;
try {
URL url = new URL(imageLink);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
icon = BitmapFactory.decodeStream(in);
iconsMap.put(imageLink, new SoftReference<>(icon));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
return icon;
}
@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));
}
}
}
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