From 5658fb705a2ceb72fee23305cbb2b7b00697d4ec Mon Sep 17 00:00:00 2001 From: Alex Wenckus Date: Fri, 1 Dec 2006 18:42:33 +0000 Subject: [PATCH] Code complete chat state manager git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6283 b35dd754-fafc-0310-a699-88a17e54d16e --- build/resources/META-INF/smack.providers | 34 ++++++++++++++--- .../jivesoftware/smackx/ChatStateManager.java | 37 ++++++++++--------- .../smackx/packet/ChatStateExtension.java | 13 +++++-- 3 files changed, 59 insertions(+), 25 deletions(-) diff --git a/build/resources/META-INF/smack.providers b/build/resources/META-INF/smack.providers index 37cbe6f98..4f5b7ecc9 100644 --- a/build/resources/META-INF/smack.providers +++ b/build/resources/META-INF/smack.providers @@ -31,11 +31,35 @@ - - - - - + + active + http://jabber.org/protocol/chatstates + org.jivesoftware.smackx.packet.ChatStateExtension.Provider + + + + composing + http://jabber.org/protocol/chatstates + org.jivesoftware.smackx.packet.ChatStateExtension.Provider + + + + paused + http://jabber.org/protocol/chatstates + org.jivesoftware.smackx.packet.ChatStateExtension.Provider + + + + inactive + http://jabber.org/protocol/chatstates + org.jivesoftware.smackx.packet.ChatStateExtension.Provider + + + + gone + http://jabber.org/protocol/chatstates + org.jivesoftware.smackx.packet.ChatStateExtension.Provider + diff --git a/source/org/jivesoftware/smackx/ChatStateManager.java b/source/org/jivesoftware/smackx/ChatStateManager.java index a8bd9d590..9811b8f91 100644 --- a/source/org/jivesoftware/smackx/ChatStateManager.java +++ b/source/org/jivesoftware/smackx/ChatStateManager.java @@ -38,9 +38,9 @@ import java.util.Collection; * packet extensions and the disco response neccesary for compliance with * XEP-0085. * + * @author Alexander Wenckus * @see org.jivesoftware.smackx.ChatState * @see org.jivesoftware.smackx.packet.ChatStateExtension - * @author Alexander Wenckus */ public class ChatStateManager { @@ -51,18 +51,20 @@ public class ChatStateManager { * Returns the ChatStateManager related to the XMPPConnection and it will create one if it does * not yet exist. * - * @param connection the connection to return the ChatStateManager/ + * @param connection the connection to return the ChatStateManager * @return the ChatStateManager related the the connection. */ - public static ChatStateManager getInstance(XMPPConnection connection) { - ChatStateManager manager = managers.get(connection); - if(manager == null) { - manager = new ChatStateManager(connection); - manager.init(); - managers.put(connection, manager); - } + public static ChatStateManager getInstance(final XMPPConnection connection) { + synchronized (connection) { + ChatStateManager manager = managers.get(connection); + if (manager == null) { + manager = new ChatStateManager(connection); + manager.init(); + managers.put(connection, manager); + } - return manager; + return manager; + } } private XMPPConnection connection; @@ -89,8 +91,9 @@ public class ChatStateManager { * * @param newState the new state of the chat * @param chat the chat. - * @throws org.jivesoftware.smack.XMPPException when there is an error sending the message - * packet. + * @throws org.jivesoftware.smack.XMPPException + * when there is an error sending the message + * packet. */ public void setCurrentState(ChatState newState, Chat chat) throws XMPPException { Message message = new Message(); @@ -102,9 +105,9 @@ public class ChatStateManager { private void fireNewChatState(Chat chat, ChatState state) { Collection listeners = chat.getListeners(); - for(MessageListener listener : listeners) { - if(listener instanceof ChatStateListener) { - ((ChatStateListener)listener).stateChanged(chat, state); + for (MessageListener listener : listeners) { + if (listener instanceof ChatStateListener) { + ((ChatStateListener) listener).stateChanged(chat, state); } } } @@ -115,7 +118,7 @@ public class ChatStateManager { if (!(packet instanceof Message)) { return; } - Message message = (Message)packet; + Message message = (Message) packet; message.addExtension(new ChatStateExtension(ChatState.active)); } } @@ -129,7 +132,7 @@ public class ChatStateManager { public void processMessage(Chat chat, Message message) { PacketExtension extension = message.getExtension("http://jabber.org/protocol/chatstates"); - if(extension == null) { + if (extension == null) { return; } diff --git a/source/org/jivesoftware/smackx/packet/ChatStateExtension.java b/source/org/jivesoftware/smackx/packet/ChatStateExtension.java index 2df204c85..a4da0c3d8 100644 --- a/source/org/jivesoftware/smackx/packet/ChatStateExtension.java +++ b/source/org/jivesoftware/smackx/packet/ChatStateExtension.java @@ -29,8 +29,8 @@ import org.xmlpull.v1.XmlPullParser; * Represents a chat state which is an extension to message packets which is used to indicate * the current status of a chat participant. * - * @see org.jivesoftware.smackx.ChatState * @author Alexander Wenckus + * @see org.jivesoftware.smackx.ChatState */ public class ChatStateExtension implements PacketExtension { @@ -57,10 +57,17 @@ public class ChatStateExtension implements PacketExtension { return "<" + getElementName() + " xmlns=\"" + getNamespace() + "\" />"; } - public class Provider implements PacketExtensionProvider { + public static class Provider implements PacketExtensionProvider { public PacketExtension parseExtension(XmlPullParser parser) throws Exception { - return null; + ChatState state; + try { + state = ChatState.valueOf(parser.getName()); + } + catch (Exception ex) { + state = ChatState.active; + } + return new ChatStateExtension(state); } } }