Search in sources :

Example 1 with ChatStateExtension

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

the class ChatStateManager method setCurrentState.

/**
     * Sets the current state of the provided chat. This method will send an empty bodied Message
     * stanza(/packet) with the state attached as a {@link org.jivesoftware.smack.packet.ExtensionElement}, if
     * and only if the new chat state is different than the last state.
     *
     * @param newState the new state of the chat
     * @param chat the chat.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void setCurrentState(ChatState newState, org.jivesoftware.smack.chat.Chat chat) throws NotConnectedException, InterruptedException {
    if (chat == null || newState == null) {
        throw new IllegalArgumentException("Arguments cannot be null.");
    }
    if (!updateChatState(chat, newState)) {
        return;
    }
    Message message = new Message();
    ChatStateExtension extension = new ChatStateExtension(newState);
    message.addExtension(extension);
    chat.sendMessage(message);
}
Also used : Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension)

Example 2 with ChatStateExtension

use of org.jivesoftware.smackx.chatstates.packet.ChatStateExtension 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 3 with ChatStateExtension

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

the class ChatStateManager method updateChatState.

/**
     * Update chat state information and send message if necessary.
     *
     * @param account
     * @param user
     * @param chatState
     */
private void updateChatState(String account, String user, ChatState chatState) {
    if (!SettingsManager.chatsStateNotification() || sent.get(account, user) == chatState)
        return;
    AbstractChat chat = MessageManager.getInstance().getChat(account, user);
    if (chat == null || !isSupported(chat, false))
        return;
    sent.put(chat.getAccount(), chat.getUser(), chatState);
    Message message = new Message();
    message.setType(chat.getType());
    message.setTo(chat.getTo());
    message.addExtension(new ChatStateExtension(chatState));
    try {
        ConnectionManager.getInstance().sendStanza(account, message);
    } catch (NetworkException e) {
    // Just ignore it.
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension) AbstractChat(com.xabber.android.data.message.AbstractChat) NetworkException(com.xabber.android.data.NetworkException)

Example 4 with ChatStateExtension

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

the class ChatStateManager method updateOutgoingMessage.

/**
     * Update outgoing message before sending.
     *
     * @param chat
     * @param message
     */
public void updateOutgoingMessage(AbstractChat chat, Message message) {
    if (!isSupported(chat, true))
        return;
    message.addExtension(new ChatStateExtension(ChatState.active));
    sent.put(chat.getAccount(), chat.getUser(), ChatState.active);
    cancelPauseIntent(chat.getAccount(), chat.getUser());
}
Also used : ChatStateExtension(org.jivesoftware.smackx.chatstates.packet.ChatStateExtension)

Example 5 with ChatStateExtension

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

the class ChatStateManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, final String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem))
        return;
    final String resource = Jid.getResource(packet.getFrom());
    if (resource == null)
        return;
    final String account = ((AccountItem) connection).getAccount();
    if (packet instanceof Presence) {
        Presence presence = (Presence) packet;
        if (presence.getType() != Type.unavailable)
            return;
        chatStates.remove(account, bareAddress, resource);
        removeCallback(account, bareAddress, resource);
        supports.remove(account, bareAddress, resource);
    } else if (packet instanceof Message) {
        boolean support = false;
        for (ExtensionElement extension : packet.getExtensions()) if (extension instanceof ChatStateExtension) {
            removeCallback(account, bareAddress, resource);
            ChatState chatState = ((ChatStateExtension) extension).getChatState();
            chatStates.put(account, bareAddress, resource, chatState);
            if (chatState != ChatState.active) {
                Runnable runnable = new Runnable() {

                    @Override
                    public void run() {
                        if (this != stateCleaners.get(account, bareAddress, resource))
                            return;
                        chatStates.remove(account, bareAddress, resource);
                        removeCallback(account, bareAddress, resource);
                        RosterManager.getInstance().onContactChanged(account, bareAddress);
                    }
                };
                handler.postDelayed(runnable, REMOVE_STATE_DELAY);
                stateCleaners.put(account, bareAddress, resource, runnable);
            }
            RosterManager.getInstance().onContactChanged(account, bareAddress);
            support = true;
            break;
        }
        Message message = (Message) packet;
        if (message.getType() != Message.Type.chat && message.getType() != Message.Type.groupchat)
            return;
        if (support)
            supports.put(account, bareAddress, resource, true);
        else if (supports.get(account, bareAddress, resource) == null)
            // Disable only if there no information about support.
            supports.put(account, bareAddress, 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) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

ChatStateExtension (org.jivesoftware.smackx.chatstates.packet.ChatStateExtension)5 Message (org.jivesoftware.smack.packet.Message)3 ChatState (org.jivesoftware.smackx.chatstates.ChatState)2 NetworkException (com.xabber.android.data.NetworkException)1 AccountItem (com.xabber.android.data.account.AccountItem)1 AbstractChat (com.xabber.android.data.message.AbstractChat)1 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)1 Presence (org.jivesoftware.smack.packet.Presence)1