From 0de72c31b4189d77f1ef3a0f76f304f36795a00b Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Thu, 10 Oct 2013 16:51:39 -0400 Subject: [PATCH] Use a locale-specific date in the header of a quoted message Also, include the sent-date in the header when using the "prefix" quote style. "Be like mutt" (and gmail, and thunderbird) Also, the quoteOriginalHtmlMessage method was using the mSourceMessage field in various places when it should be using its originalMessage parameter. Related issues: 2249, 3456 --- res/values/strings.xml | 1 + src/com/fsck/k9/activity/MessageCompose.java | 79 +++++++++++++++----- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index a79f1f6bd..8dd096506 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -264,6 +264,7 @@ Please submit bug reports, contribute new features and ask questions at To: Cc: %s wrote: + On %1$s, %2$s wrote: You must add at least one recipient. No email address could be found for this contact. Some attachments cannot be forwarded because they have not been downloaded. diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index f6451fbd4..5bdd96877 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -92,6 +92,8 @@ import org.htmlcleaner.CleanerProperties; import org.htmlcleaner.HtmlCleaner; import org.htmlcleaner.SimpleHtmlSerializer; import org.htmlcleaner.TagNode; + +import java.text.DateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -3703,12 +3705,20 @@ public class MessageCompose extends K9Activity implements OnClickListener, */ private String quoteOriginalTextMessage(final Message originalMessage, final String messageBody, final QuoteStyle quoteStyle) throws MessagingException { String body = messageBody == null ? "" : messageBody; + String sentDate = getSentDateText(originalMessage); if (quoteStyle == QuoteStyle.PREFIX) { StringBuilder quotedText = new StringBuilder(body.length() + QUOTE_BUFFER_LENGTH); - quotedText.append(String.format( - getString(R.string.message_compose_reply_header_fmt) + "\r\n", - Address.toString(originalMessage.getFrom())) - ); + if (sentDate.length() != 0) { + quotedText.append(String.format( + getString(R.string.message_compose_reply_header_fmt_with_date) + "\r\n", + sentDate, + Address.toString(originalMessage.getFrom()))); + } else { + quotedText.append(String.format( + getString(R.string.message_compose_reply_header_fmt) + "\r\n", + Address.toString(originalMessage.getFrom())) + ); + } final String prefix = mAccount.getQuotePrefix(); final String wrappedText = Utility.wrap(body, REPLY_WRAP_LINE_WIDTH - prefix.length()); @@ -3726,8 +3736,8 @@ public class MessageCompose extends K9Activity implements OnClickListener, if (originalMessage.getFrom() != null && Address.toString(originalMessage.getFrom()).length() != 0) { quotedText.append(getString(R.string.message_compose_quote_header_from)).append(" ").append(Address.toString(originalMessage.getFrom())).append("\r\n"); } - if (originalMessage.getSentDate() != null) { - quotedText.append(getString(R.string.message_compose_quote_header_send_date)).append(" ").append(originalMessage.getSentDate()).append("\r\n"); + if (sentDate.length() != 0) { + quotedText.append(getString(R.string.message_compose_quote_header_send_date)).append(" ").append(sentDate).append("\r\n"); } if (originalMessage.getRecipients(RecipientType.TO) != null && originalMessage.getRecipients(RecipientType.TO).length != 0) { quotedText.append(getString(R.string.message_compose_quote_header_to)).append(" ").append(Address.toString(originalMessage.getRecipients(RecipientType.TO))).append("\r\n"); @@ -3760,13 +3770,22 @@ public class MessageCompose extends K9Activity implements OnClickListener, private InsertableHtmlContent quoteOriginalHtmlMessage(final Message originalMessage, final String messageBody, final QuoteStyle quoteStyle) throws MessagingException { InsertableHtmlContent insertable = findInsertionPoints(messageBody); + String sentDate = getSentDateText(originalMessage); if (quoteStyle == QuoteStyle.PREFIX) { StringBuilder header = new StringBuilder(QUOTE_BUFFER_LENGTH); header.append("
"); - header.append(HtmlConverter.textToHtmlFragment(String.format( - getString(R.string.message_compose_reply_header_fmt), - Address.toString(originalMessage.getFrom())) - )); + if (sentDate.length() != 0) { + header.append(HtmlConverter.textToHtmlFragment(String.format( + getString(R.string.message_compose_reply_header_fmt_with_date), + sentDate, + Address.toString(originalMessage.getFrom())) + )); + } else { + header.append(HtmlConverter.textToHtmlFragment(String.format( + getString(R.string.message_compose_reply_header_fmt), + Address.toString(originalMessage.getFrom())) + )); + } header.append("
\r\n"); @@ -3779,29 +3798,29 @@ public class MessageCompose extends K9Activity implements OnClickListener, StringBuilder header = new StringBuilder(); header.append("
\r\n"); header.append("
\r\n"); // This gets converted into a horizontal line during html to text conversion. - if (mSourceMessage.getFrom() != null && Address.toString(mSourceMessage.getFrom()).length() != 0) { + if (originalMessage.getFrom() != null && Address.toString(originalMessage.getFrom()).length() != 0) { header.append("").append(getString(R.string.message_compose_quote_header_from)).append(" ") - .append(HtmlConverter.textToHtmlFragment(Address.toString(mSourceMessage.getFrom()))) + .append(HtmlConverter.textToHtmlFragment(Address.toString(originalMessage.getFrom()))) .append("
\r\n"); } - if (mSourceMessage.getSentDate() != null) { + if (sentDate.length() != 0) { header.append("").append(getString(R.string.message_compose_quote_header_send_date)).append(" ") - .append(mSourceMessage.getSentDate()) + .append(sentDate) .append("
\r\n"); } - if (mSourceMessage.getRecipients(RecipientType.TO) != null && mSourceMessage.getRecipients(RecipientType.TO).length != 0) { + if (originalMessage.getRecipients(RecipientType.TO) != null && originalMessage.getRecipients(RecipientType.TO).length != 0) { header.append("").append(getString(R.string.message_compose_quote_header_to)).append(" ") - .append(HtmlConverter.textToHtmlFragment(Address.toString(mSourceMessage.getRecipients(RecipientType.TO)))) + .append(HtmlConverter.textToHtmlFragment(Address.toString(originalMessage.getRecipients(RecipientType.TO)))) .append("
\r\n"); } - if (mSourceMessage.getRecipients(RecipientType.CC) != null && mSourceMessage.getRecipients(RecipientType.CC).length != 0) { + if (originalMessage.getRecipients(RecipientType.CC) != null && originalMessage.getRecipients(RecipientType.CC).length != 0) { header.append("").append(getString(R.string.message_compose_quote_header_cc)).append(" ") - .append(HtmlConverter.textToHtmlFragment(Address.toString(mSourceMessage.getRecipients(RecipientType.CC)))) + .append(HtmlConverter.textToHtmlFragment(Address.toString(originalMessage.getRecipients(RecipientType.CC)))) .append("
\r\n"); } - if (mSourceMessage.getSubject() != null) { + if (originalMessage.getSubject() != null) { header.append("").append(getString(R.string.message_compose_quote_header_subject)).append(" ") - .append(HtmlConverter.textToHtmlFragment(mSourceMessage.getSubject())) + .append(HtmlConverter.textToHtmlFragment(originalMessage.getSubject())) .append("
\r\n"); } header.append("
\r\n"); @@ -3990,6 +4009,26 @@ public class MessageCompose extends K9Activity implements OnClickListener, return (mQuotedTextMode == QuotedTextMode.SHOW); } + /** + * Extract the date from a message and convert it into a locale-specific + * date string suitable for use in a header for a quoted message. + * + * @param message + * @return A string with the formatted date/time + */ + private String getSentDateText(Message message) { + try { + final int dateStyle = DateFormat.LONG; + final int timeStyle = DateFormat.LONG; + Date date = message.getSentDate(); + Locale locale = getResources().getConfiguration().locale; + return DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale) + .format(date); + } catch (Exception e) { + return ""; + } + } + /** * An {@link EditText} extension with methods that convert line endings from * {@code \r\n} to {@code \n} and back again when setting and getting text.