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 "JniProvisioningStatusListener.h"
22 #include "JniRemoteEnrollee.h"
24 using namespace OIC::Service;
26 JniProvisioningStatusListener::JniProvisioningStatusListener(JNIEnv *env, jobject jListener,
27 JniRemoteEnrollee *owner)
28 : m_ownerResource(owner)
30 m_jwListener = env->NewWeakGlobalRef(jListener);
33 JniProvisioningStatusListener::~JniProvisioningStatusListener()
35 LOGI("~JniProvisioningStatusListener()");
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 JniProvisioningStatusListener::provisionStatusCallback (std::shared_ptr<EasySetupStatus>
50 LOGI("JniProvisioningStatusListener::provisioiningStatusCallback 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, "onStatusRecieved",
75 checkExAndRemoveListener(env);
76 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
80 EasySetupState nativeProvisioningState = easySetupStatus->getEasySetupState();
81 int provisionState = convertNativeProvisionStateToInt(nativeProvisioningState);
82 env->CallVoidMethod(jListener, midL, provisionState);
83 if (env->ExceptionCheck())
85 LOGE("Java exception is thrown");
86 checkExAndRemoveListener(env);
87 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
91 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
94 void JniProvisioningStatusListener::checkExAndRemoveListener(JNIEnv *env)
96 if (env->ExceptionCheck())
98 jthrowable ex = env->ExceptionOccurred();
99 env->ExceptionClear();
100 m_ownerResource->removeProvisioningStatusListener(env, m_jwListener);
101 env->Throw((jthrowable)ex);
105 m_ownerResource->removeProvisioningStatusListener(env, m_jwListener);