refactored sendMessage and merged with resendMessage

This commit is contained in:
Daniel Gultsch 2015-07-05 11:59:38 +02:00
parent 148955a713
commit bef731a3c8
2 changed files with 118 additions and 149 deletions

View File

@ -435,6 +435,26 @@ public class Message extends AbstractEntity {
return (status > STATUS_RECEIVED || (contact != null && contact.trusted())); return (status > STATUS_RECEIVED || (contact != null && contact.trusted()));
} }
public boolean fixCounterpart() {
Presences presences = conversation.getContact().getPresences();
if (counterpart != null && presences.has(counterpart.getResourcepart())) {
return true;
} else if (presences.size() >= 1) {
try {
counterpart = Jid.fromParts(conversation.getJid().getLocalpart(),
conversation.getJid().getDomainpart(),
presences.asStringArray()[0]);
return true;
} catch (InvalidJidException e) {
counterpart = null;
return false;
}
} else {
counterpart = null;
return false;
}
}
public enum Decision { public enum Decision {
MUST, MUST,
SHOULD, SHOULD,

View File

@ -28,6 +28,7 @@ import android.util.LruCache;
import net.java.otr4j.OtrException; import net.java.otr4j.OtrException;
import net.java.otr4j.session.Session; import net.java.otr4j.session.Session;
import net.java.otr4j.session.SessionID; import net.java.otr4j.session.SessionID;
import net.java.otr4j.session.SessionImpl;
import net.java.otr4j.session.SessionStatus; import net.java.otr4j.session.SessionStatus;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
@ -684,51 +685,18 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void sendMessage(final Message message) { public void sendMessage(final Message message) {
sendMessage(message,false);
}
private void sendMessage(final Message message, final boolean resend) {
final Account account = message.getConversation().getAccount(); final Account account = message.getConversation().getAccount();
final Conversation conversation = message.getConversation();
account.deactivateGracePeriod(); account.deactivateGracePeriod();
final Conversation conv = message.getConversation();
MessagePacket packet = null; MessagePacket packet = null;
boolean saveInDb = true; boolean saveInDb = true;
boolean send = false;
if (account.getStatus() == Account.State.ONLINE
&& account.getXmppConnection() != null) {
if (message.needsUploading()) {
if (message.getCounterpart() != null || account.httpUploadAvailable()) {
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (!conv.hasValidOtrSession()) {
conv.startOtrSession(message.getCounterpart().getResourcepart(),true);
message.setStatus(Message.STATUS_WAITING); message.setStatus(Message.STATUS_WAITING);
} else if (conv.hasValidOtrSession()
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
mJingleConnectionManager.createNewConnection(message);
}
} else {
this.sendFileMessage(message);
} if (!resend && message.getEncryption() != Message.ENCRYPTION_OTR) {
} else {
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
conv.startOtrIfNeeded();
}
message.setStatus(Message.STATUS_WAITING);
}
} else {
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (!conv.hasValidOtrSession() && (message.getCounterpart() != null)) {
conv.startOtrSession(message.getCounterpart().getResourcepart(), true);
message.setStatus(Message.STATUS_WAITING);
} else if (conv.hasValidOtrSession()) {
if (conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
packet = mMessageGenerator.generateOtrChat(message);
send = true;
} else {
message.setStatus(Message.STATUS_WAITING);
conv.startOtrIfNeeded();
}
} else {
message.setStatus(Message.STATUS_WAITING);
}
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
message.getConversation().endOtrIfNeeded(); message.getConversation().endOtrIfNeeded();
message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() { message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
@Override @Override
@ -736,28 +704,66 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
markMessage(message,Message.STATUS_SEND_FAILED); markMessage(message,Message.STATUS_SEND_FAILED);
} }
}); });
packet = mMessageGenerator.generatePgpChat(message); }
send = true;
if (account.isOnlineAndConnected()) {
switch (message.getEncryption()) {
case Message.ENCRYPTION_NONE:
if (message.needsUploading()) {
if (account.httpUploadAvailable() || message.fixCounterpart()) {
this.sendFileMessage(message);
} else { } else {
message.getConversation().endOtrIfNeeded(); break;
message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
markMessage(message,Message.STATUS_SEND_FAILED);
} }
}); } else {
packet = mMessageGenerator.generateChat(message); packet = mMessageGenerator.generateChat(message,resend);
send = true; }
break;
case Message.ENCRYPTION_PGP:
case Message.ENCRYPTION_DECRYPTED:
if (message.needsUploading()) {
if (account.httpUploadAvailable() || message.fixCounterpart()) {
this.sendFileMessage(message);
} else {
break;
}
} else {
packet = mMessageGenerator.generatePgpChat(message,resend);
}
break;
case Message.ENCRYPTION_OTR:
SessionImpl otrSession = conversation.getOtrSession();
if (otrSession != null && otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
try {
message.setCounterpart(Jid.fromSessionID(otrSession.getSessionID()));
} catch (InvalidJidException e) {
break;
}
if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
packet = mMessageGenerator.generateOtrChat(message,resend);
}
} else if (otrSession == null) {
if (message.fixCounterpart()) {
conversation.startOtrSession(message.getCounterpart().getResourcepart(), true);
} else {
break;
} }
} }
if (!account.getXmppConnection().getFeatures().sm() break;
&& conv.getMode() != Conversation.MODE_MULTI) { }
if (packet != null) {
if (account.getXmppConnection().getFeatures().sm() || conversation.getMode() == Conversation.MODE_MULTI) {
message.setStatus(Message.STATUS_UNSEND);
} else {
message.setStatus(Message.STATUS_SEND); message.setStatus(Message.STATUS_SEND);
} }
}
} else { } else {
message.setStatus(Message.STATUS_WAITING); switch(message.getEncryption()) {
if (message.getType() == Message.TYPE_TEXT) { case Message.ENCRYPTION_DECRYPTED:
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { if (!message.needsUploading()) {
String pgpBody = message.getEncryptedBody(); String pgpBody = message.getEncryptedBody();
String decryptedBody = message.getBody(); String decryptedBody = message.getBody();
message.setBody(pgpBody); message.setBody(pgpBody);
@ -766,31 +772,39 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
saveInDb = false; saveInDb = false;
message.setBody(decryptedBody); message.setBody(decryptedBody);
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (!conv.hasValidOtrSession()
&& message.getCounterpart() != null) {
conv.startOtrSession(message.getCounterpart().getResourcepart(), false);
} }
break;
case Message.ENCRYPTION_OTR:
if (!conversation.hasValidOtrSession() && message.getCounterpart() != null) {
conversation.startOtrSession(message.getCounterpart().getResourcepart(), false);
}
break;
} }
} }
if (resend) {
if (packet != null) {
if (account.getXmppConnection().getFeatures().sm() || conversation.getMode() == Conversation.MODE_MULTI) {
markMessage(message,Message.STATUS_UNSEND);
} else {
markMessage(message,Message.STATUS_SEND);
} }
conv.add(message); }
if (saveInDb) { } else {
if (message.getEncryption() == Message.ENCRYPTION_NONE conversation.add(message);
|| saveEncryptedMessages()) { if (saveInDb && (message.getEncryption() == Message.ENCRYPTION_NONE || saveEncryptedMessages())) {
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
} }
updateConversationUi();
} }
if ((send) && (packet != null)) { if (packet != null) {
if (conv.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) { if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
if (this.sendChatStates()) { if (this.sendChatStates()) {
packet.addChild(ChatState.toElement(conv.getOutgoingChatState())); packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
} }
} }
sendMessagePacket(account, packet); sendMessagePacket(account, packet);
} }
updateConversationUi();
} }
private void sendUnsentMessages(final Conversation conversation) { private void sendUnsentMessages(final Conversation conversation) {
@ -804,72 +818,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void resendMessage(final Message message) { public void resendMessage(final Message message) {
Account account = message.getConversation().getAccount(); sendMessage(message,true);
MessagePacket packet = null;
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
Presences presences = message.getConversation().getContact().getPresences();
if (!message.getConversation().hasValidOtrSession()) {
if ((message.getCounterpart() != null)
&& (presences.has(message.getCounterpart().getResourcepart()))) {
message.getConversation().startOtrSession(message.getCounterpart().getResourcepart(), true);
} else {
if (presences.size() == 1) {
String presence = presences.asStringArray()[0];
message.getConversation().startOtrSession(presence, true);
}
}
} else {
if (message.getConversation().getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
try {
message.setCounterpart(Jid.fromSessionID(message.getConversation().getOtrSession().getSessionID()));
if (message.needsUploading()) {
mJingleConnectionManager.createNewConnection(message);
} else {
packet = mMessageGenerator.generateOtrChat(message, true);
}
} catch (final InvalidJidException ignored) {
}
}
}
} else if (message.needsUploading()) {
Contact contact = message.getConversation().getContact();
Presences presences = contact.getPresences();
if (account.httpUploadAvailable() || (message.getCounterpart() != null && presences.has(message.getCounterpart().getResourcepart()))) {
this.sendFileMessage(message);
} else {
if (presences.size() == 1) {
String presence = presences.asStringArray()[0];
try {
message.setCounterpart(Jid.fromParts(contact.getJid().getLocalpart(), contact.getJid().getDomainpart(), presence));
} catch (InvalidJidException e) {
return;
}
this.sendFileMessage(message);
}
}
} else {
if (message.getEncryption() == Message.ENCRYPTION_NONE) {
packet = mMessageGenerator.generateChat(message, true);
} else if ((message.getEncryption() == Message.ENCRYPTION_DECRYPTED)
|| (message.getEncryption() == Message.ENCRYPTION_PGP)) {
packet = mMessageGenerator.generatePgpChat(message, true);
}
}
if (packet != null) {
if (!account.getXmppConnection().getFeatures().sm()
&& message.getConversation().getMode() != Conversation.MODE_MULTI) {
markMessage(message, Message.STATUS_SEND);
} else {
markMessage(message, Message.STATUS_UNSEND);
}
if (message.getConversation().setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
if (this.sendChatStates()) {
packet.addChild(ChatState.toElement(message.getConversation().getOutgoingChatState()));
}
}
sendMessagePacket(account, packet);
}
} }
public void fetchRosterFromServer(final Account account) { public void fetchRosterFromServer(final Account account) {