Class ChatStateManager

  • All Implemented Interfaces:
    ExtensionProtocol, InboundMessageHandler, OutboundMessageHandler, DiscoverableInfo

    public final class ChatStateManager
    extends Manager
    implements ExtensionProtocol, DiscoverableInfo, InboundMessageHandler, OutboundMessageHandler
    This class manages Chat State Notifications, which are used to communicate the status of a user in a chat session, thus indicating whether a chat partner is actively engaged in the chat, composing a message, temporarily paused, inactive, or gone. Chat states can be used in the context of a one-to-one chat session or a multi-user chat room.

    Because the Chat State protocol is relatively simple, the primary purpose of this manager is to enable or disable the Chat State protocol for Service Discovery purposes.

    Furthermore it ensures that every sent message has a chat state notification as required by XEP-0085.

    Sending Chat States

    Setting your own chat state can either be done in a one-to-one chat session or a group chat.

    
     ChatStateManager chatStateManager = xmppSession.getManager(ChatStateManager.class);
     chatStateManager.setChatState(ChatState.COMPOSING, chat);
     

    Receiving Chat States

    If you want to react to chat states of your chat partner(s), just check for chat state extension and deal with it accordingly.

    
     ChatState chatState = message.getExtension(ChatState.class);
     if (chatState == ChatState.COMPOSING) {
     // Contact is typing.
     } else if (chatState == ChatState.PAUSED) {
     // Contact has paused typing.
     }
     
    See Also:
    XEP-0085: Chat State Notifications
    • Method Detail

      • setChatState

        public final boolean setChatState​(ChatState chatState,
                                          Chat chat)
        Sets the chat state for a chat. If this manager is disabled this method has no effect. Before sending chat states in a one-to-one chat, you should check, if the peer supports it, e.g. like that:
        
         chatStateManager.isSupported(chat.getChatPartner()).thenAccept(result -> {
         if (result) {
         chatStateManager.setChatState(chatState, chat);
         }
         });
         
        Parameters:
        chatState - The chat state.
        chat - The chat.
        Returns:
        True, if the chat state has been sent; false, if it has not been sent (e.g. because it is known that the chat partner does not support chat states).
      • isSupported

        public final AsyncResult<Boolean> isSupported​(Jid jid)
        Indicates whether chat state notifications are supported by the peer.
        Parameters:
        jid - The JID.
        Returns:
        An async result indicating whether chat state notifications are supported.
        See Also:
        5.1 Generation of Notifications
      • getNamespace

        public final String getNamespace()
        Description copied from interface: ExtensionProtocol
        The protocol's main namespace. This is used to uniquely identify the extension protocol.
        Specified by:
        getNamespace in interface ExtensionProtocol
        Returns:
        The main namespace.