139 lines
5.2 KiB
Java
139 lines
5.2 KiB
Java
/*
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.emailcommon.utility;
|
|
|
|
import com.android.emailcommon.internet.MimeHeader;
|
|
import com.android.emailcommon.internet.MimeUtility;
|
|
import com.android.emailcommon.mail.MessagingException;
|
|
import com.android.emailcommon.mail.Part;
|
|
import com.android.emailcommon.provider.EmailContent;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class ConversionUtilities {
|
|
/**
|
|
* Values for HEADER_ANDROID_BODY_QUOTED_PART to tag body parts
|
|
*/
|
|
public static final String BODY_QUOTED_PART_REPLY = "quoted-reply";
|
|
public static final String BODY_QUOTED_PART_FORWARD = "quoted-forward";
|
|
public static final String BODY_QUOTED_PART_INTRO = "quoted-intro";
|
|
|
|
/**
|
|
* Helper function to append text to a StringBuffer, creating it if necessary.
|
|
* Optimization: The majority of the time we are *not* appending - we should have a path
|
|
* that deals with single strings.
|
|
*/
|
|
private static StringBuffer appendTextPart(StringBuffer sb, String newText) {
|
|
if (newText == null) {
|
|
return sb;
|
|
}
|
|
else if (sb == null) {
|
|
sb = new StringBuffer(newText);
|
|
} else {
|
|
if (sb.length() > 0) {
|
|
sb.append('\n');
|
|
}
|
|
sb.append(newText);
|
|
}
|
|
return sb;
|
|
}
|
|
|
|
/**
|
|
* Copy body text (plain and/or HTML) from MimeMessage to provider Message
|
|
*/
|
|
public static boolean updateBodyFields(EmailContent.Body body,
|
|
EmailContent.Message localMessage, ArrayList<Part> viewables)
|
|
throws MessagingException {
|
|
|
|
body.mMessageKey = localMessage.mId;
|
|
|
|
StringBuffer sbHtml = null;
|
|
StringBuffer sbText = null;
|
|
StringBuffer sbHtmlReply = null;
|
|
StringBuffer sbTextReply = null;
|
|
StringBuffer sbIntroText = null;
|
|
|
|
for (Part viewable : viewables) {
|
|
String text = MimeUtility.getTextFromPart(viewable);
|
|
String[] replyTags = viewable.getHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART);
|
|
String replyTag = null;
|
|
if (replyTags != null && replyTags.length > 0) {
|
|
replyTag = replyTags[0];
|
|
}
|
|
// Deploy text as marked by the various tags
|
|
boolean isHtml = "text/html".equalsIgnoreCase(viewable.getMimeType());
|
|
|
|
if (replyTag != null) {
|
|
boolean isQuotedReply = BODY_QUOTED_PART_REPLY.equalsIgnoreCase(replyTag);
|
|
boolean isQuotedForward = BODY_QUOTED_PART_FORWARD.equalsIgnoreCase(replyTag);
|
|
boolean isQuotedIntro = BODY_QUOTED_PART_INTRO.equalsIgnoreCase(replyTag);
|
|
|
|
if (isQuotedReply || isQuotedForward) {
|
|
if (isHtml) {
|
|
sbHtmlReply = appendTextPart(sbHtmlReply, text);
|
|
} else {
|
|
sbTextReply = appendTextPart(sbTextReply, text);
|
|
}
|
|
// Set message flags as well
|
|
localMessage.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK;
|
|
localMessage.mFlags |= isQuotedReply
|
|
? EmailContent.Message.FLAG_TYPE_REPLY
|
|
: EmailContent.Message.FLAG_TYPE_FORWARD;
|
|
continue;
|
|
}
|
|
if (isQuotedIntro) {
|
|
sbIntroText = appendTextPart(sbIntroText, text);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// Most of the time, just process regular body parts
|
|
if (isHtml) {
|
|
sbHtml = appendTextPart(sbHtml, text);
|
|
} else {
|
|
sbText = appendTextPart(sbText, text);
|
|
}
|
|
}
|
|
|
|
// write the combined data to the body part
|
|
if (!TextUtils.isEmpty(sbText)) {
|
|
String text = sbText.toString();
|
|
body.mTextContent = text;
|
|
localMessage.mSnippet = TextUtilities.makeSnippetFromPlainText(text);
|
|
}
|
|
if (!TextUtils.isEmpty(sbHtml)) {
|
|
String text = sbHtml.toString();
|
|
body.mHtmlContent = text;
|
|
if (localMessage.mSnippet == null) {
|
|
localMessage.mSnippet = TextUtilities.makeSnippetFromHtmlText(text);
|
|
}
|
|
}
|
|
if (sbHtmlReply != null && sbHtmlReply.length() != 0) {
|
|
body.mHtmlReply = sbHtmlReply.toString();
|
|
}
|
|
if (sbTextReply != null && sbTextReply.length() != 0) {
|
|
body.mTextReply = sbTextReply.toString();
|
|
}
|
|
if (sbIntroText != null && sbIntroText.length() != 0) {
|
|
body.mIntroText = sbIntroText.toString();
|
|
}
|
|
return true;
|
|
}
|
|
}
|