replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniKeepAliveListener.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2017 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 "JniKeepAliveListener.h"
23 #include "OCApi.h"
24 #include "JniOcRepresentation.h"
25 using namespace OC;
26
27 JniKeepAliveListener::JniKeepAliveListener(JNIEnv *env, jobject jListener,
28         RemoveListenerCallback removeListenerCallback)
29 {
30     m_jwListener = env->NewWeakGlobalRef(jListener);
31     m_removeListenerCallback = removeListenerCallback;
32 }
33
34 JniKeepAliveListener::~JniKeepAliveListener()
35 {
36     LOGI("~JniKeepAliveListener()");
37     if (m_jwListener)
38     {
39         jint ret = JNI_ERR;
40         JNIEnv *env = GetJNIEnv(ret);
41         if (nullptr == env)
42         {
43             return;
44         }
45         env->DeleteWeakGlobalRef(m_jwListener);
46         m_jwListener = nullptr;
47         if (JNI_EDETACHED == ret)
48         {
49             g_jvm->DetachCurrentThread();
50         }
51     }
52 }
53
54 void JniKeepAliveListener::onKeepAliveListener(const int result, const OC::OCRepresentation& rep)
55 {
56     jint ret = JNI_ERR;
57     JNIEnv *env = GetJNIEnv(ret);
58     if (nullptr == env)
59     {
60         return;
61     }
62
63     jobject jListener = env->NewLocalRef(m_jwListener);
64     if (!jListener)
65     {
66         checkExAndRemoveListener(env);
67         if (JNI_EDETACHED == ret)
68         {
69             g_jvm->DetachCurrentThread();
70         }
71         return;
72     }
73
74     jclass clsL = env->GetObjectClass(jListener);
75     if (!clsL)
76     {
77         checkExAndRemoveListener(env);
78         if (JNI_EDETACHED == ret)
79         {
80             g_jvm->DetachCurrentThread();
81         }
82         return;
83     }
84
85     //create java OCRep
86     OCRepresentation* nRep = new OCRepresentation(rep);
87     jlong handle = reinterpret_cast<jlong>(nRep);
88     jobject jRep = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
89                                   handle, true);
90
91     if (!jRep)
92     {
93         delete nRep;
94         checkExAndRemoveListener(env);
95         if (JNI_EDETACHED == ret)
96         {
97             g_jvm->DetachCurrentThread();
98         }
99         return;
100     }
101
102
103     jint jRet = static_cast<jint>(result);
104
105     jmethodID midL = env->GetMethodID(clsL, "onKeepAliveListener",
106                                       "(Lorg/iotivity/base/OcRepresentation;I)V");
107
108     if (!midL)
109     {
110         checkExAndRemoveListener(env);
111         if (JNI_EDETACHED == ret)
112         {
113             g_jvm->DetachCurrentThread();
114         }
115         return;
116     }
117
118     env->CallVoidMethod(jListener, midL, jRep, jRet);
119     if (env->ExceptionCheck())
120     {
121         LOGE("Java exception is thrown");
122         delete nRep;
123     }
124
125     checkExAndRemoveListener(env);
126
127     if (JNI_EDETACHED == ret)
128     {
129         g_jvm->DetachCurrentThread();
130     }
131 }
132
133 void JniKeepAliveListener::checkExAndRemoveListener(JNIEnv* env)
134 {
135     if (env->ExceptionCheck())
136     {
137         jthrowable ex = env->ExceptionOccurred();
138         env->ExceptionClear();
139         m_removeListenerCallback(env, m_jwListener);
140         env->Throw((jthrowable)ex);
141     }
142     else
143     {
144         m_removeListenerCallback(env, m_jwListener);
145     }
146 }