XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
Asked Answered
P

5

18

I am new to xmpp/asmack in android.

Can anyone please help me in getting the presence of the user's friends ( roster list)

I am using this :

Presence availability = roster.getPresence(user);
Mode userMode = availability.getMode();

What else should I do to get the availability status of each user listed in my roster.

Puce answered 22/1, 2013 at 6:51 Comment(0)
B
19

Just use like this :

Presence availability = roster.getPresence(user);
Mode userMode = availability.getMode();

retrieveState_mode(availability.getMode(),availability.isAvailable());

public static int retrieveState_mode(Mode userMode, boolean isOnline) {
        int userState = 0;
        /** 0 for offline, 1 for online, 2 for away,3 for busy*/
        if(userMode == Mode.dnd) {
            userState = 3;
        } else if (userMode == Mode.away || userMode == Mode.xa) {   
            userState = 2;
        } else if (isOnline) {
            userState = 1;
        }
        return userState;
}

Let me know if you have any problem regarding xmpp/asmack

Boatwright answered 22/1, 2013 at 6:55 Comment(8)
I tried this code, but every time I'm getting 0 (offline) even user status is online! Please suggest me.Launcelot
@RahulUpadhyay Can you please show me your code, so that I can look into it.Boatwright
hi @Puce i am not able to get the presence of the user do we need to enable any settings for this ??Vernation
@Vernation Please share your code snippet and explain what error you are getting with it, then I can help youBoatwright
Hi @GauravArora please look at this pastebin.com/j113RzV6 I am always getting mode of user as 0. Please let me know if i am missing anything ..Vernation
I tried this code, but every time I'm getting 0 (offline) even user status is online! Please suggest me.i can use xmpp/asmack-2010.05.07 libraryViki
Hi @GauravArora i also use same code but i always get false, but in server user online but cannot get correct result. dont know whyMegavolt
what is user in Presence availability = roster.getPresence(user);Saccharometer
E
4

use like this

userFromServer = con.getRoster().getPresence(userID);
userState = retrieveState(userFromServer.getMode(), userFromServer.isAvailable());

