Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOnPlatformInfoListener.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 "JniOnPlatformInfoListener.h"
23 #include "JniOcRepresentation.h"
24
25 JniOnPlatformInfoListener::JniOnPlatformInfoListener(JNIEnv *env, jobject jListener,
26     RemoveListenerCallback removeListenerCallback)
27 {
28     m_jwListener = env->NewWeakGlobalRef(jListener);
29     m_removeListenerCallback = removeListenerCallback;
30 }
31
32 JniOnPlatformInfoListener::~JniOnPlatformInfoListener()
33 {
34     LOGI("~JniOnPlatformInfoListener");
35     if (m_jwListener)
36     {
37         jint ret = JNI_ERR;
38         JNIEnv *env = GetJNIEnv(ret);
39         if (nullptr == env)
40         {
41             return;
42         }
43
44         env->DeleteWeakGlobalRef(m_jwListener);
45         m_jwListener = nullptr;
46
47         if (JNI_EDETACHED == ret)
48         {
49             g_jvm->DetachCurrentThread();
50         }
51     }
52 }
53
54 void JniOnPlatformInfoListener::foundPlatformCallback(const OC::OCRepresentation& ocRepresentation)
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         LOGI("Java onPlatformInfoListener object is already destroyed, quiting");
67         checkExAndRemoveListener(env);
68         if (JNI_EDETACHED == ret)
69         {
70             g_jvm->DetachCurrentThread();
71         }
72         return;
73     }
74
75     OCRepresentation* rep = new OCRepresentation(ocRepresentation);
76     jlong handle = reinterpret_cast<jlong>(rep);
77     jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
78         handle, true);
79     if (!jRepresentation)
80     {
81         delete rep;
82         checkExAndRemoveListener(env);
83         if (JNI_EDETACHED == ret)
84         {
85             g_jvm->DetachCurrentThread();
86         }
87         return;
88     }
89
90     jclass clsL = env->GetObjectClass(jListener);
91     if (!clsL)
92     {
93         delete rep;
94         checkExAndRemoveListener(env);
95         if (JNI_EDETACHED == ret)
96         {
97             g_jvm->DetachCurrentThread();
98         }
99         return;
100     }
101     jmethodID midL = env->GetMethodID(clsL, "onPlatformFound", "(Lorg/iotivity/base/OcRepresentation;)V");
102     if (!midL)
103     {
104         delete rep;
105         checkExAndRemoveListener(env);
106         if (JNI_EDETACHED == ret)
107         {
108             g_jvm->DetachCurrentThread();
109         }
110         return;
111     }
112
113     env->CallVoidMethod(jListener, midL, jRepresentation);
114     if (env->ExceptionCheck())
115     {
116         LOGE("Java exception is thrown");
117         delete rep;
118         checkExAndRemoveListener(env);
119     }
120
121     if (JNI_EDETACHED == ret)
122     {
123         g_jvm->DetachCurrentThread();
124     }
125 }
126
127 void JniOnPlatformInfoListener::checkExAndRemoveListener(JNIEnv* env)
128 {
129     if (env->ExceptionCheck())
130     {
131         jthrowable ex = env->ExceptionOccurred();
132         env->ExceptionClear();
133         m_removeListenerCallback(env, m_jwListener);
134         env->Throw((jthrowable)ex);
135     }
136     else
137     {
138         m_removeListenerCallback(env, m_jwListener);
139     }
140 }
141