Merge remote-tracking branch 'origin/extended-easysetup'
[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     ESCloudProvState cloudProvisionState = cloudPropProvisioningStatus->getESCloudState();
83
84     //create the java object
85     jobject jCloudPropProvisioningStatus = NULL;
86     jCloudPropProvisioningStatus = env->NewObject(g_cls_CloudPropProvisioningStatus,
87                                                 g_mid_CloudPropProvisioningStatus_ctor,
88                                                 (jint)esResult,
89                                                 (jint)cloudProvisionState);
90
91     ES_LOGI("JniCloudPropProvisioningStatus::onCloudPropProvisioningStatus - %d, %d", esResult, cloudProvisionState);
92     if (!jCloudPropProvisioningStatus)
93     {
94         ES_LOGE("JniCloudPropProvisioningStatus::onCloudPropProvisioningStatus Unable to create the java object");
95         return ;
96     }
97
98     env->CallVoidMethod(jListener, midL, jCloudPropProvisioningStatus);
99
100     bool needRemoveListener = false;
101
102     if(cloudProvisionState == ES_CLOUD_PROVISIONING_ERROR ||
103             cloudProvisionState == ES_CLOUD_PROVISIONING_SUCCESS )
104     {
105         needRemoveListener = true;
106     }
107
108     if (env->ExceptionCheck())
109     {
110         ES_LOGE("Java exception is thrown");
111         if(needRemoveListener)
112             checkExAndRemoveListener(env);
113         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
114         return;
115     }
116
117     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
118 }
119
120 void JniCloudPropProvisioningStatusListener::checkExAndRemoveListener(JNIEnv *env)
121 {
122     if (env->ExceptionCheck())
123     {
124         jthrowable ex = env->ExceptionOccurred();
125         env->ExceptionClear();
126         m_ownerResource->removeStatusListener<JniCloudPropProvisioningStatusListener>(env, m_jwListener);
127         env->Throw((jthrowable)ex);
128     }
129     else
130     {
131         m_ownerResource->removeStatusListener<JniCloudPropProvisioningStatusListener>(env, m_jwListener);
132     }
133 }