Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniPinCheckListener.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Samsung Electronics All Rights Reserved.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22 #include "JniPinCheckListener.h"
23 #include "oic_string.h"
24
25 JniPinCheckListener::JniPinCheckListener(JNIEnv *env, jobject jListener)
26 {
27     m_jListener = env->NewGlobalRef(jListener);
28 }
29
30 JniPinCheckListener::~JniPinCheckListener()
31 {
32     LOGI("~JniPinCheckListener()");
33     if (m_jListener)
34     {
35         jint ret = JNI_ERR;
36         JNIEnv *env = GetJNIEnv(ret);
37         if (nullptr == env)
38         {
39             return;
40         }
41         env->DeleteGlobalRef(m_jListener);
42         if (JNI_EDETACHED == ret)
43         {
44             g_jvm->DetachCurrentThread();
45         }
46     }
47 }
48
49 void JniPinCheckListener::PinCallback(char *pinBuf, size_t bufSize)
50 {
51     jint ret = JNI_ERR;
52     JNIEnv *env = GetJNIEnv(ret);
53     if (nullptr == env)
54     {
55         return;
56     }
57
58     jclass clsL = env->GetObjectClass(m_jListener);
59
60     if (!clsL)
61     {
62         if (JNI_EDETACHED == ret)
63         {
64             g_jvm->DetachCurrentThread();
65         }
66         return;
67     }
68
69     jmethodID midL = env->GetMethodID(clsL, "pinCallbackListener", "()Ljava/lang/String;");
70     if (!midL)
71     {
72         if (JNI_EDETACHED == ret)
73         {
74             g_jvm->DetachCurrentThread();
75         }
76         return;
77     }
78     jstring jpin = (jstring)env->CallObjectMethod(m_jListener, midL);
79     if (env->ExceptionCheck())
80     {
81         LOGE("Java exception is thrown");
82         if (JNI_EDETACHED == ret)
83         {
84             g_jvm->DetachCurrentThread();
85         }
86         return;
87     }
88
89     char *str = (char*)env->GetStringUTFChars(jpin, NULL);
90     OICStrcpy(pinBuf, bufSize, str);
91     env->ReleaseStringUTFChars(jpin, str);
92
93     if (JNI_EDETACHED == ret)
94     {
95         g_jvm->DetachCurrentThread();
96     }
97 }