).*?)\");\n // The first group in a Matcher contains the first capture group. We capture the tag found in the above REs so that\n // we can locate the *end* of that tag.\n private static final int FIND_INSERTION_POINT_FIRST_GROUP = 1;\n // HTML bits to insert as appropriate\n // TODO is it safe to assume utf-8 here?\n private static final String FIND_INSERTION_POINT_HTML_CONTENT = \"\\r\\n\";\n private static final String FIND_INSERTION_POINT_HTML_END_CONTENT = \"\";\n private static final String FIND_INSERTION_POINT_HEAD_CONTENT = \"
\";\n // Index of the start of the beginning of a String.\n private static final int FIND_INSERTION_POINT_START_OF_STRING = 0;\n\n /**\n *
Find the start and end positions of the HTML in the string. This should be the very top\n * and bottom of the displayable message. It returns a {@link InsertableHtmlContent}, which\n * contains both the insertion points and potentially modified HTML. The modified HTML should be\n * used in place of the HTML in the original message.
\n *\n *
This method loosely mimics the HTML forward/reply behavior of BlackBerry OS 4.5/BIS 2.5, which in turn mimics\n * Outlook 2003 (as best I can tell).
\n *\n * @param content Content to examine for HTML insertion points\n * @return Insertion points and HTML to use for insertion.\n */\n private InsertableHtmlContent findInsertionPoints(final String content) {\n InsertableHtmlContent insertable = new InsertableHtmlContent();\n\n // If there is no content, don't bother doing any of the regex dancing.\n if (content == null || content.equals(\"\")) {\n return insertable;\n }\n\n // Search for opening tags.\n boolean hasHtmlTag = false;\n boolean hasHeadTag = false;\n boolean hasBodyTag = false;\n // First see if we have an opening HTML tag. If we don't find one, we'll add one later.\n Matcher htmlMatcher = FIND_INSERTION_POINT_HTML.matcher(content);\n if (htmlMatcher.matches()) {\n hasHtmlTag = true;\n }\n // Look for a HEAD tag. If we're missing a BODY tag, we'll use the close of the HEAD to start our content.\n Matcher headMatcher = FIND_INSERTION_POINT_HEAD.matcher(content);\n if (headMatcher.matches()) {\n hasHeadTag = true;\n }\n // Look for a BODY tag. This is the ideal place for us to start our content.\n Matcher bodyMatcher = FIND_INSERTION_POINT_BODY.matcher(content);\n if (bodyMatcher.matches()) {\n hasBodyTag = true;\n }\n\n if (K9.DEBUG) {\n Log.d(K9.LOG_TAG, \"Open: hasHtmlTag:\" + hasHtmlTag + \" hasHeadTag:\" + hasHeadTag + \" hasBodyTag:\" + hasBodyTag);\n }\n\n // Given our inspections, let's figure out where to start our content.\n // This is the ideal case -- there's a BODY tag and we insert ourselves just after it.\n if (hasBodyTag) {\n insertable.setQuotedContent(new StringBuilder(content));\n insertable.setHeaderInsertionPoint(bodyMatcher.end(FIND_INSERTION_POINT_FIRST_GROUP));\n } else if (hasHeadTag) {\n // Now search for a HEAD tag. We can insert after there.\n\n // If BlackBerry sees a HEAD tag, it inserts right after that, so long as there is no BODY tag. It doesn't\n // try to add BODY, either. Right or wrong, it seems to work fine.\n insertable.setQuotedContent(new StringBuilder(content));\n insertable.setHeaderInsertionPoint(headMatcher.end(FIND_INSERTION_POINT_FIRST_GROUP));\n } else if (hasHtmlTag) {\n // Lastly, check for an HTML tag.\n // In this case, it will add a HEAD, but no BODY.\n StringBuilder newContent = new StringBuilder(content);\n // Insert the HEAD content just after the HTML tag.\n newContent.insert(htmlMatcher.end(FIND_INSERTION_POINT_FIRST_GROUP), FIND_INSERTION_POINT_HEAD_CONTENT);\n insertable.setQuotedContent(newContent);\n // The new insertion point is the end of the HTML tag, plus the length of the HEAD content.\n insertable.setHeaderInsertionPoint(htmlMatcher.end(FIND_INSERTION_POINT_FIRST_GROUP) + FIND_INSERTION_POINT_HEAD_CONTENT.length());\n } else {\n // If we have none of the above, we probably have a fragment of HTML. Yahoo! and Gmail both do this.\n // Again, we add a HEAD, but not BODY.\n StringBuilder newContent = new StringBuilder(content);\n // Add the HTML and HEAD tags.\n newContent.insert(FIND_INSERTION_POINT_START_OF_STRING, FIND_INSERTION_POINT_HEAD_CONTENT);\n newContent.insert(FIND_INSERTION_POINT_START_OF_STRING, FIND_INSERTION_POINT_HTML_CONTENT);\n // Append the