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