public int retrieveState(Mode userMode, boolean isOnline) {
    int userState = XmppFriend.OFFLINE; // default return value
    if (userMode == Mode.dnd) {
        userState = XmppFriend.BUSY;
    } else if (userMode == Mode.away || userMode == Mode.xa) {
        userState = XmppFriend.AWAY;
    } else if (isOnline) {
        userState = XmppFriend.ONLINE;
    }
    return userState;
}
Expediency answered 22/1, 2013 at 7:2 Comment(0)
M
1
        roster.addRosterListener(new RosterListener() {

        public void entriesAdded(Collection<String> param) {}

        public void entriesDeleted(Collection<String> addresses) {
        }
        public void entriesUpdated(Collection<String> addresses) {  
        }
        public void presenceChanged(Presence presence) {

            String user = presence.getFrom();
            Presence bestPresence = roster.getPresence(user);

            Log.d(TAG, "BestPresence: " + user + ": " + bestPresence);

            String[] temp = presence.getFrom().split("\\@");
            Log.d(TAG, "Presence: " + temp[0] + "-" + presence.toString());

            String status = presence.toString();
            // ShowInfoDialog(temp[0]+"is "+status);

            for (int i = 0; i < friendslist.size(); i++) {

                if (temp[0].equalsIgnoreCase(friendslist.get(i).getName())) {

                    friendslist.get(i).setStatus(status);
                    Log.d(TAG, "kilepet/belepet " + friendslist.get(i).getName() + " - " + friendslist.get(i).getStatus());

                    // ShowInfoDialog(friendslist.get(i).getName()+"is "+status);
                    Log.d(TAG, "WATERFAK");

                }
            }

        }

If you use RosterListener, it updates the presence in real time, it works for me just fine.

Macaronic answered 9/8, 2013 at 11:59 Comment(3)
can you explain me the type of friendslist in your code?Psia
Entity Friend containing all data what can have another user (name,email,status,avatar and so on) and I populate the list when I request the roosters after the app. starts and using a third party server for what ejjaberd lacks.Macaronic
Hi @cesztoszule.. I am using the same above method for presence of my roster..but presence changed method is calling more than once like 7-8 times. I am wondering why it is behaving like this. <presence id="NAR9p-198" from="xxxxxxxx@domainim/domain/86xxxxxxxxx5198" type="unavailable"></presence><presence id="NAR9p-199" from="xxxxxxxx@domainim/domain/86xxxxxxxxx5198" type="unavailable"></presence><presence id="NAR9p-200" from="xxxxxxxx@domainim/domain/86xxxxxxxxx5198" type="unavailable"></presence><presence id="NAR9p-201" from="xxxxxxxx@domainim/domain/86xxxxxxxxx5198" type="unavailable"></presMeave
L
0
ConnectToServer(){
    final ProgressDialog dialog = ProgressDialog.show(ChatWindowFragmentActivity.this,
            "Connecting...", "Please wait...", false);
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {


            // Object of XmppClient class
            XmppClient mXmppClient = new XmppClient();

            /*
             * // Create a connection ConnectionConfiguration connConfig =
             * new ConnectionConfiguration(HOST, PORT);
             */
            XMPPConnection connection = null;

            try {
                SmackAndroid.init(ChatWindowFragmentActivity.this);
                connection = mXmppClient.connectionToXmppServer();
            } catch (XMPPException e) {
                // TODO Auto-generated catch block
      //                    setConnection(null, null);
            }
            try {
                mXmppClient.loginUser(connection, USERNAME, PASSWORD);
                Log.i("XMPPChatDemoActivity",
                        "Logged in as" + connection.getUser());
                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);

                setConnection(connection);

                Roster roster = connection.getRoster();

                 /*
                Fetch USER availability
                */


                switch (isUserAvailable(connection)){
                    case 0:
                        imgAvailability.setBackgroundColor(Color.GRAY);
                        break;
                    case 1:
                        imgAvailability.setBackgroundColor(Color.GREEN);
                        break;
                    case 2:
                        imgAvailability.setBackgroundColor(Color.YELLOW);
                        break;
                    case 3:
                        imgAvailability.setBackgroundColor(Color.RED);
                        break;
                    default:
                        break;
                }


                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) {

                    Log.d("XMPPChatDemoActivity",
                            "--------------------------------------");
                    Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                    Log.d("XMPPChatDemoActivity",
                            "User: " + entry.getUser());
                    Log.d("XMPPChatDemoActivity",
                            "Name: " + entry.getName());
                    Log.d("XMPPChatDemoActivity",
                            "Status: " + entry.getStatus());
                    Log.d("XMPPChatDemoActivity",
                            "Type: " + entry.getType());
                    Presence entryPresence = roster.getPresence(entry
                            .getUser());

                    Log.d("XMPPChatDemoActivity", "Presence Status: "
                            + entryPresence.getStatus());
                    Log.d("XMPPChatDemoActivity", "Presence Type: "
                            + entryPresence.getType());

                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available)
                        Log.d("XMPPChatDemoActivity", "Presence AVAILABLE");
                    Log.d("XMPPChatDemoActivity", "Presence : "
                            + entryPresence);
                }



            } catch (XMPPException e) {

                e.printStackTrace();
                Log.e("XMPPChatDemoActivity", "Failed to log in as "
                        + USERNAME);
                Log.e("XMPPChatDemoActivity", e.toString());
                new ShowAlert(ChatWindowFragmentActivity.this,e.getMessage(), false).show(
                        getSupportFragmentManager(), TAG);
   //                    setConnection(null, null);
            }

            dialog.dismiss();
        }
    });
    t.start();
    dialog.show();
}

And your method called inside it.

Launcelot answered 28/5, 2013 at 12:4 Comment(4)
I'm getting this Presence.Mode userMode "null" and boolean isOnline "false"Launcelot
hi @Rahul Upadhyay i am also getting the same issue i have used the solution suggested by Gaurav Arora but it is not working for me Did you find any alternative for the solutionVernation
@Vernation nope, I didn't find anythingLauncelot
@Crazy, I didn't find anything yet. And not worked on it from that day. :( One suggestion for this, use GCM, its included xmpp in it so its good to use it. developer.android.com/google/gcm/ccs.htmlLauncelot
P
0

As my experience before you can see status and other from Presence you need to subscribe the user.

for example: user A want to see status and available status from user B, in this case, user A need to subscribe user B. after that user A can see the user B Presence.

Subscribe Code

try {
            roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
            String userName = responders.getUsers().get(i).getUsername();
            roster.createEntry("userB@domain", userName, null);
            Presence pres = new Presence(Presence.Type.subscribe);
            pres.setFrom("userA@domain");
            connection.sendStanza(pres);
        } catch (Exception e) {
            android.util.Log.e("tag", "unable to add contact: ", e);
        }
Plump answered 7/12, 2015 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.