add JNI methods related to group&invite
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnPostListener.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
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 "JniOnPostListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
25 #include "JniUtils.h"
26 #ifdef WITH_CLOUD
27 #include "JniOcAccountManager.h"
28 #endif
29
30 JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
31     : m_ownerResource(owner)
32 {
33     m_jwListener = env->NewWeakGlobalRef(jListener);
34 #ifdef WITH_CLOUD
35     m_ownerAccountManager = nullptr;
36 #endif
37 }
38
39 #ifdef WITH_CLOUD
40 JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
41     : m_ownerAccountManager(owner)
42 {
43     m_jwListener = env->NewWeakGlobalRef(jListener);
44     m_ownerResource = nullptr;
45 }
46 #endif
47
48 JniOnPostListener::~JniOnPostListener()
49 {
50     if (m_jwListener)
51     {
52         jint ret = JNI_ERR;
53         JNIEnv *env = GetJNIEnv(ret);
54         if (nullptr == env)
55         {
56             return;
57         }
58
59         env->DeleteWeakGlobalRef(m_jwListener);
60         m_jwListener = nullptr;
61
62         if (JNI_EDETACHED == ret)
63         {
64             g_jvm->DetachCurrentThread();
65         }
66     }
67 }
68
69 void JniOnPostListener::onPostCallback(const HeaderOptions& headerOptions,
70     const OCRepresentation& ocRepresentation, const int eCode)
71 {
72     jint envRet = JNI_ERR;
73     JNIEnv *env = GetJNIEnv(envRet);
74     if (nullptr == env)
75     {
76         return;
77     }
78
79     jobject jListener = env->NewLocalRef(m_jwListener);
80     if (!jListener)
81     {
82         checkExAndRemoveListener(env);
83         if (JNI_EDETACHED == envRet)
84         {
85             g_jvm->DetachCurrentThread();
86         }
87         return;
88     }
89     jclass clsL = env->GetObjectClass(jListener);
90     if (!clsL)
91     {
92         checkExAndRemoveListener(env);
93         if (JNI_EDETACHED == envRet)
94         {
95             g_jvm->DetachCurrentThread();
96         }
97         return;
98     }
99
100     if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode &&
101             OC_STACK_RESOURCE_CHANGED != eCode)
102     {
103         jobject ex = GetOcException(eCode, "stack error in onPostCallback");
104         if (!ex)
105         {
106             checkExAndRemoveListener(env);
107             if (JNI_EDETACHED == envRet)
108             {
109                 g_jvm->DetachCurrentThread();
110             }
111             return;
112         }
113         jmethodID midL = env->GetMethodID(clsL, "onPostFailed", "(Ljava/lang/Throwable;)V");
114         if (!midL)
115         {
116             checkExAndRemoveListener(env);
117             if (JNI_EDETACHED == envRet)
118             {
119                 g_jvm->DetachCurrentThread();
120             }
121             return;
122         }
123         env->CallVoidMethod(jListener, midL, ex);
124     }
125     else
126     {
127         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
128         if (!jHeaderOptionList)
129         {
130             checkExAndRemoveListener(env);
131             if (JNI_EDETACHED == envRet)
132             {
133                 g_jvm->DetachCurrentThread();
134             }
135             return;
136         }
137
138         OCRepresentation * rep = new OCRepresentation(ocRepresentation);
139         jlong handle = reinterpret_cast<jlong>(rep);
140         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
141             handle, true);
142         if (!jRepresentation)
143         {
144             delete rep;
145             checkExAndRemoveListener(env);
146             if (JNI_EDETACHED == envRet)
147             {
148                 g_jvm->DetachCurrentThread();
149             }
150             return;
151         }
152
153         jmethodID midL = env->GetMethodID(clsL, "onPostCompleted",
154             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;)V");
155         if (!midL)
156         {
157             checkExAndRemoveListener(env);
158             if (JNI_EDETACHED == envRet)
159             {
160                 g_jvm->DetachCurrentThread();
161             }
162             return;
163         }
164
165         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation);
166         if (env->ExceptionCheck())
167         {
168             LOGE("Java exception is thrown");
169             delete rep;
170         }
171     }
172
173     checkExAndRemoveListener(env);
174     if (JNI_EDETACHED == envRet)
175     {
176         g_jvm->DetachCurrentThread();
177     }
178 }
179
180 void JniOnPostListener::checkExAndRemoveListener(JNIEnv* env)
181 {
182     if (env->ExceptionCheck())
183     {
184         jthrowable ex = env->ExceptionOccurred();
185         env->ExceptionClear();
186 #ifndef WITH_CLOUD
187         m_ownerResource->removeOnPostListener(env, m_jwListener);
188 #else
189         if (nullptr != m_ownerResource)
190         {
191             m_ownerResource->removeOnPostListener(env, m_jwListener);
192         }
193         if (nullptr != m_ownerAccountManager)
194         {
195             m_ownerAccountManager->removeOnPostListener(env, m_jwListener);
196         }
197 #endif
198         env->Throw((jthrowable)ex);
199     }
200     else
201     {
202 #ifndef WITH_CLOUD
203         m_ownerResource->removeOnPostListener(env, m_jwListener);
204 #else
205         if (nullptr != m_ownerResource)
206         {
207             m_ownerResource->removeOnPostListener(env, m_jwListener);
208         }
209         if (nullptr != m_ownerAccountManager)
210         {
211             m_ownerAccountManager->removeOnPostListener(env, m_jwListener);
212         }
213 #endif
214     }
215 }