diff --git a/core/src/main/java/org/jivesoftware/smack/Chat.java b/core/src/main/java/org/jivesoftware/smack/Chat.java index 216ff9f06..b511914c9 100644 --- a/core/src/main/java/org/jivesoftware/smack/Chat.java +++ b/core/src/main/java/org/jivesoftware/smack/Chat.java @@ -128,6 +128,16 @@ public class Chat { listeners.remove(listener); } + /** + * Closes the Chat and removes all references to it from the {@link ChatManager}. The chat will + * be unusable when this method returns, so it's recommend to drop all references to the + * instance right after calling {@link #close()}. + */ + public void close() { + chatManager.closeChat(this); + listeners.clear(); + } + /** * Returns an unmodifiable collection of all of the listeners registered with this chat. * diff --git a/core/src/main/java/org/jivesoftware/smack/ChatManager.java b/core/src/main/java/org/jivesoftware/smack/ChatManager.java index 6d598b8ac..151d1fe87 100644 --- a/core/src/main/java/org/jivesoftware/smack/ChatManager.java +++ b/core/src/main/java/org/jivesoftware/smack/ChatManager.java @@ -227,6 +227,13 @@ public class ChatManager { return chat; } + void closeChat(Chat chat) { + threadChats.remove(chat.getThreadID()); + String userJID = chat.getParticipant(); + jidChats.remove(userJID); + baseJidChats.remove(StringUtils.parseBareAddress(userJID)); + } + private Chat createChat(Message message) { String threadID = message.getThread(); if(threadID == null) {