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

Add support for threaded browsing

parent 9ecaa0af
......@@ -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
......
......@@ -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());
}
/**
......
......@@ -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;
}
......
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