[Android] MediaList: switch to use ArrayList backend

Edward Wang git at videolan.org
Mon Oct 28 04:14:40 CET 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Sun Oct 27 18:03:45 2013 -0400| [bffdfa60db1c8a9789e968dc837fd21a3b4724f8] | committer: Edward Wang

MediaList: switch to use ArrayList backend
Add a MediaHolder to hold the media options because media_t is created only when playing.

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

 vlc-android/src/org/videolan/libvlc/MediaList.java |  121 ++++++++++++++------
 1 file changed, 84 insertions(+), 37 deletions(-)

diff --git a/vlc-android/src/org/videolan/libvlc/MediaList.java b/vlc-android/src/org/videolan/libvlc/MediaList.java
index 4ac851c..a3bf600 100644
--- a/vlc-android/src/org/videolan/libvlc/MediaList.java
+++ b/vlc-android/src/org/videolan/libvlc/MediaList.java
@@ -20,56 +20,68 @@
  *****************************************************************************/
 package org.videolan.libvlc;
 
+import java.util.ArrayList;
+
+import org.videolan.vlc.Media;
+
+import android.os.Bundle;
+
 /**
  * Java/JNI wrapper for the libvlc_media_list_t structure.
  */
 public class MediaList {
     private static final String TAG = "VLC/LibVLC/MediaList";
 
-    private long mMediaListInstance = 0; // Read-only, reserved for JNI
-    private long mEventHanderGlobalRef = 0; // Read-only, reserved for JNI
+    /* Since the libvlc_media_t is not created until the media plays, we have
+     * to cache them here. */
+    private class MediaHolder {
+        Media m;
+        boolean noVideo; // default false
+        boolean noOmx; // default false
+
+        public MediaHolder(Media media) {
+            m = media; noVideo = false; noOmx = false;
+        }
+        public MediaHolder(Media m_, boolean noVideo_, boolean noOmx_) {
+            m = m_; noVideo = noVideo_; noOmx = noOmx_;
+        }
+    }
+
+    /* TODO: add locking */
+    private ArrayList<MediaHolder> mInternalList;
     private LibVLC mLibVLC; // Used to create new objects that require a libvlc instance
-    private boolean destroyed = false;
     private EventHandler mEventHandler;
 
     public MediaList(LibVLC libVLC) {
         mEventHandler = new EventHandler(); // used in init() below to fire events at the correct targets
-        mMediaListInstance = init(libVLC);
+        mInternalList = new ArrayList<MediaHolder>();
         mLibVLC = libVLC;
     }
-    private native long init(LibVLC libvlc_instance);
 
-    @Override
-    public void finalize() {
-        if(!destroyed) destroy();
+    public void add(String mrl) {
+        add(new Media(mrl, false));
     }
-
-    /**
-     * Releases the media list.
-     *
-     * The object should be considered released after this and must not be used.
-     */
-    public void destroy() {
-        nativeDestroy();
-        mMediaListInstance = 0;
-        mEventHanderGlobalRef = 0;
-        mLibVLC = null;
-        destroyed = true;
+    public void add(Media media) {
+        add(media, false, false);
     }
-    private native void nativeDestroy();
-
-    public void add(String mrl) {
-        add(mLibVLC, mrl, false, false);
+    public void add(Media media, boolean noVideo) {
+        add(media, noVideo, false);
     }
-    public void add(String mrl, boolean noVideo) {
-        add(mLibVLC, mrl, noVideo, false);
+    public void add(Media media, boolean noVideo, boolean noOmx) {
+        mInternalList.add(new MediaHolder(media, noVideo, noOmx));
+        signal_list_event(EventHandler.MediaListItemAdded, mInternalList.size() - 1, media.getLocation());
     }
-    private native void add(LibVLC libvlc_instance, String mrl, boolean noVideo, boolean noOmx);
 
     /**
      * Clear the media list. (remove all media)
      */
-    public native void clear();
+    public void clear() {
+        // Signal to observers of media being deleted.
+        for(int i = 0; i < mInternalList.size(); i++) {
+            signal_list_event(EventHandler.MediaListItemDeleted, i, mInternalList.get(i).m.getLocation());
+        }
+        mInternalList.clear();
+    }
 
     /**
      * This function checks the currently playing media for subitems at the given
@@ -80,29 +92,57 @@ public class MediaList {
      * @return -1 if no subitems were found, 0 if subitems were expanded
      */
     public int expandMedia(int position) {
-        return expandMedia(mLibVLC, position);
+        ArrayList<String> children = new ArrayList<String>();
+        int ret = expandMedia(mLibVLC, position, children);
+        if(ret == 0) {
+            this.remove(position);
+            for(String mrl : children) {
+                this.insert(position, mrl);
+            }
+        }
+        return ret;
     }
-    private native int expandMedia(LibVLC libvlc_instance, int position);
+    private native int expandMedia(LibVLC libvlc_instance, int position, ArrayList<String> children);
 
     public void loadPlaylist(String mrl) {
-        loadPlaylist(mLibVLC, mrl);
+        ArrayList<String> items = new ArrayList<String>();
+        loadPlaylist(mLibVLC, mrl, items);
+        this.clear();
+        for(String item : items) {
+            this.add(item);
+        }
     }
-    private native void loadPlaylist(LibVLC libvlc_instance, String mrl);
+    private native void loadPlaylist(LibVLC libvlc_instance, String mrl, ArrayList<String> items);
 
     public void insert(int position, String mrl) {
-        insert(mLibVLC, position, mrl);
+        insert(position, new Media(mrl, false));
+    }
+    public void insert(int position, Media media) {
+        mInternalList.add(position, new MediaHolder(media));
+        signal_list_event(EventHandler.MediaListItemAdded, position, media.getLocation());
     }
-    private native void insert(LibVLC libvlc_instance, int position, String mrl);
 
-    public native void remove(int position);
+    public void remove(int position) {
+        String uri = mInternalList.get(position).m.getLocation();
+        mInternalList.remove(position);
+        signal_list_event(EventHandler.MediaListItemDeleted, position, uri);
+    }
+
+    public int size() {
+        return mInternalList.size();
+    }
 
-    public native int size();
+    public Media getMedia(int position) {
+        return mInternalList.get(position).m;
+    }
 
     /**
      * @param position The index of the media in the list
      * @return null if not found
      */
-    public native String getMRL(int position);
+    public String getMRL(int position) {
+        return mInternalList.get(position).m.getLocation();
+    }
 
     public EventHandler getEventHandler() {
         return mEventHandler;
@@ -121,4 +161,11 @@ public class MediaList {
         sb.append("}");
         return sb.toString();
     }
+
+    private void signal_list_event(int event, int position, String uri) {
+        Bundle b = new Bundle();
+        b.putString("item_uri", uri);
+        b.putInt("item_index", position);
+        mEventHandler.callback(event, b);
+    }
 }



More information about the Android mailing list