diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/Message.java b/k9mail-library/src/main/java/com/fsck/k9/mail/Message.java index 3d19cbf9d74cf9adc99a29abb4eaaf1b714560a2..1b51b8a1ed3c7c014bb922247e2e103047d6fc45 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/Message.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/Message.java @@ -90,7 +90,7 @@ public abstract class Message implements Part, CompositeBody { public abstract void setSentDate(Date sentDate, boolean hideTimeZone) throws MessagingException; - public abstract Address[] getRecipients(RecipientType type) throws MessagingException; + public abstract Address[] getRecipients(RecipientType type); public abstract void setRecipients(RecipientType type, Address[] addresses) throws MessagingException; diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java index 33a43c473290afddb3ea39fe6340b001e10b4235..43f259879abd2fd1fdf0a7ca5e9976569006f124 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java @@ -197,25 +197,29 @@ public class MimeMessage extends Message { * found the method returns an empty array. */ @Override - public Address[] getRecipients(RecipientType type) throws MessagingException { - if (type == RecipientType.TO) { - if (mTo == null) { - mTo = Address.parse(MimeUtility.unfold(getFirstHeader("To"))); + public Address[] getRecipients(RecipientType type) { + switch (type) { + case TO: { + if (mTo == null) { + mTo = Address.parse(MimeUtility.unfold(getFirstHeader("To"))); + } + return mTo; } - return mTo; - } else if (type == RecipientType.CC) { - if (mCc == null) { - mCc = Address.parse(MimeUtility.unfold(getFirstHeader("CC"))); + case CC: { + if (mCc == null) { + mCc = Address.parse(MimeUtility.unfold(getFirstHeader("CC"))); + } + return mCc; } - return mCc; - } else if (type == RecipientType.BCC) { - if (mBcc == null) { - mBcc = Address.parse(MimeUtility.unfold(getFirstHeader("BCC"))); + case BCC: { + if (mBcc == null) { + mBcc = Address.parse(MimeUtility.unfold(getFirstHeader("BCC"))); + } + return mBcc; } - return mBcc; - } else { - throw new MessagingException("Unrecognized recipient type."); } + + throw new IllegalArgumentException("Unrecognized recipient type."); } @Override diff --git a/k9mail/src/main/java/com/fsck/k9/K9.java b/k9mail/src/main/java/com/fsck/k9/K9.java index 94593025564d10eec672b4f81065dcc276cb3650..93ceeccab212c22f19eb93e1c8d727eedc6da419 100644 --- a/k9mail/src/main/java/com/fsck/k9/K9.java +++ b/k9mail/src/main/java/com/fsck/k9/K9.java @@ -538,33 +538,24 @@ public class K9 extends Application { MessagingController.getInstance(this).addListener(new MessagingListener() { private void broadcastIntent(String action, Account account, String folder, Message message) { - try { - Uri uri = Uri.parse("email://messages/" + account.getAccountNumber() + "/" + Uri.encode(folder) + "/" + Uri.encode(message.getUid())); - Intent intent = new Intent(action, uri); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_ACCOUNT, account.getDescription()); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_FOLDER, folder); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_SENT_DATE, message.getSentDate()); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_FROM, Address.toString(message.getFrom())); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_TO, Address.toString(message.getRecipients(Message.RecipientType.TO))); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_CC, Address.toString(message.getRecipients(Message.RecipientType.CC))); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_BCC, Address.toString(message.getRecipients(Message.RecipientType.BCC))); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_SUBJECT, message.getSubject()); - intent.putExtra(K9.Intents.EmailReceived.EXTRA_FROM_SELF, account.isAnIdentity(message.getFrom())); - K9.this.sendBroadcast(intent); - if (K9.DEBUG) - Log.d(K9.LOG_TAG, "Broadcasted: action=" + action - + " account=" + account.getDescription() - + " folder=" + folder - + " message uid=" + message.getUid() - ); - - } catch (MessagingException e) { - Log.w(K9.LOG_TAG, "Error: action=" + action + Uri uri = Uri.parse("email://messages/" + account.getAccountNumber() + "/" + Uri.encode(folder) + "/" + Uri.encode(message.getUid())); + Intent intent = new Intent(action, uri); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_ACCOUNT, account.getDescription()); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_FOLDER, folder); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_SENT_DATE, message.getSentDate()); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_FROM, Address.toString(message.getFrom())); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_TO, Address.toString(message.getRecipients(Message.RecipientType.TO))); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_CC, Address.toString(message.getRecipients(Message.RecipientType.CC))); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_BCC, Address.toString(message.getRecipients(Message.RecipientType.BCC))); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_SUBJECT, message.getSubject()); + intent.putExtra(K9.Intents.EmailReceived.EXTRA_FROM_SELF, account.isAnIdentity(message.getFrom())); + K9.this.sendBroadcast(intent); + if (K9.DEBUG) + Log.d(K9.LOG_TAG, "Broadcasted: action=" + action + " account=" + account.getDescription() + " folder=" + folder + " message uid=" + message.getUid() ); - } } private void updateUnreadWidget() { diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java index cdc563ee8fe3bc5cf784ba7ccd25fff21c073f23..c230537e149420853630f3986df0ac36f80f086c 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -76,6 +76,7 @@ import com.fsck.k9.fragment.ProgressDialogFragment.CancelListener; import com.fsck.k9.helper.Contacts; import com.fsck.k9.helper.IdentityHelper; import com.fsck.k9.helper.MailTo; +import com.fsck.k9.helper.ReplyToParser; import com.fsck.k9.helper.SimpleTextWatcher; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Flag; @@ -400,7 +401,8 @@ public class MessageCompose extends K9Activity implements OnClickListener, RecipientMvpView recipientMvpView = new RecipientMvpView(this); ComposePgpInlineDecider composePgpInlineDecider = new ComposePgpInlineDecider(); - recipientPresenter = new RecipientPresenter(this, recipientMvpView, mAccount, composePgpInlineDecider); + recipientPresenter = new RecipientPresenter(this, recipientMvpView, mAccount, + composePgpInlineDecider, new ReplyToParser()); mSubjectView = (EditText) findViewById(R.id.subject); mSubjectView.getInputExtras(true).putBoolean("allowEmoji", true); @@ -1598,7 +1600,8 @@ public class MessageCompose extends K9Activity implements OnClickListener, * If a reply-to was included with the message use that, otherwise use the from * or sender address. */ - recipientPresenter.initFromReplyToMessage(message); + boolean isReplyAll = mAction == Action.REPLY_ALL; + recipientPresenter.initFromReplyToMessage(message, isReplyAll); if (message.getMessageId() != null && message.getMessageId().length() > 0) { mInReplyTo = message.getMessageId(); diff --git a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.java b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.java index 52c66e1e39b4044e21a80233c6ab6e94b5c3e038..eeee0223885451031dbb6208c0c17b3d2906a159 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.java @@ -27,15 +27,14 @@ import com.fsck.k9.activity.compose.ComposeCryptoStatus.SendErrorState; import com.fsck.k9.helper.Contacts; import com.fsck.k9.helper.MailTo; import com.fsck.k9.helper.ReplyToParser; -import com.fsck.k9.helper.Utility; +import com.fsck.k9.helper.ReplyToParser.ReplyToAddresses; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Message.RecipientType; -import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mailstore.LocalMessage; -import com.fsck.k9.message.PgpMessageBuilder; import com.fsck.k9.message.ComposePgpInlineDecider; +import com.fsck.k9.message.PgpMessageBuilder; import com.fsck.k9.view.RecipientSelectView.Recipient; import org.openintents.openpgp.IOpenPgpService2; import org.openintents.openpgp.util.OpenPgpApi; @@ -61,6 +60,7 @@ public class RecipientPresenter implements PermissionPingCallback { private final Context context; private final RecipientMvpView recipientMvpView; private final ComposePgpInlineDecider composePgpInlineDecider; + private ReplyToParser replyToParser; private Account account; private String cryptoProvider; private Boolean hasContactPicker; @@ -78,10 +78,11 @@ public class RecipientPresenter implements PermissionPingCallback { public RecipientPresenter(Context context, RecipientMvpView recipientMvpView, Account account, - ComposePgpInlineDecider composePgpInlineDecider) { + ComposePgpInlineDecider composePgpInlineDecider, ReplyToParser replyToParser) { this.recipientMvpView = recipientMvpView; this.context = context; this.composePgpInlineDecider = composePgpInlineDecider; + this.replyToParser = replyToParser; recipientMvpView.setPresenter(this); onSwitchAccount(account); @@ -134,50 +135,17 @@ public class RecipientPresenter implements PermissionPingCallback { return false; } - public void initFromReplyToMessage(Message message) { - Address[] replyToAddresses = ReplyToParser.getRecipientsToReplyTo(message); - - try { - // if we're replying to a message we sent, we probably meant - // to reply to the recipient of that message - if (account.isAnIdentity(replyToAddresses)) { - replyToAddresses = message.getRecipients(RecipientType.TO); - } + public void initFromReplyToMessage(Message message, boolean isReplyAll) { + ReplyToAddresses replyToAddresses = isReplyAll ? + replyToParser.getRecipientsToReplyAllTo(message, account) : + replyToParser.getRecipientsToReplyTo(message, account); - addRecipientsFromAddresses(RecipientType.TO, replyToAddresses); + addToAddresses(replyToAddresses.to); + addCcAddresses(replyToAddresses.cc); - if (message.getReplyTo().length > 0) { - for (Address address : message.getFrom()) { - if (!account.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { - addRecipientsFromAddresses(RecipientType.TO, address); - } - } - } - - for (Address address : message.getRecipients(RecipientType.TO)) { - if (!account.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { - addToAddresses(address); - } - - } - - if (message.getRecipients(RecipientType.CC).length > 0) { - for (Address address : message.getRecipients(RecipientType.CC)) { - if (!account.isAnIdentity(address) && !Utility.arrayContains(replyToAddresses, address)) { - addCcAddresses(address); - } - - } - } - - boolean shouldSendAsPgpInline = composePgpInlineDecider.shouldReplyInline(message); - if (shouldSendAsPgpInline) { - cryptoEnablePgpInline = true; - } - - } catch (MessagingException e) { - // can't happen, we know the recipient types exist - throw new AssertionError(e); + boolean shouldSendAsPgpInline = composePgpInlineDecider.shouldReplyInline(message); + if (shouldSendAsPgpInline) { + cryptoEnablePgpInline = true; } } @@ -228,18 +196,13 @@ public class RecipientPresenter implements PermissionPingCallback { } private void initRecipientsFromDraftMessage(LocalMessage message) { - try { - addToAddresses(message.getRecipients(RecipientType.TO)); + addToAddresses(message.getRecipients(RecipientType.TO)); - Address[] ccRecipients = message.getRecipients(RecipientType.CC); - addCcAddresses(ccRecipients); + Address[] ccRecipients = message.getRecipients(RecipientType.CC); + addCcAddresses(ccRecipients); - Address[] bccRecipients = message.getRecipients(RecipientType.BCC); - addBccAddresses(bccRecipients); - } catch (MessagingException e) { - // can't happen, we know the recipient types exist - throw new AssertionError(e); - } + Address[] bccRecipients = message.getRecipients(RecipientType.BCC); + addBccAddresses(bccRecipients); } private void initPgpInlineFromDraftMessage(LocalMessage message) { diff --git a/k9mail/src/main/java/com/fsck/k9/helper/IdentityHelper.java b/k9mail/src/main/java/com/fsck/k9/helper/IdentityHelper.java index 8c9023b72a4209107746de4663804cdc80d1a7d5..ac79c416c7c0e6d6601aa2ab0c83adc390831025 100644 --- a/k9mail/src/main/java/com/fsck/k9/helper/IdentityHelper.java +++ b/k9mail/src/main/java/com/fsck/k9/helper/IdentityHelper.java @@ -1,13 +1,10 @@ package com.fsck.k9.helper; -import android.util.Log; import com.fsck.k9.Account; import com.fsck.k9.Identity; -import com.fsck.k9.K9; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Message; -import com.fsck.k9.mail.MessagingException; public class IdentityHelper { @@ -27,28 +24,24 @@ public class IdentityHelper { public static Identity getRecipientIdentityFromMessage(Account account, Message message) { Identity recipient = null; - try { - for (Address address : message.getRecipients(Message.RecipientType.TO)) { - Identity identity = account.findIdentity(address); - if (identity != null) { - recipient = identity; - break; - } + for (Address address : message.getRecipients(Message.RecipientType.TO)) { + Identity identity = account.findIdentity(address); + if (identity != null) { + recipient = identity; + break; } - if (recipient == null) { - Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC); - if (ccAddresses.length > 0) { - for (Address address : ccAddresses) { - Identity identity = account.findIdentity(address); - if (identity != null) { - recipient = identity; - break; - } + } + if (recipient == null) { + Address[] ccAddresses = message.getRecipients(Message.RecipientType.CC); + if (ccAddresses.length > 0) { + for (Address address : ccAddresses) { + Identity identity = account.findIdentity(address); + if (identity != null) { + recipient = identity; + break; } } } - } catch (MessagingException e) { - Log.w(K9.LOG_TAG, "Error finding the identity this message was sent to", e); } if (recipient == null) { diff --git a/k9mail/src/main/java/com/fsck/k9/helper/MessageHelper.java b/k9mail/src/main/java/com/fsck/k9/helper/MessageHelper.java index ddc2f4a4d01b4db89c66fdc5598ed30503d9307c..0895255f4c62bc25c0318763d93d025a89589fbb 100644 --- a/k9mail/src/main/java/com/fsck/k9/helper/MessageHelper.java +++ b/k9mail/src/main/java/com/fsck/k9/helper/MessageHelper.java @@ -1,12 +1,12 @@ package com.fsck.k9.helper; + import android.content.Context; import android.text.Spannable; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.style.ForegroundColorSpan; -import android.util.Log; import com.fsck.k9.Account; import com.fsck.k9.K9; @@ -15,7 +15,6 @@ import com.fsck.k9.activity.FolderInfoHolder; import com.fsck.k9.activity.MessageInfoHolder; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Flag; -import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mailstore.LocalMessage; @@ -53,45 +52,42 @@ public class MessageHelper { final FolderInfoHolder folder, Account account) { final Contacts contactHelper = K9.showContactName() ? Contacts.getInstance(mContext) : null; - try { - target.message = message; - target.compareArrival = message.getInternalDate(); - target.compareDate = message.getSentDate(); - if (target.compareDate == null) { - target.compareDate = message.getInternalDate(); - } - target.folder = folder; + target.message = message; + target.compareArrival = message.getInternalDate(); + target.compareDate = message.getSentDate(); + if (target.compareDate == null) { + target.compareDate = message.getInternalDate(); + } - target.read = message.isSet(Flag.SEEN); - target.answered = message.isSet(Flag.ANSWERED); - target.forwarded = message.isSet(Flag.FORWARDED); - target.flagged = message.isSet(Flag.FLAGGED); + target.folder = folder; - Address[] addrs = message.getFrom(); + target.read = message.isSet(Flag.SEEN); + target.answered = message.isSet(Flag.ANSWERED); + target.forwarded = message.isSet(Flag.FORWARDED); + target.flagged = message.isSet(Flag.FLAGGED); - if (addrs.length > 0 && account.isAnIdentity(addrs[0])) { - CharSequence to = toFriendly(message.getRecipients(RecipientType.TO), contactHelper); - target.compareCounterparty = to.toString(); - target.sender = new SpannableStringBuilder(mContext.getString(R.string.message_to_label)).append(to); - } else { - target.sender = toFriendly(addrs, contactHelper); - target.compareCounterparty = target.sender.toString(); - } + Address[] addrs = message.getFrom(); - if (addrs.length > 0) { - target.senderAddress = addrs[0].getAddress(); - } else { - // a reasonable fallback "whomever we were corresponding with - target.senderAddress = target.compareCounterparty; - } + if (addrs.length > 0 && account.isAnIdentity(addrs[0])) { + CharSequence to = toFriendly(message.getRecipients(RecipientType.TO), contactHelper); + target.compareCounterparty = to.toString(); + target.sender = new SpannableStringBuilder(mContext.getString(R.string.message_to_label)).append(to); + } else { + target.sender = toFriendly(addrs, contactHelper); + target.compareCounterparty = target.sender.toString(); + } - target.uid = message.getUid(); - target.account = message.getFolder().getAccountUuid(); - target.uri = message.getUri(); - } catch (MessagingException me) { - Log.w(K9.LOG_TAG, "Unable to load message info", me); + if (addrs.length > 0) { + target.senderAddress = addrs[0].getAddress(); + } else { + // a reasonable fallback "whomever we were corresponding with + target.senderAddress = target.compareCounterparty; } + + target.uid = message.getUid(); + target.account = message.getFolder().getAccountUuid(); + target.uri = message.getUri(); } public CharSequence getDisplayName(Account account, Address[] fromAddrs, Address[] toAddrs) { diff --git a/k9mail/src/main/java/com/fsck/k9/helper/ReplyToParser.java b/k9mail/src/main/java/com/fsck/k9/helper/ReplyToParser.java index e680c2d0b2589445772c055dc2d1b4e62bc6c850..2358189ac7c913e73968149d88c2ba95d66c47fb 100644 --- a/k9mail/src/main/java/com/fsck/k9/helper/ReplyToParser.java +++ b/k9mail/src/main/java/com/fsck/k9/helper/ReplyToParser.java @@ -1,23 +1,91 @@ package com.fsck.k9.helper; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +import android.support.annotation.VisibleForTesting; + +import com.fsck.k9.Account; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Message; +import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mail.internet.ListHeaders; public class ReplyToParser { - public static Address[] getRecipientsToReplyTo(Message message) { + + public ReplyToAddresses getRecipientsToReplyTo(Message message, Account account) { + Address[] candidateAddress; + Address[] replyToAddresses = message.getReplyTo(); + Address[] listPostAddresses = ListHeaders.getListPostAddresses(message); + Address[] fromAddresses = message.getFrom(); + if (replyToAddresses.length > 0) { - return replyToAddresses; + candidateAddress = replyToAddresses; + } else if (listPostAddresses.length > 0) { + candidateAddress = listPostAddresses; + } else { + candidateAddress = fromAddresses; } - Address[] listPostAddresses = ListHeaders.getListPostAddresses(message); - if (listPostAddresses.length > 0) { - return listPostAddresses; + boolean replyToAddressIsUserIdentity = account.isAnIdentity(candidateAddress); + if (replyToAddressIsUserIdentity) { + candidateAddress = message.getRecipients(RecipientType.TO); + } + + return new ReplyToAddresses(candidateAddress); + } + + public ReplyToAddresses getRecipientsToReplyAllTo(Message message, Account account) { + List
replyToAddresses = Arrays.asList(getRecipientsToReplyTo(message, account).to); + + HashSet alreadyAddedAddresses = new HashSet<>(replyToAddresses); + ArrayList toAddresses = new ArrayList<>(replyToAddresses); + ArrayList ccAddresses = new ArrayList<>(); + + for (Address address : message.getFrom()) { + if (!alreadyAddedAddresses.contains(address) && !account.isAnIdentity(address)) { + toAddresses.add(address); + alreadyAddedAddresses.add(address); + } + } + + for (Address address : message.getRecipients(RecipientType.TO)) { + if (!alreadyAddedAddresses.contains(address) && !account.isAnIdentity(address)) { + toAddresses.add(address); + alreadyAddedAddresses.add(address); + } + } + + for (Address address : message.getRecipients(RecipientType.CC)) { + if (!alreadyAddedAddresses.contains(address) && !account.isAnIdentity(address)) { + ccAddresses.add(address); + alreadyAddedAddresses.add(address); + } + } + + return new ReplyToAddresses(toAddresses, ccAddresses); + } + + public static class ReplyToAddresses { + public final Address[] to; + public final Address[] cc; + + @VisibleForTesting + public ReplyToAddresses(List toAddresses, List ccAddresses) { + to = toAddresses.toArray(new Address[toAddresses.size()]); + cc = ccAddresses.toArray(new Address[ccAddresses.size()]); } - return message.getFrom(); + @VisibleForTesting + public ReplyToAddresses(Address[] toAddresses) { + to = toAddresses; + cc = new Address[0]; + } } + } diff --git a/k9mail/src/main/java/com/fsck/k9/notification/NotificationContentCreator.java b/k9mail/src/main/java/com/fsck/k9/notification/NotificationContentCreator.java index 1d43b8c8a2556bec4a1eefcba17a4f55a1e9d2b1..02d5f27f20e7f2f6f951b8a8f809ccafc549037d 100644 --- a/k9mail/src/main/java/com/fsck/k9/notification/NotificationContentCreator.java +++ b/k9mail/src/main/java/com/fsck/k9/notification/NotificationContentCreator.java @@ -106,29 +106,25 @@ class NotificationContentCreator { } private String getMessageSender(Account account, Message message) { - try { - boolean isSelf = false; - final Contacts contacts = K9.showContactName() ? Contacts.getInstance(context) : null; - final Address[] fromAddresses = message.getFrom(); - - if (fromAddresses != null) { - isSelf = account.isAnIdentity(fromAddresses); - if (!isSelf && fromAddresses.length > 0) { - return MessageHelper.toFriendly(fromAddresses[0], contacts).toString(); - } + boolean isSelf = false; + final Contacts contacts = K9.showContactName() ? Contacts.getInstance(context) : null; + final Address[] fromAddresses = message.getFrom(); + + if (fromAddresses != null) { + isSelf = account.isAnIdentity(fromAddresses); + if (!isSelf && fromAddresses.length > 0) { + return MessageHelper.toFriendly(fromAddresses[0], contacts).toString(); } + } - if (isSelf) { - // show To: if the message was sent from me - Address[] recipients = message.getRecipients(Message.RecipientType.TO); + if (isSelf) { + // show To: if the message was sent from me + Address[] recipients = message.getRecipients(Message.RecipientType.TO); - if (recipients != null && recipients.length > 0) { - return context.getString(R.string.message_to_fmt, - MessageHelper.toFriendly(recipients[0], contacts).toString()); - } + if (recipients != null && recipients.length > 0) { + return context.getString(R.string.message_to_fmt, + MessageHelper.toFriendly(recipients[0], contacts).toString()); } - } catch (MessagingException e) { - Log.e(K9.LOG_TAG, "Unable to get sender information for notification.", e); } return null; diff --git a/k9mail/src/main/java/com/fsck/k9/view/MessageHeader.java b/k9mail/src/main/java/com/fsck/k9/view/MessageHeader.java index 1d95d7f9831a92864f90ce66d503b12878627934..d65ce820b75015eda0e7d87d3808cb066348dfe6 100644 --- a/k9mail/src/main/java/com/fsck/k9/view/MessageHeader.java +++ b/k9mail/src/main/java/com/fsck/k9/view/MessageHeader.java @@ -193,11 +193,7 @@ public class MessageHeader extends LinearLayout implements OnClickListener, OnLo } private void onAddRecipientsToClipboard(Message.RecipientType recipientType) { - try { - onAddAddressesToClipboard(mMessage.getRecipients(recipientType)); - } catch (MessagingException e) { - Log.e(K9.LOG_TAG, "Couldn't get recipients address", e); - } + onAddAddressesToClipboard(mMessage.getRecipients(recipientType)); } public void setOnFlagListener(OnClickListener listener) { diff --git a/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientPresenterTest.java b/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientPresenterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8e7108bc9cc21efa78e73487d5bdd512606208c7 --- /dev/null +++ b/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientPresenterTest.java @@ -0,0 +1,91 @@ +package com.fsck.k9.activity.compose; + + +import java.util.Arrays; +import java.util.List; + +import android.content.Context; + +import com.fsck.k9.Account; +import com.fsck.k9.helper.ReplyToParser; +import com.fsck.k9.helper.ReplyToParser.ReplyToAddresses; +import com.fsck.k9.mail.Address; +import com.fsck.k9.mail.Message; +import com.fsck.k9.mail.Message.RecipientType; +import com.fsck.k9.message.ComposePgpInlineDecider; +import com.fsck.k9.view.RecipientSelectView.Recipient; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowApplication; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +@RunWith(RobolectricTestRunner.class) +@Config(manifest = "src/main/AndroidManifest.xml", sdk = 21) +public class RecipientPresenterTest { + public static final ReplyToAddresses TO_ADDRESSES = new ReplyToAddresses(Address.parse("to@example.org")); + public static final List ALL_TO_ADDRESSES = Arrays.asList(Address.parse("allTo@example.org")); + public static final List ALL_CC_ADDRESSES = Arrays.asList(Address.parse("allCc@example.org")); + + + RecipientPresenter recipientPresenter; + private ReplyToParser replyToParser; + private ComposePgpInlineDecider composePgpInlineDecider; + private Account account; + private RecipientMvpView recipientMvpView; + + + @Before + public void setUp() throws Exception { + Context context = ShadowApplication.getInstance().getApplicationContext(); + + recipientMvpView = mock(RecipientMvpView.class); + account = mock(Account.class); + composePgpInlineDecider = mock(ComposePgpInlineDecider.class); + replyToParser = mock(ReplyToParser.class); + + recipientPresenter = new RecipientPresenter( + context, recipientMvpView, account, composePgpInlineDecider, replyToParser); + } + + @Test + public void testInitFromReplyToMessage() throws Exception { + Message message = mock(Message.class); + when(replyToParser.getRecipientsToReplyTo(message, account)).thenReturn(TO_ADDRESSES); + + recipientPresenter.initFromReplyToMessage(message, false); + + verify(recipientMvpView).addRecipients(eq(RecipientType.TO), any(Recipient[].class)); + } + + @Test + public void testInitFromReplyToAllMessage() throws Exception { + Message message = mock(Message.class); + when(replyToParser.getRecipientsToReplyTo(message, account)).thenReturn(TO_ADDRESSES); + ReplyToAddresses replyToAddresses = new ReplyToAddresses(ALL_TO_ADDRESSES, ALL_CC_ADDRESSES); + when(replyToParser.getRecipientsToReplyAllTo(message, account)).thenReturn(replyToAddresses); + + recipientPresenter.initFromReplyToMessage(message, true); + + verify(recipientMvpView).addRecipients(eq(RecipientType.TO), any(Recipient.class)); + verify(recipientMvpView).addRecipients(eq(RecipientType.CC), any(Recipient.class)); + } + + @Test + public void initFromReplyToMessage_shouldCallComposePgpInlineDecider() throws Exception { + Message message = mock(Message.class); + when(replyToParser.getRecipientsToReplyTo(message, account)).thenReturn(TO_ADDRESSES); + + recipientPresenter.initFromReplyToMessage(message, false); + + verify(composePgpInlineDecider).shouldReplyInline(message); + } +} diff --git a/k9mail/src/test/java/com/fsck/k9/helper/ReplyToParserTest.java b/k9mail/src/test/java/com/fsck/k9/helper/ReplyToParserTest.java index 090d2d4df175f94a4814d972f310ac5d8138e0f9..68794450bac8880050ec0fafb3a0765e69ab1d07 100644 --- a/k9mail/src/test/java/com/fsck/k9/helper/ReplyToParserTest.java +++ b/k9mail/src/test/java/com/fsck/k9/helper/ReplyToParserTest.java @@ -1,8 +1,14 @@ package com.fsck.k9.helper; +import java.lang.reflect.Array; +import java.util.ArrayList; + +import com.fsck.k9.Account; +import com.fsck.k9.helper.ReplyToParser.ReplyToAddresses; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Message; +import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mail.internet.ListHeaders; import org.junit.Before; import org.junit.Test; @@ -11,25 +17,38 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE, sdk = 21) public class ReplyToParserTest { - private static final Address[] REPLY_TO_ADDRESSES = createAddressArray("replyTo@example.com"); - private static final Address[] LIST_POST_ADDRESSES = createAddressArray("listPost@example.com"); - private static final Address[] FROM_ADDRESSES = createAddressArray("from@example.com"); + private static final Address[] REPLY_TO_ADDRESSES = Address.parse("replyTo1@example.com, replyTo2@example.com"); + private static final Address[] LIST_POST_ADDRESSES = Address.parse("listPost@example.com"); + private static final Address[] FROM_ADDRESSES = Address.parse("from@example.com"); + private static final Address[] TO_ADDRESSES = Address.parse("to1@example.com, to2@example.com"); + private static final Address[] CC_ADDRESSES = Address.parse("cc1@example.com, cc2@example.com"); private static final String[] LIST_POST_HEADER_VALUES = new String[] { "