EmailProvider.java revision 81b0f74efa39d80b0aa18686c96499faf0e2691f
1/*
2 * Copyright (C) 2009 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.provider;
18
19import android.appwidget.AppWidgetManager;
20import android.content.ComponentName;
21import android.content.ContentProvider;
22import android.content.ContentProviderOperation;
23import android.content.ContentProviderResult;
24import android.content.ContentResolver;
25import android.content.ContentUris;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
29import android.content.OperationApplicationException;
30import android.content.UriMatcher;
31import android.database.ContentObserver;
32import android.database.Cursor;
33import android.database.CursorWrapper;
34import android.database.MatrixCursor;
35import android.database.MergeCursor;
36import android.database.sqlite.SQLiteDatabase;
37import android.database.sqlite.SQLiteException;
38import android.net.Uri;
39import android.os.Bundle;
40import android.os.Parcel;
41import android.os.RemoteException;
42import android.provider.BaseColumns;
43import android.text.TextUtils;
44import android.util.Log;
45
46import com.android.common.content.ProjectionMap;
47import com.android.email.NotificationController;
48import com.android.email.Preferences;
49import com.android.email.R;
50import com.android.email.SecurityPolicy;
51import com.android.email.provider.ContentCache.CacheToken;
52import com.android.email.service.AttachmentDownloadService;
53import com.android.email.service.EmailServiceUtils;
54import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
55import com.android.email2.ui.MailActivityEmail;
56import com.android.emailcommon.Logging;
57import com.android.emailcommon.mail.Address;
58import com.android.emailcommon.provider.Account;
59import com.android.emailcommon.provider.EmailContent;
60import com.android.emailcommon.provider.EmailContent.AccountColumns;
61import com.android.emailcommon.provider.EmailContent.Attachment;
62import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
63import com.android.emailcommon.provider.EmailContent.Body;
64import com.android.emailcommon.provider.EmailContent.BodyColumns;
65import com.android.emailcommon.provider.EmailContent.MailboxColumns;
66import com.android.emailcommon.provider.EmailContent.Message;
67import com.android.emailcommon.provider.EmailContent.MessageColumns;
68import com.android.emailcommon.provider.EmailContent.PolicyColumns;
69import com.android.emailcommon.provider.EmailContent.SyncColumns;
70import com.android.emailcommon.provider.HostAuth;
71import com.android.emailcommon.provider.Mailbox;
72import com.android.emailcommon.provider.Policy;
73import com.android.emailcommon.provider.QuickResponse;
74import com.android.emailcommon.service.EmailServiceProxy;
75import com.android.emailcommon.service.IEmailService;
76import com.android.emailcommon.service.IEmailServiceCallback;
77import com.android.emailcommon.service.SearchParams;
78import com.android.emailcommon.utility.AttachmentUtilities;
79import com.android.emailcommon.utility.Utility;
80import com.android.ex.photo.provider.PhotoContract;
81import com.android.mail.providers.Folder;
82import com.android.mail.providers.UIProvider;
83import com.android.mail.providers.UIProvider.AccountCapabilities;
84import com.android.mail.providers.UIProvider.AccountCursorExtraKeys;
85import com.android.mail.providers.UIProvider.ConversationPriority;
86import com.android.mail.providers.UIProvider.ConversationSendingState;
87import com.android.mail.providers.UIProvider.DraftType;
88import com.android.mail.providers.UIProvider.Swipe;
89import com.android.mail.utils.LogUtils;
90import com.android.mail.utils.MatrixCursorWithExtra;
91import com.android.mail.utils.Utils;
92import com.android.mail.widget.BaseWidgetProvider;
93import com.android.mail.widget.WidgetProvider;
94import com.google.common.annotations.VisibleForTesting;
95import com.google.common.collect.ImmutableMap;
96import com.google.common.collect.ImmutableSet;
97
98import java.io.File;
99import java.util.ArrayList;
100import java.util.Arrays;
101import java.util.Collection;
102import java.util.HashMap;
103import java.util.List;
104import java.util.Map;
105import java.util.Set;
106import java.util.regex.Pattern;
107
108/**
109 * @author mblank
110 *
111 */
112public class EmailProvider extends ContentProvider {
113
114    private static final String TAG = "EmailProvider";
115
116    public static final String EMAIL_APP_MIME_TYPE = "application/email-ls";
117
118    protected static final String DATABASE_NAME = "EmailProvider.db";
119    protected static final String BODY_DATABASE_NAME = "EmailProviderBody.db";
120    protected static final String BACKUP_DATABASE_NAME = "EmailProviderBackup.db";
121
122    public static final String ACTION_ATTACHMENT_UPDATED = "com.android.email.ATTACHMENT_UPDATED";
123    public static final String ATTACHMENT_UPDATED_EXTRA_FLAGS =
124        "com.android.email.ATTACHMENT_UPDATED_FLAGS";
125
126    /**
127     * Notifies that changes happened. Certain UI components, e.g., widgets, can register for this
128     * {@link android.content.Intent} and update accordingly. However, this can be very broad and
129     * is NOT the preferred way of getting notification.
130     */
131    public static final String ACTION_NOTIFY_MESSAGE_LIST_DATASET_CHANGED =
132        "com.android.email.MESSAGE_LIST_DATASET_CHANGED";
133
134    public static final String EMAIL_MESSAGE_MIME_TYPE =
135        "vnd.android.cursor.item/email-message";
136    public static final String EMAIL_ATTACHMENT_MIME_TYPE =
137        "vnd.android.cursor.item/email-attachment";
138
139    /** Appended to the notification URI for delete operations */
140    public static final String NOTIFICATION_OP_DELETE = "delete";
141    /** Appended to the notification URI for insert operations */
142    public static final String NOTIFICATION_OP_INSERT = "insert";
143    /** Appended to the notification URI for update operations */
144    public static final String NOTIFICATION_OP_UPDATE = "update";
145
146    // Definitions for our queries looking for orphaned messages
147    private static final String[] ORPHANS_PROJECTION
148        = new String[] {MessageColumns.ID, MessageColumns.MAILBOX_KEY};
149    private static final int ORPHANS_ID = 0;
150    private static final int ORPHANS_MAILBOX_KEY = 1;
151
152    private static final String WHERE_ID = EmailContent.RECORD_ID + "=?";
153
154    // This is not a hard limit on accounts, per se, but beyond this, we can't guarantee that all
155    // critical mailboxes, host auth's, accounts, and policies are cached
156    private static final int MAX_CACHED_ACCOUNTS = 16;
157    // Inbox, Drafts, Sent, Outbox, Trash, and Search (these boxes are cached when possible)
158    private static final int NUM_ALWAYS_CACHED_MAILBOXES = 6;
159
160    // We'll cache the following four tables; sizes are best estimates of effective values
161    private final ContentCache mCacheAccount =
162        new ContentCache("Account", Account.CONTENT_PROJECTION, MAX_CACHED_ACCOUNTS);
163    private final ContentCache mCacheHostAuth =
164        new ContentCache("HostAuth", HostAuth.CONTENT_PROJECTION, MAX_CACHED_ACCOUNTS * 2);
165    /*package*/ final ContentCache mCacheMailbox =
166        new ContentCache("Mailbox", Mailbox.CONTENT_PROJECTION,
167                MAX_CACHED_ACCOUNTS * (NUM_ALWAYS_CACHED_MAILBOXES + 2));
168    private final ContentCache mCacheMessage =
169        new ContentCache("Message", Message.CONTENT_PROJECTION, 8);
170    private final ContentCache mCachePolicy =
171        new ContentCache("Policy", Policy.CONTENT_PROJECTION, MAX_CACHED_ACCOUNTS);
172
173    private static final int ACCOUNT_BASE = 0;
174    private static final int ACCOUNT = ACCOUNT_BASE;
175    private static final int ACCOUNT_ID = ACCOUNT_BASE + 1;
176    private static final int ACCOUNT_ID_ADD_TO_FIELD = ACCOUNT_BASE + 2;
177    private static final int ACCOUNT_RESET_NEW_COUNT = ACCOUNT_BASE + 3;
178    private static final int ACCOUNT_RESET_NEW_COUNT_ID = ACCOUNT_BASE + 4;
179    private static final int ACCOUNT_DEFAULT_ID = ACCOUNT_BASE + 5;
180    private static final int ACCOUNT_CHECK = ACCOUNT_BASE + 6;
181    private static final int ACCOUNT_PICK_TRASH_FOLDER = ACCOUNT_BASE + 7;
182    private static final int ACCOUNT_PICK_SENT_FOLDER = ACCOUNT_BASE + 8;
183
184    private static final int MAILBOX_BASE = 0x1000;
185    private static final int MAILBOX = MAILBOX_BASE;
186    private static final int MAILBOX_ID = MAILBOX_BASE + 1;
187    private static final int MAILBOX_ID_FROM_ACCOUNT_AND_TYPE = MAILBOX_BASE + 2;
188    private static final int MAILBOX_ID_ADD_TO_FIELD = MAILBOX_BASE + 3;
189    private static final int MAILBOX_NOTIFICATION = MAILBOX_BASE + 4;
190    private static final int MAILBOX_MOST_RECENT_MESSAGE = MAILBOX_BASE + 5;
191
192    private static final int MESSAGE_BASE = 0x2000;
193    private static final int MESSAGE = MESSAGE_BASE;
194    private static final int MESSAGE_ID = MESSAGE_BASE + 1;
195    private static final int SYNCED_MESSAGE_ID = MESSAGE_BASE + 2;
196    private static final int MESSAGE_SELECTION = MESSAGE_BASE + 3;
197
198    private static final int ATTACHMENT_BASE = 0x3000;
199    private static final int ATTACHMENT = ATTACHMENT_BASE;
200    private static final int ATTACHMENT_ID = ATTACHMENT_BASE + 1;
201    private static final int ATTACHMENTS_MESSAGE_ID = ATTACHMENT_BASE + 2;
202
203    private static final int HOSTAUTH_BASE = 0x4000;
204    private static final int HOSTAUTH = HOSTAUTH_BASE;
205    private static final int HOSTAUTH_ID = HOSTAUTH_BASE + 1;
206
207    private static final int UPDATED_MESSAGE_BASE = 0x5000;
208    private static final int UPDATED_MESSAGE = UPDATED_MESSAGE_BASE;
209    private static final int UPDATED_MESSAGE_ID = UPDATED_MESSAGE_BASE + 1;
210
211    private static final int DELETED_MESSAGE_BASE = 0x6000;
212    private static final int DELETED_MESSAGE = DELETED_MESSAGE_BASE;
213    private static final int DELETED_MESSAGE_ID = DELETED_MESSAGE_BASE + 1;
214
215    private static final int POLICY_BASE = 0x7000;
216    private static final int POLICY = POLICY_BASE;
217    private static final int POLICY_ID = POLICY_BASE + 1;
218
219    private static final int QUICK_RESPONSE_BASE = 0x8000;
220    private static final int QUICK_RESPONSE = QUICK_RESPONSE_BASE;
221    private static final int QUICK_RESPONSE_ID = QUICK_RESPONSE_BASE + 1;
222    private static final int QUICK_RESPONSE_ACCOUNT_ID = QUICK_RESPONSE_BASE + 2;
223
224    private static final int UI_BASE = 0x9000;
225    private static final int UI_FOLDERS = UI_BASE;
226    private static final int UI_SUBFOLDERS = UI_BASE + 1;
227    private static final int UI_MESSAGES = UI_BASE + 2;
228    private static final int UI_MESSAGE = UI_BASE + 3;
229    private static final int UI_SENDMAIL = UI_BASE + 4;
230    private static final int UI_UNDO = UI_BASE + 5;
231    private static final int UI_SAVEDRAFT = UI_BASE + 6;
232    private static final int UI_UPDATEDRAFT = UI_BASE + 7;
233    private static final int UI_SENDDRAFT = UI_BASE + 8;
234    private static final int UI_FOLDER_REFRESH = UI_BASE + 9;
235    private static final int UI_FOLDER = UI_BASE + 10;
236    private static final int UI_ACCOUNT = UI_BASE + 11;
237    private static final int UI_ACCTS = UI_BASE + 12;
238    private static final int UI_ATTACHMENTS = UI_BASE + 13;
239    private static final int UI_ATTACHMENT = UI_BASE + 14;
240    private static final int UI_SEARCH = UI_BASE + 15;
241    private static final int UI_ACCOUNT_DATA = UI_BASE + 16;
242    private static final int UI_FOLDER_LOAD_MORE = UI_BASE + 17;
243    private static final int UI_CONVERSATION = UI_BASE + 18;
244    private static final int UI_RECENT_FOLDERS = UI_BASE + 19;
245    private static final int UI_DEFAULT_RECENT_FOLDERS = UI_BASE + 20;
246    private static final int UI_ALL_FOLDERS = UI_BASE + 21;
247
248    // MUST ALWAYS EQUAL THE LAST OF THE PREVIOUS BASE CONSTANTS
249    private static final int LAST_EMAIL_PROVIDER_DB_BASE = UI_BASE;
250
251    // DO NOT CHANGE BODY_BASE!!
252    private static final int BODY_BASE = LAST_EMAIL_PROVIDER_DB_BASE + 0x1000;
253    private static final int BODY = BODY_BASE;
254    private static final int BODY_ID = BODY_BASE + 1;
255
256    private static final int BASE_SHIFT = 12;  // 12 bits to the base type: 0, 0x1000, 0x2000, etc.
257
258    // TABLE_NAMES MUST remain in the order of the BASE constants above (e.g. ACCOUNT_BASE = 0x0000,
259    // MESSAGE_BASE = 0x1000, etc.)
260    private static final String[] TABLE_NAMES = {
261        Account.TABLE_NAME,
262        Mailbox.TABLE_NAME,
263        Message.TABLE_NAME,
264        Attachment.TABLE_NAME,
265        HostAuth.TABLE_NAME,
266        Message.UPDATED_TABLE_NAME,
267        Message.DELETED_TABLE_NAME,
268        Policy.TABLE_NAME,
269        QuickResponse.TABLE_NAME,
270        null,  // UI
271        Body.TABLE_NAME,
272    };
273
274    // CONTENT_CACHES MUST remain in the order of the BASE constants above
275    private final ContentCache[] mContentCaches = {
276        mCacheAccount,
277        mCacheMailbox,
278        mCacheMessage,
279        null, // Attachment
280        mCacheHostAuth,
281        null, // Updated message
282        null, // Deleted message
283        mCachePolicy,
284        null, // Quick response
285        null, // Body
286        null  // UI
287    };
288
289    // CACHE_PROJECTIONS MUST remain in the order of the BASE constants above
290    private static final String[][] CACHE_PROJECTIONS = {
291        Account.CONTENT_PROJECTION,
292        Mailbox.CONTENT_PROJECTION,
293        Message.CONTENT_PROJECTION,
294        null, // Attachment
295        HostAuth.CONTENT_PROJECTION,
296        null, // Updated message
297        null, // Deleted message
298        Policy.CONTENT_PROJECTION,
299        null,  // Quick response
300        null,  // Body
301        null   // UI
302    };
303
304    private static UriMatcher sURIMatcher = null;
305
306    private static final String MAILBOX_PRE_CACHE_SELECTION = MailboxColumns.TYPE + " IN (" +
307        Mailbox.TYPE_INBOX + "," + Mailbox.TYPE_DRAFTS + "," + Mailbox.TYPE_TRASH + "," +
308        Mailbox.TYPE_SENT + "," + Mailbox.TYPE_SEARCH + "," + Mailbox.TYPE_OUTBOX + ")";
309
310    /**
311     * Let's only generate these SQL strings once, as they are used frequently
312     * Note that this isn't relevant for table creation strings, since they are used only once
313     */
314    private static final String UPDATED_MESSAGE_INSERT = "insert or ignore into " +
315        Message.UPDATED_TABLE_NAME + " select * from " + Message.TABLE_NAME + " where " +
316        EmailContent.RECORD_ID + '=';
317
318    private static final String UPDATED_MESSAGE_DELETE = "delete from " +
319        Message.UPDATED_TABLE_NAME + " where " + EmailContent.RECORD_ID + '=';
320
321    private static final String DELETED_MESSAGE_INSERT = "insert or replace into " +
322        Message.DELETED_TABLE_NAME + " select * from " + Message.TABLE_NAME + " where " +
323        EmailContent.RECORD_ID + '=';
324
325    private static final String DELETE_ORPHAN_BODIES = "delete from " + Body.TABLE_NAME +
326        " where " + BodyColumns.MESSAGE_KEY + " in " + "(select " + BodyColumns.MESSAGE_KEY +
327        " from " + Body.TABLE_NAME + " except select " + EmailContent.RECORD_ID + " from " +
328        Message.TABLE_NAME + ')';
329
330    private static final String DELETE_BODY = "delete from " + Body.TABLE_NAME +
331        " where " + BodyColumns.MESSAGE_KEY + '=';
332
333    private static final String ID_EQUALS = EmailContent.RECORD_ID + "=?";
334
335    private static ContentValues CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT;
336    private static final ContentValues EMPTY_CONTENT_VALUES = new ContentValues();
337
338    public static final String MESSAGE_URI_PARAMETER_MAILBOX_ID = "mailboxId";
339
340    // For undo handling
341    private int mLastSequence = -1;
342    private ArrayList<ContentProviderOperation> mLastSequenceOps =
343            new ArrayList<ContentProviderOperation>();
344
345    // Query parameter indicating the command came from UIProvider
346    private static final String IS_UIPROVIDER = "is_uiprovider";
347
348    private static final String SWIPE_DELETE = Integer.toString(Swipe.DELETE);
349    private static final String SWIPE_DISABLED = Integer.toString(Swipe.DISABLED);
350
351
352    /**
353     * Wrap the UriMatcher call so we can throw a runtime exception if an unknown Uri is passed in
354     * @param uri the Uri to match
355     * @return the match value
356     */
357    private static int findMatch(Uri uri, String methodName) {
358        int match = sURIMatcher.match(uri);
359        if (match < 0) {
360            throw new IllegalArgumentException("Unknown uri: " + uri);
361        } else if (Logging.LOGD) {
362            Log.v(TAG, methodName + ": uri=" + uri + ", match is " + match);
363        }
364        return match;
365    }
366
367    public static Uri INTEGRITY_CHECK_URI;
368    public static Uri ACCOUNT_BACKUP_URI;
369    public static Uri FOLDER_STATUS_URI;
370    public static Uri FOLDER_REFRESH_URI;
371
372    private SQLiteDatabase mDatabase;
373    private SQLiteDatabase mBodyDatabase;
374
375    public static Uri uiUri(String type, long id) {
376        return Uri.parse(uiUriString(type, id));
377    }
378
379    /**
380     * Creates a URI string from a database ID (guaranteed to be unique).
381     * @param type of the resource: uifolder, message, etc.
382     * @param id the id of the resource.
383     * @return
384     */
385    public static String uiUriString(String type, long id) {
386        return "content://" + EmailContent.AUTHORITY + "/" + type + ((id == -1) ? "" : ("/" + id));
387    }
388
389    /**
390     * Orphan record deletion utility.  Generates a sqlite statement like:
391     *  delete from <table> where <column> not in (select <foreignColumn> from <foreignTable>)
392     * @param db the EmailProvider database
393     * @param table the table whose orphans are to be removed
394     * @param column the column deletion will be based on
395     * @param foreignColumn the column in the foreign table whose absence will trigger the deletion
396     * @param foreignTable the foreign table
397     */
398    @VisibleForTesting
399    void deleteUnlinked(SQLiteDatabase db, String table, String column, String foreignColumn,
400            String foreignTable) {
401        int count = db.delete(table, column + " not in (select " + foreignColumn + " from " +
402                foreignTable + ")", null);
403        if (count > 0) {
404            Log.w(TAG, "Found " + count + " orphaned row(s) in " + table);
405        }
406    }
407
408    @VisibleForTesting
409    synchronized SQLiteDatabase getDatabase(Context context) {
410        // Always return the cached database, if we've got one
411        if (mDatabase != null) {
412            return mDatabase;
413        }
414
415        // Whenever we create or re-cache the databases, make sure that we haven't lost one
416        // to corruption
417        checkDatabases();
418
419        DBHelper.DatabaseHelper helper = new DBHelper.DatabaseHelper(context, DATABASE_NAME);
420        mDatabase = helper.getWritableDatabase();
421        DBHelper.BodyDatabaseHelper bodyHelper =
422                new DBHelper.BodyDatabaseHelper(context, BODY_DATABASE_NAME);
423        mBodyDatabase = bodyHelper.getWritableDatabase();
424        if (mBodyDatabase != null) {
425            String bodyFileName = mBodyDatabase.getPath();
426            mDatabase.execSQL("attach \"" + bodyFileName + "\" as BodyDatabase");
427        }
428
429        // Restore accounts if the database is corrupted...
430        restoreIfNeeded(context, mDatabase);
431        // Check for any orphaned Messages in the updated/deleted tables
432        deleteMessageOrphans(mDatabase, Message.UPDATED_TABLE_NAME);
433        deleteMessageOrphans(mDatabase, Message.DELETED_TABLE_NAME);
434        // Delete orphaned mailboxes/messages/policies (account no longer exists)
435        deleteUnlinked(mDatabase, Mailbox.TABLE_NAME, MailboxColumns.ACCOUNT_KEY, AccountColumns.ID,
436                Account.TABLE_NAME);
437        deleteUnlinked(mDatabase, Message.TABLE_NAME, MessageColumns.ACCOUNT_KEY, AccountColumns.ID,
438                Account.TABLE_NAME);
439        deleteUnlinked(mDatabase, Policy.TABLE_NAME, PolicyColumns.ID, AccountColumns.POLICY_KEY,
440                Account.TABLE_NAME);
441        initUiProvider();
442        preCacheData();
443        return mDatabase;
444    }
445
446    /**
447     * Perform startup actions related to UI
448     */
449    private void initUiProvider() {
450        // Clear mailbox sync status
451        mDatabase.execSQL("update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UI_SYNC_STATUS +
452                "=" + UIProvider.SyncStatus.NO_SYNC);
453    }
454
455    /**
456     * Pre-cache all of the items in a given table meeting the selection criteria
457     * @param tableUri the table uri
458     * @param baseProjection the base projection of that table
459     * @param selection the selection criteria
460     */
461    private void preCacheTable(Uri tableUri, String[] baseProjection, String selection) {
462        Cursor c = query(tableUri, EmailContent.ID_PROJECTION, selection, null, null);
463        try {
464            while (c.moveToNext()) {
465                long id = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
466                Cursor cachedCursor = query(ContentUris.withAppendedId(
467                        tableUri, id), baseProjection, null, null, null);
468                if (cachedCursor != null) {
469                    // For accounts, create a mailbox type map entry (if necessary)
470                    if (tableUri == Account.CONTENT_URI) {
471                        getOrCreateAccountMailboxTypeMap(id);
472                    }
473                    cachedCursor.close();
474                }
475            }
476        } finally {
477            c.close();
478        }
479    }
480
481    private final HashMap<Long, HashMap<Integer, Long>> mMailboxTypeMap =
482        new HashMap<Long, HashMap<Integer, Long>>();
483
484    private HashMap<Integer, Long> getOrCreateAccountMailboxTypeMap(long accountId) {
485        synchronized(mMailboxTypeMap) {
486            HashMap<Integer, Long> accountMailboxTypeMap = mMailboxTypeMap.get(accountId);
487            if (accountMailboxTypeMap == null) {
488                accountMailboxTypeMap = new HashMap<Integer, Long>();
489                mMailboxTypeMap.put(accountId, accountMailboxTypeMap);
490            }
491            return accountMailboxTypeMap;
492        }
493    }
494
495    private void addToMailboxTypeMap(Cursor c) {
496        long accountId = c.getLong(Mailbox.CONTENT_ACCOUNT_KEY_COLUMN);
497        int type = c.getInt(Mailbox.CONTENT_TYPE_COLUMN);
498        synchronized(mMailboxTypeMap) {
499            HashMap<Integer, Long> accountMailboxTypeMap =
500                getOrCreateAccountMailboxTypeMap(accountId);
501            accountMailboxTypeMap.put(type, c.getLong(Mailbox.CONTENT_ID_COLUMN));
502        }
503    }
504
505    private long getMailboxIdFromMailboxTypeMap(long accountId, int type) {
506        synchronized(mMailboxTypeMap) {
507            HashMap<Integer, Long> accountMap = mMailboxTypeMap.get(accountId);
508            Long mailboxId = null;
509            if (accountMap != null) {
510                mailboxId = accountMap.get(type);
511            }
512            if (mailboxId == null) return Mailbox.NO_MAILBOX;
513            return mailboxId;
514        }
515    }
516
517    private void preCacheData() {
518        synchronized(mMailboxTypeMap) {
519            mMailboxTypeMap.clear();
520
521            // Pre-cache accounts, host auth's, policies, and special mailboxes
522            preCacheTable(Account.CONTENT_URI, Account.CONTENT_PROJECTION, null);
523            preCacheTable(HostAuth.CONTENT_URI, HostAuth.CONTENT_PROJECTION, null);
524            preCacheTable(Policy.CONTENT_URI, Policy.CONTENT_PROJECTION, null);
525            preCacheTable(Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION,
526                    MAILBOX_PRE_CACHE_SELECTION);
527
528            // Create a map from account,type to a mailbox
529            Map<String, Cursor> snapshot = mCacheMailbox.getSnapshot();
530            Collection<Cursor> values = snapshot.values();
531            if (values != null) {
532                for (Cursor c: values) {
533                    if (c.moveToFirst()) {
534                        addToMailboxTypeMap(c);
535                    }
536                }
537            }
538        }
539    }
540
541    /*package*/ static SQLiteDatabase getReadableDatabase(Context context) {
542        DBHelper.DatabaseHelper helper = new DBHelper.DatabaseHelper(context, DATABASE_NAME);
543        return helper.getReadableDatabase();
544    }
545
546    /**
547     * Restore user Account and HostAuth data from our backup database
548     */
549    public static void restoreIfNeeded(Context context, SQLiteDatabase mainDatabase) {
550        if (MailActivityEmail.DEBUG) {
551            Log.w(TAG, "restoreIfNeeded...");
552        }
553        // Check for legacy backup
554        String legacyBackup = Preferences.getLegacyBackupPreference(context);
555        // If there's a legacy backup, create a new-style backup and delete the legacy backup
556        // In the 1:1000000000 chance that the user gets an app update just as his database becomes
557        // corrupt, oh well...
558        if (!TextUtils.isEmpty(legacyBackup)) {
559            backupAccounts(context, mainDatabase);
560            Preferences.clearLegacyBackupPreference(context);
561            Log.w(TAG, "Created new EmailProvider backup database");
562            return;
563        }
564
565        // If we have accounts, we're done
566        Cursor c = mainDatabase.query(Account.TABLE_NAME, EmailContent.ID_PROJECTION, null, null,
567                null, null, null);
568        try {
569            if (c.moveToFirst()) {
570                if (MailActivityEmail.DEBUG) {
571                    Log.w(TAG, "restoreIfNeeded: Account exists.");
572                }
573                return; // At least one account exists.
574            }
575        } finally {
576            c.close();
577        }
578
579        restoreAccounts(context, mainDatabase);
580    }
581
582    /** {@inheritDoc} */
583    @Override
584    public void shutdown() {
585        if (mDatabase != null) {
586            mDatabase.close();
587            mDatabase = null;
588        }
589        if (mBodyDatabase != null) {
590            mBodyDatabase.close();
591            mBodyDatabase = null;
592        }
593    }
594
595    /*package*/ static void deleteMessageOrphans(SQLiteDatabase database, String tableName) {
596        if (database != null) {
597            // We'll look at all of the items in the table; there won't be many typically
598            Cursor c = database.query(tableName, ORPHANS_PROJECTION, null, null, null, null, null);
599            // Usually, there will be nothing in these tables, so make a quick check
600            try {
601                if (c.getCount() == 0) return;
602                ArrayList<Long> foundMailboxes = new ArrayList<Long>();
603                ArrayList<Long> notFoundMailboxes = new ArrayList<Long>();
604                ArrayList<Long> deleteList = new ArrayList<Long>();
605                String[] bindArray = new String[1];
606                while (c.moveToNext()) {
607                    // Get the mailbox key and see if we've already found this mailbox
608                    // If so, we're fine
609                    long mailboxId = c.getLong(ORPHANS_MAILBOX_KEY);
610                    // If we already know this mailbox doesn't exist, mark the message for deletion
611                    if (notFoundMailboxes.contains(mailboxId)) {
612                        deleteList.add(c.getLong(ORPHANS_ID));
613                    // If we don't know about this mailbox, we'll try to find it
614                    } else if (!foundMailboxes.contains(mailboxId)) {
615                        bindArray[0] = Long.toString(mailboxId);
616                        Cursor boxCursor = database.query(Mailbox.TABLE_NAME,
617                                Mailbox.ID_PROJECTION, WHERE_ID, bindArray, null, null, null);
618                        try {
619                            // If it exists, we'll add it to the "found" mailboxes
620                            if (boxCursor.moveToFirst()) {
621                                foundMailboxes.add(mailboxId);
622                            // Otherwise, we'll add to "not found" and mark the message for deletion
623                            } else {
624                                notFoundMailboxes.add(mailboxId);
625                                deleteList.add(c.getLong(ORPHANS_ID));
626                            }
627                        } finally {
628                            boxCursor.close();
629                        }
630                    }
631                }
632                // Now, delete the orphan messages
633                for (long messageId: deleteList) {
634                    bindArray[0] = Long.toString(messageId);
635                    database.delete(tableName, WHERE_ID, bindArray);
636                }
637            } finally {
638                c.close();
639            }
640        }
641    }
642
643    @Override
644    public int delete(Uri uri, String selection, String[] selectionArgs) {
645        final int match = findMatch(uri, "delete");
646        Context context = getContext();
647        // Pick the correct database for this operation
648        // If we're in a transaction already (which would happen during applyBatch), then the
649        // body database is already attached to the email database and any attempt to use the
650        // body database directly will result in a SQLiteException (the database is locked)
651        SQLiteDatabase db = getDatabase(context);
652        int table = match >> BASE_SHIFT;
653        String id = "0";
654        boolean messageDeletion = false;
655        ContentResolver resolver = context.getContentResolver();
656
657        ContentCache cache = mContentCaches[table];
658        String tableName = TABLE_NAMES[table];
659        int result = -1;
660
661        try {
662            if (match == MESSAGE_ID || match == SYNCED_MESSAGE_ID) {
663                if (!uri.getBooleanQueryParameter(IS_UIPROVIDER, false)) {
664                    notifyUIConversation(uri);
665                }
666            }
667            switch (match) {
668                case UI_MESSAGE:
669                    return uiDeleteMessage(uri);
670                case UI_ACCOUNT_DATA:
671                    return uiDeleteAccountData(uri);
672                case UI_ACCOUNT:
673                    return uiDeleteAccount(uri);
674                case MESSAGE_SELECTION:
675                    Cursor findCursor = db.query(tableName, Message.ID_COLUMN_PROJECTION, selection,
676                            selectionArgs, null, null, null);
677                    try {
678                        if (findCursor.moveToFirst()) {
679                            return delete(ContentUris.withAppendedId(
680                                    Message.CONTENT_URI,
681                                    findCursor.getLong(Message.ID_COLUMNS_ID_COLUMN)),
682                                    null, null);
683                        } else {
684                            return 0;
685                        }
686                    } finally {
687                        findCursor.close();
688                    }
689                // These are cases in which one or more Messages might get deleted, either by
690                // cascade or explicitly
691                case MAILBOX_ID:
692                case MAILBOX:
693                case ACCOUNT_ID:
694                case ACCOUNT:
695                case MESSAGE:
696                case SYNCED_MESSAGE_ID:
697                case MESSAGE_ID:
698                    // Handle lost Body records here, since this cannot be done in a trigger
699                    // The process is:
700                    //  1) Begin a transaction, ensuring that both databases are affected atomically
701                    //  2) Do the requested deletion, with cascading deletions handled in triggers
702                    //  3) End the transaction, committing all changes atomically
703                    //
704                    // Bodies are auto-deleted here;  Attachments are auto-deleted via trigger
705                    messageDeletion = true;
706                    db.beginTransaction();
707                    break;
708            }
709            switch (match) {
710                case BODY_ID:
711                case DELETED_MESSAGE_ID:
712                case SYNCED_MESSAGE_ID:
713                case MESSAGE_ID:
714                case UPDATED_MESSAGE_ID:
715                case ATTACHMENT_ID:
716                case MAILBOX_ID:
717                case ACCOUNT_ID:
718                case HOSTAUTH_ID:
719                case POLICY_ID:
720                case QUICK_RESPONSE_ID:
721                    id = uri.getPathSegments().get(1);
722                    if (match == SYNCED_MESSAGE_ID) {
723                        // For synced messages, first copy the old message to the deleted table and
724                        // delete it from the updated table (in case it was updated first)
725                        // Note that this is all within a transaction, for atomicity
726                        db.execSQL(DELETED_MESSAGE_INSERT + id);
727                        db.execSQL(UPDATED_MESSAGE_DELETE + id);
728                    }
729                    if (cache != null) {
730                        cache.lock(id);
731                    }
732                    try {
733                        result = db.delete(tableName, whereWithId(id, selection), selectionArgs);
734                        if (cache != null) {
735                            switch(match) {
736                                case ACCOUNT_ID:
737                                    // Account deletion will clear all of the caches, as HostAuth's,
738                                    // Mailboxes, and Messages will be deleted in the process
739                                    mCacheMailbox.invalidate("Delete", uri, selection);
740                                    mCacheHostAuth.invalidate("Delete", uri, selection);
741                                    mCachePolicy.invalidate("Delete", uri, selection);
742                                    //$FALL-THROUGH$
743                                case MAILBOX_ID:
744                                    // Mailbox deletion will clear the Message cache
745                                    mCacheMessage.invalidate("Delete", uri, selection);
746                                    //$FALL-THROUGH$
747                                case SYNCED_MESSAGE_ID:
748                                case MESSAGE_ID:
749                                case HOSTAUTH_ID:
750                                case POLICY_ID:
751                                    cache.invalidate("Delete", uri, selection);
752                                    // Make sure all data is properly cached
753                                    if (match != MESSAGE_ID) {
754                                        preCacheData();
755                                    }
756                                    break;
757                            }
758                        }
759                    } finally {
760                        if (cache != null) {
761                            cache.unlock(id);
762                        }
763                    }
764                    if (match == ACCOUNT_ID) {
765                        notifyUI(UIPROVIDER_ACCOUNT_NOTIFIER, id);
766                        resolver.notifyChange(UIPROVIDER_ALL_ACCOUNTS_NOTIFIER, null);
767                    } else if (match == MAILBOX_ID) {
768                        notifyUI(UIPROVIDER_FOLDER_NOTIFIER, id);
769                    } else if (match == ATTACHMENT_ID) {
770                        notifyUI(UIPROVIDER_ATTACHMENT_NOTIFIER, id);
771                    }
772                    break;
773                case ATTACHMENTS_MESSAGE_ID:
774                    // All attachments for the given message
775                    id = uri.getPathSegments().get(2);
776                    result = db.delete(tableName,
777                            whereWith(Attachment.MESSAGE_KEY + "=" + id, selection), selectionArgs);
778                    break;
779
780                case BODY:
781                case MESSAGE:
782                case DELETED_MESSAGE:
783                case UPDATED_MESSAGE:
784                case ATTACHMENT:
785                case MAILBOX:
786                case ACCOUNT:
787                case HOSTAUTH:
788                case POLICY:
789                    switch(match) {
790                        // See the comments above for deletion of ACCOUNT_ID, etc
791                        case ACCOUNT:
792                            mCacheMailbox.invalidate("Delete", uri, selection);
793                            mCacheHostAuth.invalidate("Delete", uri, selection);
794                            mCachePolicy.invalidate("Delete", uri, selection);
795                            //$FALL-THROUGH$
796                        case MAILBOX:
797                            mCacheMessage.invalidate("Delete", uri, selection);
798                            //$FALL-THROUGH$
799                        case MESSAGE:
800                        case HOSTAUTH:
801                        case POLICY:
802                            cache.invalidate("Delete", uri, selection);
803                            break;
804                    }
805                    result = db.delete(tableName, selection, selectionArgs);
806                    switch(match) {
807                        case ACCOUNT:
808                        case MAILBOX:
809                        case HOSTAUTH:
810                        case POLICY:
811                            // Make sure all data is properly cached
812                            preCacheData();
813                            break;
814                    }
815                    break;
816
817                default:
818                    throw new IllegalArgumentException("Unknown URI " + uri);
819            }
820            if (messageDeletion) {
821                if (match == MESSAGE_ID) {
822                    // Delete the Body record associated with the deleted message
823                    db.execSQL(DELETE_BODY + id);
824                } else {
825                    // Delete any orphaned Body records
826                    db.execSQL(DELETE_ORPHAN_BODIES);
827                }
828                db.setTransactionSuccessful();
829            }
830        } catch (SQLiteException e) {
831            checkDatabases();
832            throw e;
833        } finally {
834            if (messageDeletion) {
835                db.endTransaction();
836            }
837        }
838
839        // Notify all notifier cursors
840        sendNotifierChange(getBaseNotificationUri(match), NOTIFICATION_OP_DELETE, id);
841
842        // Notify all email content cursors
843        resolver.notifyChange(EmailContent.CONTENT_URI, null);
844        return result;
845    }
846
847    @Override
848    // Use the email- prefix because message, mailbox, and account are so generic (e.g. SMS, IM)
849    public String getType(Uri uri) {
850        int match = findMatch(uri, "getType");
851        switch (match) {
852            case BODY_ID:
853                return "vnd.android.cursor.item/email-body";
854            case BODY:
855                return "vnd.android.cursor.dir/email-body";
856            case UPDATED_MESSAGE_ID:
857            case MESSAGE_ID:
858                // NOTE: According to the framework folks, we're supposed to invent mime types as
859                // a way of passing information to drag & drop recipients.
860                // If there's a mailboxId parameter in the url, we respond with a mime type that
861                // has -n appended, where n is the mailboxId of the message.  The drag & drop code
862                // uses this information to know not to allow dragging the item to its own mailbox
863                String mimeType = EMAIL_MESSAGE_MIME_TYPE;
864                String mailboxId = uri.getQueryParameter(MESSAGE_URI_PARAMETER_MAILBOX_ID);
865                if (mailboxId != null) {
866                    mimeType += "-" + mailboxId;
867                }
868                return mimeType;
869            case UPDATED_MESSAGE:
870            case MESSAGE:
871                return "vnd.android.cursor.dir/email-message";
872            case MAILBOX:
873                return "vnd.android.cursor.dir/email-mailbox";
874            case MAILBOX_ID:
875                return "vnd.android.cursor.item/email-mailbox";
876            case ACCOUNT:
877                return "vnd.android.cursor.dir/email-account";
878            case ACCOUNT_ID:
879                return "vnd.android.cursor.item/email-account";
880            case ATTACHMENTS_MESSAGE_ID:
881            case ATTACHMENT:
882                return "vnd.android.cursor.dir/email-attachment";
883            case ATTACHMENT_ID:
884                return EMAIL_ATTACHMENT_MIME_TYPE;
885            case HOSTAUTH:
886                return "vnd.android.cursor.dir/email-hostauth";
887            case HOSTAUTH_ID:
888                return "vnd.android.cursor.item/email-hostauth";
889            default:
890                throw new IllegalArgumentException("Unknown URI " + uri);
891        }
892    }
893
894    private static final Uri UIPROVIDER_CONVERSATION_NOTIFIER =
895            Uri.parse("content://" + UIProvider.AUTHORITY + "/uimessages");
896    private static final Uri UIPROVIDER_FOLDER_NOTIFIER =
897            Uri.parse("content://" + UIProvider.AUTHORITY + "/uifolder");
898    private static final Uri UIPROVIDER_ACCOUNT_NOTIFIER =
899            Uri.parse("content://" + UIProvider.AUTHORITY + "/uiaccount");
900    public static final Uri UIPROVIDER_SETTINGS_NOTIFIER =
901            Uri.parse("content://" + UIProvider.AUTHORITY + "/uisettings");
902    private static final Uri UIPROVIDER_ATTACHMENT_NOTIFIER =
903            Uri.parse("content://" + UIProvider.AUTHORITY + "/uiattachment");
904    private static final Uri UIPROVIDER_ATTACHMENTS_NOTIFIER =
905            Uri.parse("content://" + UIProvider.AUTHORITY + "/uiattachments");
906    public static final Uri UIPROVIDER_ALL_ACCOUNTS_NOTIFIER =
907            Uri.parse("content://" + UIProvider.AUTHORITY + "/uiaccts");
908    private static final Uri UIPROVIDER_MESSAGE_NOTIFIER =
909            Uri.parse("content://" + UIProvider.AUTHORITY + "/uimessage");
910    private static final Uri UIPROVIDER_RECENT_FOLDERS_NOTIFIER =
911            Uri.parse("content://" + UIProvider.AUTHORITY + "/uirecentfolders");
912
913    @Override
914    public Uri insert(Uri uri, ContentValues values) {
915        int match = findMatch(uri, "insert");
916        Context context = getContext();
917        ContentResolver resolver = context.getContentResolver();
918
919        // See the comment at delete(), above
920        SQLiteDatabase db = getDatabase(context);
921        int table = match >> BASE_SHIFT;
922        String id = "0";
923        long longId;
924
925        // We do NOT allow setting of unreadCount/messageCount via the provider
926        // These columns are maintained via triggers
927        if (match == MAILBOX_ID || match == MAILBOX) {
928            values.put(MailboxColumns.UNREAD_COUNT, 0);
929            values.put(MailboxColumns.MESSAGE_COUNT, 0);
930        }
931
932        Uri resultUri = null;
933
934        try {
935            switch (match) {
936                case UI_SAVEDRAFT:
937                    return uiSaveDraft(uri, values);
938                case UI_SENDMAIL:
939                    return uiSendMail(uri, values);
940                // NOTE: It is NOT legal for production code to insert directly into UPDATED_MESSAGE
941                // or DELETED_MESSAGE; see the comment below for details
942                case UPDATED_MESSAGE:
943                case DELETED_MESSAGE:
944                case MESSAGE:
945                case BODY:
946                case ATTACHMENT:
947                case MAILBOX:
948                case ACCOUNT:
949                case HOSTAUTH:
950                case POLICY:
951                case QUICK_RESPONSE:
952                    longId = db.insert(TABLE_NAMES[table], "foo", values);
953                    resultUri = ContentUris.withAppendedId(uri, longId);
954                    switch(match) {
955                        case MESSAGE:
956                            if (!uri.getBooleanQueryParameter(IS_UIPROVIDER, false)) {
957                                notifyUIConversationMailbox(values.getAsLong(Message.MAILBOX_KEY));
958                            }
959                            break;
960                        case MAILBOX:
961                            if (values.containsKey(MailboxColumns.TYPE)) {
962                                // Only cache special mailbox types
963                                int type = values.getAsInteger(MailboxColumns.TYPE);
964                                if (type != Mailbox.TYPE_INBOX && type != Mailbox.TYPE_OUTBOX &&
965                                        type != Mailbox.TYPE_DRAFTS && type != Mailbox.TYPE_SENT &&
966                                        type != Mailbox.TYPE_TRASH && type != Mailbox.TYPE_SEARCH) {
967                                    break;
968                                }
969                            }
970                            // Notify the account when a new mailbox is added
971                            Long accountId = values.getAsLong(MailboxColumns.ACCOUNT_KEY);
972                            if (accountId != null && accountId.longValue() > 0) {
973                                notifyUI(UIPROVIDER_ACCOUNT_NOTIFIER, accountId);
974                            }
975                            //$FALL-THROUGH$
976                        case ACCOUNT:
977                        case HOSTAUTH:
978                        case POLICY:
979                            // Cache new account, host auth, policy, and some mailbox rows
980                            Cursor c = query(resultUri, CACHE_PROJECTIONS[table], null, null, null);
981                            if (c != null) {
982                                if (match == MAILBOX) {
983                                    addToMailboxTypeMap(c);
984                                } else if (match == ACCOUNT) {
985                                    getOrCreateAccountMailboxTypeMap(longId);
986                                }
987                                c.close();
988                            }
989                            break;
990                    }
991                    // Clients shouldn't normally be adding rows to these tables, as they are
992                    // maintained by triggers.  However, we need to be able to do this for unit
993                    // testing, so we allow the insert and then throw the same exception that we
994                    // would if this weren't allowed.
995                    if (match == UPDATED_MESSAGE || match == DELETED_MESSAGE) {
996                        throw new IllegalArgumentException("Unknown URL " + uri);
997                    } else if (match == ATTACHMENT) {
998                        int flags = 0;
999                        if (values.containsKey(Attachment.FLAGS)) {
1000                            flags = values.getAsInteger(Attachment.FLAGS);
1001                        }
1002                        // Report all new attachments to the download service
1003                        mAttachmentService.attachmentChanged(getContext(), longId, flags);
1004                    } else if (match == ACCOUNT) {
1005                        resolver.notifyChange(UIPROVIDER_ALL_ACCOUNTS_NOTIFIER, null);
1006                    }
1007                    break;
1008                case MAILBOX_ID:
1009                    // This implies adding a message to a mailbox
1010                    // Hmm, a problem here is that we can't link the account as well, so it must be
1011                    // already in the values...
1012                    longId = Long.parseLong(uri.getPathSegments().get(1));
1013                    values.put(MessageColumns.MAILBOX_KEY, longId);
1014                    return insert(Message.CONTENT_URI, values); // Recurse
1015                case MESSAGE_ID:
1016                    // This implies adding an attachment to a message.
1017                    id = uri.getPathSegments().get(1);
1018                    longId = Long.parseLong(id);
1019                    values.put(AttachmentColumns.MESSAGE_KEY, longId);
1020                    return insert(Attachment.CONTENT_URI, values); // Recurse
1021                case ACCOUNT_ID:
1022                    // This implies adding a mailbox to an account.
1023                    longId = Long.parseLong(uri.getPathSegments().get(1));
1024                    values.put(MailboxColumns.ACCOUNT_KEY, longId);
1025                    return insert(Mailbox.CONTENT_URI, values); // Recurse
1026                case ATTACHMENTS_MESSAGE_ID:
1027                    longId = db.insert(TABLE_NAMES[table], "foo", values);
1028                    resultUri = ContentUris.withAppendedId(Attachment.CONTENT_URI, longId);
1029                    break;
1030                default:
1031                    throw new IllegalArgumentException("Unknown URL " + uri);
1032            }
1033        } catch (SQLiteException e) {
1034            checkDatabases();
1035            throw e;
1036        }
1037
1038        // Notify all notifier cursors
1039        sendNotifierChange(getBaseNotificationUri(match), NOTIFICATION_OP_INSERT, id);
1040
1041        // Notify all existing cursors.
1042        resolver.notifyChange(EmailContent.CONTENT_URI, null);
1043        return resultUri;
1044    }
1045
1046        @Override
1047        public boolean onCreate() {
1048            Context context = getContext();
1049            EmailContent.init(context);
1050            if (INTEGRITY_CHECK_URI == null) {
1051                INTEGRITY_CHECK_URI = Uri.parse("content://" + EmailContent.AUTHORITY +
1052                        "/integrityCheck");
1053                ACCOUNT_BACKUP_URI =
1054                        Uri.parse("content://" + EmailContent.AUTHORITY + "/accountBackup");
1055                FOLDER_STATUS_URI =
1056                        Uri.parse("content://" + EmailContent.AUTHORITY + "/status");
1057                FOLDER_REFRESH_URI =
1058                        Uri.parse("content://" + EmailContent.AUTHORITY + "/refresh");
1059            }
1060            MailActivityEmail.setServicesEnabledAsync(context);
1061            checkDatabases();
1062            if (sURIMatcher == null) {
1063                sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
1064            // Email URI matching table
1065            UriMatcher matcher = sURIMatcher;
1066
1067            // All accounts
1068            matcher.addURI(EmailContent.AUTHORITY, "account", ACCOUNT);
1069            // A specific account
1070            // insert into this URI causes a mailbox to be added to the account
1071            matcher.addURI(EmailContent.AUTHORITY, "account/#", ACCOUNT_ID);
1072            matcher.addURI(EmailContent.AUTHORITY, "account/default", ACCOUNT_DEFAULT_ID);
1073            matcher.addURI(EmailContent.AUTHORITY, "accountCheck/#", ACCOUNT_CHECK);
1074
1075            // Special URI to reset the new message count.  Only update works, and content values
1076            // will be ignored.
1077            matcher.addURI(EmailContent.AUTHORITY, "resetNewMessageCount",
1078                    ACCOUNT_RESET_NEW_COUNT);
1079            matcher.addURI(EmailContent.AUTHORITY, "resetNewMessageCount/#",
1080                    ACCOUNT_RESET_NEW_COUNT_ID);
1081
1082            // All mailboxes
1083            matcher.addURI(EmailContent.AUTHORITY, "mailbox", MAILBOX);
1084            // A specific mailbox
1085            // insert into this URI causes a message to be added to the mailbox
1086            // ** NOTE For now, the accountKey must be set manually in the values!
1087            matcher.addURI(EmailContent.AUTHORITY, "mailbox/#", MAILBOX_ID);
1088            matcher.addURI(EmailContent.AUTHORITY, "mailboxIdFromAccountAndType/#/#",
1089                    MAILBOX_ID_FROM_ACCOUNT_AND_TYPE);
1090            matcher.addURI(EmailContent.AUTHORITY, "mailboxNotification/#", MAILBOX_NOTIFICATION);
1091            matcher.addURI(EmailContent.AUTHORITY, "mailboxMostRecentMessage/#",
1092                    MAILBOX_MOST_RECENT_MESSAGE);
1093
1094            // All messages
1095            matcher.addURI(EmailContent.AUTHORITY, "message", MESSAGE);
1096            // A specific message
1097            // insert into this URI causes an attachment to be added to the message
1098            matcher.addURI(EmailContent.AUTHORITY, "message/#", MESSAGE_ID);
1099
1100            // A specific attachment
1101            matcher.addURI(EmailContent.AUTHORITY, "attachment", ATTACHMENT);
1102            // A specific attachment (the header information)
1103            matcher.addURI(EmailContent.AUTHORITY, "attachment/#", ATTACHMENT_ID);
1104            // The attachments of a specific message (query only) (insert & delete TBD)
1105            matcher.addURI(EmailContent.AUTHORITY, "attachment/message/#",
1106                    ATTACHMENTS_MESSAGE_ID);
1107
1108            // All mail bodies
1109            matcher.addURI(EmailContent.AUTHORITY, "body", BODY);
1110            // A specific mail body
1111            matcher.addURI(EmailContent.AUTHORITY, "body/#", BODY_ID);
1112
1113            // All hostauth records
1114            matcher.addURI(EmailContent.AUTHORITY, "hostauth", HOSTAUTH);
1115            // A specific hostauth
1116            matcher.addURI(EmailContent.AUTHORITY, "hostauth/*", HOSTAUTH_ID);
1117
1118            // Atomically a constant value to a particular field of a mailbox/account
1119            matcher.addURI(EmailContent.AUTHORITY, "mailboxIdAddToField/#",
1120                    MAILBOX_ID_ADD_TO_FIELD);
1121            matcher.addURI(EmailContent.AUTHORITY, "accountIdAddToField/#",
1122                    ACCOUNT_ID_ADD_TO_FIELD);
1123
1124            /**
1125             * THIS URI HAS SPECIAL SEMANTICS
1126             * ITS USE IS INTENDED FOR THE UI TO MARK CHANGES THAT NEED TO BE SYNCED BACK
1127             * TO A SERVER VIA A SYNC ADAPTER
1128             */
1129            matcher.addURI(EmailContent.AUTHORITY, "syncedMessage/#", SYNCED_MESSAGE_ID);
1130            matcher.addURI(EmailContent.AUTHORITY, "messageBySelection", MESSAGE_SELECTION);
1131
1132            /**
1133             * THE URIs BELOW THIS POINT ARE INTENDED TO BE USED BY SYNC ADAPTERS ONLY
1134             * THEY REFER TO DATA CREATED AND MAINTAINED BY CALLS TO THE SYNCED_MESSAGE_ID URI
1135             * BY THE UI APPLICATION
1136             */
1137            // All deleted messages
1138            matcher.addURI(EmailContent.AUTHORITY, "deletedMessage", DELETED_MESSAGE);
1139            // A specific deleted message
1140            matcher.addURI(EmailContent.AUTHORITY, "deletedMessage/#", DELETED_MESSAGE_ID);
1141
1142            // All updated messages
1143            matcher.addURI(EmailContent.AUTHORITY, "updatedMessage", UPDATED_MESSAGE);
1144            // A specific updated message
1145            matcher.addURI(EmailContent.AUTHORITY, "updatedMessage/#", UPDATED_MESSAGE_ID);
1146
1147            CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT = new ContentValues();
1148            CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT.put(Account.NEW_MESSAGE_COUNT, 0);
1149
1150            matcher.addURI(EmailContent.AUTHORITY, "policy", POLICY);
1151            matcher.addURI(EmailContent.AUTHORITY, "policy/#", POLICY_ID);
1152
1153            // All quick responses
1154            matcher.addURI(EmailContent.AUTHORITY, "quickresponse", QUICK_RESPONSE);
1155            // A specific quick response
1156            matcher.addURI(EmailContent.AUTHORITY, "quickresponse/#", QUICK_RESPONSE_ID);
1157            // All quick responses associated with a particular account id
1158            matcher.addURI(EmailContent.AUTHORITY, "quickresponse/account/#",
1159                    QUICK_RESPONSE_ACCOUNT_ID);
1160
1161            matcher.addURI(EmailContent.AUTHORITY, "uifolders/#", UI_FOLDERS);
1162            matcher.addURI(EmailContent.AUTHORITY, "uiallfolders/#", UI_ALL_FOLDERS);
1163            matcher.addURI(EmailContent.AUTHORITY, "uisubfolders/#", UI_SUBFOLDERS);
1164            matcher.addURI(EmailContent.AUTHORITY, "uimessages/#", UI_MESSAGES);
1165            matcher.addURI(EmailContent.AUTHORITY, "uimessage/#", UI_MESSAGE);
1166            matcher.addURI(EmailContent.AUTHORITY, "uisendmail/#", UI_SENDMAIL);
1167            matcher.addURI(EmailContent.AUTHORITY, "uiundo", UI_UNDO);
1168            matcher.addURI(EmailContent.AUTHORITY, "uisavedraft/#", UI_SAVEDRAFT);
1169            matcher.addURI(EmailContent.AUTHORITY, "uiupdatedraft/#", UI_UPDATEDRAFT);
1170            matcher.addURI(EmailContent.AUTHORITY, "uisenddraft/#", UI_SENDDRAFT);
1171            matcher.addURI(EmailContent.AUTHORITY, "uirefresh/#", UI_FOLDER_REFRESH);
1172            matcher.addURI(EmailContent.AUTHORITY, "uifolder/#", UI_FOLDER);
1173            matcher.addURI(EmailContent.AUTHORITY, "uiaccount/#", UI_ACCOUNT);
1174            matcher.addURI(EmailContent.AUTHORITY, "uiaccts", UI_ACCTS);
1175            matcher.addURI(EmailContent.AUTHORITY, "uiattachments/#", UI_ATTACHMENTS);
1176            matcher.addURI(EmailContent.AUTHORITY, "uiattachment/#", UI_ATTACHMENT);
1177            matcher.addURI(EmailContent.AUTHORITY, "uisearch/#", UI_SEARCH);
1178            matcher.addURI(EmailContent.AUTHORITY, "uiaccountdata/#", UI_ACCOUNT_DATA);
1179            matcher.addURI(EmailContent.AUTHORITY, "uiloadmore/#", UI_FOLDER_LOAD_MORE);
1180            matcher.addURI(EmailContent.AUTHORITY, "uiconversation/#", UI_CONVERSATION);
1181            matcher.addURI(EmailContent.AUTHORITY, "uirecentfolders/#", UI_RECENT_FOLDERS);
1182            matcher.addURI(EmailContent.AUTHORITY, "uidefaultrecentfolders/#",
1183                    UI_DEFAULT_RECENT_FOLDERS);
1184            matcher.addURI(EmailContent.AUTHORITY, "pickTrashFolder/#", ACCOUNT_PICK_TRASH_FOLDER);
1185            matcher.addURI(EmailContent.AUTHORITY, "pickSentFolder/#", ACCOUNT_PICK_SENT_FOLDER);
1186        }
1187        return false;
1188    }
1189
1190    /**
1191     * The idea here is that the two databases (EmailProvider.db and EmailProviderBody.db must
1192     * always be in sync (i.e. there are two database or NO databases).  This code will delete
1193     * any "orphan" database, so that both will be created together.  Note that an "orphan" database
1194     * will exist after either of the individual databases is deleted due to data corruption.
1195     */
1196    public synchronized void checkDatabases() {
1197        // Uncache the databases
1198        if (mDatabase != null) {
1199            mDatabase = null;
1200        }
1201        if (mBodyDatabase != null) {
1202            mBodyDatabase = null;
1203        }
1204        // Look for orphans, and delete as necessary; these must always be in sync
1205        File databaseFile = getContext().getDatabasePath(DATABASE_NAME);
1206        File bodyFile = getContext().getDatabasePath(BODY_DATABASE_NAME);
1207
1208        // TODO Make sure attachments are deleted
1209        if (databaseFile.exists() && !bodyFile.exists()) {
1210            Log.w(TAG, "Deleting orphaned EmailProvider database...");
1211            databaseFile.delete();
1212        } else if (bodyFile.exists() && !databaseFile.exists()) {
1213            Log.w(TAG, "Deleting orphaned EmailProviderBody database...");
1214            bodyFile.delete();
1215        }
1216    }
1217    @Override
1218    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
1219            String sortOrder) {
1220        long time = 0L;
1221        if (MailActivityEmail.DEBUG) {
1222            time = System.nanoTime();
1223        }
1224        Cursor c = null;
1225        int match;
1226        try {
1227            match = findMatch(uri, "query");
1228        } catch (IllegalArgumentException e) {
1229            String uriString = uri.toString();
1230            // If we were passed an illegal uri, see if it ends in /-1
1231            // if so, and if substituting 0 for -1 results in a valid uri, return an empty cursor
1232            if (uriString != null && uriString.endsWith("/-1")) {
1233                uri = Uri.parse(uriString.substring(0, uriString.length() - 2) + "0");
1234                match = findMatch(uri, "query");
1235                switch (match) {
1236                    case BODY_ID:
1237                    case MESSAGE_ID:
1238                    case DELETED_MESSAGE_ID:
1239                    case UPDATED_MESSAGE_ID:
1240                    case ATTACHMENT_ID:
1241                    case MAILBOX_ID:
1242                    case ACCOUNT_ID:
1243                    case HOSTAUTH_ID:
1244                    case POLICY_ID:
1245                        return new MatrixCursor(projection, 0);
1246                }
1247            }
1248            throw e;
1249        }
1250        Context context = getContext();
1251        // See the comment at delete(), above
1252        SQLiteDatabase db = getDatabase(context);
1253        int table = match >> BASE_SHIFT;
1254        String limit = uri.getQueryParameter(EmailContent.PARAMETER_LIMIT);
1255        String id;
1256
1257        // Find the cache for this query's table (if any)
1258        ContentCache cache = null;
1259        String tableName = TABLE_NAMES[table];
1260        // We can only use the cache if there's no selection
1261        if (selection == null) {
1262            cache = mContentCaches[table];
1263        }
1264        if (cache == null) {
1265            ContentCache.notCacheable(uri, selection);
1266        }
1267
1268        try {
1269            switch (match) {
1270                // First, dispatch queries from UnfiedEmail
1271                case UI_SEARCH:
1272                    c = uiSearch(uri, projection);
1273                    return c;
1274                case UI_ACCTS:
1275                    c = uiAccounts(projection);
1276                    return c;
1277                case UI_UNDO:
1278                    return uiUndo(projection);
1279                case UI_SUBFOLDERS:
1280                case UI_MESSAGES:
1281                case UI_MESSAGE:
1282                case UI_FOLDER:
1283                case UI_ACCOUNT:
1284                case UI_ATTACHMENT:
1285                case UI_ATTACHMENTS:
1286                case UI_CONVERSATION:
1287                case UI_RECENT_FOLDERS:
1288                case UI_ALL_FOLDERS:
1289                    // For now, we don't allow selection criteria within these queries
1290                    if (selection != null || selectionArgs != null) {
1291                        throw new IllegalArgumentException("UI queries can't have selection/args");
1292                    }
1293                    c = uiQuery(match, uri, projection);
1294                    return c;
1295                case UI_FOLDERS:
1296                    c = uiFolders(uri, projection);
1297                    return c;
1298                case UI_FOLDER_LOAD_MORE:
1299                    c = uiFolderLoadMore(uri);
1300                    return c;
1301                case UI_FOLDER_REFRESH:
1302                    c = uiFolderRefresh(uri);
1303                    return c;
1304                case MAILBOX_NOTIFICATION:
1305                    c = notificationQuery(uri);
1306                    return c;
1307                case MAILBOX_MOST_RECENT_MESSAGE:
1308                    c = mostRecentMessageQuery(uri);
1309                    return c;
1310                case ACCOUNT_DEFAULT_ID:
1311                    // Start with a snapshot of the cache
1312                    Map<String, Cursor> accountCache = mCacheAccount.getSnapshot();
1313                    long accountId = Account.NO_ACCOUNT;
1314                    // Find the account with "isDefault" set, or the lowest account ID otherwise.
1315                    // Note that the snapshot from the cached isn't guaranteed to be sorted in any
1316                    // way.
1317                    Collection<Cursor> accounts = accountCache.values();
1318                    for (Cursor accountCursor: accounts) {
1319                        // For now, at least, we can have zero count cursors (e.g. if someone looks
1320                        // up a non-existent id); we need to skip these
1321                        if (accountCursor.moveToFirst()) {
1322                            boolean isDefault =
1323                                accountCursor.getInt(Account.CONTENT_IS_DEFAULT_COLUMN) == 1;
1324                            long iterId = accountCursor.getLong(Account.CONTENT_ID_COLUMN);
1325                            // We'll remember this one if it's the default or the first one we see
1326                            if (isDefault) {
1327                                accountId = iterId;
1328                                break;
1329                            } else if ((accountId == Account.NO_ACCOUNT) || (iterId < accountId)) {
1330                                accountId = iterId;
1331                            }
1332                        }
1333                    }
1334                    // Return a cursor with an id projection
1335                    MatrixCursor mc = new MatrixCursor(EmailContent.ID_PROJECTION);
1336                    mc.addRow(new Object[] {accountId});
1337                    c = mc;
1338                    break;
1339                case MAILBOX_ID_FROM_ACCOUNT_AND_TYPE:
1340                    // Get accountId and type and find the mailbox in our map
1341                    List<String> pathSegments = uri.getPathSegments();
1342                    accountId = Long.parseLong(pathSegments.get(1));
1343                    int type = Integer.parseInt(pathSegments.get(2));
1344                    long mailboxId = getMailboxIdFromMailboxTypeMap(accountId, type);
1345                    // Return a cursor with an id projection
1346                    mc = new MatrixCursor(EmailContent.ID_PROJECTION);
1347                    mc.addRow(new Object[] {mailboxId});
1348                    c = mc;
1349                    break;
1350                case BODY:
1351                case MESSAGE:
1352                case UPDATED_MESSAGE:
1353                case DELETED_MESSAGE:
1354                case ATTACHMENT:
1355                case MAILBOX:
1356                case ACCOUNT:
1357                case HOSTAUTH:
1358                case POLICY:
1359                case QUICK_RESPONSE:
1360                    // Special-case "count of accounts"; it's common and we always know it
1361                    if (match == ACCOUNT && Arrays.equals(projection, EmailContent.COUNT_COLUMNS) &&
1362                            selection == null && limit.equals("1")) {
1363                        int accountCount = mMailboxTypeMap.size();
1364                        // In the rare case there are MAX_CACHED_ACCOUNTS or more, we can't do this
1365                        if (accountCount < MAX_CACHED_ACCOUNTS) {
1366                            mc = new MatrixCursor(projection, 1);
1367                            mc.addRow(new Object[] {accountCount});
1368                            c = mc;
1369                            break;
1370                        }
1371                    }
1372                    c = db.query(tableName, projection,
1373                            selection, selectionArgs, null, null, sortOrder, limit);
1374                    break;
1375                case BODY_ID:
1376                case MESSAGE_ID:
1377                case DELETED_MESSAGE_ID:
1378                case UPDATED_MESSAGE_ID:
1379                case ATTACHMENT_ID:
1380                case MAILBOX_ID:
1381                case ACCOUNT_ID:
1382                case HOSTAUTH_ID:
1383                case POLICY_ID:
1384                case QUICK_RESPONSE_ID:
1385                    id = uri.getPathSegments().get(1);
1386                    if (cache != null) {
1387                        c = cache.getCachedCursor(id, projection);
1388                    }
1389                    if (c == null) {
1390                        CacheToken token = null;
1391                        if (cache != null) {
1392                            token = cache.getCacheToken(id);
1393                        }
1394                        c = db.query(tableName, projection, whereWithId(id, selection),
1395                                selectionArgs, null, null, sortOrder, limit);
1396                        if (cache != null) {
1397                            c = cache.putCursor(c, id, projection, token);
1398                        }
1399                    }
1400                    break;
1401                case ATTACHMENTS_MESSAGE_ID:
1402                    // All attachments for the given message
1403                    id = uri.getPathSegments().get(2);
1404                    c = db.query(Attachment.TABLE_NAME, projection,
1405                            whereWith(Attachment.MESSAGE_KEY + "=" + id, selection),
1406                            selectionArgs, null, null, sortOrder, limit);
1407                    break;
1408                case QUICK_RESPONSE_ACCOUNT_ID:
1409                    // All quick responses for the given account
1410                    id = uri.getPathSegments().get(2);
1411                    c = db.query(QuickResponse.TABLE_NAME, projection,
1412                            whereWith(QuickResponse.ACCOUNT_KEY + "=" + id, selection),
1413                            selectionArgs, null, null, sortOrder);
1414                    break;
1415                default:
1416                    throw new IllegalArgumentException("Unknown URI " + uri);
1417            }
1418        } catch (SQLiteException e) {
1419            checkDatabases();
1420            throw e;
1421        } catch (RuntimeException e) {
1422            checkDatabases();
1423            e.printStackTrace();
1424            throw e;
1425        } finally {
1426            if (cache != null && c != null && MailActivityEmail.DEBUG) {
1427                cache.recordQueryTime(c, System.nanoTime() - time);
1428            }
1429            if (c == null) {
1430                // This should never happen, but let's be sure to log it...
1431                Log.e(TAG, "Query returning null for uri: " + uri + ", selection: " + selection);
1432            }
1433        }
1434
1435        if ((c != null) && !isTemporary()) {
1436            c.setNotificationUri(getContext().getContentResolver(), uri);
1437        }
1438        return c;
1439    }
1440
1441    private String whereWithId(String id, String selection) {
1442        StringBuilder sb = new StringBuilder(256);
1443        sb.append("_id=");
1444        sb.append(id);
1445        if (selection != null) {
1446            sb.append(" AND (");
1447            sb.append(selection);
1448            sb.append(')');
1449        }
1450        return sb.toString();
1451    }
1452
1453    /**
1454     * Combine a locally-generated selection with a user-provided selection
1455     *
1456     * This introduces risk that the local selection might insert incorrect chars
1457     * into the SQL, so use caution.
1458     *
1459     * @param where locally-generated selection, must not be null
1460     * @param selection user-provided selection, may be null
1461     * @return a single selection string
1462     */
1463    private String whereWith(String where, String selection) {
1464        if (selection == null) {
1465            return where;
1466        }
1467        StringBuilder sb = new StringBuilder(where);
1468        sb.append(" AND (");
1469        sb.append(selection);
1470        sb.append(')');
1471
1472        return sb.toString();
1473    }
1474
1475    /**
1476     * Restore a HostAuth from a database, given its unique id
1477     * @param db the database
1478     * @param id the unique id (_id) of the row
1479     * @return a fully populated HostAuth or null if the row does not exist
1480     */
1481    private static HostAuth restoreHostAuth(SQLiteDatabase db, long id) {
1482        Cursor c = db.query(HostAuth.TABLE_NAME, HostAuth.CONTENT_PROJECTION,
1483                HostAuth.RECORD_ID + "=?", new String[] {Long.toString(id)}, null, null, null);
1484        try {
1485            if (c.moveToFirst()) {
1486                HostAuth hostAuth = new HostAuth();
1487                hostAuth.restore(c);
1488                return hostAuth;
1489            }
1490            return null;
1491        } finally {
1492            c.close();
1493        }
1494    }
1495
1496    /**
1497     * Copy the Account and HostAuth tables from one database to another
1498     * @param fromDatabase the source database
1499     * @param toDatabase the destination database
1500     * @return the number of accounts copied, or -1 if an error occurred
1501     */
1502    private static int copyAccountTables(SQLiteDatabase fromDatabase, SQLiteDatabase toDatabase) {
1503        if (fromDatabase == null || toDatabase == null) return -1;
1504
1505        // Lock both databases; for the "from" database, we don't want anyone changing it from
1506        // under us; for the "to" database, we want to make the operation atomic
1507        int copyCount = 0;
1508        fromDatabase.beginTransaction();
1509        try {
1510            toDatabase.beginTransaction();
1511            try {
1512                // Delete anything hanging around here
1513                toDatabase.delete(Account.TABLE_NAME, null, null);
1514                toDatabase.delete(HostAuth.TABLE_NAME, null, null);
1515
1516                // Get our account cursor
1517                Cursor c = fromDatabase.query(Account.TABLE_NAME, Account.CONTENT_PROJECTION,
1518                        null, null, null, null, null);
1519                if (c == null) return 0;
1520                Log.d(TAG, "fromDatabase accounts: " + c.getCount());
1521                try {
1522                    // Loop through accounts, copying them and associated host auth's
1523                    while (c.moveToNext()) {
1524                        Account account = new Account();
1525                        account.restore(c);
1526
1527                        // Clear security sync key and sync key, as these were specific to the
1528                        // state of the account, and we've reset that...
1529                        // Clear policy key so that we can re-establish policies from the server
1530                        // TODO This is pretty EAS specific, but there's a lot of that around
1531                        account.mSecuritySyncKey = null;
1532                        account.mSyncKey = null;
1533                        account.mPolicyKey = 0;
1534
1535                        // Copy host auth's and update foreign keys
1536                        HostAuth hostAuth = restoreHostAuth(fromDatabase,
1537                                account.mHostAuthKeyRecv);
1538
1539                        // The account might have gone away, though very unlikely
1540                        if (hostAuth == null) continue;
1541                        account.mHostAuthKeyRecv = toDatabase.insert(HostAuth.TABLE_NAME, null,
1542                                hostAuth.toContentValues());
1543
1544                        // EAS accounts have no send HostAuth
1545                        if (account.mHostAuthKeySend > 0) {
1546                            hostAuth = restoreHostAuth(fromDatabase, account.mHostAuthKeySend);
1547                            // Belt and suspenders; I can't imagine that this is possible,
1548                            // since we checked the validity of the account above, and the
1549                            // database is now locked
1550                            if (hostAuth == null) continue;
1551                            account.mHostAuthKeySend = toDatabase.insert(
1552                                    HostAuth.TABLE_NAME, null, hostAuth.toContentValues());
1553                        }
1554
1555                        // Now, create the account in the "to" database
1556                        toDatabase.insert(Account.TABLE_NAME, null, account.toContentValues());
1557                        copyCount++;
1558                    }
1559                } finally {
1560                    c.close();
1561                }
1562
1563                // Say it's ok to commit
1564                toDatabase.setTransactionSuccessful();
1565            } finally {
1566                // STOPSHIP: Remove logging here and in at endTransaction() below
1567                Log.d(TAG, "ending toDatabase transaction; copyCount = " + copyCount);
1568                toDatabase.endTransaction();
1569            }
1570        } catch (SQLiteException ex) {
1571            Log.w(TAG, "Exception while copying account tables", ex);
1572            copyCount = -1;
1573        } finally {
1574            Log.d(TAG, "ending fromDatabase transaction; copyCount = " + copyCount);
1575            fromDatabase.endTransaction();
1576        }
1577        return copyCount;
1578    }
1579
1580    private static SQLiteDatabase getBackupDatabase(Context context) {
1581        DBHelper.DatabaseHelper helper = new DBHelper.DatabaseHelper(context, BACKUP_DATABASE_NAME);
1582        return helper.getWritableDatabase();
1583    }
1584
1585    /**
1586     * Backup account data, returning the number of accounts backed up
1587     */
1588    private static int backupAccounts(Context context, SQLiteDatabase mainDatabase) {
1589        if (MailActivityEmail.DEBUG) {
1590            Log.d(TAG, "backupAccounts...");
1591        }
1592        SQLiteDatabase backupDatabase = getBackupDatabase(context);
1593        try {
1594            int numBackedUp = copyAccountTables(mainDatabase, backupDatabase);
1595            if (numBackedUp < 0) {
1596                Log.e(TAG, "Account backup failed!");
1597            } else if (MailActivityEmail.DEBUG) {
1598                Log.d(TAG, "Backed up " + numBackedUp + " accounts...");
1599            }
1600            return numBackedUp;
1601        } finally {
1602            if (backupDatabase != null) {
1603                backupDatabase.close();
1604            }
1605        }
1606    }
1607
1608    /**
1609     * Restore account data, returning the number of accounts restored
1610     */
1611    private static int restoreAccounts(Context context, SQLiteDatabase mainDatabase) {
1612        if (MailActivityEmail.DEBUG) {
1613            Log.d(TAG, "restoreAccounts...");
1614        }
1615        SQLiteDatabase backupDatabase = getBackupDatabase(context);
1616        try {
1617            int numRecovered = copyAccountTables(backupDatabase, mainDatabase);
1618            if (numRecovered > 0) {
1619                Log.e(TAG, "Recovered " + numRecovered + " accounts!");
1620            } else if (numRecovered < 0) {
1621                Log.e(TAG, "Account recovery failed?");
1622            } else if (MailActivityEmail.DEBUG) {
1623                Log.d(TAG, "No accounts to restore...");
1624            }
1625            return numRecovered;
1626        } finally {
1627            if (backupDatabase != null) {
1628                backupDatabase.close();
1629            }
1630        }
1631    }
1632
1633    // select count(*) from (select count(*) as dupes from Mailbox where accountKey=?
1634    // group by serverId) where dupes > 1;
1635    private static final String ACCOUNT_INTEGRITY_SQL =
1636            "select count(*) from (select count(*) as dupes from " + Mailbox.TABLE_NAME +
1637            " where accountKey=? group by " + MailboxColumns.SERVER_ID + ") where dupes > 1";
1638
1639    @Override
1640    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
1641        // Handle this special case the fastest possible way
1642        if (uri == INTEGRITY_CHECK_URI) {
1643            checkDatabases();
1644            return 0;
1645        } else if (uri == ACCOUNT_BACKUP_URI) {
1646            return backupAccounts(getContext(), getDatabase(getContext()));
1647        }
1648
1649        // Notify all existing cursors, except for ACCOUNT_RESET_NEW_COUNT(_ID)
1650        Uri notificationUri = EmailContent.CONTENT_URI;
1651
1652        int match = findMatch(uri, "update");
1653        Context context = getContext();
1654        ContentResolver resolver = context.getContentResolver();
1655        // See the comment at delete(), above
1656        SQLiteDatabase db = getDatabase(context);
1657        int table = match >> BASE_SHIFT;
1658        int result;
1659
1660        // We do NOT allow setting of unreadCount/messageCount via the provider
1661        // These columns are maintained via triggers
1662        if (match == MAILBOX_ID || match == MAILBOX) {
1663            values.remove(MailboxColumns.UNREAD_COUNT);
1664            values.remove(MailboxColumns.MESSAGE_COUNT);
1665        }
1666
1667        ContentCache cache = mContentCaches[table];
1668        String tableName = TABLE_NAMES[table];
1669        String id = "0";
1670
1671        try {
1672outer:
1673            switch (match) {
1674                case ACCOUNT_PICK_TRASH_FOLDER:
1675                    return pickTrashFolder(uri);
1676                case ACCOUNT_PICK_SENT_FOLDER:
1677                    return pickSentFolder(uri);
1678                case UI_FOLDER:
1679                    return uiUpdateFolder(uri, values);
1680                case UI_RECENT_FOLDERS:
1681                    return uiUpdateRecentFolders(uri, values);
1682                case UI_DEFAULT_RECENT_FOLDERS:
1683                    return uiPopulateRecentFolders(uri);
1684                case UI_ATTACHMENT:
1685                    return uiUpdateAttachment(uri, values);
1686                case UI_UPDATEDRAFT:
1687                    return uiUpdateDraft(uri, values);
1688                case UI_SENDDRAFT:
1689                    return uiSendDraft(uri, values);
1690                case UI_MESSAGE:
1691                    return uiUpdateMessage(uri, values);
1692                case ACCOUNT_CHECK:
1693                    id = uri.getLastPathSegment();
1694                    // With any error, return 1 (a failure)
1695                    int res = 1;
1696                    Cursor ic = null;
1697                    try {
1698                        ic = db.rawQuery(ACCOUNT_INTEGRITY_SQL, new String[] {id});
1699                        if (ic.moveToFirst()) {
1700                            res = ic.getInt(0);
1701                        }
1702                    } finally {
1703                        if (ic != null) {
1704                            ic.close();
1705                        }
1706                    }
1707                    // Count of duplicated mailboxes
1708                    return res;
1709                case MAILBOX_ID_ADD_TO_FIELD:
1710                case ACCOUNT_ID_ADD_TO_FIELD:
1711                    id = uri.getPathSegments().get(1);
1712                    String field = values.getAsString(EmailContent.FIELD_COLUMN_NAME);
1713                    Long add = values.getAsLong(EmailContent.ADD_COLUMN_NAME);
1714                    if (field == null || add == null) {
1715                        throw new IllegalArgumentException("No field/add specified " + uri);
1716                    }
1717                    ContentValues actualValues = new ContentValues();
1718                    if (cache != null) {
1719                        cache.lock(id);
1720                    }
1721                    try {
1722                        db.beginTransaction();
1723                        try {
1724                            Cursor c = db.query(tableName,
1725                                    new String[] {EmailContent.RECORD_ID, field},
1726                                    whereWithId(id, selection),
1727                                    selectionArgs, null, null, null);
1728                            try {
1729                                result = 0;
1730                                String[] bind = new String[1];
1731                                if (c.moveToNext()) {
1732                                    bind[0] = c.getString(0); // _id
1733                                    long value = c.getLong(1) + add;
1734                                    actualValues.put(field, value);
1735                                    result = db.update(tableName, actualValues, ID_EQUALS, bind);
1736                                }
1737                                db.setTransactionSuccessful();
1738                            } finally {
1739                                c.close();
1740                            }
1741                        } finally {
1742                            db.endTransaction();
1743                        }
1744                    } finally {
1745                        if (cache != null) {
1746                            cache.unlock(id, actualValues);
1747                        }
1748                    }
1749                    break;
1750                case MESSAGE_SELECTION:
1751                    Cursor findCursor = db.query(tableName, Message.ID_COLUMN_PROJECTION, selection,
1752                            selectionArgs, null, null, null);
1753                    try {
1754                        if (findCursor.moveToFirst()) {
1755                            return update(ContentUris.withAppendedId(
1756                                    Message.CONTENT_URI,
1757                                    findCursor.getLong(Message.ID_COLUMNS_ID_COLUMN)),
1758                                    values, null, null);
1759                        } else {
1760                            return 0;
1761                        }
1762                    } finally {
1763                        findCursor.close();
1764                    }
1765                case SYNCED_MESSAGE_ID:
1766                case UPDATED_MESSAGE_ID:
1767                case MESSAGE_ID:
1768                case BODY_ID:
1769                case ATTACHMENT_ID:
1770                case MAILBOX_ID:
1771                case ACCOUNT_ID:
1772                case HOSTAUTH_ID:
1773                case QUICK_RESPONSE_ID:
1774                case POLICY_ID:
1775                    id = uri.getPathSegments().get(1);
1776                    if (cache != null) {
1777                        cache.lock(id);
1778                    }
1779                    try {
1780                        if (match == SYNCED_MESSAGE_ID) {
1781                            // For synced messages, first copy the old message to the updated table
1782                            // Note the insert or ignore semantics, guaranteeing that only the first
1783                            // update will be reflected in the updated message table; therefore this
1784                            // row will always have the "original" data
1785                            db.execSQL(UPDATED_MESSAGE_INSERT + id);
1786                        } else if (match == MESSAGE_ID) {
1787                            db.execSQL(UPDATED_MESSAGE_DELETE + id);
1788                        }
1789                        result = db.update(tableName, values, whereWithId(id, selection),
1790                                selectionArgs);
1791                    } catch (SQLiteException e) {
1792                        // Null out values (so they aren't cached) and re-throw
1793                        values = null;
1794                        throw e;
1795                    } finally {
1796                        if (cache != null) {
1797                            cache.unlock(id, values);
1798                        }
1799                    }
1800                    if (match == MESSAGE_ID || match == SYNCED_MESSAGE_ID) {
1801                        if (!uri.getBooleanQueryParameter(IS_UIPROVIDER, false)) {
1802                            notifyUIConversation(uri);
1803                        }
1804                    } else if (match == ATTACHMENT_ID) {
1805                        long attId = Integer.parseInt(id);
1806                        if (values.containsKey(Attachment.FLAGS)) {
1807                            int flags = values.getAsInteger(Attachment.FLAGS);
1808                            mAttachmentService.attachmentChanged(context, attId, flags);
1809                        }
1810                        // Notify UI if necessary; there are only two columns we can change that
1811                        // would be worth a notification
1812                        if (values.containsKey(AttachmentColumns.UI_STATE) ||
1813                                values.containsKey(AttachmentColumns.UI_DOWNLOADED_SIZE)) {
1814                            // Notify on individual attachment
1815                            notifyUI(UIPROVIDER_ATTACHMENT_NOTIFIER, id);
1816                            Attachment att = Attachment.restoreAttachmentWithId(context, attId);
1817                            if (att != null) {
1818                                // And on owning Message
1819                                notifyUI(UIPROVIDER_ATTACHMENTS_NOTIFIER, att.mMessageKey);
1820                            }
1821                        }
1822                    } else if (match == MAILBOX_ID && values.containsKey(Mailbox.UI_SYNC_STATUS)) {
1823                        notifyUI(UIPROVIDER_FOLDER_NOTIFIER, id);
1824                    } else if (match == ACCOUNT_ID) {
1825                        // Notify individual account and "all accounts"
1826                        notifyUI(UIPROVIDER_ACCOUNT_NOTIFIER, id);
1827                        resolver.notifyChange(UIPROVIDER_ALL_ACCOUNTS_NOTIFIER, null);
1828                    }
1829                    break;
1830                case BODY:
1831                case MESSAGE:
1832                case UPDATED_MESSAGE:
1833                case ATTACHMENT:
1834                case MAILBOX:
1835                case ACCOUNT:
1836                case HOSTAUTH:
1837                case POLICY:
1838                    switch(match) {
1839                        // To avoid invalidating the cache on updates, we execute them one at a
1840                        // time using the XXX_ID uri; these are all executed atomically
1841                        case ACCOUNT:
1842                        case MAILBOX:
1843                        case HOSTAUTH:
1844                        case POLICY:
1845                            Cursor c = db.query(tableName, EmailContent.ID_PROJECTION,
1846                                    selection, selectionArgs, null, null, null);
1847                            db.beginTransaction();
1848                            result = 0;
1849                            try {
1850                                while (c.moveToNext()) {
1851                                    update(ContentUris.withAppendedId(
1852                                                uri, c.getLong(EmailContent.ID_PROJECTION_COLUMN)),
1853                                            values, null, null);
1854                                    result++;
1855                                }
1856                                db.setTransactionSuccessful();
1857                            } finally {
1858                                db.endTransaction();
1859                                c.close();
1860                            }
1861                            break outer;
1862                        // Any cached table other than those above should be invalidated here
1863                        case MESSAGE:
1864                            // If we're doing some generic update, the whole cache needs to be
1865                            // invalidated.  This case should be quite rare
1866                            cache.invalidate("Update", uri, selection);
1867                            //$FALL-THROUGH$
1868                        default:
1869                            result = db.update(tableName, values, selection, selectionArgs);
1870                            break outer;
1871                    }
1872                case ACCOUNT_RESET_NEW_COUNT_ID:
1873                    id = uri.getPathSegments().get(1);
1874                    if (cache != null) {
1875                        cache.lock(id);
1876                    }
1877                    ContentValues newMessageCount = CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT;
1878                    if (values != null) {
1879                        Long set = values.getAsLong(EmailContent.SET_COLUMN_NAME);
1880                        if (set != null) {
1881                            newMessageCount = new ContentValues();
1882                            newMessageCount.put(Account.NEW_MESSAGE_COUNT, set);
1883                        }
1884                    }
1885                    try {
1886                        result = db.update(tableName, newMessageCount,
1887                                whereWithId(id, selection), selectionArgs);
1888                    } finally {
1889                        if (cache != null) {
1890                            cache.unlock(id, values);
1891                        }
1892                    }
1893                    notificationUri = Account.CONTENT_URI; // Only notify account cursors.
1894                    break;
1895                case ACCOUNT_RESET_NEW_COUNT:
1896                    result = db.update(tableName, CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT,
1897                            selection, selectionArgs);
1898                    // Affects all accounts.  Just invalidate all account cache.
1899                    cache.invalidate("Reset all new counts", null, null);
1900                    notificationUri = Account.CONTENT_URI; // Only notify account cursors.
1901                    break;
1902                default:
1903                    throw new IllegalArgumentException("Unknown URI " + uri);
1904            }
1905        } catch (SQLiteException e) {
1906            checkDatabases();
1907            throw e;
1908        }
1909
1910        // Notify all notifier cursors
1911        sendNotifierChange(getBaseNotificationUri(match), NOTIFICATION_OP_UPDATE, id);
1912
1913        resolver.notifyChange(notificationUri, null);
1914        return result;
1915    }
1916
1917    /**
1918     * Returns the base notification URI for the given content type.
1919     *
1920     * @param match The type of content that was modified.
1921     */
1922    private Uri getBaseNotificationUri(int match) {
1923        Uri baseUri = null;
1924        switch (match) {
1925            case MESSAGE:
1926            case MESSAGE_ID:
1927            case SYNCED_MESSAGE_ID:
1928                baseUri = Message.NOTIFIER_URI;
1929                break;
1930            case ACCOUNT:
1931            case ACCOUNT_ID:
1932                baseUri = Account.NOTIFIER_URI;
1933                break;
1934        }
1935        return baseUri;
1936    }
1937
1938    /**
1939     * Sends a change notification to any cursors observers of the given base URI. The final
1940     * notification URI is dynamically built to contain the specified information. It will be
1941     * of the format <<baseURI>>/<<op>>/<<id>>; where <<op>> and <<id>> are optional depending
1942     * upon the given values.
1943     * NOTE: If <<op>> is specified, notifications for <<baseURI>>/<<id>> will NOT be invoked.
1944     * If this is necessary, it can be added. However, due to the implementation of
1945     * {@link ContentObserver}, observers of <<baseURI>> will receive multiple notifications.
1946     *
1947     * @param baseUri The base URI to send notifications to. Must be able to take appended IDs.
1948     * @param op Optional operation to be appended to the URI.
1949     * @param id If a positive value, the ID to append to the base URI. Otherwise, no ID will be
1950     *           appended to the base URI.
1951     */
1952    private void sendNotifierChange(Uri baseUri, String op, String id) {
1953        if (baseUri == null) return;
1954
1955        final ContentResolver resolver = getContext().getContentResolver();
1956
1957        // Append the operation, if specified
1958        if (op != null) {
1959            baseUri = baseUri.buildUpon().appendEncodedPath(op).build();
1960        }
1961
1962        long longId = 0L;
1963        try {
1964            longId = Long.valueOf(id);
1965        } catch (NumberFormatException ignore) {}
1966        if (longId > 0) {
1967            resolver.notifyChange(ContentUris.withAppendedId(baseUri, longId), null);
1968        } else {
1969            resolver.notifyChange(baseUri, null);
1970        }
1971
1972        // We want to send the message list changed notification if baseUri is Message.NOTIFIER_URI.
1973        if (baseUri.equals(Message.NOTIFIER_URI)) {
1974            sendMessageListDataChangedNotification();
1975        }
1976    }
1977
1978    private void sendMessageListDataChangedNotification() {
1979        final Context context = getContext();
1980        final Intent intent = new Intent(ACTION_NOTIFY_MESSAGE_LIST_DATASET_CHANGED);
1981        // Ideally this intent would contain information about which account changed, to limit the
1982        // updates to that particular account.  Unfortunately, that information is not available in
1983        // sendNotifierChange().
1984        context.sendBroadcast(intent);
1985    }
1986
1987    @Override
1988    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
1989            throws OperationApplicationException {
1990        Context context = getContext();
1991        SQLiteDatabase db = getDatabase(context);
1992        db.beginTransaction();
1993        try {
1994            ContentProviderResult[] results = super.applyBatch(operations);
1995            db.setTransactionSuccessful();
1996            return results;
1997        } finally {
1998            db.endTransaction();
1999        }
2000    }
2001
2002    /**
2003     * For testing purposes, check whether a given row is cached
2004     * @param baseUri the base uri of the EmailContent
2005     * @param id the row id of the EmailContent
2006     * @return whether or not the row is currently cached
2007     */
2008    @VisibleForTesting
2009    protected boolean isCached(Uri baseUri, long id) {
2010        int match = findMatch(baseUri, "isCached");
2011        int table = match >> BASE_SHIFT;
2012        ContentCache cache = mContentCaches[table];
2013        if (cache == null) return false;
2014        Cursor cc = cache.get(Long.toString(id));
2015        return (cc != null);
2016    }
2017
2018    public static interface AttachmentService {
2019        /**
2020         * Notify the service that an attachment has changed.
2021         */
2022        void attachmentChanged(Context context, long id, int flags);
2023    }
2024
2025    private final AttachmentService DEFAULT_ATTACHMENT_SERVICE = new AttachmentService() {
2026        @Override
2027        public void attachmentChanged(Context context, long id, int flags) {
2028            // The default implementation delegates to the real service.
2029            AttachmentDownloadService.attachmentChanged(context, id, flags);
2030        }
2031    };
2032    private AttachmentService mAttachmentService = DEFAULT_ATTACHMENT_SERVICE;
2033
2034    /**
2035     * Injects a custom attachment service handler. If null is specified, will reset to the
2036     * default service.
2037     */
2038    public void injectAttachmentService(AttachmentService as) {
2039        mAttachmentService = (as == null) ? DEFAULT_ATTACHMENT_SERVICE : as;
2040    }
2041
2042    // SELECT DISTINCT Boxes._id, Boxes.unreadCount count(Message._id) from Message,
2043    //   (SELECT _id, unreadCount, messageCount, lastNotifiedMessageCount, lastNotifiedMessageKey
2044    //   FROM Mailbox WHERE accountKey=6 AND ((type = 0) OR (syncInterval!=0 AND syncInterval!=-1)))
2045    //      AS Boxes
2046    // WHERE Boxes.messageCount!=Boxes.lastNotifiedMessageCount
2047    //   OR (Boxes._id=Message.mailboxKey AND Message._id>Boxes.lastNotifiedMessageKey)
2048    // TODO: This query can be simplified a bit
2049    private static final String NOTIFICATION_QUERY =
2050        "SELECT DISTINCT Boxes." + MailboxColumns.ID + ", Boxes." + MailboxColumns.UNREAD_COUNT +
2051            ", count(" + Message.TABLE_NAME + "." + MessageColumns.ID + ")" +
2052        " FROM " +
2053            Message.TABLE_NAME + "," +
2054            "(SELECT " + MailboxColumns.ID + "," + MailboxColumns.UNREAD_COUNT + "," +
2055                MailboxColumns.MESSAGE_COUNT + "," + MailboxColumns.LAST_NOTIFIED_MESSAGE_COUNT +
2056                "," + MailboxColumns.LAST_NOTIFIED_MESSAGE_KEY + " FROM " + Mailbox.TABLE_NAME +
2057                " WHERE " + MailboxColumns.ACCOUNT_KEY + "=?" +
2058                " AND (" + MailboxColumns.TYPE + "=" + Mailbox.TYPE_INBOX + " OR ("
2059                + MailboxColumns.SYNC_INTERVAL + "!=0 AND " +
2060                MailboxColumns.SYNC_INTERVAL + "!=-1))) AS Boxes " +
2061        "WHERE Boxes." + MailboxColumns.ID + '=' + Message.TABLE_NAME + "." +
2062                MessageColumns.MAILBOX_KEY + " AND " + Message.TABLE_NAME + "." +
2063                MessageColumns.ID + ">Boxes." + MailboxColumns.LAST_NOTIFIED_MESSAGE_KEY +
2064                " AND " + MessageColumns.FLAG_READ + "=0 AND " + MessageColumns.TIMESTAMP + "!=0";
2065
2066    public Cursor notificationQuery(Uri uri) {
2067        SQLiteDatabase db = getDatabase(getContext());
2068        String accountId = uri.getLastPathSegment();
2069        return db.rawQuery(NOTIFICATION_QUERY, new String[] {accountId});
2070   }
2071
2072    public Cursor mostRecentMessageQuery(Uri uri) {
2073        SQLiteDatabase db = getDatabase(getContext());
2074        String mailboxId = uri.getLastPathSegment();
2075        return db.rawQuery("select max(_id) from Message where mailboxKey=?",
2076                new String[] {mailboxId});
2077   }
2078
2079    /**
2080     * Support for UnifiedEmail below
2081     */
2082
2083    private static final String NOT_A_DRAFT_STRING =
2084        Integer.toString(UIProvider.DraftType.NOT_A_DRAFT);
2085
2086    private static final String CONVERSATION_FLAGS =
2087            "CASE WHEN (" + MessageColumns.FLAGS + "&" + Message.FLAG_INCOMING_MEETING_INVITE +
2088                ") !=0 THEN " + UIProvider.ConversationFlags.CALENDAR_INVITE +
2089                " ELSE 0 END + " +
2090            "CASE WHEN (" + MessageColumns.FLAGS + "&" + Message.FLAG_FORWARDED +
2091                ") !=0 THEN " + UIProvider.ConversationFlags.FORWARDED +
2092                " ELSE 0 END + " +
2093             "CASE WHEN (" + MessageColumns.FLAGS + "&" + Message.FLAG_REPLIED_TO +
2094                ") !=0 THEN " + UIProvider.ConversationFlags.REPLIED +
2095                " ELSE 0 END";
2096
2097    /**
2098     * Array of pre-defined account colors (legacy colors from old email app)
2099     */
2100    private static final int[] ACCOUNT_COLORS = new int[] {
2101        0xff71aea7, 0xff621919, 0xff18462f, 0xffbf8e52, 0xff001f79,
2102        0xffa8afc2, 0xff6b64c4, 0xff738359, 0xff9d50a4
2103    };
2104
2105    private static final String CONVERSATION_COLOR =
2106            "@CASE (" + MessageColumns.ACCOUNT_KEY + " - 1) % " + ACCOUNT_COLORS.length +
2107                    " WHEN 0 THEN " + ACCOUNT_COLORS[0] +
2108                    " WHEN 1 THEN " + ACCOUNT_COLORS[1] +
2109                    " WHEN 2 THEN " + ACCOUNT_COLORS[2] +
2110                    " WHEN 3 THEN " + ACCOUNT_COLORS[3] +
2111                    " WHEN 4 THEN " + ACCOUNT_COLORS[4] +
2112                    " WHEN 5 THEN " + ACCOUNT_COLORS[5] +
2113                    " WHEN 6 THEN " + ACCOUNT_COLORS[6] +
2114                    " WHEN 7 THEN " + ACCOUNT_COLORS[7] +
2115                    " WHEN 8 THEN " + ACCOUNT_COLORS[8] +
2116            " END";
2117
2118    private static final String ACCOUNT_COLOR =
2119            "@CASE (" + AccountColumns.ID + " - 1) % " + ACCOUNT_COLORS.length +
2120                    " WHEN 0 THEN " + ACCOUNT_COLORS[0] +
2121                    " WHEN 1 THEN " + ACCOUNT_COLORS[1] +
2122                    " WHEN 2 THEN " + ACCOUNT_COLORS[2] +
2123                    " WHEN 3 THEN " + ACCOUNT_COLORS[3] +
2124                    " WHEN 4 THEN " + ACCOUNT_COLORS[4] +
2125                    " WHEN 5 THEN " + ACCOUNT_COLORS[5] +
2126                    " WHEN 6 THEN " + ACCOUNT_COLORS[6] +
2127                    " WHEN 7 THEN " + ACCOUNT_COLORS[7] +
2128                    " WHEN 8 THEN " + ACCOUNT_COLORS[8] +
2129            " END";
2130    /**
2131     * Mapping of UIProvider columns to EmailProvider columns for the message list (called the
2132     * conversation list in UnifiedEmail)
2133     */
2134    private ProjectionMap getMessageListMap() {
2135        if (sMessageListMap == null) {
2136            sMessageListMap = ProjectionMap.builder()
2137                .add(BaseColumns._ID, MessageColumns.ID)
2138                .add(UIProvider.ConversationColumns.URI, uriWithId("uimessage"))
2139                .add(UIProvider.ConversationColumns.MESSAGE_LIST_URI, uriWithId("uimessage"))
2140                .add(UIProvider.ConversationColumns.SUBJECT, MessageColumns.SUBJECT)
2141                .add(UIProvider.ConversationColumns.SNIPPET, MessageColumns.SNIPPET)
2142                .add(UIProvider.ConversationColumns.CONVERSATION_INFO, null)
2143                .add(UIProvider.ConversationColumns.DATE_RECEIVED_MS, MessageColumns.TIMESTAMP)
2144                .add(UIProvider.ConversationColumns.HAS_ATTACHMENTS, MessageColumns.FLAG_ATTACHMENT)
2145                .add(UIProvider.ConversationColumns.NUM_MESSAGES, "1")
2146                .add(UIProvider.ConversationColumns.NUM_DRAFTS, "0")
2147                .add(UIProvider.ConversationColumns.SENDING_STATE,
2148                        Integer.toString(ConversationSendingState.OTHER))
2149                .add(UIProvider.ConversationColumns.PRIORITY,
2150                        Integer.toString(ConversationPriority.LOW))
2151                .add(UIProvider.ConversationColumns.READ, MessageColumns.FLAG_READ)
2152                .add(UIProvider.ConversationColumns.STARRED, MessageColumns.FLAG_FAVORITE)
2153                .add(UIProvider.ConversationColumns.FLAGS, CONVERSATION_FLAGS)
2154                .add(UIProvider.ConversationColumns.ACCOUNT_URI,
2155                        uriWithColumn("uiaccount", MessageColumns.ACCOUNT_KEY))
2156                .add(UIProvider.ConversationColumns.SENDER_INFO, MessageColumns.FROM_LIST)
2157                .build();
2158        }
2159        return sMessageListMap;
2160    }
2161    private static ProjectionMap sMessageListMap;
2162
2163    /**
2164     * Generate UIProvider draft type; note the test for "reply all" must come before "reply"
2165     */
2166    private static final String MESSAGE_DRAFT_TYPE =
2167        "CASE WHEN (" + MessageColumns.FLAGS + "&" + Message.FLAG_TYPE_ORIGINAL +
2168            ") !=0 THEN " + UIProvider.DraftType.COMPOSE +
2169        " WHEN (" + MessageColumns.FLAGS + "&" + (1<<20) +
2170            ") !=0 THEN " + UIProvider.DraftType.REPLY_ALL +
2171        " WHEN (" + MessageColumns.FLAGS + "&" + Message.FLAG_TYPE_REPLY +
2172            ") !=0 THEN " + UIProvider.DraftType.REPLY +
2173        " WHEN (" + MessageColumns.FLAGS + "&" + Message.FLAG_TYPE_FORWARD +
2174            ") !=0 THEN " + UIProvider.DraftType.FORWARD +
2175            " ELSE " + UIProvider.DraftType.NOT_A_DRAFT + " END";
2176
2177    private static final String MESSAGE_FLAGS =
2178            "CASE WHEN (" + MessageColumns.FLAGS + "&" + Message.FLAG_INCOMING_MEETING_INVITE +
2179            ") !=0 THEN " + UIProvider.MessageFlags.CALENDAR_INVITE +
2180            " ELSE 0 END";
2181
2182    /**
2183     * Mapping of UIProvider columns to EmailProvider columns for a detailed message view in
2184     * UnifiedEmail
2185     */
2186    private ProjectionMap getMessageViewMap() {
2187        if (sMessageViewMap == null) {
2188            sMessageViewMap = ProjectionMap.builder()
2189                .add(BaseColumns._ID, Message.TABLE_NAME + "." + EmailContent.MessageColumns.ID)
2190                .add(UIProvider.MessageColumns.SERVER_ID, SyncColumns.SERVER_ID)
2191                .add(UIProvider.MessageColumns.URI, uriWithFQId("uimessage", Message.TABLE_NAME))
2192                .add(UIProvider.MessageColumns.CONVERSATION_ID,
2193                        uriWithFQId("uimessage", Message.TABLE_NAME))
2194                .add(UIProvider.MessageColumns.SUBJECT, EmailContent.MessageColumns.SUBJECT)
2195                .add(UIProvider.MessageColumns.SNIPPET, EmailContent.MessageColumns.SNIPPET)
2196                .add(UIProvider.MessageColumns.FROM, EmailContent.MessageColumns.FROM_LIST)
2197                .add(UIProvider.MessageColumns.TO, EmailContent.MessageColumns.TO_LIST)
2198                .add(UIProvider.MessageColumns.CC, EmailContent.MessageColumns.CC_LIST)
2199                .add(UIProvider.MessageColumns.BCC, EmailContent.MessageColumns.BCC_LIST)
2200                .add(UIProvider.MessageColumns.REPLY_TO, EmailContent.MessageColumns.REPLY_TO_LIST)
2201                .add(UIProvider.MessageColumns.DATE_RECEIVED_MS,
2202                        EmailContent.MessageColumns.TIMESTAMP)
2203                .add(UIProvider.MessageColumns.BODY_HTML, Body.HTML_CONTENT)
2204                .add(UIProvider.MessageColumns.BODY_TEXT, Body.TEXT_CONTENT)
2205                .add(UIProvider.MessageColumns.REF_MESSAGE_ID, "0")
2206                .add(UIProvider.MessageColumns.DRAFT_TYPE, NOT_A_DRAFT_STRING)
2207                .add(UIProvider.MessageColumns.APPEND_REF_MESSAGE_CONTENT, "0")
2208                .add(UIProvider.MessageColumns.HAS_ATTACHMENTS,
2209                        EmailContent.MessageColumns.FLAG_ATTACHMENT)
2210                .add(UIProvider.MessageColumns.ATTACHMENT_LIST_URI,
2211                        uriWithFQId("uiattachments", Message.TABLE_NAME))
2212                .add(UIProvider.MessageColumns.MESSAGE_FLAGS, MESSAGE_FLAGS)
2213                .add(UIProvider.MessageColumns.SAVE_MESSAGE_URI,
2214                        uriWithFQId("uiupdatedraft", Message.TABLE_NAME))
2215                .add(UIProvider.MessageColumns.SEND_MESSAGE_URI,
2216                        uriWithFQId("uisenddraft", Message.TABLE_NAME))
2217                .add(UIProvider.MessageColumns.DRAFT_TYPE, MESSAGE_DRAFT_TYPE)
2218                .add(UIProvider.MessageColumns.MESSAGE_ACCOUNT_URI,
2219                        uriWithColumn("account", MessageColumns.ACCOUNT_KEY))
2220                .add(UIProvider.MessageColumns.STARRED, EmailContent.MessageColumns.FLAG_FAVORITE)
2221                .add(UIProvider.MessageColumns.READ, EmailContent.MessageColumns.FLAG_READ)
2222                .add(UIProvider.MessageColumns.SPAM_WARNING_STRING, null)
2223                .add(UIProvider.MessageColumns.SPAM_WARNING_LEVEL,
2224                        Integer.toString(UIProvider.SpamWarningLevel.NO_WARNING))
2225                .add(UIProvider.MessageColumns.SPAM_WARNING_LINK_TYPE,
2226                        Integer.toString(UIProvider.SpamWarningLinkType.NO_LINK))
2227                .add(UIProvider.MessageColumns.VIA_DOMAIN, null)
2228                .build();
2229        }
2230        return sMessageViewMap;
2231    }
2232    private static ProjectionMap sMessageViewMap;
2233
2234    /**
2235     * Generate UIProvider folder capabilities from mailbox flags
2236     */
2237    private static final String FOLDER_CAPABILITIES =
2238        "CASE WHEN (" + MailboxColumns.FLAGS + "&" + Mailbox.FLAG_ACCEPTS_MOVED_MAIL +
2239            ") !=0 THEN " + UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES +
2240            " ELSE 0 END";
2241
2242    /**
2243     * Convert EmailProvider type to UIProvider type
2244     */
2245    private static final String FOLDER_TYPE = "CASE " + MailboxColumns.TYPE
2246            + " WHEN " + Mailbox.TYPE_INBOX   + " THEN " + UIProvider.FolderType.INBOX
2247            + " WHEN " + Mailbox.TYPE_DRAFTS  + " THEN " + UIProvider.FolderType.DRAFT
2248            + " WHEN " + Mailbox.TYPE_OUTBOX  + " THEN " + UIProvider.FolderType.OUTBOX
2249            + " WHEN " + Mailbox.TYPE_SENT    + " THEN " + UIProvider.FolderType.SENT
2250            + " WHEN " + Mailbox.TYPE_TRASH   + " THEN " + UIProvider.FolderType.TRASH
2251            + " WHEN " + Mailbox.TYPE_JUNK    + " THEN " + UIProvider.FolderType.SPAM
2252            + " WHEN " + Mailbox.TYPE_STARRED + " THEN " + UIProvider.FolderType.STARRED
2253            + " ELSE " + UIProvider.FolderType.DEFAULT + " END";
2254
2255    private static final String FOLDER_ICON = "CASE " + MailboxColumns.TYPE
2256            + " WHEN " + Mailbox.TYPE_INBOX   + " THEN " + R.drawable.ic_folder_inbox_holo_light
2257            + " WHEN " + Mailbox.TYPE_DRAFTS  + " THEN " + R.drawable.ic_folder_drafts_holo_light
2258            + " WHEN " + Mailbox.TYPE_OUTBOX  + " THEN " + R.drawable.ic_folder_outbox_holo_light
2259            + " WHEN " + Mailbox.TYPE_SENT    + " THEN " + R.drawable.ic_folder_sent_holo_light
2260            + " WHEN " + Mailbox.TYPE_STARRED + " THEN " + R.drawable.ic_menu_star_holo_light
2261            + " ELSE -1 END";
2262
2263    private ProjectionMap getFolderListMap() {
2264        if (sFolderListMap == null) {
2265            sFolderListMap = ProjectionMap.builder()
2266                .add(BaseColumns._ID, MailboxColumns.ID)
2267                .add(UIProvider.FolderColumns.URI, uriWithId("uifolder"))
2268                .add(UIProvider.FolderColumns.NAME, "displayName")
2269                .add(UIProvider.FolderColumns.HAS_CHILDREN,
2270                        MailboxColumns.FLAGS + "&" + Mailbox.FLAG_HAS_CHILDREN)
2271                .add(UIProvider.FolderColumns.CAPABILITIES, FOLDER_CAPABILITIES)
2272                .add(UIProvider.FolderColumns.SYNC_WINDOW, "3")
2273                .add(UIProvider.FolderColumns.CONVERSATION_LIST_URI, uriWithId("uimessages"))
2274                .add(UIProvider.FolderColumns.CHILD_FOLDERS_LIST_URI, uriWithId("uisubfolders"))
2275                .add(UIProvider.FolderColumns.UNREAD_COUNT, MailboxColumns.UNREAD_COUNT)
2276                .add(UIProvider.FolderColumns.TOTAL_COUNT, MailboxColumns.MESSAGE_COUNT)
2277                .add(UIProvider.FolderColumns.REFRESH_URI, uriWithId("uirefresh"))
2278                .add(UIProvider.FolderColumns.SYNC_STATUS, MailboxColumns.UI_SYNC_STATUS)
2279                .add(UIProvider.FolderColumns.LAST_SYNC_RESULT, MailboxColumns.UI_LAST_SYNC_RESULT)
2280                .add(UIProvider.FolderColumns.TYPE, FOLDER_TYPE)
2281                .add(UIProvider.FolderColumns.ICON_RES_ID, FOLDER_ICON)
2282                .add(UIProvider.FolderColumns.HIERARCHICAL_DESC, MailboxColumns.HIERARCHICAL_NAME)
2283                .build();
2284        }
2285        return sFolderListMap;
2286    }
2287    private static ProjectionMap sFolderListMap;
2288
2289    private ProjectionMap getAccountListMap() {
2290        if (sAccountListMap == null) {
2291            sAccountListMap = ProjectionMap.builder()
2292                .add(BaseColumns._ID, AccountColumns.ID)
2293                .add(UIProvider.AccountColumns.FOLDER_LIST_URI, uriWithId("uifolders"))
2294                .add(UIProvider.AccountColumns.FULL_FOLDER_LIST_URI, uriWithId("uiallfolders"))
2295                .add(UIProvider.AccountColumns.NAME, AccountColumns.DISPLAY_NAME)
2296                .add(UIProvider.AccountColumns.SAVE_DRAFT_URI, uriWithId("uisavedraft"))
2297                .add(UIProvider.AccountColumns.SEND_MAIL_URI, uriWithId("uisendmail"))
2298                .add(UIProvider.AccountColumns.UNDO_URI,
2299                        ("'content://" + UIProvider.AUTHORITY + "/uiundo'"))
2300                .add(UIProvider.AccountColumns.URI, uriWithId("uiaccount"))
2301                .add(UIProvider.AccountColumns.SEARCH_URI, uriWithId("uisearch"))
2302                // TODO: Is provider version used?
2303                .add(UIProvider.AccountColumns.PROVIDER_VERSION, "1")
2304                .add(UIProvider.AccountColumns.SYNC_STATUS, "0")
2305                .add(UIProvider.AccountColumns.RECENT_FOLDER_LIST_URI, uriWithId("uirecentfolders"))
2306                .add(UIProvider.AccountColumns.DEFAULT_RECENT_FOLDER_LIST_URI,
2307                        uriWithId("uidefaultrecentfolders"))
2308                .add(UIProvider.AccountColumns.SettingsColumns.SIGNATURE, AccountColumns.SIGNATURE)
2309                .add(UIProvider.AccountColumns.SettingsColumns.SNAP_HEADERS,
2310                        Integer.toString(UIProvider.SnapHeaderValue.ALWAYS))
2311                .add(UIProvider.AccountColumns.SettingsColumns.REPLY_BEHAVIOR,
2312                        Integer.toString(UIProvider.DefaultReplyBehavior.REPLY))
2313                .add(UIProvider.AccountColumns.SettingsColumns.CONFIRM_ARCHIVE, "0")
2314                .build();
2315        }
2316        return sAccountListMap;
2317    }
2318    private static ProjectionMap sAccountListMap;
2319
2320    /**
2321     * The "ORDER BY" clause for top level folders
2322     */
2323    private static final String MAILBOX_ORDER_BY = "CASE " + MailboxColumns.TYPE
2324        + " WHEN " + Mailbox.TYPE_INBOX   + " THEN 0"
2325        + " WHEN " + Mailbox.TYPE_DRAFTS  + " THEN 1"
2326        + " WHEN " + Mailbox.TYPE_OUTBOX  + " THEN 2"
2327        + " WHEN " + Mailbox.TYPE_SENT    + " THEN 3"
2328        + " WHEN " + Mailbox.TYPE_TRASH   + " THEN 4"
2329        + " WHEN " + Mailbox.TYPE_JUNK    + " THEN 5"
2330        // Other mailboxes (i.e. of Mailbox.TYPE_MAIL) are shown in alphabetical order.
2331        + " ELSE 10 END"
2332        + " ," + MailboxColumns.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
2333
2334    /**
2335     * Mapping of UIProvider columns to EmailProvider columns for a message's attachments
2336     */
2337    private ProjectionMap getAttachmentMap() {
2338        if (sAttachmentMap == null) {
2339            sAttachmentMap = ProjectionMap.builder()
2340                .add(UIProvider.AttachmentColumns.NAME, AttachmentColumns.FILENAME)
2341                .add(UIProvider.AttachmentColumns.SIZE, AttachmentColumns.SIZE)
2342                .add(UIProvider.AttachmentColumns.URI, uriWithId("uiattachment"))
2343                .add(UIProvider.AttachmentColumns.CONTENT_TYPE, AttachmentColumns.MIME_TYPE)
2344                .add(UIProvider.AttachmentColumns.STATE, AttachmentColumns.UI_STATE)
2345                .add(UIProvider.AttachmentColumns.DESTINATION, AttachmentColumns.UI_DESTINATION)
2346                .add(UIProvider.AttachmentColumns.DOWNLOADED_SIZE,
2347                        AttachmentColumns.UI_DOWNLOADED_SIZE)
2348                .add(UIProvider.AttachmentColumns.CONTENT_URI, AttachmentColumns.CONTENT_URI)
2349                .build();
2350        }
2351        return sAttachmentMap;
2352    }
2353    private static ProjectionMap sAttachmentMap;
2354
2355    /**
2356     * Generate the SELECT clause using a specified mapping and the original UI projection
2357     * @param map the ProjectionMap to use for this projection
2358     * @param projection the projection as sent by UnifiedEmail
2359     * @param values ContentValues to be used if the ProjectionMap entry is null
2360     * @return a StringBuilder containing the SELECT expression for a SQLite query
2361     */
2362    private StringBuilder genSelect(ProjectionMap map, String[] projection) {
2363        return genSelect(map, projection, EMPTY_CONTENT_VALUES);
2364    }
2365
2366    private StringBuilder genSelect(ProjectionMap map, String[] projection, ContentValues values) {
2367        StringBuilder sb = new StringBuilder("SELECT ");
2368        boolean first = true;
2369        for (String column: projection) {
2370            if (first) {
2371                first = false;
2372            } else {
2373                sb.append(',');
2374            }
2375            String val = null;
2376            // First look at values; this is an override of default behavior
2377            if (values.containsKey(column)) {
2378                String value = values.getAsString(column);
2379                if (value == null) {
2380                    val = "NULL AS " + column;
2381                } else if (value.startsWith("@")) {
2382                    val = value.substring(1) + " AS " + column;
2383                } else {
2384                    val = "'" + value + "' AS " + column;
2385                }
2386            } else {
2387                // Now, get the standard value for the column from our projection map
2388                val = map.get(column);
2389                // If we don't have the column, return "NULL AS <column>", and warn
2390                if (val == null) {
2391                    val = "NULL AS " + column;
2392                }
2393            }
2394            sb.append(val);
2395        }
2396        return sb;
2397    }
2398
2399    /**
2400     * Convenience method to create a Uri string given the "type" of query; we append the type
2401     * of the query and the id column name (_id)
2402     *
2403     * @param type the "type" of the query, as defined by our UriMatcher definitions
2404     * @return a Uri string
2405     */
2406    private static String uriWithId(String type) {
2407        return uriWithColumn(type, EmailContent.RECORD_ID);
2408    }
2409
2410    /**
2411     * Convenience method to create a Uri string given the "type" of query; we append the type
2412     * of the query and the passed in column name
2413     *
2414     * @param type the "type" of the query, as defined by our UriMatcher definitions
2415     * @param columnName the column in the table being queried
2416     * @return a Uri string
2417     */
2418    private static String uriWithColumn(String type, String columnName) {
2419        return "'content://" + EmailContent.AUTHORITY + "/" + type + "/' || " + columnName;
2420    }
2421
2422    /**
2423     * Convenience method to create a Uri string given the "type" of query and the table name to
2424     * which it applies; we append the type of the query and the fully qualified (FQ) id column
2425     * (i.e. including the table name); we need this for join queries where _id would otherwise
2426     * be ambiguous
2427     *
2428     * @param type the "type" of the query, as defined by our UriMatcher definitions
2429     * @param tableName the name of the table whose _id is referred to
2430     * @return a Uri string
2431     */
2432    private static String uriWithFQId(String type, String tableName) {
2433        return "'content://" + EmailContent.AUTHORITY + "/" + type + "/' || " + tableName + "._id";
2434    }
2435
2436    // Regex that matches start of img tag. '<(?i)img\s+'.
2437    private static final Pattern IMG_TAG_START_REGEX = Pattern.compile("<(?i)img\\s+");
2438
2439    /**
2440     * Class that holds the sqlite query and the attachment (JSON) value (which might be null)
2441     */
2442    private static class MessageQuery {
2443        final String query;
2444        final String attachmentJson;
2445
2446        MessageQuery(String _query, String _attachmentJson) {
2447            query = _query;
2448            attachmentJson = _attachmentJson;
2449        }
2450    }
2451
2452    /**
2453     * Generate the "view message" SQLite query, given a projection from UnifiedEmail
2454     *
2455     * @param uiProjection as passed from UnifiedEmail
2456     * @return the SQLite query to be executed on the EmailProvider database
2457     */
2458    private MessageQuery genQueryViewMessage(String[] uiProjection, String id) {
2459        Context context = getContext();
2460        long messageId = Long.parseLong(id);
2461        Message msg = Message.restoreMessageWithId(context, messageId);
2462        ContentValues values = new ContentValues();
2463        String attachmentJson = null;
2464        if (msg != null) {
2465            Body body = Body.restoreBodyWithMessageId(context, messageId);
2466            if (body != null) {
2467                if (body.mHtmlContent != null) {
2468                    if (IMG_TAG_START_REGEX.matcher(body.mHtmlContent).find()) {
2469                        values.put(UIProvider.MessageColumns.EMBEDS_EXTERNAL_RESOURCES, 1);
2470                    }
2471                }
2472            }
2473            Address[] fromList = Address.unpack(msg.mFrom);
2474            int autoShowImages = 0;
2475            Preferences prefs = Preferences.getPreferences(context);
2476            for (Address sender : fromList) {
2477                String email = sender.getAddress();
2478                if (prefs.shouldShowImagesFor(email)) {
2479                    autoShowImages = 1;
2480                    break;
2481                }
2482            }
2483            values.put(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, autoShowImages);
2484            // Add attachments...
2485            Attachment[] atts = Attachment.restoreAttachmentsWithMessageId(context, messageId);
2486            if (atts.length > 0) {
2487                ArrayList<com.android.mail.providers.Attachment> uiAtts =
2488                        new ArrayList<com.android.mail.providers.Attachment>();
2489                for (Attachment att : atts) {
2490                    if (att.mContentId != null && att.mContentUri != null) {
2491                        continue;
2492                    }
2493                    com.android.mail.providers.Attachment uiAtt =
2494                            new com.android.mail.providers.Attachment();
2495                    uiAtt.name = att.mFileName;
2496                    uiAtt.contentType = att.mMimeType;
2497                    uiAtt.size = (int) att.mSize;
2498                    uiAtt.uri = uiUri("uiattachment", att.mId);
2499                    uiAtts.add(uiAtt);
2500                }
2501                values.put(UIProvider.MessageColumns.ATTACHMENTS, "@?"); // @ for literal
2502                attachmentJson = com.android.mail.providers.Attachment.toJSONArray(uiAtts);
2503            }
2504            if (msg.mDraftInfo != 0) {
2505                values.put(UIProvider.MessageColumns.APPEND_REF_MESSAGE_CONTENT,
2506                        (msg.mDraftInfo & Message.DRAFT_INFO_APPEND_REF_MESSAGE) != 0 ? 1 : 0);
2507                values.put(UIProvider.MessageColumns.QUOTE_START_POS,
2508                        msg.mDraftInfo & Message.DRAFT_INFO_QUOTE_POS_MASK);
2509            }
2510            if ((msg.mFlags & Message.FLAG_INCOMING_MEETING_INVITE) != 0) {
2511                values.put(UIProvider.MessageColumns.EVENT_INTENT_URI,
2512                        "content://ui.email2.android.com/event/" + msg.mId);
2513            }
2514        }
2515        StringBuilder sb = genSelect(getMessageViewMap(), uiProjection, values);
2516        sb.append(" FROM " + Message.TABLE_NAME + "," + Body.TABLE_NAME + " WHERE " +
2517                Body.MESSAGE_KEY + "=" + Message.TABLE_NAME + "." + Message.RECORD_ID + " AND " +
2518                Message.TABLE_NAME + "." + Message.RECORD_ID + "=?");
2519        String sql = sb.toString();
2520        return new MessageQuery(sql, attachmentJson);
2521    }
2522
2523    /**
2524     * Generate the "message list" SQLite query, given a projection from UnifiedEmail
2525     *
2526     * @param uiProjection as passed from UnifiedEmail
2527     * @return the SQLite query to be executed on the EmailProvider database
2528     */
2529    private String genQueryMailboxMessages(String[] uiProjection) {
2530        StringBuilder sb = genSelect(getMessageListMap(), uiProjection);
2531        sb.append(" FROM " + Message.TABLE_NAME + " WHERE " + Message.MAILBOX_KEY + "=? ORDER BY " +
2532                MessageColumns.TIMESTAMP + " DESC");
2533        return sb.toString();
2534    }
2535
2536    /**
2537     * Generate various virtual mailbox SQLite queries, given a projection from UnifiedEmail
2538     *
2539     * @param uiProjection as passed from UnifiedEmail
2540     * @param id the id of the virtual mailbox
2541     * @return the SQLite query to be executed on the EmailProvider database
2542     */
2543    private Cursor getVirtualMailboxMessagesCursor(SQLiteDatabase db, String[] uiProjection,
2544            long mailboxId) {
2545        ContentValues values = new ContentValues();
2546        values.put(UIProvider.ConversationColumns.COLOR, CONVERSATION_COLOR);
2547        StringBuilder sb = genSelect(getMessageListMap(), uiProjection, values);
2548        if (isCombinedMailbox(mailboxId)) {
2549            switch (getVirtualMailboxType(mailboxId)) {
2550                case Mailbox.TYPE_INBOX:
2551                    sb.append(" FROM " + Message.TABLE_NAME + " WHERE " +
2552                            MessageColumns.MAILBOX_KEY + " IN (SELECT " + MailboxColumns.ID +
2553                            " FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.TYPE +
2554                            "=" + Mailbox.TYPE_INBOX + ") ORDER BY " + MessageColumns.TIMESTAMP +
2555                            " DESC");
2556                    break;
2557                case Mailbox.TYPE_STARRED:
2558                    sb.append(" FROM " + Message.TABLE_NAME + " WHERE " +
2559                            MessageColumns.FLAG_FAVORITE + "=1 ORDER BY " +
2560                            MessageColumns.TIMESTAMP + " DESC");
2561                    break;
2562                default:
2563                    throw new IllegalArgumentException("No virtual mailbox for: " + mailboxId);
2564            }
2565            return db.rawQuery(sb.toString(), null);
2566        } else {
2567            switch (getVirtualMailboxType(mailboxId)) {
2568                case Mailbox.TYPE_STARRED:
2569                    sb.append(" FROM " + Message.TABLE_NAME + " WHERE " +
2570                            MessageColumns.ACCOUNT_KEY + "=? AND " +
2571                            MessageColumns.FLAG_FAVORITE + "=1 ORDER BY " +
2572                            MessageColumns.TIMESTAMP + " DESC");
2573                    break;
2574                default:
2575                    throw new IllegalArgumentException("No virtual mailbox for: " + mailboxId);
2576            }
2577            return db.rawQuery(sb.toString(),
2578                    new String[] {getVirtualMailboxAccountIdString(mailboxId)});
2579        }
2580    }
2581
2582    /**
2583     * Generate the "message list" SQLite query, given a projection from UnifiedEmail
2584     *
2585     * @param uiProjection as passed from UnifiedEmail
2586     * @return the SQLite query to be executed on the EmailProvider database
2587     */
2588    private String genQueryConversation(String[] uiProjection) {
2589        StringBuilder sb = genSelect(getMessageListMap(), uiProjection);
2590        sb.append(" FROM " + Message.TABLE_NAME + " WHERE " + Message.RECORD_ID + "=?");
2591        return sb.toString();
2592    }
2593
2594    /**
2595     * Generate the "top level folder list" SQLite query, given a projection from UnifiedEmail
2596     *
2597     * @param uiProjection as passed from UnifiedEmail
2598     * @return the SQLite query to be executed on the EmailProvider database
2599     */
2600    private String genQueryAccountMailboxes(String[] uiProjection) {
2601        StringBuilder sb = genSelect(getFolderListMap(), uiProjection);
2602        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.ACCOUNT_KEY +
2603                "=? AND " + MailboxColumns.TYPE + " < " + Mailbox.TYPE_NOT_EMAIL +
2604                " AND " + MailboxColumns.PARENT_KEY + " < 0 ORDER BY ");
2605        sb.append(MAILBOX_ORDER_BY);
2606        return sb.toString();
2607    }
2608
2609    /**
2610     * Generate the "all folders" SQLite query, given a projection from UnifiedEmail.  The list is
2611     * sorted by the name as it appears in a hierarchical listing
2612     *
2613     * @param uiProjection as passed from UnifiedEmail
2614     * @return the SQLite query to be executed on the EmailProvider database
2615     */
2616    private String genQueryAccountAllMailboxes(String[] uiProjection) {
2617        StringBuilder sb = genSelect(getFolderListMap(), uiProjection);
2618        // Use a derived column to choose either hierarchicalName or displayName
2619        sb.append(", case when " + MailboxColumns.HIERARCHICAL_NAME + " is null then " +
2620                MailboxColumns.DISPLAY_NAME + " else " + MailboxColumns.HIERARCHICAL_NAME +
2621                " end as h_name");
2622        // Order by the derived column
2623        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.ACCOUNT_KEY +
2624                "=? AND " + MailboxColumns.TYPE + " < " + Mailbox.TYPE_NOT_EMAIL +
2625                " ORDER BY h_name");
2626        return sb.toString();
2627    }
2628
2629    /**
2630     * Generate the "recent folder list" SQLite query, given a projection from UnifiedEmail
2631     *
2632     * @param uiProjection as passed from UnifiedEmail
2633     * @return the SQLite query to be executed on the EmailProvider database
2634     */
2635    private String genQueryRecentMailboxes(String[] uiProjection) {
2636        StringBuilder sb = genSelect(getFolderListMap(), uiProjection);
2637        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.ACCOUNT_KEY +
2638                "=? AND " + MailboxColumns.TYPE + " < " + Mailbox.TYPE_NOT_EMAIL +
2639                " AND " + MailboxColumns.PARENT_KEY + " < 0 AND " +
2640                MailboxColumns.LAST_TOUCHED_TIME + " > 0 ORDER BY " +
2641                MailboxColumns.LAST_TOUCHED_TIME + " DESC");
2642        return sb.toString();
2643    }
2644
2645    private int getFolderCapabilities(EmailServiceInfo info, int flags, int type, long mailboxId) {
2646        // All folders support delete
2647        int caps = UIProvider.FolderCapabilities.DELETE;
2648        if (info != null && info.offerLookback) {
2649            // Protocols supporting lookback support settings
2650            caps |= UIProvider.FolderCapabilities.SUPPORTS_SETTINGS;
2651            if ((flags & Mailbox.FLAG_ACCEPTS_MOVED_MAIL) != 0) {
2652                // If the mailbox can accept moved mail, report that as well
2653                caps |= UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES;
2654            }
2655        }
2656        // For trash, we don't allow undo
2657        if (type == Mailbox.TYPE_TRASH) {
2658            caps =  UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES |
2659                    UIProvider.FolderCapabilities.CAN_HOLD_MAIL |
2660                    UIProvider.FolderCapabilities.DELETE |
2661                    UIProvider.FolderCapabilities.DELETE_ACTION_FINAL;
2662        }
2663        if (isVirtualMailbox(mailboxId)) {
2664            caps |= UIProvider.FolderCapabilities.IS_VIRTUAL;
2665        }
2666        return caps;
2667    }
2668
2669    /**
2670     * Generate a "single mailbox" SQLite query, given a projection from UnifiedEmail
2671     *
2672     * @param uiProjection as passed from UnifiedEmail
2673     * @return the SQLite query to be executed on the EmailProvider database
2674     */
2675    private String genQueryMailbox(String[] uiProjection, String id) {
2676        long mailboxId = Long.parseLong(id);
2677        ContentValues values = new ContentValues();
2678        if (mSearchParams != null && mailboxId == mSearchParams.mSearchMailboxId) {
2679            // This is the current search mailbox; use the total count
2680            values = new ContentValues();
2681            values.put(UIProvider.FolderColumns.TOTAL_COUNT, mSearchParams.mTotalCount);
2682            // "load more" is valid for search results
2683            values.put(UIProvider.FolderColumns.LOAD_MORE_URI,
2684                    uiUriString("uiloadmore", mailboxId));
2685        } else {
2686            Context context = getContext();
2687            Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
2688            // Make sure we can't get NPE if mailbox has disappeared (the result will end up moot)
2689            if (mailbox != null) {
2690                String protocol = Account.getProtocol(context, mailbox.mAccountKey);
2691                EmailServiceInfo info = EmailServiceUtils.getServiceInfo(context, protocol);
2692                // All folders support delete
2693                if (info != null && info.offerLoadMore) {
2694                    // "load more" is valid for protocols not supporting "lookback"
2695                    values.put(UIProvider.FolderColumns.LOAD_MORE_URI,
2696                            uiUriString("uiloadmore", mailboxId));
2697                }
2698                values.put(UIProvider.FolderColumns.CAPABILITIES,
2699                        getFolderCapabilities(info, mailbox.mFlags, mailbox.mType, mailboxId));
2700             }
2701        }
2702        StringBuilder sb = genSelect(getFolderListMap(), uiProjection, values);
2703        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.ID + "=?");
2704        return sb.toString();
2705    }
2706
2707    private static final Uri BASE_EXTERNAL_URI = Uri.parse("content://ui.email.android.com");
2708
2709    private static final Uri BASE_EXTERAL_URI2 = Uri.parse("content://ui.email2.android.com");
2710
2711    private static String getExternalUriString(String segment, String account) {
2712        return BASE_EXTERNAL_URI.buildUpon().appendPath(segment)
2713                .appendQueryParameter("account", account).build().toString();
2714    }
2715
2716    private static String getExternalUriStringEmail2(String segment, String account) {
2717        return BASE_EXTERAL_URI2.buildUpon().appendPath(segment)
2718                .appendQueryParameter("account", account).build().toString();
2719    }
2720
2721    private String getBits(int bitField) {
2722        StringBuilder sb = new StringBuilder(" ");
2723        for (int i = 0; i < 32; i++, bitField >>= 1) {
2724            if ((bitField & 1) != 0) {
2725                sb.append("" + i + " ");
2726            }
2727        }
2728        return sb.toString();
2729    }
2730
2731    private int getCapabilities(Context context, long accountId) {
2732        EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
2733                mServiceCallback, accountId);
2734        int capabilities = 0;
2735        Account acct = null;
2736        try {
2737            service.setTimeout(10);
2738            acct = Account.restoreAccountWithId(context, accountId);
2739            if (acct == null) {
2740                Log.d(TAG, "getCapabilities() for " + accountId + ": returning 0x0 (no account)");
2741                return 0;
2742            }
2743            capabilities = service.getCapabilities(acct);
2744            // STOPSHIP
2745            Log.d(TAG, "getCapabilities() for " + acct.mDisplayName + ": 0x" +
2746                    Integer.toHexString(capabilities) + getBits(capabilities));
2747       } catch (RemoteException e) {
2748            // Nothing to do
2749           Log.w(TAG, "getCapabilities() for " + acct.mDisplayName + ": RemoteException");
2750        }
2751        return capabilities;
2752    }
2753
2754    /**
2755     * Generate a "single account" SQLite query, given a projection from UnifiedEmail
2756     *
2757     * @param uiProjection as passed from UnifiedEmail
2758     * @return the SQLite query to be executed on the EmailProvider database
2759     */
2760    private String genQueryAccount(String[] uiProjection, String id) {
2761        final ContentValues values = new ContentValues();
2762        final long accountId = Long.parseLong(id);
2763        final Context context = getContext();
2764
2765        final Set<String> projectionColumns = ImmutableSet.copyOf(uiProjection);
2766
2767        if (projectionColumns.contains(UIProvider.AccountColumns.CAPABILITIES)) {
2768            // Get account capabilities from the service
2769            values.put(UIProvider.AccountColumns.CAPABILITIES, getCapabilities(context, accountId));
2770        }
2771        if (projectionColumns.contains(UIProvider.AccountColumns.SETTINGS_INTENT_URI)) {
2772            values.put(UIProvider.AccountColumns.SETTINGS_INTENT_URI,
2773                    getExternalUriString("settings", id));
2774        }
2775        if (projectionColumns.contains(UIProvider.AccountColumns.COMPOSE_URI)) {
2776            values.put(UIProvider.AccountColumns.COMPOSE_URI,
2777                    getExternalUriStringEmail2("compose", id));
2778        }
2779        if (projectionColumns.contains(UIProvider.AccountColumns.MIME_TYPE)) {
2780            values.put(UIProvider.AccountColumns.MIME_TYPE, EMAIL_APP_MIME_TYPE);
2781        }
2782        if (projectionColumns.contains(UIProvider.AccountColumns.COLOR)) {
2783            values.put(UIProvider.AccountColumns.COLOR, ACCOUNT_COLOR);
2784        }
2785
2786        final Preferences prefs = Preferences.getPreferences(getContext());
2787        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.CONFIRM_DELETE)) {
2788            values.put(UIProvider.AccountColumns.SettingsColumns.CONFIRM_DELETE,
2789                    prefs.getConfirmDelete() ? "1" : "0");
2790        }
2791        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.CONFIRM_SEND)) {
2792            values.put(UIProvider.AccountColumns.SettingsColumns.CONFIRM_SEND,
2793                    prefs.getConfirmSend() ? "1" : "0");
2794        }
2795        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.SWIPE)) {
2796            values.put(UIProvider.AccountColumns.SettingsColumns.SWIPE,
2797                    prefs.getSwipeDelete() ? SWIPE_DELETE : SWIPE_DISABLED);
2798        }
2799        if (projectionColumns.contains(
2800                UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)) {
2801            values.put(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES,
2802                    prefs.getHideCheckboxes() ? "1" : "0");
2803        }
2804        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.AUTO_ADVANCE)) {
2805            int autoAdvance = prefs.getAutoAdvanceDirection();
2806            values.put(UIProvider.AccountColumns.SettingsColumns.AUTO_ADVANCE,
2807                    autoAdvanceToUiValue(autoAdvance));
2808        }
2809        if (projectionColumns.contains(
2810                UIProvider.AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE)) {
2811            int textZoom = prefs.getTextZoom();
2812            values.put(UIProvider.AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE,
2813                    textZoomToUiValue(textZoom));
2814        }
2815       // Set default inbox, if we've got an inbox; otherwise, say initial sync needed
2816        long mailboxId = Mailbox.findMailboxOfType(context, accountId, Mailbox.TYPE_INBOX);
2817        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX) &&
2818                mailboxId != Mailbox.NO_MAILBOX) {
2819            values.put(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX,
2820                    uiUriString("uifolder", mailboxId));
2821        }
2822        if (projectionColumns.contains(
2823                UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX_NAME) &&
2824                mailboxId != Mailbox.NO_MAILBOX) {
2825            values.put(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX_NAME,
2826                    Mailbox.getDisplayName(context, mailboxId));
2827        }
2828        if (projectionColumns.contains(UIProvider.AccountColumns.SYNC_STATUS)) {
2829            if (mailboxId != Mailbox.NO_MAILBOX) {
2830                values.put(UIProvider.AccountColumns.SYNC_STATUS, UIProvider.SyncStatus.NO_SYNC);
2831            } else {
2832                values.put(UIProvider.AccountColumns.SYNC_STATUS,
2833                        UIProvider.SyncStatus.INITIAL_SYNC_NEEDED);
2834            }
2835        }
2836        if (projectionColumns.contains(
2837                UIProvider.AccountColumns.SettingsColumns.PRIORITY_ARROWS_ENABLED)) {
2838            // Email doesn't support priority inbox, so always state priority arrows disabled.
2839            values.put(UIProvider.AccountColumns.SettingsColumns.PRIORITY_ARROWS_ENABLED, "0");
2840        }
2841        if (projectionColumns.contains(
2842                UIProvider.AccountColumns.SettingsColumns.SETUP_INTENT_URI)) {
2843            // Set the setup intent if needed
2844            // TODO We should clarify/document the trash/setup relationship
2845            long trashId = Mailbox.findMailboxOfType(context, accountId, Mailbox.TYPE_TRASH);
2846            if (trashId == Mailbox.NO_MAILBOX) {
2847                EmailServiceInfo info = EmailServiceUtils.getServiceInfoForAccount(context,
2848                        accountId);
2849                if (info != null && info.requiresSetup) {
2850                    values.put(UIProvider.AccountColumns.SettingsColumns.SETUP_INTENT_URI,
2851                            getExternalUriString("setup", id));
2852                }
2853            }
2854        }
2855
2856        final StringBuilder sb = genSelect(getAccountListMap(), uiProjection, values);
2857        sb.append(" FROM " + Account.TABLE_NAME + " WHERE " + AccountColumns.ID + "=?");
2858        return sb.toString();
2859    }
2860
2861    private int autoAdvanceToUiValue(int autoAdvance) {
2862        switch(autoAdvance) {
2863            case Preferences.AUTO_ADVANCE_OLDER:
2864                return UIProvider.AutoAdvance.OLDER;
2865            case Preferences.AUTO_ADVANCE_NEWER:
2866                return UIProvider.AutoAdvance.NEWER;
2867            case Preferences.AUTO_ADVANCE_MESSAGE_LIST:
2868            default:
2869                return UIProvider.AutoAdvance.LIST;
2870        }
2871    }
2872
2873    private int textZoomToUiValue(int textZoom) {
2874        switch(textZoom) {
2875            case Preferences.TEXT_ZOOM_HUGE:
2876                return UIProvider.MessageTextSize.HUGE;
2877            case Preferences.TEXT_ZOOM_LARGE:
2878                return UIProvider.MessageTextSize.LARGE;
2879            case Preferences.TEXT_ZOOM_NORMAL:
2880                return UIProvider.MessageTextSize.NORMAL;
2881            case Preferences.TEXT_ZOOM_SMALL:
2882                return UIProvider.MessageTextSize.SMALL;
2883            case Preferences.TEXT_ZOOM_TINY:
2884                return UIProvider.MessageTextSize.TINY;
2885            default:
2886                return UIProvider.MessageTextSize.NORMAL;
2887        }
2888    }
2889
2890    /**
2891     * Generate a Uri string for a combined mailbox uri
2892     * @param type the uri command type (e.g. "uimessages")
2893     * @param id the id of the item (e.g. an account, mailbox, or message id)
2894     * @return a Uri string
2895     */
2896    private static String combinedUriString(String type, String id) {
2897        return "content://" + EmailContent.AUTHORITY + "/" + type + "/" + id;
2898    }
2899
2900    private static final long COMBINED_ACCOUNT_ID = 0x10000000;
2901
2902    /**
2903     * Generate an id for a combined mailbox of a given type
2904     * @param type the mailbox type for the combined mailbox
2905     * @return the id, as a String
2906     */
2907    private static String combinedMailboxId(int type) {
2908        return Long.toString(Account.ACCOUNT_ID_COMBINED_VIEW + type);
2909    }
2910
2911    private static String getVirtualMailboxIdString(long accountId, int type) {
2912        return Long.toString(getVirtualMailboxId(accountId, type));
2913    }
2914
2915    private static long getVirtualMailboxId(long accountId, int type) {
2916        return (accountId << 32) + type;
2917    }
2918
2919    private static boolean isVirtualMailbox(long mailboxId) {
2920        return mailboxId >= 0x100000000L;
2921    }
2922
2923    private static boolean isCombinedMailbox(long mailboxId) {
2924        return (mailboxId >> 32) == COMBINED_ACCOUNT_ID;
2925    }
2926
2927    private static long getVirtualMailboxAccountId(long mailboxId) {
2928        return mailboxId >> 32;
2929    }
2930
2931    private static String getVirtualMailboxAccountIdString(long mailboxId) {
2932        return Long.toString(mailboxId >> 32);
2933    }
2934
2935    private static int getVirtualMailboxType(long mailboxId) {
2936        return (int)(mailboxId & 0xF);
2937    }
2938
2939    private void addCombinedAccountRow(MatrixCursor mc) {
2940        final long id = Account.getDefaultAccountId(getContext());
2941        if (id == Account.NO_ACCOUNT) return;
2942        final String idString = Long.toString(id);
2943
2944        // Build a map of the requested columns to the appropriate positions
2945        final ImmutableMap.Builder<String, Integer> builder =
2946                new ImmutableMap.Builder<String, Integer>();
2947        final String[] columnNames = mc.getColumnNames();
2948        for (int i = 0; i < columnNames.length; i++) {
2949            builder.put(columnNames[i], i);
2950        }
2951        final Map<String, Integer> colPosMap = builder.build();
2952
2953        final Object[] values = new Object[columnNames.length];
2954        if (colPosMap.containsKey(BaseColumns._ID)) {
2955            values[colPosMap.get(BaseColumns._ID)] = 0;
2956        }
2957        if (colPosMap.containsKey(UIProvider.AccountColumns.CAPABILITIES)) {
2958            values[colPosMap.get(UIProvider.AccountColumns.CAPABILITIES)] =
2959                    AccountCapabilities.UNDO | AccountCapabilities.SENDING_UNAVAILABLE;
2960        }
2961        if (colPosMap.containsKey(UIProvider.AccountColumns.FOLDER_LIST_URI)) {
2962            values[colPosMap.get(UIProvider.AccountColumns.FOLDER_LIST_URI)] =
2963                    combinedUriString("uifolders", COMBINED_ACCOUNT_ID_STRING);
2964        }
2965        if (colPosMap.containsKey(UIProvider.AccountColumns.NAME)) {
2966            values[colPosMap.get(UIProvider.AccountColumns.NAME)] = getContext().getString(
2967                R.string.mailbox_list_account_selector_combined_view);
2968        }
2969        if (colPosMap.containsKey(UIProvider.AccountColumns.SAVE_DRAFT_URI)) {
2970            values[colPosMap.get(UIProvider.AccountColumns.SAVE_DRAFT_URI)] =
2971                    combinedUriString("uisavedraft", idString);
2972        }
2973        if (colPosMap.containsKey(UIProvider.AccountColumns.SEND_MAIL_URI)) {
2974            values[colPosMap.get(UIProvider.AccountColumns.SEND_MAIL_URI)] =
2975                    combinedUriString("uisendmail", idString);
2976        }
2977        if (colPosMap.containsKey(UIProvider.AccountColumns.UNDO_URI)) {
2978            values[colPosMap.get(UIProvider.AccountColumns.UNDO_URI)] =
2979                    "'content://" + UIProvider.AUTHORITY + "/uiundo'";
2980        }
2981        if (colPosMap.containsKey(UIProvider.AccountColumns.URI)) {
2982            values[colPosMap.get(UIProvider.AccountColumns.URI)] =
2983                    combinedUriString("uiaccount", COMBINED_ACCOUNT_ID_STRING);
2984        }
2985        if (colPosMap.containsKey(UIProvider.AccountColumns.MIME_TYPE)) {
2986            values[colPosMap.get(UIProvider.AccountColumns.MIME_TYPE)] =
2987                    EMAIL_APP_MIME_TYPE;
2988        }
2989        if (colPosMap.containsKey(UIProvider.AccountColumns.SETTINGS_INTENT_URI)) {
2990            values[colPosMap.get(UIProvider.AccountColumns.SETTINGS_INTENT_URI)] =
2991                    getExternalUriString("settings", COMBINED_ACCOUNT_ID_STRING);
2992        }
2993        if (colPosMap.containsKey(UIProvider.AccountColumns.COMPOSE_URI)) {
2994            values[colPosMap.get(UIProvider.AccountColumns.COMPOSE_URI)] =
2995                    getExternalUriStringEmail2("compose", Long.toString(id));
2996        }
2997
2998        // TODO: Get these from default account?
2999        Preferences prefs = Preferences.getPreferences(getContext());
3000        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.AUTO_ADVANCE)) {
3001            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.AUTO_ADVANCE)] =
3002                    Integer.toString(UIProvider.AutoAdvance.NEWER);
3003        }
3004        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE)) {
3005            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE)] =
3006                    Integer.toString(UIProvider.MessageTextSize.NORMAL);
3007        }
3008        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.SNAP_HEADERS)) {
3009            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.SNAP_HEADERS)] =
3010                    Integer.toString(UIProvider.SnapHeaderValue.ALWAYS);
3011        }
3012        //.add(UIProvider.SettingsColumns.SIGNATURE, AccountColumns.SIGNATURE)
3013        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.REPLY_BEHAVIOR)) {
3014            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.REPLY_BEHAVIOR)] =
3015                    Integer.toString(UIProvider.DefaultReplyBehavior.REPLY);
3016        }
3017        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)) {
3018            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)] = 0;
3019        }
3020        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.CONFIRM_DELETE)) {
3021            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.CONFIRM_DELETE)] =
3022                    prefs.getConfirmDelete() ? 1 : 0;
3023        }
3024        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.CONFIRM_ARCHIVE)) {
3025            values[colPosMap.get(
3026                    UIProvider.AccountColumns.SettingsColumns.CONFIRM_ARCHIVE)] = 0;
3027        }
3028        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.CONFIRM_SEND)) {
3029            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.CONFIRM_SEND)] =
3030                    prefs.getConfirmSend() ? 1 : 0;
3031        }
3032        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)) {
3033            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)] =
3034                    prefs.getHideCheckboxes() ? 1 : 0;
3035        }
3036        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX)) {
3037            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX)] =
3038                    combinedUriString("uifolder", combinedMailboxId(Mailbox.TYPE_INBOX));
3039        }
3040
3041        mc.addRow(values);
3042    }
3043
3044    private Cursor getVirtualMailboxCursor(long mailboxId) {
3045        MatrixCursor mc = new MatrixCursor(UIProvider.FOLDERS_PROJECTION, 1);
3046        mc.addRow(getVirtualMailboxRow(getVirtualMailboxAccountId(mailboxId),
3047                getVirtualMailboxType(mailboxId)));
3048        return mc;
3049    }
3050
3051    private Object[] getVirtualMailboxRow(long accountId, int mailboxType) {
3052        String idString = getVirtualMailboxIdString(accountId, mailboxType);
3053        Object[] values = new Object[UIProvider.FOLDERS_PROJECTION.length];
3054        values[UIProvider.FOLDER_ID_COLUMN] = 0;
3055        values[UIProvider.FOLDER_URI_COLUMN] = combinedUriString("uifolder", idString);
3056        values[UIProvider.FOLDER_NAME_COLUMN] = getMailboxNameForType(mailboxType);
3057        values[UIProvider.FOLDER_HAS_CHILDREN_COLUMN] = 0;
3058        values[UIProvider.FOLDER_CAPABILITIES_COLUMN] = UIProvider.FolderCapabilities.IS_VIRTUAL;
3059        values[UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN] = combinedUriString("uimessages",
3060                idString);
3061        values[UIProvider.FOLDER_ID_COLUMN] = 0;
3062        return values;
3063    }
3064
3065    private Cursor uiAccounts(String[] uiProjection) {
3066        Context context = getContext();
3067        SQLiteDatabase db = getDatabase(context);
3068        Cursor accountIdCursor =
3069                db.rawQuery("select _id from " + Account.TABLE_NAME, new String[0]);
3070        int numAccounts = accountIdCursor.getCount();
3071        boolean combinedAccount = false;
3072        if (numAccounts > 1) {
3073            combinedAccount = true;
3074            numAccounts++;
3075        }
3076        final Bundle extras = new Bundle();
3077        // Email always returns the accurate number of accounts
3078        extras.putInt(AccountCursorExtraKeys.ACCOUNTS_LOADED, 1);
3079        final MatrixCursor mc =
3080                new MatrixCursorWithExtra(uiProjection, accountIdCursor.getCount(), extras);
3081        Object[] values = new Object[uiProjection.length];
3082        try {
3083            if (combinedAccount) {
3084                addCombinedAccountRow(mc);
3085            }
3086            while (accountIdCursor.moveToNext()) {
3087                String id = accountIdCursor.getString(0);
3088                Cursor accountCursor =
3089                        db.rawQuery(genQueryAccount(uiProjection, id), new String[] {id});
3090                if (accountCursor.moveToNext()) {
3091                    for (int i = 0; i < uiProjection.length; i++) {
3092                        values[i] = accountCursor.getString(i);
3093                    }
3094                    mc.addRow(values);
3095                }
3096                accountCursor.close();
3097            }
3098            if (combinedAccount) {
3099                addCombinedAccountRow(mc);
3100            }
3101        } finally {
3102            accountIdCursor.close();
3103        }
3104        mc.setNotificationUri(context.getContentResolver(), UIPROVIDER_ALL_ACCOUNTS_NOTIFIER);
3105        return mc;
3106    }
3107
3108    /**
3109     * Generate the "attachment list" SQLite query, given a projection from UnifiedEmail
3110     *
3111     * @param uiProjection as passed from UnifiedEmail
3112     * @param contentTypeQueryParameters list of mimeTypes, used as a filter for the attachments
3113     * or null if there are no query parameters
3114     * @return the SQLite query to be executed on the EmailProvider database
3115     */
3116    private String genQueryAttachments(String[] uiProjection,
3117            List<String> contentTypeQueryParameters) {
3118        StringBuilder sb = genSelect(getAttachmentMap(), uiProjection);
3119        sb.append(" FROM " + Attachment.TABLE_NAME + " WHERE " + AttachmentColumns.MESSAGE_KEY +
3120                " =? ");
3121
3122        // Filter for certain content types.
3123        // The filter works by adding LIKE operators for each
3124        // content type you wish to request. Content types
3125        // are filtered by performing a case-insensitive "starts with"
3126        // filter. IE, "image/" would return "image/png" as well as "image/jpeg".
3127        if (contentTypeQueryParameters != null && !contentTypeQueryParameters.isEmpty()) {
3128            final int size = contentTypeQueryParameters.size();
3129            sb.append("AND (");
3130            for (int i = 0; i < size; i++) {
3131                final String contentType = contentTypeQueryParameters.get(i);
3132                sb.append(AttachmentColumns.MIME_TYPE + " LIKE '" + contentType + "%'");
3133
3134                if (i != size - 1) {
3135                    sb.append(" OR ");
3136                }
3137            }
3138            sb.append(")");
3139        }
3140        return sb.toString();
3141    }
3142
3143    /**
3144     * Generate the "single attachment" SQLite query, given a projection from UnifiedEmail
3145     *
3146     * @param uiProjection as passed from UnifiedEmail
3147     * @return the SQLite query to be executed on the EmailProvider database
3148     */
3149    private String genQueryAttachment(String[] uiProjection) {
3150        StringBuilder sb = genSelect(getAttachmentMap(), uiProjection);
3151        sb.append(" FROM " + Attachment.TABLE_NAME + " WHERE " + AttachmentColumns.ID + " =? ");
3152        return sb.toString();
3153    }
3154
3155    /**
3156     * Generate the "subfolder list" SQLite query, given a projection from UnifiedEmail
3157     *
3158     * @param uiProjection as passed from UnifiedEmail
3159     * @return the SQLite query to be executed on the EmailProvider database
3160     */
3161    private String genQuerySubfolders(String[] uiProjection) {
3162        StringBuilder sb = genSelect(getFolderListMap(), uiProjection);
3163        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.PARENT_KEY +
3164                " =? ORDER BY ");
3165        sb.append(MAILBOX_ORDER_BY);
3166        return sb.toString();
3167    }
3168
3169    private static final String COMBINED_ACCOUNT_ID_STRING = Long.toString(COMBINED_ACCOUNT_ID);
3170
3171    /**
3172     * Returns a cursor over all the folders for a specific URI which corresponds to a single
3173     * account.
3174     * @param uri
3175     * @param uiProjection
3176     * @return
3177     */
3178    private Cursor uiFolders(Uri uri, String[] uiProjection) {
3179        Context context = getContext();
3180        SQLiteDatabase db = getDatabase(context);
3181        String id = uri.getPathSegments().get(1);
3182        if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3183            MatrixCursor mc = new MatrixCursor(UIProvider.FOLDERS_PROJECTION, 2);
3184            Object[] row = getVirtualMailboxRow(COMBINED_ACCOUNT_ID, Mailbox.TYPE_INBOX);
3185            int numUnread = EmailContent.count(context, Message.CONTENT_URI,
3186                     MessageColumns.MAILBOX_KEY + " IN (SELECT " + MailboxColumns.ID +
3187                    " FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.TYPE +
3188                    "=" + Mailbox.TYPE_INBOX + ") AND " + MessageColumns.FLAG_READ + "=0", null);
3189            row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numUnread;
3190            mc.addRow(row);
3191            int numStarred = EmailContent.count(context, Message.CONTENT_URI,
3192                    MessageColumns.FLAG_FAVORITE + "=1", null);
3193            if (numStarred > 0) {
3194                row = getVirtualMailboxRow(COMBINED_ACCOUNT_ID, Mailbox.TYPE_STARRED);
3195                row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numStarred;
3196                mc.addRow(row);
3197            }
3198            return mc;
3199        } else {
3200            Cursor c = db.rawQuery(genQueryAccountMailboxes(uiProjection), new String[] {id});
3201            c = getFolderListCursor(db, c, uiProjection);
3202            int numStarred = EmailContent.count(context, Message.CONTENT_URI,
3203                    MessageColumns.ACCOUNT_KEY + "=? AND " + MessageColumns.FLAG_FAVORITE + "=1",
3204                    new String[] {id});
3205            if (numStarred == 0) {
3206                return c;
3207            } else {
3208                // Add starred virtual folder to the cursor
3209                // Show number of messages as unread count (for backward compatibility)
3210                MatrixCursor starCursor = new MatrixCursor(uiProjection, 1);
3211                Object[] row = getVirtualMailboxRow(Long.parseLong(id), Mailbox.TYPE_STARRED);
3212                row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numStarred;
3213                row[UIProvider.FOLDER_ICON_RES_ID_COLUMN] = R.drawable.ic_menu_star_holo_light;
3214                starCursor.addRow(row);
3215                Cursor[] cursors = new Cursor[] {starCursor, c};
3216                return new MergeCursor(cursors);
3217            }
3218        }
3219    }
3220
3221    /**
3222     * Returns an array of the default recent folders for a given URI which is unique for an
3223     * account. Some accounts might not have default recent folders, in which case an empty array
3224     * is returned.
3225     * @param id
3226     * @return
3227     */
3228    private Uri[] defaultRecentFolders(final String id) {
3229        final SQLiteDatabase db = getDatabase(getContext());
3230        if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3231            // We don't have default recents for the combined view.
3232            return new Uri[0];
3233        }
3234        // We search for the types we want, and find corresponding IDs.
3235        final String[] idAndType = { BaseColumns._ID, UIProvider.FolderColumns.TYPE };
3236
3237        // Sent, Drafts, and Starred are the default recents.
3238        final StringBuilder sb = genSelect(getFolderListMap(), idAndType);
3239        sb.append(" FROM " + Mailbox.TABLE_NAME
3240                + " WHERE " + MailboxColumns.ACCOUNT_KEY + " = " + id
3241                + " AND "
3242                + MailboxColumns.TYPE + " IN (" + Mailbox.TYPE_SENT +
3243                    ", " + Mailbox.TYPE_DRAFTS +
3244                    ", " + Mailbox.TYPE_STARRED
3245                + ")");
3246        LogUtils.d(TAG, "defaultRecentFolders: Query is %s", sb);
3247        final Cursor c = db.rawQuery(sb.toString(), null);
3248        if (c == null || c.getCount() <= 0 || !c.moveToFirst()) {
3249            return new Uri[0];
3250        }
3251        // Read all the IDs of the mailboxes, and turn them into URIs.
3252        final Uri[] recentFolders = new Uri[c.getCount()];
3253        int i = 0;
3254        do {
3255            final long folderId = c.getLong(0);
3256            recentFolders[i] = uiUri("uifolder", folderId);
3257            LogUtils.d(TAG, "Default recent folder: %d, with uri %s", folderId, recentFolders[i]);
3258            ++i;
3259        } while (c.moveToNext());
3260        return recentFolders;
3261    }
3262
3263    /**
3264     * Wrapper that handles the visibility feature (i.e. the conversation list is visible, so
3265     * any pending notifications for the corresponding mailbox should be canceled). We also handle
3266     * getExtras() to provide a snapshot of the mailbox's status
3267     */
3268    static class VisibilityCursor extends CursorWrapper {
3269        private final long mMailboxId;
3270        private final Context mContext;
3271        private final Bundle mExtras = new Bundle();
3272
3273        public VisibilityCursor(Context context, Cursor cursor, long mailboxId) {
3274            super(cursor);
3275            mMailboxId = mailboxId;
3276            mContext = context;
3277            Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
3278            if (mailbox != null) {
3279                mExtras.putInt(UIProvider.CursorExtraKeys.EXTRA_STATUS, mailbox.mUiSyncStatus);
3280                if (mailbox.mUiLastSyncResult != UIProvider.LastSyncResult.SUCCESS) {
3281                    mExtras.putInt(UIProvider.CursorExtraKeys.EXTRA_ERROR,
3282                            mailbox.mUiLastSyncResult);
3283                }
3284            }
3285        }
3286
3287        public Bundle getExtras() {
3288            return mExtras;
3289        }
3290
3291        @Override
3292        public Bundle respond(Bundle params) {
3293            final String setVisibilityKey =
3294                    UIProvider.ConversationCursorCommand.COMMAND_KEY_SET_VISIBILITY;
3295            if (params.containsKey(setVisibilityKey)) {
3296                final boolean visible = params.getBoolean(setVisibilityKey);
3297                if (visible) {
3298                    NotificationController.getInstance(mContext).cancelNewMessageNotification(
3299                            mMailboxId);
3300                    // Clear the visible limit of the mailbox (if any) on entry
3301                    if (params.containsKey(
3302                            UIProvider.ConversationCursorCommand.COMMAND_KEY_ENTERED_FOLDER)) {
3303                        Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, mMailboxId);
3304                        if (mailbox != null && mailbox.mVisibleLimit > 0) {
3305                            ContentValues values = new ContentValues();
3306                            values.put(MailboxColumns.VISIBLE_LIMIT, 0);
3307                            mContext.getContentResolver().update(
3308                                    ContentUris.withAppendedId(Mailbox.CONTENT_URI, mMailboxId),
3309                                    values, null, null);
3310                        }
3311                    }
3312                }
3313            }
3314            // Return success
3315            Bundle response = new Bundle();
3316            response.putString(setVisibilityKey,
3317                    UIProvider.ConversationCursorCommand.COMMAND_RESPONSE_OK);
3318            return response;
3319        }
3320    }
3321
3322    /**
3323     * For debugging purposes; shouldn't be used in production code
3324     */
3325    static class CloseDetectingCursor extends CursorWrapper {
3326
3327        public CloseDetectingCursor(Cursor cursor) {
3328            super(cursor);
3329        }
3330
3331        public void close() {
3332            super.close();
3333            Log.d(TAG, "Closing cursor", new Error());
3334        }
3335    }
3336
3337    /**
3338     * We need to do individual queries for the mailboxes in order to get correct
3339     * folder capabilities.
3340     */
3341    Cursor getFolderListCursor(SQLiteDatabase db, Cursor c, String[] uiProjection) {
3342        final MatrixCursor mc = new MatrixCursor(uiProjection);
3343        Object[] values = new Object[uiProjection.length];
3344        String[] args = new String[1];
3345        try {
3346            // Loop through mailboxes, building matrix cursor
3347            while (c.moveToNext()) {
3348                String id = c.getString(0);
3349                args[0] = id;
3350                Cursor mailboxCursor = db.rawQuery(genQueryMailbox(uiProjection, id), args);
3351                if (mailboxCursor.moveToNext()) {
3352                    for (int i = 0; i < uiProjection.length; i++) {
3353                        values[i] = mailboxCursor.getString(i);
3354                    }
3355                    mc.addRow(values);
3356                }
3357            }
3358        } finally {
3359            c.close();
3360        }
3361       return mc;
3362    }
3363
3364    /**
3365     * Handle UnifiedEmail queries here (dispatched from query())
3366     *
3367     * @param match the UriMatcher match for the original uri passed in from UnifiedEmail
3368     * @param uri the original uri passed in from UnifiedEmail
3369     * @param uiProjection the projection passed in from UnifiedEmail
3370     * @return the result Cursor
3371     */
3372    private Cursor uiQuery(int match, Uri uri, String[] uiProjection) {
3373        Context context = getContext();
3374        ContentResolver resolver = context.getContentResolver();
3375        SQLiteDatabase db = getDatabase(context);
3376        // Should we ever return null, or throw an exception??
3377        Cursor c = null;
3378        String id = uri.getPathSegments().get(1);
3379        Uri notifyUri = null;
3380        switch(match) {
3381            case UI_ALL_FOLDERS:
3382                c = db.rawQuery(genQueryAccountAllMailboxes(uiProjection), new String[] {id});
3383                c = getFolderListCursor(db, c, uiProjection);
3384                break;
3385            case UI_RECENT_FOLDERS:
3386                c = db.rawQuery(genQueryRecentMailboxes(uiProjection), new String[] {id});
3387                notifyUri = UIPROVIDER_RECENT_FOLDERS_NOTIFIER.buildUpon().appendPath(id).build();
3388                break;
3389            case UI_SUBFOLDERS:
3390                c = db.rawQuery(genQuerySubfolders(uiProjection), new String[] {id});
3391                c = getFolderListCursor(db, c, uiProjection);
3392                break;
3393            case UI_MESSAGES:
3394                long mailboxId = Long.parseLong(id);
3395                if (isVirtualMailbox(mailboxId)) {
3396                    c = getVirtualMailboxMessagesCursor(db, uiProjection, mailboxId);
3397                } else {
3398                    c = db.rawQuery(genQueryMailboxMessages(uiProjection), new String[] {id});
3399                }
3400                notifyUri = UIPROVIDER_CONVERSATION_NOTIFIER.buildUpon().appendPath(id).build();
3401                c = new VisibilityCursor(context, c, mailboxId);
3402                break;
3403            case UI_MESSAGE:
3404                MessageQuery qq = genQueryViewMessage(uiProjection, id);
3405                String sql = qq.query;
3406                String attJson = qq.attachmentJson;
3407                // With attachments, we have another argument to bind
3408                if (attJson != null) {
3409                    c = db.rawQuery(sql, new String[] {attJson, id});
3410                } else {
3411                    c = db.rawQuery(sql, new String[] {id});
3412                }
3413                break;
3414            case UI_ATTACHMENTS:
3415                final List<String> contentTypeQueryParameters =
3416                        uri.getQueryParameters(PhotoContract.ContentTypeParameters.CONTENT_TYPE);
3417                c = db.rawQuery(genQueryAttachments(uiProjection, contentTypeQueryParameters),
3418                        new String[] {id});
3419                notifyUri = UIPROVIDER_ATTACHMENTS_NOTIFIER.buildUpon().appendPath(id).build();
3420                break;
3421            case UI_ATTACHMENT:
3422                c = db.rawQuery(genQueryAttachment(uiProjection), new String[] {id});
3423                notifyUri = UIPROVIDER_ATTACHMENT_NOTIFIER.buildUpon().appendPath(id).build();
3424                break;
3425            case UI_FOLDER:
3426                mailboxId = Long.parseLong(id);
3427                if (isVirtualMailbox(mailboxId)) {
3428                    c = getVirtualMailboxCursor(mailboxId);
3429                } else {
3430                    c = db.rawQuery(genQueryMailbox(uiProjection, id), new String[] {id});
3431                    notifyUri = UIPROVIDER_FOLDER_NOTIFIER.buildUpon().appendPath(id).build();
3432                }
3433                break;
3434            case UI_ACCOUNT:
3435                if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3436                    MatrixCursor mc = new MatrixCursor(uiProjection, 1);
3437                    addCombinedAccountRow(mc);
3438                    c = mc;
3439                } else {
3440                    c = db.rawQuery(genQueryAccount(uiProjection, id), new String[] {id});
3441                }
3442                notifyUri = UIPROVIDER_ACCOUNT_NOTIFIER.buildUpon().appendPath(id).build();
3443                break;
3444            case UI_CONVERSATION:
3445                c = db.rawQuery(genQueryConversation(uiProjection), new String[] {id});
3446                break;
3447        }
3448        if (notifyUri != null) {
3449            c.setNotificationUri(resolver, notifyUri);
3450        }
3451        return c;
3452    }
3453
3454    /**
3455     * Convert a UIProvider attachment to an EmailProvider attachment (for sending); we only need
3456     * a few of the fields
3457     * @param uiAtt the UIProvider attachment to convert
3458     * @return the EmailProvider attachment
3459     */
3460    private Attachment convertUiAttachmentToAttachment(
3461            com.android.mail.providers.Attachment uiAtt) {
3462        Attachment att = new Attachment();
3463        att.mContentUri = uiAtt.contentUri.toString();
3464        att.mFileName = uiAtt.name;
3465        att.mMimeType = uiAtt.contentType;
3466        att.mSize = uiAtt.size;
3467        return att;
3468    }
3469
3470    private String getMailboxNameForType(int mailboxType) {
3471        Context context = getContext();
3472        int resId;
3473        switch (mailboxType) {
3474            case Mailbox.TYPE_INBOX:
3475                resId = R.string.mailbox_name_server_inbox;
3476                break;
3477            case Mailbox.TYPE_OUTBOX:
3478                resId = R.string.mailbox_name_server_outbox;
3479                break;
3480            case Mailbox.TYPE_DRAFTS:
3481                resId = R.string.mailbox_name_server_drafts;
3482                break;
3483            case Mailbox.TYPE_TRASH:
3484                resId = R.string.mailbox_name_server_trash;
3485                break;
3486            case Mailbox.TYPE_SENT:
3487                resId = R.string.mailbox_name_server_sent;
3488                break;
3489            case Mailbox.TYPE_JUNK:
3490                resId = R.string.mailbox_name_server_junk;
3491                break;
3492            case Mailbox.TYPE_STARRED:
3493                resId = R.string.widget_starred;
3494                break;
3495            default:
3496                throw new IllegalArgumentException("Illegal mailbox type");
3497        }
3498        return context.getString(resId);
3499    }
3500
3501    /**
3502     * Create a mailbox given the account and mailboxType.
3503     */
3504    private Mailbox createMailbox(long accountId, int mailboxType) {
3505        Context context = getContext();
3506        Mailbox box = Mailbox.newSystemMailbox(accountId, mailboxType,
3507                getMailboxNameForType(mailboxType));
3508        // Make sure drafts and save will show up in recents...
3509        // If these already exist (from old Email app), they will have touch times
3510        switch (mailboxType) {
3511            case Mailbox.TYPE_DRAFTS:
3512                box.mLastTouchedTime = Mailbox.DRAFTS_DEFAULT_TOUCH_TIME;
3513                break;
3514            case Mailbox.TYPE_SENT:
3515                box.mLastTouchedTime = Mailbox.SENT_DEFAULT_TOUCH_TIME;
3516                break;
3517        }
3518        box.save(context);
3519        return box;
3520    }
3521
3522    /**
3523     * Given an account name and a mailbox type, return that mailbox, creating it if necessary
3524     * @param accountName the account name to use
3525     * @param mailboxType the type of mailbox we're trying to find
3526     * @return the mailbox of the given type for the account in the uri, or null if not found
3527     */
3528    private Mailbox getMailboxByAccountIdAndType(String accountId, int mailboxType) {
3529        long id = Long.parseLong(accountId);
3530        Mailbox mailbox = Mailbox.restoreMailboxOfType(getContext(), id, mailboxType);
3531        if (mailbox == null) {
3532            mailbox = createMailbox(id, mailboxType);
3533        }
3534        return mailbox;
3535    }
3536
3537    private Message getMessageFromPathSegments(List<String> pathSegments) {
3538        Message msg = null;
3539        if (pathSegments.size() > 2) {
3540            msg = Message.restoreMessageWithId(getContext(), Long.parseLong(pathSegments.get(2)));
3541        }
3542        if (msg == null) {
3543            msg = new Message();
3544        }
3545        return msg;
3546    }
3547    /**
3548     * Given a mailbox and the content values for a message, create/save the message in the mailbox
3549     * @param mailbox the mailbox to use
3550     * @param values the content values that represent message fields
3551     * @return the uri of the newly created message
3552     */
3553    private Uri uiSaveMessage(Message msg, Mailbox mailbox, ContentValues values) {
3554        Context context = getContext();
3555        // Fill in the message
3556        Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey);
3557        if (account == null) return null;
3558        msg.mFrom = account.mEmailAddress;
3559        msg.mTimeStamp = System.currentTimeMillis();
3560        msg.mTo = values.getAsString(UIProvider.MessageColumns.TO);
3561        msg.mCc = values.getAsString(UIProvider.MessageColumns.CC);
3562        msg.mBcc = values.getAsString(UIProvider.MessageColumns.BCC);
3563        msg.mSubject = values.getAsString(UIProvider.MessageColumns.SUBJECT);
3564        msg.mText = values.getAsString(UIProvider.MessageColumns.BODY_TEXT);
3565        msg.mHtml = values.getAsString(UIProvider.MessageColumns.BODY_HTML);
3566        msg.mMailboxKey = mailbox.mId;
3567        msg.mAccountKey = mailbox.mAccountKey;
3568        msg.mDisplayName = msg.mTo;
3569        msg.mFlagLoaded = Message.FLAG_LOADED_COMPLETE;
3570        msg.mFlagRead = true;
3571        Integer quoteStartPos = values.getAsInteger(UIProvider.MessageColumns.QUOTE_START_POS);
3572        msg.mQuotedTextStartPos = quoteStartPos == null ? 0 : quoteStartPos;
3573        int flags = 0;
3574        int draftType = values.getAsInteger(UIProvider.MessageColumns.DRAFT_TYPE);
3575        switch(draftType) {
3576            case DraftType.FORWARD:
3577                flags |= Message.FLAG_TYPE_FORWARD;
3578                break;
3579            case DraftType.REPLY_ALL:
3580                flags |= Message.FLAG_TYPE_REPLY_ALL;
3581                // Fall through
3582            case DraftType.REPLY:
3583                flags |= Message.FLAG_TYPE_REPLY;
3584                break;
3585            case DraftType.COMPOSE:
3586                flags |= Message.FLAG_TYPE_ORIGINAL;
3587                break;
3588        }
3589        msg.mFlags = flags;
3590        int draftInfo = 0;
3591        if (values.containsKey(UIProvider.MessageColumns.QUOTE_START_POS)) {
3592            draftInfo = values.getAsInteger(UIProvider.MessageColumns.QUOTE_START_POS);
3593            if (values.getAsInteger(UIProvider.MessageColumns.APPEND_REF_MESSAGE_CONTENT) != 0) {
3594                draftInfo |= Message.DRAFT_INFO_APPEND_REF_MESSAGE;
3595            }
3596        }
3597        msg.mDraftInfo = draftInfo;
3598        String ref = values.getAsString(UIProvider.MessageColumns.REF_MESSAGE_ID);
3599        if (ref != null && msg.mQuotedTextStartPos >= 0) {
3600            String refId = Uri.parse(ref).getLastPathSegment();
3601            try {
3602                long sourceKey = Long.parseLong(refId);
3603                msg.mSourceKey = sourceKey;
3604            } catch (NumberFormatException e) {
3605                // This will be zero; the default
3606            }
3607        }
3608
3609        // Get attachments from the ContentValues
3610        List<com.android.mail.providers.Attachment> uiAtts =
3611                com.android.mail.providers.Attachment.fromJSONArray(
3612                        values.getAsString(UIProvider.MessageColumns.JOINED_ATTACHMENT_INFOS));
3613        ArrayList<Attachment> atts = new ArrayList<Attachment>();
3614        boolean hasUnloadedAttachments = false;
3615        for (com.android.mail.providers.Attachment uiAtt: uiAtts) {
3616            Uri attUri = uiAtt.uri;
3617            if (attUri != null && attUri.getAuthority().equals(EmailContent.AUTHORITY)) {
3618                // If it's one of ours, retrieve the attachment and add it to the list
3619                long attId = Long.parseLong(attUri.getLastPathSegment());
3620                Attachment att = Attachment.restoreAttachmentWithId(context, attId);
3621                if (att != null) {
3622                    // We must clone the attachment into a new one for this message; easiest to
3623                    // use a parcel here
3624                    Parcel p = Parcel.obtain();
3625                    att.writeToParcel(p, 0);
3626                    p.setDataPosition(0);
3627                    Attachment attClone = new Attachment(p);
3628                    p.recycle();
3629                    // Clear the messageKey (this is going to be a new attachment)
3630                    attClone.mMessageKey = 0;
3631                    // If we're sending this, it's not loaded, and we're not smart forwarding
3632                    // add the download flag, so that ADS will start up
3633                    if (mailbox.mType == Mailbox.TYPE_OUTBOX && att.mContentUri == null &&
3634                            ((account.mFlags & Account.FLAGS_SUPPORTS_SMART_FORWARD) == 0)) {
3635                        attClone.mFlags |= Attachment.FLAG_DOWNLOAD_FORWARD;
3636                        hasUnloadedAttachments = true;
3637                    }
3638                    atts.add(attClone);
3639                }
3640            } else {
3641                // Convert external attachment to one of ours and add to the list
3642                atts.add(convertUiAttachmentToAttachment(uiAtt));
3643            }
3644        }
3645        if (!atts.isEmpty()) {
3646            msg.mAttachments = atts;
3647            msg.mFlagAttachment = true;
3648            if (hasUnloadedAttachments) {
3649                Utility.showToast(context, R.string.message_view_attachment_background_load);
3650            }
3651        }
3652        // Save it or update it...
3653        if (!msg.isSaved()) {
3654            msg.save(context);
3655        } else {
3656            // This is tricky due to how messages/attachments are saved; rather than putz with
3657            // what's changed, we'll delete/re-add them
3658            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
3659            // Delete all existing attachments
3660            ops.add(ContentProviderOperation.newDelete(
3661                    ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, msg.mId))
3662                    .build());
3663            // Delete the body
3664            ops.add(ContentProviderOperation.newDelete(Body.CONTENT_URI)
3665                    .withSelection(Body.MESSAGE_KEY + "=?", new String[] {Long.toString(msg.mId)})
3666                    .build());
3667            // Add the ops for the message, atts, and body
3668            msg.addSaveOps(ops);
3669            // Do it!
3670            try {
3671                applyBatch(ops);
3672            } catch (OperationApplicationException e) {
3673            }
3674        }
3675        if (mailbox.mType == Mailbox.TYPE_OUTBOX) {
3676            EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
3677                    mServiceCallback, mailbox.mAccountKey);
3678            try {
3679                service.startSync(mailbox.mId, true);
3680            } catch (RemoteException e) {
3681            }
3682            long originalMsgId = msg.mSourceKey;
3683            if (originalMsgId != 0) {
3684                Message originalMsg = Message.restoreMessageWithId(context, originalMsgId);
3685                // If the original message exists, set its forwarded/replied to flags
3686                if (originalMsg != null) {
3687                    ContentValues cv = new ContentValues();
3688                    flags = originalMsg.mFlags;
3689                    switch(draftType) {
3690                        case DraftType.FORWARD:
3691                            flags |= Message.FLAG_FORWARDED;
3692                            break;
3693                        case DraftType.REPLY_ALL:
3694                        case DraftType.REPLY:
3695                            flags |= Message.FLAG_REPLIED_TO;
3696                            break;
3697                    }
3698                    cv.put(Message.FLAGS, flags);
3699                    context.getContentResolver().update(ContentUris.withAppendedId(
3700                            Message.CONTENT_URI, originalMsgId), cv, null, null);
3701                }
3702            }
3703        }
3704        return uiUri("uimessage", msg.mId);
3705    }
3706
3707    /**
3708     * Create and send the message via the account indicated in the uri
3709     * @param uri the incoming uri
3710     * @param values the content values that represent message fields
3711     * @return the uri of the created message
3712     */
3713    private Uri uiSendMail(Uri uri, ContentValues values) {
3714        List<String> pathSegments = uri.getPathSegments();
3715        Mailbox mailbox = getMailboxByAccountIdAndType(pathSegments.get(1), Mailbox.TYPE_OUTBOX);
3716        if (mailbox == null) return null;
3717        Message msg = getMessageFromPathSegments(pathSegments);
3718        try {
3719            return uiSaveMessage(msg, mailbox, values);
3720        } finally {
3721            // Kick observers
3722            getContext().getContentResolver().notifyChange(Mailbox.CONTENT_URI, null);
3723        }
3724    }
3725
3726    /**
3727     * Create a message and save it to the drafts folder of the account indicated in the uri
3728     * @param uri the incoming uri
3729     * @param values the content values that represent message fields
3730     * @return the uri of the created message
3731     */
3732    private Uri uiSaveDraft(Uri uri, ContentValues values) {
3733        List<String> pathSegments = uri.getPathSegments();
3734        Mailbox mailbox = getMailboxByAccountIdAndType(pathSegments.get(1), Mailbox.TYPE_DRAFTS);
3735        if (mailbox == null) return null;
3736        Message msg = getMessageFromPathSegments(pathSegments);
3737        return uiSaveMessage(msg, mailbox, values);
3738    }
3739
3740    private int uiUpdateDraft(Uri uri, ContentValues values) {
3741        Context context = getContext();
3742        Message msg = Message.restoreMessageWithId(context,
3743                Long.parseLong(uri.getPathSegments().get(1)));
3744        if (msg == null) return 0;
3745        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
3746        if (mailbox == null) return 0;
3747        uiSaveMessage(msg, mailbox, values);
3748        return 1;
3749    }
3750
3751    private int uiSendDraft(Uri uri, ContentValues values) {
3752        Context context = getContext();
3753        Message msg = Message.restoreMessageWithId(context,
3754                Long.parseLong(uri.getPathSegments().get(1)));
3755        if (msg == null) return 0;
3756        long mailboxId = Mailbox.findMailboxOfType(context, msg.mAccountKey, Mailbox.TYPE_OUTBOX);
3757        if (mailboxId == Mailbox.NO_MAILBOX) return 0;
3758        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
3759        if (mailbox == null) return 0;
3760        uiSaveMessage(msg, mailbox, values);
3761        // Kick observers
3762        context.getContentResolver().notifyChange(Mailbox.CONTENT_URI, null);
3763        return 1;
3764    }
3765
3766    private void putIntegerLongOrBoolean(ContentValues values, String columnName, Object value) {
3767        if (value instanceof Integer) {
3768            Integer intValue = (Integer)value;
3769            values.put(columnName, intValue);
3770        } else if (value instanceof Boolean) {
3771            Boolean boolValue = (Boolean)value;
3772            values.put(columnName, boolValue ? 1 : 0);
3773        } else if (value instanceof Long) {
3774            Long longValue = (Long)value;
3775            values.put(columnName, longValue);
3776        }
3777    }
3778
3779    /**
3780     * Update the timestamps for the folders specified and notifies on the recent folder URI.
3781     * @param folders
3782     * @return number of folders updated
3783     */
3784    private int updateTimestamp(final Context context, String id, Uri[] folders){
3785        int updated = 0;
3786        final long now = System.currentTimeMillis();
3787        final ContentResolver resolver = context.getContentResolver();
3788        final ContentValues touchValues = new ContentValues();
3789        for (int i=0, size=folders.length; i < size; ++i) {
3790            touchValues.put(MailboxColumns.LAST_TOUCHED_TIME, now);
3791            LogUtils.d(TAG, "updateStamp: %s updated", folders[i]);
3792            updated += resolver.update(folders[i], touchValues, null, null);
3793        }
3794        final Uri toNotify =
3795                UIPROVIDER_RECENT_FOLDERS_NOTIFIER.buildUpon().appendPath(id).build();
3796        LogUtils.d(TAG, "updateTimestamp: Notifying on %s", toNotify);
3797        resolver.notifyChange(toNotify, null);
3798        return updated;
3799    }
3800
3801    /**
3802     * Updates the recent folders. The values to be updated are specified as ContentValues pairs
3803     * of (Folder URI, access timestamp). Returns nonzero if successful, always.
3804     * @param uri
3805     * @param values
3806     * @return nonzero value always.
3807     */
3808    private int uiUpdateRecentFolders(Uri uri, ContentValues values) {
3809        final int numFolders = values.size();
3810        final String id = uri.getPathSegments().get(1);
3811        final Uri[] folders = new Uri[numFolders];
3812        final Context context = getContext();
3813        final NotificationController controller = NotificationController.getInstance(context);
3814        int i = 0;
3815        for (final String uriString: values.keySet()) {
3816            folders[i] = Uri.parse(uriString);
3817            try {
3818                final String mailboxIdString = folders[i].getLastPathSegment();
3819                final long mailboxId = Long.parseLong(mailboxIdString);
3820                controller.cancelNewMessageNotification(mailboxId);
3821            } catch (NumberFormatException e) {
3822                // Keep on going...
3823            }
3824        }
3825        return updateTimestamp(context, id, folders);
3826    }
3827
3828    /**
3829     * Populates the recent folders according to the design.
3830     * @param uri
3831     * @return the number of recent folders were populated.
3832     */
3833    private int uiPopulateRecentFolders(Uri uri) {
3834        final Context context = getContext();
3835        final String id = uri.getLastPathSegment();
3836        final Uri[] recentFolders = defaultRecentFolders(id);
3837        final int numFolders = recentFolders.length;
3838        if (numFolders <= 0) {
3839            return 0;
3840        }
3841        final int rowsUpdated = updateTimestamp(context, id, recentFolders);
3842        LogUtils.d(TAG, "uiPopulateRecentFolders: %d folders changed", rowsUpdated);
3843        return rowsUpdated;
3844    }
3845
3846    private int uiUpdateAttachment(Uri uri, ContentValues uiValues) {
3847        Integer stateValue = uiValues.getAsInteger(UIProvider.AttachmentColumns.STATE);
3848        if (stateValue != null) {
3849            // This is a command from UIProvider
3850            long attachmentId = Long.parseLong(uri.getLastPathSegment());
3851            Context context = getContext();
3852            Attachment attachment =
3853                    Attachment.restoreAttachmentWithId(context, attachmentId);
3854            if (attachment == null) {
3855                // Went away; ah, well...
3856                return 0;
3857            }
3858            ContentValues values = new ContentValues();
3859            switch (stateValue.intValue()) {
3860                case UIProvider.AttachmentState.NOT_SAVED:
3861                    // Set state, try to cancel request
3862                    values.put(AttachmentColumns.UI_STATE, stateValue);
3863                    values.put(AttachmentColumns.FLAGS,
3864                            attachment.mFlags &= ~Attachment.FLAG_DOWNLOAD_USER_REQUEST);
3865                    attachment.update(context, values);
3866                    return 1;
3867                case UIProvider.AttachmentState.DOWNLOADING:
3868                    // Set state and destination; request download
3869                    values.put(AttachmentColumns.UI_STATE, stateValue);
3870                    Integer destinationValue =
3871                        uiValues.getAsInteger(UIProvider.AttachmentColumns.DESTINATION);
3872                    values.put(AttachmentColumns.UI_DESTINATION,
3873                            destinationValue == null ? 0 : destinationValue);
3874                    values.put(AttachmentColumns.FLAGS,
3875                            attachment.mFlags | Attachment.FLAG_DOWNLOAD_USER_REQUEST);
3876                    attachment.update(context, values);
3877                    return 1;
3878                case UIProvider.AttachmentState.SAVED:
3879                    // If this is an inline attachment, notify message has changed
3880                    if (!TextUtils.isEmpty(attachment.mContentId)) {
3881                        notifyUI(UIPROVIDER_MESSAGE_NOTIFIER, attachment.mMessageKey);
3882                    }
3883                    return 1;
3884            }
3885        }
3886        return 0;
3887    }
3888
3889    private int uiUpdateFolder(Uri uri, ContentValues uiValues) {
3890        Uri ourUri = convertToEmailProviderUri(uri, Mailbox.CONTENT_URI, true);
3891        if (ourUri == null) return 0;
3892        ContentValues ourValues = new ContentValues();
3893        // This should only be called via update to "recent folders"
3894        for (String columnName: uiValues.keySet()) {
3895            if (columnName.equals(MailboxColumns.LAST_TOUCHED_TIME)) {
3896                ourValues.put(MailboxColumns.LAST_TOUCHED_TIME, uiValues.getAsLong(columnName));
3897            }
3898        }
3899        return update(ourUri, ourValues, null, null);
3900    }
3901
3902    private ContentValues convertUiMessageValues(Message message, ContentValues values) {
3903        ContentValues ourValues = new ContentValues();
3904        for (String columnName : values.keySet()) {
3905            Object val = values.get(columnName);
3906            if (columnName.equals(UIProvider.ConversationColumns.STARRED)) {
3907                putIntegerLongOrBoolean(ourValues, MessageColumns.FLAG_FAVORITE, val);
3908            } else if (columnName.equals(UIProvider.ConversationColumns.READ)) {
3909                putIntegerLongOrBoolean(ourValues, MessageColumns.FLAG_READ, val);
3910            } else if (columnName.equals(MessageColumns.MAILBOX_KEY)) {
3911                putIntegerLongOrBoolean(ourValues, MessageColumns.MAILBOX_KEY, val);
3912            } else if (columnName.equals(UIProvider.ConversationColumns.RAW_FOLDERS)) {
3913                // Convert from folder list uri to mailbox key
3914                ArrayList<Folder> folders = Folder.getFoldersArray((String) val);
3915                if (folders == null || folders.size() == 0 || folders.size() > 1) {
3916                    LogUtils.d(TAG,
3917                            "Incorrect number of folders for this message: Message is %s",
3918                            message.mId);
3919                } else {
3920                    Folder f = folders.get(0);
3921                    Uri uri = f.uri;
3922                    Long mailboxId = Long.parseLong(uri.getLastPathSegment());
3923                    putIntegerLongOrBoolean(ourValues, MessageColumns.MAILBOX_KEY, mailboxId);
3924                }
3925            } else if (columnName.equals(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES)) {
3926                Address[] fromList = Address.unpack(message.mFrom);
3927                Preferences prefs = Preferences.getPreferences(getContext());
3928                for (Address sender : fromList) {
3929                    String email = sender.getAddress();
3930                    prefs.setSenderAsTrusted(email);
3931                }
3932            } else if (columnName.equals(UIProvider.ConversationColumns.VIEWED)) {
3933                // Ignore for now
3934            } else {
3935                throw new IllegalArgumentException("Can't update " + columnName + " in message");
3936            }
3937        }
3938        return ourValues;
3939    }
3940
3941    private Uri convertToEmailProviderUri(Uri uri, Uri newBaseUri, boolean asProvider) {
3942        String idString = uri.getLastPathSegment();
3943        try {
3944            long id = Long.parseLong(idString);
3945            Uri ourUri = ContentUris.withAppendedId(newBaseUri, id);
3946            if (asProvider) {
3947                ourUri = ourUri.buildUpon().appendQueryParameter(IS_UIPROVIDER, "true").build();
3948            }
3949            return ourUri;
3950        } catch (NumberFormatException e) {
3951            return null;
3952        }
3953    }
3954
3955    private Message getMessageFromLastSegment(Uri uri) {
3956        long messageId = Long.parseLong(uri.getLastPathSegment());
3957        return Message.restoreMessageWithId(getContext(), messageId);
3958    }
3959
3960    /**
3961     * Add an undo operation for the current sequence; if the sequence is newer than what we've had,
3962     * clear out the undo list and start over
3963     * @param uri the uri we're working on
3964     * @param op the ContentProviderOperation to perform upon undo
3965     */
3966    private void addToSequence(Uri uri, ContentProviderOperation op) {
3967        String sequenceString = uri.getQueryParameter(UIProvider.SEQUENCE_QUERY_PARAMETER);
3968        if (sequenceString != null) {
3969            int sequence = Integer.parseInt(sequenceString);
3970            if (sequence > mLastSequence) {
3971                // Reset sequence
3972                mLastSequenceOps.clear();
3973                mLastSequence = sequence;
3974            }
3975            // TODO: Need something to indicate a change isn't ready (undoable)
3976            mLastSequenceOps.add(op);
3977        }
3978    }
3979
3980    // TODO: This should depend on flags on the mailbox...
3981    private boolean uploadsToServer(Context context, Mailbox m) {
3982        if (m.mType == Mailbox.TYPE_DRAFTS || m.mType == Mailbox.TYPE_OUTBOX ||
3983                m.mType == Mailbox.TYPE_SEARCH) {
3984            return false;
3985        }
3986        String protocol = Account.getProtocol(context, m.mAccountKey);
3987        EmailServiceInfo info = EmailServiceUtils.getServiceInfo(context, protocol);
3988        return (info != null && info.syncChanges);
3989    }
3990
3991    private int uiUpdateMessage(Uri uri, ContentValues values) {
3992        return uiUpdateMessage(uri, values, false);
3993    }
3994
3995    private int uiUpdateMessage(Uri uri, ContentValues values, boolean forceSync) {
3996        Context context = getContext();
3997        Message msg = getMessageFromLastSegment(uri);
3998        if (msg == null) return 0;
3999        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
4000        if (mailbox == null) return 0;
4001        Uri ourBaseUri =
4002                (forceSync || uploadsToServer(context, mailbox)) ? Message.SYNCED_CONTENT_URI :
4003                    Message.CONTENT_URI;
4004        Uri ourUri = convertToEmailProviderUri(uri, ourBaseUri, true);
4005        if (ourUri == null) return 0;
4006
4007        // Special case - meeting response
4008        if (values.containsKey(UIProvider.MessageOperations.RESPOND_COLUMN)) {
4009            EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
4010                    mServiceCallback, mailbox.mAccountKey);
4011            try {
4012                service.sendMeetingResponse(msg.mId,
4013                        values.getAsInteger(UIProvider.MessageOperations.RESPOND_COLUMN));
4014                // Delete the message immediately
4015                uiDeleteMessage(uri);
4016                Utility.showToast(context, R.string.confirm_response);
4017                // Notify box has changed so the deletion is reflected in the UI
4018                notifyUIConversationMailbox(mailbox.mId);
4019            } catch (RemoteException e) {
4020            }
4021            return 1;
4022        }
4023
4024        ContentValues undoValues = new ContentValues();
4025        ContentValues ourValues = convertUiMessageValues(msg, values);
4026        for (String columnName: ourValues.keySet()) {
4027            if (columnName.equals(MessageColumns.MAILBOX_KEY)) {
4028                undoValues.put(MessageColumns.MAILBOX_KEY, msg.mMailboxKey);
4029            } else if (columnName.equals(MessageColumns.FLAG_READ)) {
4030                undoValues.put(MessageColumns.FLAG_READ, msg.mFlagRead);
4031            } else if (columnName.equals(MessageColumns.FLAG_FAVORITE)) {
4032                undoValues.put(MessageColumns.FLAG_FAVORITE, msg.mFlagFavorite);
4033            }
4034        }
4035        if (undoValues == null || undoValues.size() == 0) {
4036            return -1;
4037        }
4038        ContentProviderOperation op =
4039                ContentProviderOperation.newUpdate(convertToEmailProviderUri(
4040                        uri, ourBaseUri, false))
4041                        .withValues(undoValues)
4042                        .build();
4043        addToSequence(uri, op);
4044        return update(ourUri, ourValues, null, null);
4045    }
4046
4047    public static final String PICKER_UI_ACCOUNT = "picker_ui_account";
4048    public static final String PICKER_MAILBOX_TYPE = "picker_mailbox_type";
4049    public static final String PICKER_MESSAGE_ID = "picker_message_id";
4050    public static final String PICKER_HEADER_ID = "picker_header_id";
4051
4052    private int uiDeleteMessage(Uri uri) {
4053        final Context context = getContext();
4054        Message msg = getMessageFromLastSegment(uri);
4055        if (msg == null) return 0;
4056        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
4057        if (mailbox == null) return 0;
4058        if (mailbox.mType == Mailbox.TYPE_TRASH || mailbox.mType == Mailbox.TYPE_DRAFTS) {
4059            // We actually delete these, including attachments
4060            AttachmentUtilities.deleteAllAttachmentFiles(context, msg.mAccountKey, msg.mId);
4061            notifyUI(UIPROVIDER_FOLDER_NOTIFIER, mailbox.mId);
4062            return context.getContentResolver().delete(
4063                    ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg.mId), null, null);
4064        }
4065        Mailbox trashMailbox =
4066                Mailbox.restoreMailboxOfType(context, msg.mAccountKey, Mailbox.TYPE_TRASH);
4067        if (trashMailbox == null) {
4068            return 0;
4069        }
4070        ContentValues values = new ContentValues();
4071        values.put(MessageColumns.MAILBOX_KEY, trashMailbox.mId);
4072        notifyUI(UIPROVIDER_FOLDER_NOTIFIER, mailbox.mId);
4073        return uiUpdateMessage(uri, values, true);
4074    }
4075
4076    private int pickFolder(Uri uri, int type, int headerId) {
4077        Context context = getContext();
4078        Long acctId = Long.parseLong(uri.getLastPathSegment());
4079        // For push imap, for example, we want the user to select the trash mailbox
4080        Cursor ac = query(uiUri("uiaccount", acctId), UIProvider.ACCOUNTS_PROJECTION,
4081                null, null, null);
4082        try {
4083            if (ac.moveToFirst()) {
4084                final com.android.mail.providers.Account uiAccount =
4085                        new com.android.mail.providers.Account(ac);
4086                Intent intent = new Intent(context, FolderPickerActivity.class);
4087                intent.putExtra(PICKER_UI_ACCOUNT, uiAccount);
4088                intent.putExtra(PICKER_MAILBOX_TYPE, type);
4089                intent.putExtra(PICKER_HEADER_ID, headerId);
4090                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4091                context.startActivity(intent);
4092                return 1;
4093            }
4094            return 0;
4095        } finally {
4096            ac.close();
4097        }
4098    }
4099
4100    private int pickTrashFolder(Uri uri) {
4101        return pickFolder(uri, Mailbox.TYPE_TRASH, R.string.trash_folder_selection_title);
4102    }
4103
4104    private int pickSentFolder(Uri uri) {
4105        return pickFolder(uri, Mailbox.TYPE_SENT, R.string.sent_folder_selection_title);
4106    }
4107
4108    private Cursor uiUndo(String[] projection) {
4109        // First see if we have any operations saved
4110        // TODO: Make sure seq matches
4111        if (!mLastSequenceOps.isEmpty()) {
4112            try {
4113                // TODO Always use this projection?  Or what's passed in?
4114                // Not sure if UI wants it, but I'm making a cursor of convo uri's
4115                MatrixCursor c = new MatrixCursor(
4116                        new String[] {UIProvider.ConversationColumns.URI},
4117                        mLastSequenceOps.size());
4118                for (ContentProviderOperation op: mLastSequenceOps) {
4119                    c.addRow(new String[] {op.getUri().toString()});
4120                }
4121                // Just apply the batch and we're done!
4122                applyBatch(mLastSequenceOps);
4123                // But clear the operations
4124                mLastSequenceOps.clear();
4125                // Tell the UI there are changes
4126                ContentResolver resolver = getContext().getContentResolver();
4127                resolver.notifyChange(UIPROVIDER_CONVERSATION_NOTIFIER, null);
4128                resolver.notifyChange(UIPROVIDER_FOLDER_NOTIFIER, null);
4129                return c;
4130            } catch (OperationApplicationException e) {
4131            }
4132        }
4133        return new MatrixCursor(projection, 0);
4134    }
4135
4136    private void notifyUIConversation(Uri uri) {
4137        String id = uri.getLastPathSegment();
4138        Message msg = Message.restoreMessageWithId(getContext(), Long.parseLong(id));
4139        if (msg != null) {
4140            notifyUIConversationMailbox(msg.mMailboxKey);
4141        }
4142    }
4143
4144    /**
4145     * Notify about the Mailbox id passed in
4146     * @param id the Mailbox id to be notified
4147     */
4148    private void notifyUIConversationMailbox(long id) {
4149        notifyUI(UIPROVIDER_CONVERSATION_NOTIFIER, Long.toString(id));
4150        Mailbox mailbox = Mailbox.restoreMailboxWithId(getContext(), id);
4151        if (mailbox == null) {
4152            Log.w(TAG, "No mailbox for notification: " + id);
4153            return;
4154        }
4155        // Notify combined inbox...
4156        if (mailbox.mType == Mailbox.TYPE_INBOX) {
4157            notifyUI(UIPROVIDER_CONVERSATION_NOTIFIER,
4158                    EmailProvider.combinedMailboxId(Mailbox.TYPE_INBOX));
4159        }
4160        notifyWidgets(id);
4161    }
4162
4163    private void notifyUI(Uri uri, String id) {
4164        Uri notifyUri = uri.buildUpon().appendPath(id).build();
4165        getContext().getContentResolver().notifyChange(notifyUri, null);
4166    }
4167
4168    private void notifyUI(Uri uri, long id) {
4169        notifyUI(uri, Long.toString(id));
4170    }
4171
4172    /**
4173     * Support for services and service notifications
4174     */
4175
4176    private final IEmailServiceCallback.Stub mServiceCallback =
4177            new IEmailServiceCallback.Stub() {
4178
4179        @Override
4180        public void syncMailboxListStatus(long accountId, int statusCode, int progress)
4181                throws RemoteException {
4182        }
4183
4184        @Override
4185        public void syncMailboxStatus(long mailboxId, int statusCode, int progress)
4186                throws RemoteException {
4187            // We'll get callbacks here from the services, which we'll pass back to the UI
4188            Uri uri = ContentUris.withAppendedId(FOLDER_STATUS_URI, mailboxId);
4189            EmailProvider.this.getContext().getContentResolver().notifyChange(uri, null);
4190        }
4191
4192        @Override
4193        public void loadAttachmentStatus(long messageId, long attachmentId, int statusCode,
4194                int progress) throws RemoteException {
4195        }
4196
4197        @Override
4198        public void sendMessageStatus(long messageId, String subject, int statusCode, int progress)
4199                throws RemoteException {
4200        }
4201
4202        @Override
4203        public void loadMessageStatus(long messageId, int statusCode, int progress)
4204                throws RemoteException {
4205        }
4206    };
4207
4208    private Cursor uiFolderRefresh(Uri uri) {
4209        Context context = getContext();
4210        String idString = uri.getLastPathSegment();
4211        long id = Long.parseLong(idString);
4212        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
4213        if (mailbox == null) return null;
4214        EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
4215                mServiceCallback, mailbox.mAccountKey);
4216        try {
4217            service.startSync(id, true);
4218        } catch (RemoteException e) {
4219        }
4220        return null;
4221    }
4222
4223    //Number of additional messages to load when a user selects "Load more..." in POP/IMAP boxes
4224    public static final int VISIBLE_LIMIT_INCREMENT = 10;
4225    //Number of additional messages to load when a user selects "Load more..." in a search
4226    public static final int SEARCH_MORE_INCREMENT = 10;
4227
4228    private Cursor uiFolderLoadMore(Uri uri) {
4229        Context context = getContext();
4230        String idString = uri.getLastPathSegment();
4231        long id = Long.parseLong(idString);
4232        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
4233        if (mailbox == null) return null;
4234        if (mailbox.mType == Mailbox.TYPE_SEARCH) {
4235            // Ask for 10 more messages
4236            mSearchParams.mOffset += SEARCH_MORE_INCREMENT;
4237            runSearchQuery(context, mailbox.mAccountKey, id);
4238        } else {
4239            ContentValues values = new ContentValues();
4240            values.put(EmailContent.FIELD_COLUMN_NAME, MailboxColumns.VISIBLE_LIMIT);
4241            values.put(EmailContent.ADD_COLUMN_NAME, VISIBLE_LIMIT_INCREMENT);
4242            Uri mailboxUri = ContentUris.withAppendedId(Mailbox.ADD_TO_FIELD_URI, id);
4243            // Increase the limit
4244            context.getContentResolver().update(mailboxUri, values, null, null);
4245            // And order a refresh
4246            uiFolderRefresh(uri);
4247        }
4248        return null;
4249    }
4250
4251    private static final String SEARCH_MAILBOX_SERVER_ID = "__search_mailbox__";
4252    private SearchParams mSearchParams;
4253
4254    /**
4255     * Returns the search mailbox for the specified account, creating one if necessary
4256     * @return the search mailbox for the passed in account
4257     */
4258    private Mailbox getSearchMailbox(long accountId) {
4259        Context context = getContext();
4260        Mailbox m = Mailbox.restoreMailboxOfType(context, accountId, Mailbox.TYPE_SEARCH);
4261        if (m == null) {
4262            m = new Mailbox();
4263            m.mAccountKey = accountId;
4264            m.mServerId = SEARCH_MAILBOX_SERVER_ID;
4265            m.mFlagVisible = false;
4266            m.mDisplayName = SEARCH_MAILBOX_SERVER_ID;
4267            m.mSyncInterval = Mailbox.CHECK_INTERVAL_NEVER;
4268            m.mType = Mailbox.TYPE_SEARCH;
4269            m.mFlags = Mailbox.FLAG_HOLDS_MAIL;
4270            m.mParentKey = Mailbox.NO_MAILBOX;
4271            m.save(context);
4272        }
4273        return m;
4274    }
4275
4276    private void runSearchQuery(final Context context, final long accountId,
4277            final long searchMailboxId) {
4278        // Start the search running in the background
4279        new Thread(new Runnable() {
4280            @Override
4281            public void run() {
4282                 try {
4283                    EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
4284                            mServiceCallback, accountId);
4285                    if (service != null) {
4286                        try {
4287                            // Save away the total count
4288                            mSearchParams.mTotalCount = service.searchMessages(accountId,
4289                                    mSearchParams, searchMailboxId);
4290                            //Log.d(TAG, "TotalCount to UI: " + mSearchParams.mTotalCount);
4291                            notifyUI(UIPROVIDER_FOLDER_NOTIFIER, searchMailboxId);
4292                        } catch (RemoteException e) {
4293                            Log.e("searchMessages", "RemoteException", e);
4294                        }
4295                    }
4296                } finally {
4297                }
4298            }}).start();
4299
4300    }
4301
4302    // TODO: Handle searching for more...
4303    private Cursor uiSearch(Uri uri, String[] projection) {
4304        final long accountId = Long.parseLong(uri.getLastPathSegment());
4305
4306        // TODO: Check the actual mailbox
4307        Mailbox inbox = Mailbox.restoreMailboxOfType(getContext(), accountId, Mailbox.TYPE_INBOX);
4308        if (inbox == null) {
4309            Log.w(Logging.LOG_TAG, "In uiSearch, inbox doesn't exist for account " + accountId);
4310            return null;
4311        }
4312
4313        String filter = uri.getQueryParameter(UIProvider.SearchQueryParameters.QUERY);
4314        if (filter == null) {
4315            throw new IllegalArgumentException("No query parameter in search query");
4316        }
4317
4318        // Find/create our search mailbox
4319        Mailbox searchMailbox = getSearchMailbox(accountId);
4320        final long searchMailboxId = searchMailbox.mId;
4321
4322        mSearchParams = new SearchParams(inbox.mId, filter, searchMailboxId);
4323
4324        final Context context = getContext();
4325        if (mSearchParams.mOffset == 0) {
4326            // Delete existing contents of search mailbox
4327            ContentResolver resolver = context.getContentResolver();
4328            resolver.delete(Message.CONTENT_URI, Message.MAILBOX_KEY + "=" + searchMailboxId,
4329                    null);
4330            ContentValues cv = new ContentValues();
4331            // For now, use the actual query as the name of the mailbox
4332            cv.put(Mailbox.DISPLAY_NAME, mSearchParams.mFilter);
4333            resolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI, searchMailboxId),
4334                    cv, null, null);
4335        }
4336
4337        // Start the search running in the background
4338        runSearchQuery(context, accountId, searchMailboxId);
4339
4340        // This will look just like a "normal" folder
4341        return uiQuery(UI_FOLDER, ContentUris.withAppendedId(Mailbox.CONTENT_URI,
4342                searchMailbox.mId), projection);
4343    }
4344
4345    private static final String MAILBOXES_FOR_ACCOUNT_SELECTION = MailboxColumns.ACCOUNT_KEY + "=?";
4346    private static final String MAILBOXES_FOR_ACCOUNT_EXCEPT_ACCOUNT_MAILBOX_SELECTION =
4347        MAILBOXES_FOR_ACCOUNT_SELECTION + " AND " + MailboxColumns.TYPE + "!=" +
4348        Mailbox.TYPE_EAS_ACCOUNT_MAILBOX;
4349    private static final String MESSAGES_FOR_ACCOUNT_SELECTION = MessageColumns.ACCOUNT_KEY + "=?";
4350
4351    /**
4352     * Delete an account and clean it up
4353     */
4354    private int uiDeleteAccount(Uri uri) {
4355        Context context = getContext();
4356        long accountId = Long.parseLong(uri.getLastPathSegment());
4357        try {
4358            // Get the account URI.
4359            final Account account = Account.restoreAccountWithId(context, accountId);
4360            if (account == null) {
4361                return 0; // Already deleted?
4362            }
4363
4364            deleteAccountData(context, accountId);
4365
4366            // Now delete the account itself
4367            uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId);
4368            context.getContentResolver().delete(uri, null, null);
4369
4370            // Clean up
4371            AccountBackupRestore.backup(context);
4372            SecurityPolicy.getInstance(context).reducePolicies();
4373            MailActivityEmail.setServicesEnabledSync(context);
4374            return 1;
4375        } catch (Exception e) {
4376            Log.w(Logging.LOG_TAG, "Exception while deleting account", e);
4377        }
4378        return 0;
4379    }
4380
4381    private int uiDeleteAccountData(Uri uri) {
4382        Context context = getContext();
4383        long accountId = Long.parseLong(uri.getLastPathSegment());
4384        // Get the account URI.
4385        final Account account = Account.restoreAccountWithId(context, accountId);
4386        if (account == null) {
4387            return 0; // Already deleted?
4388        }
4389        deleteAccountData(context, accountId);
4390        return 1;
4391    }
4392
4393    private void deleteAccountData(Context context, long accountId) {
4394        // Delete synced attachments
4395        AttachmentUtilities.deleteAllAccountAttachmentFiles(context, accountId);
4396
4397        // Delete synced email, leaving only an empty inbox.  We do this in two phases:
4398        // 1. Delete all non-inbox mailboxes (which will delete all of their messages)
4399        // 2. Delete all remaining messages (which will be the inbox messages)
4400        ContentResolver resolver = context.getContentResolver();
4401        String[] accountIdArgs = new String[] { Long.toString(accountId) };
4402        resolver.delete(Mailbox.CONTENT_URI,
4403                MAILBOXES_FOR_ACCOUNT_EXCEPT_ACCOUNT_MAILBOX_SELECTION,
4404                accountIdArgs);
4405        resolver.delete(Message.CONTENT_URI, MESSAGES_FOR_ACCOUNT_SELECTION, accountIdArgs);
4406
4407        // Delete sync keys on remaining items
4408        ContentValues cv = new ContentValues();
4409        cv.putNull(Account.SYNC_KEY);
4410        resolver.update(Account.CONTENT_URI, cv, Account.ID_SELECTION, accountIdArgs);
4411        cv.clear();
4412        cv.putNull(Mailbox.SYNC_KEY);
4413        resolver.update(Mailbox.CONTENT_URI, cv,
4414                MAILBOXES_FOR_ACCOUNT_SELECTION, accountIdArgs);
4415
4416        // Delete PIM data (contacts, calendar), stop syncs, etc. if applicable
4417        IEmailService service = EmailServiceUtils.getServiceForAccount(context, null, accountId);
4418        if (service != null) {
4419            try {
4420                service.deleteAccountPIMData(accountId);
4421            } catch (RemoteException e) {
4422                // Can't do anything about this
4423            }
4424        }
4425    }
4426
4427    private int[] mSavedWidgetIds = new int[0];
4428    private ArrayList<Long> mWidgetNotifyMailboxes = new ArrayList<Long>();
4429    private AppWidgetManager mAppWidgetManager;
4430    private ComponentName mEmailComponent;
4431
4432    private void notifyWidgets(long mailboxId) {
4433        Context context = getContext();
4434        // Lazily initialize these
4435        if (mAppWidgetManager == null) {
4436            mAppWidgetManager = AppWidgetManager.getInstance(context);
4437            mEmailComponent = new ComponentName(context, WidgetProvider.PROVIDER_NAME);
4438        }
4439
4440        // See if we have to populate our array of mailboxes used in widgets
4441        int[] widgetIds = mAppWidgetManager.getAppWidgetIds(mEmailComponent);
4442        if (!Arrays.equals(widgetIds, mSavedWidgetIds)) {
4443            mSavedWidgetIds = widgetIds;
4444            String[][] widgetInfos = BaseWidgetProvider.getWidgetInfo(context, widgetIds);
4445            // widgetInfo now has pairs of account uri/folder uri
4446            mWidgetNotifyMailboxes.clear();
4447            for (String[] widgetInfo: widgetInfos) {
4448                try {
4449                    if (widgetInfo == null) continue;
4450                    long id = Long.parseLong(Uri.parse(widgetInfo[1]).getLastPathSegment());
4451                    if (!isCombinedMailbox(id)) {
4452                        // For a regular mailbox, just add it to the list
4453                        if (!mWidgetNotifyMailboxes.contains(id)) {
4454                            mWidgetNotifyMailboxes.add(id);
4455                        }
4456                    } else {
4457                        switch (getVirtualMailboxType(id)) {
4458                            // We only handle the combined inbox in widgets
4459                            case Mailbox.TYPE_INBOX:
4460                                Cursor c = query(Mailbox.CONTENT_URI, Mailbox.ID_PROJECTION,
4461                                        MailboxColumns.TYPE + "=?",
4462                                        new String[] {Integer.toString(Mailbox.TYPE_INBOX)}, null);
4463                                try {
4464                                    while (c.moveToNext()) {
4465                                        mWidgetNotifyMailboxes.add(
4466                                                c.getLong(Mailbox.ID_PROJECTION_COLUMN));
4467                                    }
4468                                } finally {
4469                                    c.close();
4470                                }
4471                                break;
4472                        }
4473                    }
4474                } catch (NumberFormatException e) {
4475                    // Move along
4476                }
4477            }
4478        }
4479
4480        // If our mailbox needs to be notified, do so...
4481        if (mWidgetNotifyMailboxes.contains(mailboxId)) {
4482            Intent intent = new Intent(Utils.ACTION_NOTIFY_DATASET_CHANGED);
4483            intent.putExtra(Utils.EXTRA_FOLDER_URI, uiUri("uifolder", mailboxId));
4484            intent.setType(EMAIL_APP_MIME_TYPE);
4485            context.sendBroadcast(intent);
4486         }
4487    }
4488}
4489