From 116f34ce2cc145cf0f8e30c7c9c07a55ae4a44f6 Mon Sep 17 00:00:00 2001 From: "hyuna0213.jo" Date: Tue, 16 Aug 2016 08:29:22 +0900 Subject: [PATCH] Use global variable to pass the event from ca jni to app in android Use global variable to not create the local variable when we received event from stack. Change-Id: I013d916e531568f0668eb1ee850d234591607465 Signed-off-by: hyuna0213.jo Reviewed-on: https://gerrit.iotivity.org/gerrit/10467 Tested-by: jenkins-iotivity Reviewed-by: jihwan seo Reviewed-by: Ashok Babu Channa --- android/android_api/base/jni/JniCaInterface.c | 79 +++++++++++++++------------ 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/android/android_api/base/jni/JniCaInterface.c b/android/android_api/base/jni/JniCaInterface.c index bf52544..1e20461 100644 --- a/android/android_api/base/jni/JniCaInterface.c +++ b/android/android_api/base/jni/JniCaInterface.c @@ -35,6 +35,8 @@ static jobject g_foundDeviceListenerObject = NULL; static jobject g_listenerObject = NULL; static JavaVM *g_jvm = NULL; +static jclass g_jni_cls_enum = NULL; +static jmethodID g_jni_mid_enum = NULL; JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved) { @@ -118,27 +120,15 @@ void CAManagerConnectionStateChangedCB(const CAEndpoint_t *info, goto exit_error; } - jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType"); - if (!jni_cls_enum) + if (g_jni_cls_enum && g_jni_mid_enum) { - LOGE("could not get jni_cls_enum"); - goto exit_error; + jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum, + g_jni_mid_enum, info->adapter); + (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener, + jni_adaptertype, jni_address, + (jboolean)connected); } - jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance", - "(I)Lorg/iotivity/base/OcConnectivityType;"); - if (!jni_mid_enum) - { - LOGE("could not get Method ID (getInstance)"); - goto exit_error; - } - - jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum, - jni_mid_enum, info->adapter); - (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener, - jni_adaptertype, jni_address, - (jboolean)connected); - exit_error: if (isAttached) { @@ -191,27 +181,15 @@ void CAManagerAdapterStateChangedCB(CATransportAdapter_t adapter, bool enabled) goto exit_error; } - jclass jni_cls_enum = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType"); - if (!jni_cls_enum) + if (g_jni_cls_enum && g_jni_mid_enum) { - LOGE("could not get jni_cls_enum"); - goto exit_error; - } + jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, g_jni_cls_enum, + g_jni_mid_enum, adapter); - jmethodID jni_mid_enum = (*env)->GetStaticMethodID(env, jni_cls_enum, "getInstance", - "(I)Lorg/iotivity/base/OcConnectivityType;"); - if (!jni_mid_enum) - { - LOGE("could not get Method ID (getInstance)"); - goto exit_error; + (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener, + jni_adaptertype, (jboolean)enabled); } - jobject jni_adaptertype = (*env)->CallStaticObjectMethod(env, jni_cls_enum, - jni_mid_enum, adapter); - - (*env)->CallVoidMethod(env, g_listenerObject, jni_mid_listener, - jni_adaptertype, (jboolean)enabled); - exit_error: if (isAttached) { @@ -228,8 +206,25 @@ Java_org_iotivity_ca_CaInterface_caManagerInitialize(JNIEnv *env, jclass clazz, CAUtilClientInitialize(env, g_jvm, context); - g_listenerObject = (*env)->NewGlobalRef(env, listener); + if (listener) + { + g_listenerObject = (*env)->NewGlobalRef(env, listener); + } + + if (g_listenerObject) + { + jclass cls = (*env)->FindClass(env, "org/iotivity/base/OcConnectivityType"); + if (cls) + { + g_jni_cls_enum = (jclass)(*env)->NewGlobalRef(env, cls); + } + if (g_jni_cls_enum) + { + g_jni_mid_enum = (*env)->GetStaticMethodID(env, g_jni_cls_enum, "getInstance", + "(I)Lorg/iotivity/base/OcConnectivityType;"); + } + } CARegisterNetworkMonitorHandler(CAManagerAdapterStateChangedCB, CAManagerConnectionStateChangedCB); } @@ -246,6 +241,18 @@ Java_org_iotivity_ca_CaInterface_caManagerTerminate(JNIEnv *env, jclass clazz) (*env)->DeleteGlobalRef(env, g_listenerObject); g_listenerObject = NULL; } + + if (g_jni_cls_enum) + { + (*env)->DeleteGlobalRef(env, g_jni_cls_enum); + g_jni_cls_enum = NULL; + } + + if (g_jni_mid_enum) + { + (*env)->DeleteGlobalRef(env, g_jni_mid_enum); + g_jni_mid_enum = NULL; + } } JNIEXPORT void JNICALL -- 2.7.4