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>
<!-- Chat State -->
<!--<extensionProvider>-->
<!--<elementName>active</elementName>-->
<!--<namespace>http://jabber.org/protocol/chatstates</namespace>-->
<!--<className></className>-->
<!--</extensionProvider>-->
<extensionProvider>
<elementName>active</elementName>
<namespace>http://jabber.org/protocol/chatstates</namespace>
<className>org.jivesoftware.smackx.packet.ChatStateExtension.Provider</className>
</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 -->
<extensionProvider>

View File

@ -38,9 +38,9 @@ import java.util.Collection;
* packet extensions and the disco response neccesary for compliance with
* <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.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<MessageListener> 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;
}

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
* 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);
}
}
}