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