From 13bc16caf8a58b692ccf65906f9e74114efb799f Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 8 Jan 2020 22:50:26 +0100 Subject: [PATCH] Remove Message.getFolder() --- .../com/fsck/k9/mailstore/LocalMessage.java | 33 +++++++++---------- .../k9/notification/NotificationStrategy.kt | 4 +-- .../k9/notification/K9NotificationStrategy.kt | 4 +-- .../main/java/com/fsck/k9/mail/Message.java | 26 --------------- .../fsck/k9/mail/store/imap/ImapFolder.java | 16 ++++----- .../k9/mail/store/imap/ImapFolderPusher.java | 8 ++--- .../fsck/k9/mail/store/imap/ImapMessage.java | 18 +--------- .../k9/mail/store/imap/ImapFolderTest.java | 5 ++- .../fsck/k9/mail/store/pop3/Pop3Folder.java | 8 ++--- .../fsck/k9/mail/store/pop3/Pop3Message.java | 13 +------- .../k9/mail/store/webdav/WebDavFolder.java | 4 +-- .../k9/mail/store/webdav/WebDavMessage.java | 19 +++-------- .../mail/store/webdav/WebDavMessageTest.java | 16 +-------- 13 files changed, 46 insertions(+), 128 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/mailstore/LocalMessage.java b/app/core/src/main/java/com/fsck/k9/mailstore/LocalMessage.java index 94bfc2ea4..14a83aa0b 100644 --- a/app/core/src/main/java/com/fsck/k9/mailstore/LocalMessage.java +++ b/app/core/src/main/java/com/fsck/k9/mailstore/LocalMessage.java @@ -5,6 +5,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Date; +import java.util.Objects; import android.content.ContentValues; import android.database.Cursor; @@ -13,11 +14,9 @@ import androidx.annotation.VisibleForTesting; import com.fsck.k9.Account; import com.fsck.k9.K9; -import com.fsck.k9.core.BuildConfig; import com.fsck.k9.controller.MessageReference; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Flag; -import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.internet.AddressHeaderBuilder; import com.fsck.k9.mail.internet.MimeMessage; @@ -42,13 +41,10 @@ public class LocalMessage extends MimeMessage { private String mimeType; private PreviewType previewType; private boolean headerNeedsUpdating = false; + private LocalFolder mFolder; - private LocalMessage(LocalStore localStore) { - this.localStore = localStore; - } - - LocalMessage(LocalStore localStore, String uid, Folder folder) { + LocalMessage(LocalStore localStore, String uid, LocalFolder folder) { this.localStore = localStore; this.mUid = uid; this.mFolder = folder; @@ -402,9 +398,8 @@ public class LocalMessage extends MimeMessage { return messageReference; } - @Override public LocalFolder getFolder() { - return (LocalFolder) super.getFolder(); + return mFolder; } public String getUri() { @@ -456,20 +451,22 @@ public class LocalMessage extends MimeMessage { if (o == null || getClass() != o.getClass()) { return false; } - if (!super.equals(o)) { - return false; - } - // distinguish by account uuid, in addition to what Message.equals() does above - String thisAccountUuid = getAccountUuid(); - String thatAccountUuid = ((LocalMessage) o).getAccountUuid(); - return thisAccountUuid != null ? thisAccountUuid.equals(thatAccountUuid) : thatAccountUuid == null; + LocalMessage other = (LocalMessage) o; + return Objects.equals(mUid, other.mUid) && + Objects.equals(mFolder, other.mFolder) && + Objects.equals(getAccountUuid(), other.getAccountUuid()); } @Override public int hashCode() { - int result = super.hashCode(); - result = 31 * result + (getAccountUuid() != null ? getAccountUuid().hashCode() : 0); + final int MULTIPLIER = 31; + + int result = 1; + String accountUuid = getAccountUuid(); + result = MULTIPLIER * result + (accountUuid != null ? accountUuid.hashCode() : 0); + result = MULTIPLIER * result + (mFolder != null ? mFolder.hashCode() : 0); + result = MULTIPLIER * result + mUid.hashCode(); return result; } diff --git a/app/core/src/main/java/com/fsck/k9/notification/NotificationStrategy.kt b/app/core/src/main/java/com/fsck/k9/notification/NotificationStrategy.kt index 36c3069d4..a6fb5ed64 100644 --- a/app/core/src/main/java/com/fsck/k9/notification/NotificationStrategy.kt +++ b/app/core/src/main/java/com/fsck/k9/notification/NotificationStrategy.kt @@ -1,15 +1,15 @@ package com.fsck.k9.notification import com.fsck.k9.Account -import com.fsck.k9.mail.Message import com.fsck.k9.mailstore.LocalFolder +import com.fsck.k9.mailstore.LocalMessage interface NotificationStrategy { fun shouldNotifyForMessage( account: Account, localFolder: LocalFolder, - message: Message, + message: LocalMessage, isOldMessage: Boolean ): Boolean } diff --git a/app/k9mail/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt b/app/k9mail/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt index e04f21529..72f54e692 100644 --- a/app/k9mail/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt +++ b/app/k9mail/src/main/java/com/fsck/k9/notification/K9NotificationStrategy.kt @@ -4,15 +4,15 @@ import com.fsck.k9.Account import com.fsck.k9.K9 import com.fsck.k9.helper.Contacts import com.fsck.k9.mail.Flag -import com.fsck.k9.mail.Message import com.fsck.k9.mailstore.LocalFolder +import com.fsck.k9.mailstore.LocalMessage class K9NotificationStrategy(val contacts: Contacts) : NotificationStrategy { override fun shouldNotifyForMessage( account: Account, localFolder: LocalFolder, - message: Message, + message: LocalMessage, isOldMessage: Boolean ): Boolean { diff --git a/mail/common/src/main/java/com/fsck/k9/mail/Message.java b/mail/common/src/main/java/com/fsck/k9/mail/Message.java index 399254d2f..b0c016d8d 100644 --- a/mail/common/src/main/java/com/fsck/k9/mail/Message.java +++ b/mail/common/src/main/java/com/fsck/k9/mail/Message.java @@ -26,8 +26,6 @@ public abstract class Message implements Part, Body { private Date mInternalDate; - protected Folder mFolder; - public boolean olderThan(Date earliestDate) { if (earliestDate == null) { return false; @@ -39,26 +37,6 @@ public abstract class Message implements Part, Body { return myDate != null && myDate.before(earliestDate); } - @Override - public boolean equals(Object o) { - if (o == null || !(o instanceof Message)) { - return false; - } - Message other = (Message)o; - return (getUid().equals(other.getUid()) - && getFolder().getServerId().equals(other.getFolder().getServerId())); - } - - @Override - public int hashCode() { - final int MULTIPLIER = 31; - - int result = 1; - result = MULTIPLIER * result + (mFolder != null ? mFolder.getServerId().hashCode() : 0); - result = MULTIPLIER * result + mUid.hashCode(); - return result; - } - public String getUid() { return mUid; } @@ -67,10 +45,6 @@ public abstract class Message implements Part, Body { this.mUid = uid; } - public Folder getFolder() { - return mFolder; - } - public abstract String getSubject(); public abstract void setSubject(String subject); diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java index 6d071b5fa..ad665b730 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java @@ -457,7 +457,7 @@ public class ImapFolder extends Folder { @Override public ImapMessage getMessage(String uid) throws MessagingException { - return new ImapMessage(uid, this); + return new ImapMessage(uid); } @Override @@ -587,7 +587,7 @@ public class ImapFolder extends Folder { listener.messageStarted(uid, i, count); } - ImapMessage message = new ImapMessage(uid, this); + ImapMessage message = new ImapMessage(uid); messages.add(message); if (listener != null) { @@ -817,19 +817,19 @@ public class ImapFolder extends Folder { for (int i = 0, count = flags.size(); i < count; i++) { String flag = flags.getString(i); if (flag.equalsIgnoreCase("\\Deleted")) { - message.setFlagInternal(Flag.DELETED, true); + message.setFlag(Flag.DELETED, true); } else if (flag.equalsIgnoreCase("\\Answered")) { - message.setFlagInternal(Flag.ANSWERED, true); + message.setFlag(Flag.ANSWERED, true); } else if (flag.equalsIgnoreCase("\\Seen")) { - message.setFlagInternal(Flag.SEEN, true); + message.setFlag(Flag.SEEN, true); } else if (flag.equalsIgnoreCase("\\Flagged")) { - message.setFlagInternal(Flag.FLAGGED, true); + message.setFlag(Flag.FLAGGED, true); } else if (flag.equalsIgnoreCase("$Forwarded")) { - message.setFlagInternal(Flag.FORWARDED, true); + message.setFlag(Flag.FORWARDED, true); /* a message contains FORWARDED FLAG -> so we can also create them */ store.getPermanentFlagsIndex().add(Flag.FORWARDED); } else if (flag.equalsIgnoreCase("\\Draft")){ - message.setFlagInternal(Flag.DRAFT, true); + message.setFlag(Flag.DRAFT, true); } } } diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java index e09f64105..baac08931 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java @@ -587,7 +587,7 @@ class ImapFolderPusher extends ImapFolder { List messages = new ArrayList<>(); for (long uid = startUid; uid <= newUid; uid++) { - ImapMessage message = new ImapMessage(Long.toString(uid), ImapFolderPusher.this); + ImapMessage message = new ImapMessage(Long.toString(uid)); messages.add(message); } @@ -626,10 +626,10 @@ class ImapFolderPusher extends ImapFolder { } for (String uid : removeUids) { - ImapMessage message = new ImapMessage(uid, ImapFolderPusher.this); + ImapMessage message = new ImapMessage(uid); try { - message.setFlagInternal(Flag.DELETED, true); + message.setFlag(Flag.DELETED, true); } catch (MessagingException me) { Timber.e("Unable to set DELETED flag on message %s", message.getUid()); } @@ -662,7 +662,7 @@ class ImapFolderPusher extends ImapFolder { List messages = new ArrayList<>(count); for (long uid = startUid; uid < uidNext; uid++) { - ImapMessage message = new ImapMessage(Long.toString(uid), ImapFolderPusher.this); + ImapMessage message = new ImapMessage(Long.toString(uid)); messages.add(message); } diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapMessage.java b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapMessage.java index 813e73da2..52241b7b2 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapMessage.java +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapMessage.java @@ -1,31 +1,15 @@ package com.fsck.k9.mail.store.imap; -import java.util.Collections; - -import com.fsck.k9.mail.Flag; -import com.fsck.k9.mail.Folder; -import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.internet.MimeMessage; public class ImapMessage extends MimeMessage { - ImapMessage(String uid, Folder folder) { + ImapMessage(String uid) { this.mUid = uid; - this.mFolder = folder; } public void setSize(int size) { this.mSize = size; } - - public void setFlagInternal(Flag flag, boolean set) throws MessagingException { - super.setFlag(flag, set); - } - - @Override - public void setFlag(Flag flag, boolean set) throws MessagingException { - super.setFlag(flag, set); - mFolder.setFlags(Collections.singletonList(this), Collections.singleton(flag), set); - } } diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java index 8224143dc..758cf63f8 100644 --- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java +++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java @@ -851,7 +851,7 @@ public class ImapFolderTest { folder.fetch(messages, fetchProfile, null); ImapMessage imapMessage = messages.get(0); - verify(imapMessage).setFlagInternal(Flag.SEEN, true); + verify(imapMessage).setFlag(Flag.SEEN, true); } @Test @@ -1043,13 +1043,12 @@ public class ImapFolderTest { } @Test - public void getMessageByUid_returnsNewImapMessageWithUidInFolder() throws Exception { + public void getMessageByUid_returnsNewImapMessageWithUid() throws Exception { ImapFolder folder = createFolder("Folder"); ImapMessage message = folder.getMessage("uid"); assertEquals("uid", message.getUid()); - assertEquals(folder, message.getFolder()); } private Part createPlainTextPart(String serverExtra) { diff --git a/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Folder.java b/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Folder.java index bae1c0612..54e9673bf 100644 --- a/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Folder.java +++ b/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Folder.java @@ -137,7 +137,7 @@ public class Pop3Folder extends Folder { public Pop3Message getMessage(String uid) throws MessagingException { Pop3Message message = uidToMsgMap.get(uid); if (message == null) { - message = new Pop3Message(uid, this); + message = new Pop3Message(uid); } return message; } @@ -214,7 +214,7 @@ public class Pop3Folder extends Folder { return; } String msgUid = uidParts[2]; - message = new Pop3Message(msgUid, this); + message = new Pop3Message(msgUid); indexMessage(msgNum, message); } } @@ -255,7 +255,7 @@ public class Pop3Folder extends Folder { if (msgNum >= start && msgNum <= end) { Pop3Message message = msgNumToMsgMap.get(msgNum); if (message == null) { - message = new Pop3Message(msgUid, this); + message = new Pop3Message(msgUid); indexMessage(msgNum, message); } } @@ -302,7 +302,7 @@ public class Pop3Folder extends Folder { Pop3Message message = uidToMsgMap.get(msgUid); if (message == null) { - message = new Pop3Message(msgUid, this); + message = new Pop3Message(msgUid); } indexMessage(msgNum, message); } diff --git a/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Message.java b/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Message.java index 64905b4af..94b824874 100644 --- a/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Message.java +++ b/mail/protocols/pop3/src/main/java/com/fsck/k9/mail/store/pop3/Pop3Message.java @@ -1,27 +1,16 @@ package com.fsck.k9.mail.store.pop3; -import java.util.Collections; - -import com.fsck.k9.mail.Flag; -import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.internet.MimeMessage; public class Pop3Message extends MimeMessage { - Pop3Message(String uid, Pop3Folder folder) { + Pop3Message(String uid) { mUid = uid; - mFolder = folder; mSize = -1; } public void setSize(int size) { mSize = size; } - - @Override - public void setFlag(Flag flag, boolean set) throws MessagingException { - super.setFlag(flag, set); - mFolder.setFlags(Collections.singletonList(this), Collections.singleton(flag), set); - } } diff --git a/mail/protocols/webdav/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java b/mail/protocols/webdav/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java index add97d645..2d9812eb9 100644 --- a/mail/protocols/webdav/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java +++ b/mail/protocols/webdav/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java @@ -487,7 +487,7 @@ public class WebDavFolder extends Folder { WebDavMessage wdMessage = (WebDavMessage) messages.get(i); try { - wdMessage.setFlagInternal(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid())); + wdMessage.setFlag(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid())); } catch (NullPointerException e) { Timber.v(e, "Under some weird circumstances, " + "setting the read status when syncing from webdav threw an NPE. Skipping."); @@ -550,7 +550,7 @@ public class WebDavFolder extends Folder { ParsedMessageEnvelope envelope = envelopes.get(message.getUid()); if (envelope != null) { message.setNewHeaders(envelope); - message.setFlagInternal(Flag.SEEN, envelope.getReadStatus()); + message.setFlag(Flag.SEEN, envelope.getReadStatus()); } else { Timber.e("Asked to get metadata for a non-existent message: %s", message.getUid()); } diff --git a/mail/protocols/webdav/src/main/java/com/fsck/k9/mail/store/webdav/WebDavMessage.java b/mail/protocols/webdav/src/main/java/com/fsck/k9/mail/store/webdav/WebDavMessage.java index 7e62bd089..ffbbcbc0f 100644 --- a/mail/protocols/webdav/src/main/java/com/fsck/k9/mail/store/webdav/WebDavMessage.java +++ b/mail/protocols/webdav/src/main/java/com/fsck/k9/mail/store/webdav/WebDavMessage.java @@ -1,15 +1,13 @@ package com.fsck.k9.mail.store.webdav; -import com.fsck.k9.mail.Flag; -import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.internet.MimeMessage; -import timber.log.Timber; -import java.util.Collections; import java.util.Locale; import java.util.Map; +import timber.log.Timber; + import static com.fsck.k9.mail.helper.UrlEncodingHelper.decodeUtf8; import static com.fsck.k9.mail.helper.UrlEncodingHelper.encodeUtf8; @@ -17,10 +15,11 @@ import static com.fsck.k9.mail.helper.UrlEncodingHelper.encodeUtf8; * A WebDav Message */ public class WebDavMessage extends MimeMessage { + private final WebDavFolder mFolder; private String mUrl = ""; - WebDavMessage(String uid, Folder folder) { + WebDavMessage(String uid, WebDavFolder folder) { this.mUid = uid; this.mFolder = folder; } @@ -74,10 +73,6 @@ public class WebDavMessage extends MimeMessage { this.mSize = size; } - public void setFlagInternal(Flag flag, boolean set) throws MessagingException { - super.setFlag(flag, set); - } - public void setNewHeaders(ParsedMessageEnvelope envelope) throws MessagingException { String[] headers = envelope.getHeaderList(); Map messageHeaders = envelope.getMessageHeaders(); @@ -94,10 +89,4 @@ public class WebDavMessage extends MimeMessage { } } } - - @Override - public void setFlag(Flag flag, boolean set) throws MessagingException { - super.setFlag(flag, set); - mFolder.setFlags(Collections.singletonList(this), Collections.singleton(flag), set); - } } diff --git a/mail/protocols/webdav/src/test/java/com/fsck/k9/mail/store/webdav/WebDavMessageTest.java b/mail/protocols/webdav/src/test/java/com/fsck/k9/mail/store/webdav/WebDavMessageTest.java index 7919be15a..e4453e847 100644 --- a/mail/protocols/webdav/src/test/java/com/fsck/k9/mail/store/webdav/WebDavMessageTest.java +++ b/mail/protocols/webdav/src/test/java/com/fsck/k9/mail/store/webdav/WebDavMessageTest.java @@ -1,17 +1,14 @@ package com.fsck.k9.mail.store.webdav; -import java.util.Collections; - -import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.MessagingException; + import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class WebDavMessageTest { @@ -19,10 +16,6 @@ public class WebDavMessageTest { private WebDavMessage message; @Mock private WebDavFolder mockFolder; - @Mock - private WebDavStore mockStore; - @Mock - private WebDavFolder mockTrashFolder; @Before public void before() { @@ -55,11 +48,4 @@ public class WebDavMessageTest { message.setNewHeaders(parsedMessageEnvelope); assertEquals(1024, message.getSize()); } - - @Test - public void setFlag_asks_folder_to_set_flag() throws MessagingException { - message.setFlag(Flag.FLAGGED, true); - verify(mockFolder).setFlags(Collections.singletonList(message), - Collections.singleton(Flag.FLAGGED), true); - } }