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