end otr sessions earlier / more often. can lead to unesessary handshakes but can improve reliabilty

This commit is contained in:
Daniel Gultsch 2014-05-23 10:54:40 +02:00
parent 8133dcd881
commit a0dde05ee5
3 changed files with 32 additions and 17 deletions

View File

@ -10,11 +10,11 @@ import net.java.otr4j.crypto.OtrCryptoException;
import net.java.otr4j.session.SessionID; import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionImpl; import net.java.otr4j.session.SessionImpl;
import net.java.otr4j.session.SessionStatus; import net.java.otr4j.session.SessionStatus;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
public class Conversation extends AbstractEntity { public class Conversation extends AbstractEntity {
@ -240,6 +240,7 @@ public class Conversation extends AbstractEntity {
public void endOtrIfNeeded() { public void endOtrIfNeeded() {
if (this.otrSession != null) { if (this.otrSession != null) {
if (this.otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) { if (this.otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
Log.d("xmppService","ending otr session with "+getContactJid());
try { try {
this.otrSession.endSession(); this.otrSession.endSession();
this.resetOtrSession(); this.resetOtrSession();
@ -251,20 +252,7 @@ public class Conversation extends AbstractEntity {
} }
public boolean hasValidOtrSession() { public boolean hasValidOtrSession() {
if (this.otrSession == null) { return 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;
}
}
} }
public String getOtrFingerprint() { public String getOtrFingerprint() {

View File

@ -36,6 +36,7 @@ import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnBindListener; import eu.siacs.conversations.xmpp.OnBindListener;
import eu.siacs.conversations.xmpp.OnContactStatusChanged;
import eu.siacs.conversations.xmpp.OnIqPacketReceived; import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.OnMessagePacketReceived; import eu.siacs.conversations.xmpp.OnMessagePacketReceived;
import eu.siacs.conversations.xmpp.OnPresencePacketReceived; import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
@ -95,6 +96,16 @@ public class XmppConnectionService extends Service {
private int convChangedListenerCount = 0; private int convChangedListenerCount = 0;
private OnAccountListChangedListener accountChangedListener = null; private OnAccountListChangedListener accountChangedListener = null;
private OnTLSExceptionReceived tlsException = 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( public void setOnTLSExceptionReceivedListener(
OnTLSExceptionReceived listener) { OnTLSExceptionReceived listener) {
@ -325,8 +336,7 @@ public class XmppConnectionService extends Service {
msg, x.getContent())); msg, x.getContent()));
} }
} }
} else { onContactStatusChanged.onContactStatusChanged(contact);
// Log.d(LOGTAG,"presence without resource "+packet.toString());
} }
} else if (type.equals("unavailable")) { } else if (type.equals("unavailable")) {
if (fromParts.length != 2) { if (fromParts.length != 2) {
@ -334,6 +344,7 @@ public class XmppConnectionService extends Service {
} else { } else {
contact.removePresence(fromParts[1]); contact.removePresence(fromParts[1]);
} }
onContactStatusChanged.onContactStatusChanged(contact);
} else if (type.equals("subscribe")) { } else if (type.equals("subscribe")) {
Log.d(LOGTAG, "received subscribe packet from " Log.d(LOGTAG, "received subscribe packet from "
+ packet.getFrom()); + packet.getFrom());
@ -948,6 +959,15 @@ public class XmppConnectionService extends Service {
return this.accounts; 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, public Conversation findOrCreateConversation(Account account, String jid,
boolean muc) { boolean muc) {
for (Conversation conv : this.getConversations()) { for (Conversation conv : this.getConversations()) {

View File

@ -0,0 +1,7 @@
package eu.siacs.conversations.xmpp;
import eu.siacs.conversations.entities.Contact;
public interface OnContactStatusChanged {
public void onContactStatusChanged(Contact contact);
}