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