2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "JniOnDirectPairingListener.h"
23 #include "JniOcDirectPairDevice.h"
24 #include "OCDirectPairing.h"
28 JniOnDirectPairingListener::JniOnDirectPairingListener(JNIEnv *env, jobject jListener,
29 RemoveListenerCallback removeListenerCallback)
31 m_jwListener = env->NewWeakGlobalRef(jListener);
32 m_removeListenerCallback = removeListenerCallback;
36 JniOnDirectPairingListener::~JniOnDirectPairingListener()
38 LOGI("~JniOnDirectPairingListener()");
42 JNIEnv *env = GetJNIEnv(ret);
43 if (nullptr == env) return;
44 env->DeleteWeakGlobalRef(m_jwListener);
45 m_jwListener = nullptr;
46 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
50 void JniOnDirectPairingListener::doDirectPairingCB(std::shared_ptr<OC::OCDirectPairing> dpDev,
54 JNIEnv *env = GetJNIEnv(ret);
55 if (nullptr == env) return;
57 jobject jListener = env->NewLocalRef(m_jwListener);
60 checkExAndRemoveListener(env);
61 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
65 jclass clsL = env->GetObjectClass(jListener);
69 checkExAndRemoveListener(env);
70 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
74 JniOcDirectPairDevice *device = new JniOcDirectPairDevice(dpDev);
77 jstring jStr = env->NewStringUTF((dpDev->getDeviceID()).c_str());
80 checkExAndRemoveListener(env);
81 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
85 jobject jresult = env->NewObject(g_cls_OcDirectPairDevice, g_mid_OcDirectPairDevice_ctor);
88 checkExAndRemoveListener(env);
89 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
93 SetHandle<JniOcDirectPairDevice>(env, jresult, device);
95 jint jres = static_cast<jint>(result);
97 jmethodID midL = env->GetMethodID(clsL, "onDirectPairingListener", "(Ljava/lang/String;I)V");
101 checkExAndRemoveListener(env);
102 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
106 env->CallVoidMethod(jListener, midL, jStr, jres);
107 if (env->ExceptionCheck())
109 LOGE("Java exception is thrown");
112 checkExAndRemoveListener(env);
113 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
116 void JniOnDirectPairingListener::checkExAndRemoveListener(JNIEnv* env)
118 if (env->ExceptionCheck())
120 jthrowable ex = env->ExceptionOccurred();
121 env->ExceptionClear();
122 m_removeListenerCallback(env, m_jwListener);
123 env->Throw((jthrowable)ex);
127 m_removeListenerCallback(env, m_jwListener);