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