mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 09:39:39 +02:00
Improve MUC message and presence listeners
instead of using a PacketListener, which means that the user has to downcast the Packet to Message, we now use a Listener which callback parameter is already Message/Presence. It is necessary to introduce MessageListener and PresenceListener, which are interfaces that have a callback for Message/Presence instead of Packet. The 'old' MessageListener is renamed to ChatMessageListener. Use Generics in ConnectionDetachedPacketCollector.
This commit is contained in:
parent
38582eed84
commit
e835df5641
13 changed files with 178 additions and 65 deletions
|
@ -40,7 +40,7 @@ public class Chat {
|
|||
private ChatManager chatManager;
|
||||
private String threadID;
|
||||
private String participant;
|
||||
private final Set<MessageListener> listeners = new CopyOnWriteArraySet<MessageListener>();
|
||||
private final Set<ChatMessageListener> listeners = new CopyOnWriteArraySet<ChatMessageListener>();
|
||||
|
||||
/**
|
||||
* Creates a new chat with the specified user and thread ID.
|
||||
|
@ -118,7 +118,7 @@ public class Chat {
|
|||
*
|
||||
* @param listener a packet listener.
|
||||
*/
|
||||
public void addMessageListener(MessageListener listener) {
|
||||
public void addMessageListener(ChatMessageListener listener) {
|
||||
if(listener == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ public class Chat {
|
|||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeMessageListener(MessageListener listener) {
|
||||
public void removeMessageListener(ChatMessageListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
|
@ -145,7 +145,7 @@ public class Chat {
|
|||
*
|
||||
* @return an unmodifiable collection of all of the listeners registered with this chat.
|
||||
*/
|
||||
public Collection<MessageListener> getListeners() {
|
||||
public Collection<ChatMessageListener> getListeners() {
|
||||
return Collections.unmodifiableCollection(listeners);
|
||||
}
|
||||
|
||||
|
@ -174,7 +174,7 @@ public class Chat {
|
|||
// probably never had one.
|
||||
message.setThread(threadID);
|
||||
|
||||
for (MessageListener listener : listeners) {
|
||||
for (ChatMessageListener listener : listeners) {
|
||||
listener.processMessage(this, message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -206,7 +206,7 @@ public class ChatManager extends Manager{
|
|||
* @param listener the listener which will listen for new messages from this chat.
|
||||
* @return the created chat.
|
||||
*/
|
||||
public Chat createChat(String userJID, MessageListener listener) {
|
||||
public Chat createChat(String userJID, ChatMessageListener listener) {
|
||||
return createChat(userJID, null, listener);
|
||||
}
|
||||
|
||||
|
@ -218,7 +218,7 @@ public class ChatManager extends Manager{
|
|||
* @param listener the listener to add to the chat
|
||||
* @return the created chat.
|
||||
*/
|
||||
public Chat createChat(String userJID, String thread, MessageListener listener) {
|
||||
public Chat createChat(String userJID, String thread, ChatMessageListener listener) {
|
||||
if (thread == null) {
|
||||
thread = nextID();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2003-2007 Jive Software.
|
||||
*
|
||||
* 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.smack;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface ChatMessageListener {
|
||||
void processMessage(Chat chat, Message message);
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2003-2007 Jive Software.
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -23,5 +23,5 @@ import org.jivesoftware.smack.packet.Message;
|
|||
*
|
||||
*/
|
||||
public interface MessageListener {
|
||||
void processMessage(Chat chat, Message message);
|
||||
void processMessage(Message message);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* 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.smack;
|
||||
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface PresenceListener {
|
||||
void processPresence(Presence presence);
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
*
|
||||
* 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.smack.filter;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
|
||||
/**
|
||||
* Filters message stanzas which have at least one body
|
||||
*/
|
||||
public class MessageWithSubjectFilter extends FlexiblePacketTypeFilter<Message> {
|
||||
|
||||
public static final PacketFilter INSTANCE = new MessageWithSubjectFilter();
|
||||
|
||||
private MessageWithSubjectFilter() {
|
||||
super(Message.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean acceptSpecific(Message message) {
|
||||
// Accept only messages which have a subject set
|
||||
return message.getSubject() != null;
|
||||
}
|
||||
|
||||
}
|
|
@ -386,7 +386,7 @@ public class ChatConnectionTest {
|
|||
|
||||
class TestChatManagerListener implements ChatManagerListener {
|
||||
private Chat newChat;
|
||||
private MessageListener listener;
|
||||
private ChatMessageListener listener;
|
||||
|
||||
public TestChatManagerListener(TestMessageListener msgListener) {
|
||||
listener = msgListener;
|
||||
|
@ -423,7 +423,7 @@ public class ChatConnectionTest {
|
|||
}
|
||||
}
|
||||
|
||||
private class TestMessageListener implements MessageListener {
|
||||
private class TestMessageListener implements ChatMessageListener {
|
||||
private Chat msgChat;
|
||||
private int counter = 0;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue