private void fireNewChatState(Chat chat, ChatState state) {
   for (MessageListener listener : chat.getListeners()) {
     if (listener instanceof ChatStateListener) {
       ((ChatStateListener) listener).stateChanged(chat, state);
     }
   }
 }
  /**
   * Sets the current state of the provided chat. This method will send an empty bodied Message
   * packet with the state attached as a {@link z.org.jivesoftware.smack.packet.PacketExtension}, 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 z.org.jivesoftware.smack.XMPPException when there is an error sending the message
   *     packet.
   */
  public void setCurrentState(ChatState newState, Chat chat) throws XMPPException {
    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);
  }
 public void chatCreated(final Chat chat, boolean createdLocally) {
   chat.addMessageListener(this);
 }