X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=android%2Fandroid_api%2Fbase%2Fjni%2FJniOnDeviceInfoListener.cpp;h=28d8e4403fc4fdb3585dd632f6e6f1c60b227de0;hb=17c68b2fd1e74586f85e552eeab4e32dc121f8a0;hp=483fac6924786b85d73b07cc19d7742e6b2182c6;hpb=8c01dff2c5bc5496f7dc1632c498943ec6ecb015;p=platform%2Fupstream%2Fiotivity.git diff --git a/android/android_api/base/jni/JniOnDeviceInfoListener.cpp b/android/android_api/base/jni/JniOnDeviceInfoListener.cpp index 483fac6..28d8e44 100644 --- a/android/android_api/base/jni/JniOnDeviceInfoListener.cpp +++ b/android/android_api/base/jni/JniOnDeviceInfoListener.cpp @@ -1,113 +1,116 @@ -/* -* //****************************************************************** -* // -* // Copyright 2015 Intel Corporation. -* // -* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -* // -* // Licensed under the Apache License, Version 2.0 (the "License"); -* // you may not use this file except in compliance with the License. -* // You may obtain a copy of the License at -* // -* // http://www.apache.org/licenses/LICENSE-2.0 -* // -* // Unless required by applicable law or agreed to in writing, software -* // distributed under the License is distributed on an "AS IS" BASIS, -* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* // See the License for the specific language governing permissions and -* // limitations under the License. -* // -* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -*/ -#include "JniOnDeviceInfoListener.h" -#include "JniOcRepresentation.h" - -JniOnDeviceInfoListener::JniOnDeviceInfoListener(JNIEnv *env, jobject jListener, - RemoveListenerCallback removeListenerCallback) -{ - m_jwListener = env->NewWeakGlobalRef(jListener); - m_removeListenerCallback = removeListenerCallback; -} - -JniOnDeviceInfoListener::~JniOnDeviceInfoListener() -{ - LOGI("~JniOnDeviceInfoListener"); - if (m_jwListener) - { - jint ret; - JNIEnv *env = GetJNIEnv(ret); - if (NULL == env) return; - env->DeleteWeakGlobalRef(m_jwListener); - if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); - } -} - -void JniOnDeviceInfoListener::foundDeviceCallback(const OC::OCRepresentation& ocRepresentation) -{ - jint ret; - JNIEnv *env = GetJNIEnv(ret); - if (NULL == env) return; - - jobject jListener = env->NewLocalRef(m_jwListener); - if (!jListener) - { - LOGI("Java onDeviceInfoListener object is already destroyed, quiting"); - checkExAndRemoveListener(env); - if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); - return; - } - - OCRepresentation* rep = new OCRepresentation(ocRepresentation); - jlong handle = reinterpret_cast(rep); - jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool, - handle, true); - if (!jRepresentation) - { - delete rep; - checkExAndRemoveListener(env); - if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); - return; - } - - jclass clsL = env->GetObjectClass(jListener); - if (!clsL) - { - delete rep; - checkExAndRemoveListener(env); - if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); - return; - } - jmethodID midL = env->GetMethodID(clsL, "onDeviceFound", "(Lorg/iotivity/base/OcRepresentation;)V"); - if (!midL) - { - delete rep; - checkExAndRemoveListener(env); - if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); - return; - } - - env->CallVoidMethod(jListener, midL, jRepresentation); - if (env->ExceptionCheck()) - { - LOGE("Java exception is thrown"); - delete rep; - checkExAndRemoveListener(env); - } - - if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); -} - -void JniOnDeviceInfoListener::checkExAndRemoveListener(JNIEnv* env) -{ - if (env->ExceptionCheck()) - { - jthrowable ex = env->ExceptionOccurred(); - env->ExceptionClear(); - m_removeListenerCallback(env, m_jwListener); - env->Throw((jthrowable)ex); - } - else - { - m_removeListenerCallback(env, m_jwListener); - } +/* +* //****************************************************************** +* // +* // Copyright 2015 Intel Corporation. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // Licensed under the Apache License, Version 2.0 (the "License"); +* // you may not use this file except in compliance with the License. +* // You may obtain a copy of the License at +* // +* // http://www.apache.org/licenses/LICENSE-2.0 +* // +* // Unless required by applicable law or agreed to in writing, software +* // distributed under the License is distributed on an "AS IS" BASIS, +* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* // See the License for the specific language governing permissions and +* // limitations under the License. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +*/ +#include "JniOnDeviceInfoListener.h" +#include "JniOcRepresentation.h" + +JniOnDeviceInfoListener::JniOnDeviceInfoListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); + m_removeListenerCallback = removeListenerCallback; +} + +JniOnDeviceInfoListener::~JniOnDeviceInfoListener() +{ + LOGI("~JniOnDeviceInfoListener"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == env) return; + + env->DeleteWeakGlobalRef(m_jwListener); + m_jwListener = nullptr; + + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniOnDeviceInfoListener::foundDeviceCallback(const OC::OCRepresentation& ocRepresentation) +{ + jint ret; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == env) return; + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + LOGI("Java onDeviceInfoListener object is already destroyed, quiting"); + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + OCRepresentation* rep = new OCRepresentation(ocRepresentation); + jlong handle = reinterpret_cast(rep); + jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool, + handle, true); + if (!jRepresentation) + { + delete rep; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jclass clsL = env->GetObjectClass(jListener); + if (!clsL) + { + delete rep; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + jmethodID midL = env->GetMethodID(clsL, "onDeviceFound", "(Lorg/iotivity/base/OcRepresentation;)V"); + if (!midL) + { + delete rep; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + env->CallVoidMethod(jListener, midL, jRepresentation); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + delete rep; + checkExAndRemoveListener(env); + } + + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); +} + +void JniOnDeviceInfoListener::checkExAndRemoveListener(JNIEnv* env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_removeListenerCallback(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_removeListenerCallback(env, m_jwListener); + } } \ No newline at end of file