How to know Typing Status in XMPP openfire using Smack

I am developing chat application by using Openfire XMPP server. I can text chat between two user. But i want to know Typing status when some one is typing message. So i created a class :-

public class typingStatus implements ChatStateListener {

    @Override
    public void processMessage(Chat arg0, Message arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void stateChanged(Chat arg0, ChatState arg1) {
        // TODO Auto-generated method stub
        System.out.println(arg0.getParticipant() + " is " + arg1.name());

    }

}

But i am confuse so that How will it work? I know that i need a packet where i can it in Listener. But i am unable to find that packet.

Please any one suggest, How will it work?

and also what is difference between Smack and asmack?

Thank you!


Solution 1:

To enable ChatStateListener you need to create a custom MessageListener Class

public class MessageListenerImpl implements MessageListener,ChatStateListener  {

    @Override
    public void processMessage(Chat arg0, Message arg1) {
        System.out.println("Received message: " + arg1);

    }

    @Override
    public void stateChanged(Chat arg0, ChatState arg1) {
        if (ChatState.composing.equals(arg1)) {
            Log.d("Chat State",arg0.getParticipant() + " is typing..");
        } else if (ChatState.gone.equals(arg1)) {
            Log.d("Chat State",arg0.getParticipant() + " has left the conversation.");
        } else {
            Log.d("Chat State",arg0.getParticipant() + ": " + arg1.name());
        }

    }

}

Then you create MessageListener object

MessageListener messageListener = new MessageListenerImpl();

And then pass this in the create chat method

Chat newChat = chatmanager.createChat(jabber_id_of_friend, messageListener);

what is difference between Smack and asmack? <-- Check This

Solution 2:

finally I got the solution. I need to use chat listener along with chat manager and also I need to use in built sendcomposingnotification function. No need to use Messageeventrequestlistener interface or any other custom class for this. I added the following lines,,

connection.getChatManager().addChatListener(new ChatManagerListener() {

            @Override
            public void chatCreated(final Chat arg0, final boolean arg1) {
                // TODO Auto-generated method stub

                arg0.addMessageListener(new MessageListener()
                {

                    @Override
                    public void processMessage(Chat arg0, Message arg1) 
                    {
                        // TODO Auto-generated method stub

                        Log.d("TYpe Stat",title[0] + " is typing......");
                        Toast.makeText(getApplicationContext(),title[0] + " is typing......",Toast.LENGTH_SHORT).show();
                    }



                    }
                }); 
            }
        });

and also need to send notification like this..

mem.sendComposingNotification(etRecipient.getText().toString(), message.getPacketID());
        System.out.println("Sending notification");

where mem is type of MessageEventManger. Ref: http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/MessageEventManager.html

Solution 3:

ChatManager chatManager = ChatManager.getInstanceFor(connection);
Chat chat= chatManager.createChat(to, new ChatStateListener() {
      @Override
      public void stateChanged(Chat chat, ChatState state) {
                switch (state){
                        case active:
                             Log.d("state","active");
                             break;
                        case composing:
                             Log.d("state","composing");
                             break;
                        case paused:
                             Log.d("state","paused");
                             break;
                        case inactive:
                             Log.d("state","inactive");
                             break;
                        case gone:
                             Log.d("state","gone");
                             break;
                 }
        }

        @Override
        public void processMessage(Chat chat, Message message) {
               Log.d("processMessage","processMessage");
        }
});

use this code.hope so will work

Solution 4:

i am using chat state listener :

Chat chat = chatManager.createChat(jid,
                        new ChatStateChangedListener());

bind the chatstatelistener with each jid like above , then :

public class ChatStateChangedListener implements ChatStateListener {

    public ChatStateChangedListener() {
        printLog("Chat State Changed Listner Constructor");
    }

    @Override
    public void processMessage(Chat arg0, Message arg1) {

    }


    @Override
    public void stateChanged(Chat chat, ChatState state) {

        if (state.toString().equals(ChatState.composing.toString())) {
                tvLastSeen.setText("Typing...");
            } else if (state.toString().equals(ChatState.paused.toString())) {
                tvLastSeen.setText("paused...");
            } else {
                tvLastSeen.setText("nothing");
            }

        }


    }

}