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,113 +685,126 @@ 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; message.setStatus(Message.STATUS_WAITING);
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);
} else if (conv.hasValidOtrSession()
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
mJingleConnectionManager.createNewConnection(message);
}
} else {
this.sendFileMessage(message);
} if (!resend && message.getEncryption() != Message.ENCRYPTION_OTR) {
} else { message.getConversation().endOtrIfNeeded();
if (message.getEncryption() == Message.ENCRYPTION_OTR) { message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
conv.startOtrIfNeeded(); @Override
} public void onMessageFound(Message message) {
message.setStatus(Message.STATUS_WAITING); markMessage(message,Message.STATUS_SEND_FAILED);
} }
} else { });
if (message.getEncryption() == Message.ENCRYPTION_OTR) { }
if (!conv.hasValidOtrSession() && (message.getCounterpart() != null)) {
conv.startOtrSession(message.getCounterpart().getResourcepart(), true); if (account.isOnlineAndConnected()) {
message.setStatus(Message.STATUS_WAITING); switch (message.getEncryption()) {
} else if (conv.hasValidOtrSession()) { case Message.ENCRYPTION_NONE:
if (conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) { if (message.needsUploading()) {
packet = mMessageGenerator.generateOtrChat(message); if (account.httpUploadAvailable() || message.fixCounterpart()) {
send = true; this.sendFileMessage(message);
} else { } else {
message.setStatus(Message.STATUS_WAITING); break;
conv.startOtrIfNeeded();
} }
} else { } else {
message.setStatus(Message.STATUS_WAITING); packet = mMessageGenerator.generateChat(message,resend);
} }
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { break;
message.getConversation().endOtrIfNeeded(); case Message.ENCRYPTION_PGP:
message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() { case Message.ENCRYPTION_DECRYPTED:
@Override if (message.needsUploading()) {
public void onMessageFound(Message message) { if (account.httpUploadAvailable() || message.fixCounterpart()) {
markMessage(message,Message.STATUS_SEND_FAILED); this.sendFileMessage(message);
} else {
break;
} }
}); } else {
packet = mMessageGenerator.generatePgpChat(message); packet = mMessageGenerator.generatePgpChat(message,resend);
send = true; }
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;
}
}
break;
}
if (packet != null) {
if (account.getXmppConnection().getFeatures().sm() || conversation.getMode() == Conversation.MODE_MULTI) {
message.setStatus(Message.STATUS_UNSEND);
} else { } else {
message.getConversation().endOtrIfNeeded(); message.setStatus(Message.STATUS_SEND);
message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
markMessage(message,Message.STATUS_SEND_FAILED);
}
});
packet = mMessageGenerator.generateChat(message);
send = true;
} }
} }
if (!account.getXmppConnection().getFeatures().sm()
&& conv.getMode() != Conversation.MODE_MULTI) {
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);
message.setEncryption(Message.ENCRYPTION_PGP); message.setEncryption(Message.ENCRYPTION_PGP);
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
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() break;
&& message.getCounterpart() != null) { case Message.ENCRYPTION_OTR:
conv.startOtrSession(message.getCounterpart().getResourcepart(), false); 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);
} }
} }
} else {
} conversation.add(message);
conv.add(message); if (saveInDb && (message.getEncryption() == Message.ENCRYPTION_NONE || saveEncryptedMessages())) {
if (saveInDb) {
if (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) {