2006-11-23 02:51:00 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision: 2407 $
|
|
|
|
* $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2006-11-23 02:51:00 +01:00
|
|
|
*
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smackx;
|
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
import org.jivesoftware.smack.*;
|
2007-01-17 20:36:45 +01:00
|
|
|
import org.jivesoftware.smack.util.collections.ReferenceMap;
|
2006-11-23 22:10:59 +01:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
|
|
|
import org.jivesoftware.smack.filter.NotFilter;
|
|
|
|
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
2006-11-23 02:51:00 +01:00
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2006-11-23 22:10:59 +01:00
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
|
|
|
import org.jivesoftware.smackx.packet.ChatStateExtension;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
2006-11-23 02:51:00 +01:00
|
|
|
|
|
|
|
/**
|
2006-11-23 22:10:59 +01:00
|
|
|
* Handles chat state for all chats on a particular XMPPConnection. This class manages both the
|
|
|
|
* packet extensions and the disco response neccesary for compliance with
|
|
|
|
* <a href="http://www.xmpp.org/extensions/xep-0085.html">XEP-0085</a>.
|
2006-11-23 02:51:00 +01:00
|
|
|
*
|
2006-12-01 19:42:33 +01:00
|
|
|
* @author Alexander Wenckus
|
2006-11-23 22:10:59 +01:00
|
|
|
* @see org.jivesoftware.smackx.ChatState
|
|
|
|
* @see org.jivesoftware.smackx.packet.ChatStateExtension
|
2006-11-23 02:51:00 +01:00
|
|
|
*/
|
|
|
|
public class ChatStateManager {
|
|
|
|
|
2007-06-05 00:59:20 +02:00
|
|
|
private static final Map<XMPPConnection, ChatStateManager> managers =
|
2006-11-23 22:10:59 +01:00
|
|
|
new WeakHashMap<XMPPConnection, ChatStateManager>();
|
|
|
|
|
2007-06-05 00:59:20 +02:00
|
|
|
private static final PacketFilter filter = new NotFilter(
|
2007-01-17 20:36:45 +01:00
|
|
|
new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
|
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
/**
|
|
|
|
* Returns the ChatStateManager related to the XMPPConnection and it will create one if it does
|
|
|
|
* not yet exist.
|
|
|
|
*
|
2006-12-01 19:42:33 +01:00
|
|
|
* @param connection the connection to return the ChatStateManager
|
2006-11-23 22:10:59 +01:00
|
|
|
* @return the ChatStateManager related the the connection.
|
|
|
|
*/
|
2006-12-01 19:42:33 +01:00
|
|
|
public static ChatStateManager getInstance(final XMPPConnection connection) {
|
2007-06-05 00:59:20 +02:00
|
|
|
if(connection == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
synchronized (managers) {
|
2006-12-01 19:42:33 +01:00
|
|
|
ChatStateManager manager = managers.get(connection);
|
|
|
|
if (manager == null) {
|
|
|
|
manager = new ChatStateManager(connection);
|
|
|
|
manager.init();
|
|
|
|
managers.put(connection, manager);
|
|
|
|
}
|
2006-11-23 22:10:59 +01:00
|
|
|
|
2006-12-01 19:42:33 +01:00
|
|
|
return manager;
|
|
|
|
}
|
2006-11-23 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2007-06-05 00:59:20 +02:00
|
|
|
private final XMPPConnection connection;
|
2006-11-23 22:10:59 +01:00
|
|
|
|
2007-06-05 00:59:20 +02:00
|
|
|
private final OutgoingMessageInterceptor outgoingInterceptor = new OutgoingMessageInterceptor();
|
2006-11-23 22:10:59 +01:00
|
|
|
|
2007-06-05 00:59:20 +02:00
|
|
|
private final IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor();
|
2006-11-23 22:10:59 +01:00
|
|
|
|
2007-01-17 20:36:45 +01:00
|
|
|
/**
|
|
|
|
* Maps chat to last chat state.
|
|
|
|
*/
|
2007-06-05 00:59:20 +02:00
|
|
|
private final Map<Chat, ChatState> chatStates =
|
|
|
|
new ReferenceMap<Chat, ChatState>(ReferenceMap.WEAK, ReferenceMap.HARD);
|
2007-01-17 20:36:45 +01:00
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
private ChatStateManager(XMPPConnection connection) {
|
|
|
|
this.connection = connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void init() {
|
|
|
|
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor,
|
|
|
|
filter);
|
|
|
|
connection.getChatManager().addChatListener(incomingInterceptor);
|
2007-01-17 18:35:45 +01:00
|
|
|
|
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection)
|
|
|
|
.addFeature("http://jabber.org/protocol/chatstates");
|
2006-11-23 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2006-11-23 02:51:00 +01:00
|
|
|
/**
|
2006-11-23 22:10:59 +01:00
|
|
|
* Sets the current state of the provided chat. This method will send an empty bodied Message
|
2007-06-05 00:59:20 +02:00
|
|
|
* packet with the state attached as a {@link org.jivesoftware.smack.packet.PacketExtension}, if
|
|
|
|
* and only if the new chat state is different than the last state.
|
2006-11-23 22:10:59 +01:00
|
|
|
*
|
|
|
|
* @param newState the new state of the chat
|
|
|
|
* @param chat the chat.
|
2006-12-01 19:42:33 +01:00
|
|
|
* @throws org.jivesoftware.smack.XMPPException
|
|
|
|
* when there is an error sending the message
|
|
|
|
* packet.
|
2006-11-23 02:51:00 +01:00
|
|
|
*/
|
2006-11-23 22:10:59 +01:00
|
|
|
public void setCurrentState(ChatState newState, Chat chat) throws XMPPException {
|
2007-06-05 00:59:20 +02:00
|
|
|
if(chat == null || newState == null) {
|
|
|
|
throw new IllegalArgumentException("Arguments cannot be null.");
|
|
|
|
}
|
2007-01-17 20:36:45 +01:00
|
|
|
if(!updateChatState(chat, newState)) {
|
|
|
|
return;
|
|
|
|
}
|
2006-11-23 02:51:00 +01:00
|
|
|
Message message = new Message();
|
2006-11-23 22:10:59 +01:00
|
|
|
ChatStateExtension extension = new ChatStateExtension(newState);
|
|
|
|
message.addExtension(extension);
|
|
|
|
|
|
|
|
chat.sendMessage(message);
|
|
|
|
}
|
|
|
|
|
2007-06-05 00:59:20 +02:00
|
|
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
ChatStateManager that = (ChatStateManager) o;
|
|
|
|
|
|
|
|
return connection.equals(that.connection);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
|
|
|
return connection.hashCode();
|
|
|
|
}
|
|
|
|
|
2007-01-17 20:36:45 +01:00
|
|
|
private boolean updateChatState(Chat chat, ChatState newState) {
|
|
|
|
ChatState lastChatState = chatStates.get(chat);
|
|
|
|
if (lastChatState == null || lastChatState != newState) {
|
|
|
|
chatStates.put(chat, lastChatState);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
private void fireNewChatState(Chat chat, ChatState state) {
|
2007-01-17 18:35:45 +01:00
|
|
|
for (MessageListener listener : chat.getListeners()) {
|
2006-12-01 19:42:33 +01:00
|
|
|
if (listener instanceof ChatStateListener) {
|
|
|
|
((ChatStateListener) listener).stateChanged(chat, state);
|
2006-11-23 22:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class OutgoingMessageInterceptor implements PacketInterceptor {
|
|
|
|
|
|
|
|
public void interceptPacket(Packet packet) {
|
2007-01-17 20:36:45 +01:00
|
|
|
Message message = (Message) packet;
|
|
|
|
Chat chat = connection.getChatManager().getThreadChat(message.getThread());
|
|
|
|
if (chat == null) {
|
2006-11-23 22:10:59 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-01-17 20:36:45 +01:00
|
|
|
if (updateChatState(chat, ChatState.active)) {
|
|
|
|
message.addExtension(new ChatStateExtension(ChatState.active));
|
|
|
|
}
|
2006-11-23 22:10:59 +01:00
|
|
|
}
|
2006-11-23 02:51:00 +01:00
|
|
|
}
|
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
private class IncomingMessageInterceptor implements ChatManagerListener, MessageListener {
|
|
|
|
|
|
|
|
public void chatCreated(final Chat chat, boolean createdLocally) {
|
|
|
|
chat.addMessageListener(this);
|
|
|
|
}
|
2006-11-23 02:51:00 +01:00
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
public void processMessage(Chat chat, Message message) {
|
|
|
|
PacketExtension extension
|
|
|
|
= message.getExtension("http://jabber.org/protocol/chatstates");
|
2006-12-01 19:42:33 +01:00
|
|
|
if (extension == null) {
|
2006-11-23 22:10:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatState state;
|
|
|
|
try {
|
|
|
|
state = ChatState.valueOf(extension.getElementName());
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fireNewChatState(chat, state);
|
|
|
|
}
|
|
|
|
}
|
2006-11-23 02:51:00 +01:00
|
|
|
}
|