Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnDirectPairingListener.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 "JniOnDirectPairingListener.h"
23 #include "JniOcDirectPairDevice.h"
24 #include "OCDirectPairing.h"
25 #include "OCApi.h"
26 using namespace OC;
27
28 JniOnDirectPairingListener::JniOnDirectPairingListener(JNIEnv *env, jobject jListener,
29     RemoveListenerCallback removeListenerCallback)
30 {
31     m_jwListener = env->NewWeakGlobalRef(jListener);
32     m_removeListenerCallback = removeListenerCallback;
33 }
34
35
36 JniOnDirectPairingListener::~JniOnDirectPairingListener()
37 {
38     LOGI("~JniOnDirectPairingListener()");
39     if (m_jwListener)
40     {
41         jint ret = JNI_ERR;
42         JNIEnv *env = GetJNIEnv(ret);
43         if (nullptr == env)
44         {
45             return;
46         }
47         env->DeleteWeakGlobalRef(m_jwListener);
48         m_jwListener = nullptr;
49         if (JNI_EDETACHED == ret)
50         {
51             g_jvm->DetachCurrentThread();
52         }
53     }
54 }
55
56 void JniOnDirectPairingListener::doDirectPairingCB(std::shared_ptr<OC::OCDirectPairing> dpDev,
57                                                                     OCStackResult result)
58 {
59     jint ret = JNI_ERR;
60     JNIEnv *env = GetJNIEnv(ret);
61     if (nullptr == env)
62     {
63         return;
64     }
65
66     jobject jListener = env->NewLocalRef(m_jwListener);
67     if (!jListener)
68     {
69         checkExAndRemoveListener(env);
70         if (JNI_EDETACHED == ret)
71         {
72             g_jvm->DetachCurrentThread();
73         }
74         return;
75     }
76
77     jclass clsL = env->GetObjectClass(jListener);
78     if (!clsL)
79     {
80         checkExAndRemoveListener(env);
81         if (JNI_EDETACHED == ret)
82         {
83             g_jvm->DetachCurrentThread();
84         }
85         return;
86     }
87
88     JniOcDirectPairDevice *device = new JniOcDirectPairDevice(dpDev);
89     if (!device)
90     {
91         return;
92     }
93
94     jstring jStr = env->NewStringUTF((dpDev->getDeviceID()).c_str());
95     if (!jStr)
96     {
97         checkExAndRemoveListener(env);
98         if (JNI_EDETACHED == ret)
99         {
100             g_jvm->DetachCurrentThread();
101         }
102         return;
103     }
104
105     jobject jresult = env->NewObject(g_cls_OcDirectPairDevice, g_mid_OcDirectPairDevice_ctor);
106     if (!jresult)
107     {
108         checkExAndRemoveListener(env);
109         if (JNI_EDETACHED == ret)
110         {
111             g_jvm->DetachCurrentThread();
112         }
113         return;
114     }
115
116     SetHandle<JniOcDirectPairDevice>(env, jresult, device);
117
118     jint jres = static_cast<jint>(result);
119
120     jmethodID midL = env->GetMethodID(clsL, "onDirectPairingListener", "(Ljava/lang/String;I)V");
121
122     if (!midL)
123     {
124         checkExAndRemoveListener(env);
125         if (JNI_EDETACHED == ret)
126         {
127             g_jvm->DetachCurrentThread();
128         }
129         return;
130     }
131
132     env->CallVoidMethod(jListener, midL, jStr, jres);
133     if (env->ExceptionCheck())
134     {
135         LOGE("Java exception is thrown");
136     }
137
138     checkExAndRemoveListener(env);
139
140     if (JNI_EDETACHED == ret)
141     {
142         g_jvm->DetachCurrentThread();
143     }
144 }
145
146 void JniOnDirectPairingListener::checkExAndRemoveListener(JNIEnv* env)
147 {
148     if (env->ExceptionCheck())
149     {
150         jthrowable ex = env->ExceptionOccurred();
151         env->ExceptionClear();
152         m_removeListenerCallback(env, m_jwListener);
153         env->Throw((jthrowable)ex);
154     }
155     else
156     {
157         m_removeListenerCallback(env, m_jwListener);
158     }
159 }