LegacyConversions.java revision eb7752bf695b2a93854e0bb89ddbbc2236bb9aea
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email;
18
19import com.android.email.mail.Address;
20import com.android.email.mail.Flag;
21import com.android.email.mail.Message;
22import com.android.email.mail.MessagingException;
23import com.android.email.mail.Part;
24import com.android.email.mail.Message.RecipientType;
25import com.android.email.mail.internet.MimeBodyPart;
26import com.android.email.mail.internet.MimeHeader;
27import com.android.email.mail.internet.MimeMessage;
28import com.android.email.mail.internet.MimeMultipart;
29import com.android.email.mail.internet.MimeUtility;
30import com.android.email.mail.internet.TextBody;
31import com.android.email.provider.AttachmentProvider;
32import com.android.email.provider.EmailContent;
33import com.android.email.provider.EmailContent.Attachment;
34import com.android.email.provider.EmailContent.AttachmentColumns;
35
36import org.apache.commons.io.IOUtils;
37
38import android.content.ContentUris;
39import android.content.ContentValues;
40import android.content.Context;
41import android.net.Uri;
42import android.util.Log;
43
44import java.io.File;
45import java.io.FileOutputStream;
46import java.io.IOException;
47import java.io.InputStream;
48import java.util.ArrayList;
49import java.util.Date;
50
51public class LegacyConversions {
52
53    /**
54     * Values for HEADER_ANDROID_BODY_QUOTED_PART to tag body parts
55     */
56    /* package */ static final String BODY_QUOTED_PART_REPLY = "quoted-reply";
57    /* package */ static final String BODY_QUOTED_PART_FORWARD = "quoted-forward";
58    /* package */ static final String BODY_QUOTED_PART_INTRO = "quoted-intro";
59
60    /**
61     * Copy field-by-field from a "store" message to a "provider" message
62     * @param message The message we've just downloaded (must be a MimeMessage)
63     * @param localMessage The message we'd like to write into the DB
64     * @result true if dirty (changes were made)
65     */
66    public static boolean updateMessageFields(EmailContent.Message localMessage, Message message,
67                long accountId, long mailboxId) throws MessagingException {
68
69        Address[] from = message.getFrom();
70        Address[] to = message.getRecipients(Message.RecipientType.TO);
71        Address[] cc = message.getRecipients(Message.RecipientType.CC);
72        Address[] bcc = message.getRecipients(Message.RecipientType.BCC);
73        Address[] replyTo = message.getReplyTo();
74        String subject = message.getSubject();
75        Date sentDate = message.getSentDate();
76        Date internalDate = message.getInternalDate();
77
78        if (from != null && from.length > 0) {
79            localMessage.mDisplayName = from[0].toFriendly();
80        }
81        if (sentDate != null) {
82            localMessage.mTimeStamp = sentDate.getTime();
83        }
84        if (subject != null) {
85            localMessage.mSubject = subject;
86        }
87        localMessage.mFlagRead = message.isSet(Flag.SEEN);
88
89        // Keep the message in the "unloaded" state until it has (at least) a display name.
90        // This prevents early flickering of empty messages in POP download.
91        if (localMessage.mFlagLoaded != EmailContent.Message.FLAG_LOADED_COMPLETE) {
92            if (localMessage.mDisplayName == null || "".equals(localMessage.mDisplayName)) {
93                localMessage.mFlagLoaded = EmailContent.Message.FLAG_LOADED_UNLOADED;
94            } else {
95                localMessage.mFlagLoaded = EmailContent.Message.FLAG_LOADED_PARTIAL;
96            }
97        }
98        localMessage.mFlagFavorite = message.isSet(Flag.FLAGGED);
99//        public boolean mFlagAttachment = false;
100//        public int mFlags = 0;
101
102        localMessage.mServerId = message.getUid();
103        if (internalDate != null) {
104            localMessage.mServerTimeStamp = internalDate.getTime();
105        }
106//        public String mClientId;
107
108        // Absorb a MessagingException here in the case of messages that were delivered without
109        // a proper message-id.  This is seen in some ISP's but it is non-fatal -- (we'll just use
110        // the locally-generated message-id.)
111        try {
112            localMessage.mMessageId = ((MimeMessage)message).getMessageId();
113        } catch (MessagingException me)  {
114            if (Email.DEBUG) {
115                Log.d(Email.LOG_TAG, "Missing message-id for UID=" + localMessage.mServerId);
116            }
117        }
118
119//        public long mBodyKey;
120        localMessage.mMailboxKey = mailboxId;
121        localMessage.mAccountKey = accountId;
122
123        if (from != null && from.length > 0) {
124            localMessage.mFrom = Address.pack(from);
125        }
126
127        localMessage.mTo = Address.pack(to);
128        localMessage.mCc = Address.pack(cc);
129        localMessage.mBcc = Address.pack(bcc);
130        localMessage.mReplyTo = Address.pack(replyTo);
131
132//        public String mText;
133//        public String mHtml;
134//        public String mTextReply;
135//        public String mHtmlReply;
136
137//        // Can be used while building messages, but is NOT saved by the Provider
138//        transient public ArrayList<Attachment> mAttachments = null;
139
140        return true;
141    }
142
143    /**
144     * Copy body text (plain and/or HTML) from MimeMessage to provider Message
145     */
146    public static boolean updateBodyFields(EmailContent.Body body,
147            EmailContent.Message localMessage, ArrayList<Part> viewables)
148            throws MessagingException {
149
150        body.mMessageKey = localMessage.mId;
151
152        StringBuffer sbHtml = null;
153        StringBuffer sbText = null;
154        StringBuffer sbHtmlReply = null;
155        StringBuffer sbTextReply = null;
156        StringBuffer sbIntroText = null;
157
158        for (Part viewable : viewables) {
159            String text = MimeUtility.getTextFromPart(viewable);
160            String[] replyTags = viewable.getHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART);
161            String replyTag = null;
162            if (replyTags != null && replyTags.length > 0) {
163                replyTag = replyTags[0];
164            }
165            // Deploy text as marked by the various tags
166            boolean isHtml = "text/html".equalsIgnoreCase(viewable.getMimeType());
167
168            if (replyTag != null) {
169                boolean isQuotedReply = BODY_QUOTED_PART_REPLY.equalsIgnoreCase(replyTag);
170                boolean isQuotedForward = BODY_QUOTED_PART_FORWARD.equalsIgnoreCase(replyTag);
171                boolean isQuotedIntro = BODY_QUOTED_PART_INTRO.equalsIgnoreCase(replyTag);
172
173                if (isQuotedReply || isQuotedForward) {
174                    if (isHtml) {
175                        sbHtmlReply = appendTextPart(sbHtmlReply, text);
176                    } else {
177                        sbTextReply = appendTextPart(sbTextReply, text);
178                    }
179                    // Set message flags as well
180                    localMessage.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK;
181                    localMessage.mFlags |= isQuotedReply
182                            ? EmailContent.Message.FLAG_TYPE_REPLY
183                            : EmailContent.Message.FLAG_TYPE_FORWARD;
184                    continue;
185                }
186                if (isQuotedIntro) {
187                    sbIntroText = appendTextPart(sbIntroText, text);
188                    continue;
189                }
190            }
191
192            // Most of the time, just process regular body parts
193            if (isHtml) {
194                sbHtml = appendTextPart(sbHtml, text);
195            } else {
196                sbText = appendTextPart(sbText, text);
197            }
198        }
199
200        // write the combined data to the body part
201        if (sbText != null && sbText.length() != 0) {
202            body.mTextContent = sbText.toString();
203        }
204        if (sbHtml != null && sbHtml.length() != 0) {
205            body.mHtmlContent = sbHtml.toString();
206        }
207        if (sbHtmlReply != null && sbHtmlReply.length() != 0) {
208            body.mHtmlReply = sbHtmlReply.toString();
209        }
210        if (sbTextReply != null && sbTextReply.length() != 0) {
211            body.mTextReply = sbTextReply.toString();
212        }
213        if (sbIntroText != null && sbIntroText.length() != 0) {
214            body.mIntroText = sbIntroText.toString();
215        }
216        return true;
217    }
218
219    /**
220     * Helper function to append text to a StringBuffer, creating it if necessary.
221     * Optimization:  The majority of the time we are *not* appending - we should have a path
222     * that deals with single strings.
223     */
224    private static StringBuffer appendTextPart(StringBuffer sb, String newText) {
225        if (sb == null) {
226            sb = new StringBuffer(newText);
227        } else {
228            if (sb.length() > 0) {
229                sb.append('\n');
230            }
231            sb.append(newText);
232        }
233        return sb;
234    }
235
236    /**
237     * Copy attachments from MimeMessage to provider Message.
238     *
239     * @param context a context for file operations
240     * @param localMessage the attachments will be built against this message
241     * @param attachments the attachments to add
242     * @throws IOException
243     */
244    public static void updateAttachments(Context context, EmailContent.Message localMessage,
245            ArrayList<Part> attachments) throws MessagingException, IOException {
246        localMessage.mAttachments = null;
247        for (Part attachmentPart : attachments) {
248            addOneAttachment(context, localMessage, attachmentPart);
249        }
250    }
251
252    /**
253     * Add a single attachment part to the message
254     *
255     * TODO: This will simply add to any existing attachments - could this ever happen?  If so,
256     * change it to find existing attachments and delete/merge them.
257     * TODO: Take a closer look at encoding and deal with it if necessary.
258     *
259     * @param context a context for file operations
260     * @param localMessage the attachments will be built against this message
261     * @param part a single attachment part from POP or IMAP
262     * @throws IOException
263     */
264    private static void addOneAttachment(Context context, EmailContent.Message localMessage,
265            Part part) throws MessagingException, IOException {
266
267        Attachment localAttachment = new Attachment();
268
269        // Transfer fields from mime format to provider format
270        String contentType = MimeUtility.unfoldAndDecode(part.getContentType());
271        String name = MimeUtility.getHeaderParameter(contentType, "name");
272        if (name == null) {
273            String contentDisposition = MimeUtility.unfoldAndDecode(part.getContentType());
274            name = MimeUtility.getHeaderParameter(contentDisposition, "filename");
275        }
276
277        // Try to pull size from disposition (if not downloaded)
278        long size = 0;
279        String disposition = part.getDisposition();
280        if (disposition != null) {
281            String s = MimeUtility.getHeaderParameter(disposition, "size");
282            if (s != null) {
283                size = Long.parseLong(s);
284            }
285        }
286
287        // Get partId for unloaded IMAP attachments (if any)
288        // This is only provided (and used) when we have structure but not the actual attachment
289        String[] partIds = part.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
290        String partId = partIds != null ? partIds[0] : null;
291
292        localAttachment.mFileName = MimeUtility.getHeaderParameter(contentType, "name");
293        localAttachment.mMimeType = part.getMimeType();
294        localAttachment.mSize = size;           // May be reset below if file handled
295        localAttachment.mContentId = part.getContentId();
296        localAttachment.mContentUri = null;     // Will be set when file is saved
297        localAttachment.mMessageKey = localMessage.mId;
298        localAttachment.mLocation = partId;
299        localAttachment.mEncoding = "B";        // TODO - convert other known encodings
300
301        // Save the attachment (so far) in order to obtain an id
302        localAttachment.save(context);
303
304        // If an attachment body was actually provided, we need to write the file now
305        saveAttachmentBody(context, part, localAttachment, localMessage.mAccountKey);
306
307        if (localMessage.mAttachments == null) {
308            localMessage.mAttachments = new ArrayList<Attachment>();
309        }
310        localMessage.mAttachments.add(localAttachment);
311        localMessage.mFlagAttachment = true;
312    }
313
314    /**
315     * Save the body part of a single attachment, to a file in the attachments directory.
316     */
317    public static void saveAttachmentBody(Context context, Part part, Attachment localAttachment,
318            long accountId) throws MessagingException, IOException {
319        if (part.getBody() != null) {
320            long attachmentId = localAttachment.mId;
321
322            InputStream in = part.getBody().getInputStream();
323
324            File saveIn = AttachmentProvider.getAttachmentDirectory(context, accountId);
325            if (!saveIn.exists()) {
326                saveIn.mkdirs();
327            }
328            File saveAs = AttachmentProvider.getAttachmentFilename(context, accountId,
329                    attachmentId);
330            saveAs.createNewFile();
331            FileOutputStream out = new FileOutputStream(saveAs);
332            long copySize = IOUtils.copy(in, out);
333            in.close();
334            out.close();
335
336            // update the attachment with the extra information we now know
337            String contentUriString = AttachmentProvider.getAttachmentUri(
338                    accountId, attachmentId).toString();
339
340            localAttachment.mSize = copySize;
341            localAttachment.mContentUri = contentUriString;
342
343            // update the attachment in the database as well
344            ContentValues cv = new ContentValues();
345            cv.put(AttachmentColumns.SIZE, copySize);
346            cv.put(AttachmentColumns.CONTENT_URI, contentUriString);
347            Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachmentId);
348            context.getContentResolver().update(uri, cv, null, null);
349        }
350    }
351
352    /**
353     * Read a complete Provider message into a legacy message (for IMAP upload).  This
354     * is basically the equivalent of LocalFolder.getMessages() + LocalFolder.fetch().
355     */
356    public static Message makeMessage(Context context, EmailContent.Message localMessage)
357            throws MessagingException {
358        MimeMessage message = new MimeMessage();
359
360        // LocalFolder.getMessages() equivalent:  Copy message fields
361        message.setSubject(localMessage.mSubject == null ? "" : localMessage.mSubject);
362        Address[] from = Address.unpack(localMessage.mFrom);
363        if (from.length > 0) {
364            message.setFrom(from[0]);
365        }
366        message.setSentDate(new Date(localMessage.mTimeStamp));
367        message.setUid(localMessage.mServerId);
368        message.setFlag(Flag.DELETED,
369                localMessage.mFlagLoaded == EmailContent.Message.FLAG_LOADED_DELETED);
370        message.setFlag(Flag.SEEN, localMessage.mFlagRead);
371        message.setFlag(Flag.FLAGGED, localMessage.mFlagFavorite);
372//      message.setFlag(Flag.DRAFT, localMessage.mMailboxKey == draftMailboxKey);
373        message.setRecipients(RecipientType.TO, Address.unpack(localMessage.mTo));
374        message.setRecipients(RecipientType.CC, Address.unpack(localMessage.mCc));
375        message.setRecipients(RecipientType.BCC, Address.unpack(localMessage.mBcc));
376        message.setReplyTo(Address.unpack(localMessage.mReplyTo));
377        message.setInternalDate(new Date(localMessage.mServerTimeStamp));
378        message.setMessageId(localMessage.mMessageId);
379
380        // LocalFolder.fetch() equivalent: build body parts
381        message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "multipart/mixed");
382        MimeMultipart mp = new MimeMultipart();
383        mp.setSubType("mixed");
384        message.setBody(mp);
385
386        try {
387            addTextBodyPart(mp, "text/html", null,
388                    EmailContent.Body.restoreBodyHtmlWithMessageId(context, localMessage.mId));
389        } catch (RuntimeException rte) {
390            Log.d(Email.LOG_TAG, "Exception while reading html body " + rte.toString());
391        }
392
393        try {
394            addTextBodyPart(mp, "text/plain", null,
395                    EmailContent.Body.restoreBodyTextWithMessageId(context, localMessage.mId));
396        } catch (RuntimeException rte) {
397            Log.d(Email.LOG_TAG, "Exception while reading text body " + rte.toString());
398        }
399
400        boolean isReply = (localMessage.mFlags & EmailContent.Message.FLAG_TYPE_REPLY) != 0;
401        boolean isForward = (localMessage.mFlags & EmailContent.Message.FLAG_TYPE_FORWARD) != 0;
402
403        // If there is a quoted part (forwarding or reply), add the intro first, and then the
404        // rest of it.  If it is opened in some other viewer, it will (hopefully) be displayed in
405        // the same order as we've just set up the blocks:  composed text, intro, replied text
406        if (isReply || isForward) {
407            try {
408                addTextBodyPart(mp, "text/plain", BODY_QUOTED_PART_INTRO,
409                        EmailContent.Body.restoreIntroTextWithMessageId(context, localMessage.mId));
410            } catch (RuntimeException rte) {
411                Log.d(Email.LOG_TAG, "Exception while reading text reply " + rte.toString());
412            }
413
414            String replyTag = isReply ? BODY_QUOTED_PART_REPLY : BODY_QUOTED_PART_FORWARD;
415            try {
416                addTextBodyPart(mp, "text/html", replyTag,
417                        EmailContent.Body.restoreReplyHtmlWithMessageId(context, localMessage.mId));
418            } catch (RuntimeException rte) {
419                Log.d(Email.LOG_TAG, "Exception while reading html reply " + rte.toString());
420            }
421
422            try {
423                addTextBodyPart(mp, "text/plain", replyTag,
424                        EmailContent.Body.restoreReplyTextWithMessageId(context, localMessage.mId));
425            } catch (RuntimeException rte) {
426                Log.d(Email.LOG_TAG, "Exception while reading text reply " + rte.toString());
427            }
428        }
429
430        // Attachments
431        // TODO: Make sure we deal with these as structures and don't accidentally upload files
432//        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
433//        Cursor attachments = context.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
434//                null, null, null);
435//        try {
436//
437//        } finally {
438//            attachments.close();
439//        }
440
441        return message;
442    }
443
444    /**
445     * Helper method to add a body part for a given type of text, if found
446     *
447     * @param mp The text body part will be added to this multipart
448     * @param contentType The content-type of the text being added
449     * @param quotedPartTag If non-null, HEADER_ANDROID_BODY_QUOTED_PART will be set to this value
450     * @param partText The text to add.  If null, nothing happens
451     */
452    private static void addTextBodyPart(MimeMultipart mp, String contentType, String quotedPartTag,
453            String partText) throws MessagingException {
454        if (partText == null) {
455            return;
456        }
457        TextBody body = new TextBody(partText);
458        MimeBodyPart bp = new MimeBodyPart(body, contentType);
459        if (quotedPartTag != null) {
460            bp.addHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART, quotedPartTag);
461        }
462        mp.addBodyPart(bp);
463    }
464}
465