Commit f71e05b2 authored by Geoffrey Métais's avatar Geoffrey Métais

Add support for threaded browsing

parent 9ecaa0af
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
package org.videolan.libvlc; package org.videolan.libvlc;
import android.os.Handler;
import android.util.SparseArray; import android.util.SparseArray;
@SuppressWarnings("unused, JniMissingFunction") @SuppressWarnings("unused, JniMissingFunction")
...@@ -115,8 +116,8 @@ public class MediaList extends VLCObject<MediaList.Event> { ...@@ -115,8 +116,8 @@ public class MediaList extends VLCObject<MediaList.Event> {
return media; return media;
} }
public void setEventListener(EventListener listener) { public void setEventListener(EventListener listener, Handler handler) {
super.setEventListener(listener); super.setEventListener(listener, handler);
} }
@Override @Override
......
...@@ -98,11 +98,22 @@ abstract class VLCObject<T extends VLCEvent> { ...@@ -98,11 +98,22 @@ abstract class VLCObject<T extends VLCEvent> {
* @param listener see {@link VLCEvent.Listener} * @param listener see {@link VLCEvent.Listener}
*/ */
protected synchronized void setEventListener(VLCEvent.Listener<T> 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) if (mHandler != null)
mHandler.removeCallbacksAndMessages(null); mHandler.removeCallbacksAndMessages(null);
mEventListener = listener; mEventListener = listener;
if (mEventListener != null && mHandler == null) if (mEventListener == null)
mHandler = new Handler(Looper.getMainLooper()); mHandler = null;
else if (mHandler == null)
mHandler = handler != null ? handler : new Handler(Looper.getMainLooper());
} }
/** /**
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
package org.videolan.libvlc.util; package org.videolan.libvlc.util;
import android.net.Uri; import android.net.Uri;
import android.os.Handler;
import android.support.annotation.MainThread; import android.support.annotation.MainThread;
import android.util.Log; import android.util.Log;
...@@ -40,6 +41,7 @@ public class MediaBrowser { ...@@ -40,6 +41,7 @@ public class MediaBrowser {
private MediaList mBrowserMediaList; private MediaList mBrowserMediaList;
private Media mMedia; private Media mMedia;
private EventListener mEventListener; private EventListener mEventListener;
private Handler mHandler;
private boolean mAlive; private boolean mAlive;
private static final String IGNORE_LIST_OPTION = ":ignore-filetypes="; private static final String IGNORE_LIST_OPTION = ":ignore-filetypes=";
...@@ -76,6 +78,13 @@ public class MediaBrowser { ...@@ -76,6 +78,13 @@ public class MediaBrowser {
void onBrowseEnd(); 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) { public MediaBrowser(LibVLC libvlc, EventListener listener) {
mLibVlc = libvlc; mLibVlc = libvlc;
mLibVlc.retain(); mLibVlc.retain();
...@@ -83,6 +92,17 @@ public class MediaBrowser { ...@@ -83,6 +92,17 @@ public class MediaBrowser {
mAlive = true; 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() { private void reset() {
for (MediaDiscoverer md : mMediaDiscoverers) for (MediaDiscoverer md : mMediaDiscoverers)
md.release(); md.release();
...@@ -125,7 +145,7 @@ public class MediaBrowser { ...@@ -125,7 +145,7 @@ public class MediaBrowser {
MediaDiscoverer md = new MediaDiscoverer(mLibVlc, discovererName); MediaDiscoverer md = new MediaDiscoverer(mLibVlc, discovererName);
mMediaDiscoverers.add(md); mMediaDiscoverers.add(md);
final MediaList ml = md.getMediaList(); final MediaList ml = md.getMediaList();
ml.setEventListener(mDiscovererMediaListEventListener); ml.setEventListener(mDiscovererMediaListEventListener, mHandler);
ml.release(); ml.release();
md.start(); md.start();
} }
...@@ -203,7 +223,7 @@ public class MediaBrowser { ...@@ -203,7 +223,7 @@ public class MediaBrowser {
mediaFlags |= Media.Parse.DoInteract; mediaFlags |= Media.Parse.DoInteract;
reset(); reset();
mBrowserMediaList = media.subItems(); mBrowserMediaList = media.subItems();
mBrowserMediaList.setEventListener(mBrowserMediaListEventListener); mBrowserMediaList.setEventListener(mBrowserMediaListEventListener, mHandler);
media.parseAsync(mediaFlags, 0); media.parseAsync(mediaFlags, 0);
mMedia = media; mMedia = media;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment