MessagingController.java revision c17e9ce85f98d143e950b63aa9e5fd57d31dcbe5
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 newMessage =
1379            EmailContent.Message.restoreMessageWithId(mContext, messageId);
1380        boolean deleteUpdate = false;
1381        if (newMessage == 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 if (newMessage != null && newMessage.mMailboxKey != mailbox.mId) {
1394            deleteUpdate = false;
1395            Log.d(Logging.LOG_TAG, "Upsync skipped; mailbox changed, id=" + messageId);
1396        } else {
1397            Log.d(Logging.LOG_TAG, "Upsyc triggered for message id=" + messageId);
1398            deleteUpdate = processPendingAppend(remoteStore, account, mailbox, newMessage);
1399        }
1400        if (deleteUpdate) {
1401            // Finally, delete the update (if any)
1402            Uri uri = ContentUris.withAppendedId(EmailContent.Message.UPDATED_CONTENT_URI, messageId);
1403            resolver.delete(uri, null, null);
1404        }
1405    }
1406
1407    /**
1408     * Upsync changes to read, flagged, or mailbox
1409     *
1410     * @param remoteStore the remote store for this mailbox
1411     * @param mailbox the mailbox the message is stored in
1412     * @param changeRead whether the message's read state has changed
1413     * @param changeFlagged whether the message's flagged state has changed
1414     * @param changeMailbox whether the message's mailbox has changed
1415     * @param oldMessage the message in it's pre-change state
1416     * @param newMessage the current version of the message
1417     */
1418    private void processPendingDataChange(Store remoteStore, Mailbox mailbox, boolean changeRead,
1419            boolean changeFlagged, boolean changeMailbox, EmailContent.Message oldMessage,
1420            EmailContent.Message newMessage) throws MessagingException {
1421        Mailbox newMailbox = null;;
1422
1423        // 0. No remote update if the message is local-only
1424        if (newMessage.mServerId == null || newMessage.mServerId.equals("")
1425                || newMessage.mServerId.startsWith(LOCAL_SERVERID_PREFIX) || (mailbox == null)) {
1426            return;
1427        }
1428
1429        // 0.5 If the mailbox has changed, use the original mailbox for operations
1430        // After any flag changes (which we execute in the original mailbox), we then
1431        // copy the message to the new mailbox
1432        if (changeMailbox) {
1433            newMailbox = mailbox;
1434            mailbox = Mailbox.restoreMailboxWithId(mContext, oldMessage.mMailboxKey);
1435        }
1436
1437        if (mailbox == null) {
1438            return;
1439        }
1440
1441        // 1. No remote update for DRAFTS or OUTBOX
1442        if (mailbox.mType == Mailbox.TYPE_DRAFTS || mailbox.mType == Mailbox.TYPE_OUTBOX) {
1443            return;
1444        }
1445
1446        // 2. Open the remote store & folder
1447        Folder remoteFolder = remoteStore.getFolder(mailbox.mDisplayName);
1448        if (!remoteFolder.exists()) {
1449            return;
1450        }
1451        remoteFolder.open(OpenMode.READ_WRITE, null);
1452        if (remoteFolder.getMode() != OpenMode.READ_WRITE) {
1453            return;
1454        }
1455
1456        // 3. Finally, apply the changes to the message
1457        Message remoteMessage = remoteFolder.getMessage(newMessage.mServerId);
1458        if (remoteMessage == null) {
1459            return;
1460        }
1461        if (Email.DEBUG) {
1462            Log.d(Logging.LOG_TAG,
1463                    "Update for msg id=" + newMessage.mId
1464                    + " read=" + newMessage.mFlagRead
1465                    + " flagged=" + newMessage.mFlagFavorite
1466                    + " new mailbox=" + newMessage.mMailboxKey);
1467        }
1468        Message[] messages = new Message[] { remoteMessage };
1469        if (changeRead) {
1470            remoteFolder.setFlags(messages, FLAG_LIST_SEEN, newMessage.mFlagRead);
1471        }
1472        if (changeFlagged) {
1473            remoteFolder.setFlags(messages, FLAG_LIST_FLAGGED, newMessage.mFlagFavorite);
1474        }
1475        if (changeMailbox) {
1476            Folder toFolder = remoteStore.getFolder(newMailbox.mDisplayName);
1477            if (!remoteFolder.exists()) {
1478                return;
1479            }
1480            // Copy the message to its new folder
1481            remoteFolder.copyMessages(messages, toFolder, null);
1482            // Delete the message from the remote source folder
1483            remoteMessage.setFlag(Flag.DELETED, true);
1484            remoteFolder.expunge();
1485            // Set the serverId to 0, since we don't know what the new server id will be
1486            ContentValues cv = new ContentValues();
1487            cv.put(EmailContent.Message.SERVER_ID, "0");
1488            mContext.getContentResolver().update(ContentUris.withAppendedId(
1489                    EmailContent.Message.CONTENT_URI, newMessage.mId), cv, null, null);
1490        }
1491        remoteFolder.close(false);
1492    }
1493
1494    /**
1495     * Process a pending trash message command.
1496     *
1497     * @param remoteStore the remote store we're working in
1498     * @param account The account in which we are working
1499     * @param newMailbox The local trash mailbox
1500     * @param oldMessage The message copy that was saved in the updates shadow table
1501     * @param newMessage The message that was moved to the mailbox
1502     */
1503    private void processPendingMoveToTrash(Store remoteStore,
1504            EmailContent.Account account, Mailbox newMailbox, EmailContent.Message oldMessage,
1505            final EmailContent.Message newMessage) throws MessagingException {
1506
1507        // 0. No remote move if the message is local-only
1508        if (newMessage.mServerId == null || newMessage.mServerId.equals("")
1509                || newMessage.mServerId.startsWith(LOCAL_SERVERID_PREFIX)) {
1510            return;
1511        }
1512
1513        // 1. Escape early if we can't find the local mailbox
1514        // TODO smaller projection here
1515        Mailbox oldMailbox = Mailbox.restoreMailboxWithId(mContext, oldMessage.mMailboxKey);
1516        if (oldMailbox == null) {
1517            // can't find old mailbox, it may have been deleted.  just return.
1518            return;
1519        }
1520        // 2. We don't support delete-from-trash here
1521        if (oldMailbox.mType == Mailbox.TYPE_TRASH) {
1522            return;
1523        }
1524
1525        // 3. If DELETE_POLICY_NEVER, simply write back the deleted sentinel and return
1526        //
1527        // This sentinel takes the place of the server-side message, and locally "deletes" it
1528        // by inhibiting future sync or display of the message.  It will eventually go out of
1529        // scope when it becomes old, or is deleted on the server, and the regular sync code
1530        // will clean it up for us.
1531        if (account.getDeletePolicy() == Account.DELETE_POLICY_NEVER) {
1532            EmailContent.Message sentinel = new EmailContent.Message();
1533            sentinel.mAccountKey = oldMessage.mAccountKey;
1534            sentinel.mMailboxKey = oldMessage.mMailboxKey;
1535            sentinel.mFlagLoaded = EmailContent.Message.FLAG_LOADED_DELETED;
1536            sentinel.mFlagRead = true;
1537            sentinel.mServerId = oldMessage.mServerId;
1538            sentinel.save(mContext);
1539
1540            return;
1541        }
1542
1543        // The rest of this method handles server-side deletion
1544
1545        // 4.  Find the remote mailbox (that we deleted from), and open it
1546        Folder remoteFolder = remoteStore.getFolder(oldMailbox.mDisplayName);
1547        if (!remoteFolder.exists()) {
1548            return;
1549        }
1550
1551        remoteFolder.open(OpenMode.READ_WRITE, null);
1552        if (remoteFolder.getMode() != OpenMode.READ_WRITE) {
1553            remoteFolder.close(false);
1554            return;
1555        }
1556
1557        // 5. Find the remote original message
1558        Message remoteMessage = remoteFolder.getMessage(oldMessage.mServerId);
1559        if (remoteMessage == null) {
1560            remoteFolder.close(false);
1561            return;
1562        }
1563
1564        // 6. Find the remote trash folder, and create it if not found
1565        Folder remoteTrashFolder = remoteStore.getFolder(newMailbox.mDisplayName);
1566        if (!remoteTrashFolder.exists()) {
1567            /*
1568             * If the remote trash folder doesn't exist we try to create it.
1569             */
1570            remoteTrashFolder.create(FolderType.HOLDS_MESSAGES);
1571        }
1572
1573        // 7.  Try to copy the message into the remote trash folder
1574        // Note, this entire section will be skipped for POP3 because there's no remote trash
1575        if (remoteTrashFolder.exists()) {
1576            /*
1577             * Because remoteTrashFolder may be new, we need to explicitly open it
1578             */
1579            remoteTrashFolder.open(OpenMode.READ_WRITE, null);
1580            if (remoteTrashFolder.getMode() != OpenMode.READ_WRITE) {
1581                remoteFolder.close(false);
1582                remoteTrashFolder.close(false);
1583                return;
1584            }
1585
1586            remoteFolder.copyMessages(new Message[] { remoteMessage }, remoteTrashFolder,
1587                    new Folder.MessageUpdateCallbacks() {
1588                public void onMessageUidChange(Message message, String newUid) {
1589                    // update the UID in the local trash folder, because some stores will
1590                    // have to change it when copying to remoteTrashFolder
1591                    ContentValues cv = new ContentValues();
1592                    cv.put(EmailContent.Message.SERVER_ID, newUid);
1593                    mContext.getContentResolver().update(newMessage.getUri(), cv, null, null);
1594                }
1595
1596                /**
1597                 * This will be called if the deleted message doesn't exist and can't be
1598                 * deleted (e.g. it was already deleted from the server.)  In this case,
1599                 * attempt to delete the local copy as well.
1600                 */
1601                public void onMessageNotFound(Message message) {
1602                    mContext.getContentResolver().delete(newMessage.getUri(), null, null);
1603                }
1604            });
1605            remoteTrashFolder.close(false);
1606        }
1607
1608        // 8. Delete the message from the remote source folder
1609        remoteMessage.setFlag(Flag.DELETED, true);
1610        remoteFolder.expunge();
1611        remoteFolder.close(false);
1612    }
1613
1614    /**
1615     * Process a pending trash message command.
1616     *
1617     * @param remoteStore the remote store we're working in
1618     * @param account The account in which we are working
1619     * @param oldMailbox The local trash mailbox
1620     * @param oldMessage The message that was deleted from the trash
1621     */
1622    private void processPendingDeleteFromTrash(Store remoteStore,
1623            EmailContent.Account account, Mailbox oldMailbox, EmailContent.Message oldMessage)
1624            throws MessagingException {
1625
1626        // 1. We only support delete-from-trash here
1627        if (oldMailbox.mType != Mailbox.TYPE_TRASH) {
1628            return;
1629        }
1630
1631        // 2.  Find the remote trash folder (that we are deleting from), and open it
1632        Folder remoteTrashFolder = remoteStore.getFolder(oldMailbox.mDisplayName);
1633        if (!remoteTrashFolder.exists()) {
1634            return;
1635        }
1636
1637        remoteTrashFolder.open(OpenMode.READ_WRITE, null);
1638        if (remoteTrashFolder.getMode() != OpenMode.READ_WRITE) {
1639            remoteTrashFolder.close(false);
1640            return;
1641        }
1642
1643        // 3. Find the remote original message
1644        Message remoteMessage = remoteTrashFolder.getMessage(oldMessage.mServerId);
1645        if (remoteMessage == null) {
1646            remoteTrashFolder.close(false);
1647            return;
1648        }
1649
1650        // 4. Delete the message from the remote trash folder
1651        remoteMessage.setFlag(Flag.DELETED, true);
1652        remoteTrashFolder.expunge();
1653        remoteTrashFolder.close(false);
1654    }
1655
1656    /**
1657     * Process a pending append message command. This command uploads a local message to the
1658     * server, first checking to be sure that the server message is not newer than
1659     * the local message.
1660     *
1661     * @param remoteStore the remote store we're working in
1662     * @param account The account in which we are working
1663     * @param newMailbox The mailbox we're appending to
1664     * @param message The message we're appending
1665     * @return true if successfully uploaded
1666     */
1667    private boolean processPendingAppend(Store remoteStore, EmailContent.Account account,
1668            Mailbox newMailbox, EmailContent.Message message)
1669            throws MessagingException {
1670
1671        boolean updateInternalDate = false;
1672        boolean updateMessage = false;
1673        boolean deleteMessage = false;
1674
1675        // 1. Find the remote folder that we're appending to and create and/or open it
1676        Folder remoteFolder = remoteStore.getFolder(newMailbox.mDisplayName);
1677        if (!remoteFolder.exists()) {
1678            if (!remoteFolder.canCreate(FolderType.HOLDS_MESSAGES)) {
1679                // This is POP3, we cannot actually upload.  Instead, we'll update the message
1680                // locally with a fake serverId (so we don't keep trying here) and return.
1681                if (message.mServerId == null || message.mServerId.length() == 0) {
1682                    message.mServerId = LOCAL_SERVERID_PREFIX + message.mId;
1683                    Uri uri =
1684                        ContentUris.withAppendedId(EmailContent.Message.CONTENT_URI, message.mId);
1685                    ContentValues cv = new ContentValues();
1686                    cv.put(EmailContent.Message.SERVER_ID, message.mServerId);
1687                    mContext.getContentResolver().update(uri, cv, null, null);
1688                }
1689                return true;
1690            }
1691            if (!remoteFolder.create(FolderType.HOLDS_MESSAGES)) {
1692                // This is a (hopefully) transient error and we return false to try again later
1693                return false;
1694            }
1695        }
1696        remoteFolder.open(OpenMode.READ_WRITE, null);
1697        if (remoteFolder.getMode() != OpenMode.READ_WRITE) {
1698            return false;
1699        }
1700
1701        // 2. If possible, load a remote message with the matching UID
1702        Message remoteMessage = null;
1703        if (message.mServerId != null && message.mServerId.length() > 0) {
1704            remoteMessage = remoteFolder.getMessage(message.mServerId);
1705        }
1706
1707        // 3. If a remote message could not be found, upload our local message
1708        if (remoteMessage == null) {
1709            // 3a. Create a legacy message to upload
1710            Message localMessage = LegacyConversions.makeMessage(mContext, message);
1711
1712            // 3b. Upload it
1713            FetchProfile fp = new FetchProfile();
1714            fp.add(FetchProfile.Item.BODY);
1715            remoteFolder.appendMessages(new Message[] { localMessage });
1716
1717            // 3b. And record the UID from the server
1718            message.mServerId = localMessage.getUid();
1719            updateInternalDate = true;
1720            updateMessage = true;
1721        } else {
1722            // 4. If the remote message exists we need to determine which copy to keep.
1723            FetchProfile fp = new FetchProfile();
1724            fp.add(FetchProfile.Item.ENVELOPE);
1725            remoteFolder.fetch(new Message[] { remoteMessage }, fp, null);
1726            Date localDate = new Date(message.mServerTimeStamp);
1727            Date remoteDate = remoteMessage.getInternalDate();
1728            if (remoteDate != null && remoteDate.compareTo(localDate) > 0) {
1729                // 4a. If the remote message is newer than ours we'll just
1730                // delete ours and move on. A sync will get the server message
1731                // if we need to be able to see it.
1732                deleteMessage = true;
1733            } else {
1734                // 4b. Otherwise we'll upload our message and then delete the remote message.
1735
1736                // Create a legacy message to upload
1737                Message localMessage = LegacyConversions.makeMessage(mContext, message);
1738
1739                // 4c. Upload it
1740                fp.clear();
1741                fp = new FetchProfile();
1742                fp.add(FetchProfile.Item.BODY);
1743                remoteFolder.appendMessages(new Message[] { localMessage });
1744
1745                // 4d. Record the UID and new internalDate from the server
1746                message.mServerId = localMessage.getUid();
1747                updateInternalDate = true;
1748                updateMessage = true;
1749
1750                // 4e. And delete the old copy of the message from the server
1751                remoteMessage.setFlag(Flag.DELETED, true);
1752            }
1753        }
1754
1755        // 5. If requested, Best-effort to capture new "internaldate" from the server
1756        if (updateInternalDate && message.mServerId != null) {
1757            try {
1758                Message remoteMessage2 = remoteFolder.getMessage(message.mServerId);
1759                if (remoteMessage2 != null) {
1760                    FetchProfile fp2 = new FetchProfile();
1761                    fp2.add(FetchProfile.Item.ENVELOPE);
1762                    remoteFolder.fetch(new Message[] { remoteMessage2 }, fp2, null);
1763                    message.mServerTimeStamp = remoteMessage2.getInternalDate().getTime();
1764                    updateMessage = true;
1765                }
1766            } catch (MessagingException me) {
1767                // skip it - we can live without this
1768            }
1769        }
1770
1771        // 6. Perform required edits to local copy of message
1772        if (deleteMessage || updateMessage) {
1773            Uri uri = ContentUris.withAppendedId(EmailContent.Message.CONTENT_URI, message.mId);
1774            ContentResolver resolver = mContext.getContentResolver();
1775            if (deleteMessage) {
1776                resolver.delete(uri, null, null);
1777            } else if (updateMessage) {
1778                ContentValues cv = new ContentValues();
1779                cv.put(EmailContent.Message.SERVER_ID, message.mServerId);
1780                cv.put(EmailContent.Message.SERVER_TIMESTAMP, message.mServerTimeStamp);
1781                resolver.update(uri, cv, null, null);
1782            }
1783        }
1784
1785        return true;
1786    }
1787
1788    /**
1789     * Finish loading a message that have been partially downloaded.
1790     *
1791     * @param messageId the message to load
1792     * @param listener the callback by which results will be reported
1793     */
1794    public void loadMessageForView(final long messageId, MessagingListener listener) {
1795        mListeners.loadMessageForViewStarted(messageId);
1796        put("loadMessageForViewRemote", listener, new Runnable() {
1797            public void run() {
1798                try {
1799                    // 1. Resample the message, in case it disappeared or synced while
1800                    // this command was in queue
1801                    EmailContent.Message message =
1802                        EmailContent.Message.restoreMessageWithId(mContext, messageId);
1803                    if (message == null) {
1804                        mListeners.loadMessageForViewFailed(messageId, "Unknown message");
1805                        return;
1806                    }
1807                    if (message.mFlagLoaded == EmailContent.Message.FLAG_LOADED_COMPLETE) {
1808                        mListeners.loadMessageForViewFinished(messageId);
1809                        return;
1810                    }
1811
1812                    // 2. Open the remote folder.
1813                    // TODO all of these could be narrower projections
1814                    // TODO combine with common code in loadAttachment
1815                    EmailContent.Account account =
1816                        EmailContent.Account.restoreAccountWithId(mContext, message.mAccountKey);
1817                    EmailContent.Mailbox mailbox =
1818                        EmailContent.Mailbox.restoreMailboxWithId(mContext, message.mMailboxKey);
1819                    if (account == null || mailbox == null) {
1820                        mListeners.loadMessageForViewFailed(messageId, "null account or mailbox");
1821                        return;
1822                    }
1823
1824                    Store remoteStore =
1825                        Store.getInstance(account.getStoreUri(mContext), mContext, null);
1826                    Folder remoteFolder = remoteStore.getFolder(mailbox.mDisplayName);
1827                    remoteFolder.open(OpenMode.READ_WRITE, null);
1828
1829                    // 3. Not supported, because IMAP & POP don't use it: structure prefetch
1830//                  if (remoteStore.requireStructurePrefetch()) {
1831//                  // For remote stores that require it, prefetch the message structure.
1832//                  FetchProfile fp = new FetchProfile();
1833//                  fp.add(FetchProfile.Item.STRUCTURE);
1834//                  localFolder.fetch(new Message[] { message }, fp, null);
1835//
1836//                  ArrayList<Part> viewables = new ArrayList<Part>();
1837//                  ArrayList<Part> attachments = new ArrayList<Part>();
1838//                  MimeUtility.collectParts(message, viewables, attachments);
1839//                  fp.clear();
1840//                  for (Part part : viewables) {
1841//                      fp.add(part);
1842//                  }
1843//
1844//                  remoteFolder.fetch(new Message[] { message }, fp, null);
1845//
1846//                  // Store the updated message locally
1847//                  localFolder.updateMessage((LocalMessage)message);
1848
1849                    // 4. Set up to download the entire message
1850                    Message remoteMessage = remoteFolder.getMessage(message.mServerId);
1851                    FetchProfile fp = new FetchProfile();
1852                    fp.add(FetchProfile.Item.BODY);
1853                    remoteFolder.fetch(new Message[] { remoteMessage }, fp, null);
1854
1855                    // 5. Write to provider
1856                    copyOneMessageToProvider(remoteMessage, account, mailbox,
1857                            EmailContent.Message.FLAG_LOADED_COMPLETE);
1858
1859                    // 6. Notify UI
1860                    mListeners.loadMessageForViewFinished(messageId);
1861
1862                } catch (MessagingException me) {
1863                    if (Email.LOGD) Log.v(Logging.LOG_TAG, "", me);
1864                    mListeners.loadMessageForViewFailed(messageId, me.getMessage());
1865                } catch (RuntimeException rte) {
1866                    mListeners.loadMessageForViewFailed(messageId, rte.getMessage());
1867                }
1868            }
1869        });
1870    }
1871
1872    /**
1873     * Attempts to load the attachment specified by id from the given account and message.
1874     * @param account
1875     * @param message
1876     * @param part
1877     * @param listener
1878     */
1879    public void loadAttachment(final long accountId, final long messageId, final long mailboxId,
1880            final long attachmentId, MessagingListener listener, final boolean background) {
1881        mListeners.loadAttachmentStarted(accountId, messageId, attachmentId, true);
1882
1883        put("loadAttachment", listener, new Runnable() {
1884            public void run() {
1885                try {
1886                    //1. Check if the attachment is already here and return early in that case
1887                    Attachment attachment =
1888                        Attachment.restoreAttachmentWithId(mContext, attachmentId);
1889                    if (attachment == null) {
1890                        mListeners.loadAttachmentFailed(accountId, messageId, attachmentId,
1891                                   new MessagingException("The attachment is null"),
1892                                   background);
1893                        return;
1894                    }
1895                    if (Utility.attachmentExists(mContext, attachment)) {
1896                        mListeners.loadAttachmentFinished(accountId, messageId, attachmentId);
1897                        return;
1898                    }
1899
1900                    // 2. Open the remote folder.
1901                    // TODO all of these could be narrower projections
1902                    EmailContent.Account account =
1903                        EmailContent.Account.restoreAccountWithId(mContext, accountId);
1904                    EmailContent.Mailbox mailbox =
1905                        EmailContent.Mailbox.restoreMailboxWithId(mContext, mailboxId);
1906                    EmailContent.Message message =
1907                        EmailContent.Message.restoreMessageWithId(mContext, messageId);
1908
1909                    if (account == null || mailbox == null || message == null) {
1910                        mListeners.loadAttachmentFailed(accountId, messageId, attachmentId,
1911                                new MessagingException(
1912                                        "Account, mailbox, message or attachment are null"),
1913                                background);
1914                        return;
1915                    }
1916
1917                    Store remoteStore =
1918                        Store.getInstance(account.getStoreUri(mContext), mContext, null);
1919                    Folder remoteFolder = remoteStore.getFolder(mailbox.mDisplayName);
1920                    remoteFolder.open(OpenMode.READ_WRITE, null);
1921
1922                    // 3. Generate a shell message in which to retrieve the attachment,
1923                    // and a shell BodyPart for the attachment.  Then glue them together.
1924                    Message storeMessage = remoteFolder.createMessage(message.mServerId);
1925                    MimeBodyPart storePart = new MimeBodyPart();
1926                    storePart.setSize((int)attachment.mSize);
1927                    storePart.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA,
1928                            attachment.mLocation);
1929                    storePart.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
1930                            String.format("%s;\n name=\"%s\"",
1931                            attachment.mMimeType,
1932                            attachment.mFileName));
1933                    // TODO is this always true for attachments?  I think we dropped the
1934                    // true encoding along the way
1935                    storePart.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
1936
1937                    MimeMultipart multipart = new MimeMultipart();
1938                    multipart.setSubType("mixed");
1939                    multipart.addBodyPart(storePart);
1940
1941                    storeMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "multipart/mixed");
1942                    storeMessage.setBody(multipart);
1943
1944                    // 4. Now ask for the attachment to be fetched
1945                    FetchProfile fp = new FetchProfile();
1946                    fp.add(storePart);
1947                    remoteFolder.fetch(new Message[] { storeMessage }, fp,
1948                            mController.new MessageRetrievalListenerBridge(
1949                                    messageId, attachmentId));
1950
1951                    // If we failed to load the attachment, throw an Exception here, so that
1952                    // AttachmentDownloadService knows that we failed
1953                    if (storePart.getBody() == null) {
1954                        throw new MessagingException("Attachment not loaded.");
1955                    }
1956
1957                    // 5. Save the downloaded file and update the attachment as necessary
1958                    LegacyConversions.saveAttachmentBody(mContext, storePart, attachment,
1959                            accountId);
1960
1961                    // 6. Report success
1962                    mListeners.loadAttachmentFinished(accountId, messageId, attachmentId);
1963                }
1964                catch (MessagingException me) {
1965                    if (Email.LOGD) Log.v(Logging.LOG_TAG, "", me);
1966                    mListeners.loadAttachmentFailed(
1967                            accountId, messageId, attachmentId, me, background);
1968                } catch (IOException ioe) {
1969                    Log.e(Logging.LOG_TAG, "Error while storing attachment." + ioe.toString());
1970                }
1971            }});
1972    }
1973
1974    /**
1975     * Attempt to send any messages that are sitting in the Outbox.
1976     * @param account
1977     * @param listener
1978     */
1979    public void sendPendingMessages(final EmailContent.Account account, final long sentFolderId,
1980            MessagingListener listener) {
1981        put("sendPendingMessages", listener, new Runnable() {
1982            public void run() {
1983                sendPendingMessagesSynchronous(account, sentFolderId);
1984            }
1985        });
1986    }
1987
1988    /**
1989     * Attempt to send any messages that are sitting in the Outbox.
1990     *
1991     * @param account
1992     * @param listener
1993     */
1994    public void sendPendingMessagesSynchronous(final EmailContent.Account account,
1995            long sentFolderId) {
1996        NotificationController nc = NotificationController.getInstance(mContext);
1997        // 1.  Loop through all messages in the account's outbox
1998        long outboxId = Mailbox.findMailboxOfType(mContext, account.mId, Mailbox.TYPE_OUTBOX);
1999        if (outboxId == Mailbox.NO_MAILBOX) {
2000            return;
2001        }
2002        ContentResolver resolver = mContext.getContentResolver();
2003        Cursor c = resolver.query(EmailContent.Message.CONTENT_URI,
2004                EmailContent.Message.ID_COLUMN_PROJECTION,
2005                EmailContent.Message.MAILBOX_KEY + "=?", new String[] { Long.toString(outboxId) },
2006                null);
2007        try {
2008            // 2.  exit early
2009            if (c.getCount() <= 0) {
2010                return;
2011            }
2012            // 3. do one-time setup of the Sender & other stuff
2013            mListeners.sendPendingMessagesStarted(account.mId, -1);
2014
2015            Sender sender = Sender.getInstance(mContext, account.getSenderUri(mContext));
2016            Store remoteStore = Store.getInstance(account.getStoreUri(mContext), mContext, null);
2017            boolean requireMoveMessageToSentFolder = remoteStore.requireCopyMessageToSentFolder();
2018            ContentValues moveToSentValues = null;
2019            if (requireMoveMessageToSentFolder) {
2020                moveToSentValues = new ContentValues();
2021                moveToSentValues.put(MessageColumns.MAILBOX_KEY, sentFolderId);
2022            }
2023
2024            // 4.  loop through the available messages and send them
2025            while (c.moveToNext()) {
2026                long messageId = -1;
2027                try {
2028                    messageId = c.getLong(0);
2029                    mListeners.sendPendingMessagesStarted(account.mId, messageId);
2030                    // Don't send messages with unloaded attachments
2031                    if (Utility.hasUnloadedAttachments(mContext, messageId)) {
2032                        if (Email.DEBUG) {
2033                            Log.d(Logging.LOG_TAG, "Can't send #" + messageId +
2034                                    "; unloaded attachments");
2035                        }
2036                        continue;
2037                    }
2038                    sender.sendMessage(messageId);
2039                } catch (MessagingException me) {
2040                    // report error for this message, but keep trying others
2041                    if (me instanceof AuthenticationFailedException) {
2042                        nc.showLoginFailedNotification(account.mId);
2043                    }
2044                    mListeners.sendPendingMessagesFailed(account.mId, messageId, me);
2045                    continue;
2046                }
2047                // 5. move to sent, or delete
2048                Uri syncedUri =
2049                    ContentUris.withAppendedId(EmailContent.Message.SYNCED_CONTENT_URI, messageId);
2050                if (requireMoveMessageToSentFolder) {
2051                    // If this is a forwarded message and it has attachments, delete them, as they
2052                    // duplicate information found elsewhere (on the server).  This saves storage.
2053                    EmailContent.Message msg =
2054                        EmailContent.Message.restoreMessageWithId(mContext, messageId);
2055                    if (msg != null &&
2056                            ((msg.mFlags & EmailContent.Message.FLAG_TYPE_FORWARD) != 0)) {
2057                        AttachmentUtilities.deleteAllAttachmentFiles(mContext, account.mId,
2058                                messageId);
2059                    }
2060                    resolver.update(syncedUri, moveToSentValues, null, null);
2061                } else {
2062                    AttachmentUtilities.deleteAllAttachmentFiles(mContext, account.mId,
2063                            messageId);
2064                    Uri uri =
2065                        ContentUris.withAppendedId(EmailContent.Message.CONTENT_URI, messageId);
2066                    resolver.delete(uri, null, null);
2067                    resolver.delete(syncedUri, null, null);
2068                }
2069            }
2070            // 6. report completion/success
2071            mListeners.sendPendingMessagesCompleted(account.mId);
2072            nc.cancelLoginFailedNotification(account.mId);
2073        } catch (MessagingException me) {
2074            if (me instanceof AuthenticationFailedException) {
2075                nc.showLoginFailedNotification(account.mId);
2076            }
2077            mListeners.sendPendingMessagesFailed(account.mId, -1, me);
2078        } finally {
2079            c.close();
2080        }
2081    }
2082
2083    /**
2084     * Checks mail for an account.
2085     * This entry point is for use by the mail checking service only, because it
2086     * gives slightly different callbacks (so the service doesn't get confused by callbacks
2087     * triggered by/for the foreground UI.
2088     *
2089     * TODO clean up the execution model which is unnecessarily threaded due to legacy code
2090     *
2091     * @param accountId the account to check
2092     * @param listener
2093     */
2094    public void checkMail(final long accountId, final long tag, final MessagingListener listener) {
2095        mListeners.checkMailStarted(mContext, accountId, tag);
2096
2097        // This puts the command on the queue (not synchronous)
2098        listFolders(accountId, null);
2099
2100        // Put this on the queue as well so it follows listFolders
2101        put("checkMail", listener, new Runnable() {
2102            public void run() {
2103                // send any pending outbound messages.  note, there is a slight race condition
2104                // here if we somehow don't have a sent folder, but this should never happen
2105                // because the call to sendMessage() would have built one previously.
2106                long inboxId = -1;
2107                EmailContent.Account account =
2108                    EmailContent.Account.restoreAccountWithId(mContext, accountId);
2109                if (account != null) {
2110                    long sentboxId = Mailbox.findMailboxOfType(mContext, accountId,
2111                            Mailbox.TYPE_SENT);
2112                    if (sentboxId != Mailbox.NO_MAILBOX) {
2113                        sendPendingMessagesSynchronous(account, sentboxId);
2114                    }
2115                    // find mailbox # for inbox and sync it.
2116                    // TODO we already know this in Controller, can we pass it in?
2117                    inboxId = Mailbox.findMailboxOfType(mContext, accountId, Mailbox.TYPE_INBOX);
2118                    if (inboxId != Mailbox.NO_MAILBOX) {
2119                        EmailContent.Mailbox mailbox =
2120                            EmailContent.Mailbox.restoreMailboxWithId(mContext, inboxId);
2121                        if (mailbox != null) {
2122                            synchronizeMailboxSynchronous(account, mailbox);
2123                        }
2124                    }
2125                }
2126                mListeners.checkMailFinished(mContext, accountId, inboxId, tag);
2127            }
2128        });
2129    }
2130
2131    private static class Command {
2132        public Runnable runnable;
2133
2134        public MessagingListener listener;
2135
2136        public String description;
2137
2138        @Override
2139        public String toString() {
2140            return description;
2141        }
2142    }
2143}
2144