[Android] libvlcjni: refactor event handler code

Edward Wang git at videolan.org
Tue Aug 27 20:24:43 CEST 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Tue Aug 27 20:18:09 2013 +0200| [1044f6a535f7c982394fd5d396799c56600b1243] | committer: Edward Wang

libvlcjni: refactor event handler code

To prevent code duplication for use in another class soon

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=1044f6a535f7c982394fd5d396799c56600b1243
---

 vlc-android/jni/libvlcjni-util.c |   17 +++++++++++++++++
 vlc-android/jni/libvlcjni.c      |   14 +-------------
 vlc-android/jni/utils.h          |    2 ++
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/vlc-android/jni/libvlcjni-util.c b/vlc-android/jni/libvlcjni-util.c
index 37e880b..3d86102 100644
--- a/vlc-android/jni/libvlcjni-util.c
+++ b/vlc-android/jni/libvlcjni-util.c
@@ -115,6 +115,23 @@ void setString(JNIEnv *env, jobject item, const char* field, const char* text) {
     (*env)->SetObjectField(env, item, fieldId, jstr);
 }
 
+jobject getEventHandlerReference(JNIEnv *env, jobject thiz, jobject eventHandler)
+{
+    jclass cls = (*env)->GetObjectClass(env, eventHandler);
+    if (!cls) {
+        LOGE("setEventHandler: failed to get class reference");
+        return NULL;
+    }
+
+    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(ILandroid/os/Bundle;)V");
+    if (!methodID) {
+        LOGE("setEventHandler: failed to get the callback method");
+        return NULL;
+    }
+
+    return (*env)->NewGlobalRef(env, eventHandler);
+}
+
 static void debug_buffer_log(void *data, int level, const char *fmt, va_list ap)
 {
     bool isAttached = false;
diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index cada5e4..cddb5ce 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -326,19 +326,7 @@ void Java_org_videolan_libvlc_LibVLC_setEventHandler(JNIEnv *env, jobject thiz,
         eventHandlerInstance = NULL;
     }
 
-    jclass cls = (*env)->GetObjectClass(env, eventHandler);
-    if (!cls) {
-        LOGE("setEventHandler: failed to get class reference");
-        return;
-    }
-
-    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(ILandroid/os/Bundle;)V");
-    if (!methodID) {
-        LOGE("setEventHandler: failed to get the callback method");
-        return;
-    }
-
-    eventHandlerInstance = (*env)->NewGlobalRef(env, eventHandler);
+    eventHandlerInstance = getEventHandlerReference(env, thiz, eventHandler);
 }
 
 static void create_player_and_play(JNIEnv* env, jobject thiz,
diff --git a/vlc-android/jni/utils.h b/vlc-android/jni/utils.h
index f5a8c00..43ffd82 100644
--- a/vlc-android/jni/utils.h
+++ b/vlc-android/jni/utils.h
@@ -39,6 +39,8 @@ void setFloat(JNIEnv *env, jobject item, const char* field, jfloat value);
 
 void setString(JNIEnv *env, jobject item, const char* field, const char* text);
 
+jobject getEventHandlerReference(JNIEnv *env, jobject thiz, jobject eventHandler);
+
 void debug_log(void *data, int level, const libvlc_log_t *ctx, const char *fmt, va_list ap);
 
 #endif // LIBVLCJNI_UTILS_H



More information about the Android mailing list