Code complete chat state manager

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6283 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-12-01 18:42:33 +00:00 committed by alex
parent dd754e5a7f
commit 5658fb705a
3 changed files with 59 additions and 25 deletions

View File

@ -31,11 +31,35 @@
</extensionProvider> </extensionProvider>
<!-- Chat State --> <!-- Chat State -->
<!--<extensionProvider>--> <extensionProvider>
<!--<elementName>active</elementName>--> <elementName>active</elementName>
<!--<namespace>http://jabber.org/protocol/chatstates</namespace>--> <namespace>http://jabber.org/protocol/chatstates</namespace>
<!--<className></className>--> <className>org.jivesoftware.smackx.packet.ChatStateExtension.Provider</className>
<!--</extensionProvider>--> </extensionProvider>
<extensionProvider>
<elementName>composing</elementName>
<namespace>http://jabber.org/protocol/chatstates</namespace>
<className>org.jivesoftware.smackx.packet.ChatStateExtension.Provider</className>
</extensionProvider>
<extensionProvider>
<elementName>paused</elementName>
<namespace>http://jabber.org/protocol/chatstates</namespace>
<className>org.jivesoftware.smackx.packet.ChatStateExtension.Provider</className>
</extensionProvider>
<extensionProvider>
<elementName>inactive</elementName>
<namespace>http://jabber.org/protocol/chatstates</namespace>
<className>org.jivesoftware.smackx.packet.ChatStateExtension.Provider</className>
</extensionProvider>
<extensionProvider>
<elementName>gone</elementName>
<namespace>http://jabber.org/protocol/chatstates</namespace>
<className>org.jivesoftware.smackx.packet.ChatStateExtension.Provider</className>
</extensionProvider>
<!-- XHTML --> <!-- XHTML -->
<extensionProvider> <extensionProvider>

View File

@ -38,9 +38,9 @@ import java.util.Collection;
* packet extensions and the disco response neccesary for compliance with * packet extensions and the disco response neccesary for compliance with
* <a href="http://www.xmpp.org/extensions/xep-0085.html">XEP-0085</a>. * <a href="http://www.xmpp.org/extensions/xep-0085.html">XEP-0085</a>.
* *
* @author Alexander Wenckus
* @see org.jivesoftware.smackx.ChatState * @see org.jivesoftware.smackx.ChatState
* @see org.jivesoftware.smackx.packet.ChatStateExtension * @see org.jivesoftware.smackx.packet.ChatStateExtension
* @author Alexander Wenckus
*/ */
public class ChatStateManager { 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 * Returns the ChatStateManager related to the XMPPConnection and it will create one if it does
* not yet exist. * 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. * @return the ChatStateManager related the the connection.
*/ */
public static ChatStateManager getInstance(XMPPConnection connection) { public static ChatStateManager getInstance(final XMPPConnection connection) {
ChatStateManager manager = managers.get(connection); synchronized (connection) {
if(manager == null) { ChatStateManager manager = managers.get(connection);
manager = new ChatStateManager(connection); if (manager == null) {
manager.init(); manager = new ChatStateManager(connection);
managers.put(connection, manager); manager.init();
} managers.put(connection, manager);
}
return manager; return manager;
}
} }
private XMPPConnection connection; private XMPPConnection connection;
@ -89,8 +91,9 @@ public class ChatStateManager {
* *
* @param newState the new state of the chat * @param newState the new state of the chat
* @param chat the chat. * @param chat the chat.
* @throws org.jivesoftware.smack.XMPPException when there is an error sending the message * @throws org.jivesoftware.smack.XMPPException
* packet. * when there is an error sending the message
* packet.
*/ */
public void setCurrentState(ChatState newState, Chat chat) throws XMPPException { public void setCurrentState(ChatState newState, Chat chat) throws XMPPException {
Message message = new Message(); Message message = new Message();
@ -102,9 +105,9 @@ public class ChatStateManager {
private void fireNewChatState(Chat chat, ChatState state) { private void fireNewChatState(Chat chat, ChatState state) {
Collection<MessageListener> listeners = chat.getListeners(); Collection<MessageListener> listeners = chat.getListeners();
for(MessageListener listener : listeners) { for (MessageListener listener : listeners) {
if(listener instanceof ChatStateListener) { if (listener instanceof ChatStateListener) {
((ChatStateListener)listener).stateChanged(chat, state); ((ChatStateListener) listener).stateChanged(chat, state);
} }
} }
} }
@ -115,7 +118,7 @@ public class ChatStateManager {
if (!(packet instanceof Message)) { if (!(packet instanceof Message)) {
return; return;
} }
Message message = (Message)packet; Message message = (Message) packet;
message.addExtension(new ChatStateExtension(ChatState.active)); message.addExtension(new ChatStateExtension(ChatState.active));
} }
} }
@ -129,7 +132,7 @@ public class ChatStateManager {
public void processMessage(Chat chat, Message message) { public void processMessage(Chat chat, Message message) {
PacketExtension extension PacketExtension extension
= message.getExtension("http://jabber.org/protocol/chatstates"); = message.getExtension("http://jabber.org/protocol/chatstates");
if(extension == null) { if (extension == null) {
return; return;
} }

View File

@ -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 * Represents a chat state which is an extension to message packets which is used to indicate
* the current status of a chat participant. * the current status of a chat participant.
* *
* @see org.jivesoftware.smackx.ChatState
* @author Alexander Wenckus * @author Alexander Wenckus
* @see org.jivesoftware.smackx.ChatState
*/ */
public class ChatStateExtension implements PacketExtension { public class ChatStateExtension implements PacketExtension {
@ -57,10 +57,17 @@ public class ChatStateExtension implements PacketExtension {
return "<" + getElementName() + " xmlns=\"" + getNamespace() + "\" />"; return "<" + getElementName() + " xmlns=\"" + getNamespace() + "\" />";
} }
public class Provider implements PacketExtensionProvider { public static class Provider implements PacketExtensionProvider {
public PacketExtension parseExtension(XmlPullParser parser) throws Exception { 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);
} }
} }
} }