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