From 9d5336341355e8f3add31bf23bf94a651edd76ff Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 6 Apr 2018 19:31:32 +0200 Subject: [PATCH] Synchronize ChatStateManager's chatStateListeners --- .../smackx/chatstates/ChatStateManager.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/chatstates/ChatStateManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/chatstates/ChatStateManager.java index 62a47c637..76d95813e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/chatstates/ChatStateManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/chatstates/ChatStateManager.java @@ -17,7 +17,9 @@ package org.jivesoftware.smackx.chatstates; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; @@ -146,7 +148,13 @@ public final class ChatStateManager extends Manager { return; } - for (ChatStateListener listener : chatStateListeners) { + List listeners; + synchronized (chatStateListeners) { + listeners = new ArrayList<>(chatStateListeners.size()); + listeners.addAll(chatStateListeners); + } + + for (ChatStateListener listener : listeners) { listener.stateChanged(chat, state, message); } } @@ -162,7 +170,9 @@ public final class ChatStateManager extends Manager { * @return true, if the listener was not registered before */ public boolean addChatStateListener(ChatStateListener listener) { - return chatStateListeners.add(listener); + synchronized (chatStateListeners) { + return chatStateListeners.add(listener); + } } /** @@ -172,7 +182,9 @@ public final class ChatStateManager extends Manager { * @return true, if the listener was registered before */ public boolean removeChatStateListener(ChatStateListener listener) { - return chatStateListeners.remove(listener); + synchronized (chatStateListeners) { + return chatStateListeners.remove(listener); + } }