+JniOnObserveListener* AddOnObserveListener(JNIEnv* env, jobject jListener)
+{
+ JniOnObserveListener *onObserveListener = nullptr;
+
+ observeMapLock.lock();
+
+ for (auto it = onObserveListenerMap.begin(); it != onObserveListenerMap.end(); ++it)
+ {
+ if (env->IsSameObject(jListener, it->first))
+ {
+ auto refPair = it->second;
+ onObserveListener = refPair.first;
+ refPair.second++;
+ it->second = refPair;
+ onObserveListenerMap.insert(*it);
+ LOGD("OnObserveListener: ref. count incremented");
+ break;
+ }
+ }
+ if (!onObserveListener)
+ {
+ onObserveListener = new JniOnObserveListener(env, jListener, (JniOcResource*)nullptr);
+ jobject jgListener = env->NewGlobalRef(jListener);
+ onObserveListenerMap.insert(
+ std::pair<jobject, std::pair<JniOnObserveListener*, int>>(
+ jgListener,
+ std::pair<JniOnObserveListener*, int>(onObserveListener, 1)));
+ LOGI("OnObserveListener: new listener");
+ }
+ observeMapLock.unlock();
+ return onObserveListener;
+}
+
+void RemoveOnObserveListener(JNIEnv* env, jobject jListener)
+{
+ observeMapLock.lock();
+ bool isFound = false;
+ for (auto it = onObserveListenerMap.begin(); it != onObserveListenerMap.end(); ++it)
+ {
+ if (env->IsSameObject(jListener, it->first))
+ {
+ auto refPair = it->second;
+ if (refPair.second > 1)
+ {
+ refPair.second--;
+ it->second = refPair;
+ onObserveListenerMap.insert(*it);
+ LOGI("OnObserveListener: ref. count decremented");
+ }
+ else
+ {
+ env->DeleteGlobalRef(it->first);
+ JniOnObserveListener* listener = refPair.first;
+ delete listener;
+ onObserveListenerMap.erase(it);
+ LOGI("OnObserveListener is removed");
+ }
+ isFound = true;
+ break;
+ }
+ }
+ if (!isFound)
+ {
+ ThrowOcException(JNI_EXCEPTION, "OnObserveListener not found");
+ }
+ observeMapLock.unlock();
+}
+
+JniOnDPDevicesFoundListener* AddOnDPDevicesFoundListener(JNIEnv* env, jobject jListener)
+{
+ JniOnDPDevicesFoundListener *onDPDeviceListener = nullptr;
+
+ dpDevicesFoundListenerMapLock.lock();
+
+ for (auto it = onDPDevicesFoundListenerMap.begin(); it !=
+ onDPDevicesFoundListenerMap.end(); ++it)
+ {
+ if (env->IsSameObject(jListener, it->first))
+ {
+ auto refPair = it->second;
+ onDPDeviceListener = refPair.first;
+ refPair.second++;
+ it->second = refPair;
+ onDPDevicesFoundListenerMap.insert(*it);
+ LOGD("onDPDeviceListener: ref. count incremented");
+ break;
+ }
+ }
+ if (!onDPDeviceListener)
+ {
+ onDPDeviceListener = new JniOnDPDevicesFoundListener(env, jListener,
+ RemoveOnDPDevicesFoundListener);
+ jobject jgListener = env->NewGlobalRef(jListener);
+ onDPDevicesFoundListenerMap.insert(
+ std::pair<jobject, std::pair<JniOnDPDevicesFoundListener*, int>>(
+ jgListener,
+ std::pair<JniOnDPDevicesFoundListener*, int>(onDPDeviceListener, 1)));
+ LOGI("onDPDeviceListener: new listener");
+ }
+ dpDevicesFoundListenerMapLock.unlock();
+ return onDPDeviceListener;
+}
+
+void RemoveOnDPDevicesFoundListener(JNIEnv* env, jobject jListener)
+{
+ dpDevicesFoundListenerMapLock.lock();
+ bool isFound = false;
+ for (auto it = onDPDevicesFoundListenerMap.begin(); it !=
+ onDPDevicesFoundListenerMap.end(); ++it)
+ {
+ if (env->IsSameObject(jListener, it->first))
+ {
+ auto refPair = it->second;
+ if (refPair.second > 1)
+ {
+ refPair.second--;
+ it->second = refPair;
+ onDPDevicesFoundListenerMap.insert(*it);
+ LOGI("onDPDeviceListener: ref. count decremented");
+ }
+ else
+ {
+ env->DeleteGlobalRef(it->first);
+ JniOnDPDevicesFoundListener* listener = refPair.first;
+ delete listener;
+ onDPDevicesFoundListenerMap.erase(it);
+ LOGI("onDPDeviceListener is removed");
+ }
+ isFound = true;
+ break;
+ }
+ }
+ if (!isFound)
+ {
+ ThrowOcException(JNI_EXCEPTION, "onDPDeviceListener not found");
+ }
+ dpDevicesFoundListenerMapLock.unlock();
+}
+
+JniOnDirectPairingListener* AddOnDirectPairingListener(JNIEnv* env, jobject jListener)
+{
+ JniOnDirectPairingListener *onDirectPairingListener = nullptr;
+
+ directPairingListenerMapLock.lock();
+
+ for (auto it = directPairingListenerMap.begin(); it !=
+ directPairingListenerMap.end(); ++it)
+ {
+ if (env->IsSameObject(jListener, it->first))
+ {
+ auto refPair = it->second;
+ onDirectPairingListener = refPair.first;
+ refPair.second++;
+ it->second = refPair;
+ directPairingListenerMap.insert(*it);
+ LOGD("onDirectPairingListener: ref. count incremented");
+ break;
+ }
+ }
+ if (!onDirectPairingListener)
+ {
+ onDirectPairingListener = new JniOnDirectPairingListener(env, jListener,
+ RemoveOnDirectPairingListener);
+ jobject jgListener = env->NewGlobalRef(jListener);
+ directPairingListenerMap.insert(
+ std::pair<jobject, std::pair<JniOnDirectPairingListener*, int>>(
+ jgListener,
+ std::pair<JniOnDirectPairingListener*, int>(onDirectPairingListener, 1)));
+ LOGI("onDirectPairingListener: new listener");
+ }
+ directPairingListenerMapLock.unlock();
+ return onDirectPairingListener;
+}
+
+void RemoveOnDirectPairingListener(JNIEnv* env, jobject jListener)
+{
+ directPairingListenerMapLock.lock();
+ bool isFound = false;
+ for (auto it = directPairingListenerMap.begin(); it !=
+ directPairingListenerMap.end(); ++it)
+ {
+ if (env->IsSameObject(jListener, it->first))
+ {
+ auto refPair = it->second;
+ if (refPair.second > 1)
+ {
+ refPair.second--;
+ it->second = refPair;
+ directPairingListenerMap.insert(*it);
+ LOGI("onDirectPairingListener: ref. count decremented");
+ }
+ else
+ {
+ env->DeleteGlobalRef(it->first);
+ JniOnDirectPairingListener* listener = refPair.first;
+ delete listener;
+ directPairingListenerMap.erase(it);
+ LOGI("onDirectPairingListener is removed");
+ }
+ isFound = true;
+ break;
+ }
+ }
+ if (!isFound)
+ {
+ ThrowOcException(JNI_EXCEPTION, "onDirectPairingListener not found");
+ }
+ directPairingListenerMapLock.unlock();
+}
+
+#ifdef TCP_ADAPTER
+JniKeepAliveListener* AddKeepAliveListener(JNIEnv* env, jobject jListener)
+{
+ JniKeepAliveListener *KeepAliveListener = nullptr;
+
+ KeepAliveListenerMapLock.lock();
+
+ for (auto it = KeepAliveListenerMap.begin(); it !=
+ KeepAliveListenerMap.end(); ++it)
+ {
+ if (env->IsSameObject(jListener, it->first))
+ {
+ auto refPair = it->second;
+ KeepAliveListener = refPair.first;
+ refPair.second++;
+ it->second = refPair;
+ KeepAliveListenerMap.insert(*it);
+ LOGD("KeepAliveListener: ref. count incremented");
+ break;
+ }
+ }
+ if (!KeepAliveListener)
+ {
+ KeepAliveListener = new JniKeepAliveListener(env, jListener, RemoveKeepAliveListener);
+ jobject jgListener = env->NewGlobalRef(jListener);
+ KeepAliveListenerMap.insert(std::pair<jobject, std::pair<JniKeepAliveListener*, int>>(
+ jgListener,
+ std::pair<JniKeepAliveListener*, int>(KeepAliveListener, 1)));
+ LOGI("KeepAliveListener: new listener");
+ }
+ KeepAliveListenerMapLock.unlock();
+ return KeepAliveListener;
+}
+#endif
+#ifdef TCP_ADAPTER
+void RemoveKeepAliveListener(JNIEnv* env, jobject jListener)
+{
+ KeepAliveListenerMapLock.lock();
+ bool isFound = false;
+ for (auto it = KeepAliveListenerMap.begin(); it !=
+ KeepAliveListenerMap.end(); ++it)
+ {
+ if (env->IsSameObject(jListener, it->first))
+ {
+ auto refPair = it->second;
+ if (refPair.second > 1)
+ {
+ refPair.second--;
+ it->second = refPair;
+ KeepAliveListenerMap.insert(*it);
+ LOGI("KeepAliveListener: ref.count decremented");
+ }
+ else
+ {
+ env->DeleteGlobalRef(it->first);
+ JniKeepAliveListener* listener = refPair.first;
+ delete listener;
+ KeepAliveListenerMap.erase(it);
+ LOGI("KeepAliveListener is removed");
+ }
+ isFound = true;
+ break;
+ }
+ }
+ if (!isFound)
+ {
+ ThrowOcException(JNI_EXCEPTION, "KeepAliveListener not found");
+ }
+ KeepAliveListenerMapLock.unlock();
+}
+#endif