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