Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[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 = JNI_ERR;
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 = JNI_ERR;
47     JNIEnv *env = GetJNIEnv(ret);
48     if (NULL == env)
49     {
50         return;
51     }
52
53     jclass clsL = env->GetObjectClass(m_jgListener);
54
55     if (!clsL)
56     {
57         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
58         return;
59     }
60
61     jmethodID midL = env->GetMethodID(clsL, "displayPinListener", "(Ljava/lang/String;)V");
62     if (!midL)
63     {
64         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
65         return;
66     }
67
68     char *pinStr = (char*)OICMalloc(pinSize + 1);
69     if (!pinStr)
70     {
71         LOGE("malloc failed");
72         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
73         return ;
74     }
75     OICStrcpy(pinStr, (pinSize+1), pinBuf);
76
77     env->CallVoidMethod(m_jgListener, midL, env->NewStringUTF(pinStr));
78     OICFree(pinStr);
79
80     if (env->ExceptionCheck())
81     {
82         LOGE("Java exception is thrown");
83         env->ExceptionClear();
84     }
85
86     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
87 }