Add APIs of cloud provisioning for Android and JNI layer
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / jni / JniCloudProvisioningStatusListener.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 "JniCloudProvisioningStatusListener.h"
21 #include "JniRemoteEnrollee.h"
22
23 using namespace OIC::Service;
24
25 JniCloudProvisioningStatusListener::JniCloudProvisioningStatusListener(JNIEnv *env, jobject jListener,
26         JniRemoteEnrollee *owner)
27     : m_ownerResource(owner)
28 {
29     m_jwListener = env->NewWeakGlobalRef(jListener);
30 }
31
32 JniCloudProvisioningStatusListener::~JniCloudProvisioningStatusListener()
33 {
34     LOGI("~JniCloudProvisioningStatusListener()");
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 JniCloudProvisioningStatusListener::onCloudProvisioningStatus(std::shared_ptr<CloudProvisioningStatus>
46         cloudProvisioningStatus)
47 {
48
49     LOGI("JniCloudProvisioningStatusListener::onCloudProvisioningStatus 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                                       "CloudProvisioningStatus;"
73                                       ")V");
74     if (!midL)
75     {
76         checkExAndRemoveListener(env);
77         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
78         return;
79     }
80
81     ESResult esResult = cloudProvisioningStatus->getESResult();
82     ESCloudProvState cloudProvisionState = cloudProvisioningStatus->getESCloudState();
83
84     //create the java object
85     jobject jCloudProvisioningStatus = NULL;
86     jCloudProvisioningStatus = env->NewObject(g_cls_CloudProvisioningStatus,
87                                                 g_mid_CloudProvisioningStatus_ctor,
88                                                 (jint)esResult,
89                                                 (jint)cloudProvisionState);
90
91     LOGI("JniCloudProvisioningStatus::onCloudProvisioningStatus - %d, %d", esResult, cloudProvisionState);
92     if (!jCloudProvisioningStatus)
93     {
94         LOGE("JniCloudProvisioningStatus::onCloudProvisioningStatus Unable to create the java object");
95         return ;
96     }
97
98     env->CallVoidMethod(jListener, midL, jCloudProvisioningStatus);
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         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 JniCloudProvisioningStatusListener::checkExAndRemoveListener(JNIEnv *env)
121 {
122     if (env->ExceptionCheck())
123     {
124         jthrowable ex = env->ExceptionOccurred();
125         env->ExceptionClear();
126         m_ownerResource->removeCloudProvisioningStatusListener(env, m_jwListener);
127         env->Throw((jthrowable)ex);
128     }
129     else
130     {
131         m_ownerResource->removeCloudProvisioningStatusListener(env, m_jwListener);
132     }
133 }