mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-30 13:12:25 -05:00
Restructured and documented MessageCompose.buildText()
Also fixed a bug where the signature wasn't appended in the HTML part of newly composed messages.
This commit is contained in:
parent
a191415860
commit
0d8497b04b
@ -983,125 +983,153 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
|
|||||||
return buildText(isDraft, mMessageFormat);
|
return buildText(isDraft, mMessageFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Build the Body that will contain the text of the message. We'll decide where to
|
* Build the {@link Body} that will contain the text of the message.
|
||||||
* include it later. Draft messages are treated somewhat differently in that signatures are not
|
*
|
||||||
* appended and HTML separators between composed text and quoted text are not added.
|
* <p>
|
||||||
* @param isDraft If we should build a message that will be saved as a draft (as opposed to sent).
|
* Draft messages are treated somewhat differently in that signatures are not appended and HTML
|
||||||
* @param messageFormat Set MessageFormat to build.
|
* separators between composed text and quoted text are not added.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param isDraft
|
||||||
|
* If {@code true} we build a message that will be saved as a draft (as opposed to
|
||||||
|
* sent).
|
||||||
|
* @param messageFormat
|
||||||
|
* Specifies what type of message to build ({@code text/plain} vs. {@code text/html}).
|
||||||
|
*
|
||||||
|
* @return {@link TextBody} instance that contains the entered text and possibly the quoted
|
||||||
|
* original message.
|
||||||
*/
|
*/
|
||||||
private TextBody buildText(boolean isDraft, MessageFormat messageFormat) {
|
private TextBody buildText(boolean isDraft, MessageFormat messageFormat) {
|
||||||
boolean replyAfterQuote = false;
|
// The length of the formatted version of the user-supplied text/reply
|
||||||
String action = getIntent().getAction();
|
int composedMessageLength;
|
||||||
if (mAccount.isReplyAfterQuote() &&
|
|
||||||
(ACTION_REPLY.equals(action) || ACTION_REPLY_ALL.equals(action) ||
|
|
||||||
ACTION_EDIT_DRAFT.equals(action))) {
|
|
||||||
replyAfterQuote = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// The offset of the user-supplied text/reply in the final text body
|
||||||
|
int composedMessageOffset;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Find out if there is some text to quote.
|
||||||
|
*
|
||||||
|
* We include the quoted text in the body if the user didn't choose to hide it. We always
|
||||||
|
* include the quoted text when we're saving a draft. That's so the user is able to
|
||||||
|
* "un-hide" the quoted text if (s)he opens a saved draft.
|
||||||
|
*/
|
||||||
|
boolean includeQuotedText = (mQuotedHtmlContent != null &&
|
||||||
|
(mQuotedTextMode.equals(QuotedTextMode.SHOW) || isDraft));
|
||||||
|
|
||||||
|
// Reply after quote makes no sense for HEADER style replies
|
||||||
|
boolean replyAfterQuote = (mQuoteStyle == QuoteStyle.HEADER) ?
|
||||||
|
false : mAccount.isReplyAfterQuote();
|
||||||
|
|
||||||
|
boolean signatureBeforeQuotedText = mAccount.isSignatureBeforeQuotedText();
|
||||||
|
|
||||||
|
// Get the user-supplied text
|
||||||
String text = mMessageContentView.getText().toString();
|
String text = mMessageContentView.getText().toString();
|
||||||
|
|
||||||
boolean saveQuotedText = false;
|
// Handle HTML separate from the rest of the text content
|
||||||
if (isDraft || mQuotedTextMode.equals(QuotedTextMode.SHOW)) {
|
|
||||||
saveQuotedText = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Handle HTML separate from the rest of the text content. HTML mode doesn't allow signature after the quoted
|
|
||||||
// text, nor does it allow reply after quote. Users who want that functionality will need to stick with text
|
|
||||||
// mode.
|
|
||||||
if (messageFormat == MessageFormat.HTML) {
|
if (messageFormat == MessageFormat.HTML) {
|
||||||
// Place the signature immediately after the reply.
|
|
||||||
if (!isDraft) {
|
|
||||||
if (mQuoteStyle == QuoteStyle.HEADER || replyAfterQuote || mAccount.isSignatureBeforeQuotedText()) {
|
|
||||||
text = appendSignature(text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
text = HtmlConverter.textToHtmlFragment(text);
|
|
||||||
// Insert it into the existing content object.
|
|
||||||
if (K9.DEBUG && mQuotedHtmlContent != null)
|
|
||||||
Log.d(K9.LOG_TAG, "insertable: " + mQuotedHtmlContent.toDebugString());
|
|
||||||
if (mQuotedHtmlContent != null && saveQuotedText) {
|
|
||||||
|
|
||||||
// Set the insertion location based upon our reply after quote setting. Reply after
|
// Do we have to modify an existing message to include our reply?
|
||||||
// quote makes no sense for HEADER style replies. In addition, add some extra
|
if (includeQuotedText) {
|
||||||
// separators between the composed message and quoted message depending on the quote
|
if (K9.DEBUG) {
|
||||||
// location. We only add the extra separators when we're sending, that way when we
|
Log.d(K9.LOG_TAG, "insertable: " + mQuotedHtmlContent.toDebugString());
|
||||||
// load a draft, we don't have to know the length of the separators to remove them
|
}
|
||||||
// before editing.
|
|
||||||
if (mQuoteStyle == QuoteStyle.PREFIX && replyAfterQuote) {
|
if (!isDraft) {
|
||||||
mQuotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.AFTER_QUOTE);
|
// Append signature to the reply
|
||||||
|
if (replyAfterQuote || signatureBeforeQuotedText) {
|
||||||
|
text = appendSignature(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the text to HTML
|
||||||
|
text = HtmlConverter.textToHtmlFragment(text);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set the insertion location based upon our reply after quote setting.
|
||||||
|
* Additionally, add some extra separators between the composed message and quoted
|
||||||
|
* message depending on the quote location. We only add the extra separators when
|
||||||
|
* we're sending, that way when we load a draft, we don't have to know the length
|
||||||
|
* of the separators to remove them before editing.
|
||||||
|
*/
|
||||||
|
if (replyAfterQuote) {
|
||||||
|
mQuotedHtmlContent.setInsertionLocation(
|
||||||
|
InsertableHtmlContent.InsertionLocation.AFTER_QUOTE);
|
||||||
if (!isDraft) {
|
if (!isDraft) {
|
||||||
text = "<br clear=\"all\">" + text;
|
text = "<br clear=\"all\">" + text;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mQuotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.BEFORE_QUOTE);
|
mQuotedHtmlContent.setInsertionLocation(
|
||||||
|
InsertableHtmlContent.InsertionLocation.BEFORE_QUOTE);
|
||||||
if (!isDraft) {
|
if (!isDraft) {
|
||||||
text += "<br><br>";
|
text += "<br><br>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Place signature immediately after quote.
|
|
||||||
if (!isDraft) {
|
if (!isDraft) {
|
||||||
if (mQuoteStyle == QuoteStyle.PREFIX && !replyAfterQuote && !mAccount.isSignatureBeforeQuotedText()) {
|
// Place signature immediately after the quoted text
|
||||||
|
if (!(replyAfterQuote || signatureBeforeQuotedText)) {
|
||||||
mQuotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml());
|
mQuotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mQuotedHtmlContent.setUserContent(text);
|
mQuotedHtmlContent.setUserContent(text);
|
||||||
|
|
||||||
// All done. Build the body.
|
|
||||||
TextBody body = new TextBody(mQuotedHtmlContent.toString());
|
|
||||||
// Save length of the body and its offset. This is used when thawing drafts.
|
// Save length of the body and its offset. This is used when thawing drafts.
|
||||||
body.setComposedMessageLength(text.length());
|
composedMessageLength = text.length();
|
||||||
body.setComposedMessageOffset(mQuotedHtmlContent.getInsertionPoint());
|
composedMessageOffset = mQuotedHtmlContent.getInsertionPoint();
|
||||||
return body;
|
text = mQuotedHtmlContent.toString();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
TextBody body = new TextBody(text);
|
// There is no text to quote so simply append the signature if available
|
||||||
body.setComposedMessageLength(text.length());
|
if (!isDraft) {
|
||||||
// Not in reply to anything so the message starts at the beginning (0).
|
|
||||||
body.setComposedMessageOffset(0);
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
} else if (messageFormat == MessageFormat.TEXT) {
|
|
||||||
// Capture composed message length before we start attaching quoted parts and signatures.
|
|
||||||
Integer composedMessageLength = text.length();
|
|
||||||
Integer composedMessageOffset = 0;
|
|
||||||
|
|
||||||
// Placing the signature before the quoted text does not make sense if replyAfterQuote is true.
|
|
||||||
if (!isDraft) {
|
|
||||||
if (mQuoteStyle == QuoteStyle.HEADER ||
|
|
||||||
(!replyAfterQuote && mAccount.isSignatureBeforeQuotedText())) {
|
|
||||||
text = appendSignature(text);
|
text = appendSignature(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert the text to HTML
|
||||||
|
text = HtmlConverter.textToHtmlFragment(text);
|
||||||
|
|
||||||
|
//TODO: Wrap this in proper HTML tags
|
||||||
|
|
||||||
|
composedMessageLength = text.length();
|
||||||
|
composedMessageOffset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (saveQuotedText) {
|
|
||||||
if (mQuoteStyle == QuoteStyle.PREFIX && replyAfterQuote) {
|
|
||||||
composedMessageOffset = mQuotedText.getText().toString().length() + "\n".length();
|
|
||||||
text = mQuotedText.getText().toString() + "\n" + text;
|
|
||||||
} else {
|
|
||||||
text += "\n\n" + mQuotedText.getText().toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: If user has selected reply after quote AND signature before quote, ignore the
|
|
||||||
// latter setting and append the signature at the end.
|
|
||||||
if (!isDraft) {
|
|
||||||
if (mQuoteStyle == QuoteStyle.PREFIX &&
|
|
||||||
(replyAfterQuote || !mAccount.isSignatureBeforeQuotedText())) {
|
|
||||||
text = appendSignature(text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Build the body.
|
|
||||||
TextBody body = new TextBody(text);
|
|
||||||
body.setComposedMessageLength(composedMessageLength);
|
|
||||||
body.setComposedMessageOffset(composedMessageOffset);
|
|
||||||
return body;
|
|
||||||
} else {
|
} else {
|
||||||
// Shouldn't happen.
|
// Capture composed message length before we start attaching quoted parts and signatures.
|
||||||
return new TextBody("");
|
composedMessageLength = text.length();
|
||||||
|
composedMessageOffset = 0;
|
||||||
|
|
||||||
|
if (!isDraft) {
|
||||||
|
// Append signature to the text/reply
|
||||||
|
if (replyAfterQuote || signatureBeforeQuotedText) {
|
||||||
|
text = appendSignature(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (includeQuotedText) {
|
||||||
|
String quotedText = mQuotedText.getText().toString();
|
||||||
|
if (replyAfterQuote) {
|
||||||
|
composedMessageOffset = quotedText.length() + "\n".length();
|
||||||
|
text = quotedText + "\n" + text;
|
||||||
|
} else {
|
||||||
|
text += "\n\n" + quotedText.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isDraft) {
|
||||||
|
// Place signature immediately after the quoted text
|
||||||
|
if (!(replyAfterQuote || signatureBeforeQuotedText)) {
|
||||||
|
text = appendSignature(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextBody body = new TextBody(text);
|
||||||
|
body.setComposedMessageLength(composedMessageLength);
|
||||||
|
body.setComposedMessageOffset(composedMessageOffset);
|
||||||
|
|
||||||
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user