MessagingController.java revision 3a58509b2ad7b6f946eb0a4a7b0f75ece6d6a46f
1/*
2 * Copyright (C) 2008 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.Sender;
20import com.android.email.mail.Store;
21import com.android.email.mail.StoreSynchronizer;
22import com.android.emailcommon.Logging;
23import com.android.emailcommon.internet.MimeBodyPart;
24import com.android.emailcommon.internet.MimeHeader;
25import com.android.emailcommon.internet.MimeMultipart;
26import com.android.emailcommon.internet.MimeUtility;
27import com.android.emailcommon.mail.AuthenticationFailedException;
28import com.android.emailcommon.mail.FetchProfile;
29import com.android.emailcommon.mail.Flag;
30import com.android.emailcommon.mail.Folder;
31import com.android.emailcommon.mail.Message;
32import com.android.emailcommon.mail.MessagingException;
33import com.android.emailcommon.mail.Part;
34import com.android.emailcommon.mail.Folder.FolderType;
35import com.android.emailcommon.mail.Folder.MessageRetrievalListener;
36import com.android.emailcommon.mail.Folder.OpenMode;
37import com.android.emailcommon.provider.EmailContent;
38import com.android.emailcommon.provider.EmailContent.Attachment;
39import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
40import com.android.emailcommon.provider.EmailContent.Mailbox;
41import com.android.emailcommon.provider.EmailContent.MailboxColumns;
42import com.android.emailcommon.provider.EmailContent.MessageColumns;
43import com.android.emailcommon.provider.EmailContent.SyncColumns;
44import com.android.emailcommon.utility.AttachmentUtilities;
45import com.android.emailcommon.utility.ConversionUtilities;
46import com.android.emailcommon.utility.Utility;
47
48import android.content.ContentResolver;
49import android.content.ContentUris;
50import android.content.ContentValues;
51import android.content.Context;
52import android.database.Cursor;
53import android.net.Uri;
54import android.os.Process;
55import android.util.Log;
56
57import java.io.IOException;
58import java.util.ArrayList;
59import java.util.Date;
60import java.util.HashMap;
61import java.util.HashSet;
62import java.util.concurrent.BlockingQueue;
63import java.util.concurrent.LinkedBlockingQueue;
64
65/**
66 * Starts a long running (application) Thread that will run through commands
67 * that require remote mailbox access. This class is used to serialize and
68 * prioritize these commands. Each method that will submit a command requires a
69 * MessagingListener instance to be provided. It is expected that that listener
70 * has also been added as a registered listener using addListener(). When a
71 * command is to be executed, if the listener that was provided with the command
72 * is no longer registered the command is skipped. The design idea for the above
73 * is that when an Activity starts it registers as a listener. When it is paused
74 * it removes itself. Thus, any commands that that activity submitted are
75 * removed from the queue once the activity is no longer active.
76 */
77public class MessagingController implements Runnable {
78
79    /**
80     * The maximum message size that we'll consider to be "small". A small message is downloaded
81     * in full immediately instead of in pieces. Anything over this size will be downloaded in
82     * pieces with attachments being left off completely and downloaded on demand.
83     *
84     *
85     * 25k for a "small" message was picked by educated trial and error.
86     * http://answers.google.com/answers/threadview?id=312463 claims that the
87     * average size of an email is 59k, which I feel is too large for our
88     * blind download. The following tests were performed on a download of
89     * 25 random messages.
90     * <pre>
91     * 5k - 61 seconds,
92     * 25k - 51 seconds,
93     * 55k - 53 seconds,
94     * </pre>
95     * So 25k gives good performance and a reasonable data footprint. Sounds good to me.
96     */
97    private static final int MAX_SMALL_MESSAGE_SIZE = (25 * 1024);
98
99    private static final Flag[] FLAG_LIST_SEEN = new Flag[] { Flag.SEEN };
100    private static final Flag[] FLAG_LIST_FLAGGED = new Flag[] { Flag.FLAGGED };
101
102    /**
103     * We write this into the serverId field of messages that will never be upsynced.
104     */
105    private static final String LOCAL_SERVERID_PREFIX = "Local-";
106
107    /**
108     * Projections & CVs used by pruneCachedAttachments
109     */
110    private static final String[] PRUNE_ATTACHMENT_PROJECTION = new String[] {
111        AttachmentColumns.LOCATION
112    };
113    private static final ContentValues PRUNE_ATTACHMENT_CV = new ContentValues();
114    static {
115        PRUNE_ATTACHMENT_CV.putNull(AttachmentColumns.CONTENT_URI);
116    }
117
118    private static MessagingController sInstance = null;
119    private final BlockingQueue<Command> mCommands = new LinkedBlockingQueue<Command>();
120    private final Thread mThread;
121
122    /**
123     * All access to mListeners *must* be synchronized
124     */
125    private final GroupMessagingListener mListeners = new GroupMessagingListener();
126    private boolean mBusy;
127    private final Context mContext;
128    private final Controller mController;
129
130    protected MessagingController(Context _context, Controller _controller) {
131        mContext = _context.getApplicationContext();
132        mController = _controller;
133        mThread = new Thread(this);
134        mThread.start();
135    }
136
137    /**
138     * Gets or creates the singleton instance of MessagingController. Application is used to
139     * provide a Context to classes that need it.
140     */
141    public synchronized static MessagingController getInstance(Context _context,
142            Controller _controller) {
143        if (sInstance == null) {
144            sInstance = new MessagingController(_context, _controller);
145        }
146        return sInstance;
147    }
148
149    /**
150     * Inject a mock controller.  Used only for testing.  Affects future calls to getInstance().
151     */
152    public static void injectMockController(MessagingController mockController) {
153        sInstance = mockController;
154    }
155
156    // TODO: seems that this reading of mBusy isn't thread-safe
157    public boolean isBusy() {
158        return mBusy;
159    }
160
161    public void run() {
162        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
163        // TODO: add an end test to this infinite loop
164        while (true) {
165            Command command;
166            try {
167                command = mCommands.take();
168            } catch (InterruptedException e) {
169                continue; //re-test the condition on the eclosing while
170            }
171            if (command.listener == null || isActiveListener(command.listener)) {
172                mBusy = true;
173                command.runnable.run();
174                mListeners.controllerCommandCompleted(mCommands.size() > 0);
175            }
176            mBusy = false;
177        }
178    }
179
180    private void put(String description, MessagingListener listener, Runnable runnable) {
181        try {
182            Command command = new Command();
183            command.listener = listener;
184            command.runnable = runnable;
185            command.description = description;
186            mCommands.add(command);
187        }
188        catch (IllegalStateException ie) {
189            throw new Error(ie);
190        }
191    }
192
193    public void addListener(MessagingListener listener) {
194        mListeners.addListener(listener);
195    }
196
197    public void removeListener(MessagingListener listener) {
198        mListeners.removeListener(listener);
199    }
200
201    private boolean isActiveListener(MessagingListener listener) {
202        return mListeners.isActiveListener(listener);
203    }
204
205    /**
206     * Lightweight class for capturing local mailboxes in an account.  Just the columns
207     * necessary for a sync.
208     */
209    private static class LocalMailboxInfo {
210        private static final int COLUMN_ID = 0;
211        private static final int COLUMN_DISPLAY_NAME = 1;
212        private static final int COLUMN_ACCOUNT_KEY = 2;
213        private static final int COLUMN_TYPE = 3;
214
215        private static final String[] PROJECTION = new String[] {
216            EmailContent.RECORD_ID,
217            MailboxColumns.DISPLAY_NAME, MailboxColumns.ACCOUNT_KEY, MailboxColumns.TYPE,
218        };
219
220        final long mId;
221        final String mDisplayName;
222        final long mAccountKey;
223        final int mType;
224
225        public LocalMailboxInfo(Cursor c) {
226            mId = c.getLong(COLUMN_ID);
227            mDisplayName = c.getString(COLUMN_DISPLAY_NAME);
228            mAccountKey = c.getLong(COLUMN_ACCOUNT_KEY);
229            mType = c.getInt(COLUMN_TYPE);
230        }
231    }
232
233    /**
234     * Lists folders that are available locally and remotely. This method calls
235     * listFoldersCallback for local folders before it returns, and then for
236     * remote folders at some later point. If there are no local folders
237     * includeRemote is forced by this method. This method should be called from
238     * a Thread as it may take several seconds to list the local folders.
239     *
240     * TODO this needs to cache the remote folder list
241     * TODO break out an inner listFoldersSynchronized which could simplify checkMail
242     *
243     * @param account
244     * @param listener
245     * @throws MessagingException
246     */
247    public void listFolders(final long accountId, MessagingListener listener) {
248        final EmailContent.Account account =
249                EmailContent.Account.restoreAccountWithId(mContext, accountId);
250        if (account == null) {
251            return;
252        }
253        mListeners.listFoldersStarted(accountId);
254        put("listFolders", listener, new Runnable() {
255            public void run() {
256                Cursor localFolderCursor = null;
257                try {
258                    // Step 1:  Get remote folders, make a list, and add any local folders
259                    // that don't already exist.
260
261                    Store store = Store.getInstance(account.getStoreUri(mContext), mContext, null);
262
263                    Folder[] remoteFolders = store.getAllFolders();
264
265                    HashSet<String> remoteFolderNames = new HashSet<String>();
266                    for (int i = 0, count = remoteFolders.length; i < count; i++) {
267                        remoteFolderNames.add(remoteFolders[i].getName());
268                    }
269
270                    HashMap<String, LocalMailboxInfo> localFolders =
271                        new HashMap<String, LocalMailboxInfo>();
272                    HashSet<String> localFolderNames = new HashSet<String>();
273                    localFolderCursor = mContext.getContentResolver().query(
274                            EmailContent.Mailbox.CONTENT_URI,
275                            LocalMailboxInfo.PROJECTION,
276                            EmailContent.MailboxColumns.ACCOUNT_KEY + "=?",
277                            new String[] { String.valueOf(account.mId) },
278                            null);
279                    while (localFolderCursor.moveToNext()) {
280                        LocalMailboxInfo info = new LocalMailboxInfo(localFolderCursor);
281                        localFolders.put(info.mDisplayName, info);
282                        localFolderNames.add(info.mDisplayName);
283                    }
284
285                    // Short circuit the rest if the sets are the same (the usual case)
286                    if (!remoteFolderNames.equals(localFolderNames)) {
287
288                        // They are different, so we have to do some adds and drops
289
290                        // Drops first, to make things smaller rather than larger
291                        HashSet<String> localsToDrop = new HashSet<String>(localFolderNames);
292                        localsToDrop.removeAll(remoteFolderNames);
293                        for (String localNameToDrop : localsToDrop) {
294                            LocalMailboxInfo localInfo = localFolders.get(localNameToDrop);
295                            // Exclusion list - never delete local special folders, irrespective
296                            // of server-side existence.
297                            switch (localInfo.mType) {
298                                case Mailbox.TYPE_INBOX:
299                                case Mailbox.TYPE_DRAFTS:
300                                case Mailbox.TYPE_OUTBOX:
301                                case Mailbox.TYPE_SENT:
302                                case Mailbox.TYPE_TRASH:
303                                    break;
304                                default:
305                                    // Drop all attachment files related to this mailbox
306                                    AttachmentUtilities.deleteAllMailboxAttachmentFiles(
307                                            mContext, accountId, localInfo.mId);
308                                    // Delete the mailbox.  Triggers will take care of
309                                    // related Message, Body and Attachment records.
310                                    Uri uri = ContentUris.withAppendedId(
311                                            EmailContent.Mailbox.CONTENT_URI, localInfo.mId);
312                                    mContext.getContentResolver().delete(uri, null, null);
313                                    break;
314                            }
315                        }
316
317                        // Now do the adds
318                        remoteFolderNames.removeAll(localFolderNames);
319                        for (String remoteNameToAdd : remoteFolderNames) {
320                            EmailContent.Mailbox box = new EmailContent.Mailbox();
321                            box.mDisplayName = remoteNameToAdd;
322                            // box.mServerId;
323                            // box.mParentServerId;
324                            // box.mParentKey;
325                            box.mAccountKey = account.mId;
326                            box.mType = LegacyConversions.inferMailboxTypeFromName(
327                                    mContext, remoteNameToAdd);
328                            // box.mDelimiter;
329                            // box.mSyncKey;
330                            // box.mSyncLookback;
331                            // box.mSyncFrequency;
332                            // box.mSyncTime;
333                            // box.mUnreadCount;
334                            box.mFlagVisible = true;
335                            // box.mFlags;
336                            box.mVisibleLimit = Email.VISIBLE_LIMIT_DEFAULT;
337                            box.save(mContext);
338                        }
339                    }
340                    mListeners.listFoldersFinished(accountId);
341                } catch (Exception e) {
342                    mListeners.listFoldersFailed(accountId, "");
343                } finally {
344                    if (localFolderCursor != null) {
345                        localFolderCursor.close();
346                    }
347                }
348            }
349        });
350    }
351
352    /**
353     * Start background synchronization of the specified folder.
354     * @param account
355     * @param folder
356     * @param listener
357     */
358    public void synchronizeMailbox(final EmailContent.Account account,
359            final EmailContent.Mailbox folder, MessagingListener listener) {
360        /*
361         * We don't ever sync the Outbox.
362         */
363        if (folder.mType == EmailContent.Mailbox.TYPE_OUTBOX) {
364            return;
365        }
366        mListeners.synchronizeMailboxStarted(account.mId, folder.mId);
367        put("synchronizeMailbox", listener, new Runnable() {
368            public void run() {
369                synchronizeMailboxSynchronous(account, folder);
370            }
371        });
372    }
373
374    /**
375     * Start foreground synchronization of the specified folder. This is called by
376     * synchronizeMailbox or checkMail.
377     * TODO this should use ID's instead of fully-restored objects
378     * @param account
379     * @param folder
380     */
381    private void synchronizeMailboxSynchronous(final EmailContent.Account account,
382            final EmailContent.Mailbox folder) {
383        mListeners.synchronizeMailboxStarted(account.mId, folder.mId);
384        NotificationController nc = NotificationController.getInstance(mContext);
385        try {
386            processPendingActionsSynchronous(account);
387
388            StoreSynchronizer.SyncResults results;
389
390            // Select generic sync or store-specific sync
391            Store remoteStore = Store.getInstance(account.getStoreUri(mContext), mContext, null);
392            StoreSynchronizer customSync = remoteStore.getMessageSynchronizer();
393            if (customSync == null) {
394                results = synchronizeMailboxGeneric(account, folder);
395            } else {
396                results = customSync.SynchronizeMessagesSynchronous(
397                        account, folder, mListeners, mContext);
398            }
399            mListeners.synchronizeMailboxFinished(account.mId, folder.mId,
400                                                  results.mTotalMessages,
401                                                  results.mNewMessages);
402            // Clear authentication notification for this account
403            nc.cancelLoginFailedNotification(account.mId);
404        } catch (MessagingException e) {
405            if (Email.LOGD) {
406                Log.v(Logging.LOG_TAG, "synchronizeMailbox", e);
407            }
408            if (e instanceof AuthenticationFailedException) {
409                // Generate authentication notification
410                nc.showLoginFailedNotification(account.mId);
411            }
412            mListeners.synchronizeMailboxFailed(account.mId, folder.mId, e);
413        }
414    }
415
416    /**
417     * Lightweight record for the first pass of message sync, where I'm just seeing if
418     * the local message requires sync.  Later (for messages that need syncing) we'll do a full
419     * readout from the DB.
420     */
421    private static class LocalMessageInfo {
422        private static final int COLUMN_ID = 0;
423        private static final int COLUMN_FLAG_READ = 1;
424        private static final int COLUMN_FLAG_FAVORITE = 2;
425        private static final int COLUMN_FLAG_LOADED = 3;
426        private static final int COLUMN_SERVER_ID = 4;
427        private static final String[] PROJECTION = new String[] {
428            EmailContent.RECORD_ID,
429            MessageColumns.FLAG_READ, MessageColumns.FLAG_FAVORITE, MessageColumns.FLAG_LOADED,
430            SyncColumns.SERVER_ID, MessageColumns.MAILBOX_KEY, MessageColumns.ACCOUNT_KEY
431        };
432
433        final int mCursorIndex;
434        final long mId;
435        final boolean mFlagRead;
436        final boolean mFlagFavorite;
437        final int mFlagLoaded;
438        final String mServerId;
439
440        public LocalMessageInfo(Cursor c) {
441            mCursorIndex = c.getPosition();
442            mId = c.getLong(COLUMN_ID);
443            mFlagRead = c.getInt(COLUMN_FLAG_READ) != 0;
444            mFlagFavorite = c.getInt(COLUMN_FLAG_FAVORITE) != 0;
445            mFlagLoaded = c.getInt(COLUMN_FLAG_LOADED);
446            mServerId = c.getString(COLUMN_SERVER_ID);
447            // Note: mailbox key and account key not needed - they are projected for the SELECT
448        }
449    }
450
451    private void saveOrUpdate(EmailContent content, Context context) {
452        if (content.isSaved()) {
453            content.update(context, content.toContentValues());
454        } else {
455            content.save(context);
456        }
457    }
458
459    /**
460     * Generic synchronizer - used for POP3 and IMAP.
461     *
462     * TODO Break this method up into smaller chunks.
463     *
464     * @param account the account to sync
465     * @param folder the mailbox to sync
466     * @return results of the sync pass
467     * @throws MessagingException
468     */
469    private StoreSynchronizer.SyncResults synchronizeMailboxGeneric(
470            final EmailContent.Account account, final EmailContent.Mailbox folder)
471            throws MessagingException {
472
473        Log.d(Logging.LOG_TAG, "*** synchronizeMailboxGeneric ***");
474        ContentResolver resolver = mContext.getContentResolver();
475
476        // 0.  We do not ever sync DRAFTS or OUTBOX (down or up)
477        if (folder.mType == Mailbox.TYPE_DRAFTS || folder.mType == Mailbox.TYPE_OUTBOX) {
478            int totalMessages = EmailContent.count(mContext, folder.getUri(), null, null);
479            return new StoreSynchronizer.SyncResults(totalMessages, 0);
480        }
481
482        // 1.  Get the message list from the local store and create an index of the uids
483
484        Cursor localUidCursor = null;
485        HashMap<String, LocalMessageInfo> localMessageMap = new HashMap<String, LocalMessageInfo>();
486
487        try {
488            localUidCursor = resolver.query(
489                    EmailContent.Message.CONTENT_URI,
490                    LocalMessageInfo.PROJECTION,
491                    EmailContent.MessageColumns.ACCOUNT_KEY + "=?" +
492                    " AND " + MessageColumns.MAILBOX_KEY + "=?",
493                    new String[] {
494                            String.valueOf(account.mId),
495                            String.valueOf(folder.mId)
496                    },
497                    null);
498            while (localUidCursor.moveToNext()) {
499                LocalMessageInfo info = new LocalMessageInfo(localUidCursor);
500                localMessageMap.put(info.mServerId, info);
501            }
502        } finally {
503            if (localUidCursor != null) {
504                localUidCursor.close();
505            }
506        }
507
508        // 1a. Count the unread messages before changing anything
509        int localUnreadCount = EmailContent.count(mContext, EmailContent.Message.CONTENT_URI,
510                EmailContent.MessageColumns.ACCOUNT_KEY + "=?" +
511                " AND " + MessageColumns.MAILBOX_KEY + "=?" +
512                " AND " + MessageColumns.FLAG_READ + "=0",
513                new String[] {
514                        String.valueOf(account.mId),
515                        String.valueOf(folder.mId)
516                });
517
518        // 2.  Open the remote folder and create the remote folder if necessary
519
520        Store remoteStore = Store.getInstance(account.getStoreUri(mContext), mContext, null);
521        Folder remoteFolder = remoteStore.getFolder(folder.mDisplayName);
522
523        /*
524         * If the folder is a "special" folder we need to see if it exists
525         * on the remote server. It if does not exist we'll try to create it. If we
526         * can't create we'll abort. This will happen on every single Pop3 folder as
527         * designed and on Imap folders during error conditions. This allows us
528         * to treat Pop3 and Imap the same in this code.
529         */
530        if (folder.mType == Mailbox.TYPE_TRASH || folder.mType == Mailbox.TYPE_SENT
531                || folder.mType == Mailbox.TYPE_DRAFTS) {
532            if (!remoteFolder.exists()) {
533                if (!remoteFolder.create(FolderType.HOLDS_MESSAGES)) {
534                    return new StoreSynchronizer.SyncResults(0, 0);
535                }
536            }
537        }
538
539        // 3, Open the remote folder. This pre-loads certain metadata like message count.
540        remoteFolder.open(OpenMode.READ_WRITE, null);
541
542        // 4. Trash any remote messages that are marked as trashed locally.
543        // TODO - this comment was here, but no code was here.
544
545        // 5. Get the remote message count.
546        int remoteMessageCount = remoteFolder.getMessageCount();
547
548        // 6. Determine the limit # of messages to download
549        int visibleLimit = folder.mVisibleLimit;
550        if (visibleLimit <= 0) {
551            Store.StoreInfo info = Store.StoreInfo.getStoreInfo(account.getStoreUri(mContext),
552                    mContext);
553            visibleLimit = info.mVisibleLimitDefault;
554        }
555
556        // 7.  Create a list of messages to download
557        Message[] remoteMessages = new Message[0];
558        final ArrayList<Message> unsyncedMessages = new ArrayList<Message>();
559        HashMap<String, Message> remoteUidMap = new HashMap<String, Message>();
560
561        int newMessageCount = 0;
562        if (remoteMessageCount > 0) {
563            /*
564             * Message numbers start at 1.
565             */
566            int remoteStart = Math.max(0, remoteMessageCount - visibleLimit) + 1;
567            int remoteEnd = remoteMessageCount;
568            remoteMessages = remoteFolder.getMessages(remoteStart, remoteEnd, null);
569            for (Message message : remoteMessages) {
570                remoteUidMap.put(message.getUid(), message);
571            }
572
573            /*
574             * Get a list of the messages that are in the remote list but not on the
575             * local store, or messages that are in the local store but failed to download
576             * on the last sync. These are the new messages that we will download.
577             * Note, we also skip syncing messages which are flagged as "deleted message" sentinels,
578             * because they are locally deleted and we don't need or want the old message from
579             * the server.
580             */
581            for (Message message : remoteMessages) {
582                LocalMessageInfo localMessage = localMessageMap.get(message.getUid());
583                if (localMessage == null) {
584                    newMessageCount++;
585                }
586                // localMessage == null -> message has never been created (not even headers)
587                // mFlagLoaded = UNLOADED -> message created, but none of body loaded
588                // mFlagLoaded = PARTIAL -> message created, a "sane" amt of body has been loaded
589                // mFlagLoaded = COMPLETE -> message body has been completely loaded
590                // mFlagLoaded = DELETED -> message has been deleted
591                // Only the first two of these are "unsynced", so let's retrieve them
592                if (localMessage == null ||
593                        (localMessage.mFlagLoaded == EmailContent.Message.FLAG_LOADED_UNLOADED)) {
594                    unsyncedMessages.add(message);
595                }
596            }
597        }
598
599        // 8.  Download basic info about the new/unloaded messages (if any)
600        /*
601         * A list of messages that were downloaded and which did not have the Seen flag set.
602         * This will serve to indicate the true "new" message count that will be reported to
603         * the user via notification.
604         */
605        final ArrayList<Message> newMessages = new ArrayList<Message>();
606
607        /*
608         * Fetch the flags and envelope only of the new messages. This is intended to get us
609         * critical data as fast as possible, and then we'll fill in the details.
610         */
611        if (unsyncedMessages.size() > 0) {
612            FetchProfile fp = new FetchProfile();
613            fp.add(FetchProfile.Item.FLAGS);
614            fp.add(FetchProfile.Item.ENVELOPE);
615            final HashMap<String, LocalMessageInfo> localMapCopy =
616                new HashMap<String, LocalMessageInfo>(localMessageMap);
617
618            remoteFolder.fetch(unsyncedMessages.toArray(new Message[0]), fp,
619                    new MessageRetrievalListener() {
620                        public void messageRetrieved(Message message) {
621                            try {
622                                // Determine if the new message was already known (e.g. partial)
623                                // And create or reload the full message info
624                                LocalMessageInfo localMessageInfo =
625                                    localMapCopy.get(message.getUid());
626                                EmailContent.Message localMessage = null;
627                                if (localMessageInfo == null) {
628                                    localMessage = new EmailContent.Message();
629                                } else {
630                                    localMessage = EmailContent.Message.restoreMessageWithId(
631                                            mContext, localMessageInfo.mId);
632                                }
633
634                                if (localMessage != null) {
635                                    try {
636                                        // Copy the fields that are available into the message
637                                        LegacyConversions.updateMessageFields(localMessage,
638                                                message, account.mId, folder.mId);
639                                        // Commit the message to the local store
640                                        saveOrUpdate(localMessage, mContext);
641                                        // Track the "new" ness of the downloaded message
642                                        if (!message.isSet(Flag.SEEN)) {
643                                            newMessages.add(message);
644                                        }
645                                    } catch (MessagingException me) {
646                                        Log.e(Logging.LOG_TAG,
647                                                "Error while copying downloaded message." + me);
648                                    }
649
650                                }
651                            }
652                            catch (Exception e) {
653                                Log.e(Logging.LOG_TAG,
654                                        "Error while storing downloaded message." + e.toString());
655                            }
656                        }
657
658                        @Override
659                        public void loadAttachmentProgress(int progress) {
660                        }
661                    });
662        }
663
664        // 9. Refresh the flags for any messages in the local store that we didn't just download.
665        FetchProfile fp = new FetchProfile();
666        fp.add(FetchProfile.Item.FLAGS);
667        remoteFolder.fetch(remoteMessages, fp, null);
668        boolean remoteSupportsSeen = false;
669        boolean remoteSupportsFlagged = false;
670        for (Flag flag : remoteFolder.getPermanentFlags()) {
671            if (flag == Flag.SEEN) {
672                remoteSupportsSeen = true;
673            }
674            if (flag == Flag.FLAGGED) {
675                remoteSupportsFlagged = true;
676            }
677        }
678        // Update the SEEN & FLAGGED (star) flags (if supported remotely - e.g. not for POP3)
679        if (remoteSupportsSeen || remoteSupportsFlagged) {
680            for (Message remoteMessage : remoteMessages) {
681                LocalMessageInfo localMessageInfo = localMessageMap.get(remoteMessage.getUid());
682                if (localMessageInfo == null) {
683                    continue;
684                }
685                boolean localSeen = localMessageInfo.mFlagRead;
686                boolean remoteSeen = remoteMessage.isSet(Flag.SEEN);
687                boolean newSeen = (remoteSupportsSeen && (remoteSeen != localSeen));
688                boolean localFlagged = localMessageInfo.mFlagFavorite;
689                boolean remoteFlagged = remoteMessage.isSet(Flag.FLAGGED);
690                boolean newFlagged = (remoteSupportsFlagged && (localFlagged != remoteFlagged));
691                if (newSeen || newFlagged) {
692                    Uri uri = ContentUris.withAppendedId(
693                            EmailContent.Message.CONTENT_URI, localMessageInfo.mId);
694                    ContentValues updateValues = new ContentValues();
695                    updateValues.put(EmailContent.Message.FLAG_READ, remoteSeen);
696                    updateValues.put(EmailContent.Message.FLAG_FAVORITE, remoteFlagged);
697                    resolver.update(uri, updateValues, null, null);
698                }
699            }
700        }
701
702        // 10. Compute and store the unread message count.
703        // -- no longer necessary - Provider uses DB triggers to keep track
704
705//        int remoteUnreadMessageCount = remoteFolder.getUnreadMessageCount();
706//        if (remoteUnreadMessageCount == -1) {
707//            if (remoteSupportsSeenFlag) {
708//                /*
709//                 * If remote folder doesn't supported unread message count but supports
710//                 * seen flag, use local folder's unread message count and the size of
711//                 * new messages. This mode is not used for POP3, or IMAP.
712//                 */
713//
714//                remoteUnreadMessageCount = folder.mUnreadCount + newMessages.size();
715//            } else {
716//                /*
717//                 * If remote folder doesn't supported unread message count and doesn't
718//                 * support seen flag, use localUnreadCount and newMessageCount which
719//                 * don't rely on remote SEEN flag.  This mode is used by POP3.
720//                 */
721//                remoteUnreadMessageCount = localUnreadCount + newMessageCount;
722//            }
723//        } else {
724//            /*
725//             * If remote folder supports unread message count, use remoteUnreadMessageCount.
726//             * This mode is used by IMAP.
727//             */
728//         }
729//        Uri uri = ContentUris.withAppendedId(EmailContent.Mailbox.CONTENT_URI, folder.mId);
730//        ContentValues updateValues = new ContentValues();
731//        updateValues.put(EmailContent.Mailbox.UNREAD_COUNT, remoteUnreadMessageCount);
732//        resolver.update(uri, updateValues, null, null);
733
734        // 11. Remove any messages that are in the local store but no longer on the remote store.
735
736        HashSet<String> localUidsToDelete = new HashSet<String>(localMessageMap.keySet());
737        localUidsToDelete.removeAll(remoteUidMap.keySet());
738        for (String uidToDelete : localUidsToDelete) {
739            LocalMessageInfo infoToDelete = localMessageMap.get(uidToDelete);
740
741            // Delete associated data (attachment files)
742            // Attachment & Body records are auto-deleted when we delete the Message record
743            AttachmentUtilities.deleteAllAttachmentFiles(mContext, account.mId,
744                    infoToDelete.mId);
745
746            // Delete the message itself
747            Uri uriToDelete = ContentUris.withAppendedId(
748                    EmailContent.Message.CONTENT_URI, infoToDelete.mId);
749            resolver.delete(uriToDelete, null, null);
750
751            // Delete extra rows (e.g. synced or deleted)
752            Uri syncRowToDelete = ContentUris.withAppendedId(
753                    EmailContent.Message.UPDATED_CONTENT_URI, infoToDelete.mId);
754            resolver.delete(syncRowToDelete, null, null);
755            Uri deletERowToDelete = ContentUris.withAppendedId(
756                    EmailContent.Message.UPDATED_CONTENT_URI, infoToDelete.mId);
757            resolver.delete(deletERowToDelete, null, null);
758        }
759
760        // 12. Divide the unsynced messages into small & large (by size)
761
762        // TODO doing this work here (synchronously) is problematic because it prevents the UI
763        // from affecting the order (e.g. download a message because the user requested it.)  Much
764        // of this logic should move out to a different sync loop that attempts to update small
765        // groups of messages at a time, as a background task.  However, we can't just return
766        // (yet) because POP messages don't have an envelope yet....
767
768        ArrayList<Message> largeMessages = new ArrayList<Message>();
769        ArrayList<Message> smallMessages = new ArrayList<Message>();
770        for (Message message : unsyncedMessages) {
771            if (message.getSize() > (MAX_SMALL_MESSAGE_SIZE)) {
772                largeMessages.add(message);
773            } else {
774                smallMessages.add(message);
775            }
776        }
777
778        // 13. Download small messages
779
780        // TODO Problems with this implementation.  1. For IMAP, where we get a real envelope,
781        // this is going to be inefficient and duplicate work we've already done.  2.  It's going
782        // back to the DB for a local message that we already had (and discarded).
783
784        // For small messages, we specify "body", which returns everything (incl. attachments)
785        fp = new FetchProfile();
786        fp.add(FetchProfile.Item.BODY);
787        remoteFolder.fetch(smallMessages.toArray(new Message[smallMessages.size()]), fp,
788                new MessageRetrievalListener() {
789                    public void messageRetrieved(Message message) {
790                        // Store the updated message locally and mark it fully loaded
791                        copyOneMessageToProvider(message, account, folder,
792                                EmailContent.Message.FLAG_LOADED_COMPLETE);
793                    }
794
795                    @Override
796                    public void loadAttachmentProgress(int progress) {
797                    }
798        });
799
800        // 14. Download large messages.  We ask the server to give us the message structure,
801        // but not all of the attachments.
802        fp.clear();
803        fp.add(FetchProfile.Item.STRUCTURE);
804        remoteFolder.fetch(largeMessages.toArray(new Message[largeMessages.size()]), fp, null);
805        for (Message message : largeMessages) {
806            if (message.getBody() == null) {
807                // POP doesn't support STRUCTURE mode, so we'll just do a partial download
808                // (hopefully enough to see some/all of the body) and mark the message for
809                // further download.
810                fp.clear();
811                fp.add(FetchProfile.Item.BODY_SANE);
812                //  TODO a good optimization here would be to make sure that all Stores set
813                //  the proper size after this fetch and compare the before and after size. If
814                //  they equal we can mark this SYNCHRONIZED instead of PARTIALLY_SYNCHRONIZED
815                remoteFolder.fetch(new Message[] { message }, fp, null);
816
817                // Store the partially-loaded message and mark it partially loaded
818                copyOneMessageToProvider(message, account, folder,
819                        EmailContent.Message.FLAG_LOADED_PARTIAL);
820            } else {
821                // We have a structure to deal with, from which
822                // we can pull down the parts we want to actually store.
823                // Build a list of parts we are interested in. Text parts will be downloaded
824                // right now, attachments will be left for later.
825                ArrayList<Part> viewables = new ArrayList<Part>();
826                ArrayList<Part> attachments = new ArrayList<Part>();
827                MimeUtility.collectParts(message, viewables, attachments);
828                // Download the viewables immediately
829                for (Part part : viewables) {
830                    fp.clear();
831                    fp.add(part);
832                    // TODO what happens if the network connection dies? We've got partial
833                    // messages with incorrect status stored.
834                    remoteFolder.fetch(new Message[] { message }, fp, null);
835                }
836                // Store the updated message locally and mark it fully loaded
837                copyOneMessageToProvider(message, account, folder,
838                        EmailContent.Message.FLAG_LOADED_COMPLETE);
839            }
840        }
841
842        // 15. Clean up and report results
843
844        remoteFolder.close(false);
845        // TODO - more
846
847        // Original sync code.  Using for reference, will delete when done.
848        if (false) {
849        /*
850         * Now do the large messages that require more round trips.
851         */
852        fp.clear();
853        fp.add(FetchProfile.Item.STRUCTURE);
854        remoteFolder.fetch(largeMessages.toArray(new Message[largeMessages.size()]),
855                fp, null);
856        for (Message message : largeMessages) {
857            if (message.getBody() == null) {
858                /*
859                 * The provider was unable to get the structure of the message, so
860                 * we'll download a reasonable portion of the messge and mark it as
861                 * incomplete so the entire thing can be downloaded later if the user
862                 * wishes to download it.
863                 */
864                fp.clear();
865                fp.add(FetchProfile.Item.BODY_SANE);
866                /*
867                 *  TODO a good optimization here would be to make sure that all Stores set
868                 *  the proper size after this fetch and compare the before and after size. If
869                 *  they equal we can mark this SYNCHRONIZED instead of PARTIALLY_SYNCHRONIZED
870                 */
871
872                remoteFolder.fetch(new Message[] { message }, fp, null);
873                // Store the updated message locally
874//                localFolder.appendMessages(new Message[] {
875//                    message
876//                });
877
878//                Message localMessage = localFolder.getMessage(message.getUid());
879
880                // Set a flag indicating that the message has been partially downloaded and
881                // is ready for view.
882//                localMessage.setFlag(Flag.X_DOWNLOADED_PARTIAL, true);
883            } else {
884                /*
885                 * We have a structure to deal with, from which
886                 * we can pull down the parts we want to actually store.
887                 * Build a list of parts we are interested in. Text parts will be downloaded
888                 * right now, attachments will be left for later.
889                 */
890
891                ArrayList<Part> viewables = new ArrayList<Part>();
892                ArrayList<Part> attachments = new ArrayList<Part>();
893                MimeUtility.collectParts(message, viewables, attachments);
894
895                /*
896                 * Now download the parts we're interested in storing.
897                 */
898                for (Part part : viewables) {
899                    fp.clear();
900                    fp.add(part);
901                    // TODO what happens if the network connection dies? We've got partial
902                    // messages with incorrect status stored.
903                    remoteFolder.fetch(new Message[] { message }, fp, null);
904                }
905                // Store the updated message locally
906//                localFolder.appendMessages(new Message[] {
907//                    message
908//                });
909
910//                Message localMessage = localFolder.getMessage(message.getUid());
911
912                // Set a flag indicating this message has been fully downloaded and can be
913                // viewed.
914//                localMessage.setFlag(Flag.X_DOWNLOADED_FULL, true);
915            }
916
917            // Update the listener with what we've found
918//            synchronized (mListeners) {
919//                for (MessagingListener l : mListeners) {
920//                    l.synchronizeMailboxNewMessage(
921//                            account,
922//                            folder,
923//                            localFolder.getMessage(message.getUid()));
924//                }
925//            }
926        }
927
928
929        /*
930         * Report successful sync
931         */
932        StoreSynchronizer.SyncResults results = new StoreSynchronizer.SyncResults(
933                remoteFolder.getMessageCount(), newMessages.size());
934
935        remoteFolder.close(false);
936//        localFolder.close(false);
937
938        return results;
939        }
940
941        return new StoreSynchronizer.SyncResults(remoteMessageCount, newMessages.size());
942    }
943
944    /**
945     * Copy one downloaded message (which may have partially-loaded sections)
946     * into a newly created EmailProvider Message, given the account and mailbox
947     *
948     * @param message the remote message we've just downloaded
949     * @param account the account it will be stored into
950     * @param folder the mailbox it will be stored into
951     * @param loadStatus when complete, the message will be marked with this status (e.g.
952     *        EmailContent.Message.LOADED)
953     */
954    public void copyOneMessageToProvider(Message message, EmailContent.Account account,
955            EmailContent.Mailbox folder, int loadStatus) {
956        EmailContent.Message localMessage = null;
957        Cursor c = null;
958        try {
959            c = mContext.getContentResolver().query(
960                    EmailContent.Message.CONTENT_URI,
961                    EmailContent.Message.CONTENT_PROJECTION,
962                    EmailContent.MessageColumns.ACCOUNT_KEY + "=?" +
963                    " AND " + MessageColumns.MAILBOX_KEY + "=?" +
964                    " AND " + SyncColumns.SERVER_ID + "=?",
965                    new String[] {
966                            String.valueOf(account.mId),
967                            String.valueOf(folder.mId),
968                            String.valueOf(message.getUid())
969                    },
970                    null);
971            if (c.moveToNext()) {
972                localMessage = EmailContent.getContent(c, EmailContent.Message.class);
973                localMessage.mMailboxKey = folder.mId;
974                localMessage.mAccountKey = account.mId;
975                copyOneMessageToProvider(message, localMessage, loadStatus, mContext);
976            }
977        } finally {
978            if (c != null) {
979                c.close();
980            }
981        }
982    }
983
984    /**
985     * Copy one downloaded message (which may have partially-loaded sections)
986     * into an already-created EmailProvider Message
987     *
988     * @param message the remote message we've just downloaded
989     * @param localMessage the EmailProvider Message, already created
990     * @param loadStatus when complete, the message will be marked with this status (e.g.
991     *        EmailContent.Message.LOADED)
992     * @param context the context to be used for EmailProvider
993     */
994    public void copyOneMessageToProvider(Message message, EmailContent.Message localMessage,
995            int loadStatus, Context context) {
996        try {
997
998            EmailContent.Body body = EmailContent.Body.restoreBodyWithMessageId(context,
999                    localMessage.mId);
1000            if (body == null) {
1001                body = new EmailContent.Body();
1002            }
1003            try {
1004                // Copy the fields that are available into the message object
1005                LegacyConversions.updateMessageFields(localMessage, message,
1006                        localMessage.mAccountKey, localMessage.mMailboxKey);
1007
1008                // Now process body parts & attachments
1009                ArrayList<Part> viewables = new ArrayList<Part>();
1010                ArrayList<Part> attachments = new ArrayList<Part>();
1011                MimeUtility.collectParts(message, viewables, attachments);
1012
1013                ConversionUtilities.updateBodyFields(body, localMessage, viewables);
1014
1015                // Commit the message & body to the local store immediately
1016                saveOrUpdate(localMessage, context);
1017                saveOrUpdate(body, context);
1018
1019                // process (and save) attachments
1020                LegacyConversions.updateAttachments(context, localMessage, attachments);
1021
1022                // One last update of message with two updated flags
1023                localMessage.mFlagLoaded = loadStatus;
1024
1025                ContentValues cv = new ContentValues();
1026                cv.put(EmailContent.MessageColumns.FLAG_ATTACHMENT, localMessage.mFlagAttachment);
1027                cv.put(EmailContent.MessageColumns.FLAG_LOADED, localMessage.mFlagLoaded);
1028                Uri uri = ContentUris.withAppendedId(EmailContent.Message.CONTENT_URI,
1029                        localMessage.mId);
1030                context.getContentResolver().update(uri, cv, null, null);
1031
1032            } catch (MessagingException me) {
1033                Log.e(Logging.LOG_TAG, "Error while copying downloaded message." + me);
1034            }
1035
1036        } catch (RuntimeException rte) {
1037            Log.e(Logging.LOG_TAG, "Error while storing downloaded message." + rte.toString());
1038        } catch (IOException ioe) {
1039            Log.e(Logging.LOG_TAG, "Error while storing attachment." + ioe.toString());
1040        }
1041    }
1042
1043    public void processPendingActions(final long accountId) {
1044        put("processPendingActions", null, new Runnable() {
1045            public void run() {
1046                try {
1047                    EmailContent.Account account =
1048                        EmailContent.Account.restoreAccountWithId(mContext, accountId);
1049                    if (account == null) {
1050                        return;
1051                    }
1052                    processPendingActionsSynchronous(account);
1053                }
1054                catch (MessagingException me) {
1055                    if (Email.LOGD) {
1056                        Log.v(Logging.LOG_TAG, "processPendingActions", me);
1057                    }
1058                    /*
1059                     * Ignore any exceptions from the commands. Commands will be processed
1060                     * on the next round.
1061                     */
1062                }
1063            }
1064        });
1065    }
1066
1067    /**
1068     * Find messages in the updated table that need to be written back to server.
1069     *
1070     * Handles:
1071     *   Read/Unread
1072     *   Flagged
1073     *   Append (upload)
1074     *   Move To Trash
1075     *   Empty trash
1076     * TODO:
1077     *   Move
1078     *
1079     * @param account the account to scan for pending actions
1080     * @throws MessagingException
1081     */
1082    private void processPendingActionsSynchronous(EmailContent.Account account)
1083           throws MessagingException {
1084        ContentResolver resolver = mContext.getContentResolver();
1085        String[] accountIdArgs = new String[] { Long.toString(account.mId) };
1086
1087        // Handle deletes first, it's always better to get rid of things first
1088        processPendingDeletesSynchronous(account, resolver, accountIdArgs);
1089
1090        // Handle uploads (currently, only to sent messages)
1091        processPendingUploadsSynchronous(account, resolver, accountIdArgs);
1092
1093        // Now handle updates / upsyncs
1094        processPendingUpdatesSynchronous(account, resolver, accountIdArgs);
1095    }
1096
1097    /**
1098     * Scan for messages that are in the Message_Deletes table, look for differences that
1099     * we can deal with, and do the work.
1100     *
1101     * @param account
1102     * @param resolver
1103     * @param accountIdArgs
1104     */
1105    private void processPendingDeletesSynchronous(EmailContent.Account account,
1106            ContentResolver resolver, String[] accountIdArgs) {
1107        Cursor deletes = resolver.query(EmailContent.Message.DELETED_CONTENT_URI,
1108                EmailContent.Message.CONTENT_PROJECTION,
1109                EmailContent.MessageColumns.ACCOUNT_KEY + "=?", accountIdArgs,
1110                EmailContent.MessageColumns.MAILBOX_KEY);
1111        long lastMessageId = -1;
1112        try {
1113            // Defer setting up the store until we know we need to access it
1114            Store remoteStore = null;
1115            // Demand load mailbox (note order-by to reduce thrashing here)
1116            Mailbox mailbox = null;
1117            // loop through messages marked as deleted
1118            while (deletes.moveToNext()) {
1119                boolean deleteFromTrash = false;
1120
1121                EmailContent.Message oldMessage =
1122                        EmailContent.getContent(deletes, EmailContent.Message.class);
1123
1124                if (oldMessage != null) {
1125                    lastMessageId = oldMessage.mId;
1126                    if (mailbox == null || mailbox.mId != oldMessage.mMailboxKey) {
1127                        mailbox = Mailbox.restoreMailboxWithId(mContext, oldMessage.mMailboxKey);
1128                        if (mailbox == null) {
1129                            continue; // Mailbox removed. Move to the next message.
1130                        }
1131                    }
1132                    deleteFromTrash = mailbox.mType == Mailbox.TYPE_TRASH;
1133                }
1134
1135                // Load the remote store if it will be needed
1136                if (remoteStore == null && deleteFromTrash) {
1137                    remoteStore = Store.getInstance(account.getStoreUri(mContext), mContext, null);
1138                }
1139
1140                // Dispatch here for specific change types
1141                if (deleteFromTrash) {
1142                    // Move message to trash
1143                    processPendingDeleteFromTrash(remoteStore, account, mailbox, oldMessage);
1144                }
1145
1146                // Finally, delete the update
1147                Uri uri = ContentUris.withAppendedId(EmailContent.Message.DELETED_CONTENT_URI,
1148                        oldMessage.mId);
1149                resolver.delete(uri, null, null);
1150            }
1151
1152        } catch (MessagingException me) {
1153            // Presumably an error here is an account connection failure, so there is
1154            // no point in continuing through the rest of the pending updates.
1155            if (Email.DEBUG) {
1156                Log.d(Logging.LOG_TAG, "Unable to process pending delete for id="
1157                            + lastMessageId + ": " + me);
1158            }
1159        } finally {
1160            deletes.close();
1161        }
1162    }
1163
1164    /**
1165     * Scan for messages that are in Sent, and are in need of upload,
1166     * and send them to the server.  "In need of upload" is defined as:
1167     *  serverId == null (no UID has been assigned)
1168     * or
1169     *  message is in the updated list
1170     *
1171     * Note we also look for messages that are moving from drafts->outbox->sent.  They never
1172     * go through "drafts" or "outbox" on the server, so we hang onto these until they can be
1173     * uploaded directly to the Sent folder.
1174     *
1175     * @param account
1176     * @param resolver
1177     * @param accountIdArgs
1178     */
1179    private void processPendingUploadsSynchronous(EmailContent.Account account,
1180            ContentResolver resolver, String[] accountIdArgs) throws MessagingException {
1181        // Find the Sent folder (since that's all we're uploading for now
1182        Cursor mailboxes = resolver.query(Mailbox.CONTENT_URI, Mailbox.ID_PROJECTION,
1183                MailboxColumns.ACCOUNT_KEY + "=?"
1184                + " and " + MailboxColumns.TYPE + "=" + Mailbox.TYPE_SENT,
1185                accountIdArgs, null);
1186        long lastMessageId = -1;
1187        try {
1188            // Defer setting up the store until we know we need to access it
1189            Store remoteStore = null;
1190            while (mailboxes.moveToNext()) {
1191                long mailboxId = mailboxes.getLong(Mailbox.ID_PROJECTION_COLUMN);
1192                String[] mailboxKeyArgs = new String[] { Long.toString(mailboxId) };
1193                // Demand load mailbox
1194                Mailbox mailbox = null;
1195
1196                // First handle the "new" messages (serverId == null)
1197                Cursor upsyncs1 = resolver.query(EmailContent.Message.CONTENT_URI,
1198                        EmailContent.Message.ID_PROJECTION,
1199                        EmailContent.Message.MAILBOX_KEY + "=?"
1200                        + " and (" + EmailContent.Message.SERVER_ID + " is null"
1201                        + " or " + EmailContent.Message.SERVER_ID + "=''" + ")",
1202                        mailboxKeyArgs,
1203                        null);
1204                try {
1205                    while (upsyncs1.moveToNext()) {
1206                        // Load the remote store if it will be needed
1207                        if (remoteStore == null) {
1208                            remoteStore =
1209                                Store.getInstance(account.getStoreUri(mContext), mContext, null);
1210                        }
1211                        // Load the mailbox if it will be needed
1212                        if (mailbox == null) {
1213                            mailbox = Mailbox.restoreMailboxWithId(mContext, mailboxId);
1214                            if (mailbox == null) {
1215                                continue; // Mailbox removed. Move to the next message.
1216                            }
1217                        }
1218                        // upsync the message
1219                        long id = upsyncs1.getLong(EmailContent.Message.ID_PROJECTION_COLUMN);
1220                        lastMessageId = id;
1221                        processUploadMessage(resolver, remoteStore, account, mailbox, id);
1222                    }
1223                } finally {
1224                    if (upsyncs1 != null) {
1225                        upsyncs1.close();
1226                    }
1227                }
1228
1229                // Next, handle any updates (e.g. edited in place, although this shouldn't happen)
1230                Cursor upsyncs2 = resolver.query(EmailContent.Message.UPDATED_CONTENT_URI,
1231                        EmailContent.Message.ID_PROJECTION,
1232                        EmailContent.MessageColumns.MAILBOX_KEY + "=?", mailboxKeyArgs,
1233                        null);
1234                try {
1235                    while (upsyncs2.moveToNext()) {
1236                        // Load the remote store if it will be needed
1237                        if (remoteStore == null) {
1238                            remoteStore =
1239                                Store.getInstance(account.getStoreUri(mContext), mContext, null);
1240                        }
1241                        // Load the mailbox if it will be needed
1242                        if (mailbox == null) {
1243                            mailbox = Mailbox.restoreMailboxWithId(mContext, mailboxId);
1244                            if (mailbox == null) {
1245                                continue; // Mailbox removed. Move to the next message.
1246                            }
1247                        }
1248                        // upsync the message
1249                        long id = upsyncs2.getLong(EmailContent.Message.ID_PROJECTION_COLUMN);
1250                        lastMessageId = id;
1251                        processUploadMessage(resolver, remoteStore, account, mailbox, id);
1252                    }
1253                } finally {
1254                    if (upsyncs2 != null) {
1255                        upsyncs2.close();
1256                    }
1257                }
1258            }
1259        } catch (MessagingException me) {
1260            // Presumably an error here is an account connection failure, so there is
1261            // no point in continuing through the rest of the pending updates.
1262            if (Email.DEBUG) {
1263                Log.d(Logging.LOG_TAG, "Unable to process pending upsync for id="
1264                        + lastMessageId + ": " + me);
1265            }
1266        } finally {
1267            if (mailboxes != null) {
1268                mailboxes.close();
1269            }
1270        }
1271    }
1272
1273    /**
1274     * Scan for messages that are in the Message_Updates table, look for differences that
1275     * we can deal with, and do the work.
1276     *
1277     * @param account
1278     * @param resolver
1279     * @param accountIdArgs
1280     */
1281    private void processPendingUpdatesSynchronous(EmailContent.Account account,
1282            ContentResolver resolver, String[] accountIdArgs) {
1283        Cursor updates = resolver.query(EmailContent.Message.UPDATED_CONTENT_URI,
1284                EmailContent.Message.CONTENT_PROJECTION,
1285                EmailContent.MessageColumns.ACCOUNT_KEY + "=?", accountIdArgs,
1286                EmailContent.MessageColumns.MAILBOX_KEY);
1287        long lastMessageId = -1;
1288        try {
1289            // Defer setting up the store until we know we need to access it
1290            Store remoteStore = null;
1291            // Demand load mailbox (note order-by to reduce thrashing here)
1292            Mailbox mailbox = null;
1293            // loop through messages marked as needing updates
1294            while (updates.moveToNext()) {
1295                boolean changeMoveToTrash = false;
1296                boolean changeRead = false;
1297                boolean changeFlagged = false;
1298                boolean changeMailbox = false;
1299
1300                EmailContent.Message oldMessage =
1301                    EmailContent.getContent(updates, EmailContent.Message.class);
1302                lastMessageId = oldMessage.mId;
1303                EmailContent.Message newMessage =
1304                    EmailContent.Message.restoreMessageWithId(mContext, oldMessage.mId);
1305                if (newMessage != null) {
1306                    if (mailbox == null || mailbox.mId != newMessage.mMailboxKey) {
1307                        mailbox = Mailbox.restoreMailboxWithId(mContext, newMessage.mMailboxKey);
1308                        if (mailbox == null) {
1309                            continue; // Mailbox removed. Move to the next message.
1310                        }
1311                    }
1312                    if (oldMessage.mMailboxKey != newMessage.mMailboxKey) {
1313                        if (mailbox.mType == Mailbox.TYPE_TRASH) {
1314                            changeMoveToTrash = true;
1315                        } else {
1316                            changeMailbox = true;
1317                        }
1318                    }
1319                    changeRead = oldMessage.mFlagRead != newMessage.mFlagRead;
1320                    changeFlagged = oldMessage.mFlagFavorite != newMessage.mFlagFavorite;
1321               }
1322
1323                // Load the remote store if it will be needed
1324                if (remoteStore == null &&
1325                        (changeMoveToTrash || changeRead || changeFlagged || changeMailbox)) {
1326                    remoteStore = Store.getInstance(account.getStoreUri(mContext), mContext, null);
1327                }
1328
1329                // Dispatch here for specific change types
1330                if (changeMoveToTrash) {
1331                    // Move message to trash
1332                    processPendingMoveToTrash(remoteStore, account, mailbox, oldMessage,
1333                            newMessage);
1334                } else if (changeRead || changeFlagged || changeMailbox) {
1335                    processPendingDataChange(remoteStore, mailbox, changeRead, changeFlagged,
1336                            changeMailbox, oldMessage, newMessage);
1337                }
1338
1339                // Finally, delete the update
1340                Uri uri = ContentUris.withAppendedId(EmailContent.Message.UPDATED_CONTENT_URI,
1341                        oldMessage.mId);
1342                resolver.delete(uri, null, null);
1343            }
1344
1345        } catch (MessagingException me) {
1346            // Presumably an error here is an account connection failure, so there is
1347            // no point in continuing through the rest of the pending updates.
1348            if (Email.DEBUG) {
1349                Log.d(Logging.LOG_TAG, "Unable to process pending update for id="
1350                            + lastMessageId + ": " + me);
1351            }
1352        } finally {
1353            updates.close();
1354        }
1355    }
1356
1357    /**
1358     * Upsync an entire message.  This must also unwind whatever triggered it (either by
1359     * updating the serverId, or by deleting the update record, or it's going to keep happening
1360     * over and over again.
1361     *
1362     * Note:  If the message is being uploaded into an unexpected mailbox, we *do not* upload.
1363     * This is to avoid unnecessary uploads into the trash.  Although the caller attempts to select
1364     * only the Drafts and Sent folders, this can happen when the update record and the current
1365     * record mismatch.  In this case, we let the update record remain, because the filters
1366     * in processPendingUpdatesSynchronous() will pick it up as a move and handle it (or drop it)
1367     * appropriately.
1368     *
1369     * @param resolver
1370     * @param remoteStore
1371     * @param account
1372     * @param mailbox the actual mailbox
1373     * @param messageId
1374     */
1375    private void processUploadMessage(ContentResolver resolver, Store remoteStore,
1376            EmailContent.Account account, Mailbox mailbox, long messageId)
1377            throws MessagingException {
1378        EmailContent.Message message =
1379            EmailContent.Message.restoreMessageWithId(mContext, messageId);
1380        boolean deleteUpdate = false;
1381        if (message == null) {
1382            deleteUpdate = true;
1383            Log.d(Logging.LOG_TAG, "Upsync failed for null message, id=" + messageId);
1384        } else if (mailbox.mType == Mailbox.TYPE_DRAFTS) {
1385            deleteUpdate = false;
1386            Log.d(Logging.LOG_TAG, "Upsync skipped for mailbox=drafts, id=" + messageId);
1387        } else if (mailbox.mType == Mailbox.TYPE_OUTBOX) {
1388            deleteUpdate = false;
1389            Log.d(Logging.LOG_TAG, "Upsync skipped for mailbox=outbox, id=" + messageId);
1390        } else if (mailbox.mType == Mailbox.TYPE_TRASH) {
1391            deleteUpdate = false;
1392            Log.d(Logging.LOG_TAG, "Upsync skipped for mailbox=trash, id=" + messageId);
1393        } else {
1394            Log.d(Logging.LOG_TAG, "Upsyc triggered for message id=" + messageId);
1395            deleteUpdate = processPendingAppend(remoteStore, account, mailbox, message);
1396        }
1397        if (deleteUpdate) {
1398            // Finally, delete the update (if any)
1399            Uri uri = ContentUris.withAppendedId(EmailContent.Message.UPDATED_CONTENT_URI, messageId);
1400            resolver.delete(uri, null, null);
1401        }
1402    }
1403
1404    /**
1405     * Upsync changes to read, flagged, or mailbox
1406     *
1407     * @param remoteStore the remote store for this mailbox
1408     * @param mailbox the mailbox the message is stored in
1409     * @param changeRead whether the message's read state has changed
1410     * @param changeFlagged whether the message's flagged state has changed
1411     * @param changeMailbox whether the message's mailbox has changed
1412     * @param oldMessage the message in it's pre-change state
1413     * @param newMessage the current version of the message
1414     */
1415    private void processPendingDataChange(Store remoteStore, Mailbox mailbox, boolean changeRead,
1416            boolean changeFlagged, boolean changeMailbox, EmailContent.Message oldMessage,
1417            EmailContent.Message newMessage) throws MessagingException {
1418        Mailbox newMailbox = null;;
1419
1420        // 0. No remote update if the message is local-only
1421        if (newMessage.mServerId == null || newMessage.mServerId.equals("")
1422                || newMessage.mServerId.startsWith(LOCAL_SERVERID_PREFIX) || (mailbox == null)) {
1423            return;
1424        }
1425
1426        // 0.5 If the mailbox has changed, use the original mailbox for operations
1427        // After any flag changes (which we execute in the original mailbox), we then
1428        // copy the message to the new mailbox
1429        if (changeMailbox) {
1430            newMailbox = mailbox;
1431            mailbox = Mailbox.restoreMailboxWithId(mContext, oldMessage.mMailboxKey);
1432        }
1433
1434        if (mailbox == null) {
1435            return;
1436        }
1437
1438        // 1. No remote update for DRAFTS or OUTBOX
1439        if (mailbox.mType == Mailbox.TYPE_DRAFTS || mailbox.mType == Mailbox.TYPE_OUTBOX) {
1440            return;
1441        }
1442
1443        // 2. Open the remote store & folder
1444        Folder remoteFolder = remoteStore.getFolder(mailbox.mDisplayName);
1445        if (!remoteFolder.exists()) {
1446            return;
1447        }
1448        remoteFolder.open(OpenMode.READ_WRITE, null);
1449        if (remoteFolder.getMode() != OpenMode.READ_WRITE) {
1450            return;
1451        }
1452
1453        // 3. Finally, apply the changes to the message
1454        Message remoteMessage = remoteFolder.getMessage(newMessage.mServerId);
1455        if (remoteMessage == null) {
1456            return;
1457        }
1458        if (Email.DEBUG) {
1459            Log.d(Logging.LOG_TAG,
1460                    "Update for msg id=" + newMessage.mId
1461                    + " read=" + newMessage.mFlagRead
1462                    + " flagged=" + newMessage.mFlagFavorite
1463                    + " new mailbox=" + newMessage.mMailboxKey);
1464        }
1465        Message[] messages = new Message[] { remoteMessage };
1466        if (changeRead) {
1467            remoteFolder.setFlags(messages, FLAG_LIST_SEEN, newMessage.mFlagRead);
1468        }
1469        if (changeFlagged) {
1470            remoteFolder.setFlags(messages, FLAG_LIST_FLAGGED, newMessage.mFlagFavorite);
1471        }
1472        if (changeMailbox) {
1473            Folder toFolder = remoteStore.getFolder(newMailbox.mDisplayName);
1474            if (!remoteFolder.exists()) {
1475                return;
1476            }
1477            // Copy the message to its new folder
1478            remoteFolder.copyMessages(messages, toFolder, null);
1479            // Delete the message from the remote source folder
1480            remoteMessage.setFlag(Flag.DELETED, true);
1481            remoteFolder.expunge();
1482            // Set the serverId to 0, since we don't know what the new server id will be
1483            ContentValues cv = new ContentValues();
1484            cv.put(EmailContent.Message.SERVER_ID, "0");
1485            mContext.getContentResolver().update(ContentUris.withAppendedId(
1486                    EmailContent.Message.CONTENT_URI, newMessage.mId), cv, null, null);
1487        }
1488        remoteFolder.close(false);
1489    }
1490
1491    /**
1492     * Process a pending trash message command.
1493     *
1494     * @param remoteStore the remote store we're working in
1495     * @param account The account in which we are working
1496     * @param newMailbox The local trash mailbox
1497     * @param oldMessage The message copy that was saved in the updates shadow table
1498     * @param newMessage The message that was moved to the mailbox
1499     */
1500    private void processPendingMoveToTrash(Store remoteStore,
1501            EmailContent.Account account, Mailbox newMailbox, EmailContent.Message oldMessage,
1502            final EmailContent.Message newMessage) throws MessagingException {
1503
1504        // 0. No remote move if the message is local-only
1505        if (newMessage.mServerId == null || newMessage.mServerId.equals("")
1506                || newMessage.mServerId.startsWith(LOCAL_SERVERID_PREFIX)) {
1507            return;
1508        }
1509
1510        // 1. Escape early if we can't find the local mailbox
1511        // TODO smaller projection here
1512        Mailbox oldMailbox = Mailbox.restoreMailboxWithId(mContext, oldMessage.mMailboxKey);
1513        if (oldMailbox == null) {
1514            // can't find old mailbox, it may have been deleted.  just return.
1515            return;
1516        }
1517        // 2. We don't support delete-from-trash here
1518        if (oldMailbox.mType == Mailbox.TYPE_TRASH) {
1519            return;
1520        }
1521
1522        // 3. If DELETE_POLICY_NEVER, simply write back the deleted sentinel and return
1523        //
1524        // This sentinel takes the place of the server-side message, and locally "deletes" it
1525        // by inhibiting future sync or display of the message.  It will eventually go out of
1526        // scope when it becomes old, or is deleted on the server, and the regular sync code
1527        // will clean it up for us.
1528        if (account.getDeletePolicy() == Account.DELETE_POLICY_NEVER) {
1529            EmailContent.Message sentinel = new EmailContent.Message();
1530            sentinel.mAccountKey = oldMessage.mAccountKey;
1531            sentinel.mMailboxKey = oldMessage.mMailboxKey;
1532            sentinel.mFlagLoaded = EmailContent.Message.FLAG_LOADED_DELETED;
1533            sentinel.mFlagRead = true;
1534            sentinel.mServerId = oldMessage.mServerId;
1535            sentinel.save(mContext);
1536
1537            return;
1538        }
1539
1540        // The rest of this method handles server-side deletion
1541
1542        // 4.  Find the remote mailbox (that we deleted from), and open it
1543        Folder remoteFolder = remoteStore.getFolder(oldMailbox.mDisplayName);
1544        if (!remoteFolder.exists()) {
1545            return;
1546        }
1547
1548        remoteFolder.open(OpenMode.READ_WRITE, null);
1549        if (remoteFolder.getMode() != OpenMode.READ_WRITE) {
1550            remoteFolder.close(false);
1551            return;
1552        }
1553
1554        // 5. Find the remote original message
1555        Message remoteMessage = remoteFolder.getMessage(oldMessage.mServerId);
1556        if (remoteMessage == null) {
1557            remoteFolder.close(false);
1558            return;
1559        }
1560
1561        // 6. Find the remote trash folder, and create it if not found
1562        Folder remoteTrashFolder = remoteStore.getFolder(newMailbox.mDisplayName);
1563        if (!remoteTrashFolder.exists()) {
1564            /*
1565             * If the remote trash folder doesn't exist we try to create it.
1566             */
1567            remoteTrashFolder.create(FolderType.HOLDS_MESSAGES);
1568        }
1569
1570        // 7.  Try to copy the message into the remote trash folder
1571        // Note, this entire section will be skipped for POP3 because there's no remote trash
1572        if (remoteTrashFolder.exists()) {
1573            /*
1574             * Because remoteTrashFolder may be new, we need to explicitly open it
1575             */
1576            remoteTrashFolder.open(OpenMode.READ_WRITE, null);
1577            if (remoteTrashFolder.getMode() != OpenMode.READ_WRITE) {
1578                remoteFolder.close(false);
1579                remoteTrashFolder.close(false);
1580                return;
1581            }
1582
1583            remoteFolder.copyMessages(new Message[] { remoteMessage }, remoteTrashFolder,
1584                    new Folder.MessageUpdateCallbacks() {
1585                public void onMessageUidChange(Message message, String newUid) {
1586                    // update the UID in the local trash folder, because some stores will
1587                    // have to change it when copying to remoteTrashFolder
1588                    ContentValues cv = new ContentValues();
1589                    cv.put(EmailContent.Message.SERVER_ID, newUid);
1590                    mContext.getContentResolver().update(newMessage.getUri(), cv, null, null);
1591                }
1592
1593                /**
1594                 * This will be called if the deleted message doesn't exist and can't be
1595                 * deleted (e.g. it was already deleted from the server.)  In this case,
1596                 * attempt to delete the local copy as well.
1597                 */
1598                public void onMessageNotFound(Message message) {
1599                    mContext.getContentResolver().delete(newMessage.getUri(), null, null);
1600                }
1601            });
1602            remoteTrashFolder.close(false);
1603        }
1604
1605        // 8. Delete the message from the remote source folder
1606        remoteMessage.setFlag(Flag.DELETED, true);
1607        remoteFolder.expunge();
1608        remoteFolder.close(false);
1609    }
1610
1611    /**
1612     * Process a pending trash message command.
1613     *
1614     * @param remoteStore the remote store we're working in
1615     * @param account The account in which we are working
1616     * @param oldMailbox The local trash mailbox
1617     * @param oldMessage The message that was deleted from the trash
1618     */
1619    private void processPendingDeleteFromTrash(Store remoteStore,
1620            EmailContent.Account account, Mailbox oldMailbox, EmailContent.Message oldMessage)
1621            throws MessagingException {
1622
1623        // 1. We only support delete-from-trash here
1624        if (oldMailbox.mType != Mailbox.TYPE_TRASH) {
1625            return;
1626        }
1627
1628        // 2.  Find the remote trash folder (that we are deleting from), and open it
1629        Folder remoteTrashFolder = remoteStore.getFolder(oldMailbox.mDisplayName);
1630        if (!remoteTrashFolder.exists()) {
1631            return;
1632        }
1633
1634        remoteTrashFolder.open(OpenMode.READ_WRITE, null);
1635        if (remoteTrashFolder.getMode() != OpenMode.READ_WRITE) {
1636            remoteTrashFolder.close(false);
1637            return;
1638        }
1639
1640        // 3. Find the remote original message
1641        Message remoteMessage = remoteTrashFolder.getMessage(oldMessage.mServerId);
1642        if (remoteMessage == null) {
1643            remoteTrashFolder.close(false);
1644            return;
1645        }
1646
1647        // 4. Delete the message from the remote trash folder
1648        remoteMessage.setFlag(Flag.DELETED, true);
1649        remoteTrashFolder.expunge();
1650        remoteTrashFolder.close(false);
1651    }
1652
1653    /**
1654     * Process a pending append message command. This command uploads a local message to the
1655     * server, first checking to be sure that the server message is not newer than
1656     * the local message.
1657     *
1658     * @param remoteStore the remote store we're working in
1659     * @param account The account in which we are working
1660     * @param newMailbox The mailbox we're appending to
1661     * @param message The message we're appending
1662     * @return true if successfully uploaded
1663     */
1664    private boolean processPendingAppend(Store remoteStore, EmailContent.Account account,
1665            Mailbox newMailbox, EmailContent.Message message)
1666            throws MessagingException {
1667
1668        boolean updateInternalDate = false;
1669        boolean updateMessage = false;
1670        boolean deleteMessage = false;
1671
1672        // 1. Find the remote folder that we're appending to and create and/or open it
1673        Folder remoteFolder = remoteStore.getFolder(newMailbox.mDisplayName);
1674        if (!remoteFolder.exists()) {
1675            if (!remoteFolder.canCreate(FolderType.HOLDS_MESSAGES)) {
1676                // This is POP3, we cannot actually upload.  Instead, we'll update the message
1677                // locally with a fake serverId (so we don't keep trying here) and return.
1678                if (message.mServerId == null || message.mServerId.length() == 0) {
1679                    message.mServerId = LOCAL_SERVERID_PREFIX + message.mId;
1680                    Uri uri =
1681                        ContentUris.withAppendedId(EmailContent.Message.CONTENT_URI, message.mId);
1682                    ContentValues cv = new ContentValues();
1683                    cv.put(EmailContent.Message.SERVER_ID, message.mServerId);
1684                    mContext.getContentResolver().update(uri, cv, null, null);
1685                }
1686                return true;
1687            }
1688            if (!remoteFolder.create(FolderType.HOLDS_MESSAGES)) {
1689                // This is a (hopefully) transient error and we return false to try again later
1690                return false;
1691            }
1692        }
1693        remoteFolder.open(OpenMode.READ_WRITE, null);
1694        if (remoteFolder.getMode() != OpenMode.READ_WRITE) {
1695            return false;
1696        }
1697
1698        // 2. If possible, load a remote message with the matching UID
1699        Message remoteMessage = null;
1700        if (message.mServerId != null && message.mServerId.length() > 0) {
1701            remoteMessage = remoteFolder.getMessage(message.mServerId);
1702        }
1703
1704        // 3. If a remote message could not be found, upload our local message
1705        if (remoteMessage == null) {
1706            // 3a. Create a legacy message to upload
1707            Message localMessage = LegacyConversions.makeMessage(mContext, message);
1708
1709            // 3b. Upload it
1710            FetchProfile fp = new FetchProfile();
1711            fp.add(FetchProfile.Item.BODY);
1712            remoteFolder.appendMessages(new Message[] { localMessage });
1713
1714            // 3b. And record the UID from the server
1715            message.mServerId = localMessage.getUid();
1716            updateInternalDate = true;
1717            updateMessage = true;
1718        } else {
1719            // 4. If the remote message exists we need to determine which copy to keep.
1720            FetchProfile fp = new FetchProfile();
1721            fp.add(FetchProfile.Item.ENVELOPE);
1722            remoteFolder.fetch(new Message[] { remoteMessage }, fp, null);
1723            Date localDate = new Date(message.mServerTimeStamp);
1724            Date remoteDate = remoteMessage.getInternalDate();
1725            if (remoteDate != null && remoteDate.compareTo(localDate) > 0) {
1726                // 4a. If the remote message is newer than ours we'll just
1727                // delete ours and move on. A sync will get the server message
1728                // if we need to be able to see it.
1729                deleteMessage = true;
1730            } else {
1731                // 4b. Otherwise we'll upload our message and then delete the remote message.
1732
1733                // Create a legacy message to upload
1734                Message localMessage = LegacyConversions.makeMessage(mContext, message);
1735
1736                // 4c. Upload it
1737                fp.clear();
1738                fp = new FetchProfile();
1739                fp.add(FetchProfile.Item.BODY);
1740                remoteFolder.appendMessages(new Message[] { localMessage });
1741
1742                // 4d. Record the UID and new internalDate from the server
1743                message.mServerId = localMessage.getUid();
1744                updateInternalDate = true;
1745                updateMessage = true;
1746
1747                // 4e. And delete the old copy of the message from the server
1748                remoteMessage.setFlag(Flag.DELETED, true);
1749            }
1750        }
1751
1752        // 5. If requested, Best-effort to capture new "internaldate" from the server
1753        if (updateInternalDate && message.mServerId != null) {
1754            try {
1755                Message remoteMessage2 = remoteFolder.getMessage(message.mServerId);
1756                if (remoteMessage2 != null) {
1757                    FetchProfile fp2 = new FetchProfile();
1758                    fp2.add(FetchProfile.Item.ENVELOPE);
1759                    remoteFolder.fetch(new Message[] { remoteMessage2 }, fp2, null);
1760                    message.mServerTimeStamp = remoteMessage2.getInternalDate().getTime();
1761                    updateMessage = true;
1762                }
1763            } catch (MessagingException me) {
1764                // skip it - we can live without this
1765            }
1766        }
1767
1768        // 6. Perform required edits to local copy of message
1769        if (deleteMessage || updateMessage) {
1770            Uri uri = ContentUris.withAppendedId(EmailContent.Message.CONTENT_URI, message.mId);
1771            ContentResolver resolver = mContext.getContentResolver();
1772            if (deleteMessage) {
1773                resolver.delete(uri, null, null);
1774            } else if (updateMessage) {
1775                ContentValues cv = new ContentValues();
1776                cv.put(EmailContent.Message.SERVER_ID, message.mServerId);
1777                cv.put(EmailContent.Message.SERVER_TIMESTAMP, message.mServerTimeStamp);
1778                resolver.update(uri, cv, null, null);
1779            }
1780        }
1781
1782        return true;
1783    }
1784
1785    /**
1786     * Finish loading a message that have been partially downloaded.
1787     *
1788     * @param messageId the message to load
1789     * @param listener the callback by which results will be reported
1790     */
1791    public void loadMessageForView(final long messageId, MessagingListener listener) {
1792        mListeners.loadMessageForViewStarted(messageId);
1793        put("loadMessageForViewRemote", listener, new Runnable() {
1794            public void run() {
1795                try {
1796                    // 1. Resample the message, in case it disappeared or synced while
1797                    // this command was in queue
1798                    EmailContent.Message message =
1799                        EmailContent.Message.restoreMessageWithId(mContext, messageId);
1800                    if (message == null) {
1801                        mListeners.loadMessageForViewFailed(messageId, "Unknown message");
1802                        return;
1803                    }
1804                    if (message.mFlagLoaded == EmailContent.Message.FLAG_LOADED_COMPLETE) {
1805                        mListeners.loadMessageForViewFinished(messageId);
1806                        return;
1807                    }
1808
1809                    // 2. Open the remote folder.
1810                    // TODO all of these could be narrower projections
1811                    // TODO combine with common code in loadAttachment
1812                    EmailContent.Account account =
1813                        EmailContent.Account.restoreAccountWithId(mContext, message.mAccountKey);
1814                    EmailContent.Mailbox mailbox =
1815                        EmailContent.Mailbox.restoreMailboxWithId(mContext, message.mMailboxKey);
1816                    if (account == null || mailbox == null) {
1817                        mListeners.loadMessageForViewFailed(messageId, "null account or mailbox");
1818                        return;
1819                    }
1820
1821                    Store remoteStore =
1822                        Store.getInstance(account.getStoreUri(mContext), mContext, null);
1823                    Folder remoteFolder = remoteStore.getFolder(mailbox.mDisplayName);
1824                    remoteFolder.open(OpenMode.READ_WRITE, null);
1825
1826                    // 3. Not supported, because IMAP & POP don't use it: structure prefetch
1827//                  if (remoteStore.requireStructurePrefetch()) {
1828//                  // For remote stores that require it, prefetch the message structure.
1829//                  FetchProfile fp = new FetchProfile();
1830//                  fp.add(FetchProfile.Item.STRUCTURE);
1831//                  localFolder.fetch(new Message[] { message }, fp, null);
1832//
1833//                  ArrayList<Part> viewables = new ArrayList<Part>();
1834//                  ArrayList<Part> attachments = new ArrayList<Part>();
1835//                  MimeUtility.collectParts(message, viewables, attachments);
1836//                  fp.clear();
1837//                  for (Part part : viewables) {
1838//                      fp.add(part);
1839//                  }
1840//
1841//                  remoteFolder.fetch(new Message[] { message }, fp, null);
1842//
1843//                  // Store the updated message locally
1844//                  localFolder.updateMessage((LocalMessage)message);
1845
1846                    // 4. Set up to download the entire message
1847                    Message remoteMessage = remoteFolder.getMessage(message.mServerId);
1848                    FetchProfile fp = new FetchProfile();
1849                    fp.add(FetchProfile.Item.BODY);
1850                    remoteFolder.fetch(new Message[] { remoteMessage }, fp, null);
1851
1852                    // 5. Write to provider
1853                    copyOneMessageToProvider(remoteMessage, account, mailbox,
1854                            EmailContent.Message.FLAG_LOADED_COMPLETE);
1855
1856                    // 6. Notify UI
1857                    mListeners.loadMessageForViewFinished(messageId);
1858
1859                } catch (MessagingException me) {
1860                    if (Email.LOGD) Log.v(Logging.LOG_TAG, "", me);
1861                    mListeners.loadMessageForViewFailed(messageId, me.getMessage());
1862                } catch (RuntimeException rte) {
1863                    mListeners.loadMessageForViewFailed(messageId, rte.getMessage());
1864                }
1865            }
1866        });
1867    }
1868
1869    /**
1870     * Attempts to load the attachment specified by id from the given account and message.
1871     * @param account
1872     * @param message
1873     * @param part
1874     * @param listener
1875     */
1876    public void loadAttachment(final long accountId, final long messageId, final long mailboxId,
1877            final long attachmentId, MessagingListener listener, final boolean background) {
1878        mListeners.loadAttachmentStarted(accountId, messageId, attachmentId, true);
1879
1880        put("loadAttachment", listener, new Runnable() {
1881            public void run() {
1882                try {
1883                    //1. Check if the attachment is already here and return early in that case
1884                    Attachment attachment =
1885                        Attachment.restoreAttachmentWithId(mContext, attachmentId);
1886                    if (attachment == null) {
1887                        mListeners.loadAttachmentFailed(accountId, messageId, attachmentId,
1888                                   new MessagingException("The attachment is null"),
1889                                   background);
1890                        return;
1891                    }
1892                    if (Utility.attachmentExists(mContext, attachment)) {
1893                        mListeners.loadAttachmentFinished(accountId, messageId, attachmentId);
1894                        return;
1895                    }
1896
1897                    // 2. Open the remote folder.
1898                    // TODO all of these could be narrower projections
1899                    EmailContent.Account account =
1900                        EmailContent.Account.restoreAccountWithId(mContext, accountId);
1901                    EmailContent.Mailbox mailbox =
1902                        EmailContent.Mailbox.restoreMailboxWithId(mContext, mailboxId);
1903                    EmailContent.Message message =
1904                        EmailContent.Message.restoreMessageWithId(mContext, messageId);
1905
1906                    if (account == null || mailbox == null || message == null) {
1907                        mListeners.loadAttachmentFailed(accountId, messageId, attachmentId,
1908                                new MessagingException(
1909                                        "Account, mailbox, message or attachment are null"),
1910                                background);
1911                        return;
1912                    }
1913
1914                    Store remoteStore =
1915                        Store.getInstance(account.getStoreUri(mContext), mContext, null);
1916                    Folder remoteFolder = remoteStore.getFolder(mailbox.mDisplayName);
1917                    remoteFolder.open(OpenMode.READ_WRITE, null);
1918
1919                    // 3. Generate a shell message in which to retrieve the attachment,
1920                    // and a shell BodyPart for the attachment.  Then glue them together.
1921                    Message storeMessage = remoteFolder.createMessage(message.mServerId);
1922                    MimeBodyPart storePart = new MimeBodyPart();
1923                    storePart.setSize((int)attachment.mSize);
1924                    storePart.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA,
1925                            attachment.mLocation);
1926                    storePart.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
1927                            String.format("%s;\n name=\"%s\"",
1928                            attachment.mMimeType,
1929                            attachment.mFileName));
1930                    // TODO is this always true for attachments?  I think we dropped the
1931                    // true encoding along the way
1932                    storePart.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
1933
1934                    MimeMultipart multipart = new MimeMultipart();
1935                    multipart.setSubType("mixed");
1936                    multipart.addBodyPart(storePart);
1937
1938                    storeMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "multipart/mixed");
1939                    storeMessage.setBody(multipart);
1940
1941                    // 4. Now ask for the attachment to be fetched
1942                    FetchProfile fp = new FetchProfile();
1943                    fp.add(storePart);
1944                    remoteFolder.fetch(new Message[] { storeMessage }, fp,
1945                            mController.new MessageRetrievalListenerBridge(
1946                                    messageId, attachmentId));
1947
1948                    // If we failed to load the attachment, throw an Exception here, so that
1949                    // AttachmentDownloadService knows that we failed
1950                    if (storePart.getBody() == null) {
1951                        throw new MessagingException("Attachment not loaded.");
1952                    }
1953
1954                    // 5. Save the downloaded file and update the attachment as necessary
1955                    LegacyConversions.saveAttachmentBody(mContext, storePart, attachment,
1956                            accountId);
1957
1958                    // 6. Report success
1959                    mListeners.loadAttachmentFinished(accountId, messageId, attachmentId);
1960                }
1961                catch (MessagingException me) {
1962                    if (Email.LOGD) Log.v(Logging.LOG_TAG, "", me);
1963                    mListeners.loadAttachmentFailed(
1964                            accountId, messageId, attachmentId, me, background);
1965                } catch (IOException ioe) {
1966                    Log.e(Logging.LOG_TAG, "Error while storing attachment." + ioe.toString());
1967                }
1968            }});
1969    }
1970
1971    /**
1972     * Attempt to send any messages that are sitting in the Outbox.
1973     * @param account
1974     * @param listener
1975     */
1976    public void sendPendingMessages(final EmailContent.Account account, final long sentFolderId,
1977            MessagingListener listener) {
1978        put("sendPendingMessages", listener, new Runnable() {
1979            public void run() {
1980                sendPendingMessagesSynchronous(account, sentFolderId);
1981            }
1982        });
1983    }
1984
1985    /**
1986     * Attempt to send any messages that are sitting in the Outbox.
1987     *
1988     * @param account
1989     * @param listener
1990     */
1991    public void sendPendingMessagesSynchronous(final EmailContent.Account account,
1992            long sentFolderId) {
1993        NotificationController nc = NotificationController.getInstance(mContext);
1994        // 1.  Loop through all messages in the account's outbox
1995        long outboxId = Mailbox.findMailboxOfType(mContext, account.mId, Mailbox.TYPE_OUTBOX);
1996        if (outboxId == Mailbox.NO_MAILBOX) {
1997            return;
1998        }
1999        ContentResolver resolver = mContext.getContentResolver();
2000        Cursor c = resolver.query(EmailContent.Message.CONTENT_URI,
2001                EmailContent.Message.ID_COLUMN_PROJECTION,
2002                EmailContent.Message.MAILBOX_KEY + "=?", new String[] { Long.toString(outboxId) },
2003                null);
2004        try {
2005            // 2.  exit early
2006            if (c.getCount() <= 0) {
2007                return;
2008            }
2009            // 3. do one-time setup of the Sender & other stuff
2010            mListeners.sendPendingMessagesStarted(account.mId, -1);
2011
2012            Sender sender = Sender.getInstance(mContext, account.getSenderUri(mContext));
2013            Store remoteStore = Store.getInstance(account.getStoreUri(mContext), mContext, null);
2014            boolean requireMoveMessageToSentFolder = remoteStore.requireCopyMessageToSentFolder();
2015            ContentValues moveToSentValues = null;
2016            if (requireMoveMessageToSentFolder) {
2017                moveToSentValues = new ContentValues();
2018                moveToSentValues.put(MessageColumns.MAILBOX_KEY, sentFolderId);
2019            }
2020
2021            // 4.  loop through the available messages and send them
2022            while (c.moveToNext()) {
2023                long messageId = -1;
2024                try {
2025                    messageId = c.getLong(0);
2026                    mListeners.sendPendingMessagesStarted(account.mId, messageId);
2027                    // Don't send messages with unloaded attachments
2028                    if (Utility.hasUnloadedAttachments(mContext, messageId)) {
2029                        if (Email.DEBUG) {
2030                            Log.d(Logging.LOG_TAG, "Can't send #" + messageId +
2031                                    "; unloaded attachments");
2032                        }
2033                        continue;
2034                    }
2035                    sender.sendMessage(messageId);
2036                } catch (MessagingException me) {
2037                    // report error for this message, but keep trying others
2038                    if (me instanceof AuthenticationFailedException) {
2039                        nc.showLoginFailedNotification(account.mId);
2040                    }
2041                    mListeners.sendPendingMessagesFailed(account.mId, messageId, me);
2042                    continue;
2043                }
2044                // 5. move to sent, or delete
2045                Uri syncedUri =
2046                    ContentUris.withAppendedId(EmailContent.Message.SYNCED_CONTENT_URI, messageId);
2047                if (requireMoveMessageToSentFolder) {
2048                    // If this is a forwarded message and it has attachments, delete them, as they
2049                    // duplicate information found elsewhere (on the server).  This saves storage.
2050                    EmailContent.Message msg =
2051                        EmailContent.Message.restoreMessageWithId(mContext, messageId);
2052                    if (msg != null &&
2053                            ((msg.mFlags & EmailContent.Message.FLAG_TYPE_FORWARD) != 0)) {
2054                        AttachmentUtilities.deleteAllAttachmentFiles(mContext, account.mId,
2055                                messageId);
2056                    }
2057                    resolver.update(syncedUri, moveToSentValues, null, null);
2058                } else {
2059                    AttachmentUtilities.deleteAllAttachmentFiles(mContext, account.mId,
2060                            messageId);
2061                    Uri uri =
2062                        ContentUris.withAppendedId(EmailContent.Message.CONTENT_URI, messageId);
2063                    resolver.delete(uri, null, null);
2064                    resolver.delete(syncedUri, null, null);
2065                }
2066            }
2067            // 6. report completion/success
2068            mListeners.sendPendingMessagesCompleted(account.mId);
2069            nc.cancelLoginFailedNotification(account.mId);
2070        } catch (MessagingException me) {
2071            if (me instanceof AuthenticationFailedException) {
2072                nc.showLoginFailedNotification(account.mId);
2073            }
2074            mListeners.sendPendingMessagesFailed(account.mId, -1, me);
2075        } finally {
2076            c.close();
2077        }
2078    }
2079
2080    /**
2081     * Checks mail for an account.
2082     * This entry point is for use by the mail checking service only, because it
2083     * gives slightly different callbacks (so the service doesn't get confused by callbacks
2084     * triggered by/for the foreground UI.
2085     *
2086     * TODO clean up the execution model which is unnecessarily threaded due to legacy code
2087     *
2088     * @param accountId the account to check
2089     * @param listener
2090     */
2091    public void checkMail(final long accountId, final long tag, final MessagingListener listener) {
2092        mListeners.checkMailStarted(mContext, accountId, tag);
2093
2094        // This puts the command on the queue (not synchronous)
2095        listFolders(accountId, null);
2096
2097        // Put this on the queue as well so it follows listFolders
2098        put("checkMail", listener, new Runnable() {
2099            public void run() {
2100                // send any pending outbound messages.  note, there is a slight race condition
2101                // here if we somehow don't have a sent folder, but this should never happen
2102                // because the call to sendMessage() would have built one previously.
2103                long inboxId = -1;
2104                EmailContent.Account account =
2105                    EmailContent.Account.restoreAccountWithId(mContext, accountId);
2106                if (account != null) {
2107                    long sentboxId = Mailbox.findMailboxOfType(mContext, accountId,
2108                            Mailbox.TYPE_SENT);
2109                    if (sentboxId != Mailbox.NO_MAILBOX) {
2110                        sendPendingMessagesSynchronous(account, sentboxId);
2111                    }
2112                    // find mailbox # for inbox and sync it.
2113                    // TODO we already know this in Controller, can we pass it in?
2114                    inboxId = Mailbox.findMailboxOfType(mContext, accountId, Mailbox.TYPE_INBOX);
2115                    if (inboxId != Mailbox.NO_MAILBOX) {
2116                        EmailContent.Mailbox mailbox =
2117                            EmailContent.Mailbox.restoreMailboxWithId(mContext, inboxId);
2118                        if (mailbox != null) {
2119                            synchronizeMailboxSynchronous(account, mailbox);
2120                        }
2121                    }
2122                }
2123                mListeners.checkMailFinished(mContext, accountId, inboxId, tag);
2124            }
2125        });
2126    }
2127
2128    private static class Command {
2129        public Runnable runnable;
2130
2131        public MessagingListener listener;
2132
2133        public String description;
2134
2135        @Override
2136        public String toString() {
2137            return description;
2138        }
2139    }
2140}
2141