Added to call unregister for Android IP
authorjihwan.seo <jihwan.seo@samsung.com>
Mon, 9 Nov 2015 11:55:17 +0000 (20:55 +0900)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 11 Nov 2015 02:21:20 +0000 (02:21 +0000)
Change-Id: I7e6ae3220955ae0314ce03807530f5cdbb912166
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4095
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Naga Ashok Jampani <jn.ashok@samsung.com>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java
resource/csdk/connectivity/samples/android/casample/cAInterface/src/main/java/org/iotivity/ca/CaIpInterface.java
resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c

index a011dfe..81ab7d5 100644 (file)
@@ -66,6 +66,10 @@ public class CaIpInterface {
         mContext.registerReceiver(mReceiver, intentFilter);\r
     }\r
 \r
+    public static void destroyIpInterface() {\r
+        mContext.unregisterReceiver(mReceiver);\r
+    }\r
+\r
     private static BroadcastReceiver mReceiver = new BroadcastReceiver() {\r
         @Override\r
         public void onReceive(Context context, Intent intent) {\r
index cbe0ec0..1b85af5 100755 (executable)
@@ -44,6 +44,10 @@ public class CaIpInterface {
         mContext.registerReceiver(mReceiver, intentFilter);
     }
 
+    public static void destroyIpInterface() {
+        mContext.unregisterReceiver(mReceiver);
+    }
+
     private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
index 544f561..7df7585 100755 (executable)
@@ -46,6 +46,12 @@ static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
 
 CAResult_t CAIPJniInit();
 
+/**
+ * destroy JNI interface.
+ * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
+ */
+static CAResult_t CAIPDestroyJniInterface();
+
 #define MAX_INTERFACE_INFO_LENGTH 1024 // allows 32 interfaces from SIOCGIFCONF
 
 CAResult_t CAIPStartNetworkMonitor()
@@ -55,7 +61,7 @@ CAResult_t CAIPStartNetworkMonitor()
 
 CAResult_t CAIPStopNetworkMonitor()
 {
-    return CA_STATUS_OK;
+    return CAIPDestroyJniInterface();
 }
 
 int CAGetPollingInterval(int interval)
@@ -361,6 +367,78 @@ CAResult_t CAIPJniInit()
     return CA_STATUS_OK;
 }
 
+static CAResult_t CAIPDestroyJniInterface()
+{
+    OIC_LOG(DEBUG, TAG, "CAIPDestroyJniInterface");
+
+    JavaVM *jvm = CANativeJNIGetJavaVM();
+    if (!jvm)
+    {
+        OIC_LOG(ERROR, TAG, "Could not get JavaVM pointer");
+        return CA_STATUS_FAILED;
+    }
+
+    bool isAttached = false;
+    JNIEnv* env;
+    jint res = (*jvm)->GetEnv(jvm, (void**) &env, JNI_VERSION_1_6);
+    if (JNI_OK != res)
+    {
+        OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
+        res = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
+
+        if (JNI_OK != res)
+        {
+            OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
+            return CA_STATUS_FAILED;
+        }
+        isAttached = true;
+    }
+
+    jclass jni_IpInterface = (*env)->FindClass(env, "org/iotivity/ca/CaIpInterface");
+    if (!jni_IpInterface)
+    {
+        OIC_LOG(ERROR, TAG, "Could not get CaIpInterface class");
+        goto error_exit;
+    }
+
+    jmethodID jni_InterfaceDestroyMethod = (*env)->GetStaticMethodID(env, jni_IpInterface,
+                                                                     "destroyIpInterface",
+                                                                     "()V");
+    if (!jni_InterfaceDestroyMethod)
+    {
+        OIC_LOG(ERROR, TAG, "Could not get CaIpInterface destroy method");
+        goto error_exit;
+    }
+
+    (*env)->CallStaticVoidMethod(env, jni_IpInterface, jni_InterfaceDestroyMethod);
+
+    if ((*env)->ExceptionCheck(env))
+    {
+        OIC_LOG(ERROR, TAG, "destroyIpInterface has failed");
+        (*env)->ExceptionDescribe(env);
+        (*env)->ExceptionClear(env);
+        goto error_exit;
+    }
+
+    OIC_LOG(DEBUG, TAG, "Destroy instance for CaIpInterface");
+
+    if (isAttached)
+    {
+        (*jvm)->DetachCurrentThread(jvm);
+    }
+
+    return CA_STATUS_OK;
+
+error_exit:
+
+    if (isAttached)
+    {
+        (*jvm)->DetachCurrentThread(jvm);
+    }
+
+    return CA_STATUS_FAILED;
+}
+
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_CaIpInterface_caIpStateEnabled(JNIEnv *env, jclass class)
 {