Imported Upstream version 0.9.2
[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;
38         JNIEnv *env = GetJNIEnv(ret);
39         if (NULL == env) return;
40         env->DeleteWeakGlobalRef(m_jwListener);
41         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
42     }
43 }
44
45 void JniOnPlatformInfoListener::foundPlatformCallback(const OC::OCRepresentation& ocRepresentation)
46 {
47     jint ret;
48     JNIEnv *env = GetJNIEnv(ret);
49     if (NULL == env) return;
50
51     jobject jListener = env->NewLocalRef(m_jwListener);
52     if (!jListener)
53     {
54         LOGI("Java onPlatformInfoListener object is already destroyed, quiting");
55         checkExAndRemoveListener(env);
56         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
57         return;
58     }
59
60     OCRepresentation* rep = new OCRepresentation(ocRepresentation);
61     jlong handle = reinterpret_cast<jlong>(rep);
62     jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
63         handle, true);
64     if (!jRepresentation)
65     {
66         delete rep;
67         checkExAndRemoveListener(env);
68         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
69         return;
70     }
71
72     jclass clsL = env->GetObjectClass(jListener);
73     if (!clsL)
74     {
75         delete rep;
76         checkExAndRemoveListener(env);
77         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
78         return;
79     }
80     jmethodID midL = env->GetMethodID(clsL, "onPlatformFound", "(Lorg/iotivity/base/OcRepresentation;)V");
81     if (!midL)
82     {
83         delete rep;
84         checkExAndRemoveListener(env);
85         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
86         return;
87     }
88
89     env->CallVoidMethod(jListener, midL, jRepresentation);
90     if (env->ExceptionCheck())
91     {
92         LOGE("Java exception is thrown");
93         delete rep;
94         checkExAndRemoveListener(env);
95     }
96
97     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
98 }
99
100 void JniOnPlatformInfoListener::checkExAndRemoveListener(JNIEnv* env)
101 {
102     if (env->ExceptionCheck())
103     {
104         jthrowable ex = env->ExceptionOccurred();
105         env->ExceptionClear();
106         m_removeListenerCallback(env, m_jwListener);
107         env->Throw((jthrowable)ex);
108     }
109     else
110     {
111         m_removeListenerCallback(env, m_jwListener);
112     }
113 }
114