Imported Upstream version 1.1.1
[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;
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();
47     }
48 }
49
50 void JniOnDirectPairingListener::doDirectPairingCB(std::shared_ptr<OC::OCDirectPairing> dpDev,
51                                                                     OCStackResult result)
52 {
53     jint ret;
54     JNIEnv *env = GetJNIEnv(ret);
55     if (nullptr == env) return;
56
57     jobject jListener = env->NewLocalRef(m_jwListener);
58     if (!jListener)
59     {
60         checkExAndRemoveListener(env);
61         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
62         return;
63     }
64
65     jclass clsL = env->GetObjectClass(jListener);
66
67     if (!clsL)
68     {
69         checkExAndRemoveListener(env);
70         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
71         return;
72     }
73
74     JniOcDirectPairDevice *device = new JniOcDirectPairDevice(dpDev);
75     if (!device) return;
76
77     jstring jStr = env->NewStringUTF((dpDev->getDeviceID()).c_str());
78     if (!jStr)
79     {
80         checkExAndRemoveListener(env);
81         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
82         return ;
83     }
84
85     jobject jresult = env->NewObject(g_cls_OcDirectPairDevice, g_mid_OcDirectPairDevice_ctor);
86     if (!jresult)
87     {
88         checkExAndRemoveListener(env);
89         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
90         return ;
91     }
92
93     SetHandle<JniOcDirectPairDevice>(env, jresult, device);
94
95     jint jres = static_cast<jint>(result);
96
97     jmethodID midL = env->GetMethodID(clsL, "onDirectPairingListener", "(Ljava/lang/String;I)V");
98
99     if (!midL)
100     {
101         checkExAndRemoveListener(env);
102         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
103         return;
104     }
105
106     env->CallVoidMethod(jListener, midL, jStr, jres);
107     if (env->ExceptionCheck())
108     {
109         LOGE("Java exception is thrown");
110     }
111
112     checkExAndRemoveListener(env);
113     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
114 }
115
116 void JniOnDirectPairingListener::checkExAndRemoveListener(JNIEnv* env)
117 {
118     if (env->ExceptionCheck())
119     {
120         jthrowable ex = env->ExceptionOccurred();
121         env->ExceptionClear();
122         m_removeListenerCallback(env, m_jwListener);
123         env->Throw((jthrowable)ex);
124     }
125     else
126     {
127         m_removeListenerCallback(env, m_jwListener);
128     }
129 }