1 /******************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 /** @file JniEsLisenerManager.h
23 * @brief This file contains JNI Listener Manager class for JniRemoteEnrollee
26 #ifndef __JNI_ES_LISTENER_MANAGER_H_
27 #define __JNI_ES_LISTENER_MANAGER_H_
32 class JniRemoteEnrollee;
35 * @class JniEsListenerManager
36 * @brief This class provides functions for managing listeners
40 class JniEsListenerManager
44 * API for Adding the Listener to listener Map.
46 * @param owner - JniRemoteEnrollee
48 T *addListener(JNIEnv *env, jobject jListener, JniRemoteEnrollee *owner)
50 T *onEventListener = NULL;
54 for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
56 if (env->IsSameObject(jListener, it->first))
58 auto refPair = it->second;
59 onEventListener = refPair.first;
62 m_listenerMap.insert(*it);
63 ES_LOGD("OnEventListener: ref. count is incremented");
69 onEventListener = new T(env, jListener, owner);
70 jobject jgListener = env->NewGlobalRef(jListener);
76 std::pair<T *, int >> (jgListener, std::pair<T *, int>(onEventListener, 1)));
80 ES_LOGD("OnEventListener: Failed to create global listener ref.");
81 delete onEventListener;
83 ES_LOGD("OnEventListener: new listener");
86 return onEventListener;
90 * @brief API for removing the Listener from listener Map.
92 void removeListener(JNIEnv *env, jobject jListener)
95 for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
97 if (env->IsSameObject(jListener, it->first))
99 auto refPair = it->second;
100 if (refPair.second > 1)
103 it->second = refPair;
104 m_listenerMap.insert(*it);
105 ES_LOGI("OnEventListener: ref. count is decremented");
109 env->DeleteGlobalRef(it->first);
110 T *listener = refPair.first;
112 m_listenerMap.erase(it);
114 ES_LOGI("OnEventListener is removed");
123 * @brief API for removing all the Listener from listener Map.
125 void removeAllListeners(JNIEnv *env)
129 for (auto &pair : m_listenerMap)
131 env->DeleteGlobalRef(pair.first);
132 auto refPair = pair.second;
133 delete refPair.first;
135 m_listenerMap.clear();
141 std::map<jobject, std::pair<T *, int>> m_listenerMap;
142 std::mutex m_mapMutex;
145 #endif //__JNI_ES_LISTENER_MANAGER_H_