Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniDisplayPinListener.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 "JniDisplayPinListener.h"
23 #include "oic_string.h"
24 #include "oic_malloc.h"
25
26 JniDisplayPinListener::JniDisplayPinListener(JNIEnv *env, jobject jListener)
27 {
28     m_jgListener = env->NewGlobalRef(jListener);
29 }
30
31 JniDisplayPinListener::~JniDisplayPinListener()
32 {
33     LOGI("~JniDisplayPinListener()");
34     if (m_jgListener)
35     {
36         jint ret;
37         JNIEnv *env = GetJNIEnv(ret);
38         if (NULL == env) return;
39         env->DeleteGlobalRef(m_jgListener);
40         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
41     }
42 }
43
44 void JniDisplayPinListener::displayPinCallback(char *pinBuf, size_t pinSize)
45 {
46     jint ret;
47     JNIEnv *env = GetJNIEnv(ret);
48     if (NULL == env) return;
49
50     jclass clsL = env->GetObjectClass(m_jgListener);
51
52     if (!clsL)
53     {
54         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
55         return;
56     }
57
58     jmethodID midL = env->GetMethodID(clsL, "displayPinListener", "(Ljava/lang/String;)V");
59     if (!midL)
60     {
61         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
62         return;
63     }
64
65     char *pinStr = (char*)OICMalloc(pinSize + 1);
66     if (!pinStr)
67     {
68         LOGE("malloc failed");
69         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
70         return ;
71     }
72     OICStrcpy(pinStr, (pinSize+1), pinBuf);
73
74     env->CallVoidMethod(m_jgListener, midL, env->NewStringUTF(pinStr));
75     OICFree(pinStr);
76
77     if (env->ExceptionCheck())
78     {
79         LOGE("Java exception is thrown");
80         env->ExceptionClear();
81     }
82
83     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
84 }