LegacyConversions.java revision 5e39f90e9d4665713eba38586b08546f1d581adb
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.Body;
21import com.android.email.mail.Flag;
22import com.android.email.mail.Folder;
23import com.android.email.mail.Message;
24import com.android.email.mail.MessagingException;
25import com.android.email.mail.Part;
26import com.android.email.mail.Message.RecipientType;
27import com.android.email.mail.internet.MimeBodyPart;
28import com.android.email.mail.internet.MimeHeader;
29import com.android.email.mail.internet.MimeMessage;
30import com.android.email.mail.internet.MimeMultipart;
31import com.android.email.mail.internet.MimeUtility;
32import com.android.email.mail.internet.TextBody;
33import com.android.email.mail.store.LocalStore;
34import com.android.email.provider.AttachmentProvider;
35import com.android.email.provider.EmailContent;
36import com.android.email.provider.EmailContent.Attachment;
37import com.android.email.provider.EmailContent.AttachmentColumns;
38import com.android.email.provider.EmailContent.Mailbox;
39
40import org.apache.commons.io.IOUtils;
41
42import android.content.ContentUris;
43import android.content.ContentValues;
44import android.content.Context;
45import android.database.Cursor;
46import android.net.Uri;
47import android.provider.OpenableColumns;
48import android.text.TextUtils;
49import android.util.Log;
50
51import java.io.File;
52import java.io.FileOutputStream;
53import java.io.IOException;
54import java.io.InputStream;
55import java.util.ArrayList;
56import java.util.Date;
57import java.util.HashMap;
58
59public class LegacyConversions {
60
61    /** DO NOT CHECK IN "TRUE" */
62    private static final boolean DEBUG_ATTACHMENTS = false;
63
64    /** Used for mapping folder names to type codes (e.g. inbox, drafts, trash) */
65    private static final HashMap<String, Integer>
66            sServerMailboxNames = new HashMap<String, Integer>();
67
68    /**
69     * Values for HEADER_ANDROID_BODY_QUOTED_PART to tag body parts
70     */
71    /* package */ static final String BODY_QUOTED_PART_REPLY = "quoted-reply";
72    /* package */ static final String BODY_QUOTED_PART_FORWARD = "quoted-forward";
73    /* package */ static final String BODY_QUOTED_PART_INTRO = "quoted-intro";
74
75    /**
76     * Standard columns for querying content providers
77     */
78    private static final String[] ATTACHMENT_META_COLUMNS_PROJECTION = {
79        OpenableColumns.DISPLAY_NAME,
80        OpenableColumns.SIZE
81    };
82    private static final int ATTACHMENT_META_COLUMNS_SIZE = 1;
83
84    /**
85     * Copy field-by-field from a "store" message to a "provider" message
86     * @param message The message we've just downloaded (must be a MimeMessage)
87     * @param localMessage The message we'd like to write into the DB
88     * @result true if dirty (changes were made)
89     */
90    public static boolean updateMessageFields(EmailContent.Message localMessage, Message message,
91                long accountId, long mailboxId) throws MessagingException {
92
93        Address[] from = message.getFrom();
94        Address[] to = message.getRecipients(Message.RecipientType.TO);
95        Address[] cc = message.getRecipients(Message.RecipientType.CC);
96        Address[] bcc = message.getRecipients(Message.RecipientType.BCC);
97        Address[] replyTo = message.getReplyTo();
98        String subject = message.getSubject();
99        Date sentDate = message.getSentDate();
100        Date internalDate = message.getInternalDate();
101
102        if (from != null && from.length > 0) {
103            localMessage.mDisplayName = from[0].toFriendly();
104        }
105        if (sentDate != null) {
106            localMessage.mTimeStamp = sentDate.getTime();
107        }
108        if (subject != null) {
109            localMessage.mSubject = subject;
110        }
111        localMessage.mFlagRead = message.isSet(Flag.SEEN);
112
113        // Keep the message in the "unloaded" state until it has (at least) a display name.
114        // This prevents early flickering of empty messages in POP download.
115        if (localMessage.mFlagLoaded != EmailContent.Message.FLAG_LOADED_COMPLETE) {
116            if (localMessage.mDisplayName == null || "".equals(localMessage.mDisplayName)) {
117                localMessage.mFlagLoaded = EmailContent.Message.FLAG_LOADED_UNLOADED;
118            } else {
119                localMessage.mFlagLoaded = EmailContent.Message.FLAG_LOADED_PARTIAL;
120            }
121        }
122        localMessage.mFlagFavorite = message.isSet(Flag.FLAGGED);
123//        public boolean mFlagAttachment = false;
124//        public int mFlags = 0;
125
126        localMessage.mServerId = message.getUid();
127        if (internalDate != null) {
128            localMessage.mServerTimeStamp = internalDate.getTime();
129        }
130//        public String mClientId;
131
132        // Only replace the local message-id if a new one was found.  This is seen in some ISP's
133        // which may deliver messages w/o a message-id header.
134        String messageId = ((MimeMessage)message).getMessageId();
135        if (messageId != null) {
136            localMessage.mMessageId = messageId;
137        }
138
139//        public long mBodyKey;
140        localMessage.mMailboxKey = mailboxId;
141        localMessage.mAccountKey = accountId;
142
143        if (from != null && from.length > 0) {
144            localMessage.mFrom = Address.pack(from);
145        }
146
147        localMessage.mTo = Address.pack(to);
148        localMessage.mCc = Address.pack(cc);
149        localMessage.mBcc = Address.pack(bcc);
150        localMessage.mReplyTo = Address.pack(replyTo);
151
152//        public String mText;
153//        public String mHtml;
154//        public String mTextReply;
155//        public String mHtmlReply;
156
157//        // Can be used while building messages, but is NOT saved by the Provider
158//        transient public ArrayList<Attachment> mAttachments = null;
159
160        return true;
161    }
162
163    /**
164     * Copy body text (plain and/or HTML) from MimeMessage to provider Message
165     */
166    public static boolean updateBodyFields(EmailContent.Body body,
167            EmailContent.Message localMessage, ArrayList<Part> viewables)
168            throws MessagingException {
169
170        body.mMessageKey = localMessage.mId;
171
172        StringBuffer sbHtml = null;
173        StringBuffer sbText = null;
174        StringBuffer sbHtmlReply = null;
175        StringBuffer sbTextReply = null;
176        StringBuffer sbIntroText = null;
177
178        for (Part viewable : viewables) {
179            String text = MimeUtility.getTextFromPart(viewable);
180            String[] replyTags = viewable.getHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART);
181            String replyTag = null;
182            if (replyTags != null && replyTags.length > 0) {
183                replyTag = replyTags[0];
184            }
185            // Deploy text as marked by the various tags
186            boolean isHtml = "text/html".equalsIgnoreCase(viewable.getMimeType());
187
188            if (replyTag != null) {
189                boolean isQuotedReply = BODY_QUOTED_PART_REPLY.equalsIgnoreCase(replyTag);
190                boolean isQuotedForward = BODY_QUOTED_PART_FORWARD.equalsIgnoreCase(replyTag);
191                boolean isQuotedIntro = BODY_QUOTED_PART_INTRO.equalsIgnoreCase(replyTag);
192
193                if (isQuotedReply || isQuotedForward) {
194                    if (isHtml) {
195                        sbHtmlReply = appendTextPart(sbHtmlReply, text);
196                    } else {
197                        sbTextReply = appendTextPart(sbTextReply, text);
198                    }
199                    // Set message flags as well
200                    localMessage.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK;
201                    localMessage.mFlags |= isQuotedReply
202                            ? EmailContent.Message.FLAG_TYPE_REPLY
203                            : EmailContent.Message.FLAG_TYPE_FORWARD;
204                    continue;
205                }
206                if (isQuotedIntro) {
207                    sbIntroText = appendTextPart(sbIntroText, text);
208                    continue;
209                }
210            }
211
212            // Most of the time, just process regular body parts
213            if (isHtml) {
214                sbHtml = appendTextPart(sbHtml, text);
215            } else {
216                sbText = appendTextPart(sbText, text);
217            }
218        }
219
220        // write the combined data to the body part
221        if (!TextUtils.isEmpty(sbText)) {
222            String text = sbText.toString();
223            body.mTextContent = text;
224            localMessage.mSnippet = Snippet.fromPlainText(text);
225        }
226        if (!TextUtils.isEmpty(sbHtml)) {
227            String text = sbHtml.toString();
228            body.mHtmlContent = text;
229            if (localMessage.mSnippet == null) {
230                localMessage.mSnippet = Snippet.fromHtmlText(text);
231            }
232        }
233        if (sbHtmlReply != null && sbHtmlReply.length() != 0) {
234            body.mHtmlReply = sbHtmlReply.toString();
235        }
236        if (sbTextReply != null && sbTextReply.length() != 0) {
237            body.mTextReply = sbTextReply.toString();
238        }
239        if (sbIntroText != null && sbIntroText.length() != 0) {
240            body.mIntroText = sbIntroText.toString();
241        }
242        return true;
243    }
244
245    /**
246     * Helper function to append text to a StringBuffer, creating it if necessary.
247     * Optimization:  The majority of the time we are *not* appending - we should have a path
248     * that deals with single strings.
249     */
250    private static StringBuffer appendTextPart(StringBuffer sb, String newText) {
251        if (newText == null) {
252            return sb;
253        }
254        else if (sb == null) {
255            sb = new StringBuffer(newText);
256        } else {
257            if (sb.length() > 0) {
258                sb.append('\n');
259            }
260            sb.append(newText);
261        }
262        return sb;
263    }
264
265    /**
266     * Copy attachments from MimeMessage to provider Message.
267     *
268     * @param context a context for file operations
269     * @param localMessage the attachments will be built against this message
270     * @param attachments the attachments to add
271     * @param upgrading if true, we are upgrading a local account - handle attachments differently
272     * @throws IOException
273     */
274    public static void updateAttachments(Context context, EmailContent.Message localMessage,
275            ArrayList<Part> attachments, boolean upgrading) throws MessagingException, IOException {
276        localMessage.mAttachments = null;
277        for (Part attachmentPart : attachments) {
278            addOneAttachment(context, localMessage, attachmentPart, upgrading);
279        }
280    }
281
282    /**
283     * Add a single attachment part to the message
284     *
285     * This will skip adding attachments if they are already found in the attachments table.
286     * The heuristic for this will fail (false-positive) if two identical attachments are
287     * included in a single POP3 message.
288     * TODO: Fix that, by (elsewhere) simulating an mLocation value based on the attachments
289     * position within the list of multipart/mixed elements.  This would make every POP3 attachment
290     * unique, and might also simplify the code (since we could just look at the positions, and
291     * ignore the filename, etc.)
292     *
293     * TODO: Take a closer look at encoding and deal with it if necessary.
294     *
295     * @param context a context for file operations
296     * @param localMessage the attachments will be built against this message
297     * @param part a single attachment part from POP or IMAP
298     * @param upgrading true if upgrading a local account - handle attachments differently
299     * @throws IOException
300     */
301    private static void addOneAttachment(Context context, EmailContent.Message localMessage,
302            Part part, boolean upgrading) throws MessagingException, IOException {
303
304        Attachment localAttachment = new Attachment();
305
306        // Transfer fields from mime format to provider format
307        String contentType = MimeUtility.unfoldAndDecode(part.getContentType());
308        String name = MimeUtility.getHeaderParameter(contentType, "name");
309        if (name == null) {
310            String contentDisposition = MimeUtility.unfoldAndDecode(part.getDisposition());
311            name = MimeUtility.getHeaderParameter(contentDisposition, "filename");
312        }
313
314        // Select the URI for the new attachments.  For attachments downloaded by the legacy
315        // IMAP/POP code, this is not determined yet, so is null (it will be rewritten below,
316        // or later, when the actual attachment file is created.)
317        //
318        // When upgrading older local accounts, the URI represents a local asset (e.g. a photo)
319        // so we need to preserve the URI.
320        // TODO This works for outgoing messages, where the URI does not change.  May need
321        // additional logic to handle the case of rewriting URI for received attachments.
322        Uri contentUri = null;
323        String contentUriString = null;
324        if (upgrading) {
325            Body body = part.getBody();
326            if (body instanceof LocalStore.LocalAttachmentBody) {
327                LocalStore.LocalAttachmentBody localBody = (LocalStore.LocalAttachmentBody) body;
328                contentUri = localBody.getContentUri();
329                if (contentUri != null) {
330                    contentUriString = contentUri.toString();
331                }
332            }
333        }
334
335        // Find size, if available, via a number of techniques:
336        long size = 0;
337        if (upgrading) {
338            // If upgrading a legacy account, the size must be recaptured from the data source
339            if (contentUri != null) {
340                Cursor metadataCursor = context.getContentResolver().query(contentUri,
341                        ATTACHMENT_META_COLUMNS_PROJECTION, null, null, null);
342                if (metadataCursor != null) {
343                    try {
344                        if (metadataCursor.moveToFirst()) {
345                            size = metadataCursor.getInt(ATTACHMENT_META_COLUMNS_SIZE);
346                        }
347                    } finally {
348                        metadataCursor.close();
349                    }
350                }
351            }
352            // TODO: a downloaded legacy attachment - see if the above code works
353        } else {
354            // Incoming attachment: Try to pull size from disposition (if not downloaded yet)
355            String disposition = part.getDisposition();
356            if (disposition != null) {
357                String s = MimeUtility.getHeaderParameter(disposition, "size");
358                if (s != null) {
359                    size = Long.parseLong(s);
360                }
361            }
362        }
363
364        // Get partId for unloaded IMAP attachments (if any)
365        // This is only provided (and used) when we have structure but not the actual attachment
366        String[] partIds = part.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
367        String partId = partIds != null ? partIds[0] : null;
368
369        localAttachment.mFileName = name;
370        localAttachment.mMimeType = part.getMimeType();
371        localAttachment.mSize = size;           // May be reset below if file handled
372        localAttachment.mContentId = part.getContentId();
373        localAttachment.mContentUri = contentUriString;
374        localAttachment.mMessageKey = localMessage.mId;
375        localAttachment.mLocation = partId;
376        localAttachment.mEncoding = "B";        // TODO - convert other known encodings
377        localAttachment.mAccountKey = localMessage.mAccountKey;
378
379        if (DEBUG_ATTACHMENTS) {
380            Log.d(Email.LOG_TAG, "Add attachment " + localAttachment);
381        }
382
383        // To prevent duplication - do we already have a matching attachment?
384        // The fields we'll check for equality are:
385        //  mFileName, mMimeType, mContentId, mMessageKey, mLocation
386        // NOTE:  This will false-positive if you attach the exact same file, twice, to a POP3
387        // message.  We can live with that - you'll get one of the copies.
388        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
389        Cursor cursor = context.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
390                null, null, null);
391        boolean attachmentFoundInDb = false;
392        try {
393            while (cursor.moveToNext()) {
394                Attachment dbAttachment = new Attachment();
395                dbAttachment.restore(cursor);
396                // We test each of the fields here (instead of in SQL) because they may be
397                // null, or may be strings.
398                if (stringNotEqual(dbAttachment.mFileName, localAttachment.mFileName)) continue;
399                if (stringNotEqual(dbAttachment.mMimeType, localAttachment.mMimeType)) continue;
400                if (stringNotEqual(dbAttachment.mContentId, localAttachment.mContentId)) continue;
401                if (stringNotEqual(dbAttachment.mLocation, localAttachment.mLocation)) continue;
402                // We found a match, so use the existing attachment id, and stop looking/looping
403                attachmentFoundInDb = true;
404                localAttachment.mId = dbAttachment.mId;
405                if (DEBUG_ATTACHMENTS) {
406                    Log.d(Email.LOG_TAG, "Skipped, found db attachment " + dbAttachment);
407                }
408                break;
409            }
410        } finally {
411            cursor.close();
412        }
413
414        // Save the attachment (so far) in order to obtain an id
415        if (!attachmentFoundInDb) {
416            localAttachment.save(context);
417        }
418
419        // If an attachment body was actually provided, we need to write the file now
420        if (!upgrading) {
421            saveAttachmentBody(context, part, localAttachment, localMessage.mAccountKey);
422        }
423
424        if (localMessage.mAttachments == null) {
425            localMessage.mAttachments = new ArrayList<Attachment>();
426        }
427        localMessage.mAttachments.add(localAttachment);
428        localMessage.mFlagAttachment = true;
429    }
430
431    /**
432     * Helper for addOneAttachment that compares two strings, deals with nulls, and treats
433     * nulls and empty strings as equal.
434     */
435    /* package */ static boolean stringNotEqual(String a, String b) {
436        if (a == null && b == null) return false;       // fast exit for two null strings
437        if (a == null) a = "";
438        if (b == null) b = "";
439        return !a.equals(b);
440    }
441
442    /**
443     * Save the body part of a single attachment, to a file in the attachments directory.
444     */
445    public static void saveAttachmentBody(Context context, Part part, Attachment localAttachment,
446            long accountId) throws MessagingException, IOException {
447        if (part.getBody() != null) {
448            long attachmentId = localAttachment.mId;
449
450            InputStream in = part.getBody().getInputStream();
451
452            File saveIn = AttachmentProvider.getAttachmentDirectory(context, accountId);
453            if (!saveIn.exists()) {
454                saveIn.mkdirs();
455            }
456            File saveAs = AttachmentProvider.getAttachmentFilename(context, accountId,
457                    attachmentId);
458            saveAs.createNewFile();
459            FileOutputStream out = new FileOutputStream(saveAs);
460            long copySize = IOUtils.copy(in, out);
461            in.close();
462            out.close();
463
464            // update the attachment with the extra information we now know
465            String contentUriString = AttachmentProvider.getAttachmentUri(
466                    accountId, attachmentId).toString();
467
468            localAttachment.mSize = copySize;
469            localAttachment.mContentUri = contentUriString;
470
471            // update the attachment in the database as well
472            ContentValues cv = new ContentValues();
473            cv.put(AttachmentColumns.SIZE, copySize);
474            cv.put(AttachmentColumns.CONTENT_URI, contentUriString);
475            Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachmentId);
476            context.getContentResolver().update(uri, cv, null, null);
477        }
478    }
479
480    /**
481     * Read a complete Provider message into a legacy message (for IMAP upload).  This
482     * is basically the equivalent of LocalFolder.getMessages() + LocalFolder.fetch().
483     */
484    public static Message makeMessage(Context context, EmailContent.Message localMessage)
485            throws MessagingException {
486        MimeMessage message = new MimeMessage();
487
488        // LocalFolder.getMessages() equivalent:  Copy message fields
489        message.setSubject(localMessage.mSubject == null ? "" : localMessage.mSubject);
490        Address[] from = Address.unpack(localMessage.mFrom);
491        if (from.length > 0) {
492            message.setFrom(from[0]);
493        }
494        message.setSentDate(new Date(localMessage.mTimeStamp));
495        message.setUid(localMessage.mServerId);
496        message.setFlag(Flag.DELETED,
497                localMessage.mFlagLoaded == EmailContent.Message.FLAG_LOADED_DELETED);
498        message.setFlag(Flag.SEEN, localMessage.mFlagRead);
499        message.setFlag(Flag.FLAGGED, localMessage.mFlagFavorite);
500//      message.setFlag(Flag.DRAFT, localMessage.mMailboxKey == draftMailboxKey);
501        message.setRecipients(RecipientType.TO, Address.unpack(localMessage.mTo));
502        message.setRecipients(RecipientType.CC, Address.unpack(localMessage.mCc));
503        message.setRecipients(RecipientType.BCC, Address.unpack(localMessage.mBcc));
504        message.setReplyTo(Address.unpack(localMessage.mReplyTo));
505        message.setInternalDate(new Date(localMessage.mServerTimeStamp));
506        message.setMessageId(localMessage.mMessageId);
507
508        // LocalFolder.fetch() equivalent: build body parts
509        message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "multipart/mixed");
510        MimeMultipart mp = new MimeMultipart();
511        mp.setSubType("mixed");
512        message.setBody(mp);
513
514        try {
515            addTextBodyPart(mp, "text/html", null,
516                    EmailContent.Body.restoreBodyHtmlWithMessageId(context, localMessage.mId));
517        } catch (RuntimeException rte) {
518            Log.d(Email.LOG_TAG, "Exception while reading html body " + rte.toString());
519        }
520
521        try {
522            addTextBodyPart(mp, "text/plain", null,
523                    EmailContent.Body.restoreBodyTextWithMessageId(context, localMessage.mId));
524        } catch (RuntimeException rte) {
525            Log.d(Email.LOG_TAG, "Exception while reading text body " + rte.toString());
526        }
527
528        boolean isReply = (localMessage.mFlags & EmailContent.Message.FLAG_TYPE_REPLY) != 0;
529        boolean isForward = (localMessage.mFlags & EmailContent.Message.FLAG_TYPE_FORWARD) != 0;
530
531        // If there is a quoted part (forwarding or reply), add the intro first, and then the
532        // rest of it.  If it is opened in some other viewer, it will (hopefully) be displayed in
533        // the same order as we've just set up the blocks:  composed text, intro, replied text
534        if (isReply || isForward) {
535            try {
536                addTextBodyPart(mp, "text/plain", BODY_QUOTED_PART_INTRO,
537                        EmailContent.Body.restoreIntroTextWithMessageId(context, localMessage.mId));
538            } catch (RuntimeException rte) {
539                Log.d(Email.LOG_TAG, "Exception while reading text reply " + rte.toString());
540            }
541
542            String replyTag = isReply ? BODY_QUOTED_PART_REPLY : BODY_QUOTED_PART_FORWARD;
543            try {
544                addTextBodyPart(mp, "text/html", replyTag,
545                        EmailContent.Body.restoreReplyHtmlWithMessageId(context, localMessage.mId));
546            } catch (RuntimeException rte) {
547                Log.d(Email.LOG_TAG, "Exception while reading html reply " + rte.toString());
548            }
549
550            try {
551                addTextBodyPart(mp, "text/plain", replyTag,
552                        EmailContent.Body.restoreReplyTextWithMessageId(context, localMessage.mId));
553            } catch (RuntimeException rte) {
554                Log.d(Email.LOG_TAG, "Exception while reading text reply " + rte.toString());
555            }
556        }
557
558        // Attachments
559        // TODO: Make sure we deal with these as structures and don't accidentally upload files
560//        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
561//        Cursor attachments = context.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
562//                null, null, null);
563//        try {
564//
565//        } finally {
566//            attachments.close();
567//        }
568
569        return message;
570    }
571
572    /**
573     * Helper method to add a body part for a given type of text, if found
574     *
575     * @param mp The text body part will be added to this multipart
576     * @param contentType The content-type of the text being added
577     * @param quotedPartTag If non-null, HEADER_ANDROID_BODY_QUOTED_PART will be set to this value
578     * @param partText The text to add.  If null, nothing happens
579     */
580    private static void addTextBodyPart(MimeMultipart mp, String contentType, String quotedPartTag,
581            String partText) throws MessagingException {
582        if (partText == null) {
583            return;
584        }
585        TextBody body = new TextBody(partText);
586        MimeBodyPart bp = new MimeBodyPart(body, contentType);
587        if (quotedPartTag != null) {
588            bp.addHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART, quotedPartTag);
589        }
590        mp.addBodyPart(bp);
591    }
592
593    /**
594     * Conversion from provider account to legacy account
595     *
596     * Used for backup/restore.
597     *
598     * @param context application context
599     * @param fromAccount the provider account to be backed up (including transient hostauth's)
600     * @return a legacy Account object ready to be committed to preferences
601     */
602    /* package */ static Account makeLegacyAccount(Context context,
603            EmailContent.Account fromAccount) {
604        Account result = new Account(context);
605
606        result.setDescription(fromAccount.getDisplayName());
607        result.setEmail(fromAccount.getEmailAddress());
608        // fromAccount.mSyncKey - assume lost if restoring
609        result.setSyncWindow(fromAccount.getSyncLookback());
610        result.setAutomaticCheckIntervalMinutes(fromAccount.getSyncInterval());
611        // fromAccount.mHostAuthKeyRecv - id not saved; will be reassigned when restoring
612        // fromAccount.mHostAuthKeySend - id not saved; will be reassigned when restoring
613
614        // Provider Account flags, and how they are mapped.
615        //  FLAGS_NOTIFY_NEW_MAIL       -> mNotifyNewMail
616        //  FLAGS_VIBRATE_ALWAYS        -> mVibrate
617        //  FLAGS_VIBRATE_WHEN_SILENT   -> mVibrateWhenSilent
618        //  DELETE_POLICY_NEVER         -> mDeletePolicy
619        //  DELETE_POLICY_7DAYS
620        //  DELETE_POLICY_ON_DELETE
621        result.setNotifyNewMail(0 !=
622            (fromAccount.getFlags() & EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL));
623        result.setVibrate(0 !=
624            (fromAccount.getFlags() & EmailContent.Account.FLAGS_VIBRATE_ALWAYS));
625        result.setVibrateWhenSilent(0 !=
626            (fromAccount.getFlags() & EmailContent.Account.FLAGS_VIBRATE_WHEN_SILENT));
627        result.setDeletePolicy(fromAccount.getDeletePolicy());
628
629        result.mUuid = fromAccount.getUuid();
630        result.setName(fromAccount.mSenderName);
631        result.setRingtone(fromAccount.mRingtoneUri);
632        result.mProtocolVersion = fromAccount.mProtocolVersion;
633        // int fromAccount.mNewMessageCount = will be reset on next sync
634        result.mSecurityFlags = fromAccount.mSecurityFlags;
635        result.mSignature = fromAccount.mSignature;
636
637        // Use the existing conversions from HostAuth <-> Uri
638        result.setStoreUri(fromAccount.getStoreUri(context));
639        result.setSenderUri(fromAccount.getSenderUri(context));
640
641        return result;
642    }
643
644    /**
645     * Conversion from legacy account to provider account
646     *
647     * Used for backup/restore and for account migration.
648     *
649     * @param context application context
650     * @param fromAccount the legacy account to convert to modern format
651     * @return an Account ready to be committed to provider
652     */
653    public static EmailContent.Account makeAccount(Context context, Account fromAccount) {
654
655        EmailContent.Account result = new EmailContent.Account();
656
657        result.setDisplayName(fromAccount.getDescription());
658        result.setEmailAddress(fromAccount.getEmail());
659        result.mSyncKey = null;
660        result.setSyncLookback(fromAccount.getSyncWindow());
661        result.setSyncInterval(fromAccount.getAutomaticCheckIntervalMinutes());
662        // result.mHostAuthKeyRecv;     -- will be set when object is saved
663        // result.mHostAuthKeySend;     -- will be set when object is saved
664        int flags = 0;
665        if (fromAccount.isNotifyNewMail())  flags |= EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL;
666        if (fromAccount.isVibrate())        flags |= EmailContent.Account.FLAGS_VIBRATE_ALWAYS;
667        if (fromAccount.isVibrateWhenSilent())
668            flags |= EmailContent.Account.FLAGS_VIBRATE_WHEN_SILENT;
669        result.setFlags(flags);
670        result.setDeletePolicy(fromAccount.getDeletePolicy());
671        // result.setDefaultAccount();  -- will be set by caller, if needed
672        result.mCompatibilityUuid = fromAccount.getUuid();
673        result.setSenderName(fromAccount.getName());
674        result.setRingtone(fromAccount.getRingtone());
675        result.mProtocolVersion = fromAccount.mProtocolVersion;
676        result.mNewMessageCount = 0;
677        result.mSecurityFlags = fromAccount.mSecurityFlags;
678        result.mSecuritySyncKey = null;
679        result.mSignature = fromAccount.mSignature;
680
681        result.setStoreUri(context, fromAccount.getStoreUri());
682        result.setSenderUri(context, fromAccount.getSenderUri());
683
684        return result;
685    }
686
687    /**
688     * Conversion from legacy folder to provider mailbox.  Used for account migration.
689     * Note: Many mailbox fields are unused in IMAP & POP accounts.
690     *
691     * @param context application context
692     * @param toAccount the provider account that this folder will be associated with
693     * @param fromFolder the legacy folder to convert to modern format
694     * @return an Account ready to be committed to provider
695     */
696    public static EmailContent.Mailbox makeMailbox(Context context, EmailContent.Account toAccount,
697            Folder fromFolder) throws MessagingException {
698        EmailContent.Mailbox result = new EmailContent.Mailbox();
699
700        result.mDisplayName = fromFolder.getName();
701        // result.mServerId
702        // result.mParentServerId
703        result.mAccountKey = toAccount.mId;
704        result.mType = inferMailboxTypeFromName(context, fromFolder.getName());
705        // result.mDelimiter
706        // result.mSyncKey
707        // result.mSyncLookback
708        // result.mSyncInterval
709        result.mSyncTime = 0;
710        result.mFlagVisible = true;
711        result.mFlags = 0;
712        result.mVisibleLimit = Email.VISIBLE_LIMIT_DEFAULT;
713        // result.mSyncStatus
714
715        return result;
716    }
717
718    /**
719     * Infer mailbox type from mailbox name.  Used by MessagingController (for live folder sync)
720     * and for legacy account upgrades.
721     */
722    public static synchronized int inferMailboxTypeFromName(Context context, String mailboxName) {
723        if (sServerMailboxNames.size() == 0) {
724            // preload the hashmap, one time only
725            sServerMailboxNames.put(
726                    context.getString(R.string.mailbox_name_server_inbox).toLowerCase(),
727                    Mailbox.TYPE_INBOX);
728            sServerMailboxNames.put(
729                    context.getString(R.string.mailbox_name_server_outbox).toLowerCase(),
730                    Mailbox.TYPE_OUTBOX);
731            sServerMailboxNames.put(
732                    context.getString(R.string.mailbox_name_server_drafts).toLowerCase(),
733                    Mailbox.TYPE_DRAFTS);
734            sServerMailboxNames.put(
735                    context.getString(R.string.mailbox_name_server_trash).toLowerCase(),
736                    Mailbox.TYPE_TRASH);
737            sServerMailboxNames.put(
738                    context.getString(R.string.mailbox_name_server_sent).toLowerCase(),
739                    Mailbox.TYPE_SENT);
740            sServerMailboxNames.put(
741                    context.getString(R.string.mailbox_name_server_junk).toLowerCase(),
742                    Mailbox.TYPE_JUNK);
743        }
744        if (mailboxName == null || mailboxName.length() == 0) {
745            return EmailContent.Mailbox.TYPE_MAIL;
746        }
747        String lowerCaseName = mailboxName.toLowerCase();
748        Integer type = sServerMailboxNames.get(lowerCaseName);
749        if (type != null) {
750            return type;
751        }
752        return EmailContent.Mailbox.TYPE_MAIL;
753    }
754}
755