code cleanup / unification

This commit is contained in:
iNPUTmice 2014-06-16 14:06:34 +02:00
parent b9477edb53
commit 41834b5e24
2 changed files with 22 additions and 40 deletions

View File

@ -645,25 +645,24 @@ public class XmppConnectionService extends Service {
Account account = message.getConversation().getAccount(); Account account = message.getConversation().getAccount();
Conversation conv = message.getConversation(); Conversation conv = message.getConversation();
MessagePacket packet = null; MessagePacket packet = null;
boolean saveInDb = false; boolean saveInDb = true;
boolean addToConversation = false;
boolean send = false; boolean send = false;
if (account.getStatus() == Account.STATUS_ONLINE) { if (account.getStatus() == Account.STATUS_ONLINE) {
if (message.getType() == Message.TYPE_IMAGE) { if (message.getType() == Message.TYPE_IMAGE) {
if (message.getPresence() !=null ) { if (message.getPresence() != null) {
mJingleConnectionManager.createNewConnection(message); mJingleConnectionManager.createNewConnection(message);
} else { } else {
message.setStatus(Message.STATUS_WAITING); message.setStatus(Message.STATUS_WAITING);
saveInDb = true;
addToConversation = true;
} }
} else { } else {
if (message.getEncryption() == Message.ENCRYPTION_OTR) { if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (!conv.hasValidOtrSession()&&(message.getPresence() != null)) { if (!conv.hasValidOtrSession()
&& (message.getPresence() != null)) {
// starting otr session. messages will be send later // starting otr session. messages will be send later
conv.startOtrSession(getApplicationContext(), conv.startOtrSession(getApplicationContext(),
message.getPresence(), true); message.getPresence(), true);
} else if (conv.hasValidOtrSession() && conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) { } else if (conv.hasValidOtrSession()
&& conv.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) {
// otr session aleary exists, creating message packet // otr session aleary exists, creating message packet
// accordingly // accordingly
packet = prepareMessagePacket(account, message, packet = prepareMessagePacket(account, message,
@ -673,8 +672,6 @@ public class XmppConnectionService extends Service {
} else if (message.getPresence() == null) { } else if (message.getPresence() == null) {
message.setStatus(Message.STATUS_WAITING); message.setStatus(Message.STATUS_WAITING);
} }
saveInDb = true;
addToConversation = true;
} else if (message.getEncryption() == Message.ENCRYPTION_PGP) { } else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
message.getConversation().endOtrIfNeeded(); message.getConversation().endOtrIfNeeded();
packet = prepareMessagePacket(account, message, null); packet = prepareMessagePacket(account, message, null);
@ -683,8 +680,6 @@ public class XmppConnectionService extends Service {
message.getEncryptedBody()); message.getEncryptedBody());
message.setStatus(Message.STATUS_SEND); message.setStatus(Message.STATUS_SEND);
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
saveInDb = true;
addToConversation = true;
send = true; send = true;
} else { } else {
message.getConversation().endOtrIfNeeded(); message.getConversation().endOtrIfNeeded();
@ -694,37 +689,28 @@ public class XmppConnectionService extends Service {
} }
packet = prepareMessagePacket(account, message, null); packet = prepareMessagePacket(account, message, null);
send = true; send = true;
saveInDb = true;
addToConversation = true;
} }
} }
} else { } else {
message.setStatus(Message.STATUS_WAITING); message.setStatus(Message.STATUS_WAITING);
if (message.getType() == Message.TYPE_IMAGE) { if (message.getType() == Message.TYPE_TEXT) {
saveInDb = true;
addToConversation = true;
} else {
if (message.getEncryption() == Message.ENCRYPTION_PGP) { if (message.getEncryption() == Message.ENCRYPTION_PGP) {
String pgpBody = message.getEncryptedBody(); String pgpBody = message.getEncryptedBody();
String decryptedBody = message.getBody(); String decryptedBody = message.getBody();
message.setBody(pgpBody); message.setBody(pgpBody);
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
saveInDb = false;
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
message.setBody(decryptedBody); message.setBody(decryptedBody);
addToConversation = true;
} else if (message.getEncryption() == Message.ENCRYPTION_OTR) { } else if (message.getEncryption() == Message.ENCRYPTION_OTR) {
if (conv.hasValidOtrSession()) { if (conv.hasValidOtrSession()) {
message.setPresence(conv.getOtrSession().getSessionID() message.setPresence(conv.getOtrSession().getSessionID()
.getUserID()); .getUserID());
} else if (!conv.hasValidOtrSession() && message.getPresence() != null) { } else if (!conv.hasValidOtrSession()
&& message.getPresence() != null) {
conv.startOtrSession(getApplicationContext(), conv.startOtrSession(getApplicationContext(),
message.getPresence(), false); message.getPresence(), false);
} }
saveInDb = true;
addToConversation = true;
} else {
saveInDb = true;
addToConversation = true;
} }
} }
@ -732,12 +718,10 @@ public class XmppConnectionService extends Service {
if (saveInDb) { if (saveInDb) {
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
} }
if (addToConversation) {
conv.getMessages().add(message); conv.getMessages().add(message);
if (convChangedListener != null) { if (convChangedListener != null) {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
} }
}
if ((send) && (packet != null)) { if ((send) && (packet != null)) {
account.getXmppConnection().sendMessagePacket(packet); account.getXmppConnection().sendMessagePacket(packet);
} }
@ -796,7 +780,8 @@ public class XmppConnectionService extends Service {
markMessage(message, Message.STATUS_SEND); markMessage(message, Message.STATUS_SEND);
} }
} else if (message.getType() == Message.TYPE_IMAGE) { } else if (message.getType() == Message.TYPE_IMAGE) {
Presences presences = message.getConversation().getContact().getPresences(); Presences presences = message.getConversation().getContact()
.getPresences();
if ((message.getPresence() != null) if ((message.getPresence() != null)
&& (presences.has(message.getPresence()))) { && (presences.has(message.getPresence()))) {
markMessage(message, Message.STATUS_OFFERED); markMessage(message, Message.STATUS_OFFERED);
@ -931,9 +916,11 @@ public class XmppConnectionService extends Service {
return this.conversations; return this.conversations;
} }
public List<Message> getMoreMessages(Conversation conversation,long timestamp) { public List<Message> getMoreMessages(Conversation conversation,
List<Message> messages = databaseBackend.getMessages(conversation, 50, timestamp); long timestamp) {
for(Message message : messages) { List<Message> messages = databaseBackend.getMessages(conversation, 50,
timestamp);
for (Message message : messages) {
message.setConversation(conversation); message.setConversation(conversation);
} }
return messages; return messages;

View File

@ -822,12 +822,7 @@ public class ConversationActivity extends XmppActivity {
@Override @Override
public void success(Message message) { public void success(Message message) {
message.getConversation().getMessages().add(message);
xmppConnectionService.databaseBackend
.createMessage(message);
xmppConnectionService.sendMessage(message); xmppConnectionService.sendMessage(message);
xmppConnectionService.updateUi(
message.getConversation(), false);
} }
@Override @Override