[Android] jni: Use bundles for event passing
Edward Wang
git at videolan.org
Tue Aug 28 15:33:01 CEST 2012
vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Mon Aug 27 16:20:04 2012 -0400| [ab3b25a9c633f7f6ff71b95293495db4c919591d] | committer: Edward Wang
jni: Use bundles for event passing
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=ab3b25a9c633f7f6ff71b95293495db4c919591d
---
vlc-android/jni/libvlcjni.c | 30 ++++++++++++++------
vlc-android/src/org/videolan/vlc/EventManager.java | 4 +--
2 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index d6b3423..1d49ea8 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,25 @@ static void vlc_event_callback(const libvlc_event_t *ev, void *data)
isAttached = true;
}
+ /* Creating the bundle in C allows us to subscribe to more events
+ * and get better flexibility for each event. For example, we can
+ * have totally different types of data for each event, instead of,
+ * for example, only an integer and/or string.
+ */
+ 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);
+ }
+
/* Get the object class */
jclass cls = (*env)->GetObjectClass(env, eventManagerInstance);
if (!cls) {
@@ -275,9 +289,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");
}
@@ -467,7 +481,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..cbb9184 100644
--- a/vlc-android/src/org/videolan/vlc/EventManager.java
+++ b/vlc-android/src/org/videolan/vlc/EventManager.java
@@ -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);
More information about the Android
mailing list