From a5e15d5a108bca8c0286d7e0171d7130c957fd24 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 26 Apr 2017 12:43:52 +0200 Subject: [PATCH] code cleanup: made conversation property final in message object --- .../conversations/entities/Conversation.java | 3 --- .../siacs/conversations/entities/Message.java | 27 ++++++++----------- .../persistance/DatabaseBackend.java | 5 ++-- .../ui/ConversationFragment.java | 5 ++-- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index c3da6977f..3c57f5630 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -472,7 +472,6 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl return message; } else { Message message = this.messages.get(this.messages.size() - 1); - message.setConversation(this); return message; } } @@ -939,14 +938,12 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl } public void add(Message message) { - message.setConversation(this); synchronized (this.messages) { this.messages.add(message); } } public void prepend(Message message) { - message.setConversation(this); synchronized (this.messages) { this.messages.add(0,message); } diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index f06d68aa0..f360122de 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -80,15 +80,15 @@ public class Message extends AbstractEntity { protected boolean read = true; protected String remoteMsgId = null; protected String serverMsgId = null; - protected Conversation conversation = null; + private final Conversation conversation; protected Transferable transferable = null; private Message mNextMessage = null; private Message mPreviousMessage = null; private String axolotlFingerprint = null; private String errorMessage = null; - private Message() { - + private Message(Conversation conversation) { + this.conversation = conversation; } public Message(Conversation conversation, String body, int encryption) { @@ -96,7 +96,7 @@ public class Message extends AbstractEntity { } public Message(Conversation conversation, String body, int encryption, int status) { - this(java.util.UUID.randomUUID().toString(), + this(conversation, java.util.UUID.randomUUID().toString(), conversation.getUuid(), conversation.getJid() == null ? null : conversation.getJid().toBareJid(), null, @@ -114,15 +114,15 @@ public class Message extends AbstractEntity { null, false, null); - this.conversation = conversation; } - private Message(final String uuid, final String conversationUUid, final Jid counterpart, + private Message(final Conversation conversation, final String uuid, final String conversationUUid, final Jid counterpart, final Jid trueCounterpart, final String body, final long timeSent, final int encryption, final int status, final int type, final boolean carbon, final String remoteMsgId, final String relativeFilePath, final String serverMsgId, final String fingerprint, final boolean read, final String edited, final boolean oob, final String errorMessage) { + this.conversation = conversation; this.uuid = uuid; this.conversationUuid = conversationUUid; this.counterpart = counterpart; @@ -143,7 +143,7 @@ public class Message extends AbstractEntity { this.errorMessage = errorMessage; } - public static Message fromCursor(Cursor cursor) { + public static Message fromCursor(Cursor cursor, Conversation conversation) { Jid jid; try { String value = cursor.getString(cursor.getColumnIndex(COUNTERPART)); @@ -168,7 +168,8 @@ public class Message extends AbstractEntity { } catch (InvalidJidException e) { trueCounterpart = null; } - return new Message(cursor.getString(cursor.getColumnIndex(UUID)), + return new Message(conversation, + cursor.getString(cursor.getColumnIndex(UUID)), cursor.getString(cursor.getColumnIndex(CONVERSATION)), jid, trueCounterpart, @@ -189,17 +190,15 @@ public class Message extends AbstractEntity { } public static Message createStatusMessage(Conversation conversation, String body) { - final Message message = new Message(); + final Message message = new Message(conversation); message.setType(Message.TYPE_STATUS); - message.setConversation(conversation); message.setBody(body); return message; } public static Message createLoadMoreMessage(Conversation conversation) { - final Message message = new Message(); + final Message message = new Message(conversation); message.setType(Message.TYPE_STATUS); - message.setConversation(conversation); message.setBody("LOAD_MORE"); return message; } @@ -244,10 +243,6 @@ public class Message extends AbstractEntity { return this.conversation; } - public void setConversation(Conversation conv) { - this.conversation = conv; - } - public Jid getCounterpart() { return counterpart; } diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 092a52ab9..3ee1acb80 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -644,9 +644,8 @@ public class DatabaseBackend extends SQLiteOpenHelper { if (cursor.getCount() > 0) { cursor.moveToLast(); do { - Message message = Message.fromCursor(cursor); + Message message = Message.fromCursor(cursor,conversation); if (message != null) { - message.setConversation(conversation); list.add(message); } } while (cursor.moveToPrevious()); @@ -677,7 +676,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { @Override public Message next() { - Message message = Message.fromCursor(cursor); + Message message = Message.fromCursor(cursor, conversation); cursor.moveToNext(); return message; } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 0e02d47b2..c211e1f00 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -379,7 +379,8 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa private void sendMessage() { final String body = mEditMessage.getText().toString(); - if (body.length() == 0 || this.conversation == null) { + final Conversation conversation = this.conversation; + if (body.length() == 0 || conversation == null) { return; } final Message message; @@ -397,7 +398,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa message.setEdited(message.getUuid()); message.setUuid(UUID.randomUUID().toString()); } - switch (conversation.getNextEncryption()) { + switch (message.getConversation().getNextEncryption()) { case Message.ENCRYPTION_OTR: sendOtrMessage(message); break;