[Android] Thumbnail: fix infinite retries + simplify

Sébastien Toque git at videolan.org
Tue Mar 26 12:00:39 CET 2013


vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Tue Mar 26 11:43:56 2013 +0100| [836e45c493d868285b21d78676b3f38e0f63827c] | committer: Jean-Baptiste Kempf

Thumbnail: fix infinite retries + simplify

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=836e45c493d868285b21d78676b3f38e0f63827c
---

 vlc-android/src/org/videolan/vlc/BitmapCache.java  |   12 ++++++++++
 .../src/org/videolan/vlc/ThumbnailerManager.java   |    4 ++--
 .../videolan/vlc/gui/audio/AudioListAdapter.java   |   12 ++--------
 .../vlc/gui/audio/AudioPlaylistAdapter.java        |   23 ++++---------------
 .../videolan/vlc/gui/video/VideoListAdapter.java   |   24 ++++++++------------
 5 files changed, 30 insertions(+), 45 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/BitmapCache.java b/vlc-android/src/org/videolan/vlc/BitmapCache.java
index 005b625..1ce08a4 100644
--- a/vlc-android/src/org/videolan/vlc/BitmapCache.java
+++ b/vlc-android/src/org/videolan/vlc/BitmapCache.java
@@ -23,8 +23,10 @@ package org.videolan.vlc;
 import android.app.ActivityManager;
 import android.content.Context;
 import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.support.v4.util.LruCache;
 import android.util.Log;
+import android.view.View;
 
 public class BitmapCache {
 
@@ -93,4 +95,14 @@ public class BitmapCache {
     public void clear() {
         mMemCache.evictAll();
     }
+
+    public static Bitmap GetFromResource(View v, int resId) {
+        BitmapCache cache = BitmapCache.getInstance();
+        Bitmap bitmap = cache.getBitmapFromMemCache(resId);
+        if (bitmap == null) {
+            bitmap = BitmapFactory.decodeResource(v.getResources(), resId);
+            cache.addBitmapToMemCache(resId, bitmap);
+        }
+        return bitmap;
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/ThumbnailerManager.java b/vlc-android/src/org/videolan/vlc/ThumbnailerManager.java
index fc5c44a..fe332ee 100644
--- a/vlc-android/src/org/videolan/vlc/ThumbnailerManager.java
+++ b/vlc-android/src/org/videolan/vlc/ThumbnailerManager.java
@@ -161,8 +161,8 @@ public class ThumbnailerManager implements Runnable {
             //Log.i(TAG, "create new bitmap for: " + item.getName());
             byte[] b = mLibVlc.getThumbnail(item.getLocation(), width, height);
 
-            if (b == null) {// We were not able to create a thumbnail for this item.
-                item.setPicture(mContext, null);
+            if (b == null) {// We were not able to create a thumbnail for this item, store a dummy
+                item.setPicture(mContext, Bitmap.createBitmap(1, 1, Config.ARGB_8888));
                 continue;
             }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioListAdapter.java
index b9a18e7..8e4eac8 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioListAdapter.java
@@ -31,7 +31,6 @@ import org.videolan.vlc.Util;
 import android.content.Context;
 import android.content.res.ColorStateList;
 import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -91,15 +90,8 @@ public class AudioListAdapter extends ArrayAdapter<Media> {
         Media media = getItem(position);
 
         Bitmap cover = AudioUtil.getCover(v.getContext(), media, 64);
-
-        if (cover == null) {
-            BitmapCache cache = BitmapCache.getInstance();
-            cover = cache.getBitmapFromMemCache(R.drawable.icon);
-            if (cover == null) {
-                cover = BitmapFactory.decodeResource(v.getResources(), R.drawable.icon);
-                cache.addBitmapToMemCache(R.drawable.icon, cover);
-            }
-        }
+        if (cover == null)
+            cover = BitmapCache.GetFromResource(v, R.drawable.icon);
 
         holder.cover.setImageBitmap(cover);
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlaylistAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlaylistAdapter.java
index 59a92a5..3fba895 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlaylistAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlaylistAdapter.java
@@ -32,7 +32,6 @@ import org.videolan.vlc.Util;
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -170,15 +169,8 @@ public class AudioPlaylistAdapter extends BaseExpandableListAdapter {
         Resources res = mContext.getResources();
 
         Bitmap cover = AudioUtil.getCover(v.getContext(), list.get(0), 64);
-
-        if (cover == null) {
-            BitmapCache cache = BitmapCache.getInstance();
-            cover = cache.getBitmapFromMemCache(R.drawable.icon);
-            if (cover == null) {
-                cover = BitmapFactory.decodeResource(v.getResources(), R.drawable.icon);
-                cache.addBitmapToMemCache(R.drawable.icon, cover);
-            }
-        }
+        if (cover == null)
+            cover = BitmapCache.GetFromResource(v, R.drawable.icon);
 
         holder.cover.setImageBitmap(cover);
 
@@ -221,15 +213,8 @@ public class AudioPlaylistAdapter extends BaseExpandableListAdapter {
         Resources res = mContext.getResources();
 
         Bitmap cover = AudioUtil.getCover(v.getContext(), list.get(0), 64);
-
-        if (cover == null) {
-            BitmapCache cache = BitmapCache.getInstance();
-            cover = cache.getBitmapFromMemCache(R.drawable.icon);
-            if (cover == null) {
-                cover = BitmapFactory.decodeResource(v.getResources(), R.drawable.icon);
-                cache.addBitmapToMemCache(R.drawable.icon, cover);
-            }
-        }
+        if (cover == null)
+            cover = BitmapCache.GetFromResource(v, R.drawable.icon);
 
         holder.cover.setImageBitmap(cover);
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 5a17de9..03ba310 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -31,7 +31,6 @@ import org.videolan.vlc.Util;
 import android.content.Context;
 import android.content.res.ColorStateList;
 import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -156,20 +155,17 @@ public class VideoListAdapter extends ArrayAdapter<Media>
         Media media = getItem(position);
 
         /* Thumbnail */
-        if (media.getPicture() != null) {
-            //FIXME Warning: the thumbnails are upscaled in the grid view!
-            final Bitmap thumbnail = media.getPicture();
-            holder.thumbnail.setImageBitmap(thumbnail);
-        } else {
-            // set default thumbnail
-            BitmapCache cache = BitmapCache.getInstance();
-            Bitmap bitmap = cache.getBitmapFromMemCache(R.drawable.thumbnail);
-            if (bitmap == null) {
-                bitmap = BitmapFactory.decodeResource(v.getResources(), R.drawable.thumbnail);
-                cache.addBitmapToMemCache(R.drawable.thumbnail, bitmap);
-            }
-            holder.thumbnail.setImageBitmap(bitmap);
+        Bitmap thumbnail = media.getPicture();
+        if (thumbnail == null) {
+            // missing thumbnail
+            thumbnail = BitmapCache.GetFromResource(v, R.drawable.thumbnail);
+        }
+        else if (thumbnail.getWidth() == 1 && thumbnail.getHeight() == 1) {
+            // dummy thumbnail
+            thumbnail = BitmapCache.GetFromResource(v, R.drawable.icon);
         }
+        //FIXME Warning: the thumbnails are upscaled in the grid view!
+        holder.thumbnail.setImageBitmap(thumbnail);
 
         /* Color state */
         ColorStateList titleColor = v.getResources().getColorStateList(R.color.list_title);



More information about the Android mailing list