replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniDisplayVerifyNumListener.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2016 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 "JniDisplayVerifyNumListener.h"
23 #include "oic_malloc.h"
24
25 JniDisplayVerifyNumListener::JniDisplayVerifyNumListener(JNIEnv *env, jobject jListener)
26 {
27     m_jgListener = env->NewGlobalRef(jListener);
28 }
29
30 JniDisplayVerifyNumListener::~JniDisplayVerifyNumListener()
31 {
32     LOGI("~JniDisplayVerifyNumListener()");
33     if (m_jgListener)
34     {
35         jint ret = JNI_ERR;
36         JNIEnv *env = GetJNIEnv(ret);
37         if (NULL == env) return;
38         env->DeleteGlobalRef(m_jgListener);
39         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
40     }
41 }
42
43 OCStackResult JniDisplayVerifyNumListener::displayMutualVerifNumCallback(uint8_t verifNum[3])
44 {
45     jint ret = JNI_ERR;
46     JNIEnv *env = GetJNIEnv(ret);
47     char *byteArray;
48
49     if (NULL == env)
50     {
51         return OC_STACK_ERROR;
52     }
53
54     jclass clsL = env->GetObjectClass(m_jgListener);
55
56     if (!clsL)
57     {
58         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
59         return OC_STACK_ERROR;
60     }
61
62     jmethodID midL = env->GetMethodID(clsL, "displayNumListener", "(Ljava/lang/String;)I");
63     if (!midL)
64     {
65         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
66         return OC_STACK_ERROR;
67     }
68
69     byteArray = (char*)OICCalloc(20, sizeof(char));
70
71     if (NULL == byteArray)
72     {
73         if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
74         return OC_STACK_NO_MEMORY;
75     }
76
77     sprintf(byteArray, "%02X%02X%02X",  verifNum[0], verifNum[1], verifNum[2]);
78
79     jstring jStr = env->NewStringUTF(byteArray);
80     if (!jStr)
81     {
82         if (JNI_EDETACHED == ret)
83         {
84             g_jvm->DetachCurrentThread();
85         }
86         return OC_STACK_ERROR;
87     }
88     OCStackResult result = (OCStackResult)env->CallIntMethod(m_jgListener, midL, jStr);
89
90     if (env->ExceptionCheck())
91     {
92         LOGE("Java exception is thrown");
93         env->ExceptionClear();
94     }
95
96     if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
97     free(byteArray);
98     return result;
99 }