Andorid(JNI) Implementation for Cloud Provisioning.
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniGetAclIdByDeviceListener.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 "JniGetAclIdByDeviceListener.h"
23
24 JniGetAclIdByDeviceListener::JniGetAclIdByDeviceListener(JNIEnv *env, jobject jListener,
25     RemoveCallback removeListener)
26 {
27     m_jwListener = env->NewWeakGlobalRef(jListener);
28     m_removeGetAclbyIDListener = removeListener;
29 }
30
31 JniGetAclIdByDeviceListener::~JniGetAclIdByDeviceListener()
32 {
33     LOGI("~JniGetAclIdByDeviceListener()");
34     if (m_jwListener)
35     {
36         jint ret = JNI_ERR;
37         JNIEnv *env = GetJNIEnv(ret);
38         if (nullptr == env)
39         {
40             return;
41         }
42         env->DeleteWeakGlobalRef(m_jwListener);
43         if (JNI_EDETACHED == ret)
44         {
45             g_jvm->DetachCurrentThread();
46         }
47     }
48 }
49
50 void JniGetAclIdByDeviceListener::GetAclIdByDeviceListenerCB(int result, std::string aclID)
51 {
52     jint ret = JNI_ERR;
53     JNIEnv *env = GetJNIEnv(ret);
54     if (nullptr == env)
55     {
56         return;
57     }
58
59     jobject jListener = env->NewLocalRef(m_jwListener);
60     if (!jListener)
61     {
62         checkExAndRemoveListener(env);
63         if (JNI_EDETACHED == ret)
64         {
65             g_jvm->DetachCurrentThread();
66         }
67         return;
68     }
69
70     jclass clsL = env->GetObjectClass(jListener);
71
72     if (!clsL)
73     {
74         checkExAndRemoveListener(env);
75         if (JNI_EDETACHED == ret)
76         {
77             g_jvm->DetachCurrentThread();
78         }
79         return;
80     }
81
82     jmethodID midL = env->GetMethodID(clsL, "getAclIdByDeviceListener", "(ZLjava/lang/String;)V");
83     if (!midL)
84     {
85         checkExAndRemoveListener(env);
86         if (JNI_EDETACHED == ret)
87         {
88             g_jvm->DetachCurrentThread();
89         }
90         return;
91     }
92     jstring jStr = env-> NewStringUTF(aclID.c_str());
93     if (!jStr)
94     {
95         checkExAndRemoveListener(env);
96         if (JNI_EDETACHED == ret)
97         {
98             g_jvm->DetachCurrentThread();
99         }
100         return;
101
102     }
103     env->CallVoidMethod(jListener, midL, (jboolean)result, jStr);
104     if (env->ExceptionCheck())
105     {
106         LOGE("Java exception is thrown");
107     }
108     checkExAndRemoveListener(env);
109     if (JNI_EDETACHED == ret)
110     {
111         g_jvm->DetachCurrentThread();
112     }
113 }
114
115 void JniGetAclIdByDeviceListener::checkExAndRemoveListener(JNIEnv* env)
116 {
117     if (env->ExceptionCheck())
118     {
119         jthrowable ex = env->ExceptionOccurred();
120         env->ExceptionClear();
121         m_removeGetAclbyIDListener(env, m_jwListener);
122         env->Throw((jthrowable)ex);
123     }
124     else
125     {
126         m_removeGetAclbyIDListener(env, m_jwListener);
127     }
128 }