Use global variable to pass the event from ca jni to app in android
authorhyuna0213.jo <hyuna0213.jo@samsung.com>
Mon, 15 Aug 2016 23:29:22 +0000 (08:29 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Wed, 24 Aug 2016 10:03:46 +0000 (10:03 +0000)
Use global variable to not create the local variable when we received
event from stack.

Change-Id: I013d916e531568f0668eb1ee850d234591607465
Signed-off-by: hyuna0213.jo <hyuna0213.jo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10467
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: jihwan seo <jihwan.seo@samsung.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
android/android_api/base/jni/JniCaInterface.c

index bf52544..1e20461 100644 (file)
@@ -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