From a04d736f75c793dc943c1e0ce7c89e1c20a3a684 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 16 Apr 2014 19:14:20 +0200 Subject: [PATCH] fixed possible race conditions with presences --- src/eu/siacs/conversations/entities/Contact.java | 6 ++++++ src/eu/siacs/conversations/entities/Presences.java | 8 ++++++-- .../services/XmppConnectionService.java | 13 ++++++------- .../conversations/ui/ContactDetailsActivity.java | 6 +++++- src/eu/siacs/conversations/xmpp/XmppConnection.java | 1 - 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/eu/siacs/conversations/entities/Contact.java b/src/eu/siacs/conversations/entities/Contact.java index 4b97cc41..132b2717 100644 --- a/src/eu/siacs/conversations/entities/Contact.java +++ b/src/eu/siacs/conversations/entities/Contact.java @@ -13,6 +13,7 @@ import eu.siacs.conversations.xml.Element; import android.content.ContentValues; import android.database.Cursor; +import android.util.Log; public class Contact extends AbstractEntity implements Serializable { private static final long serialVersionUID = -4570817093119419962L; @@ -163,11 +164,16 @@ public class Contact extends AbstractEntity implements Serializable { public void updatePresence(String resource, int status) { this.presences.updatePresence(resource, status); + Log.d("xmppService","updatingPresence for contact="+this.jid+" resource="+resource+" num="+presences.size()); } public void removePresence(String resource) { this.presences.removePresence(resource); } + + public void clearPresences() { + this.presences.clearPresences(); + } public int getMostAvailableStatus() { return this.presences.getMostAvailableStatus(); diff --git a/src/eu/siacs/conversations/entities/Presences.java b/src/eu/siacs/conversations/entities/Presences.java index 9a22e559..acbaafca 100644 --- a/src/eu/siacs/conversations/entities/Presences.java +++ b/src/eu/siacs/conversations/entities/Presences.java @@ -33,6 +33,10 @@ public class Presences { this.presences.remove(resource); } + public void clearPresences() { + this.presences.clear(); + } + public int getMostAvailableStatus() { int status = OFFLINE; Iterator> it = presences.entrySet().iterator(); @@ -54,7 +58,7 @@ public class Presences { jObj.put("resource", entry.getKey()); jObj.put("status", entry.getValue()); } catch (JSONException e) { - + } json.put(jObj); } @@ -71,7 +75,7 @@ public class Presences { jObj.getInt("status")); } } catch (JSONException e1) { - + } return presences; } diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index a1054c40..e9f93a63 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -1,6 +1,5 @@ package eu.siacs.conversations.services; -import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collections; @@ -288,8 +287,7 @@ public class XmppConnectionService extends Service { } else { Contact contact = findContact(account, fromParts[0]); if (contact == null) { - //Log.d(LOGTAG,"contact was null"); - // most likely roster not synced + Log.d(LOGTAG,packet.getFrom()+ " could not be found"); return; } if (type == null) { @@ -314,17 +312,19 @@ public class XmppConnectionService extends Service { } } } + replaceContactInConversation(contact.getJid(), contact); databaseBackend.updateContact(contact); } else { - // Log.d(LOGTAG,"presence without resource "+packet.toString()); + //Log.d(LOGTAG,"presence without resource "+packet.toString()); } } else if (type.equals("unavailable")) { if (fromParts.length != 2) { - // Log.d(LOGTAG,"received presence with no resource "+packet.toString()); + contact.clearPresences(); } else { contact.removePresence(fromParts[1]); - databaseBackend.updateContact(contact); } + replaceContactInConversation(contact.getJid(), contact); + databaseBackend.updateContact(contact); } else if (type.equals("subscribe")) { Log.d(LOGTAG,"received subscribe packet from "+packet.getFrom()); if (contact @@ -348,7 +348,6 @@ public class XmppConnectionService extends Service { } else { //Log.d(LOGTAG, packet.toString()); } - replaceContactInConversation(contact.getJid(), contact); } } } diff --git a/src/eu/siacs/conversations/ui/ContactDetailsActivity.java b/src/eu/siacs/conversations/ui/ContactDetailsActivity.java index 86535ba1..1ed3fa13 100644 --- a/src/eu/siacs/conversations/ui/ContactDetailsActivity.java +++ b/src/eu/siacs/conversations/ui/ContactDetailsActivity.java @@ -227,7 +227,11 @@ public class ContactDetailsActivity extends XmppActivity { status.setTextColor(0xFFe92727); break; } - contactJid.setText(contact.getJid()); + if (contact.getPresences().size() > 1) { + contactJid.setText(contact.getJid()+" ("+contact.getPresences().size()+")"); + } else { + contactJid.setText(contact.getJid()); + } accountJid.setText(contact.getAccount().getJid()); UIHelper.prepareContactBadge(this, badge, contact, getApplicationContext()); diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index 0019094c..41791dc2 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -674,7 +674,6 @@ public class XmppConnection implements Runnable { "var")); } } - Log.d(LOGTAG,"put "+server+" "+features.toString()); disco.put(server, features); if (account.getServer().equals(server)) {