replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / jni / JniCloudPropProvisioningStatusListener.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  ******************************************************************/
19
20 #include "JniCloudPropProvisioningStatusListener.h"
21 #include "JniRemoteEnrollee.h"
22
23 using namespace OIC::Service;
24
25 JniCloudPropProvisioningStatusListener::JniCloudPropProvisioningStatusListener(JNIEnv *env, jobject jListener,
26         JniRemoteEnrollee *owner)
27     : m_ownerResource(owner)
28 {
29     m_jwListener = env->NewWeakGlobalRef(jListener);
30 }
31
32 JniCloudPropProvisioningStatusListener::~JniCloudPropProvisioningStatusListener()
33 {
34     ES_LOGI("~JniCloudPropProvisioningStatusListener()");
35     if (m_jwListener)
36     {
37         jint ret;
38         JNIEnv *env = GetESJNIEnv(ret);
39         if (NULL == env) return;
40         env->DeleteWeakGlobalRef(m_jwListener);
41         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
42     }
43 }
44
45 void JniCloudPropProvisioningStatusListener::onCloudPropProvisioningStatus(std::shared_ptr<CloudPropProvisioningStatus>
46         cloudPropProvisioningStatus)
47 {
48
49     ES_LOGI("JniCloudPropProvisioningStatusListener::onCloudPropProvisioningStatus enter");
50
51     jint ret;
52     JNIEnv *env = GetESJNIEnv(ret);
53     if (NULL == env) return;
54     jobject jListener = env->NewLocalRef(m_jwListener);
55     if (!jListener)
56     {
57         checkExAndRemoveListener(env);
58         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
59         return;
60     }
61
62     jclass clsL = env->GetObjectClass(jListener);
63     if (!clsL)
64     {
65         checkExAndRemoveListener(env);
66         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
67         return;
68     }
69
70     jmethodID midL = env->GetMethodID(clsL, "onProgress",
71                                       "(Lorg/iotivity/service/easysetup/mediator/"
72                                       "CloudPropProvisioningStatus;"
73                                       ")V");
74     if (!midL)
75     {
76         checkExAndRemoveListener(env);
77         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
78         return;
79     }
80
81     ESResult esResult = cloudPropProvisioningStatus->getESResult();
82
83     //create the java object
84     jobject jCloudPropProvisioningStatus = NULL;
85     jCloudPropProvisioningStatus = env->NewObject(g_cls_CloudPropProvisioningStatus,
86                                                 g_mid_CloudPropProvisioningStatus_ctor,
87                                                 (jint)esResult);
88
89     ES_LOGI("JniCloudPropProvisioningStatus::onCloudPropProvisioningStatus - %d", esResult);
90     if (!jCloudPropProvisioningStatus)
91     {
92         ES_LOGE("JniCloudPropProvisioningStatus::onCloudPropProvisioningStatus Unable to create the java object");
93         return ;
94     }
95
96     env->CallVoidMethod(jListener, midL, jCloudPropProvisioningStatus);
97
98     bool needRemoveListener = false;
99
100     if(esResult == ES_OK)
101     {
102         needRemoveListener = true;
103     }
104
105     if (env->ExceptionCheck())
106     {
107         ES_LOGE("Java exception is thrown");
108         if(needRemoveListener)
109             checkExAndRemoveListener(env);
110         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
111         return;
112     }
113
114     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
115 }
116
117 void JniCloudPropProvisioningStatusListener::checkExAndRemoveListener(JNIEnv *env)
118 {
119     if (env->ExceptionCheck())
120     {
121         jthrowable ex = env->ExceptionOccurred();
122         env->ExceptionClear();
123         m_ownerResource->removeStatusListener<JniCloudPropProvisioningStatusListener>(env, m_jwListener);
124         env->Throw((jthrowable)ex);
125     }
126     else
127     {
128         m_ownerResource->removeStatusListener<JniCloudPropProvisioningStatusListener>(env, m_jwListener);
129     }
130 }