1 /******************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "JniDevicePropProvisioningStatusListener.h"
22 #include "JniRemoteEnrollee.h"
24 using namespace OIC::Service;
26 JniDevicePropProvisioningStatusListener::JniDevicePropProvisioningStatusListener(JNIEnv *env, jobject jListener,
27 JniRemoteEnrollee *owner)
28 : m_ownerResource(owner)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
33 JniDevicePropProvisioningStatusListener::~JniDevicePropProvisioningStatusListener()
35 ES_LOGI("~JniDevicePropProvisioningStatusListener()");
39 JNIEnv *env = GetESJNIEnv(ret);
40 if (NULL == env) return;
41 env->DeleteWeakGlobalRef(m_jwListener);
42 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
46 void JniDevicePropProvisioningStatusListener::onDevicePropProvisioningStatusCallback (std::shared_ptr<DevicePropProvisioningStatus>
47 devicePropProvStatusCb)
50 ES_LOGI("JniDevicePropProvisioningStatusListener::onDevicePropProvisioningStatusCallback enter");
53 JNIEnv *env = GetESJNIEnv(ret);
54 if (NULL == env) return;
55 jobject jListener = env->NewLocalRef(m_jwListener);
58 checkExAndRemoveListener(env);
59 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
63 jclass clsL = env->GetObjectClass(jListener);
66 checkExAndRemoveListener(env);
67 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
71 jmethodID midL = env->GetMethodID(clsL, "onProgress",
72 "(Lorg/iotivity/service/easysetup/mediator/"
73 "DevicePropProvisioningStatus;"
78 checkExAndRemoveListener(env);
79 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
83 ESResult esResult = devicePropProvStatusCb->getESResult();
85 jobject jDevicePropProvisioningStatus = NULL;
86 jDevicePropProvisioningStatus = env->NewObject(g_cls_DevicePropProvisioningStatus,
87 g_mid_DevicePropProvisioningStatus_ctor,
90 ES_LOGI("JniDevicePropProvisioningStatus::onDevicePropProvisioningStatus - %d", esResult);
91 if (!jDevicePropProvisioningStatus)
93 ES_LOGE("JniDevicePropProvisioningStatus::onDevicePropProvisioningStatus Unable to create the java object");
97 env->CallVoidMethod(jListener, midL, jDevicePropProvisioningStatus);
99 if (env->ExceptionCheck())
101 ES_LOGE("Java exception is thrown");
102 checkExAndRemoveListener(env);
103 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
107 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
110 void JniDevicePropProvisioningStatusListener::checkExAndRemoveListener(JNIEnv *env)
112 if (env->ExceptionCheck())
114 jthrowable ex = env->ExceptionOccurred();
115 env->ExceptionClear();
116 m_ownerResource->removeStatusListener<JniDevicePropProvisioningStatusListener>(env, m_jwListener);
117 env->Throw((jthrowable)ex);
121 m_ownerResource->removeStatusListener<JniDevicePropProvisioningStatusListener>(env, m_jwListener);