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 "JniOnDPDevicesFoundListener.h"
23 #include "JniOcDirectPairDevice.h"
27 JniOnDPDevicesFoundListener::JniOnDPDevicesFoundListener(JNIEnv *env, jobject jListener,
28 RemoveListenerCallback removeListenerCallback)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
31 m_removeListenerCallback = removeListenerCallback;
34 jobject JniOnDPDevicesFoundListener::convertdpDevVectorToJavaList(JNIEnv *env, PairedDevices DPdevList)
36 jobject jResultList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
42 for (size_t i = 0; i < DPdevList.size(); ++i)
44 JniOcDirectPairDevice *device = new JniOcDirectPairDevice(DPdevList.at(i));
50 jstring jStr = env->NewStringUTF(((DPdevList.at(i))->getDeviceID()).c_str());
55 jobject jresult = env->NewObject(g_cls_OcDirectPairDevice,
56 g_mid_OcDirectPairDevice_dev_ctor,jStr);
62 SetHandle<JniOcDirectPairDevice>(env, jresult, device);
64 env->CallBooleanMethod(jResultList, g_mid_LinkedList_add_object, jresult);
65 if (env->ExceptionCheck())
69 env->DeleteLocalRef(jresult);
70 env->DeleteLocalRef(jStr);
75 JniOnDPDevicesFoundListener::~JniOnDPDevicesFoundListener()
77 LOGI("~JniOnDPDevicesFoundListener()");
81 JNIEnv *env = GetJNIEnv(ret);
86 env->DeleteWeakGlobalRef(m_jwListener);
87 m_jwListener = nullptr;
88 if (JNI_EDETACHED == ret)
90 g_jvm->DetachCurrentThread();
95 void JniOnDPDevicesFoundListener::directPairingDevicesCallback(PairedDevices paringDevicesList, DPFunc func)
98 JNIEnv *env = GetJNIEnv(ret);
104 jobject jListener = env->NewLocalRef(m_jwListener);
107 checkExAndRemoveListener(env);
108 if (JNI_EDETACHED == ret)
110 g_jvm->DetachCurrentThread();
115 jclass clsL = env->GetObjectClass(jListener);
119 checkExAndRemoveListener(env);
120 if (JNI_EDETACHED == ret)
122 g_jvm->DetachCurrentThread();
127 jobject jpairingDevicesList = convertdpDevVectorToJavaList(env, paringDevicesList);
128 std::string calledFunc;
131 case DPFunc::FIND_DIRECT_PAIRED_DEV_LIST:
133 calledFunc = "onFindDirectPairingListener";
136 case DPFunc::GET_PAIRED_DEV_LIST:
138 calledFunc = "onGetDirectPairedListener";
143 checkExAndRemoveListener(env);
144 if (JNI_EDETACHED == ret)
146 g_jvm->DetachCurrentThread();
152 jmethodID midL = env->GetMethodID(clsL, calledFunc.c_str(), "(Ljava/util/List;)V");
156 checkExAndRemoveListener(env);
157 if (JNI_EDETACHED == ret)
159 g_jvm->DetachCurrentThread();
164 env->CallVoidMethod(jListener, midL, jpairingDevicesList);
165 if (env->ExceptionCheck())
167 LOGE("Java exception is thrown");
170 checkExAndRemoveListener(env);
171 if (JNI_EDETACHED == ret)
173 g_jvm->DetachCurrentThread();
177 void JniOnDPDevicesFoundListener::checkExAndRemoveListener(JNIEnv* env)
179 if (env->ExceptionCheck())
181 jthrowable ex = env->ExceptionOccurred();
182 env->ExceptionClear();
183 m_removeListenerCallback(env, m_jwListener);
184 env->Throw((jthrowable)ex);
188 m_removeListenerCallback(env, m_jwListener);