mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-29 23:42:06 +01:00
Update to track last chat state of a chat so that the user is not updated more than once, and the XEP is not violated.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6711 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
1426964334
commit
da9a0e4543
3 changed files with 37 additions and 6 deletions
|
@ -169,4 +169,12 @@ public class Chat {
|
||||||
listener.processMessage(this, message);
|
listener.processMessage(this, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return obj instanceof Chat
|
||||||
|
&& threadID.equals(((Chat)obj).getThreadID())
|
||||||
|
&& participant.equals(((Chat)obj).getParticipant());
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -177,7 +177,7 @@ public class ChatManager {
|
||||||
return jidChats.get(userJID);
|
return jidChats.get(userJID);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Chat getThreadChat(String thread) {
|
public Chat getThreadChat(String thread) {
|
||||||
return threadChats.get(thread);
|
return threadChats.get(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
package org.jivesoftware.smackx;
|
package org.jivesoftware.smackx;
|
||||||
|
|
||||||
import org.jivesoftware.smack.*;
|
import org.jivesoftware.smack.*;
|
||||||
|
import org.jivesoftware.smack.util.collections.ReferenceMap;
|
||||||
import org.jivesoftware.smack.filter.PacketFilter;
|
import org.jivesoftware.smack.filter.PacketFilter;
|
||||||
import org.jivesoftware.smack.filter.NotFilter;
|
import org.jivesoftware.smack.filter.NotFilter;
|
||||||
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
||||||
|
@ -46,6 +47,9 @@ public class ChatStateManager {
|
||||||
private static Map<XMPPConnection, ChatStateManager> managers =
|
private static Map<XMPPConnection, ChatStateManager> managers =
|
||||||
new WeakHashMap<XMPPConnection, ChatStateManager>();
|
new WeakHashMap<XMPPConnection, ChatStateManager>();
|
||||||
|
|
||||||
|
private static PacketFilter filter = new NotFilter(
|
||||||
|
new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
@ -72,13 +76,17 @@ public class ChatStateManager {
|
||||||
|
|
||||||
private IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor();
|
private IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps chat to last chat state.
|
||||||
|
*/
|
||||||
|
private Map<Chat, ChatState> chatStates = new ReferenceMap<Chat, ChatState>(ReferenceMap.WEAK,
|
||||||
|
ReferenceMap.HARD);
|
||||||
|
|
||||||
private ChatStateManager(XMPPConnection connection) {
|
private ChatStateManager(XMPPConnection connection) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
PacketFilter filter = new NotFilter(
|
|
||||||
new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
|
|
||||||
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor,
|
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor,
|
||||||
filter);
|
filter);
|
||||||
connection.getChatManager().addChatListener(incomingInterceptor);
|
connection.getChatManager().addChatListener(incomingInterceptor);
|
||||||
|
@ -98,6 +106,9 @@ public class ChatStateManager {
|
||||||
* packet.
|
* packet.
|
||||||
*/
|
*/
|
||||||
public void setCurrentState(ChatState newState, Chat chat) throws XMPPException {
|
public void setCurrentState(ChatState newState, Chat chat) throws XMPPException {
|
||||||
|
if(!updateChatState(chat, newState)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
Message message = new Message();
|
Message message = new Message();
|
||||||
ChatStateExtension extension = new ChatStateExtension(newState);
|
ChatStateExtension extension = new ChatStateExtension(newState);
|
||||||
message.addExtension(extension);
|
message.addExtension(extension);
|
||||||
|
@ -105,6 +116,15 @@ public class ChatStateManager {
|
||||||
chat.sendMessage(message);
|
chat.sendMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
private void fireNewChatState(Chat chat, ChatState state) {
|
private void fireNewChatState(Chat chat, ChatState state) {
|
||||||
for (MessageListener listener : chat.getListeners()) {
|
for (MessageListener listener : chat.getListeners()) {
|
||||||
if (listener instanceof ChatStateListener) {
|
if (listener instanceof ChatStateListener) {
|
||||||
|
@ -116,13 +136,16 @@ public class ChatStateManager {
|
||||||
private class OutgoingMessageInterceptor implements PacketInterceptor {
|
private class OutgoingMessageInterceptor implements PacketInterceptor {
|
||||||
|
|
||||||
public void interceptPacket(Packet packet) {
|
public void interceptPacket(Packet packet) {
|
||||||
if (!(packet instanceof Message)) {
|
Message message = (Message) packet;
|
||||||
|
Chat chat = connection.getChatManager().getThreadChat(message.getThread());
|
||||||
|
if (chat == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Message message = (Message) packet;
|
if (updateChatState(chat, ChatState.active)) {
|
||||||
message.addExtension(new ChatStateExtension(ChatState.active));
|
message.addExtension(new ChatStateExtension(ChatState.active));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class IncomingMessageInterceptor implements ChatManagerListener, MessageListener {
|
private class IncomingMessageInterceptor implements ChatManagerListener, MessageListener {
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue