Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnPresenceListener.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 "JniOnPresenceListener.h"
23 #include "JniUtils.h"
24
25 JniOnPresenceListener::JniOnPresenceListener(JNIEnv *env, jobject jListener,
26     RemoveListenerCallback removeListenerCallback)
27 {
28     m_jwListener = env->NewWeakGlobalRef(jListener);
29     m_removeListenerCallback = removeListenerCallback;
30 }
31
32 JniOnPresenceListener::~JniOnPresenceListener()
33 {
34     LOGD("~JniOnPresenceListener");
35     if (m_jwListener)
36     {
37         jint ret;
38         JNIEnv *env = GetJNIEnv(ret);
39         if (nullptr == env) return;
40
41         env->DeleteWeakGlobalRef(m_jwListener);
42         m_jwListener = nullptr;
43
44         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
45     }
46 }
47
48 void JniOnPresenceListener::onPresenceCallback(OCStackResult result, const unsigned int nonce,
49     const std::string& hostAddress)
50 {
51     LOGI("JniOnPresenceListener::onPresenceCallback");
52     if (!m_jwListener) return;
53
54     jint ret;
55     JNIEnv *env = GetJNIEnv(ret);
56     if (nullptr == env) return;
57
58     if (OC_STACK_OK != result && OC_STACK_PRESENCE_STOPPED != result &&
59         OC_STACK_PRESENCE_TIMEOUT != result &&  OC_STACK_PRESENCE_DO_NOT_HANDLE != result)
60     {
61         ThrowOcException(result, "onPresenceCallback: stack failure");
62         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
63         return;
64     }
65
66     std::string enumField = JniUtils::stackResultToStr(result);
67     if (enumField.empty())
68     {
69         ThrowOcException(JNI_INVALID_VALUE, "Unexpected OCStackResult value");
70         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
71         return;
72     }
73
74     jobject jPresenceStatus = env->CallStaticObjectMethod(g_cls_OcPresenceStatus,
75         g_mid_OcPresenceStatus_get, env->NewStringUTF(enumField.c_str()));
76     if (!jPresenceStatus)
77     {
78         checkExAndRemoveListener(env);
79         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
80         return;
81     }
82
83     jobject jListener = env->NewLocalRef(m_jwListener);
84     if (!jListener)
85     {
86         checkExAndRemoveListener(env);
87         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
88         return;
89     }
90
91     jclass clsL = env->GetObjectClass(jListener);
92     if (!clsL)
93     {
94         checkExAndRemoveListener(env);
95         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
96         return;
97     }
98     jmethodID midL = env->GetMethodID(clsL, "onPresence",
99         "(Lorg/iotivity/base/OcPresenceStatus;ILjava/lang/String;)V");
100     if (!midL)
101     {
102         checkExAndRemoveListener(env);
103         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
104         return;
105     }
106
107     env->CallVoidMethod(jListener, midL, jPresenceStatus,
108         (jint)nonce, env->NewStringUTF(hostAddress.c_str()));
109     if (env->ExceptionCheck())
110     {
111         LOGE("Java exception is thrown");
112         checkExAndRemoveListener(env);
113     }
114     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
115 }
116
117 void JniOnPresenceListener::checkExAndRemoveListener(JNIEnv* env)
118 {
119     if (env->ExceptionCheck())
120     {
121         jthrowable ex = env->ExceptionOccurred();
122         env->ExceptionClear();
123         m_removeListenerCallback(env, m_jwListener);
124         env->Throw((jthrowable)ex);
125     }
126     else
127     {
128         m_removeListenerCallback(env, m_jwListener);
129     }
130 }
131
132 jweak JniOnPresenceListener::getJWListener()
133 {
134     return this->m_jwListener;
135 }