AI 147452: Update LocalFolder.fetch() to support STRUCTURE mode, in
addition to existing support for BODY mode. Add tests. BUG=1807499 Automated import of CL 147452
This commit is contained in:
parent
912dc3b194
commit
2990ff1f96
|
@ -560,12 +560,14 @@ public class LocalStore extends Store {
|
|||
new Object[] { mVisibleLimit, mFolderId });
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Supports FetchProfile.Item.BODY and FetchProfile.Item.STRUCTURE
|
||||
*/
|
||||
@Override
|
||||
public void fetch(Message[] messages, FetchProfile fp, MessageRetrievalListener listener)
|
||||
throws MessagingException {
|
||||
open(OpenMode.READ_WRITE);
|
||||
if (fp.contains(FetchProfile.Item.BODY)) {
|
||||
if (fp.contains(FetchProfile.Item.BODY) || fp.contains(FetchProfile.Item.STRUCTURE)) {
|
||||
for (Message message : messages) {
|
||||
LocalMessage localMessage = (LocalMessage)message;
|
||||
Cursor cursor = null;
|
||||
|
@ -573,30 +575,45 @@ public class LocalStore extends Store {
|
|||
MimeMultipart mp = new MimeMultipart();
|
||||
mp.setSubType("mixed");
|
||||
localMessage.setBody(mp);
|
||||
try {
|
||||
cursor = mDb.rawQuery("SELECT html_content, text_content FROM messages "
|
||||
+ "WHERE id = ?",
|
||||
new String[] { Long.toString(localMessage.mId) });
|
||||
cursor.moveToNext();
|
||||
String htmlContent = cursor.getString(0);
|
||||
String textContent = cursor.getString(1);
|
||||
|
||||
// If fetching the body, retrieve html & plaintext from DB.
|
||||
// If fetching structure, simply build placeholders for them.
|
||||
if (fp.contains(FetchProfile.Item.BODY)) {
|
||||
try {
|
||||
cursor = mDb.rawQuery("SELECT html_content, text_content FROM messages "
|
||||
+ "WHERE id = ?",
|
||||
new String[] { Long.toString(localMessage.mId) });
|
||||
cursor.moveToNext();
|
||||
String htmlContent = cursor.getString(0);
|
||||
String textContent = cursor.getString(1);
|
||||
|
||||
if (htmlContent != null) {
|
||||
TextBody body = new TextBody(htmlContent);
|
||||
MimeBodyPart bp = new MimeBodyPart(body, "text/html");
|
||||
mp.addBodyPart(bp);
|
||||
}
|
||||
if (htmlContent != null) {
|
||||
TextBody body = new TextBody(htmlContent);
|
||||
MimeBodyPart bp = new MimeBodyPart(body, "text/html");
|
||||
mp.addBodyPart(bp);
|
||||
}
|
||||
|
||||
if (textContent != null) {
|
||||
TextBody body = new TextBody(textContent);
|
||||
MimeBodyPart bp = new MimeBodyPart(body, "text/plain");
|
||||
mp.addBodyPart(bp);
|
||||
if (textContent != null) {
|
||||
TextBody body = new TextBody(textContent);
|
||||
MimeBodyPart bp = new MimeBodyPart(body, "text/plain");
|
||||
mp.addBodyPart(bp);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
MimeBodyPart bp = new MimeBodyPart();
|
||||
bp.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
|
||||
"text/html;\n charset=\"UTF-8\"");
|
||||
mp.addBodyPart(bp);
|
||||
|
||||
bp = new MimeBodyPart();
|
||||
bp.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
|
||||
"text/plain;\n charset=\"UTF-8\"");
|
||||
mp.addBodyPart(bp);
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
|
@ -18,15 +18,21 @@ package com.android.email.mail.store;
|
|||
|
||||
import com.android.email.Email;
|
||||
import com.android.email.mail.Address;
|
||||
import com.android.email.mail.Body;
|
||||
import com.android.email.mail.FetchProfile;
|
||||
import com.android.email.mail.Flag;
|
||||
import com.android.email.mail.Folder;
|
||||
import com.android.email.mail.Message;
|
||||
import com.android.email.mail.MessageTestUtils;
|
||||
import com.android.email.mail.MessagingException;
|
||||
import com.android.email.mail.Part;
|
||||
import com.android.email.mail.Folder.FolderType;
|
||||
import com.android.email.mail.Folder.OpenMode;
|
||||
import com.android.email.mail.Message.RecipientType;
|
||||
import com.android.email.mail.MessageTestUtils.MultipartBuilder;
|
||||
import com.android.email.mail.internet.BinaryTempFileBody;
|
||||
import com.android.email.mail.internet.MimeMessage;
|
||||
import com.android.email.mail.internet.MimeUtility;
|
||||
import com.android.email.mail.internet.TextBody;
|
||||
|
||||
import android.content.ContentValues;
|
||||
|
@ -211,6 +217,53 @@ public class LocalStoreUnitTests extends AndroidTestCase {
|
|||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test two modes (STRUCTURE vs. BODY) of fetch()
|
||||
*/
|
||||
public void testFetchModes() throws MessagingException {
|
||||
final String BODY_TEXT_PLAIN = "This is the body text.";
|
||||
final String BODY_TEXT_HTML = "But this is the HTML version of the body text.";
|
||||
|
||||
MimeMessage message = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY);
|
||||
message.setMessageId(MESSAGE_ID);
|
||||
Body body = new MultipartBuilder("multipart/mixed")
|
||||
.addBodyPart(MessageTestUtils.bodyPart("image/tiff", "cid.4@android.com"))
|
||||
.addBodyPart(new MultipartBuilder("multipart/related")
|
||||
.addBodyPart(new MultipartBuilder("multipart/alternative")
|
||||
.addBodyPart(MessageTestUtils.textPart("text/plain", BODY_TEXT_PLAIN))
|
||||
.addBodyPart(MessageTestUtils.textPart("text/html", BODY_TEXT_HTML))
|
||||
.buildBodyPart())
|
||||
.buildBodyPart())
|
||||
.addBodyPart(MessageTestUtils.bodyPart("image/gif", "cid.3@android.com"))
|
||||
.build();
|
||||
message.setBody(body);
|
||||
|
||||
mFolder.open(OpenMode.READ_WRITE, null);
|
||||
mFolder.appendMessages(new Message[]{ message });
|
||||
|
||||
// Now read it back, and fetch it two ways - first, structure only
|
||||
Message[] messages = mFolder.getMessages(null);
|
||||
FetchProfile fp = new FetchProfile();
|
||||
fp.add(FetchProfile.Item.STRUCTURE);
|
||||
mFolder.fetch(messages, fp, null);
|
||||
// check for empty body parts
|
||||
Part textPart = MimeUtility.findFirstPartByMimeType(messages[0], "text/plain");
|
||||
Part htmlPart = MimeUtility.findFirstPartByMimeType(messages[0], "text/html");
|
||||
assertNull(MimeUtility.getTextFromPart(textPart));
|
||||
assertNull(MimeUtility.getTextFromPart(htmlPart));
|
||||
|
||||
// Next, complete body
|
||||
messages = mFolder.getMessages(null);
|
||||
fp.clear();
|
||||
fp.add(FetchProfile.Item.BODY);
|
||||
mFolder.fetch(messages, fp, null);
|
||||
// check for real body parts
|
||||
textPart = MimeUtility.findFirstPartByMimeType(messages[0], "text/plain");
|
||||
htmlPart = MimeUtility.findFirstPartByMimeType(messages[0], "text/html");
|
||||
assertEquals(BODY_TEXT_PLAIN, MimeUtility.getTextFromPart(textPart));
|
||||
assertEquals(BODY_TEXT_HTML, MimeUtility.getTextFromPart(htmlPart));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test functionality of setting & saving store persistence values
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue