refactored the way certain messages are being found within a conversation

This commit is contained in:
iNPUTmice 2014-12-14 18:10:46 +01:00
parent fc293aaede
commit de06cb38d1
5 changed files with 193 additions and 117 deletions

View File

@ -77,6 +77,104 @@ public class Conversation extends AbstractEntity {
private Bookmark bookmark; private Bookmark bookmark;
public Message findUnsentMessageWithUuid(String uuid) {
synchronized(this.messages) {
for (final Message message : this.messages) {
final int s = message.getStatus();
if ((s == Message.STATUS_UNSEND || s == Message.STATUS_WAITING) && message.getUuid().equals(uuid)) {
return message;
}
}
}
return null;
}
public void findWaitingMessages(OnMessageFound onMessageFound) {
synchronized (this.messages) {
for(Message message : this.messages) {
if (message.getStatus() == Message.STATUS_WAITING) {
onMessageFound.onMessageFound(message);
}
}
}
}
public void findMessagesWithFiles(OnMessageFound onMessageFound) {
synchronized (this.messages) {
for (Message message : this.messages) {
if ((message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE)
&& message.getEncryption() != Message.ENCRYPTION_PGP) {
onMessageFound.onMessageFound(message);
}
}
}
}
public Message findMessageWithFileAndUuid(String uuid) {
synchronized (this.messages) {
for (Message message : this.messages) {
if (message.getType() == Message.TYPE_IMAGE
&& message.getEncryption() != Message.ENCRYPTION_PGP
&& message.getUuid().equals(uuid)) {
return message;
}
}
}
return null;
}
public void clearMessages() {
synchronized (this.messages) {
this.messages.clear();
}
}
public void findUnsentMessagesWithOtrEncryption(OnMessageFound onMessageFound) {
synchronized (this.messages) {
for (Message message : this.messages) {
if ((message.getStatus() == Message.STATUS_UNSEND || message.getStatus() == Message.STATUS_WAITING)
&& (message.getEncryption() == Message.ENCRYPTION_OTR)) {
onMessageFound.onMessageFound(message);
}
}
}
}
public void findUnsentTextMessages(OnMessageFound onMessageFound) {
synchronized (this.messages) {
for (Message message : this.messages) {
if (message.getType() != Message.TYPE_IMAGE
&& message.getStatus() == Message.STATUS_UNSEND) {
onMessageFound.onMessageFound(message);
}
}
}
}
public Message findSentMessageWithUuid(String uuid) {
synchronized (this.messages) {
for (Message message : this.messages) {
if (uuid.equals(message.getUuid())
|| (message.getStatus() >= Message.STATUS_SEND && uuid
.equals(message.getRemoteMsgId()))) {
return message;
}
}
}
return null;
}
public void populateWithMessages(List<Message> messages) {
synchronized (this.messages) {
messages.clear();
messages.addAll(this.messages);
}
}
public interface OnMessageFound {
public void onMessageFound(final Message message);
}
public Conversation(final String name, final Account account, final Jid contactJid, public Conversation(final String name, final Account account, final Jid contactJid,
final int mode) { final int mode) {
this(java.util.UUID.randomUUID().toString(), name, null, account this(java.util.UUID.randomUUID().toString(), name, null, account
@ -103,10 +201,6 @@ public class Conversation extends AbstractEntity {
} }
} }
public List<Message> getMessages() {
return messages;
}
public boolean isRead() { public boolean isRead() {
return (this.messages == null) || (this.messages.size() == 0) || this.messages.get(this.messages.size() - 1).isRead(); return (this.messages == null) || (this.messages.size() == 0) || this.messages.get(this.messages.size() - 1).isRead();
} }
@ -455,17 +549,19 @@ public class Conversation extends AbstractEntity {
} }
public boolean hasDuplicateMessage(Message message) { public boolean hasDuplicateMessage(Message message) {
for (int i = this.getMessages().size() - 1; i >= 0; --i) { synchronized (this.messages) {
for (int i = this.messages.size() - 1; i >= 0; --i) {
if (this.messages.get(i).equals(message)) { if (this.messages.get(i).equals(message)) {
return true; return true;
} }
} }
}
return false; return false;
} }
public Message findSentMessageWithBody(String body) { public Message findSentMessageWithBody(String body) {
synchronized (this.messages) { synchronized (this.messages) {
for (int i = this.getMessages().size() - 1; i >= 0; --i) { for (int i = this.messages.size() - 1; i >= 0; --i) {
Message message = this.messages.get(i); Message message = this.messages.get(i);
if ((message.getStatus() == Message.STATUS_UNSEND || message.getStatus() == Message.STATUS_SEND) && message.getBody() != null && message.getBody().equals(body)) { if ((message.getStatus() == Message.STATUS_UNSEND || message.getStatus() == Message.STATUS_SEND) && message.getBody() != null && message.getBody().equals(body)) {
return message; return message;
@ -567,7 +663,7 @@ public class Conversation extends AbstractEntity {
public void sort() { public void sort() {
synchronized (this.messages) { synchronized (this.messages) {
Collections.sort(this.messages,new Comparator<Message>() { Collections.sort(this.messages, new Comparator<Message>() {
@Override @Override
public int compare(Message left, Message right) { public int compare(Message left, Message right) {
if (left.getTimeSent() < right.getTimeSent()) { if (left.getTimeSent() < right.getTimeSent()) {

View File

@ -323,12 +323,10 @@ public class Message extends AbstractEntity {
synchronized (this.conversation.messages) { synchronized (this.conversation.messages) {
if (this.mNextMessage == null) { if (this.mNextMessage == null) {
int index = this.conversation.messages.indexOf(this); int index = this.conversation.messages.indexOf(this);
if (index < 0 if (index < 0 || index >= this.conversation.messages.size() - 1) {
|| index >= this.conversation.getMessages().size() - 1) {
this.mNextMessage = null; this.mNextMessage = null;
} else { } else {
this.mNextMessage = this.conversation.messages this.mNextMessage = this.conversation.messages.get(index + 1);
.get(index + 1);
} }
} }
return this.mNextMessage; return this.mNextMessage;
@ -338,13 +336,11 @@ public class Message extends AbstractEntity {
public Message prev() { public Message prev() {
synchronized (this.conversation.messages) { synchronized (this.conversation.messages) {
if (this.mPreviousMessage == null) { if (this.mPreviousMessage == null) {
int index = this.conversation.messages.indexOf(this); int index = this.conversation.messages.indexOf(this);
if (index <= 0 || index > this.conversation.messages.size()) { if (index <= 0 || index > this.conversation.messages.size()) {
this.mPreviousMessage = null; this.mPreviousMessage = null;
} else { } else {
this.mPreviousMessage = this.conversation.messages this.mPreviousMessage = this.conversation.messages.get(index - 1);
.get(index - 1);
} }
} }
return this.mPreviousMessage; return this.mPreviousMessage;

View File

@ -120,7 +120,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
conversation.resetOtrSession(); conversation.resetOtrSession();
} }
if (online && (contact.getPresences().size() == 1)) { if (online && (contact.getPresences().size() == 1)) {
sendUnsendMessages(conversation); sendUnsentMessages(conversation);
} }
} }
} }
@ -165,7 +165,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
for (Conversation conversation : conversations) { for (Conversation conversation : conversations) {
if (conversation.getAccount() == account) { if (conversation.getAccount() == account) {
conversation.startOtrIfNeeded(); conversation.startOtrIfNeeded();
sendUnsendMessages(conversation); sendUnsentMessages(conversation);
} }
} }
if (connection != null && connection.getFeatures().csi()) { if (connection != null && connection.getFeatures().csi()) {
@ -258,15 +258,12 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void onMessageAcknowledged(Account account, String uuid) { public void onMessageAcknowledged(Account account, String uuid) {
for (final Conversation conversation : getConversations()) { for (final Conversation conversation : getConversations()) {
if (conversation.getAccount() == account) { if (conversation.getAccount() == account) {
for (final Message message : conversation.getMessages()) { Message message = conversation.findUnsentMessageWithUuid(uuid);
final int s = message.getStatus(); if (message != null) {
if ((s == Message.STATUS_UNSEND || s == Message.STATUS_WAITING) && message.getUuid().equals(uuid)) {
markMessage(message, Message.STATUS_SEND); markMessage(message, Message.STATUS_SEND);
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) { if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
databaseBackend.updateConversation(conversation); databaseBackend.updateConversation(conversation);
} }
return;
}
} }
} }
} }
@ -640,12 +637,22 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { } else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
message.getConversation().endOtrIfNeeded(); message.getConversation().endOtrIfNeeded();
failWaitingOtrMessages(message.getConversation()); message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
markMessage(message,Message.STATUS_SEND_FAILED);
}
});
packet = mMessageGenerator.generatePgpChat(message); packet = mMessageGenerator.generatePgpChat(message);
send = true; send = true;
} else { } else {
message.getConversation().endOtrIfNeeded(); message.getConversation().endOtrIfNeeded();
failWaitingOtrMessages(message.getConversation()); message.getConversation().findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
@Override
public void onMessageFound(Message message) {
markMessage(message,Message.STATUS_SEND_FAILED);
}
});
packet = mMessageGenerator.generateChat(message); packet = mMessageGenerator.generateChat(message);
send = true; send = true;
} }
@ -688,13 +695,14 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
updateConversationUi(); updateConversationUi();
} }
private void sendUnsendMessages(Conversation conversation) { private void sendUnsentMessages(Conversation conversation) {
for (int i = 0; i < conversation.getMessages().size(); ++i) { conversation.findWaitingMessages(new Conversation.OnMessageFound() {
int status = conversation.getMessages().get(i).getStatus();
if (status == Message.STATUS_WAITING) { @Override
resendMessage(conversation.getMessages().get(i)); public void onMessageFound(Message message) {
} resendMessage(message);
} }
});
} }
private void resendMessage(Message message) { private void resendMessage(Message message) {
@ -898,22 +906,21 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
private void checkDeletedFiles(Conversation conversation) { private void checkDeletedFiles(Conversation conversation) {
for (Message message : conversation.getMessages()) { conversation.findMessagesWithFiles(new Conversation.OnMessageFound() {
if ((message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE)
&& message.getEncryption() != Message.ENCRYPTION_PGP) { @Override
public void onMessageFound(Message message) {
if (!getFileBackend().isFileAvailable(message)) { if (!getFileBackend().isFileAvailable(message)) {
message.setDownloadable(new DownloadablePlaceholder(Downloadable.STATUS_DELETED)); message.setDownloadable(new DownloadablePlaceholder(Downloadable.STATUS_DELETED));
} }
} }
} });
} }
private void markFileDeleted(String uuid) { private void markFileDeleted(String uuid) {
for (Conversation conversation : getConversations()) { for (Conversation conversation : getConversations()) {
for (Message message : conversation.getMessages()) { Message message = conversation.findMessageWithFileAndUuid(uuid);
if (message.getType() == Message.TYPE_IMAGE if (message != null) {
&& message.getEncryption() != Message.ENCRYPTION_PGP
&& message.getUuid().equals(uuid)) {
if (!getFileBackend().isFileAvailable(message)) { if (!getFileBackend().isFileAvailable(message)) {
message.setDownloadable(new DownloadablePlaceholder(Downloadable.STATUS_DELETED)); message.setDownloadable(new DownloadablePlaceholder(Downloadable.STATUS_DELETED));
updateConversationUi(); updateConversationUi();
@ -922,7 +929,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} }
} }
}
public void populateWithOrderedConversations(final List<Conversation> list) { public void populateWithOrderedConversations(final List<Conversation> list) {
populateWithOrderedConversations(list, true); populateWithOrderedConversations(list, true);
@ -1067,12 +1073,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} }
public void clearConversationHistory(Conversation conversation) {
this.databaseBackend.deleteMessagesInConversation(conversation);
conversation.getMessages().clear();
updateConversationUi();
}
public void createAccount(Account account) { public void createAccount(Account account) {
account.initOtrEngine(this); account.initOtrEngine(this);
databaseBackend.createAccount(account); databaseBackend.createAccount(account);
@ -1533,37 +1533,36 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void onOtrSessionEstablished(Conversation conversation) { public void onOtrSessionEstablished(Conversation conversation) {
Account account = conversation.getAccount(); final Account account = conversation.getAccount();
List<Message> messages = conversation.getMessages(); final Session otrSession = conversation.getOtrSession();
Session otrSession = conversation.getOtrSession();
Log.d(Config.LOGTAG, Log.d(Config.LOGTAG,
account.getJid().toBareJid() + " otr session established with " account.getJid().toBareJid() + " otr session established with "
+ conversation.getContactJid() + "/" + conversation.getContactJid() + "/"
+ otrSession.getSessionID().getUserID()); + otrSession.getSessionID().getUserID());
for (Message msg : messages) { conversation.findUnsentMessagesWithOtrEncryption(new Conversation.OnMessageFound() {
if ((msg.getStatus() == Message.STATUS_UNSEND || msg.getStatus() == Message.STATUS_WAITING)
&& (msg.getEncryption() == Message.ENCRYPTION_OTR)) { @Override
public void onMessageFound(Message message) {
SessionID id = otrSession.getSessionID(); SessionID id = otrSession.getSessionID();
try { try {
msg.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID())); message.setCounterpart(Jid.fromString(id.getAccountID() + "/" + id.getUserID()));
} catch (InvalidJidException e) { } catch (InvalidJidException e) {
break; return;
} }
if (msg.getType() == Message.TYPE_TEXT) { if (message.getType() == Message.TYPE_TEXT) {
MessagePacket outPacket = mMessageGenerator MessagePacket outPacket = mMessageGenerator.generateOtrChat(message, true);
.generateOtrChat(msg, true);
if (outPacket != null) { if (outPacket != null) {
msg.setStatus(Message.STATUS_SEND); message.setStatus(Message.STATUS_SEND);
databaseBackend.updateMessage(msg); databaseBackend.updateMessage(message);
sendMessagePacket(account, outPacket); sendMessagePacket(account, outPacket);
} }
} else if (msg.getType() == Message.TYPE_IMAGE || msg.getType() == Message.TYPE_FILE) { } else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
mJingleConnectionManager.createNewConnection(msg); mJingleConnectionManager.createNewConnection(message);
}
}
} }
updateConversationUi(); updateConversationUi();
} }
});
}
public boolean renewSymmetricKey(Conversation conversation) { public boolean renewSymmetricKey(Conversation conversation) {
Account account = conversation.getAccount(); Account account = conversation.getAccount();
@ -1817,12 +1816,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void resetSendingToWaiting(Account account) { public void resetSendingToWaiting(Account account) {
for (Conversation conversation : getConversations()) { for (Conversation conversation : getConversations()) {
if (conversation.getAccount() == account) { if (conversation.getAccount() == account) {
for (Message message : conversation.getMessages()) { conversation.findUnsentTextMessages(new Conversation.OnMessageFound() {
if (message.getType() != Message.TYPE_IMAGE
&& message.getStatus() == Message.STATUS_UNSEND) { @Override
public void onMessageFound(Message message) {
markMessage(message, Message.STATUS_WAITING); markMessage(message, Message.STATUS_WAITING);
} }
} });
} }
} }
} }
@ -1847,17 +1847,15 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
if (uuid == null) { if (uuid == null) {
return false; return false;
} else { } else {
for (Message message : conversation.getMessages()) { Message message = conversation.findSentMessageWithUuid(uuid);
if (uuid.equals(message.getUuid()) if (message!=null) {
|| (message.getStatus() >= Message.STATUS_SEND && uuid markMessage(message,status);
.equals(message.getRemoteMsgId()))) {
markMessage(message, status);
return true; return true;
} } else {
}
return false; return false;
} }
} }
}
public void markMessage(Message message, int status) { public void markMessage(Message message, int status) {
if (status == Message.STATUS_SEND_FAILED if (status == Message.STATUS_SEND_FAILED
@ -1949,15 +1947,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
} }
public void failWaitingOtrMessages(Conversation conversation) {
for (Message message : conversation.getMessages()) {
if (message.getEncryption() == Message.ENCRYPTION_OTR
&& message.getStatus() == Message.STATUS_WAITING) {
markMessage(message, Message.STATUS_SEND_FAILED);
}
}
}
public SecureRandom getRNG() { public SecureRandom getRNG() {
return this.mRandom; return this.mRandom;
} }

View File

@ -491,10 +491,12 @@ public class ConversationActivity extends XmppActivity implements
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
ConversationActivity.this.xmppConnectionService conversation.clearMessages();
.clearConversationHistory(conversation);
if (endConversationCheckBox.isChecked()) { if (endConversationCheckBox.isChecked()) {
endConversation(conversation); endConversation(conversation);
} else {
updateConversationList();
ConversationActivity.this.mConversationFragment.updateMessages();
} }
} }
}); });

View File

@ -120,8 +120,7 @@ public class ConversationFragment extends Fragment {
long timestamp = ConversationFragment.this.messageList.get(0).getTimeSent(); long timestamp = ConversationFragment.this.messageList.get(0).getTimeSent();
messagesLoaded = false; messagesLoaded = false;
int size = activity.xmppConnectionService.loadMoreMessages(conversation, timestamp); int size = activity.xmppConnectionService.loadMoreMessages(conversation, timestamp);
ConversationFragment.this.messageList.clear(); conversation.populateWithMessages(ConversationFragment.this.messageList);
ConversationFragment.this.messageList.addAll(conversation.getMessages());
updateStatusMessages(); updateStatusMessages();
messageListAdapter.notifyDataSetChanged(); messageListAdapter.notifyDataSetChanged();
if (size != 0) { if (size != 0) {
@ -580,12 +579,7 @@ public class ConversationFragment extends Fragment {
break; break;
} }
} }
this.messageList.clear(); conversation.populateWithMessages(ConversationFragment.this.messageList);
if (this.conversation.getMessages().size() == 0) {
messagesLoaded = false;
} else {
this.messageList.addAll(this.conversation.getMessages());
messagesLoaded = true;
for (Message message : this.messageList) { for (Message message : this.messageList) {
if (message.getEncryption() == Message.ENCRYPTION_PGP if (message.getEncryption() == Message.ENCRYPTION_PGP
&& (message.getStatus() == Message.STATUS_RECEIVED || message && (message.getStatus() == Message.STATUS_RECEIVED || message
@ -598,7 +592,6 @@ public class ConversationFragment extends Fragment {
} }
decryptNext(); decryptNext();
updateStatusMessages(); updateStatusMessages();
}
this.messageListAdapter.notifyDataSetChanged(); this.messageListAdapter.notifyDataSetChanged();
updateChatMsgHint(); updateChatMsgHint();
if (!activity.isConversationsOverviewVisable() || !activity.isConversationsOverviewHideable()) { if (!activity.isConversationsOverviewVisable() || !activity.isConversationsOverviewHideable()) {