Search in sources :

Example 1 with ChatState

use of org.jivesoftware.smackx.chatstates.ChatState in project Smack by igniterealtime.

the class ChatStateExtensionProvider method parse.

@Override
public ChatStateExtension parse(XmlPullParser parser, int initialDepth) throws Exception {
    String chatStateString = parser.getName();
    ChatState state = ChatState.valueOf(chatStateString);
    return new ChatStateExtension(state);
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState) ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension)

Example 2 with ChatState

use of org.jivesoftware.smackx.chatstates.ChatState in project xabber-android by redsolution.

the class EligibleForChatMarkerFilter method accept.

/**
 * From XEP-0333, Protocol Format: The Chat Marker MUST have an 'id' which is the 'id' of the
 * message being marked.<br>
 * In order to make Chat Markers works together with XEP-0085 as it said in
 * 8.5 Interaction with Chat States, only messages with <tt>active</tt> chat
 * state are accepted.
 *
 * @param message to be analyzed.
 * @return true if the message contains a stanza Id.
 * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat Markers</a>
 */
@Override
public boolean accept(Stanza message) {
    if (!message.hasStanzaIdSet()) {
        return false;
    }
    if (super.accept(message)) {
        ExtensionElement extension = message.getExtension(ChatStateManager.NAMESPACE);
        String chatStateElementName = extension.getElementName();
        ChatState state;
        try {
            state = ChatState.valueOf(chatStateElementName);
            return (state == ChatState.active);
        } catch (Exception ex) {
            return false;
        }
    }
    return true;
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement)

Example 3 with ChatState

use of org.jivesoftware.smackx.chatstates.ChatState in project xabber-android by redsolution.

the class ChatStateManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (stanza.getFrom() == null) {
        return;
    }
    final Resourcepart resource = stanza.getFrom().getResourceOrNull();
    if (resource == null) {
        return;
    }
    final AccountJid account = ((AccountItem) connection).getAccount();
    final UserJid bareUserJid;
    try {
        bareUserJid = UserJid.from(stanza.getFrom()).getBareUserJid();
    } catch (UserJid.UserJidCreateException e) {
        return;
    }
    if (stanza instanceof Presence) {
        Presence presence = (Presence) stanza;
        if (presence.getType() != Type.unavailable) {
            return;
        }
        chatStates.remove(account.toString(), bareUserJid.toString(), resource);
        removeCallback(account, bareUserJid.getBareJid(), resource);
        supports.remove(account.toString(), bareUserJid.toString(), resource);
    } else if (stanza instanceof Message) {
        boolean support = false;
        for (ExtensionElement extension : stanza.getExtensions()) if (extension instanceof ChatStateExtension) {
            removeCallback(account, bareUserJid.getBareJid(), resource);
            ChatState chatState = ((ChatStateExtension) extension).getChatState();
            chatStates.put(account.toString(), bareUserJid.toString(), resource, chatState);
            if (chatState != ChatState.active) {
                Runnable runnable = new Runnable() {

                    @Override
                    public void run() {
                        if (this != stateCleaners.get(account.toString(), bareUserJid.toString(), resource)) {
                            return;
                        }
                        chatStates.remove(account.toString(), bareUserJid.toString(), resource);
                        removeCallback(account, bareUserJid.getBareJid(), resource);
                        RosterManager.onChatStateChanged(account, bareUserJid);
                    }
                };
                handler.postDelayed(runnable, REMOVE_STATE_DELAY);
                stateCleaners.put(account.toString(), bareUserJid.toString(), resource, runnable);
            }
            RosterManager.onChatStateChanged(account, bareUserJid);
            support = true;
            break;
        }
        Message message = (Message) stanza;
        if (message.getType() != Message.Type.chat && message.getType() != Message.Type.groupchat) {
            return;
        }
        if (support) {
            supports.put(account.toString(), bareUserJid.toString(), resource, true);
        } else if (supports.get(account.toString(), bareUserJid.toString(), resource) == null) {
            // Disable only if there no information about support.
            supports.put(account.toString(), bareUserJid.toString(), resource, false);
        }
    }
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState) Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension) AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) UserJid(com.xabber.android.data.entity.UserJid) Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 4 with ChatState

use of org.jivesoftware.smackx.chatstates.ChatState in project xabber-android by redsolution.

the class ChatStateManager method getChatState.

/**
 * Returns best information chat state for specified bare address.
 *
 * @return <code>null</code> if there is no available information.
 */
public ChatState getChatState(AccountJid account, UserJid bareAddress) {
    Map<Resourcepart, ChatState> map = chatStates.get(account.toString(), bareAddress.toString());
    if (map == null) {
        return null;
    }
    ChatState chatState = null;
    for (ChatState check : map.values()) {
        if (chatState == null || check.compareTo(chatState) < 0) {
            chatState = check;
        }
    }
    return chatState;
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 5 with ChatState

use of org.jivesoftware.smackx.chatstates.ChatState in project xabber-android by redsolution.

the class ChatStateManager method getChatState.

/**
     * Returns best information chat state for specified bare address.
     *
     * @param account
     * @param bareAddress
     * @return <code>null</code> if there is no available information.
     */
public ChatState getChatState(String account, String bareAddress) {
    Map<String, ChatState> map = chatStates.get(account, bareAddress);
    if (map == null)
        return null;
    ChatState chatState = null;
    for (ChatState check : map.values()) if (chatState == null || check.compareTo(chatState) < 0)
        chatState = check;
    return chatState;
}
Also used : ChatState(org.jivesoftware.smackx.chatstates.ChatState)

Aggregations

ChatState (org.jivesoftware.smackx.chatstates.ChatState)10 ChatStateExtension (org.jivesoftware.smackx.chatstates.packet.ChatStateExtension)4 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)3 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 AccountItem (com.xabber.android.data.account.AccountItem)2 Message (org.jivesoftware.smack.packet.Message)2 Presence (org.jivesoftware.smack.packet.Presence)2 Resourcepart (org.jxmpp.jid.parts.Resourcepart)2 AccountJid (com.xabber.android.data.entity.AccountJid)1 UserJid (com.xabber.android.data.entity.UserJid)1 AbstractChat (com.xabber.android.data.message.AbstractChat)1 XmlElement (org.jivesoftware.smack.packet.XmlElement)1