[Android] Export HttpImageFetcher in a standalone class

Geoffrey Métais git at videolan.org
Wed Feb 17 18:05:30 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Feb 17 18:01:02 2016 +0100| [74cfd0a89b0147452a2502ac686231ff61e7549e] | committer: Geoffrey Métais

Export HttpImageFetcher in a standalone class

> https://code.videolan.org/videolan/vlc-android/commit/74cfd0a89b0147452a2502ac686231ff61e7549e
---

 .../videolan/vlc/gui/browser/ExtensionAdapter.java | 58 +-------------
 .../org/videolan/vlc/util/HttpImageFetcher.java    | 91 ++++++++++++++++++++++
 2 files changed, 92 insertions(+), 57 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/ExtensionAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/ExtensionAdapter.java
index c988f42..b3632af 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/ExtensionAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/ExtensionAdapter.java
@@ -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:
diff --git a/vlc-android/src/org/videolan/vlc/util/HttpImageFetcher.java b/vlc-android/src/org/videolan/vlc/util/HttpImageFetcher.java
new file mode 100644
index 0000000..aca03a0
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/util/HttpImageFetcher.java
@@ -0,0 +1,91 @@
+/*
+ * ************************************************************************
+ *  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));
+        }
+    }
+}



More information about the Android mailing list