fixed warning for Android build
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnDPDevicesFoundListener.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
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
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
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.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22 #include "JniOnDPDevicesFoundListener.h"
23 #include "JniOcDirectPairDevice.h"
24 #include "OCApi.h"
25 using namespace OC;
26
27 JniOnDPDevicesFoundListener::JniOnDPDevicesFoundListener(JNIEnv *env, jobject jListener,
28     RemoveListenerCallback removeListenerCallback)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31     m_removeListenerCallback = removeListenerCallback;
32 }
33
34 jobject JniOnDPDevicesFoundListener::convertdpDevVectorToJavaList(JNIEnv *env, PairedDevices DPdevList)
35 {
36     jobject jResultList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
37     if (!jResultList)
38     {
39         return nullptr;
40     }
41
42     for (size_t i = 0; i < DPdevList.size(); ++i)
43     {
44         JniOcDirectPairDevice *device = new JniOcDirectPairDevice(DPdevList.at(i));
45         if (!device)
46         {
47             return nullptr;
48         }
49
50         jstring jStr = env->NewStringUTF(((DPdevList.at(i))->getDeviceID()).c_str());
51         if (!jStr)
52         {
53             return nullptr;
54         }
55         jobject jresult = env->NewObject(g_cls_OcDirectPairDevice,
56                 g_mid_OcDirectPairDevice_dev_ctor,jStr);
57         if (!jresult)
58         {
59             return nullptr;
60         }
61
62         SetHandle<JniOcDirectPairDevice>(env, jresult, device);
63
64         env->CallBooleanMethod(jResultList, g_mid_LinkedList_add_object, jresult);
65         if (env->ExceptionCheck())
66         {
67             return nullptr;
68         }
69         env->DeleteLocalRef(jresult);
70         env->DeleteLocalRef(jStr);
71     }
72     return jResultList;
73 }
74
75 JniOnDPDevicesFoundListener::~JniOnDPDevicesFoundListener()
76 {
77     LOGI("~JniOnDPDevicesFoundListener()");
78     if (m_jwListener)
79     {
80         jint ret = JNI_ERR;
81         JNIEnv *env = GetJNIEnv(ret);
82         if (nullptr == env)
83         {
84             return;
85         }
86         env->DeleteWeakGlobalRef(m_jwListener);
87         m_jwListener = nullptr;
88         if (JNI_EDETACHED == ret)
89         {
90             g_jvm->DetachCurrentThread();
91         }
92     }
93 }
94
95 void JniOnDPDevicesFoundListener::directPairingDevicesCallback(PairedDevices paringDevicesList, DPFunc func)
96 {
97     jint ret = JNI_ERR;
98     JNIEnv *env = GetJNIEnv(ret);
99     if (nullptr == env)
100     {
101         return;
102     }
103
104     jobject jListener = env->NewLocalRef(m_jwListener);
105     if (!jListener)
106     {
107         checkExAndRemoveListener(env);
108         if (JNI_EDETACHED == ret)
109         {
110             g_jvm->DetachCurrentThread();
111         }
112         return;
113     }
114
115     jclass clsL = env->GetObjectClass(jListener);
116
117     if (!clsL)
118     {
119         checkExAndRemoveListener(env);
120         if (JNI_EDETACHED == ret)
121         {
122             g_jvm->DetachCurrentThread();
123         }
124         return;
125     }
126
127     jobject jpairingDevicesList = convertdpDevVectorToJavaList(env, paringDevicesList);
128     std::string calledFunc;
129     switch (func)
130     {
131         case DPFunc::FIND_DIRECT_PAIRED_DEV_LIST:
132             {
133                 calledFunc = "onFindDirectPairingListener";
134             }
135             break;
136         case DPFunc::GET_PAIRED_DEV_LIST:
137             {
138                 calledFunc = "onGetDirectPairedListener";
139             }
140             break;
141         default:
142             {
143                 checkExAndRemoveListener(env);
144                 if (JNI_EDETACHED == ret)
145                 {
146                     g_jvm->DetachCurrentThread();
147                 }
148             }
149             return;
150     }
151
152     jmethodID midL = env->GetMethodID(clsL, calledFunc.c_str(), "(Ljava/util/List;)V");
153
154     if (!midL)
155     {
156         checkExAndRemoveListener(env);
157         if (JNI_EDETACHED == ret)
158         {
159             g_jvm->DetachCurrentThread();
160         }
161         return;
162     }
163
164     env->CallVoidMethod(jListener, midL, jpairingDevicesList);
165     if (env->ExceptionCheck())
166     {
167         LOGE("Java exception is thrown");
168     }
169
170     checkExAndRemoveListener(env);
171     if (JNI_EDETACHED == ret)
172     {
173         g_jvm->DetachCurrentThread();
174     }
175 }
176
177 void JniOnDPDevicesFoundListener::checkExAndRemoveListener(JNIEnv* env)
178 {
179     if (env->ExceptionCheck())
180     {
181         jthrowable ex = env->ExceptionOccurred();
182         env->ExceptionClear();
183         m_removeListenerCallback(env, m_jwListener);
184         env->Throw((jthrowable)ex);
185     }
186     else
187     {
188         m_removeListenerCallback(env, m_jwListener);
189     }
190 }