[Android] Separate MediaDatabase operations from the Media object

Edward Wang git at videolan.org
Sat Nov 16 18:19:24 CET 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Sat Nov 16 12:07:14 2013 -0500| [e83020380a9584e0e8ade2ce89e6b6ad63261801] | committer: Edward Wang

Separate MediaDatabase operations from the Media object

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

 vlc-android/src/org/videolan/vlc/Media.java        |   48 ++++++++------------
 vlc-android/src/org/videolan/vlc/MediaGroup.java   |    2 +-
 vlc-android/src/org/videolan/vlc/Thumbnailer.java  |    6 +--
 vlc-android/src/org/videolan/vlc/Util.java         |   38 ++++++++++++++++
 .../videolan/vlc/gui/video/VideoListAdapter.java   |    2 +-
 5 files changed, 61 insertions(+), 35 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/Media.java b/vlc-android/src/org/videolan/vlc/Media.java
index 6232261..0e7cada 100644
--- a/vlc-android/src/org/videolan/vlc/Media.java
+++ b/vlc-android/src/org/videolan/vlc/Media.java
@@ -328,39 +328,27 @@ public class Media implements Comparable<Media> {
         return mHeight;
     }
 
+    /**
+     * Returns the raw picture object. Likely to be NULL in VLC for Android
+     * due to lazy-loading.
+     *
+     * Use {@link org.videolan.vlc.Util#getPictureFromCache(Media)} instead.
+     *
+     * @return The raw picture or NULL
+     */
     public Bitmap getPicture() {
-        // mPicture is not null only if passed through
-        // the ctor which is deprecated by now.
-        if (mPicture == null) {
-            BitmapCache cache = BitmapCache.getInstance();
-            Bitmap picture = cache.getBitmapFromMemCache(mLocation);
-            if (picture == null) {
-                /* Not in memcache:
-                 * serving the file from the database and
-                 * adding it to the memcache for later use.
-                 */
-                Context c = VLCApplication.getAppContext();
-                picture = MediaDatabase.getInstance(c).getPicture(c, mLocation);
-                cache.addBitmapToMemCache(mLocation, picture);
-            }
-            return picture;
-        } else {
-            return mPicture;
-        }
+        return mPicture;
     }
 
