4
votes

There is a openfire server and Android clients (smack). All clients can add each other to buddy/roster list (without authorization, I want user can see each other without accept buddy request). I have some problems of getting the Presence information of the buddy request sender.
Assume there are 2 users - User A, User B.

I can add User B to User A's Roster by:

Roster roster = xmppManager.connection.getRoster();
roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
roster.createEntry("userB", "[email protected]", null);

I can see User B at User A's roster list. Everything is fine so far

There are few problems with User B. I state what is the problem at the code below:

//I have set the available and status of User A by:
//xmppManager.setStatus(available, bundle.getString("new_status"));
...

// That's how I get Roster and Presence of user A
Roster roster = connection.getRoster();

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


    Presence presence = roster.getPresence(entry.getUser());

    // User A always not available even I set User A to available
    Log.e(TAG, "presence.isAvailable() = " + presence.isAvailable());

    // User A's status always empty
    Log.e(TAG, "presence.getStatus() = " + presence.getStatus());


    // User A's getName() always null
    if (entry.getName() != null)
    {
        name = entry.getName();
    }
    else
        Log.e(TAG, "GetName is null");  

}

Do I need to createEntry() at User A?
Or do I need to do something with the buddy request like this?

@Override  
    public void entriesAdded(Collection<String> collection) {
        String user = "";  
        Iterator<String> it = collection.iterator();  
        if(it.hasNext()){  
            user=it.next();  
        }  


        Presence presence = new Presence(Presence.Type.subscribe);   
        presence.setTo(user);   
        connection.sendPacket(presence);  

    }  

But it does not work. It seems that I need to do something to user B first. Any idea is welcome, thanks!

1

1 Answers

2
votes

Okay, I toiled hard at this for a couple of days and finally got things working. I have implemented it with a manual subscription mode (ie. user needs to accept another user's request manually). There is no need to create entries yourself as the server handles this automatically according to the presences sent/received.

For your case (automatic subscription), simply send a subscribe and subscribed presence immediately instead of saving the request locally.

This is how it works:

  • User1 sends subscribe presence to User2.
  • Roster entry gets automatically created in User1's roster (but not in User2's roster).
  • User2 receives subscribe request from User1.
  • User2 sends back a subscribed presence to User2 (User2 > User1 subscription complete).
  • User2 checks if User1 is in User2's roster. User1 is not in User2's roster. User2 sends back a subscribe presence to User1.
  • Roster entry gets automatically created in User2's roster.
  • User1 receives subscribe presence from User2.
  • User1 checks if User2 is in User1's roster. User2 is in User1's roster. User1 sends back a subscribed presence to User2 (User2 > User1 subscription complete).

            final Presence newPresence = (Presence) packet;
            final Presence.Type presenceType = newPresence.getType();
            final String fromId = newPresence.getFrom();
            final RosterEntry newEntry = getRoster().getEntry(fromId);
    
            if (presenceType == Presence.Type.subscribe)
            {
                //from new user
                if (newEntry == null)
                {
                    //save request locally for later accept/reject
                    //later accept will send back a subscribe & subscribed presence to user with fromId
                    //or accept immediately by sending back subscribe and unsubscribed right now
                }
                //from a user that previously accepted your request
                else
                {
                    //send back subscribed presence to user with fromId
                }
            }