[Android] Add support for threaded browsing

Geoffrey Métais git at videolan.org
Fri Mar 10 11:42:58 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Mar  8 17:07:58 2017 +0100| [f71e05b20200bf13fb19e61b0b0ddaa71bf515d7] | committer: Geoffrey Métais

Add support for threaded browsing

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

 libvlc/src/org/videolan/libvlc/MediaList.java      |  5 +++--
 libvlc/src/org/videolan/libvlc/VLCObject.java      | 15 ++++++++++++--
 .../src/org/videolan/libvlc/util/MediaBrowser.java | 24 ++++++++++++++++++++--
 3 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/MediaList.java b/libvlc/src/org/videolan/libvlc/MediaList.java
index 1c4263b..9c6fc18 100644
--- a/libvlc/src/org/videolan/libvlc/MediaList.java
+++ b/libvlc/src/org/videolan/libvlc/MediaList.java
@@ -20,6 +20,7 @@
 
 package org.videolan.libvlc;
 
+import android.os.Handler;
 import android.util.SparseArray;
 
 @SuppressWarnings("unused, JniMissingFunction")
@@ -115,8 +116,8 @@ public class MediaList extends VLCObject<MediaList.Event> {
         return media;
     }
 
-    public void setEventListener(EventListener listener) {
-        super.setEventListener(listener);
+    public void setEventListener(EventListener listener, Handler handler) {
+        super.setEventListener(listener, handler);
     }
 
     @Override
diff --git a/libvlc/src/org/videolan/libvlc/VLCObject.java b/libvlc/src/org/videolan/libvlc/VLCObject.java
index 7d11b8e..eb50957 100644
--- a/libvlc/src/org/videolan/libvlc/VLCObject.java
+++ b/libvlc/src/org/videolan/libvlc/VLCObject.java
@@ -98,11 +98,22 @@ abstract class VLCObject<T extends VLCEvent> {
      * @param listener see {@link VLCEvent.Listener}
      */
     protected synchronized void setEventListener(VLCEvent.Listener<T> listener) {
+        setEventListener(listener, null);
+    }
+
+    /**
+     * Set an event listener and an executor Handler
+     * @param listener see {@link VLCEvent.Listener}
+     * @param handler Handler in which events are sent. If null, a handler will be created running on the main thread
+     */
+    protected synchronized void setEventListener(VLCEvent.Listener<T> listener, Handler handler) {
         if (mHandler != null)
             mHandler.removeCallbacksAndMessages(null);
         mEventListener = listener;
-        if (mEventListener != null && mHandler == null)
-            mHandler = new Handler(Looper.getMainLooper());
+        if (mEventListener == null)
+            mHandler = null;
+        else if (mHandler == null)
+            mHandler = handler != null ? handler : new Handler(Looper.getMainLooper());
     }
 
     /**
diff --git a/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
index 59dbb7a..ce07348 100644
--- a/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
+++ b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
@@ -21,6 +21,7 @@
 package org.videolan.libvlc.util;
 
 import android.net.Uri;
+import android.os.Handler;
 import android.support.annotation.MainThread;
 import android.util.Log;
 
@@ -40,6 +41,7 @@ public class MediaBrowser {
     private MediaList mBrowserMediaList;
     private Media mMedia;
     private EventListener mEventListener;
+    private Handler mHandler;
     private boolean mAlive;
 
     private static final String IGNORE_LIST_OPTION =  ":ignore-filetypes=";
@@ -76,6 +78,13 @@ public class MediaBrowser {
         void onBrowseEnd();
     }
 
+     /**
+     *
+     * @param libvlc The LibVLC instance to use
+     * @param listener The Listener which will receive callbacks
+     *
+     * With this constructor, callbacks will be executed in the main thread
+     */
     public MediaBrowser(LibVLC libvlc, EventListener listener) {
         mLibVlc = libvlc;
         mLibVlc.retain();
@@ -83,6 +92,17 @@ public class MediaBrowser {
         mAlive = true;
     }
 
+    /**
+     *
+     * @param libvlc The LibVLC instance to use
+     * @param listener The Listener which will receive callbacks
+     * @param handler Optional Handler in which callbacks will be posted. If set to null, a Handler will be created running on the main thread
+     */
+    public MediaBrowser(LibVLC libvlc, EventListener listener, Handler handler) {
+        this(libvlc, listener);
+        mHandler = handler;
+    }
+
     private void reset() {
         for (MediaDiscoverer md : mMediaDiscoverers)
             md.release();
@@ -125,7 +145,7 @@ public class MediaBrowser {
         MediaDiscoverer md = new MediaDiscoverer(mLibVlc, discovererName);
         mMediaDiscoverers.add(md);
         final MediaList ml = md.getMediaList();
-        ml.setEventListener(mDiscovererMediaListEventListener);
+        ml.setEventListener(mDiscovererMediaListEventListener, mHandler);
         ml.release();
         md.start();
     }
@@ -203,7 +223,7 @@ public class MediaBrowser {
             mediaFlags |= Media.Parse.DoInteract;
         reset();
         mBrowserMediaList = media.subItems();
-        mBrowserMediaList.setEventListener(mBrowserMediaListEventListener);
+        mBrowserMediaList.setEventListener(mBrowserMediaListEventListener, mHandler);
         media.parseAsync(mediaFlags, 0);
         mMedia = media;
     }



More information about the Android mailing list