Merge tizen_5.0 codes into tizen_4.0
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnObserveListener.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 "JniOnObserveListener.h"
23 #include "JniOcResource.h"
24 #include "JniOcRepresentation.h"
25 #include "JniUtils.h"
26
27 #define CA_OBSERVE_MAX_SEQUENCE_NUMBER 0xFFFFFF
28
29 JniOnObserveListener::JniOnObserveListener(JNIEnv *env, jobject jListener, RemoveListenerCallback removeListener)
30     : m_removeListener(removeListener)
31 {
32     m_jwListener = env->NewWeakGlobalRef(jListener);
33 }
34
35 JniOnObserveListener::~JniOnObserveListener()
36 {
37     if (m_jwListener)
38     {
39         jint ret = JNI_ERR;
40         JNIEnv *env = GetJNIEnv(ret);
41         if (nullptr == env)
42         {
43             return;
44         }
45
46         env->DeleteWeakGlobalRef(m_jwListener);
47         m_jwListener = nullptr;
48
49         if (JNI_EDETACHED == ret)
50         {
51             g_jvm->DetachCurrentThread();
52         }
53     }
54 }
55
56 void JniOnObserveListener::onObserveCallback(const HeaderOptions headerOptions,
57     const OCRepresentation& ocRepresentation, const int& eCode, const int& sequenceNumber)
58 {
59     jint envRet = JNI_ERR;
60     JNIEnv *env = GetJNIEnv(envRet);
61     if (nullptr == env)
62     {
63         return;
64     }
65
66     if (nullptr == m_jwListener)
67     {
68         LOGE("listener is not available");
69         if (JNI_EDETACHED == envRet)
70         {
71             g_jvm->DetachCurrentThread();
72         }
73         return;
74     }
75
76     jobject jListener = env->NewLocalRef(m_jwListener);
77     if (!jListener)
78     {
79         checkExAndRemoveListener(env);
80         if (JNI_EDETACHED == envRet)
81         {
82             g_jvm->DetachCurrentThread();
83         }
84         return;
85     }
86
87     jclass clsL = env->GetObjectClass(jListener);
88     if (!clsL)
89     {
90         env->DeleteLocalRef(jListener);
91         checkExAndRemoveListener(env);
92         if (JNI_EDETACHED == envRet)
93         {
94             g_jvm->DetachCurrentThread();
95         }
96         return;
97     }
98
99     if (OC_STACK_OK != eCode && OC_STACK_RESOURCE_CREATED != eCode &&
100             OC_STACK_RESOURCE_DELETED != eCode && OC_STACK_RESOURCE_CHANGED != eCode)
101     {
102         jobject ex = GetOcException(eCode, "stack error in onObserveCallback");
103         if (!ex)
104         {
105             goto JNI_EXIT;
106         }
107
108         jmethodID midL = env->GetMethodID(clsL, "onObserveFailed", "(Ljava/lang/Throwable;)V");
109         if (!midL)
110         {
111             env->DeleteLocalRef(ex);
112             goto JNI_EXIT;
113         }
114         env->CallVoidMethod(jListener, midL, ex);
115     }
116     else
117     {
118         jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
119         if (!jHeaderOptionList)
120         {
121             goto JNI_EXIT;
122         }
123
124         OCRepresentation * rep = new OCRepresentation(ocRepresentation);
125         jlong handle = reinterpret_cast<jlong>(rep);
126         jobject jRepresentation = env->NewObject(g_cls_OcRepresentation,
127             g_mid_OcRepresentation_N_ctor_bool, handle, true);
128         if (!jRepresentation)
129         {
130             delete rep;
131             env->DeleteLocalRef(jHeaderOptionList);
132             goto JNI_EXIT;
133         }
134
135         jmethodID midL = env->GetMethodID(clsL, "onObserveCompleted",
136             "(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;I)V");
137         if (!midL)
138         {
139             env->DeleteLocalRef(jRepresentation);
140             env->DeleteLocalRef(jHeaderOptionList);
141             goto JNI_EXIT;
142         }
143
144         env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation,
145             static_cast<jint>(sequenceNumber));
146         if (env->ExceptionCheck())
147         {
148             LOGE("Java exception is thrown");
149             delete rep;
150             env->DeleteLocalRef(jRepresentation);
151             env->DeleteLocalRef(jHeaderOptionList);
152             jthrowable ex = env->ExceptionOccurred();
153             env->ExceptionClear();
154             m_removeListener(env, m_jwListener);
155             env->Throw((jthrowable)ex);
156         }
157
158         if (CA_OBSERVE_MAX_SEQUENCE_NUMBER + 1 == sequenceNumber)
159         {
160             LOGI("Observe De-registration action is successful");
161             goto JNI_EXIT;
162         }
163     }
164
165     env->DeleteLocalRef(clsL);
166     env->DeleteLocalRef(jListener);
167     if (JNI_EDETACHED == envRet)
168     {
169         g_jvm->DetachCurrentThread();
170     }
171     return;
172
173 JNI_EXIT:
174     env->DeleteLocalRef(clsL);
175     env->DeleteLocalRef(jListener);
176     checkExAndRemoveListener(env);
177     if (JNI_EDETACHED == envRet)
178     {
179         g_jvm->DetachCurrentThread();
180     }
181 }
182
183 void JniOnObserveListener::checkExAndRemoveListener(JNIEnv* env)
184 {
185     LOGI("checkExAndRemoveListener");
186     if (env->ExceptionCheck())
187     {
188         jthrowable ex = env->ExceptionOccurred();
189         env->ExceptionClear();
190         m_removeListener(env, m_jwListener);
191         env->Throw((jthrowable)ex);
192     }
193     else
194     {
195         m_removeListener(env, m_jwListener);
196     }
197 }
198
199 jweak JniOnObserveListener::getJWListener()
200 {
201     return this->m_jwListener;
202 }