2006-11-23 02:51:00 +01:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2006-11-23 02:51:00 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2006-11-23 02:51:00 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
package org.jivesoftware.smackx.chatstates;
|
2006-11-23 02:51:00 +01:00
|
|
|
|
2012-11-22 03:51:51 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
2014-10-12 19:19:44 +02:00
|
|
|
import org.jivesoftware.smack.MessageListener;
|
2014-03-19 14:22:20 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2014-03-10 09:45:50 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2014-03-09 11:19:44 +01:00
|
|
|
import org.jivesoftware.smack.Manager;
|
2015-01-22 13:53:50 +01:00
|
|
|
import org.jivesoftware.smack.chat.Chat;
|
|
|
|
import org.jivesoftware.smack.chat.ChatManager;
|
|
|
|
import org.jivesoftware.smack.chat.ChatManagerListener;
|
|
|
|
import org.jivesoftware.smack.chat.ChatMessageListener;
|
2006-11-23 22:10:59 +01:00
|
|
|
import org.jivesoftware.smack.filter.NotFilter;
|
|
|
|
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
2012-11-22 03:51:51 +01:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
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.PacketExtension;
|
2014-02-15 11:35:08 +01:00
|
|
|
import org.jivesoftware.smackx.chatstates.packet.ChatStateExtension;
|
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
2006-11-23 22:10:59 +01:00
|
|
|
|
2006-11-23 02:51:00 +01:00
|
|
|
/**
|
2014-03-10 09:45:50 +01:00
|
|
|
* Handles chat state for all chats on a particular XMPPConnection. This class manages both the
|
2014-03-17 10:26:39 +01:00
|
|
|
* packet extensions and the disco response necessary for compliance with
|
2006-11-23 22:10:59 +01:00
|
|
|
* <a href="http://www.xmpp.org/extensions/xep-0085.html">XEP-0085</a>.
|
2006-11-23 02:51:00 +01:00
|
|
|
*
|
2014-03-10 09:45:50 +01:00
|
|
|
* NOTE: {@link org.jivesoftware.smackx.chatstates.ChatStateManager#getInstance(org.jivesoftware.smack.XMPPConnection)}
|
2007-06-05 06:38:29 +02:00
|
|
|
* needs to be called in order for the listeners to be registered appropriately with the connection.
|
|
|
|
* If this does not occur you will not receive the update notifications.
|
|
|
|
*
|
2006-12-01 19:42:33 +01:00
|
|
|
* @author Alexander Wenckus
|
2014-02-15 11:35:08 +01:00
|
|
|
* @see org.jivesoftware.smackx.chatstates.ChatState
|
|
|
|
* @see org.jivesoftware.smackx.chatstates.packet.ChatStateExtension
|
2006-11-23 02:51:00 +01:00
|
|
|
*/
|
2014-03-09 11:19:44 +01:00
|
|
|
public class ChatStateManager extends Manager {
|
|
|
|
public static final String NAMESPACE = "http://jabber.org/protocol/chatstates";
|
2006-11-23 02:51:00 +01:00
|
|
|
|
2014-03-10 09:45:50 +01:00
|
|
|
private static final Map<XMPPConnection, ChatStateManager> INSTANCES =
|
|
|
|
new WeakHashMap<XMPPConnection, ChatStateManager>();
|
2006-11-23 22:10:59 +01:00
|
|
|
|
2014-03-09 11:19:44 +01:00
|
|
|
private static final PacketFilter filter = new NotFilter(new PacketExtensionFilter(NAMESPACE));
|
2007-01-17 20:36:45 +01:00
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
/**
|
2014-03-10 09:45:50 +01:00
|
|
|
* Returns the ChatStateManager related to the XMPPConnection and it will create one if it does
|
2006-11-23 22:10:59 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-03-10 09:45:50 +01:00
|
|
|
public static synchronized ChatStateManager getInstance(final XMPPConnection connection) {
|
2014-03-09 11:19:44 +01:00
|
|
|
ChatStateManager manager = INSTANCES.get(connection);
|
|
|
|
if (manager == null) {
|
2006-12-01 19:42:33 +01:00
|
|
|
manager = new ChatStateManager(connection);
|
|
|
|
}
|
|
|
|
return manager;
|
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.
|
|
|
|
*/
|
2014-03-17 10:26:39 +01:00
|
|
|
private final Map<Chat, ChatState> chatStates = new WeakHashMap<Chat, ChatState>();
|
2007-01-17 20:36:45 +01:00
|
|
|
|
2014-03-17 20:46:08 +01:00
|
|
|
private final ChatManager chatManager;
|
|
|
|
|
2014-03-10 09:45:50 +01:00
|
|
|
private ChatStateManager(XMPPConnection connection) {
|
2014-03-09 11:19:44 +01:00
|
|
|
super(connection);
|
2014-03-17 20:46:08 +01:00
|
|
|
chatManager = ChatManager.getInstanceFor(connection);
|
|
|
|
chatManager.addOutgoingMessageInterceptor(outgoingInterceptor, filter);
|
|
|
|
chatManager.addChatListener(incomingInterceptor);
|
2007-01-17 18:35:45 +01:00
|
|
|
|
2014-03-09 11:19:44 +01:00
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
|
|
|
|
INSTANCES.put(connection, this);
|
2006-11-23 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2014-03-09 11:19:44 +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.
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2006-11-23 02:51:00 +01:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public void setCurrentState(ChatState newState, Chat chat) throws NotConnectedException, InterruptedException {
|
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;
|
|
|
|
|
2014-03-09 11:19:44 +01:00
|
|
|
return connection().equals(that.connection());
|
2007-06-05 00:59:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
2014-03-09 11:19:44 +01:00
|
|
|
return connection().hashCode();
|
2007-06-05 00:59:20 +02:00
|
|
|
}
|
|
|
|
|
2014-03-17 10:26:39 +01:00
|
|
|
private synchronized boolean updateChatState(Chat chat, ChatState newState) {
|
2007-01-17 20:36:45 +01:00
|
|
|
ChatState lastChatState = chatStates.get(chat);
|
2007-06-05 22:30:22 +02:00
|
|
|
if (lastChatState != newState) {
|
|
|
|
chatStates.put(chat, newState);
|
2007-01-17 20:36:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
private void fireNewChatState(Chat chat, ChatState state) {
|
2014-10-12 18:01:19 +02:00
|
|
|
for (ChatMessageListener 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 19:19:44 +02:00
|
|
|
private class OutgoingMessageInterceptor implements MessageListener {
|
2006-11-23 22:10:59 +01:00
|
|
|
|
2014-10-12 19:19:44 +02:00
|
|
|
@Override
|
|
|
|
public void processMessage(Message message) {
|
2014-03-17 20:46:08 +01:00
|
|
|
Chat chat = chatManager.getThreadChat(message.getThread());
|
2007-01-17 20:36:45 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-12 18:01:19 +02:00
|
|
|
private class IncomingMessageInterceptor implements ChatManagerListener, ChatMessageListener {
|
2006-11-23 22:10:59 +01:00
|
|
|
|
|
|
|
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) {
|
2014-03-09 11:19:44 +01:00
|
|
|
PacketExtension extension = message.getExtension(NAMESPACE);
|
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
|
|
|
}
|