From a0dde05ee59058bfd71968eb4072aaaa9be5c090 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 23 May 2014 10:54:40 +0200 Subject: [PATCH] end otr sessions earlier / more often. can lead to unesessary handshakes but can improve reliabilty --- .../conversations/entities/Conversation.java | 18 +++----------- .../services/XmppConnectionService.java | 24 +++++++++++++++++-- .../xmpp/OnContactStatusChanged.java | 7 ++++++ 3 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 src/eu/siacs/conversations/xmpp/OnContactStatusChanged.java diff --git a/src/eu/siacs/conversations/entities/Conversation.java b/src/eu/siacs/conversations/entities/Conversation.java index e61537da..37a230df 100644 --- a/src/eu/siacs/conversations/entities/Conversation.java +++ b/src/eu/siacs/conversations/entities/Conversation.java @@ -10,11 +10,11 @@ import net.java.otr4j.crypto.OtrCryptoException; import net.java.otr4j.session.SessionID; import net.java.otr4j.session.SessionImpl; import net.java.otr4j.session.SessionStatus; - import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; +import android.util.Log; public class Conversation extends AbstractEntity { @@ -240,6 +240,7 @@ public class Conversation extends AbstractEntity { public void endOtrIfNeeded() { if (this.otrSession != null) { if (this.otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) { + Log.d("xmppService","ending otr session with "+getContactJid()); try { this.otrSession.endSession(); this.resetOtrSession(); @@ -251,20 +252,7 @@ public class Conversation extends AbstractEntity { } public boolean hasValidOtrSession() { - if (this.otrSession == null) { - return false; - } else { - String foreignPresence = this.otrSession.getSessionID().getUserID(); - if (getContact()==null) { - return true; - } else { - if (!getContact().getPresences().containsKey(foreignPresence)) { - this.resetOtrSession(); - return false; - } - return true; - } - } + return this.otrSession != null; } public String getOtrFingerprint() { diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 764e139f..e31d28e0 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -36,6 +36,7 @@ import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.OnBindListener; +import eu.siacs.conversations.xmpp.OnContactStatusChanged; import eu.siacs.conversations.xmpp.OnIqPacketReceived; import eu.siacs.conversations.xmpp.OnMessagePacketReceived; import eu.siacs.conversations.xmpp.OnPresencePacketReceived; @@ -95,6 +96,16 @@ public class XmppConnectionService extends Service { private int convChangedListenerCount = 0; private OnAccountListChangedListener accountChangedListener = null; private OnTLSExceptionReceived tlsException = null; + private OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() { + + @Override + public void onContactStatusChanged(Contact contact) { + Conversation conversation = findActiveConversation(contact); + if (conversation!=null) { + conversation.endOtrIfNeeded(); + } + } + }; public void setOnTLSExceptionReceivedListener( OnTLSExceptionReceived listener) { @@ -325,8 +336,7 @@ public class XmppConnectionService extends Service { msg, x.getContent())); } } - } else { - // Log.d(LOGTAG,"presence without resource "+packet.toString()); + onContactStatusChanged.onContactStatusChanged(contact); } } else if (type.equals("unavailable")) { if (fromParts.length != 2) { @@ -334,6 +344,7 @@ public class XmppConnectionService extends Service { } else { contact.removePresence(fromParts[1]); } + onContactStatusChanged.onContactStatusChanged(contact); } else if (type.equals("subscribe")) { Log.d(LOGTAG, "received subscribe packet from " + packet.getFrom()); @@ -947,6 +958,15 @@ public class XmppConnectionService extends Service { public List getAccounts() { return this.accounts; } + + public Conversation findActiveConversation(Contact contact) { + for (Conversation conversation : this.getConversations()) { + if (conversation.getContact() == contact) { + return conversation; + } + } + return null; + } public Conversation findOrCreateConversation(Account account, String jid, boolean muc) { diff --git a/src/eu/siacs/conversations/xmpp/OnContactStatusChanged.java b/src/eu/siacs/conversations/xmpp/OnContactStatusChanged.java new file mode 100644 index 00000000..8597a753 --- /dev/null +++ b/src/eu/siacs/conversations/xmpp/OnContactStatusChanged.java @@ -0,0 +1,7 @@ +package eu.siacs.conversations.xmpp; + +import eu.siacs.conversations.entities.Contact; + +public interface OnContactStatusChanged { + public void onContactStatusChanged(Contact contact); +}