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