EmailProvider.java revision 26164054710375519ba7468987971a7a3340ba7e
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 int getCapabilities(Context context, long accountId) {
2722        EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
2723                mServiceCallback, accountId);
2724        int capabilities = 0;
2725        try {
2726            service.setTimeout(10);
2727            Account acct = Account.restoreAccountWithId(context, accountId);
2728            if (acct == null) return 0;
2729            capabilities = service.getCapabilities(acct);
2730        } catch (RemoteException e) {
2731            // Nothing to do
2732        }
2733        return capabilities;
2734    }
2735
2736    /**
2737     * Generate a "single account" SQLite query, given a projection from UnifiedEmail
2738     *
2739     * @param uiProjection as passed from UnifiedEmail
2740     * @return the SQLite query to be executed on the EmailProvider database
2741     */
2742    private String genQueryAccount(String[] uiProjection, String id) {
2743        final ContentValues values = new ContentValues();
2744        final long accountId = Long.parseLong(id);
2745        final Context context = getContext();
2746
2747        final Set<String> projectionColumns = ImmutableSet.copyOf(uiProjection);
2748
2749        if (projectionColumns.contains(UIProvider.AccountColumns.CAPABILITIES)) {
2750            // Get account capabilities from the service
2751            values.put(UIProvider.AccountColumns.CAPABILITIES, getCapabilities(context, accountId));
2752        }
2753        if (projectionColumns.contains(UIProvider.AccountColumns.SETTINGS_INTENT_URI)) {
2754            values.put(UIProvider.AccountColumns.SETTINGS_INTENT_URI,
2755                    getExternalUriString("settings", id));
2756        }
2757        if (projectionColumns.contains(UIProvider.AccountColumns.COMPOSE_URI)) {
2758            values.put(UIProvider.AccountColumns.COMPOSE_URI,
2759                    getExternalUriStringEmail2("compose", id));
2760        }
2761        if (projectionColumns.contains(UIProvider.AccountColumns.MIME_TYPE)) {
2762            values.put(UIProvider.AccountColumns.MIME_TYPE, EMAIL_APP_MIME_TYPE);
2763        }
2764        if (projectionColumns.contains(UIProvider.AccountColumns.COLOR)) {
2765            values.put(UIProvider.AccountColumns.COLOR, ACCOUNT_COLOR);
2766        }
2767
2768        final Preferences prefs = Preferences.getPreferences(getContext());
2769        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.CONFIRM_DELETE)) {
2770            values.put(UIProvider.AccountColumns.SettingsColumns.CONFIRM_DELETE,
2771                    prefs.getConfirmDelete() ? "1" : "0");
2772        }
2773        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.CONFIRM_SEND)) {
2774            values.put(UIProvider.AccountColumns.SettingsColumns.CONFIRM_SEND,
2775                    prefs.getConfirmSend() ? "1" : "0");
2776        }
2777        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.SWIPE)) {
2778            values.put(UIProvider.AccountColumns.SettingsColumns.SWIPE,
2779                    prefs.getSwipeDelete() ? SWIPE_DELETE : SWIPE_DISABLED);
2780        }
2781        if (projectionColumns.contains(
2782                UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)) {
2783            values.put(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES,
2784                    prefs.getHideCheckboxes() ? "1" : "0");
2785        }
2786        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.AUTO_ADVANCE)) {
2787            int autoAdvance = prefs.getAutoAdvanceDirection();
2788            values.put(UIProvider.AccountColumns.SettingsColumns.AUTO_ADVANCE,
2789                    autoAdvanceToUiValue(autoAdvance));
2790        }
2791        if (projectionColumns.contains(
2792                UIProvider.AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE)) {
2793            int textZoom = prefs.getTextZoom();
2794            values.put(UIProvider.AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE,
2795                    textZoomToUiValue(textZoom));
2796        }
2797       // Set default inbox, if we've got an inbox; otherwise, say initial sync needed
2798        long mailboxId = Mailbox.findMailboxOfType(context, accountId, Mailbox.TYPE_INBOX);
2799        if (projectionColumns.contains(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX) &&
2800                mailboxId != Mailbox.NO_MAILBOX) {
2801            values.put(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX,
2802                    uiUriString("uifolder", mailboxId));
2803        }
2804        if (projectionColumns.contains(
2805                UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX_NAME) &&
2806                mailboxId != Mailbox.NO_MAILBOX) {
2807            values.put(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX_NAME,
2808                    Mailbox.getDisplayName(context, mailboxId));
2809        }
2810        if (projectionColumns.contains(UIProvider.AccountColumns.SYNC_STATUS)) {
2811            if (mailboxId != Mailbox.NO_MAILBOX) {
2812                values.put(UIProvider.AccountColumns.SYNC_STATUS, UIProvider.SyncStatus.NO_SYNC);
2813            } else {
2814                values.put(UIProvider.AccountColumns.SYNC_STATUS,
2815                        UIProvider.SyncStatus.INITIAL_SYNC_NEEDED);
2816            }
2817        }
2818        if (projectionColumns.contains(
2819                UIProvider.AccountColumns.SettingsColumns.PRIORITY_ARROWS_ENABLED)) {
2820            // Email doesn't support priority inbox, so always state priority arrows disabled.
2821            values.put(UIProvider.AccountColumns.SettingsColumns.PRIORITY_ARROWS_ENABLED, "0");
2822        }
2823        if (projectionColumns.contains(
2824                UIProvider.AccountColumns.SettingsColumns.SETUP_INTENT_URI)) {
2825            // Use this if needed
2826            long trashId = Mailbox.findMailboxOfType(context, accountId, Mailbox.TYPE_TRASH);
2827            if (trashId == Mailbox.NO_MAILBOX) {
2828                values.put(UIProvider.AccountColumns.SettingsColumns.SETUP_INTENT_URI,
2829                        getExternalUriString("setup", id));
2830            }
2831        }
2832
2833        final StringBuilder sb = genSelect(getAccountListMap(), uiProjection, values);
2834        sb.append(" FROM " + Account.TABLE_NAME + " WHERE " + AccountColumns.ID + "=?");
2835        return sb.toString();
2836    }
2837
2838    private int autoAdvanceToUiValue(int autoAdvance) {
2839        switch(autoAdvance) {
2840            case Preferences.AUTO_ADVANCE_OLDER:
2841                return UIProvider.AutoAdvance.OLDER;
2842            case Preferences.AUTO_ADVANCE_NEWER:
2843                return UIProvider.AutoAdvance.NEWER;
2844            case Preferences.AUTO_ADVANCE_MESSAGE_LIST:
2845            default:
2846                return UIProvider.AutoAdvance.LIST;
2847        }
2848    }
2849
2850    private int textZoomToUiValue(int textZoom) {
2851        switch(textZoom) {
2852            case Preferences.TEXT_ZOOM_HUGE:
2853                return UIProvider.MessageTextSize.HUGE;
2854            case Preferences.TEXT_ZOOM_LARGE:
2855                return UIProvider.MessageTextSize.LARGE;
2856            case Preferences.TEXT_ZOOM_NORMAL:
2857                return UIProvider.MessageTextSize.NORMAL;
2858            case Preferences.TEXT_ZOOM_SMALL:
2859                return UIProvider.MessageTextSize.SMALL;
2860            case Preferences.TEXT_ZOOM_TINY:
2861                return UIProvider.MessageTextSize.TINY;
2862            default:
2863                return UIProvider.MessageTextSize.NORMAL;
2864        }
2865    }
2866
2867    /**
2868     * Generate a Uri string for a combined mailbox uri
2869     * @param type the uri command type (e.g. "uimessages")
2870     * @param id the id of the item (e.g. an account, mailbox, or message id)
2871     * @return a Uri string
2872     */
2873    private static String combinedUriString(String type, String id) {
2874        return "content://" + EmailContent.AUTHORITY + "/" + type + "/" + id;
2875    }
2876
2877    private static final long COMBINED_ACCOUNT_ID = 0x10000000;
2878
2879    /**
2880     * Generate an id for a combined mailbox of a given type
2881     * @param type the mailbox type for the combined mailbox
2882     * @return the id, as a String
2883     */
2884    private static String combinedMailboxId(int type) {
2885        return Long.toString(Account.ACCOUNT_ID_COMBINED_VIEW + type);
2886    }
2887
2888    private static String getVirtualMailboxIdString(long accountId, int type) {
2889        return Long.toString(getVirtualMailboxId(accountId, type));
2890    }
2891
2892    private static long getVirtualMailboxId(long accountId, int type) {
2893        return (accountId << 32) + type;
2894    }
2895
2896    private static boolean isVirtualMailbox(long mailboxId) {
2897        return mailboxId >= 0x100000000L;
2898    }
2899
2900    private static boolean isCombinedMailbox(long mailboxId) {
2901        return (mailboxId >> 32) == COMBINED_ACCOUNT_ID;
2902    }
2903
2904    private static long getVirtualMailboxAccountId(long mailboxId) {
2905        return mailboxId >> 32;
2906    }
2907
2908    private static String getVirtualMailboxAccountIdString(long mailboxId) {
2909        return Long.toString(mailboxId >> 32);
2910    }
2911
2912    private static int getVirtualMailboxType(long mailboxId) {
2913        return (int)(mailboxId & 0xF);
2914    }
2915
2916    private void addCombinedAccountRow(MatrixCursor mc) {
2917        final long id = Account.getDefaultAccountId(getContext());
2918        if (id == Account.NO_ACCOUNT) return;
2919        final String idString = Long.toString(id);
2920
2921        // Build a map of the requested columns to the appropriate positions
2922        final ImmutableMap.Builder<String, Integer> builder =
2923                new ImmutableMap.Builder<String, Integer>();
2924        final String[] columnNames = mc.getColumnNames();
2925        for (int i = 0; i < columnNames.length; i++) {
2926            builder.put(columnNames[i], i);
2927        }
2928        final Map<String, Integer> colPosMap = builder.build();
2929
2930        final Object[] values = new Object[columnNames.length];
2931        if (colPosMap.containsKey(BaseColumns._ID)) {
2932            values[colPosMap.get(BaseColumns._ID)] = 0;
2933        }
2934        if (colPosMap.containsKey(UIProvider.AccountColumns.CAPABILITIES)) {
2935            values[colPosMap.get(UIProvider.AccountColumns.CAPABILITIES)] =
2936                    AccountCapabilities.UNDO | AccountCapabilities.SENDING_UNAVAILABLE;
2937        }
2938        if (colPosMap.containsKey(UIProvider.AccountColumns.FOLDER_LIST_URI)) {
2939            values[colPosMap.get(UIProvider.AccountColumns.FOLDER_LIST_URI)] =
2940                    combinedUriString("uifolders", COMBINED_ACCOUNT_ID_STRING);
2941        }
2942        if (colPosMap.containsKey(UIProvider.AccountColumns.NAME)) {
2943            values[colPosMap.get(UIProvider.AccountColumns.NAME)] = getContext().getString(
2944                R.string.mailbox_list_account_selector_combined_view);
2945        }
2946        if (colPosMap.containsKey(UIProvider.AccountColumns.SAVE_DRAFT_URI)) {
2947            values[colPosMap.get(UIProvider.AccountColumns.SAVE_DRAFT_URI)] =
2948                    combinedUriString("uisavedraft", idString);
2949        }
2950        if (colPosMap.containsKey(UIProvider.AccountColumns.SEND_MAIL_URI)) {
2951            values[colPosMap.get(UIProvider.AccountColumns.SEND_MAIL_URI)] =
2952                    combinedUriString("uisendmail", idString);
2953        }
2954        if (colPosMap.containsKey(UIProvider.AccountColumns.UNDO_URI)) {
2955            values[colPosMap.get(UIProvider.AccountColumns.UNDO_URI)] =
2956                    "'content://" + UIProvider.AUTHORITY + "/uiundo'";
2957        }
2958        if (colPosMap.containsKey(UIProvider.AccountColumns.URI)) {
2959            values[colPosMap.get(UIProvider.AccountColumns.URI)] =
2960                    combinedUriString("uiaccount", COMBINED_ACCOUNT_ID_STRING);
2961        }
2962        if (colPosMap.containsKey(UIProvider.AccountColumns.MIME_TYPE)) {
2963            values[colPosMap.get(UIProvider.AccountColumns.MIME_TYPE)] =
2964                    EMAIL_APP_MIME_TYPE;
2965        }
2966        if (colPosMap.containsKey(UIProvider.AccountColumns.SETTINGS_INTENT_URI)) {
2967            values[colPosMap.get(UIProvider.AccountColumns.SETTINGS_INTENT_URI)] =
2968                    getExternalUriString("settings", COMBINED_ACCOUNT_ID_STRING);
2969        }
2970        if (colPosMap.containsKey(UIProvider.AccountColumns.COMPOSE_URI)) {
2971            values[colPosMap.get(UIProvider.AccountColumns.COMPOSE_URI)] =
2972                    getExternalUriStringEmail2("compose", Long.toString(id));
2973        }
2974
2975        // TODO: Get these from default account?
2976        Preferences prefs = Preferences.getPreferences(getContext());
2977        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.AUTO_ADVANCE)) {
2978            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.AUTO_ADVANCE)] =
2979                    Integer.toString(UIProvider.AutoAdvance.NEWER);
2980        }
2981        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE)) {
2982            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.MESSAGE_TEXT_SIZE)] =
2983                    Integer.toString(UIProvider.MessageTextSize.NORMAL);
2984        }
2985        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.SNAP_HEADERS)) {
2986            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.SNAP_HEADERS)] =
2987                    Integer.toString(UIProvider.SnapHeaderValue.ALWAYS);
2988        }
2989        //.add(UIProvider.SettingsColumns.SIGNATURE, AccountColumns.SIGNATURE)
2990        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.REPLY_BEHAVIOR)) {
2991            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.REPLY_BEHAVIOR)] =
2992                    Integer.toString(UIProvider.DefaultReplyBehavior.REPLY);
2993        }
2994        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)) {
2995            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)] = 0;
2996        }
2997        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.CONFIRM_DELETE)) {
2998            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.CONFIRM_DELETE)] =
2999                    prefs.getConfirmDelete() ? 1 : 0;
3000        }
3001        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.CONFIRM_ARCHIVE)) {
3002            values[colPosMap.get(
3003                    UIProvider.AccountColumns.SettingsColumns.CONFIRM_ARCHIVE)] = 0;
3004        }
3005        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.CONFIRM_SEND)) {
3006            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.CONFIRM_SEND)] =
3007                    prefs.getConfirmSend() ? 1 : 0;
3008        }
3009        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)) {
3010            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.HIDE_CHECKBOXES)] =
3011                    prefs.getHideCheckboxes() ? 1 : 0;
3012        }
3013        if (colPosMap.containsKey(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX)) {
3014            values[colPosMap.get(UIProvider.AccountColumns.SettingsColumns.DEFAULT_INBOX)] =
3015                    combinedUriString("uifolder", combinedMailboxId(Mailbox.TYPE_INBOX));
3016        }
3017
3018        mc.addRow(values);
3019    }
3020
3021    private Cursor getVirtualMailboxCursor(long mailboxId) {
3022        MatrixCursor mc = new MatrixCursor(UIProvider.FOLDERS_PROJECTION, 1);
3023        mc.addRow(getVirtualMailboxRow(getVirtualMailboxAccountId(mailboxId),
3024                getVirtualMailboxType(mailboxId)));
3025        return mc;
3026    }
3027
3028    private Object[] getVirtualMailboxRow(long accountId, int mailboxType) {
3029        String idString = getVirtualMailboxIdString(accountId, mailboxType);
3030        Object[] values = new Object[UIProvider.FOLDERS_PROJECTION.length];
3031        values[UIProvider.FOLDER_ID_COLUMN] = 0;
3032        values[UIProvider.FOLDER_URI_COLUMN] = combinedUriString("uifolder", idString);
3033        values[UIProvider.FOLDER_NAME_COLUMN] = getMailboxNameForType(mailboxType);
3034        values[UIProvider.FOLDER_HAS_CHILDREN_COLUMN] = 0;
3035        values[UIProvider.FOLDER_CAPABILITIES_COLUMN] = UIProvider.FolderCapabilities.IS_VIRTUAL;
3036        values[UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN] = combinedUriString("uimessages",
3037                idString);
3038        values[UIProvider.FOLDER_ID_COLUMN] = 0;
3039        return values;
3040    }
3041
3042    private Cursor uiAccounts(String[] uiProjection) {
3043        Context context = getContext();
3044        SQLiteDatabase db = getDatabase(context);
3045        Cursor accountIdCursor =
3046                db.rawQuery("select _id from " + Account.TABLE_NAME, new String[0]);
3047        int numAccounts = accountIdCursor.getCount();
3048        boolean combinedAccount = false;
3049        if (numAccounts > 1) {
3050            combinedAccount = true;
3051            numAccounts++;
3052        }
3053        final Bundle extras = new Bundle();
3054        // Email always returns the accurate number of accounts
3055        extras.putInt(AccountCursorExtraKeys.ACCOUNTS_LOADED, 1);
3056        final MatrixCursor mc =
3057                new MatrixCursorWithExtra(uiProjection, accountIdCursor.getCount(), extras);
3058        Object[] values = new Object[uiProjection.length];
3059        try {
3060            if (combinedAccount) {
3061                addCombinedAccountRow(mc);
3062            }
3063            while (accountIdCursor.moveToNext()) {
3064                String id = accountIdCursor.getString(0);
3065                Cursor accountCursor =
3066                        db.rawQuery(genQueryAccount(uiProjection, id), new String[] {id});
3067                if (accountCursor.moveToNext()) {
3068                    for (int i = 0; i < uiProjection.length; i++) {
3069                        values[i] = accountCursor.getString(i);
3070                    }
3071                    mc.addRow(values);
3072                }
3073                accountCursor.close();
3074            }
3075            if (combinedAccount) {
3076                addCombinedAccountRow(mc);
3077            }
3078        } finally {
3079            accountIdCursor.close();
3080        }
3081        mc.setNotificationUri(context.getContentResolver(), UIPROVIDER_ALL_ACCOUNTS_NOTIFIER);
3082        return mc;
3083    }
3084
3085    /**
3086     * Generate the "attachment list" SQLite query, given a projection from UnifiedEmail
3087     *
3088     * @param uiProjection as passed from UnifiedEmail
3089     * @param contentTypeQueryParameters list of mimeTypes, used as a filter for the attachments
3090     * or null if there are no query parameters
3091     * @return the SQLite query to be executed on the EmailProvider database
3092     */
3093    private String genQueryAttachments(String[] uiProjection,
3094            List<String> contentTypeQueryParameters) {
3095        StringBuilder sb = genSelect(getAttachmentMap(), uiProjection);
3096        sb.append(" FROM " + Attachment.TABLE_NAME + " WHERE " + AttachmentColumns.MESSAGE_KEY +
3097                " =? ");
3098
3099        // Filter for certain content types.
3100        // The filter works by adding LIKE operators for each
3101        // content type you wish to request. Content types
3102        // are filtered by performing a case-insensitive "starts with"
3103        // filter. IE, "image/" would return "image/png" as well as "image/jpeg".
3104        if (contentTypeQueryParameters != null && !contentTypeQueryParameters.isEmpty()) {
3105            final int size = contentTypeQueryParameters.size();
3106            sb.append("AND (");
3107            for (int i = 0; i < size; i++) {
3108                final String contentType = contentTypeQueryParameters.get(i);
3109                sb.append(AttachmentColumns.MIME_TYPE + " LIKE '" + contentType + "%'");
3110
3111                if (i != size - 1) {
3112                    sb.append(" OR ");
3113                }
3114            }
3115            sb.append(")");
3116        }
3117        return sb.toString();
3118    }
3119
3120    /**
3121     * Generate the "single attachment" SQLite query, given a projection from UnifiedEmail
3122     *
3123     * @param uiProjection as passed from UnifiedEmail
3124     * @return the SQLite query to be executed on the EmailProvider database
3125     */
3126    private String genQueryAttachment(String[] uiProjection) {
3127        StringBuilder sb = genSelect(getAttachmentMap(), uiProjection);
3128        sb.append(" FROM " + Attachment.TABLE_NAME + " WHERE " + AttachmentColumns.ID + " =? ");
3129        return sb.toString();
3130    }
3131
3132    /**
3133     * Generate the "subfolder list" SQLite query, given a projection from UnifiedEmail
3134     *
3135     * @param uiProjection as passed from UnifiedEmail
3136     * @return the SQLite query to be executed on the EmailProvider database
3137     */
3138    private String genQuerySubfolders(String[] uiProjection) {
3139        StringBuilder sb = genSelect(getFolderListMap(), uiProjection);
3140        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.PARENT_KEY +
3141                " =? ORDER BY ");
3142        sb.append(MAILBOX_ORDER_BY);
3143        return sb.toString();
3144    }
3145
3146    private static final String COMBINED_ACCOUNT_ID_STRING = Long.toString(COMBINED_ACCOUNT_ID);
3147
3148    /**
3149     * Returns a cursor over all the folders for a specific URI which corresponds to a single
3150     * account.
3151     * @param uri
3152     * @param uiProjection
3153     * @return
3154     */
3155    private Cursor uiFolders(Uri uri, String[] uiProjection) {
3156        Context context = getContext();
3157        SQLiteDatabase db = getDatabase(context);
3158        String id = uri.getPathSegments().get(1);
3159        if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3160            MatrixCursor mc = new MatrixCursor(UIProvider.FOLDERS_PROJECTION, 2);
3161            Object[] row = getVirtualMailboxRow(COMBINED_ACCOUNT_ID, Mailbox.TYPE_INBOX);
3162            int numUnread = EmailContent.count(context, Message.CONTENT_URI,
3163                     MessageColumns.MAILBOX_KEY + " IN (SELECT " + MailboxColumns.ID +
3164                    " FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.TYPE +
3165                    "=" + Mailbox.TYPE_INBOX + ") AND " + MessageColumns.FLAG_READ + "=0", null);
3166            row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numUnread;
3167            mc.addRow(row);
3168            int numStarred = EmailContent.count(context, Message.CONTENT_URI,
3169                    MessageColumns.FLAG_FAVORITE + "=1", null);
3170            if (numStarred > 0) {
3171                row = getVirtualMailboxRow(COMBINED_ACCOUNT_ID, Mailbox.TYPE_STARRED);
3172                row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numStarred;
3173                mc.addRow(row);
3174            }
3175            return mc;
3176        } else {
3177            Cursor c = db.rawQuery(genQueryAccountMailboxes(uiProjection), new String[] {id});
3178            c = getFolderListCursor(db, c, uiProjection);
3179            int numStarred = EmailContent.count(context, Message.CONTENT_URI,
3180                    MessageColumns.ACCOUNT_KEY + "=? AND " + MessageColumns.FLAG_FAVORITE + "=1",
3181                    new String[] {id});
3182            if (numStarred == 0) {
3183                return c;
3184            } else {
3185                // Add starred virtual folder to the cursor
3186                // Show number of messages as unread count (for backward compatibility)
3187                MatrixCursor starCursor = new MatrixCursor(uiProjection, 1);
3188                Object[] row = getVirtualMailboxRow(Long.parseLong(id), Mailbox.TYPE_STARRED);
3189                row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numStarred;
3190                row[UIProvider.FOLDER_ICON_RES_ID_COLUMN] = R.drawable.ic_menu_star_holo_light;
3191                starCursor.addRow(row);
3192                Cursor[] cursors = new Cursor[] {starCursor, c};
3193                return new MergeCursor(cursors);
3194            }
3195        }
3196    }
3197
3198    /**
3199     * Returns an array of the default recent folders for a given URI which is unique for an
3200     * account. Some accounts might not have default recent folders, in which case an empty array
3201     * is returned.
3202     * @param id
3203     * @return
3204     */
3205    private Uri[] defaultRecentFolders(final String id) {
3206        final SQLiteDatabase db = getDatabase(getContext());
3207        if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3208            // We don't have default recents for the combined view.
3209            return new Uri[0];
3210        }
3211        // We search for the types we want, and find corresponding IDs.
3212        final String[] idAndType = { BaseColumns._ID, UIProvider.FolderColumns.TYPE };
3213
3214        // Sent, Drafts, and Starred are the default recents.
3215        final StringBuilder sb = genSelect(getFolderListMap(), idAndType);
3216        sb.append(" FROM " + Mailbox.TABLE_NAME
3217                + " WHERE " + MailboxColumns.ACCOUNT_KEY + " = " + id
3218                + " AND "
3219                + MailboxColumns.TYPE + " IN (" + Mailbox.TYPE_SENT +
3220                    ", " + Mailbox.TYPE_DRAFTS +
3221                    ", " + Mailbox.TYPE_STARRED
3222                + ")");
3223        LogUtils.d(TAG, "defaultRecentFolders: Query is %s", sb);
3224        final Cursor c = db.rawQuery(sb.toString(), null);
3225        if (c == null || c.getCount() <= 0 || !c.moveToFirst()) {
3226            return new Uri[0];
3227        }
3228        // Read all the IDs of the mailboxes, and turn them into URIs.
3229        final Uri[] recentFolders = new Uri[c.getCount()];
3230        int i = 0;
3231        do {
3232            final long folderId = c.getLong(0);
3233            recentFolders[i] = uiUri("uifolder", folderId);
3234            LogUtils.d(TAG, "Default recent folder: %d, with uri %s", folderId, recentFolders[i]);
3235            ++i;
3236        } while (c.moveToNext());
3237        return recentFolders;
3238    }
3239
3240    /**
3241     * Wrapper that handles the visibility feature (i.e. the conversation list is visible, so
3242     * any pending notifications for the corresponding mailbox should be canceled). We also handle
3243     * getExtras() to provide a snapshot of the mailbox's status
3244     */
3245    static class VisibilityCursor extends CursorWrapper {
3246        private final long mMailboxId;
3247        private final Context mContext;
3248        private final Bundle mExtras = new Bundle();
3249
3250        public VisibilityCursor(Context context, Cursor cursor, long mailboxId) {
3251            super(cursor);
3252            mMailboxId = mailboxId;
3253            mContext = context;
3254            Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
3255            if (mailbox != null) {
3256                mExtras.putInt(UIProvider.CursorExtraKeys.EXTRA_STATUS, mailbox.mUiSyncStatus);
3257                if (mailbox.mUiLastSyncResult != UIProvider.LastSyncResult.SUCCESS) {
3258                    mExtras.putInt(UIProvider.CursorExtraKeys.EXTRA_ERROR,
3259                            mailbox.mUiLastSyncResult);
3260                }
3261            }
3262        }
3263
3264        public Bundle getExtras() {
3265            return mExtras;
3266        }
3267
3268        @Override
3269        public Bundle respond(Bundle params) {
3270            final String setVisibilityKey =
3271                    UIProvider.ConversationCursorCommand.COMMAND_KEY_SET_VISIBILITY;
3272            if (params.containsKey(setVisibilityKey)) {
3273                final boolean visible = params.getBoolean(setVisibilityKey);
3274                if (visible) {
3275                    NotificationController.getInstance(mContext).cancelNewMessageNotification(
3276                            mMailboxId);
3277                    // Clear the visible limit of the mailbox (if any) on entry
3278                    if (params.containsKey(
3279                            UIProvider.ConversationCursorCommand.COMMAND_KEY_ENTERED_FOLDER)) {
3280                        Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, mMailboxId);
3281                        if (mailbox != null && mailbox.mVisibleLimit > 0) {
3282                            ContentValues values = new ContentValues();
3283                            values.put(MailboxColumns.VISIBLE_LIMIT, 0);
3284                            mContext.getContentResolver().update(
3285                                    ContentUris.withAppendedId(Mailbox.CONTENT_URI, mMailboxId),
3286                                    values, null, null);
3287                        }
3288                    }
3289                }
3290            }
3291            // Return success
3292            Bundle response = new Bundle();
3293            response.putString(setVisibilityKey,
3294                    UIProvider.ConversationCursorCommand.COMMAND_RESPONSE_OK);
3295            return response;
3296        }
3297    }
3298
3299    /**
3300     * For debugging purposes; shouldn't be used in production code
3301     */
3302    static class CloseDetectingCursor extends CursorWrapper {
3303
3304        public CloseDetectingCursor(Cursor cursor) {
3305            super(cursor);
3306        }
3307
3308        public void close() {
3309            super.close();
3310            Log.d(TAG, "Closing cursor", new Error());
3311        }
3312    }
3313
3314    /**
3315     * We need to do individual queries for the mailboxes in order to get correct
3316     * folder capabilities.
3317     */
3318    Cursor getFolderListCursor(SQLiteDatabase db, Cursor c, String[] uiProjection) {
3319        final MatrixCursor mc = new MatrixCursor(uiProjection);
3320        Object[] values = new Object[uiProjection.length];
3321        String[] args = new String[1];
3322        try {
3323            // Loop through mailboxes, building matrix cursor
3324            while (c.moveToNext()) {
3325                String id = c.getString(0);
3326                args[0] = id;
3327                Cursor mailboxCursor = db.rawQuery(genQueryMailbox(uiProjection, id), args);
3328                if (mailboxCursor.moveToNext()) {
3329                    for (int i = 0; i < uiProjection.length; i++) {
3330                        values[i] = mailboxCursor.getString(i);
3331                    }
3332                    mc.addRow(values);
3333                }
3334            }
3335        } finally {
3336            c.close();
3337        }
3338       return mc;
3339    }
3340
3341    /**
3342     * Handle UnifiedEmail queries here (dispatched from query())
3343     *
3344     * @param match the UriMatcher match for the original uri passed in from UnifiedEmail
3345     * @param uri the original uri passed in from UnifiedEmail
3346     * @param uiProjection the projection passed in from UnifiedEmail
3347     * @return the result Cursor
3348     */
3349    private Cursor uiQuery(int match, Uri uri, String[] uiProjection) {
3350        Context context = getContext();
3351        ContentResolver resolver = context.getContentResolver();
3352        SQLiteDatabase db = getDatabase(context);
3353        // Should we ever return null, or throw an exception??
3354        Cursor c = null;
3355        String id = uri.getPathSegments().get(1);
3356        Uri notifyUri = null;
3357        switch(match) {
3358            case UI_ALL_FOLDERS:
3359                c = db.rawQuery(genQueryAccountAllMailboxes(uiProjection), new String[] {id});
3360                c = getFolderListCursor(db, c, uiProjection);
3361                break;
3362            case UI_RECENT_FOLDERS:
3363                c = db.rawQuery(genQueryRecentMailboxes(uiProjection), new String[] {id});
3364                notifyUri = UIPROVIDER_RECENT_FOLDERS_NOTIFIER.buildUpon().appendPath(id).build();
3365                break;
3366            case UI_SUBFOLDERS:
3367                c = db.rawQuery(genQuerySubfolders(uiProjection), new String[] {id});
3368                c = getFolderListCursor(db, c, uiProjection);
3369                break;
3370            case UI_MESSAGES:
3371                long mailboxId = Long.parseLong(id);
3372                if (isVirtualMailbox(mailboxId)) {
3373                    c = getVirtualMailboxMessagesCursor(db, uiProjection, mailboxId);
3374                } else {
3375                    c = db.rawQuery(genQueryMailboxMessages(uiProjection), new String[] {id});
3376                }
3377                notifyUri = UIPROVIDER_CONVERSATION_NOTIFIER.buildUpon().appendPath(id).build();
3378                c = new VisibilityCursor(context, c, mailboxId);
3379                break;
3380            case UI_MESSAGE:
3381                MessageQuery qq = genQueryViewMessage(uiProjection, id);
3382                String sql = qq.query;
3383                String attJson = qq.attachmentJson;
3384                // With attachments, we have another argument to bind
3385                if (attJson != null) {
3386                    c = db.rawQuery(sql, new String[] {attJson, id});
3387                } else {
3388                    c = db.rawQuery(sql, new String[] {id});
3389                }
3390                break;
3391            case UI_ATTACHMENTS:
3392                final List<String> contentTypeQueryParameters =
3393                        uri.getQueryParameters(PhotoContract.ContentTypeParameters.CONTENT_TYPE);
3394                c = db.rawQuery(genQueryAttachments(uiProjection, contentTypeQueryParameters),
3395                        new String[] {id});
3396                notifyUri = UIPROVIDER_ATTACHMENTS_NOTIFIER.buildUpon().appendPath(id).build();
3397                break;
3398            case UI_ATTACHMENT:
3399                c = db.rawQuery(genQueryAttachment(uiProjection), new String[] {id});
3400                notifyUri = UIPROVIDER_ATTACHMENT_NOTIFIER.buildUpon().appendPath(id).build();
3401                break;
3402            case UI_FOLDER:
3403                mailboxId = Long.parseLong(id);
3404                if (isVirtualMailbox(mailboxId)) {
3405                    c = getVirtualMailboxCursor(mailboxId);
3406                } else {
3407                    c = db.rawQuery(genQueryMailbox(uiProjection, id), new String[] {id});
3408                    notifyUri = UIPROVIDER_FOLDER_NOTIFIER.buildUpon().appendPath(id).build();
3409                }
3410                break;
3411            case UI_ACCOUNT:
3412                if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3413                    MatrixCursor mc = new MatrixCursor(uiProjection, 1);
3414                    addCombinedAccountRow(mc);
3415                    c = mc;
3416                } else {
3417                    c = db.rawQuery(genQueryAccount(uiProjection, id), new String[] {id});
3418                }
3419                notifyUri = UIPROVIDER_ACCOUNT_NOTIFIER.buildUpon().appendPath(id).build();
3420                break;
3421            case UI_CONVERSATION:
3422                c = db.rawQuery(genQueryConversation(uiProjection), new String[] {id});
3423                break;
3424        }
3425        if (notifyUri != null) {
3426            c.setNotificationUri(resolver, notifyUri);
3427        }
3428        return c;
3429    }
3430
3431    /**
3432     * Convert a UIProvider attachment to an EmailProvider attachment (for sending); we only need
3433     * a few of the fields
3434     * @param uiAtt the UIProvider attachment to convert
3435     * @return the EmailProvider attachment
3436     */
3437    private Attachment convertUiAttachmentToAttachment(
3438            com.android.mail.providers.Attachment uiAtt) {
3439        Attachment att = new Attachment();
3440        att.mContentUri = uiAtt.contentUri.toString();
3441        att.mFileName = uiAtt.name;
3442        att.mMimeType = uiAtt.contentType;
3443        att.mSize = uiAtt.size;
3444        return att;
3445    }
3446
3447    private String getMailboxNameForType(int mailboxType) {
3448        Context context = getContext();
3449        int resId;
3450        switch (mailboxType) {
3451            case Mailbox.TYPE_INBOX:
3452                resId = R.string.mailbox_name_server_inbox;
3453                break;
3454            case Mailbox.TYPE_OUTBOX:
3455                resId = R.string.mailbox_name_server_outbox;
3456                break;
3457            case Mailbox.TYPE_DRAFTS:
3458                resId = R.string.mailbox_name_server_drafts;
3459                break;
3460            case Mailbox.TYPE_TRASH:
3461                resId = R.string.mailbox_name_server_trash;
3462                break;
3463            case Mailbox.TYPE_SENT:
3464                resId = R.string.mailbox_name_server_sent;
3465                break;
3466            case Mailbox.TYPE_JUNK:
3467                resId = R.string.mailbox_name_server_junk;
3468                break;
3469            case Mailbox.TYPE_STARRED:
3470                resId = R.string.widget_starred;
3471                break;
3472            default:
3473                throw new IllegalArgumentException("Illegal mailbox type");
3474        }
3475        return context.getString(resId);
3476    }
3477
3478    /**
3479     * Create a mailbox given the account and mailboxType.
3480     */
3481    private Mailbox createMailbox(long accountId, int mailboxType) {
3482        Context context = getContext();
3483        Mailbox box = Mailbox.newSystemMailbox(accountId, mailboxType,
3484                getMailboxNameForType(mailboxType));
3485        // Make sure drafts and save will show up in recents...
3486        // If these already exist (from old Email app), they will have touch times
3487        switch (mailboxType) {
3488            case Mailbox.TYPE_DRAFTS:
3489                box.mLastTouchedTime = Mailbox.DRAFTS_DEFAULT_TOUCH_TIME;
3490                break;
3491            case Mailbox.TYPE_SENT:
3492                box.mLastTouchedTime = Mailbox.SENT_DEFAULT_TOUCH_TIME;
3493                break;
3494        }
3495        box.save(context);
3496        return box;
3497    }
3498
3499    /**
3500     * Given an account name and a mailbox type, return that mailbox, creating it if necessary
3501     * @param accountName the account name to use
3502     * @param mailboxType the type of mailbox we're trying to find
3503     * @return the mailbox of the given type for the account in the uri, or null if not found
3504     */
3505    private Mailbox getMailboxByAccountIdAndType(String accountId, int mailboxType) {
3506        long id = Long.parseLong(accountId);
3507        Mailbox mailbox = Mailbox.restoreMailboxOfType(getContext(), id, mailboxType);
3508        if (mailbox == null) {
3509            mailbox = createMailbox(id, mailboxType);
3510        }
3511        return mailbox;
3512    }
3513
3514    private Message getMessageFromPathSegments(List<String> pathSegments) {
3515        Message msg = null;
3516        if (pathSegments.size() > 2) {
3517            msg = Message.restoreMessageWithId(getContext(), Long.parseLong(pathSegments.get(2)));
3518        }
3519        if (msg == null) {
3520            msg = new Message();
3521        }
3522        return msg;
3523    }
3524    /**
3525     * Given a mailbox and the content values for a message, create/save the message in the mailbox
3526     * @param mailbox the mailbox to use
3527     * @param values the content values that represent message fields
3528     * @return the uri of the newly created message
3529     */
3530    private Uri uiSaveMessage(Message msg, Mailbox mailbox, ContentValues values) {
3531        Context context = getContext();
3532        // Fill in the message
3533        Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey);
3534        if (account == null) return null;
3535        msg.mFrom = account.mEmailAddress;
3536        msg.mTimeStamp = System.currentTimeMillis();
3537        msg.mTo = values.getAsString(UIProvider.MessageColumns.TO);
3538        msg.mCc = values.getAsString(UIProvider.MessageColumns.CC);
3539        msg.mBcc = values.getAsString(UIProvider.MessageColumns.BCC);
3540        msg.mSubject = values.getAsString(UIProvider.MessageColumns.SUBJECT);
3541        msg.mText = values.getAsString(UIProvider.MessageColumns.BODY_TEXT);
3542        msg.mHtml = values.getAsString(UIProvider.MessageColumns.BODY_HTML);
3543        msg.mMailboxKey = mailbox.mId;
3544        msg.mAccountKey = mailbox.mAccountKey;
3545        msg.mDisplayName = msg.mTo;
3546        msg.mFlagLoaded = Message.FLAG_LOADED_COMPLETE;
3547        msg.mFlagRead = true;
3548        Integer quoteStartPos = values.getAsInteger(UIProvider.MessageColumns.QUOTE_START_POS);
3549        msg.mQuotedTextStartPos = quoteStartPos == null ? 0 : quoteStartPos;
3550        int flags = 0;
3551        int draftType = values.getAsInteger(UIProvider.MessageColumns.DRAFT_TYPE);
3552        switch(draftType) {
3553            case DraftType.FORWARD:
3554                flags |= Message.FLAG_TYPE_FORWARD;
3555                break;
3556            case DraftType.REPLY_ALL:
3557                flags |= Message.FLAG_TYPE_REPLY_ALL;
3558                // Fall through
3559            case DraftType.REPLY:
3560                flags |= Message.FLAG_TYPE_REPLY;
3561                break;
3562            case DraftType.COMPOSE:
3563                flags |= Message.FLAG_TYPE_ORIGINAL;
3564                break;
3565        }
3566        msg.mFlags = flags;
3567        int draftInfo = 0;
3568        if (values.containsKey(UIProvider.MessageColumns.QUOTE_START_POS)) {
3569            draftInfo = values.getAsInteger(UIProvider.MessageColumns.QUOTE_START_POS);
3570            if (values.getAsInteger(UIProvider.MessageColumns.APPEND_REF_MESSAGE_CONTENT) != 0) {
3571                draftInfo |= Message.DRAFT_INFO_APPEND_REF_MESSAGE;
3572            }
3573        }
3574        msg.mDraftInfo = draftInfo;
3575        String ref = values.getAsString(UIProvider.MessageColumns.REF_MESSAGE_ID);
3576        if (ref != null && msg.mQuotedTextStartPos >= 0) {
3577            String refId = Uri.parse(ref).getLastPathSegment();
3578            try {
3579                long sourceKey = Long.parseLong(refId);
3580                msg.mSourceKey = sourceKey;
3581            } catch (NumberFormatException e) {
3582                // This will be zero; the default
3583            }
3584        }
3585
3586        // Get attachments from the ContentValues
3587        List<com.android.mail.providers.Attachment> uiAtts =
3588                com.android.mail.providers.Attachment.fromJSONArray(
3589                        values.getAsString(UIProvider.MessageColumns.JOINED_ATTACHMENT_INFOS));
3590        ArrayList<Attachment> atts = new ArrayList<Attachment>();
3591        boolean hasUnloadedAttachments = false;
3592        for (com.android.mail.providers.Attachment uiAtt: uiAtts) {
3593            Uri attUri = uiAtt.uri;
3594            if (attUri != null && attUri.getAuthority().equals(EmailContent.AUTHORITY)) {
3595                // If it's one of ours, retrieve the attachment and add it to the list
3596                long attId = Long.parseLong(attUri.getLastPathSegment());
3597                Attachment att = Attachment.restoreAttachmentWithId(context, attId);
3598                if (att != null) {
3599                    // We must clone the attachment into a new one for this message; easiest to
3600                    // use a parcel here
3601                    Parcel p = Parcel.obtain();
3602                    att.writeToParcel(p, 0);
3603                    p.setDataPosition(0);
3604                    Attachment attClone = new Attachment(p);
3605                    p.recycle();
3606                    // Clear the messageKey (this is going to be a new attachment)
3607                    attClone.mMessageKey = 0;
3608                    // If we're sending this, it's not loaded, and we're not smart forwarding
3609                    // add the download flag, so that ADS will start up
3610                    if (mailbox.mType == Mailbox.TYPE_OUTBOX && att.mContentUri == null &&
3611                            ((account.mFlags & Account.FLAGS_SUPPORTS_SMART_FORWARD) == 0)) {
3612                        attClone.mFlags |= Attachment.FLAG_DOWNLOAD_FORWARD;
3613                        hasUnloadedAttachments = true;
3614                    }
3615                    atts.add(attClone);
3616                }
3617            } else {
3618                // Convert external attachment to one of ours and add to the list
3619                atts.add(convertUiAttachmentToAttachment(uiAtt));
3620            }
3621        }
3622        if (!atts.isEmpty()) {
3623            msg.mAttachments = atts;
3624            msg.mFlagAttachment = true;
3625            if (hasUnloadedAttachments) {
3626                Utility.showToast(context, R.string.message_view_attachment_background_load);
3627            }
3628        }
3629        // Save it or update it...
3630        if (!msg.isSaved()) {
3631            msg.save(context);
3632        } else {
3633            // This is tricky due to how messages/attachments are saved; rather than putz with
3634            // what's changed, we'll delete/re-add them
3635            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
3636            // Delete all existing attachments
3637            ops.add(ContentProviderOperation.newDelete(
3638                    ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, msg.mId))
3639                    .build());
3640            // Delete the body
3641            ops.add(ContentProviderOperation.newDelete(Body.CONTENT_URI)
3642                    .withSelection(Body.MESSAGE_KEY + "=?", new String[] {Long.toString(msg.mId)})
3643                    .build());
3644            // Add the ops for the message, atts, and body
3645            msg.addSaveOps(ops);
3646            // Do it!
3647            try {
3648                applyBatch(ops);
3649            } catch (OperationApplicationException e) {
3650            }
3651        }
3652        if (mailbox.mType == Mailbox.TYPE_OUTBOX) {
3653            EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
3654                    mServiceCallback, mailbox.mAccountKey);
3655            try {
3656                service.startSync(mailbox.mId, true);
3657            } catch (RemoteException e) {
3658            }
3659            long originalMsgId = msg.mSourceKey;
3660            if (originalMsgId != 0) {
3661                Message originalMsg = Message.restoreMessageWithId(context, originalMsgId);
3662                // If the original message exists, set its forwarded/replied to flags
3663                if (originalMsg != null) {
3664                    ContentValues cv = new ContentValues();
3665                    flags = originalMsg.mFlags;
3666                    switch(draftType) {
3667                        case DraftType.FORWARD:
3668                            flags |= Message.FLAG_FORWARDED;
3669                            break;
3670                        case DraftType.REPLY_ALL:
3671                        case DraftType.REPLY:
3672                            flags |= Message.FLAG_REPLIED_TO;
3673                            break;
3674                    }
3675                    cv.put(Message.FLAGS, flags);
3676                    context.getContentResolver().update(ContentUris.withAppendedId(
3677                            Message.CONTENT_URI, originalMsgId), cv, null, null);
3678                }
3679            }
3680        }
3681        return uiUri("uimessage", msg.mId);
3682    }
3683
3684    /**
3685     * Create and send the message via the account indicated in the uri
3686     * @param uri the incoming uri
3687     * @param values the content values that represent message fields
3688     * @return the uri of the created message
3689     */
3690    private Uri uiSendMail(Uri uri, ContentValues values) {
3691        List<String> pathSegments = uri.getPathSegments();
3692        Mailbox mailbox = getMailboxByAccountIdAndType(pathSegments.get(1), Mailbox.TYPE_OUTBOX);
3693        if (mailbox == null) return null;
3694        Message msg = getMessageFromPathSegments(pathSegments);
3695        try {
3696            return uiSaveMessage(msg, mailbox, values);
3697        } finally {
3698            // Kick observers
3699            getContext().getContentResolver().notifyChange(Mailbox.CONTENT_URI, null);
3700        }
3701    }
3702
3703    /**
3704     * Create a message and save it to the drafts folder of the account indicated in the uri
3705     * @param uri the incoming uri
3706     * @param values the content values that represent message fields
3707     * @return the uri of the created message
3708     */
3709    private Uri uiSaveDraft(Uri uri, ContentValues values) {
3710        List<String> pathSegments = uri.getPathSegments();
3711        Mailbox mailbox = getMailboxByAccountIdAndType(pathSegments.get(1), Mailbox.TYPE_DRAFTS);
3712        if (mailbox == null) return null;
3713        Message msg = getMessageFromPathSegments(pathSegments);
3714        return uiSaveMessage(msg, mailbox, values);
3715    }
3716
3717    private int uiUpdateDraft(Uri uri, ContentValues values) {
3718        Context context = getContext();
3719        Message msg = Message.restoreMessageWithId(context,
3720                Long.parseLong(uri.getPathSegments().get(1)));
3721        if (msg == null) return 0;
3722        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
3723        if (mailbox == null) return 0;
3724        uiSaveMessage(msg, mailbox, values);
3725        return 1;
3726    }
3727
3728    private int uiSendDraft(Uri uri, ContentValues values) {
3729        Context context = getContext();
3730        Message msg = Message.restoreMessageWithId(context,
3731                Long.parseLong(uri.getPathSegments().get(1)));
3732        if (msg == null) return 0;
3733        long mailboxId = Mailbox.findMailboxOfType(context, msg.mAccountKey, Mailbox.TYPE_OUTBOX);
3734        if (mailboxId == Mailbox.NO_MAILBOX) return 0;
3735        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
3736        if (mailbox == null) return 0;
3737        uiSaveMessage(msg, mailbox, values);
3738        // Kick observers
3739        context.getContentResolver().notifyChange(Mailbox.CONTENT_URI, null);
3740        return 1;
3741    }
3742
3743    private void putIntegerLongOrBoolean(ContentValues values, String columnName, Object value) {
3744        if (value instanceof Integer) {
3745            Integer intValue = (Integer)value;
3746            values.put(columnName, intValue);
3747        } else if (value instanceof Boolean) {
3748            Boolean boolValue = (Boolean)value;
3749            values.put(columnName, boolValue ? 1 : 0);
3750        } else if (value instanceof Long) {
3751            Long longValue = (Long)value;
3752            values.put(columnName, longValue);
3753        }
3754    }
3755
3756    /**
3757     * Update the timestamps for the folders specified and notifies on the recent folder URI.
3758     * @param folders
3759     * @return number of folders updated
3760     */
3761    private int updateTimestamp(final Context context, String id, Uri[] folders){
3762        int updated = 0;
3763        final long now = System.currentTimeMillis();
3764        final ContentResolver resolver = context.getContentResolver();
3765        final ContentValues touchValues = new ContentValues();
3766        for (int i=0, size=folders.length; i < size; ++i) {
3767            touchValues.put(MailboxColumns.LAST_TOUCHED_TIME, now);
3768            LogUtils.d(TAG, "updateStamp: %s updated", folders[i]);
3769            updated += resolver.update(folders[i], touchValues, null, null);
3770        }
3771        final Uri toNotify =
3772                UIPROVIDER_RECENT_FOLDERS_NOTIFIER.buildUpon().appendPath(id).build();
3773        LogUtils.d(TAG, "updateTimestamp: Notifying on %s", toNotify);
3774        resolver.notifyChange(toNotify, null);
3775        return updated;
3776    }
3777
3778    /**
3779     * Updates the recent folders. The values to be updated are specified as ContentValues pairs
3780     * of (Folder URI, access timestamp). Returns nonzero if successful, always.
3781     * @param uri
3782     * @param values
3783     * @return nonzero value always.
3784     */
3785    private int uiUpdateRecentFolders(Uri uri, ContentValues values) {
3786        final int numFolders = values.size();
3787        final String id = uri.getPathSegments().get(1);
3788        final Uri[] folders = new Uri[numFolders];
3789        final Context context = getContext();
3790        final NotificationController controller = NotificationController.getInstance(context);
3791        int i = 0;
3792        for (final String uriString: values.keySet()) {
3793            folders[i] = Uri.parse(uriString);
3794            try {
3795                final String mailboxIdString = folders[i].getLastPathSegment();
3796                final long mailboxId = Long.parseLong(mailboxIdString);
3797                controller.cancelNewMessageNotification(mailboxId);
3798            } catch (NumberFormatException e) {
3799                // Keep on going...
3800            }
3801        }
3802        return updateTimestamp(context, id, folders);
3803    }
3804
3805    /**
3806     * Populates the recent folders according to the design.
3807     * @param uri
3808     * @return the number of recent folders were populated.
3809     */
3810    private int uiPopulateRecentFolders(Uri uri) {
3811        final Context context = getContext();
3812        final String id = uri.getLastPathSegment();
3813        final Uri[] recentFolders = defaultRecentFolders(id);
3814        final int numFolders = recentFolders.length;
3815        if (numFolders <= 0) {
3816            return 0;
3817        }
3818        final int rowsUpdated = updateTimestamp(context, id, recentFolders);
3819        LogUtils.d(TAG, "uiPopulateRecentFolders: %d folders changed", rowsUpdated);
3820        return rowsUpdated;
3821    }
3822
3823    private int uiUpdateAttachment(Uri uri, ContentValues uiValues) {
3824        Integer stateValue = uiValues.getAsInteger(UIProvider.AttachmentColumns.STATE);
3825        if (stateValue != null) {
3826            // This is a command from UIProvider
3827            long attachmentId = Long.parseLong(uri.getLastPathSegment());
3828            Context context = getContext();
3829            Attachment attachment =
3830                    Attachment.restoreAttachmentWithId(context, attachmentId);
3831            if (attachment == null) {
3832                // Went away; ah, well...
3833                return 0;
3834            }
3835            ContentValues values = new ContentValues();
3836            switch (stateValue.intValue()) {
3837                case UIProvider.AttachmentState.NOT_SAVED:
3838                    // Set state, try to cancel request
3839                    values.put(AttachmentColumns.UI_STATE, stateValue);
3840                    values.put(AttachmentColumns.FLAGS,
3841                            attachment.mFlags &= ~Attachment.FLAG_DOWNLOAD_USER_REQUEST);
3842                    attachment.update(context, values);
3843                    return 1;
3844                case UIProvider.AttachmentState.DOWNLOADING:
3845                    // Set state and destination; request download
3846                    values.put(AttachmentColumns.UI_STATE, stateValue);
3847                    Integer destinationValue =
3848                        uiValues.getAsInteger(UIProvider.AttachmentColumns.DESTINATION);
3849                    values.put(AttachmentColumns.UI_DESTINATION,
3850                            destinationValue == null ? 0 : destinationValue);
3851                    values.put(AttachmentColumns.FLAGS,
3852                            attachment.mFlags | Attachment.FLAG_DOWNLOAD_USER_REQUEST);
3853                    attachment.update(context, values);
3854                    return 1;
3855                case UIProvider.AttachmentState.SAVED:
3856                    // If this is an inline attachment, notify message has changed
3857                    if (!TextUtils.isEmpty(attachment.mContentId)) {
3858                        notifyUI(UIPROVIDER_MESSAGE_NOTIFIER, attachment.mMessageKey);
3859                    }
3860                    return 1;
3861            }
3862        }
3863        return 0;
3864    }
3865
3866    private int uiUpdateFolder(Uri uri, ContentValues uiValues) {
3867        Uri ourUri = convertToEmailProviderUri(uri, Mailbox.CONTENT_URI, true);
3868        if (ourUri == null) return 0;
3869        ContentValues ourValues = new ContentValues();
3870        // This should only be called via update to "recent folders"
3871        for (String columnName: uiValues.keySet()) {
3872            if (columnName.equals(MailboxColumns.LAST_TOUCHED_TIME)) {
3873                ourValues.put(MailboxColumns.LAST_TOUCHED_TIME, uiValues.getAsLong(columnName));
3874            }
3875        }
3876        return update(ourUri, ourValues, null, null);
3877    }
3878
3879    private ContentValues convertUiMessageValues(Message message, ContentValues values) {
3880        ContentValues ourValues = new ContentValues();
3881        for (String columnName : values.keySet()) {
3882            Object val = values.get(columnName);
3883            if (columnName.equals(UIProvider.ConversationColumns.STARRED)) {
3884                putIntegerLongOrBoolean(ourValues, MessageColumns.FLAG_FAVORITE, val);
3885            } else if (columnName.equals(UIProvider.ConversationColumns.READ)) {
3886                putIntegerLongOrBoolean(ourValues, MessageColumns.FLAG_READ, val);
3887            } else if (columnName.equals(MessageColumns.MAILBOX_KEY)) {
3888                putIntegerLongOrBoolean(ourValues, MessageColumns.MAILBOX_KEY, val);
3889            } else if (columnName.equals(UIProvider.ConversationColumns.RAW_FOLDERS)) {
3890                // Convert from folder list uri to mailbox key
3891                ArrayList<Folder> folders = Folder.getFoldersArray((String) val);
3892                if (folders == null || folders.size() == 0 || folders.size() > 1) {
3893                    LogUtils.d(TAG,
3894                            "Incorrect number of folders for this message: Message is %s",
3895                            message.mId);
3896                } else {
3897                    Folder f = folders.get(0);
3898                    Uri uri = f.uri;
3899                    Long mailboxId = Long.parseLong(uri.getLastPathSegment());
3900                    putIntegerLongOrBoolean(ourValues, MessageColumns.MAILBOX_KEY, mailboxId);
3901                }
3902            } else if (columnName.equals(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES)) {
3903                Address[] fromList = Address.unpack(message.mFrom);
3904                Preferences prefs = Preferences.getPreferences(getContext());
3905                for (Address sender : fromList) {
3906                    String email = sender.getAddress();
3907                    prefs.setSenderAsTrusted(email);
3908                }
3909            } else if (columnName.equals(UIProvider.ConversationColumns.VIEWED)) {
3910                // Ignore for now
3911            } else {
3912                throw new IllegalArgumentException("Can't update " + columnName + " in message");
3913            }
3914        }
3915        return ourValues;
3916    }
3917
3918    private Uri convertToEmailProviderUri(Uri uri, Uri newBaseUri, boolean asProvider) {
3919        String idString = uri.getLastPathSegment();
3920        try {
3921            long id = Long.parseLong(idString);
3922            Uri ourUri = ContentUris.withAppendedId(newBaseUri, id);
3923            if (asProvider) {
3924                ourUri = ourUri.buildUpon().appendQueryParameter(IS_UIPROVIDER, "true").build();
3925            }
3926            return ourUri;
3927        } catch (NumberFormatException e) {
3928            return null;
3929        }
3930    }
3931
3932    private Message getMessageFromLastSegment(Uri uri) {
3933        long messageId = Long.parseLong(uri.getLastPathSegment());
3934        return Message.restoreMessageWithId(getContext(), messageId);
3935    }
3936
3937    /**
3938     * Add an undo operation for the current sequence; if the sequence is newer than what we've had,
3939     * clear out the undo list and start over
3940     * @param uri the uri we're working on
3941     * @param op the ContentProviderOperation to perform upon undo
3942     */
3943    private void addToSequence(Uri uri, ContentProviderOperation op) {
3944        String sequenceString = uri.getQueryParameter(UIProvider.SEQUENCE_QUERY_PARAMETER);
3945        if (sequenceString != null) {
3946            int sequence = Integer.parseInt(sequenceString);
3947            if (sequence > mLastSequence) {
3948                // Reset sequence
3949                mLastSequenceOps.clear();
3950                mLastSequence = sequence;
3951            }
3952            // TODO: Need something to indicate a change isn't ready (undoable)
3953            mLastSequenceOps.add(op);
3954        }
3955    }
3956
3957    // TODO: This should depend on flags on the mailbox...
3958    private boolean uploadsToServer(Context context, Mailbox m) {
3959        if (m.mType == Mailbox.TYPE_DRAFTS || m.mType == Mailbox.TYPE_OUTBOX ||
3960                m.mType == Mailbox.TYPE_SEARCH) {
3961            return false;
3962        }
3963        String protocol = Account.getProtocol(context, m.mAccountKey);
3964        EmailServiceInfo info = EmailServiceUtils.getServiceInfo(context, protocol);
3965        return (info != null && info.syncChanges);
3966    }
3967
3968    private int uiUpdateMessage(Uri uri, ContentValues values) {
3969        return uiUpdateMessage(uri, values, false);
3970    }
3971
3972    private int uiUpdateMessage(Uri uri, ContentValues values, boolean forceSync) {
3973        Context context = getContext();
3974        Message msg = getMessageFromLastSegment(uri);
3975        if (msg == null) return 0;
3976        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
3977        if (mailbox == null) return 0;
3978        Uri ourBaseUri =
3979                (forceSync || uploadsToServer(context, mailbox)) ? Message.SYNCED_CONTENT_URI :
3980                    Message.CONTENT_URI;
3981        Uri ourUri = convertToEmailProviderUri(uri, ourBaseUri, true);
3982        if (ourUri == null) return 0;
3983
3984        // Special case - meeting response
3985        if (values.containsKey(UIProvider.MessageOperations.RESPOND_COLUMN)) {
3986            EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
3987                    mServiceCallback, mailbox.mAccountKey);
3988            try {
3989                service.sendMeetingResponse(msg.mId,
3990                        values.getAsInteger(UIProvider.MessageOperations.RESPOND_COLUMN));
3991                // Delete the message immediately
3992                uiDeleteMessage(uri);
3993                Utility.showToast(context, R.string.confirm_response);
3994                // Notify box has changed so the deletion is reflected in the UI
3995                notifyUIConversationMailbox(mailbox.mId);
3996            } catch (RemoteException e) {
3997            }
3998            return 1;
3999        }
4000
4001        ContentValues undoValues = new ContentValues();
4002        ContentValues ourValues = convertUiMessageValues(msg, values);
4003        for (String columnName: ourValues.keySet()) {
4004            if (columnName.equals(MessageColumns.MAILBOX_KEY)) {
4005                undoValues.put(MessageColumns.MAILBOX_KEY, msg.mMailboxKey);
4006            } else if (columnName.equals(MessageColumns.FLAG_READ)) {
4007                undoValues.put(MessageColumns.FLAG_READ, msg.mFlagRead);
4008            } else if (columnName.equals(MessageColumns.FLAG_FAVORITE)) {
4009                undoValues.put(MessageColumns.FLAG_FAVORITE, msg.mFlagFavorite);
4010            }
4011        }
4012        if (undoValues == null || undoValues.size() == 0) {
4013            return -1;
4014        }
4015        ContentProviderOperation op =
4016                ContentProviderOperation.newUpdate(convertToEmailProviderUri(
4017                        uri, ourBaseUri, false))
4018                        .withValues(undoValues)
4019                        .build();
4020        addToSequence(uri, op);
4021        return update(ourUri, ourValues, null, null);
4022    }
4023
4024    public static final String PICKER_UI_ACCOUNT = "picker_ui_account";
4025    public static final String PICKER_MAILBOX_TYPE = "picker_mailbox_type";
4026    public static final String PICKER_MESSAGE_ID = "picker_message_id";
4027    public static final String PICKER_HEADER_ID = "picker_header_id";
4028
4029    private int uiDeleteMessage(Uri uri) {
4030        final Context context = getContext();
4031        Message msg = getMessageFromLastSegment(uri);
4032        if (msg == null) return 0;
4033        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
4034        if (mailbox == null) return 0;
4035        if (mailbox.mType == Mailbox.TYPE_TRASH || mailbox.mType == Mailbox.TYPE_DRAFTS) {
4036            // We actually delete these, including attachments
4037            AttachmentUtilities.deleteAllAttachmentFiles(context, msg.mAccountKey, msg.mId);
4038            notifyUI(UIPROVIDER_FOLDER_NOTIFIER, mailbox.mId);
4039            return context.getContentResolver().delete(
4040                    ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg.mId), null, null);
4041        }
4042        Mailbox trashMailbox =
4043                Mailbox.restoreMailboxOfType(context, msg.mAccountKey, Mailbox.TYPE_TRASH);
4044        if (trashMailbox == null) {
4045            return 0;
4046        }
4047        ContentValues values = new ContentValues();
4048        values.put(MessageColumns.MAILBOX_KEY, trashMailbox.mId);
4049        notifyUI(UIPROVIDER_FOLDER_NOTIFIER, mailbox.mId);
4050        return uiUpdateMessage(uri, values, true);
4051    }
4052
4053    private int pickFolder(Uri uri, int type, int headerId) {
4054        Context context = getContext();
4055        Long acctId = Long.parseLong(uri.getLastPathSegment());
4056        // For push imap, for example, we want the user to select the trash mailbox
4057        Cursor ac = query(uiUri("uiaccount", acctId), UIProvider.ACCOUNTS_PROJECTION,
4058                null, null, null);
4059        try {
4060            if (ac.moveToFirst()) {
4061                final com.android.mail.providers.Account uiAccount =
4062                        new com.android.mail.providers.Account(ac);
4063                Intent intent = new Intent(context, FolderPickerActivity.class);
4064                intent.putExtra(PICKER_UI_ACCOUNT, uiAccount);
4065                intent.putExtra(PICKER_MAILBOX_TYPE, type);
4066                intent.putExtra(PICKER_HEADER_ID, headerId);
4067                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4068                context.startActivity(intent);
4069                return 1;
4070            }
4071            return 0;
4072        } finally {
4073            ac.close();
4074        }
4075    }
4076
4077    private int pickTrashFolder(Uri uri) {
4078        return pickFolder(uri, Mailbox.TYPE_TRASH, R.string.trash_folder_selection_title);
4079    }
4080
4081    private int pickSentFolder(Uri uri) {
4082        return pickFolder(uri, Mailbox.TYPE_SENT, R.string.sent_folder_selection_title);
4083    }
4084
4085    private Cursor uiUndo(String[] projection) {
4086        // First see if we have any operations saved
4087        // TODO: Make sure seq matches
4088        if (!mLastSequenceOps.isEmpty()) {
4089            try {
4090                // TODO Always use this projection?  Or what's passed in?
4091                // Not sure if UI wants it, but I'm making a cursor of convo uri's
4092                MatrixCursor c = new MatrixCursor(
4093                        new String[] {UIProvider.ConversationColumns.URI},
4094                        mLastSequenceOps.size());
4095                for (ContentProviderOperation op: mLastSequenceOps) {
4096                    c.addRow(new String[] {op.getUri().toString()});
4097                }
4098                // Just apply the batch and we're done!
4099                applyBatch(mLastSequenceOps);
4100                // But clear the operations
4101                mLastSequenceOps.clear();
4102                // Tell the UI there are changes
4103                ContentResolver resolver = getContext().getContentResolver();
4104                resolver.notifyChange(UIPROVIDER_CONVERSATION_NOTIFIER, null);
4105                resolver.notifyChange(UIPROVIDER_FOLDER_NOTIFIER, null);
4106                return c;
4107            } catch (OperationApplicationException e) {
4108            }
4109        }
4110        return new MatrixCursor(projection, 0);
4111    }
4112
4113    private void notifyUIConversation(Uri uri) {
4114        String id = uri.getLastPathSegment();
4115        Message msg = Message.restoreMessageWithId(getContext(), Long.parseLong(id));
4116        if (msg != null) {
4117            notifyUIConversationMailbox(msg.mMailboxKey);
4118        }
4119    }
4120
4121    /**
4122     * Notify about the Mailbox id passed in
4123     * @param id the Mailbox id to be notified
4124     */
4125    private void notifyUIConversationMailbox(long id) {
4126        notifyUI(UIPROVIDER_CONVERSATION_NOTIFIER, Long.toString(id));
4127        Mailbox mailbox = Mailbox.restoreMailboxWithId(getContext(), id);
4128        if (mailbox == null) {
4129            Log.w(TAG, "No mailbox for notification: " + id);
4130            return;
4131        }
4132        // Notify combined inbox...
4133        if (mailbox.mType == Mailbox.TYPE_INBOX) {
4134            notifyUI(UIPROVIDER_CONVERSATION_NOTIFIER,
4135                    EmailProvider.combinedMailboxId(Mailbox.TYPE_INBOX));
4136        }
4137        notifyWidgets(id);
4138    }
4139
4140    private void notifyUI(Uri uri, String id) {
4141        Uri notifyUri = uri.buildUpon().appendPath(id).build();
4142        getContext().getContentResolver().notifyChange(notifyUri, null);
4143    }
4144
4145    private void notifyUI(Uri uri, long id) {
4146        notifyUI(uri, Long.toString(id));
4147    }
4148
4149    /**
4150     * Support for services and service notifications
4151     */
4152
4153    private final IEmailServiceCallback.Stub mServiceCallback =
4154            new IEmailServiceCallback.Stub() {
4155
4156        @Override
4157        public void syncMailboxListStatus(long accountId, int statusCode, int progress)
4158                throws RemoteException {
4159        }
4160
4161        @Override
4162        public void syncMailboxStatus(long mailboxId, int statusCode, int progress)
4163                throws RemoteException {
4164            // We'll get callbacks here from the services, which we'll pass back to the UI
4165            Uri uri = ContentUris.withAppendedId(FOLDER_STATUS_URI, mailboxId);
4166            EmailProvider.this.getContext().getContentResolver().notifyChange(uri, null);
4167        }
4168
4169        @Override
4170        public void loadAttachmentStatus(long messageId, long attachmentId, int statusCode,
4171                int progress) throws RemoteException {
4172        }
4173
4174        @Override
4175        public void sendMessageStatus(long messageId, String subject, int statusCode, int progress)
4176                throws RemoteException {
4177        }
4178
4179        @Override
4180        public void loadMessageStatus(long messageId, int statusCode, int progress)
4181                throws RemoteException {
4182        }
4183    };
4184
4185    private Cursor uiFolderRefresh(Uri uri) {
4186        Context context = getContext();
4187        String idString = uri.getLastPathSegment();
4188        long id = Long.parseLong(idString);
4189        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
4190        if (mailbox == null) return null;
4191        EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
4192                mServiceCallback, mailbox.mAccountKey);
4193        try {
4194            service.startSync(id, true);
4195        } catch (RemoteException e) {
4196        }
4197        return null;
4198    }
4199
4200    //Number of additional messages to load when a user selects "Load more..." in POP/IMAP boxes
4201    public static final int VISIBLE_LIMIT_INCREMENT = 10;
4202    //Number of additional messages to load when a user selects "Load more..." in a search
4203    public static final int SEARCH_MORE_INCREMENT = 10;
4204
4205    private Cursor uiFolderLoadMore(Uri uri) {
4206        Context context = getContext();
4207        String idString = uri.getLastPathSegment();
4208        long id = Long.parseLong(idString);
4209        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
4210        if (mailbox == null) return null;
4211        if (mailbox.mType == Mailbox.TYPE_SEARCH) {
4212            // Ask for 10 more messages
4213            mSearchParams.mOffset += SEARCH_MORE_INCREMENT;
4214            runSearchQuery(context, mailbox.mAccountKey, id);
4215        } else {
4216            ContentValues values = new ContentValues();
4217            values.put(EmailContent.FIELD_COLUMN_NAME, MailboxColumns.VISIBLE_LIMIT);
4218            values.put(EmailContent.ADD_COLUMN_NAME, VISIBLE_LIMIT_INCREMENT);
4219            Uri mailboxUri = ContentUris.withAppendedId(Mailbox.ADD_TO_FIELD_URI, id);
4220            // Increase the limit
4221            context.getContentResolver().update(mailboxUri, values, null, null);
4222            // And order a refresh
4223            uiFolderRefresh(uri);
4224        }
4225        return null;
4226    }
4227
4228    private static final String SEARCH_MAILBOX_SERVER_ID = "__search_mailbox__";
4229    private SearchParams mSearchParams;
4230
4231    /**
4232     * Returns the search mailbox for the specified account, creating one if necessary
4233     * @return the search mailbox for the passed in account
4234     */
4235    private Mailbox getSearchMailbox(long accountId) {
4236        Context context = getContext();
4237        Mailbox m = Mailbox.restoreMailboxOfType(context, accountId, Mailbox.TYPE_SEARCH);
4238        if (m == null) {
4239            m = new Mailbox();
4240            m.mAccountKey = accountId;
4241            m.mServerId = SEARCH_MAILBOX_SERVER_ID;
4242            m.mFlagVisible = false;
4243            m.mDisplayName = SEARCH_MAILBOX_SERVER_ID;
4244            m.mSyncInterval = Mailbox.CHECK_INTERVAL_NEVER;
4245            m.mType = Mailbox.TYPE_SEARCH;
4246            m.mFlags = Mailbox.FLAG_HOLDS_MAIL;
4247            m.mParentKey = Mailbox.NO_MAILBOX;
4248            m.save(context);
4249        }
4250        return m;
4251    }
4252
4253    private void runSearchQuery(final Context context, final long accountId,
4254            final long searchMailboxId) {
4255        // Start the search running in the background
4256        new Thread(new Runnable() {
4257            @Override
4258            public void run() {
4259                 try {
4260                    EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
4261                            mServiceCallback, accountId);
4262                    if (service != null) {
4263                        try {
4264                            // Save away the total count
4265                            mSearchParams.mTotalCount = service.searchMessages(accountId,
4266                                    mSearchParams, searchMailboxId);
4267                            //Log.d(TAG, "TotalCount to UI: " + mSearchParams.mTotalCount);
4268                            notifyUI(UIPROVIDER_FOLDER_NOTIFIER, searchMailboxId);
4269                        } catch (RemoteException e) {
4270                            Log.e("searchMessages", "RemoteException", e);
4271                        }
4272                    }
4273                } finally {
4274                }
4275            }}).start();
4276
4277    }
4278
4279    // TODO: Handle searching for more...
4280    private Cursor uiSearch(Uri uri, String[] projection) {
4281        final long accountId = Long.parseLong(uri.getLastPathSegment());
4282
4283        // TODO: Check the actual mailbox
4284        Mailbox inbox = Mailbox.restoreMailboxOfType(getContext(), accountId, Mailbox.TYPE_INBOX);
4285        if (inbox == null) {
4286            Log.w(Logging.LOG_TAG, "In uiSearch, inbox doesn't exist for account " + accountId);
4287            return null;
4288        }
4289
4290        String filter = uri.getQueryParameter(UIProvider.SearchQueryParameters.QUERY);
4291        if (filter == null) {
4292            throw new IllegalArgumentException("No query parameter in search query");
4293        }
4294
4295        // Find/create our search mailbox
4296        Mailbox searchMailbox = getSearchMailbox(accountId);
4297        final long searchMailboxId = searchMailbox.mId;
4298
4299        mSearchParams = new SearchParams(inbox.mId, filter, searchMailboxId);
4300
4301        final Context context = getContext();
4302        if (mSearchParams.mOffset == 0) {
4303            // Delete existing contents of search mailbox
4304            ContentResolver resolver = context.getContentResolver();
4305            resolver.delete(Message.CONTENT_URI, Message.MAILBOX_KEY + "=" + searchMailboxId,
4306                    null);
4307            ContentValues cv = new ContentValues();
4308            // For now, use the actual query as the name of the mailbox
4309            cv.put(Mailbox.DISPLAY_NAME, mSearchParams.mFilter);
4310            resolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI, searchMailboxId),
4311                    cv, null, null);
4312        }
4313
4314        // Start the search running in the background
4315        runSearchQuery(context, accountId, searchMailboxId);
4316
4317        // This will look just like a "normal" folder
4318        return uiQuery(UI_FOLDER, ContentUris.withAppendedId(Mailbox.CONTENT_URI,
4319                searchMailbox.mId), projection);
4320    }
4321
4322    private static final String MAILBOXES_FOR_ACCOUNT_SELECTION = MailboxColumns.ACCOUNT_KEY + "=?";
4323    private static final String MAILBOXES_FOR_ACCOUNT_EXCEPT_ACCOUNT_MAILBOX_SELECTION =
4324        MAILBOXES_FOR_ACCOUNT_SELECTION + " AND " + MailboxColumns.TYPE + "!=" +
4325        Mailbox.TYPE_EAS_ACCOUNT_MAILBOX;
4326    private static final String MESSAGES_FOR_ACCOUNT_SELECTION = MessageColumns.ACCOUNT_KEY + "=?";
4327
4328    /**
4329     * Delete an account and clean it up
4330     */
4331    private int uiDeleteAccount(Uri uri) {
4332        Context context = getContext();
4333        long accountId = Long.parseLong(uri.getLastPathSegment());
4334        try {
4335            // Get the account URI.
4336            final Account account = Account.restoreAccountWithId(context, accountId);
4337            if (account == null) {
4338                return 0; // Already deleted?
4339            }
4340
4341            deleteAccountData(context, accountId);
4342
4343            // Now delete the account itself
4344            uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId);
4345            context.getContentResolver().delete(uri, null, null);
4346
4347            // Clean up
4348            AccountBackupRestore.backup(context);
4349            SecurityPolicy.getInstance(context).reducePolicies();
4350            MailActivityEmail.setServicesEnabledSync(context);
4351            return 1;
4352        } catch (Exception e) {
4353            Log.w(Logging.LOG_TAG, "Exception while deleting account", e);
4354        }
4355        return 0;
4356    }
4357
4358    private int uiDeleteAccountData(Uri uri) {
4359        Context context = getContext();
4360        long accountId = Long.parseLong(uri.getLastPathSegment());
4361        // Get the account URI.
4362        final Account account = Account.restoreAccountWithId(context, accountId);
4363        if (account == null) {
4364            return 0; // Already deleted?
4365        }
4366        deleteAccountData(context, accountId);
4367        return 1;
4368    }
4369
4370    private void deleteAccountData(Context context, long accountId) {
4371        // Delete synced attachments
4372        AttachmentUtilities.deleteAllAccountAttachmentFiles(context, accountId);
4373
4374        // Delete synced email, leaving only an empty inbox.  We do this in two phases:
4375        // 1. Delete all non-inbox mailboxes (which will delete all of their messages)
4376        // 2. Delete all remaining messages (which will be the inbox messages)
4377        ContentResolver resolver = context.getContentResolver();
4378        String[] accountIdArgs = new String[] { Long.toString(accountId) };
4379        resolver.delete(Mailbox.CONTENT_URI,
4380                MAILBOXES_FOR_ACCOUNT_EXCEPT_ACCOUNT_MAILBOX_SELECTION,
4381                accountIdArgs);
4382        resolver.delete(Message.CONTENT_URI, MESSAGES_FOR_ACCOUNT_SELECTION, accountIdArgs);
4383
4384        // Delete sync keys on remaining items
4385        ContentValues cv = new ContentValues();
4386        cv.putNull(Account.SYNC_KEY);
4387        resolver.update(Account.CONTENT_URI, cv, Account.ID_SELECTION, accountIdArgs);
4388        cv.clear();
4389        cv.putNull(Mailbox.SYNC_KEY);
4390        resolver.update(Mailbox.CONTENT_URI, cv,
4391                MAILBOXES_FOR_ACCOUNT_SELECTION, accountIdArgs);
4392
4393        // Delete PIM data (contacts, calendar), stop syncs, etc. if applicable
4394        IEmailService service = EmailServiceUtils.getServiceForAccount(context, null, accountId);
4395        if (service != null) {
4396            try {
4397                service.deleteAccountPIMData(accountId);
4398            } catch (RemoteException e) {
4399                // Can't do anything about this
4400            }
4401        }
4402    }
4403
4404    private int[] mSavedWidgetIds = new int[0];
4405    private ArrayList<Long> mWidgetNotifyMailboxes = new ArrayList<Long>();
4406    private AppWidgetManager mAppWidgetManager;
4407    private ComponentName mEmailComponent;
4408
4409    private void notifyWidgets(long mailboxId) {
4410        Context context = getContext();
4411        // Lazily initialize these
4412        if (mAppWidgetManager == null) {
4413            mAppWidgetManager = AppWidgetManager.getInstance(context);
4414            mEmailComponent = new ComponentName(context, WidgetProvider.PROVIDER_NAME);
4415        }
4416
4417        // See if we have to populate our array of mailboxes used in widgets
4418        int[] widgetIds = mAppWidgetManager.getAppWidgetIds(mEmailComponent);
4419        if (!Arrays.equals(widgetIds, mSavedWidgetIds)) {
4420            mSavedWidgetIds = widgetIds;
4421            String[][] widgetInfos = BaseWidgetProvider.getWidgetInfo(context, widgetIds);
4422            // widgetInfo now has pairs of account uri/folder uri
4423            mWidgetNotifyMailboxes.clear();
4424            for (String[] widgetInfo: widgetInfos) {
4425                try {
4426                    if (widgetInfo == null) continue;
4427                    long id = Long.parseLong(Uri.parse(widgetInfo[1]).getLastPathSegment());
4428                    if (!isCombinedMailbox(id)) {
4429                        // For a regular mailbox, just add it to the list
4430                        if (!mWidgetNotifyMailboxes.contains(id)) {
4431                            mWidgetNotifyMailboxes.add(id);
4432                        }
4433                    } else {
4434                        switch (getVirtualMailboxType(id)) {
4435                            // We only handle the combined inbox in widgets
4436                            case Mailbox.TYPE_INBOX:
4437                                Cursor c = query(Mailbox.CONTENT_URI, Mailbox.ID_PROJECTION,
4438                                        MailboxColumns.TYPE + "=?",
4439                                        new String[] {Integer.toString(Mailbox.TYPE_INBOX)}, null);
4440                                try {
4441                                    while (c.moveToNext()) {
4442                                        mWidgetNotifyMailboxes.add(
4443                                                c.getLong(Mailbox.ID_PROJECTION_COLUMN));
4444                                    }
4445                                } finally {
4446                                    c.close();
4447                                }
4448                                break;
4449                        }
4450                    }
4451                } catch (NumberFormatException e) {
4452                    // Move along
4453                }
4454            }
4455        }
4456
4457        // If our mailbox needs to be notified, do so...
4458        if (mWidgetNotifyMailboxes.contains(mailboxId)) {
4459            Intent intent = new Intent(Utils.ACTION_NOTIFY_DATASET_CHANGED);
4460            intent.putExtra(Utils.EXTRA_FOLDER_URI, uiUri("uifolder", mailboxId));
4461            intent.setType(EMAIL_APP_MIME_TYPE);
4462            context.sendBroadcast(intent);
4463         }
4464    }
4465}
4466