Better implementation of Presence change listener
authorDa Risk <darisk972@gmail.com>
Mon, 24 May 2010 20:45:31 +0200
changeset 754 151841f2b1a1
parent 753 ea83164648a4
child 755 de343f195f37
Better implementation of Presence change listener
src/com/beem/project/beem/ui/ContactList.java
--- a/src/com/beem/project/beem/ui/ContactList.java	Mon May 24 20:44:56 2010 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Mon May 24 20:45:31 2010 +0200
@@ -551,25 +551,23 @@
 	public void onPresenceChanged(PresenceAdapter presence) throws RemoteException {
 	    String from = presence.getFrom();
 	    boolean resfound = false;
-	    for (Contact curContact : mListContact) {
-		if (curContact.getJID().equals(StringUtils.parseBareAddress(from))) {
-		    String pres = StringUtils.parseResource(from);
-		    for (String res : curContact.getMRes()) {
-			if (res.equals(pres)) {
-			    resfound = true;
-			    break;
-			}
-		    }
-		    curContact.setStatus(mRoster.getPresence(StringUtils.parseBareAddress(presence.getFrom())));
-		    int status = presence.getStatus();
-		    if (!resfound && !Status.statusOnline(status))
-			curContact.addRes(pres);
-		    else if (resfound && Status.statusOnline(status))
-			curContact.delRes(pres);
-		    mHandler.post(new RunnableChange());
-		    return;
+	    int foundIdx = 0;
+	    boolean found = false;
+	    Contact contact = mRoster.getContact(StringUtils.parseBareAddress(from));
+	    for (int i = 0; i < mListContact.size(); i++) {
+		Contact curContact = mListContact.get(i);
+		if (curContact.getJID().equals(contact.getJID())) {
+		    found = true;
+		    foundIdx = i;
+		    break;
 		}
 	    }
+	    if (found){
+		mListContact.set(foundIdx, contact);
+	    } else {
+		mListContact.add(contact);
+	    }
+	    mHandler.post(new RunnableChange());
 	}
     }