LegacyConversions.java revision 75a873be8420e50f0aeb5a77716358ee0ca66b01
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().restore(cursor);
395                // We test each of the fields here (instead of in SQL) because they may be
396                // null, or may be strings.
397                if (stringNotEqual(dbAttachment.mFileName, localAttachment.mFileName)) continue;
398                if (stringNotEqual(dbAttachment.mMimeType, localAttachment.mMimeType)) continue;
399                if (stringNotEqual(dbAttachment.mContentId, localAttachment.mContentId)) continue;
400                if (stringNotEqual(dbAttachment.mLocation, localAttachment.mLocation)) continue;
401                // We found a match, so use the existing attachment id, and stop looking/looping
402                attachmentFoundInDb = true;
403                localAttachment.mId = dbAttachment.mId;
404                if (DEBUG_ATTACHMENTS) {
405                    Log.d(Email.LOG_TAG, "Skipped, found db attachment " + dbAttachment);
406                }
407                break;
408            }
409        } finally {
410            cursor.close();
411        }
412
413        // Save the attachment (so far) in order to obtain an id
414        if (!attachmentFoundInDb) {
415            localAttachment.save(context);
416        }
417
418        // If an attachment body was actually provided, we need to write the file now
419        if (!upgrading) {
420            saveAttachmentBody(context, part, localAttachment, localMessage.mAccountKey);
421        }
422
423        if (localMessage.mAttachments == null) {
424            localMessage.mAttachments = new ArrayList<Attachment>();
425        }
426        localMessage.mAttachments.add(localAttachment);
427        localMessage.mFlagAttachment = true;
428    }
429
430    /**
431     * Helper for addOneAttachment that compares two strings, deals with nulls, and treats
432     * nulls and empty strings as equal.
433     */
434    /* package */ static boolean stringNotEqual(String a, String b) {
435        if (a == null && b == null) return false;       // fast exit for two null strings
436        if (a == null) a = "";
437        if (b == null) b = "";
438        return !a.equals(b);
439    }
440
441    /**
442     * Save the body part of a single attachment, to a file in the attachments directory.
443     */
444    public static void saveAttachmentBody(Context context, Part part, Attachment localAttachment,
445            long accountId) throws MessagingException, IOException {
446        if (part.getBody() != null) {
447            long attachmentId = localAttachment.mId;
448
449            InputStream in = part.getBody().getInputStream();
450
451            File saveIn = AttachmentProvider.getAttachmentDirectory(context, accountId);
452            if (!saveIn.exists()) {
453                saveIn.mkdirs();
454            }
455            File saveAs = AttachmentProvider.getAttachmentFilename(context, accountId,
456                    attachmentId);
457            saveAs.createNewFile();
458            FileOutputStream out = new FileOutputStream(saveAs);
459            long copySize = IOUtils.copy(in, out);
460            in.close();
461            out.close();
462
463            // update the attachment with the extra information we now know
464            String contentUriString = AttachmentProvider.getAttachmentUri(
465                    accountId, attachmentId).toString();
466
467            localAttachment.mSize = copySize;
468            localAttachment.mContentUri = contentUriString;
469
470            // update the attachment in the database as well
471            ContentValues cv = new ContentValues();
472            cv.put(AttachmentColumns.SIZE, copySize);
473            cv.put(AttachmentColumns.CONTENT_URI, contentUriString);
474            Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachmentId);
475            context.getContentResolver().update(uri, cv, null, null);
476        }
477    }
478
479    /**
480     * Read a complete Provider message into a legacy message (for IMAP upload).  This
481     * is basically the equivalent of LocalFolder.getMessages() + LocalFolder.fetch().
482     */
483    public static Message makeMessage(Context context, EmailContent.Message localMessage)
484            throws MessagingException {
485        MimeMessage message = new MimeMessage();
486
487        // LocalFolder.getMessages() equivalent:  Copy message fields
488        message.setSubject(localMessage.mSubject == null ? "" : localMessage.mSubject);
489        Address[] from = Address.unpack(localMessage.mFrom);
490        if (from.length > 0) {
491            message.setFrom(from[0]);
492        }
493        message.setSentDate(new Date(localMessage.mTimeStamp));
494        message.setUid(localMessage.mServerId);
495        message.setFlag(Flag.DELETED,
496                localMessage.mFlagLoaded == EmailContent.Message.FLAG_LOADED_DELETED);
497        message.setFlag(Flag.SEEN, localMessage.mFlagRead);
498        message.setFlag(Flag.FLAGGED, localMessage.mFlagFavorite);
499//      message.setFlag(Flag.DRAFT, localMessage.mMailboxKey == draftMailboxKey);
500        message.setRecipients(RecipientType.TO, Address.unpack(localMessage.mTo));
501        message.setRecipients(RecipientType.CC, Address.unpack(localMessage.mCc));
502        message.setRecipients(RecipientType.BCC, Address.unpack(localMessage.mBcc));
503        message.setReplyTo(Address.unpack(localMessage.mReplyTo));
504        message.setInternalDate(new Date(localMessage.mServerTimeStamp));
505        message.setMessageId(localMessage.mMessageId);
506
507        // LocalFolder.fetch() equivalent: build body parts
508        message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "multipart/mixed");
509        MimeMultipart mp = new MimeMultipart();
510        mp.setSubType("mixed");
511        message.setBody(mp);
512
513        try {
514            addTextBodyPart(mp, "text/html", null,
515                    EmailContent.Body.restoreBodyHtmlWithMessageId(context, localMessage.mId));
516        } catch (RuntimeException rte) {
517            Log.d(Email.LOG_TAG, "Exception while reading html body " + rte.toString());
518        }
519
520        try {
521            addTextBodyPart(mp, "text/plain", null,
522                    EmailContent.Body.restoreBodyTextWithMessageId(context, localMessage.mId));
523        } catch (RuntimeException rte) {
524            Log.d(Email.LOG_TAG, "Exception while reading text body " + rte.toString());
525        }
526
527        boolean isReply = (localMessage.mFlags & EmailContent.Message.FLAG_TYPE_REPLY) != 0;
528        boolean isForward = (localMessage.mFlags & EmailContent.Message.FLAG_TYPE_FORWARD) != 0;
529
530        // If there is a quoted part (forwarding or reply), add the intro first, and then the
531        // rest of it.  If it is opened in some other viewer, it will (hopefully) be displayed in
532        // the same order as we've just set up the blocks:  composed text, intro, replied text
533        if (isReply || isForward) {
534            try {
535                addTextBodyPart(mp, "text/plain", BODY_QUOTED_PART_INTRO,
536                        EmailContent.Body.restoreIntroTextWithMessageId(context, localMessage.mId));
537            } catch (RuntimeException rte) {
538                Log.d(Email.LOG_TAG, "Exception while reading text reply " + rte.toString());
539            }
540
541            String replyTag = isReply ? BODY_QUOTED_PART_REPLY : BODY_QUOTED_PART_FORWARD;
542            try {
543                addTextBodyPart(mp, "text/html", replyTag,
544                        EmailContent.Body.restoreReplyHtmlWithMessageId(context, localMessage.mId));
545            } catch (RuntimeException rte) {
546                Log.d(Email.LOG_TAG, "Exception while reading html reply " + rte.toString());
547            }
548
549            try {
550                addTextBodyPart(mp, "text/plain", replyTag,
551                        EmailContent.Body.restoreReplyTextWithMessageId(context, localMessage.mId));
552            } catch (RuntimeException rte) {
553                Log.d(Email.LOG_TAG, "Exception while reading text reply " + rte.toString());
554            }
555        }
556
557        // Attachments
558        // TODO: Make sure we deal with these as structures and don't accidentally upload files
559//        Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId);
560//        Cursor attachments = context.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION,
561//                null, null, null);
562//        try {
563//
564//        } finally {
565//            attachments.close();
566//        }
567
568        return message;
569    }
570
571    /**
572     * Helper method to add a body part for a given type of text, if found
573     *
574     * @param mp The text body part will be added to this multipart
575     * @param contentType The content-type of the text being added
576     * @param quotedPartTag If non-null, HEADER_ANDROID_BODY_QUOTED_PART will be set to this value
577     * @param partText The text to add.  If null, nothing happens
578     */
579    private static void addTextBodyPart(MimeMultipart mp, String contentType, String quotedPartTag,
580            String partText) throws MessagingException {
581        if (partText == null) {
582            return;
583        }
584        TextBody body = new TextBody(partText);
585        MimeBodyPart bp = new MimeBodyPart(body, contentType);
586        if (quotedPartTag != null) {
587            bp.addHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART, quotedPartTag);
588        }
589        mp.addBodyPart(bp);
590    }
591
592    /**
593     * Conversion from provider account to legacy account
594     *
595     * Used for backup/restore.
596     *
597     * @param context application context
598     * @param fromAccount the provider account to be backed up (including transient hostauth's)
599     * @return a legacy Account object ready to be committed to preferences
600     */
601    /* package */ static Account makeLegacyAccount(Context context,
602            EmailContent.Account fromAccount) {
603        Account result = new Account(context);
604
605        result.setDescription(fromAccount.getDisplayName());
606        result.setEmail(fromAccount.getEmailAddress());
607        // fromAccount.mSyncKey - assume lost if restoring
608        result.setSyncWindow(fromAccount.getSyncLookback());
609        result.setAutomaticCheckIntervalMinutes(fromAccount.getSyncInterval());
610        // fromAccount.mHostAuthKeyRecv - id not saved; will be reassigned when restoring
611        // fromAccount.mHostAuthKeySend - id not saved; will be reassigned when restoring
612
613        // Provider Account flags, and how they are mapped.
614        //  FLAGS_NOTIFY_NEW_MAIL       -> mNotifyNewMail
615        //  FLAGS_VIBRATE_ALWAYS        -> mVibrate
616        //  FLAGS_VIBRATE_WHEN_SILENT   -> mVibrateWhenSilent
617        //  DELETE_POLICY_NEVER         -> mDeletePolicy
618        //  DELETE_POLICY_7DAYS
619        //  DELETE_POLICY_ON_DELETE
620        result.setNotifyNewMail(0 !=
621            (fromAccount.getFlags() & EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL));
622        result.setVibrate(0 !=
623            (fromAccount.getFlags() & EmailContent.Account.FLAGS_VIBRATE_ALWAYS));
624        result.setVibrateWhenSilent(0 !=
625            (fromAccount.getFlags() & EmailContent.Account.FLAGS_VIBRATE_WHEN_SILENT));
626        result.setDeletePolicy(fromAccount.getDeletePolicy());
627
628        result.mUuid = fromAccount.getUuid();
629        result.setName(fromAccount.mSenderName);
630        result.setRingtone(fromAccount.mRingtoneUri);
631        result.mProtocolVersion = fromAccount.mProtocolVersion;
632        // int fromAccount.mNewMessageCount = will be reset on next sync
633        result.mSecurityFlags = fromAccount.mSecurityFlags;
634        result.mSignature = fromAccount.mSignature;
635
636        // Use the existing conversions from HostAuth <-> Uri
637        result.setStoreUri(fromAccount.getStoreUri(context));
638        result.setSenderUri(fromAccount.getSenderUri(context));
639
640        return result;
641    }
642
643    /**
644     * Conversion from legacy account to provider account
645     *
646     * Used for backup/restore and for account migration.
647     *
648     * @param context application context
649     * @param fromAccount the legacy account to convert to modern format
650     * @return an Account ready to be committed to provider
651     */
652    public static EmailContent.Account makeAccount(Context context, Account fromAccount) {
653
654        EmailContent.Account result = new EmailContent.Account();
655
656        result.setDisplayName(fromAccount.getDescription());
657        result.setEmailAddress(fromAccount.getEmail());
658        result.mSyncKey = null;
659        result.setSyncLookback(fromAccount.getSyncWindow());
660        result.setSyncInterval(fromAccount.getAutomaticCheckIntervalMinutes());
661        // result.mHostAuthKeyRecv;     -- will be set when object is saved
662        // result.mHostAuthKeySend;     -- will be set when object is saved
663        int flags = 0;
664        if (fromAccount.isNotifyNewMail())  flags |= EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL;
665        if (fromAccount.isVibrate())        flags |= EmailContent.Account.FLAGS_VIBRATE_ALWAYS;
666        if (fromAccount.isVibrateWhenSilent())
667            flags |= EmailContent.Account.FLAGS_VIBRATE_WHEN_SILENT;
668        result.setFlags(flags);
669        result.setDeletePolicy(fromAccount.getDeletePolicy());
670        // result.setDefaultAccount();  -- will be set by caller, if neededf
671        result.mCompatibilityUuid = fromAccount.getUuid();
672        result.setSenderName(fromAccount.getName());
673        result.setRingtone(fromAccount.getRingtone());
674        result.mProtocolVersion = fromAccount.mProtocolVersion;
675        result.mNewMessageCount = 0;
676        result.mSecurityFlags = fromAccount.mSecurityFlags;
677        result.mSecuritySyncKey = null;
678        result.mSignature = fromAccount.mSignature;
679
680        result.setStoreUri(context, fromAccount.getStoreUri());
681        result.setSenderUri(context, fromAccount.getSenderUri());
682
683        return result;
684    }
685
686    /**
687     * Conversion from legacy folder to provider mailbox.  Used for account migration.
688     * Note: Many mailbox fields are unused in IMAP & POP accounts.
689     *
690     * @param context application context
691     * @param toAccount the provider account that this folder will be associated with
692     * @param fromFolder the legacy folder to convert to modern format
693     * @return an Account ready to be committed to provider
694     */
695    public static EmailContent.Mailbox makeMailbox(Context context, EmailContent.Account toAccount,
696            Folder fromFolder) throws MessagingException {
697        EmailContent.Mailbox result = new EmailContent.Mailbox();
698
699        result.mDisplayName = fromFolder.getName();
700        // result.mServerId
701        // result.mParentServerId
702        result.mAccountKey = toAccount.mId;
703        result.mType = inferMailboxTypeFromName(context, fromFolder.getName());
704        // result.mDelimiter
705        // result.mSyncKey
706        // result.mSyncLookback
707        // result.mSyncInterval
708        result.mSyncTime = 0;
709        result.mFlagVisible = true;
710        result.mFlags = 0;
711        result.mVisibleLimit = Email.VISIBLE_LIMIT_DEFAULT;
712        // result.mSyncStatus
713
714        return result;
715    }
716
717    /**
718     * Infer mailbox type from mailbox name.  Used by MessagingController (for live folder sync)
719     * and for legacy account upgrades.
720     */
721    public static synchronized int inferMailboxTypeFromName(Context context, String mailboxName) {
722        if (sServerMailboxNames.size() == 0) {
723            // preload the hashmap, one time only
724            sServerMailboxNames.put(
725                    context.getString(R.string.mailbox_name_server_inbox).toLowerCase(),
726                    Mailbox.TYPE_INBOX);
727            sServerMailboxNames.put(
728                    context.getString(R.string.mailbox_name_server_outbox).toLowerCase(),
729                    Mailbox.TYPE_OUTBOX);
730            sServerMailboxNames.put(
731                    context.getString(R.string.mailbox_name_server_drafts).toLowerCase(),
732                    Mailbox.TYPE_DRAFTS);
733            sServerMailboxNames.put(
734                    context.getString(R.string.mailbox_name_server_trash).toLowerCase(),
735                    Mailbox.TYPE_TRASH);
736            sServerMailboxNames.put(
737                    context.getString(R.string.mailbox_name_server_sent).toLowerCase(),
738                    Mailbox.TYPE_SENT);
739            sServerMailboxNames.put(
740                    context.getString(R.string.mailbox_name_server_junk).toLowerCase(),
741                    Mailbox.TYPE_JUNK);
742        }
743        if (mailboxName == null || mailboxName.length() == 0) {
744            return EmailContent.Mailbox.TYPE_MAIL;
745        }
746        String lowerCaseName = mailboxName.toLowerCase();
747        Integer type = sServerMailboxNames.get(lowerCaseName);
748        if (type != null) {
749            return type;
750        }
751        return EmailContent.Mailbox.TYPE_MAIL;
752    }
753}
754