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