-    public void setPicture(Context context, Bitmap p) {
-        Log.d(TAG, "Set new picture for " + getTitle());
-        try {
-            MediaDatabase.getInstance(context).updateMedia(
-                mLocation,
-                MediaDatabase.mediaColumn.MEDIA_PICTURE,
-                p);
-        } catch (SQLiteFullException e) {
-            // TODO: do something clever
-            e.printStackTrace();
-        }
-        mIsPictureParsed = true;
+    /**
+     * Sets the raw picture object.
+     *
+     * In VLC for Android, use {@link org.videolan.vlc.Util#setPicture(Context, Media, Bitmap)} instead.
+     *
+     * @param p
+     */
+    public void setPicture(Bitmap p) {
+        mPicture = p;
     }
 
     public boolean isPictureParsed() {
diff --git a/vlc-android/src/org/videolan/vlc/MediaGroup.java b/vlc-android/src/org/videolan/vlc/MediaGroup.java
index 7a107b1..3773663 100644
--- a/vlc-android/src/org/videolan/vlc/MediaGroup.java
+++ b/vlc-android/src/org/videolan/vlc/MediaGroup.java
@@ -38,7 +38,7 @@ public class MediaGroup extends Media {
                 media.getTime(),
                 media.getLength(),
                 Media.TYPE_GROUP,
-                media.getPicture(),
+                Util.getPictureFromCache(media),
                 media.getTitle(),
                 media.getArtist(),
                 media.getGenre(),
diff --git a/vlc-android/src/org/videolan/vlc/Thumbnailer.java b/vlc-android/src/org/videolan/vlc/Thumbnailer.java
index 368efb1..4bc1587 100644
--- a/vlc-android/src/org/videolan/vlc/Thumbnailer.java
+++ b/vlc-android/src/org/videolan/vlc/Thumbnailer.java
@@ -107,7 +107,7 @@ public class Thumbnailer implements Runnable {
      * @param id the if of the file browser item.
      */
     public void addJob(Media item) {
-        if (item.getPicture() != null || item.isPictureParsed())
+        if(Util.getPictureFromCache(item) != null || item.isPictureParsed())
             return;
         lock.lock();
         mItems.add(item);
@@ -166,7 +166,7 @@ public class Thumbnailer implements Runnable {
             byte[] b = mLibVlc.getThumbnail(item.getLocation(), width, height);
 
             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));
+                Util.setPicture(mContext, item, Bitmap.createBitmap(1, 1, Config.ARGB_8888));
                 continue;
             }
 
@@ -174,7 +174,7 @@ public class Thumbnailer implements Runnable {
 
             Log.i(TAG, "Thumbnail created for " + item.getFileName());
 
-            item.setPicture(mContext, thumbnail);
+            Util.setPicture(mContext, item, thumbnail);
             // Post to the file browser the new item.
             mVideoGridFragment.setItemToUpdate(item);
 
diff --git a/vlc-android/src/org/videolan/vlc/Util.java b/vlc-android/src/org/videolan/vlc/Util.java
index d8eec0c..86d66a3 100644
--- a/vlc-android/src/org/videolan/vlc/Util.java
+++ b/vlc-android/src/org/videolan/vlc/Util.java
@@ -42,11 +42,13 @@ import org.videolan.libvlc.LibVlcException;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
+import android.database.sqlite.SQLiteFullException;
 import android.graphics.Bitmap;
 import android.os.Environment;
 import android.preference.PreferenceManager;
 import android.telephony.TelephonyManager;
 import android.util.DisplayMetrics;
+import android.util.Log;
 import android.util.TypedValue;
 import android.view.Display;
 import android.view.WindowManager;
@@ -290,6 +292,42 @@ public class Util {
                 (width - (2 * left)), (height - (2 * top)));
     }
 
+    public static Bitmap getPictureFromCache(Media media)
+    {
+        // mPicture is not null only if passed through
+        // the ctor which is deprecated by now.
+        Bitmap b = media.getPicture();
+        if(b == null) {
+            BitmapCache cache = BitmapCache.getInstance();
+            Bitmap picture = cache.getBitmapFromMemCache(media.getLocation());
+            if(picture == null) {
+                /* Not in memcache:
+                 * serving the file from the database and
+                 * adding it to the memcache for later use.
+                 */
+                Context c = VLCApplication.getAppContext();
+                picture = MediaDatabase.getInstance(c).getPicture(c, media.getLocation());
+                cache.addBitmapToMemCache(media.getLocation(), picture);
+            }
+            return picture;
+        } else {
+            return b;
+        }
+    }
+
+    public static void setPicture(Context context, Media m, Bitmap p) {
+        Log.d(TAG, "Setting new picture for " + m.getTitle());
+        try {
+            MediaDatabase.getInstance(context).updateMedia(
+                m.getLocation(),
+                MediaDatabase.mediaColumn.MEDIA_PICTURE,
+                p);
+        } catch (SQLiteFullException e) {
+            Log.d(TAG, "SQLiteFullException while setting picture");
+        }
+        m.setPictureParsed(true);
+    }
+
     public static String getValue(String string, int defaultId)
     {
         return (string != null && string.length() > 0) ?
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 b09a6ec..cd34eca 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -166,7 +166,7 @@ public class VideoListAdapter extends ArrayAdapter<Media>
         Media media = getItem(position);
 
         /* Thumbnail */
-        Bitmap thumbnail = media.getPicture();
+        Bitmap thumbnail = Util.getPictureFromCache(media);
         if (thumbnail == null) {
             // missing thumbnail
             thumbnail = BitmapCache.GetFromResource(v, R.drawable.thumbnail);



More information about the Android mailing list