[Android] [PATCH 08/24] jni: Subscribe to MediaListItemAdded and MediaListItemDeleted

Edward Wang edward.c.wang at compdigitec.com
Wed Aug 22 23:15:08 CEST 2012


So that we can use playlists and history view in GUI.
---
 vlc-android/jni/libvlcjni.c                        |   56 +++++++++++++++++---
 vlc-android/src/org/videolan/vlc/EventManager.java |    8 +--
 2 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index d6b3423..7ea4972 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -247,11 +247,6 @@ static void vlc_event_callback(const libvlc_event_t *ev, void *data)
     JNIEnv *env;
     JavaVM *myVm = data;
 
-    int ev_opt_data = 0;
-    if(ev->type == libvlc_MediaPlayerVout) {
-        /* For determining the vout/ES track change */
-        ev_opt_data = ev->u.media_player_vout.new_count;
-    }
     bool isAttached = false;
 
     if (eventManagerInstance == NULL)
@@ -267,6 +262,42 @@ static void vlc_event_callback(const libvlc_event_t *ev, void *data)
         isAttached = true;
     }
 
+    jclass clsBundle = (*env)->FindClass(env, "android/os/Bundle");
+    jmethodID clsCtor = (*env)->GetMethodID(env, clsBundle, "<init>", "()V" );
+    jobject bundle = (*env)->NewObject(env, clsBundle, clsCtor);
+
+    jmethodID putInt = (*env)->GetMethodID(env, clsBundle, "putInt", "(Ljava/lang/String;I)V" );
+    jmethodID putString = (*env)->GetMethodID(env, clsBundle, "putString", "(Ljava/lang/String;Ljava/lang/String;)V" );
+
+    if(ev->type == libvlc_MediaPlayerVout) {
+        /* For determining the vout/ES track change */
+        jstring sData = (*env)->NewStringUTF(env, "data");
+        (*env)->CallVoidMethod(env, bundle, putInt, sData, ev->u.media_player_vout.new_count);
+        (*env)->DeleteLocalRef(env, sData);
+    } else if(ev->type == libvlc_MediaListItemAdded ||
+              ev->type == libvlc_MediaListItemDeleted ) {
+        jstring item_uri = (*env)->NewStringUTF(env, "item_uri");
+        jstring item_index = (*env)->NewStringUTF(env, "item_index");
+        char* mrl = libvlc_media_get_mrl(
+            ev->type == libvlc_MediaListItemAdded ?
+            ev->u.media_list_item_added.item :
+            ev->u.media_list_item_deleted.item
+            );
+        jstring item_uri_value = (*env)->NewStringUTF(env, mrl);
+
+        (*env)->CallVoidMethod(env, bundle, putString, item_uri, item_uri_value);
+        (*env)->CallVoidMethod(env, bundle, putInt, item_index,
+            ev->type == libvlc_MediaListItemAdded ?
+            ev->u.media_list_item_added.index :
+            ev->u.media_list_item_deleted.index
+        );
+
+        (*env)->DeleteLocalRef(env, item_uri);
+        (*env)->DeleteLocalRef(env, item_uri_value);
+        (*env)->DeleteLocalRef(env, item_index);
+        free(mrl);
+    }
+
     /* Get the object class */
     jclass cls = (*env)->GetObjectClass(env, eventManagerInstance);
     if (!cls) {
@@ -275,9 +306,9 @@ static void vlc_event_callback(const libvlc_event_t *ev, void *data)
     }
 
     /* Find the callback ID */
-    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(II)V");
+    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(ILandroid/os/Bundle;)V");
     if (methodID) {
-        (*env)->CallVoidMethod(env, eventManagerInstance, methodID, ev->type, ev_opt_data);
+        (*env)->CallVoidMethod(env, eventManagerInstance, methodID, ev->type, bundle);
     } else {
         LOGE("EventManager: failed to get the callback method");
     }
@@ -410,6 +441,15 @@ void Java_org_videolan_vlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz, jboolean
         return;
     }
 
+    /* Connect the event manager */
+    libvlc_event_manager_t *ev = libvlc_media_list_event_manager(pointer);
+    static const libvlc_event_type_t mp_events[] = {
+        libvlc_MediaListItemAdded,
+        libvlc_MediaListItemDeleted,
+    };
+    for(int i = 0; i < (sizeof(mp_events) / sizeof(*mp_events)); i++)
+        libvlc_event_attach(ev, mp_events[i], vlc_event_callback, myVm);
+
     setLong(env, thiz, "mMediaListInstance", (jlong)pointer);
 }
 
@@ -467,7 +507,7 @@ void Java_org_videolan_vlc_LibVLC_setEventManager(JNIEnv *env, jobject thiz, job
         return;
     }
 
-    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(II)V");
+    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(ILandroid/os/Bundle;)V");
     if (!methodID) {
         LOGE("setEventManager: failed to get the callback method");
         return;
diff --git a/vlc-android/src/org/videolan/vlc/EventManager.java b/vlc-android/src/org/videolan/vlc/EventManager.java
index b3f959b..d5eb298 100644
--- a/vlc-android/src/org/videolan/vlc/EventManager.java
+++ b/vlc-android/src/org/videolan/vlc/EventManager.java
@@ -59,9 +59,9 @@ public class EventManager {
     //public static final int MediaPlayerLengthChanged        = 0x111;
     public static final int MediaPlayerVout                   = 0x112;
 
-    //public static final int MediaListItemAdded              = 0x200;
+    public static final int MediaListItemAdded                = 0x200;
     //public static final int MediaListWillAddItem            = 0x201;
-    //public static final int MediaListItemDeleted            = 0x202;
+    public static final int MediaListItemDeleted              = 0x202;
     //public static final int MediaListWillDeleteItem         = 0x203;
 
     //public static final int MediaListViewItemAdded          = 0x300;
@@ -112,10 +112,8 @@ public class EventManager {
     }
 
     /** This method is called by a native thread **/
-    public void callback(int event, int optional_data) {
-        Bundle b = new Bundle();
+    public void callback(int event, Bundle b) {
         b.putInt("event", event);
-        b.putInt("data", optional_data);
         for (int i = 0; i < mEventHandler.size(); i++) {
             Message msg = Message.obtain();
             msg.setData(b);
-- 
1.7.5.4



More information about the Android mailing list