EmailProvider.java revision 71b57354c1e9df99d121a6725358311b3450c859
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.content.ContentProvider;
20import android.content.ContentProviderOperation;
21import android.content.ContentProviderResult;
22import android.content.ContentResolver;
23import android.content.ContentUris;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
27import android.content.OperationApplicationException;
28import android.content.UriMatcher;
29import android.database.ContentObserver;
30import android.database.Cursor;
31import android.database.MatrixCursor;
32import android.database.sqlite.SQLiteDatabase;
33import android.database.sqlite.SQLiteException;
34import android.net.Uri;
35import android.os.RemoteException;
36import android.provider.BaseColumns;
37import android.text.TextUtils;
38import android.util.Log;
39
40import com.android.common.content.ProjectionMap;
41import com.android.email.Email;
42import com.android.email.Preferences;
43import com.android.email.R;
44import com.android.email.provider.ContentCache.CacheToken;
45import com.android.email.service.AttachmentDownloadService;
46import com.android.email.service.EmailServiceUtils;
47import com.android.emailcommon.Logging;
48import com.android.emailcommon.provider.Account;
49import com.android.emailcommon.provider.EmailContent;
50import com.android.emailcommon.provider.EmailContent.AccountColumns;
51import com.android.emailcommon.provider.EmailContent.Attachment;
52import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
53import com.android.emailcommon.provider.EmailContent.Body;
54import com.android.emailcommon.provider.EmailContent.BodyColumns;
55import com.android.emailcommon.provider.EmailContent.MailboxColumns;
56import com.android.emailcommon.provider.EmailContent.Message;
57import com.android.emailcommon.provider.EmailContent.MessageColumns;
58import com.android.emailcommon.provider.EmailContent.PolicyColumns;
59import com.android.emailcommon.provider.EmailContent.SyncColumns;
60import com.android.emailcommon.provider.HostAuth;
61import com.android.emailcommon.provider.Mailbox;
62import com.android.emailcommon.provider.Policy;
63import com.android.emailcommon.provider.QuickResponse;
64import com.android.emailcommon.service.EmailServiceProxy;
65import com.android.emailcommon.service.IEmailServiceCallback;
66import com.android.mail.providers.UIProvider;
67import com.android.mail.providers.UIProvider.AccountCapabilities;
68import com.android.mail.providers.UIProvider.ConversationPriority;
69import com.android.mail.providers.UIProvider.ConversationSendingState;
70import com.google.common.annotations.VisibleForTesting;
71
72import java.io.File;
73import java.util.ArrayList;
74import java.util.Arrays;
75import java.util.Collection;
76import java.util.HashMap;
77import java.util.List;
78import java.util.Map;
79
80public class EmailProvider extends ContentProvider {
81
82    private static final String TAG = "EmailProvider";
83
84    protected static final String DATABASE_NAME = "EmailProvider.db";
85    protected static final String BODY_DATABASE_NAME = "EmailProviderBody.db";
86    protected static final String BACKUP_DATABASE_NAME = "EmailProviderBackup.db";
87
88    public static final String ACTION_ATTACHMENT_UPDATED = "com.android.email.ATTACHMENT_UPDATED";
89    public static final String ATTACHMENT_UPDATED_EXTRA_FLAGS =
90        "com.android.email.ATTACHMENT_UPDATED_FLAGS";
91
92    /**
93     * Notifies that changes happened. Certain UI components, e.g., widgets, can register for this
94     * {@link android.content.Intent} and update accordingly. However, this can be very broad and
95     * is NOT the preferred way of getting notification.
96     */
97    public static final String ACTION_NOTIFY_MESSAGE_LIST_DATASET_CHANGED =
98        "com.android.email.MESSAGE_LIST_DATASET_CHANGED";
99
100    public static final String EMAIL_MESSAGE_MIME_TYPE =
101        "vnd.android.cursor.item/email-message";
102    public static final String EMAIL_ATTACHMENT_MIME_TYPE =
103        "vnd.android.cursor.item/email-attachment";
104
105    public static final Uri INTEGRITY_CHECK_URI =
106        Uri.parse("content://" + EmailContent.AUTHORITY + "/integrityCheck");
107    public static final Uri ACCOUNT_BACKUP_URI =
108        Uri.parse("content://" + EmailContent.AUTHORITY + "/accountBackup");
109    public static final Uri FOLDER_STATUS_URI =
110            Uri.parse("content://" + EmailContent.AUTHORITY + "/status");
111    public static final Uri FOLDER_REFRESH_URI =
112            Uri.parse("content://" + EmailContent.AUTHORITY + "/refresh");
113
114    /** Appended to the notification URI for delete operations */
115    public static final String NOTIFICATION_OP_DELETE = "delete";
116    /** Appended to the notification URI for insert operations */
117    public static final String NOTIFICATION_OP_INSERT = "insert";
118    /** Appended to the notification URI for update operations */
119    public static final String NOTIFICATION_OP_UPDATE = "update";
120
121    // Definitions for our queries looking for orphaned messages
122    private static final String[] ORPHANS_PROJECTION
123        = new String[] {MessageColumns.ID, MessageColumns.MAILBOX_KEY};
124    private static final int ORPHANS_ID = 0;
125    private static final int ORPHANS_MAILBOX_KEY = 1;
126
127    private static final String WHERE_ID = EmailContent.RECORD_ID + "=?";
128
129    // This is not a hard limit on accounts, per se, but beyond this, we can't guarantee that all
130    // critical mailboxes, host auth's, accounts, and policies are cached
131    private static final int MAX_CACHED_ACCOUNTS = 16;
132    // Inbox, Drafts, Sent, Outbox, Trash, and Search (these boxes are cached when possible)
133    private static final int NUM_ALWAYS_CACHED_MAILBOXES = 6;
134
135    // We'll cache the following four tables; sizes are best estimates of effective values
136    private final ContentCache mCacheAccount =
137        new ContentCache("Account", Account.CONTENT_PROJECTION, MAX_CACHED_ACCOUNTS);
138    private final ContentCache mCacheHostAuth =
139        new ContentCache("HostAuth", HostAuth.CONTENT_PROJECTION, MAX_CACHED_ACCOUNTS * 2);
140    /*package*/ final ContentCache mCacheMailbox =
141        new ContentCache("Mailbox", Mailbox.CONTENT_PROJECTION,
142                MAX_CACHED_ACCOUNTS * (NUM_ALWAYS_CACHED_MAILBOXES + 2));
143    private final ContentCache mCacheMessage =
144        new ContentCache("Message", Message.CONTENT_PROJECTION, 8);
145    private final ContentCache mCachePolicy =
146        new ContentCache("Policy", Policy.CONTENT_PROJECTION, MAX_CACHED_ACCOUNTS);
147
148    private static final int ACCOUNT_BASE = 0;
149    private static final int ACCOUNT = ACCOUNT_BASE;
150    private static final int ACCOUNT_ID = ACCOUNT_BASE + 1;
151    private static final int ACCOUNT_ID_ADD_TO_FIELD = ACCOUNT_BASE + 2;
152    private static final int ACCOUNT_RESET_NEW_COUNT = ACCOUNT_BASE + 3;
153    private static final int ACCOUNT_RESET_NEW_COUNT_ID = ACCOUNT_BASE + 4;
154    private static final int ACCOUNT_DEFAULT_ID = ACCOUNT_BASE + 5;
155
156    private static final int MAILBOX_BASE = 0x1000;
157    private static final int MAILBOX = MAILBOX_BASE;
158    private static final int MAILBOX_ID = MAILBOX_BASE + 1;
159    private static final int MAILBOX_ID_FROM_ACCOUNT_AND_TYPE = MAILBOX_BASE + 2;
160    private static final int MAILBOX_ID_ADD_TO_FIELD = MAILBOX_BASE + 3;
161    private static final int MAILBOX_NOTIFICATION = MAILBOX_BASE + 4;
162    private static final int MAILBOX_MOST_RECENT_MESSAGE = MAILBOX_BASE + 5;
163
164    private static final int MESSAGE_BASE = 0x2000;
165    private static final int MESSAGE = MESSAGE_BASE;
166    private static final int MESSAGE_ID = MESSAGE_BASE + 1;
167    private static final int SYNCED_MESSAGE_ID = MESSAGE_BASE + 2;
168
169    private static final int ATTACHMENT_BASE = 0x3000;
170    private static final int ATTACHMENT = ATTACHMENT_BASE;
171    private static final int ATTACHMENT_ID = ATTACHMENT_BASE + 1;
172    private static final int ATTACHMENTS_MESSAGE_ID = ATTACHMENT_BASE + 2;
173
174    private static final int HOSTAUTH_BASE = 0x4000;
175    private static final int HOSTAUTH = HOSTAUTH_BASE;
176    private static final int HOSTAUTH_ID = HOSTAUTH_BASE + 1;
177
178    private static final int UPDATED_MESSAGE_BASE = 0x5000;
179    private static final int UPDATED_MESSAGE = UPDATED_MESSAGE_BASE;
180    private static final int UPDATED_MESSAGE_ID = UPDATED_MESSAGE_BASE + 1;
181
182    private static final int DELETED_MESSAGE_BASE = 0x6000;
183    private static final int DELETED_MESSAGE = DELETED_MESSAGE_BASE;
184    private static final int DELETED_MESSAGE_ID = DELETED_MESSAGE_BASE + 1;
185
186    private static final int POLICY_BASE = 0x7000;
187    private static final int POLICY = POLICY_BASE;
188    private static final int POLICY_ID = POLICY_BASE + 1;
189
190    private static final int QUICK_RESPONSE_BASE = 0x8000;
191    private static final int QUICK_RESPONSE = QUICK_RESPONSE_BASE;
192    private static final int QUICK_RESPONSE_ID = QUICK_RESPONSE_BASE + 1;
193    private static final int QUICK_RESPONSE_ACCOUNT_ID = QUICK_RESPONSE_BASE + 2;
194
195    private static final int UI_BASE = 0x9000;
196    private static final int UI_FOLDERS = UI_BASE;
197    private static final int UI_SUBFOLDERS = UI_BASE + 1;
198    private static final int UI_MESSAGES = UI_BASE + 2;
199    private static final int UI_MESSAGE = UI_BASE + 3;
200    private static final int UI_SENDMAIL = UI_BASE + 4;
201    private static final int UI_UNDO = UI_BASE + 5;
202    private static final int UI_SAVEDRAFT = UI_BASE + 6;
203    private static final int UI_UPDATEDRAFT = UI_BASE + 7;
204    private static final int UI_SENDDRAFT = UI_BASE + 8;
205    private static final int UI_FOLDER_REFRESH = UI_BASE + 9;
206    private static final int UI_FOLDER = UI_BASE + 10;
207    private static final int UI_ACCOUNT = UI_BASE + 11;
208    private static final int UI_ACCTS = UI_BASE + 12;
209    private static final int UI_SETTINGS = UI_BASE + 13;
210    private static final int UI_ATTACHMENTS = UI_BASE + 14;
211    private static final int UI_ATTACHMENT = UI_BASE + 15;
212
213    // MUST ALWAYS EQUAL THE LAST OF THE PREVIOUS BASE CONSTANTS
214    private static final int LAST_EMAIL_PROVIDER_DB_BASE = UI_BASE;
215
216    // DO NOT CHANGE BODY_BASE!!
217    private static final int BODY_BASE = LAST_EMAIL_PROVIDER_DB_BASE + 0x1000;
218    private static final int BODY = BODY_BASE;
219    private static final int BODY_ID = BODY_BASE + 1;
220
221    private static final int BASE_SHIFT = 12;  // 12 bits to the base type: 0, 0x1000, 0x2000, etc.
222
223    // TABLE_NAMES MUST remain in the order of the BASE constants above (e.g. ACCOUNT_BASE = 0x0000,
224    // MESSAGE_BASE = 0x1000, etc.)
225    private static final String[] TABLE_NAMES = {
226        Account.TABLE_NAME,
227        Mailbox.TABLE_NAME,
228        Message.TABLE_NAME,
229        Attachment.TABLE_NAME,
230        HostAuth.TABLE_NAME,
231        Message.UPDATED_TABLE_NAME,
232        Message.DELETED_TABLE_NAME,
233        Policy.TABLE_NAME,
234        QuickResponse.TABLE_NAME,
235        null,  // UI
236        Body.TABLE_NAME,
237    };
238
239    // CONTENT_CACHES MUST remain in the order of the BASE constants above
240    private final ContentCache[] mContentCaches = {
241        mCacheAccount,
242        mCacheMailbox,
243        mCacheMessage,
244        null, // Attachment
245        mCacheHostAuth,
246        null, // Updated message
247        null, // Deleted message
248        mCachePolicy,
249        null, // Quick response
250        null, // Body
251        null  // UI
252    };
253
254    // CACHE_PROJECTIONS MUST remain in the order of the BASE constants above
255    private static final String[][] CACHE_PROJECTIONS = {
256        Account.CONTENT_PROJECTION,
257        Mailbox.CONTENT_PROJECTION,
258        Message.CONTENT_PROJECTION,
259        null, // Attachment
260        HostAuth.CONTENT_PROJECTION,
261        null, // Updated message
262        null, // Deleted message
263        Policy.CONTENT_PROJECTION,
264        null,  // Quick response
265        null,  // Body
266        null   // UI
267    };
268
269    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
270
271    private static final String MAILBOX_PRE_CACHE_SELECTION = MailboxColumns.TYPE + " IN (" +
272        Mailbox.TYPE_INBOX + "," + Mailbox.TYPE_DRAFTS + "," + Mailbox.TYPE_TRASH + "," +
273        Mailbox.TYPE_SENT + "," + Mailbox.TYPE_SEARCH + "," + Mailbox.TYPE_OUTBOX + ")";
274
275    /**
276     * Let's only generate these SQL strings once, as they are used frequently
277     * Note that this isn't relevant for table creation strings, since they are used only once
278     */
279    private static final String UPDATED_MESSAGE_INSERT = "insert or ignore into " +
280        Message.UPDATED_TABLE_NAME + " select * from " + Message.TABLE_NAME + " where " +
281        EmailContent.RECORD_ID + '=';
282
283    private static final String UPDATED_MESSAGE_DELETE = "delete from " +
284        Message.UPDATED_TABLE_NAME + " where " + EmailContent.RECORD_ID + '=';
285
286    private static final String DELETED_MESSAGE_INSERT = "insert or replace into " +
287        Message.DELETED_TABLE_NAME + " select * from " + Message.TABLE_NAME + " where " +
288        EmailContent.RECORD_ID + '=';
289
290    private static final String DELETE_ORPHAN_BODIES = "delete from " + Body.TABLE_NAME +
291        " where " + BodyColumns.MESSAGE_KEY + " in " + "(select " + BodyColumns.MESSAGE_KEY +
292        " from " + Body.TABLE_NAME + " except select " + EmailContent.RECORD_ID + " from " +
293        Message.TABLE_NAME + ')';
294
295    private static final String DELETE_BODY = "delete from " + Body.TABLE_NAME +
296        " where " + BodyColumns.MESSAGE_KEY + '=';
297
298    private static final String ID_EQUALS = EmailContent.RECORD_ID + "=?";
299
300    private static final ContentValues CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT;
301    private static final ContentValues EMPTY_CONTENT_VALUES = new ContentValues();
302
303    public static final String MESSAGE_URI_PARAMETER_MAILBOX_ID = "mailboxId";
304
305    // For undo handling
306    private int mLastSequence = -1;
307    private ArrayList<ContentProviderOperation> mLastSequenceOps =
308            new ArrayList<ContentProviderOperation>();
309
310    // Query parameter indicating the command came from UIProvider
311    private static final String IS_UIPROVIDER = "is_uiprovider";
312
313    static {
314        // Email URI matching table
315        UriMatcher matcher = sURIMatcher;
316
317        // All accounts
318        matcher.addURI(EmailContent.AUTHORITY, "account", ACCOUNT);
319        // A specific account
320        // insert into this URI causes a mailbox to be added to the account
321        matcher.addURI(EmailContent.AUTHORITY, "account/#", ACCOUNT_ID);
322        matcher.addURI(EmailContent.AUTHORITY, "account/default", ACCOUNT_DEFAULT_ID);
323
324        // Special URI to reset the new message count.  Only update works, and content values
325        // will be ignored.
326        matcher.addURI(EmailContent.AUTHORITY, "resetNewMessageCount",
327                ACCOUNT_RESET_NEW_COUNT);
328        matcher.addURI(EmailContent.AUTHORITY, "resetNewMessageCount/#",
329                ACCOUNT_RESET_NEW_COUNT_ID);
330
331        // All mailboxes
332        matcher.addURI(EmailContent.AUTHORITY, "mailbox", MAILBOX);
333        // A specific mailbox
334        // insert into this URI causes a message to be added to the mailbox
335        // ** NOTE For now, the accountKey must be set manually in the values!
336        matcher.addURI(EmailContent.AUTHORITY, "mailbox/#", MAILBOX_ID);
337        matcher.addURI(EmailContent.AUTHORITY, "mailboxIdFromAccountAndType/#/#",
338                MAILBOX_ID_FROM_ACCOUNT_AND_TYPE);
339        matcher.addURI(EmailContent.AUTHORITY, "mailboxNotification/#", MAILBOX_NOTIFICATION);
340        matcher.addURI(EmailContent.AUTHORITY, "mailboxMostRecentMessage/#",
341                MAILBOX_MOST_RECENT_MESSAGE);
342
343        // All messages
344        matcher.addURI(EmailContent.AUTHORITY, "message", MESSAGE);
345        // A specific message
346        // insert into this URI causes an attachment to be added to the message
347        matcher.addURI(EmailContent.AUTHORITY, "message/#", MESSAGE_ID);
348
349        // A specific attachment
350        matcher.addURI(EmailContent.AUTHORITY, "attachment", ATTACHMENT);
351        // A specific attachment (the header information)
352        matcher.addURI(EmailContent.AUTHORITY, "attachment/#", ATTACHMENT_ID);
353        // The attachments of a specific message (query only) (insert & delete TBD)
354        matcher.addURI(EmailContent.AUTHORITY, "attachment/message/#",
355                ATTACHMENTS_MESSAGE_ID);
356
357        // All mail bodies
358        matcher.addURI(EmailContent.AUTHORITY, "body", BODY);
359        // A specific mail body
360        matcher.addURI(EmailContent.AUTHORITY, "body/#", BODY_ID);
361
362        // All hostauth records
363        matcher.addURI(EmailContent.AUTHORITY, "hostauth", HOSTAUTH);
364        // A specific hostauth
365        matcher.addURI(EmailContent.AUTHORITY, "hostauth/#", HOSTAUTH_ID);
366
367        // Atomically a constant value to a particular field of a mailbox/account
368        matcher.addURI(EmailContent.AUTHORITY, "mailboxIdAddToField/#",
369                MAILBOX_ID_ADD_TO_FIELD);
370        matcher.addURI(EmailContent.AUTHORITY, "accountIdAddToField/#",
371                ACCOUNT_ID_ADD_TO_FIELD);
372
373        /**
374         * THIS URI HAS SPECIAL SEMANTICS
375         * ITS USE IS INTENDED FOR THE UI APPLICATION TO MARK CHANGES THAT NEED TO BE SYNCED BACK
376         * TO A SERVER VIA A SYNC ADAPTER
377         */
378        matcher.addURI(EmailContent.AUTHORITY, "syncedMessage/#", SYNCED_MESSAGE_ID);
379
380        /**
381         * THE URIs BELOW THIS POINT ARE INTENDED TO BE USED BY SYNC ADAPTERS ONLY
382         * THEY REFER TO DATA CREATED AND MAINTAINED BY CALLS TO THE SYNCED_MESSAGE_ID URI
383         * BY THE UI APPLICATION
384         */
385        // All deleted messages
386        matcher.addURI(EmailContent.AUTHORITY, "deletedMessage", DELETED_MESSAGE);
387        // A specific deleted message
388        matcher.addURI(EmailContent.AUTHORITY, "deletedMessage/#", DELETED_MESSAGE_ID);
389
390        // All updated messages
391        matcher.addURI(EmailContent.AUTHORITY, "updatedMessage", UPDATED_MESSAGE);
392        // A specific updated message
393        matcher.addURI(EmailContent.AUTHORITY, "updatedMessage/#", UPDATED_MESSAGE_ID);
394
395        CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT = new ContentValues();
396        CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT.put(Account.NEW_MESSAGE_COUNT, 0);
397
398        matcher.addURI(EmailContent.AUTHORITY, "policy", POLICY);
399        matcher.addURI(EmailContent.AUTHORITY, "policy/#", POLICY_ID);
400
401        // All quick responses
402        matcher.addURI(EmailContent.AUTHORITY, "quickresponse", QUICK_RESPONSE);
403        // A specific quick response
404        matcher.addURI(EmailContent.AUTHORITY, "quickresponse/#", QUICK_RESPONSE_ID);
405        // All quick responses associated with a particular account id
406        matcher.addURI(EmailContent.AUTHORITY, "quickresponse/account/#",
407                QUICK_RESPONSE_ACCOUNT_ID);
408
409        matcher.addURI(EmailContent.AUTHORITY, "uifolders/#", UI_FOLDERS);
410        matcher.addURI(EmailContent.AUTHORITY, "uisubfolders/#", UI_SUBFOLDERS);
411        matcher.addURI(EmailContent.AUTHORITY, "uimessages/#", UI_MESSAGES);
412        matcher.addURI(EmailContent.AUTHORITY, "uimessage/#", UI_MESSAGE);
413        matcher.addURI(EmailContent.AUTHORITY, "uisendmail/#", UI_SENDMAIL);
414        matcher.addURI(EmailContent.AUTHORITY, "uiundo/#", UI_UNDO);
415        matcher.addURI(EmailContent.AUTHORITY, "uisavedraft/#", UI_SAVEDRAFT);
416        matcher.addURI(EmailContent.AUTHORITY, "uiupdatedraft/#", UI_UPDATEDRAFT);
417        matcher.addURI(EmailContent.AUTHORITY, "uisenddraft/#", UI_SENDDRAFT);
418        matcher.addURI(EmailContent.AUTHORITY, "uirefresh/#", UI_FOLDER_REFRESH);
419        matcher.addURI(EmailContent.AUTHORITY, "uifolder/#", UI_FOLDER);
420        matcher.addURI(EmailContent.AUTHORITY, "uiaccount/#", UI_ACCOUNT);
421        matcher.addURI(EmailContent.AUTHORITY, "uiaccts", UI_ACCTS);
422        matcher.addURI(EmailContent.AUTHORITY, "uisettings/#", UI_SETTINGS);
423        matcher.addURI(EmailContent.AUTHORITY, "uiattachments/#", UI_ATTACHMENTS);
424        matcher.addURI(EmailContent.AUTHORITY, "uiattachment/#", UI_ATTACHMENT);
425    }
426
427    /**
428     * Wrap the UriMatcher call so we can throw a runtime exception if an unknown Uri is passed in
429     * @param uri the Uri to match
430     * @return the match value
431     */
432    private static int findMatch(Uri uri, String methodName) {
433        int match = sURIMatcher.match(uri);
434        if (match < 0) {
435            throw new IllegalArgumentException("Unknown uri: " + uri);
436        } else if (Logging.LOGD) {
437            Log.v(TAG, methodName + ": uri=" + uri + ", match is " + match);
438        }
439        return match;
440    }
441
442    private SQLiteDatabase mDatabase;
443    private SQLiteDatabase mBodyDatabase;
444
445    /**
446     * Orphan record deletion utility.  Generates a sqlite statement like:
447     *  delete from <table> where <column> not in (select <foreignColumn> from <foreignTable>)
448     * @param db the EmailProvider database
449     * @param table the table whose orphans are to be removed
450     * @param column the column deletion will be based on
451     * @param foreignColumn the column in the foreign table whose absence will trigger the deletion
452     * @param foreignTable the foreign table
453     */
454    @VisibleForTesting
455    void deleteUnlinked(SQLiteDatabase db, String table, String column, String foreignColumn,
456            String foreignTable) {
457        int count = db.delete(table, column + " not in (select " + foreignColumn + " from " +
458                foreignTable + ")", null);
459        if (count > 0) {
460            Log.w(TAG, "Found " + count + " orphaned row(s) in " + table);
461        }
462    }
463
464    @VisibleForTesting
465    synchronized SQLiteDatabase getDatabase(Context context) {
466        // Always return the cached database, if we've got one
467        if (mDatabase != null) {
468            return mDatabase;
469        }
470
471        // Whenever we create or re-cache the databases, make sure that we haven't lost one
472        // to corruption
473        checkDatabases();
474
475        DBHelper.DatabaseHelper helper = new DBHelper.DatabaseHelper(context, DATABASE_NAME);
476        mDatabase = helper.getWritableDatabase();
477        DBHelper.BodyDatabaseHelper bodyHelper =
478                new DBHelper.BodyDatabaseHelper(context, BODY_DATABASE_NAME);
479        mBodyDatabase = bodyHelper.getWritableDatabase();
480        if (mBodyDatabase != null) {
481            String bodyFileName = mBodyDatabase.getPath();
482            mDatabase.execSQL("attach \"" + bodyFileName + "\" as BodyDatabase");
483        }
484
485        // Restore accounts if the database is corrupted...
486        restoreIfNeeded(context, mDatabase);
487
488        if (Email.DEBUG) {
489            Log.d(TAG, "Deleting orphans...");
490        }
491        // Check for any orphaned Messages in the updated/deleted tables
492        deleteMessageOrphans(mDatabase, Message.UPDATED_TABLE_NAME);
493        deleteMessageOrphans(mDatabase, Message.DELETED_TABLE_NAME);
494        // Delete orphaned mailboxes/messages/policies (account no longer exists)
495        deleteUnlinked(mDatabase, Mailbox.TABLE_NAME, MailboxColumns.ACCOUNT_KEY, AccountColumns.ID,
496                Account.TABLE_NAME);
497        deleteUnlinked(mDatabase, Message.TABLE_NAME, MessageColumns.ACCOUNT_KEY, AccountColumns.ID,
498                Account.TABLE_NAME);
499        deleteUnlinked(mDatabase, Policy.TABLE_NAME, PolicyColumns.ID, AccountColumns.POLICY_KEY,
500                Account.TABLE_NAME);
501
502        if (Email.DEBUG) {
503            Log.d(TAG, "EmailProvider pre-caching...");
504        }
505        preCacheData();
506        if (Email.DEBUG) {
507            Log.d(TAG, "EmailProvider ready.");
508        }
509        return mDatabase;
510    }
511
512    /**
513     * Pre-cache all of the items in a given table meeting the selection criteria
514     * @param tableUri the table uri
515     * @param baseProjection the base projection of that table
516     * @param selection the selection criteria
517     */
518    private void preCacheTable(Uri tableUri, String[] baseProjection, String selection) {
519        Cursor c = query(tableUri, EmailContent.ID_PROJECTION, selection, null, null);
520        try {
521            while (c.moveToNext()) {
522                long id = c.getLong(EmailContent.ID_PROJECTION_COLUMN);
523                Cursor cachedCursor = query(ContentUris.withAppendedId(
524                        tableUri, id), baseProjection, null, null, null);
525                if (cachedCursor != null) {
526                    // For accounts, create a mailbox type map entry (if necessary)
527                    if (tableUri == Account.CONTENT_URI) {
528                        getOrCreateAccountMailboxTypeMap(id);
529                    }
530                    cachedCursor.close();
531                }
532            }
533        } finally {
534            c.close();
535        }
536    }
537
538    private final HashMap<Long, HashMap<Integer, Long>> mMailboxTypeMap =
539        new HashMap<Long, HashMap<Integer, Long>>();
540
541    private HashMap<Integer, Long> getOrCreateAccountMailboxTypeMap(long accountId) {
542        synchronized(mMailboxTypeMap) {
543            HashMap<Integer, Long> accountMailboxTypeMap = mMailboxTypeMap.get(accountId);
544            if (accountMailboxTypeMap == null) {
545                accountMailboxTypeMap = new HashMap<Integer, Long>();
546                mMailboxTypeMap.put(accountId, accountMailboxTypeMap);
547            }
548            return accountMailboxTypeMap;
549        }
550    }
551
552    private void addToMailboxTypeMap(Cursor c) {
553        long accountId = c.getLong(Mailbox.CONTENT_ACCOUNT_KEY_COLUMN);
554        int type = c.getInt(Mailbox.CONTENT_TYPE_COLUMN);
555        synchronized(mMailboxTypeMap) {
556            HashMap<Integer, Long> accountMailboxTypeMap =
557                getOrCreateAccountMailboxTypeMap(accountId);
558            accountMailboxTypeMap.put(type, c.getLong(Mailbox.CONTENT_ID_COLUMN));
559        }
560    }
561
562    private long getMailboxIdFromMailboxTypeMap(long accountId, int type) {
563        synchronized(mMailboxTypeMap) {
564            HashMap<Integer, Long> accountMap = mMailboxTypeMap.get(accountId);
565            Long mailboxId = null;
566            if (accountMap != null) {
567                mailboxId = accountMap.get(type);
568            }
569            if (mailboxId == null) return Mailbox.NO_MAILBOX;
570            return mailboxId;
571        }
572    }
573
574    private void preCacheData() {
575        synchronized(mMailboxTypeMap) {
576            mMailboxTypeMap.clear();
577
578            // Pre-cache accounts, host auth's, policies, and special mailboxes
579            preCacheTable(Account.CONTENT_URI, Account.CONTENT_PROJECTION, null);
580            preCacheTable(HostAuth.CONTENT_URI, HostAuth.CONTENT_PROJECTION, null);
581            preCacheTable(Policy.CONTENT_URI, Policy.CONTENT_PROJECTION, null);
582            preCacheTable(Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION,
583                    MAILBOX_PRE_CACHE_SELECTION);
584
585            // Create a map from account,type to a mailbox
586            Map<String, Cursor> snapshot = mCacheMailbox.getSnapshot();
587            Collection<Cursor> values = snapshot.values();
588            if (values != null) {
589                for (Cursor c: values) {
590                    if (c.moveToFirst()) {
591                        addToMailboxTypeMap(c);
592                    }
593                }
594            }
595        }
596    }
597
598    /*package*/ static SQLiteDatabase getReadableDatabase(Context context) {
599        DBHelper.DatabaseHelper helper = new DBHelper.DatabaseHelper(context, DATABASE_NAME);
600        return helper.getReadableDatabase();
601    }
602
603    /**
604     * Restore user Account and HostAuth data from our backup database
605     */
606    public static void restoreIfNeeded(Context context, SQLiteDatabase mainDatabase) {
607        if (Email.DEBUG) {
608            Log.w(TAG, "restoreIfNeeded...");
609        }
610        // Check for legacy backup
611        String legacyBackup = Preferences.getLegacyBackupPreference(context);
612        // If there's a legacy backup, create a new-style backup and delete the legacy backup
613        // In the 1:1000000000 chance that the user gets an app update just as his database becomes
614        // corrupt, oh well...
615        if (!TextUtils.isEmpty(legacyBackup)) {
616            backupAccounts(context, mainDatabase);
617            Preferences.clearLegacyBackupPreference(context);
618            Log.w(TAG, "Created new EmailProvider backup database");
619            return;
620        }
621
622        // If we have accounts, we're done
623        Cursor c = mainDatabase.query(Account.TABLE_NAME, EmailContent.ID_PROJECTION, null, null,
624                null, null, null);
625        if (c.moveToFirst()) {
626            if (Email.DEBUG) {
627                Log.w(TAG, "restoreIfNeeded: Account exists.");
628            }
629            return; // At least one account exists.
630        }
631        restoreAccounts(context, mainDatabase);
632    }
633
634    /** {@inheritDoc} */
635    @Override
636    public void shutdown() {
637        if (mDatabase != null) {
638            mDatabase.close();
639            mDatabase = null;
640        }
641        if (mBodyDatabase != null) {
642            mBodyDatabase.close();
643            mBodyDatabase = null;
644        }
645    }
646
647    /*package*/ static void deleteMessageOrphans(SQLiteDatabase database, String tableName) {
648        if (database != null) {
649            // We'll look at all of the items in the table; there won't be many typically
650            Cursor c = database.query(tableName, ORPHANS_PROJECTION, null, null, null, null, null);
651            // Usually, there will be nothing in these tables, so make a quick check
652            try {
653                if (c.getCount() == 0) return;
654                ArrayList<Long> foundMailboxes = new ArrayList<Long>();
655                ArrayList<Long> notFoundMailboxes = new ArrayList<Long>();
656                ArrayList<Long> deleteList = new ArrayList<Long>();
657                String[] bindArray = new String[1];
658                while (c.moveToNext()) {
659                    // Get the mailbox key and see if we've already found this mailbox
660                    // If so, we're fine
661                    long mailboxId = c.getLong(ORPHANS_MAILBOX_KEY);
662                    // If we already know this mailbox doesn't exist, mark the message for deletion
663                    if (notFoundMailboxes.contains(mailboxId)) {
664                        deleteList.add(c.getLong(ORPHANS_ID));
665                    // If we don't know about this mailbox, we'll try to find it
666                    } else if (!foundMailboxes.contains(mailboxId)) {
667                        bindArray[0] = Long.toString(mailboxId);
668                        Cursor boxCursor = database.query(Mailbox.TABLE_NAME,
669                                Mailbox.ID_PROJECTION, WHERE_ID, bindArray, null, null, null);
670                        try {
671                            // If it exists, we'll add it to the "found" mailboxes
672                            if (boxCursor.moveToFirst()) {
673                                foundMailboxes.add(mailboxId);
674                            // Otherwise, we'll add to "not found" and mark the message for deletion
675                            } else {
676                                notFoundMailboxes.add(mailboxId);
677                                deleteList.add(c.getLong(ORPHANS_ID));
678                            }
679                        } finally {
680                            boxCursor.close();
681                        }
682                    }
683                }
684                // Now, delete the orphan messages
685                for (long messageId: deleteList) {
686                    bindArray[0] = Long.toString(messageId);
687                    database.delete(tableName, WHERE_ID, bindArray);
688                }
689            } finally {
690                c.close();
691            }
692        }
693    }
694
695    @Override
696    public int delete(Uri uri, String selection, String[] selectionArgs) {
697        final int match = findMatch(uri, "delete");
698        Context context = getContext();
699        // Pick the correct database for this operation
700        // If we're in a transaction already (which would happen during applyBatch), then the
701        // body database is already attached to the email database and any attempt to use the
702        // body database directly will result in a SQLiteException (the database is locked)
703        SQLiteDatabase db = getDatabase(context);
704        int table = match >> BASE_SHIFT;
705        String id = "0";
706        boolean messageDeletion = false;
707        ContentResolver resolver = context.getContentResolver();
708
709        ContentCache cache = mContentCaches[table];
710        String tableName = TABLE_NAMES[table];
711        int result = -1;
712
713        try {
714            if (match == MESSAGE_ID || match == SYNCED_MESSAGE_ID) {
715                if (!uri.getBooleanQueryParameter(IS_UIPROVIDER, false)) {
716                    notifyUIConversation(uri);
717                }
718            }
719            switch (match) {
720                case UI_MESSAGE:
721                    return uiDeleteMessage(uri);
722                // These are cases in which one or more Messages might get deleted, either by
723                // cascade or explicitly
724                case MAILBOX_ID:
725                case MAILBOX:
726                case ACCOUNT_ID:
727                case ACCOUNT:
728                case MESSAGE:
729                case SYNCED_MESSAGE_ID:
730                case MESSAGE_ID:
731                    // Handle lost Body records here, since this cannot be done in a trigger
732                    // The process is:
733                    //  1) Begin a transaction, ensuring that both databases are affected atomically
734                    //  2) Do the requested deletion, with cascading deletions handled in triggers
735                    //  3) End the transaction, committing all changes atomically
736                    //
737                    // Bodies are auto-deleted here;  Attachments are auto-deleted via trigger
738                    messageDeletion = true;
739                    db.beginTransaction();
740                    break;
741            }
742            switch (match) {
743                case BODY_ID:
744                case DELETED_MESSAGE_ID:
745                case SYNCED_MESSAGE_ID:
746                case MESSAGE_ID:
747                case UPDATED_MESSAGE_ID:
748                case ATTACHMENT_ID:
749                case MAILBOX_ID:
750                case ACCOUNT_ID:
751                case HOSTAUTH_ID:
752                case POLICY_ID:
753                case QUICK_RESPONSE_ID:
754                    id = uri.getPathSegments().get(1);
755                    if (match == SYNCED_MESSAGE_ID) {
756                        // For synced messages, first copy the old message to the deleted table and
757                        // delete it from the updated table (in case it was updated first)
758                        // Note that this is all within a transaction, for atomicity
759                        db.execSQL(DELETED_MESSAGE_INSERT + id);
760                        db.execSQL(UPDATED_MESSAGE_DELETE + id);
761                    }
762                    if (cache != null) {
763                        cache.lock(id);
764                    }
765                    try {
766                        result = db.delete(tableName, whereWithId(id, selection), selectionArgs);
767                        if (cache != null) {
768                            switch(match) {
769                                case ACCOUNT_ID:
770                                    // Account deletion will clear all of the caches, as HostAuth's,
771                                    // Mailboxes, and Messages will be deleted in the process
772                                    mCacheMailbox.invalidate("Delete", uri, selection);
773                                    mCacheHostAuth.invalidate("Delete", uri, selection);
774                                    mCachePolicy.invalidate("Delete", uri, selection);
775                                    //$FALL-THROUGH$
776                                case MAILBOX_ID:
777                                    // Mailbox deletion will clear the Message cache
778                                    mCacheMessage.invalidate("Delete", uri, selection);
779                                    //$FALL-THROUGH$
780                                case SYNCED_MESSAGE_ID:
781                                case MESSAGE_ID:
782                                case HOSTAUTH_ID:
783                                case POLICY_ID:
784                                    cache.invalidate("Delete", uri, selection);
785                                    // Make sure all data is properly cached
786                                    if (match != MESSAGE_ID) {
787                                        preCacheData();
788                                    }
789                                    break;
790                            }
791                        }
792                    } finally {
793                        if (cache != null) {
794                            cache.unlock(id);
795                        }
796                    }
797                    break;
798                case ATTACHMENTS_MESSAGE_ID:
799                    // All attachments for the given message
800                    id = uri.getPathSegments().get(2);
801                    result = db.delete(tableName,
802                            whereWith(Attachment.MESSAGE_KEY + "=" + id, selection), selectionArgs);
803                    break;
804
805                case BODY:
806                case MESSAGE:
807                case DELETED_MESSAGE:
808                case UPDATED_MESSAGE:
809                case ATTACHMENT:
810                case MAILBOX:
811                case ACCOUNT:
812                case HOSTAUTH:
813                case POLICY:
814                    switch(match) {
815                        // See the comments above for deletion of ACCOUNT_ID, etc
816                        case ACCOUNT:
817                            mCacheMailbox.invalidate("Delete", uri, selection);
818                            mCacheHostAuth.invalidate("Delete", uri, selection);
819                            mCachePolicy.invalidate("Delete", uri, selection);
820                            //$FALL-THROUGH$
821                        case MAILBOX:
822                            mCacheMessage.invalidate("Delete", uri, selection);
823                            //$FALL-THROUGH$
824                        case MESSAGE:
825                        case HOSTAUTH:
826                        case POLICY:
827                            cache.invalidate("Delete", uri, selection);
828                            break;
829                    }
830                    result = db.delete(tableName, selection, selectionArgs);
831                    switch(match) {
832                        case ACCOUNT:
833                        case MAILBOX:
834                        case HOSTAUTH:
835                        case POLICY:
836                            // Make sure all data is properly cached
837                            preCacheData();
838                            break;
839                    }
840                    break;
841
842                default:
843                    throw new IllegalArgumentException("Unknown URI " + uri);
844            }
845            if (messageDeletion) {
846                if (match == MESSAGE_ID) {
847                    // Delete the Body record associated with the deleted message
848                    db.execSQL(DELETE_BODY + id);
849                } else {
850                    // Delete any orphaned Body records
851                    db.execSQL(DELETE_ORPHAN_BODIES);
852                }
853                db.setTransactionSuccessful();
854            }
855        } catch (SQLiteException e) {
856            checkDatabases();
857            throw e;
858        } finally {
859            if (messageDeletion) {
860                db.endTransaction();
861            }
862        }
863
864        // Notify all notifier cursors
865        sendNotifierChange(getBaseNotificationUri(match), NOTIFICATION_OP_DELETE, id);
866
867        // Notify all email content cursors
868        resolver.notifyChange(EmailContent.CONTENT_URI, null);
869        return result;
870    }
871
872    @Override
873    // Use the email- prefix because message, mailbox, and account are so generic (e.g. SMS, IM)
874    public String getType(Uri uri) {
875        int match = findMatch(uri, "getType");
876        switch (match) {
877            case BODY_ID:
878                return "vnd.android.cursor.item/email-body";
879            case BODY:
880                return "vnd.android.cursor.dir/email-body";
881            case UPDATED_MESSAGE_ID:
882            case MESSAGE_ID:
883                // NOTE: According to the framework folks, we're supposed to invent mime types as
884                // a way of passing information to drag & drop recipients.
885                // If there's a mailboxId parameter in the url, we respond with a mime type that
886                // has -n appended, where n is the mailboxId of the message.  The drag & drop code
887                // uses this information to know not to allow dragging the item to its own mailbox
888                String mimeType = EMAIL_MESSAGE_MIME_TYPE;
889                String mailboxId = uri.getQueryParameter(MESSAGE_URI_PARAMETER_MAILBOX_ID);
890                if (mailboxId != null) {
891                    mimeType += "-" + mailboxId;
892                }
893                return mimeType;
894            case UPDATED_MESSAGE:
895            case MESSAGE:
896                return "vnd.android.cursor.dir/email-message";
897            case MAILBOX:
898                return "vnd.android.cursor.dir/email-mailbox";
899            case MAILBOX_ID:
900                return "vnd.android.cursor.item/email-mailbox";
901            case ACCOUNT:
902                return "vnd.android.cursor.dir/email-account";
903            case ACCOUNT_ID:
904                return "vnd.android.cursor.item/email-account";
905            case ATTACHMENTS_MESSAGE_ID:
906            case ATTACHMENT:
907                return "vnd.android.cursor.dir/email-attachment";
908            case ATTACHMENT_ID:
909                return EMAIL_ATTACHMENT_MIME_TYPE;
910            case HOSTAUTH:
911                return "vnd.android.cursor.dir/email-hostauth";
912            case HOSTAUTH_ID:
913                return "vnd.android.cursor.item/email-hostauth";
914            default:
915                throw new IllegalArgumentException("Unknown URI " + uri);
916        }
917    }
918
919    private static final Uri UIPROVIDER_CONVERSATION_NOTIFIER =
920            Uri.parse("content://" + UIProvider.AUTHORITY + "/uimessages");
921    private static final Uri UIPROVIDER_MAILBOX_NOTIFIER =
922            Uri.parse("content://" + UIProvider.AUTHORITY + "/uifolder");
923    private static final Uri UIPROVIDER_ACCOUNT_NOTIFIER =
924            Uri.parse("content://" + UIProvider.AUTHORITY + "/uiaccount");
925    private static final Uri UIPROVIDER_SETTINGS_NOTIFIER =
926            Uri.parse("content://" + UIProvider.AUTHORITY + "/uisettings");
927    private static final Uri UIPROVIDER_ATTACHMENT_NOTIFIER =
928            Uri.parse("content://" + UIProvider.AUTHORITY + "/uiattachment");
929
930    @Override
931    public Uri insert(Uri uri, ContentValues values) {
932        int match = findMatch(uri, "insert");
933        Context context = getContext();
934        ContentResolver resolver = context.getContentResolver();
935
936        // See the comment at delete(), above
937        SQLiteDatabase db = getDatabase(context);
938        int table = match >> BASE_SHIFT;
939        String id = "0";
940        long longId;
941
942        // We do NOT allow setting of unreadCount/messageCount via the provider
943        // These columns are maintained via triggers
944        if (match == MAILBOX_ID || match == MAILBOX) {
945            values.put(MailboxColumns.UNREAD_COUNT, 0);
946            values.put(MailboxColumns.MESSAGE_COUNT, 0);
947        }
948
949        Uri resultUri = null;
950
951        try {
952            switch (match) {
953                case UI_SAVEDRAFT:
954                    return uiSaveDraft(uri, values);
955                case UI_SENDMAIL:
956                    return uiSendMail(uri, values);
957                // NOTE: It is NOT legal for production code to insert directly into UPDATED_MESSAGE
958                // or DELETED_MESSAGE; see the comment below for details
959                case UPDATED_MESSAGE:
960                case DELETED_MESSAGE:
961                case MESSAGE:
962                case BODY:
963                case ATTACHMENT:
964                case MAILBOX:
965                case ACCOUNT:
966                case HOSTAUTH:
967                case POLICY:
968                case QUICK_RESPONSE:
969                    longId = db.insert(TABLE_NAMES[table], "foo", values);
970                    resultUri = ContentUris.withAppendedId(uri, longId);
971                    switch(match) {
972                        case MESSAGE:
973                            if (!uri.getBooleanQueryParameter(IS_UIPROVIDER, false)) {
974                                notifyUIConversationMailbox(values.getAsLong(Message.MAILBOX_KEY));
975                            }
976                            break;
977                        case MAILBOX:
978                            if (values.containsKey(MailboxColumns.TYPE)) {
979                                // Only cache special mailbox types
980                                int type = values.getAsInteger(MailboxColumns.TYPE);
981                                if (type != Mailbox.TYPE_INBOX && type != Mailbox.TYPE_OUTBOX &&
982                                        type != Mailbox.TYPE_DRAFTS && type != Mailbox.TYPE_SENT &&
983                                        type != Mailbox.TYPE_TRASH && type != Mailbox.TYPE_SEARCH) {
984                                    break;
985                                }
986                            }
987                            //$FALL-THROUGH$
988                        case ACCOUNT:
989                        case HOSTAUTH:
990                        case POLICY:
991                            // Cache new account, host auth, policy, and some mailbox rows
992                            Cursor c = query(resultUri, CACHE_PROJECTIONS[table], null, null, null);
993                            if (c != null) {
994                                if (match == MAILBOX) {
995                                    addToMailboxTypeMap(c);
996                                } else if (match == ACCOUNT) {
997                                    getOrCreateAccountMailboxTypeMap(longId);
998                                }
999                                c.close();
1000                            }
1001                            break;
1002                    }
1003                    // Clients shouldn't normally be adding rows to these tables, as they are
1004                    // maintained by triggers.  However, we need to be able to do this for unit
1005                    // testing, so we allow the insert and then throw the same exception that we
1006                    // would if this weren't allowed.
1007                    if (match == UPDATED_MESSAGE || match == DELETED_MESSAGE) {
1008                        throw new IllegalArgumentException("Unknown URL " + uri);
1009                    }
1010                    if (match == ATTACHMENT) {
1011                        int flags = 0;
1012                        if (values.containsKey(Attachment.FLAGS)) {
1013                            flags = values.getAsInteger(Attachment.FLAGS);
1014                        }
1015                        // Report all new attachments to the download service
1016                        mAttachmentService.attachmentChanged(getContext(), longId, flags);
1017                    }
1018                    break;
1019                case MAILBOX_ID:
1020                    // This implies adding a message to a mailbox
1021                    // Hmm, a problem here is that we can't link the account as well, so it must be
1022                    // already in the values...
1023                    longId = Long.parseLong(uri.getPathSegments().get(1));
1024                    values.put(MessageColumns.MAILBOX_KEY, longId);
1025                    return insert(Message.CONTENT_URI, values); // Recurse
1026                case MESSAGE_ID:
1027                    // This implies adding an attachment to a message.
1028                    id = uri.getPathSegments().get(1);
1029                    longId = Long.parseLong(id);
1030                    values.put(AttachmentColumns.MESSAGE_KEY, longId);
1031                    return insert(Attachment.CONTENT_URI, values); // Recurse
1032                case ACCOUNT_ID:
1033                    // This implies adding a mailbox to an account.
1034                    longId = Long.parseLong(uri.getPathSegments().get(1));
1035                    values.put(MailboxColumns.ACCOUNT_KEY, longId);
1036                    return insert(Mailbox.CONTENT_URI, values); // Recurse
1037                case ATTACHMENTS_MESSAGE_ID:
1038                    longId = db.insert(TABLE_NAMES[table], "foo", values);
1039                    resultUri = ContentUris.withAppendedId(Attachment.CONTENT_URI, longId);
1040                    break;
1041                default:
1042                    throw new IllegalArgumentException("Unknown URL " + uri);
1043            }
1044        } catch (SQLiteException e) {
1045            checkDatabases();
1046            throw e;
1047        }
1048
1049        // Notify all notifier cursors
1050        sendNotifierChange(getBaseNotificationUri(match), NOTIFICATION_OP_INSERT, id);
1051
1052        // Notify all existing cursors.
1053        resolver.notifyChange(EmailContent.CONTENT_URI, null);
1054        return resultUri;
1055    }
1056
1057    @Override
1058    public boolean onCreate() {
1059        checkDatabases();
1060        return false;
1061    }
1062
1063    /**
1064     * The idea here is that the two databases (EmailProvider.db and EmailProviderBody.db must
1065     * always be in sync (i.e. there are two database or NO databases).  This code will delete
1066     * any "orphan" database, so that both will be created together.  Note that an "orphan" database
1067     * will exist after either of the individual databases is deleted due to data corruption.
1068     */
1069    public void checkDatabases() {
1070        // Uncache the databases
1071        if (mDatabase != null) {
1072            mDatabase = null;
1073        }
1074        if (mBodyDatabase != null) {
1075            mBodyDatabase = null;
1076        }
1077        // Look for orphans, and delete as necessary; these must always be in sync
1078        File databaseFile = getContext().getDatabasePath(DATABASE_NAME);
1079        File bodyFile = getContext().getDatabasePath(BODY_DATABASE_NAME);
1080
1081        // TODO Make sure attachments are deleted
1082        if (databaseFile.exists() && !bodyFile.exists()) {
1083            Log.w(TAG, "Deleting orphaned EmailProvider database...");
1084            databaseFile.delete();
1085        } else if (bodyFile.exists() && !databaseFile.exists()) {
1086            Log.w(TAG, "Deleting orphaned EmailProviderBody database...");
1087            bodyFile.delete();
1088        }
1089    }
1090    @Override
1091    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
1092            String sortOrder) {
1093        long time = 0L;
1094        if (Email.DEBUG) {
1095            time = System.nanoTime();
1096        }
1097        Cursor c = null;
1098        int match;
1099        try {
1100            match = findMatch(uri, "query");
1101        } catch (IllegalArgumentException e) {
1102            String uriString = uri.toString();
1103            // If we were passed an illegal uri, see if it ends in /-1
1104            // if so, and if substituting 0 for -1 results in a valid uri, return an empty cursor
1105            if (uriString != null && uriString.endsWith("/-1")) {
1106                uri = Uri.parse(uriString.substring(0, uriString.length() - 2) + "0");
1107                match = findMatch(uri, "query");
1108                switch (match) {
1109                    case BODY_ID:
1110                    case MESSAGE_ID:
1111                    case DELETED_MESSAGE_ID:
1112                    case UPDATED_MESSAGE_ID:
1113                    case ATTACHMENT_ID:
1114                    case MAILBOX_ID:
1115                    case ACCOUNT_ID:
1116                    case HOSTAUTH_ID:
1117                    case POLICY_ID:
1118                        return new MatrixCursor(projection, 0);
1119                }
1120            }
1121            throw e;
1122        }
1123        Context context = getContext();
1124        // See the comment at delete(), above
1125        SQLiteDatabase db = getDatabase(context);
1126        int table = match >> BASE_SHIFT;
1127        String limit = uri.getQueryParameter(EmailContent.PARAMETER_LIMIT);
1128        String id;
1129
1130        // Find the cache for this query's table (if any)
1131        ContentCache cache = null;
1132        String tableName = TABLE_NAMES[table];
1133        // We can only use the cache if there's no selection
1134        if (selection == null) {
1135            cache = mContentCaches[table];
1136        }
1137        if (cache == null) {
1138            ContentCache.notCacheable(uri, selection);
1139        }
1140
1141        try {
1142            switch (match) {
1143                // First, dispatch queries from UnfiedEmail
1144                case UI_ACCTS:
1145                    c = uiAccounts(projection);
1146                    return c;
1147                case UI_UNDO:
1148                    return uiUndo(uri, projection);
1149                case UI_SUBFOLDERS:
1150                case UI_FOLDERS:
1151                case UI_MESSAGES:
1152                case UI_MESSAGE:
1153                case UI_FOLDER:
1154                case UI_ACCOUNT:
1155                case UI_SETTINGS:
1156                case UI_ATTACHMENT:
1157                case UI_ATTACHMENTS:
1158                    // For now, we don't allow selection criteria within these queries
1159                    if (selection != null || selectionArgs != null) {
1160                        throw new IllegalArgumentException("UI queries can't have selection/args");
1161                    }
1162                    c = uiQuery(match, uri, projection);
1163                    return c;
1164                case UI_FOLDER_REFRESH:
1165                    c = uiFolderRefresh(uri, projection);
1166                    return c;
1167                case MAILBOX_NOTIFICATION:
1168                    c = notificationQuery(uri);
1169                    return c;
1170                case MAILBOX_MOST_RECENT_MESSAGE:
1171                    c = mostRecentMessageQuery(uri);
1172                    return c;
1173                case ACCOUNT_DEFAULT_ID:
1174                    // Start with a snapshot of the cache
1175                    Map<String, Cursor> accountCache = mCacheAccount.getSnapshot();
1176                    long accountId = Account.NO_ACCOUNT;
1177                    // Find the account with "isDefault" set, or the lowest account ID otherwise.
1178                    // Note that the snapshot from the cached isn't guaranteed to be sorted in any
1179                    // way.
1180                    Collection<Cursor> accounts = accountCache.values();
1181                    for (Cursor accountCursor: accounts) {
1182                        // For now, at least, we can have zero count cursors (e.g. if someone looks
1183                        // up a non-existent id); we need to skip these
1184                        if (accountCursor.moveToFirst()) {
1185                            boolean isDefault =
1186                                accountCursor.getInt(Account.CONTENT_IS_DEFAULT_COLUMN) == 1;
1187                            long iterId = accountCursor.getLong(Account.CONTENT_ID_COLUMN);
1188                            // We'll remember this one if it's the default or the first one we see
1189                            if (isDefault) {
1190                                accountId = iterId;
1191                                break;
1192                            } else if ((accountId == Account.NO_ACCOUNT) || (iterId < accountId)) {
1193                                accountId = iterId;
1194                            }
1195                        }
1196                    }
1197                    // Return a cursor with an id projection
1198                    MatrixCursor mc = new MatrixCursor(EmailContent.ID_PROJECTION);
1199                    mc.addRow(new Object[] {accountId});
1200                    c = mc;
1201                    break;
1202                case MAILBOX_ID_FROM_ACCOUNT_AND_TYPE:
1203                    // Get accountId and type and find the mailbox in our map
1204                    List<String> pathSegments = uri.getPathSegments();
1205                    accountId = Long.parseLong(pathSegments.get(1));
1206                    int type = Integer.parseInt(pathSegments.get(2));
1207                    long mailboxId = getMailboxIdFromMailboxTypeMap(accountId, type);
1208                    // Return a cursor with an id projection
1209                    mc = new MatrixCursor(EmailContent.ID_PROJECTION);
1210                    mc.addRow(new Object[] {mailboxId});
1211                    c = mc;
1212                    break;
1213                case BODY:
1214                case MESSAGE:
1215                case UPDATED_MESSAGE:
1216                case DELETED_MESSAGE:
1217                case ATTACHMENT:
1218                case MAILBOX:
1219                case ACCOUNT:
1220                case HOSTAUTH:
1221                case POLICY:
1222                case QUICK_RESPONSE:
1223                    // Special-case "count of accounts"; it's common and we always know it
1224                    if (match == ACCOUNT && Arrays.equals(projection, EmailContent.COUNT_COLUMNS) &&
1225                            selection == null && limit.equals("1")) {
1226                        int accountCount = mMailboxTypeMap.size();
1227                        // In the rare case there are MAX_CACHED_ACCOUNTS or more, we can't do this
1228                        if (accountCount < MAX_CACHED_ACCOUNTS) {
1229                            mc = new MatrixCursor(projection, 1);
1230                            mc.addRow(new Object[] {accountCount});
1231                            c = mc;
1232                            break;
1233                        }
1234                    }
1235                    c = db.query(tableName, projection,
1236                            selection, selectionArgs, null, null, sortOrder, limit);
1237                    break;
1238                case BODY_ID:
1239                case MESSAGE_ID:
1240                case DELETED_MESSAGE_ID:
1241                case UPDATED_MESSAGE_ID:
1242                case ATTACHMENT_ID:
1243                case MAILBOX_ID:
1244                case ACCOUNT_ID:
1245                case HOSTAUTH_ID:
1246                case POLICY_ID:
1247                case QUICK_RESPONSE_ID:
1248                    id = uri.getPathSegments().get(1);
1249                    if (cache != null) {
1250                        c = cache.getCachedCursor(id, projection);
1251                    }
1252                    if (c == null) {
1253                        CacheToken token = null;
1254                        if (cache != null) {
1255                            token = cache.getCacheToken(id);
1256                        }
1257                        c = db.query(tableName, projection, whereWithId(id, selection),
1258                                selectionArgs, null, null, sortOrder, limit);
1259                        if (cache != null) {
1260                            c = cache.putCursor(c, id, projection, token);
1261                        }
1262                    }
1263                    break;
1264                case ATTACHMENTS_MESSAGE_ID:
1265                    // All attachments for the given message
1266                    id = uri.getPathSegments().get(2);
1267                    c = db.query(Attachment.TABLE_NAME, projection,
1268                            whereWith(Attachment.MESSAGE_KEY + "=" + id, selection),
1269                            selectionArgs, null, null, sortOrder, limit);
1270                    break;
1271                case QUICK_RESPONSE_ACCOUNT_ID:
1272                    // All quick responses for the given account
1273                    id = uri.getPathSegments().get(2);
1274                    c = db.query(QuickResponse.TABLE_NAME, projection,
1275                            whereWith(QuickResponse.ACCOUNT_KEY + "=" + id, selection),
1276                            selectionArgs, null, null, sortOrder);
1277                    break;
1278                default:
1279                    throw new IllegalArgumentException("Unknown URI " + uri);
1280            }
1281        } catch (SQLiteException e) {
1282            checkDatabases();
1283            throw e;
1284        } catch (RuntimeException e) {
1285            checkDatabases();
1286            e.printStackTrace();
1287            throw e;
1288        } finally {
1289            if (cache != null && c != null && Email.DEBUG) {
1290                cache.recordQueryTime(c, System.nanoTime() - time);
1291            }
1292            if (c == null) {
1293                // This should never happen, but let's be sure to log it...
1294                Log.e(TAG, "Query returning null for uri: " + uri + ", selection: " + selection);
1295            }
1296        }
1297
1298        if ((c != null) && !isTemporary()) {
1299            c.setNotificationUri(getContext().getContentResolver(), uri);
1300        }
1301        return c;
1302    }
1303
1304    private String whereWithId(String id, String selection) {
1305        StringBuilder sb = new StringBuilder(256);
1306        sb.append("_id=");
1307        sb.append(id);
1308        if (selection != null) {
1309            sb.append(" AND (");
1310            sb.append(selection);
1311            sb.append(')');
1312        }
1313        return sb.toString();
1314    }
1315
1316    /**
1317     * Combine a locally-generated selection with a user-provided selection
1318     *
1319     * This introduces risk that the local selection might insert incorrect chars
1320     * into the SQL, so use caution.
1321     *
1322     * @param where locally-generated selection, must not be null
1323     * @param selection user-provided selection, may be null
1324     * @return a single selection string
1325     */
1326    private String whereWith(String where, String selection) {
1327        if (selection == null) {
1328            return where;
1329        }
1330        StringBuilder sb = new StringBuilder(where);
1331        sb.append(" AND (");
1332        sb.append(selection);
1333        sb.append(')');
1334
1335        return sb.toString();
1336    }
1337
1338    /**
1339     * Restore a HostAuth from a database, given its unique id
1340     * @param db the database
1341     * @param id the unique id (_id) of the row
1342     * @return a fully populated HostAuth or null if the row does not exist
1343     */
1344    private static HostAuth restoreHostAuth(SQLiteDatabase db, long id) {
1345        Cursor c = db.query(HostAuth.TABLE_NAME, HostAuth.CONTENT_PROJECTION,
1346                HostAuth.RECORD_ID + "=?", new String[] {Long.toString(id)}, null, null, null);
1347        try {
1348            if (c.moveToFirst()) {
1349                HostAuth hostAuth = new HostAuth();
1350                hostAuth.restore(c);
1351                return hostAuth;
1352            }
1353            return null;
1354        } finally {
1355            c.close();
1356        }
1357    }
1358
1359    /**
1360     * Copy the Account and HostAuth tables from one database to another
1361     * @param fromDatabase the source database
1362     * @param toDatabase the destination database
1363     * @return the number of accounts copied, or -1 if an error occurred
1364     */
1365    private static int copyAccountTables(SQLiteDatabase fromDatabase, SQLiteDatabase toDatabase) {
1366        if (fromDatabase == null || toDatabase == null) return -1;
1367        int copyCount = 0;
1368        try {
1369            // Lock both databases; for the "from" database, we don't want anyone changing it from
1370            // under us; for the "to" database, we want to make the operation atomic
1371            fromDatabase.beginTransaction();
1372            toDatabase.beginTransaction();
1373            // Delete anything hanging around here
1374            toDatabase.delete(Account.TABLE_NAME, null, null);
1375            toDatabase.delete(HostAuth.TABLE_NAME, null, null);
1376            // Get our account cursor
1377            Cursor c = fromDatabase.query(Account.TABLE_NAME, Account.CONTENT_PROJECTION,
1378                    null, null, null, null, null);
1379            boolean noErrors = true;
1380            try {
1381                // Loop through accounts, copying them and associated host auth's
1382                while (c.moveToNext()) {
1383                    Account account = new Account();
1384                    account.restore(c);
1385
1386                    // Clear security sync key and sync key, as these were specific to the state of
1387                    // the account, and we've reset that...
1388                    // Clear policy key so that we can re-establish policies from the server
1389                    // TODO This is pretty EAS specific, but there's a lot of that around
1390                    account.mSecuritySyncKey = null;
1391                    account.mSyncKey = null;
1392                    account.mPolicyKey = 0;
1393
1394                    // Copy host auth's and update foreign keys
1395                    HostAuth hostAuth = restoreHostAuth(fromDatabase, account.mHostAuthKeyRecv);
1396                    // The account might have gone away, though very unlikely
1397                    if (hostAuth == null) continue;
1398                    account.mHostAuthKeyRecv = toDatabase.insert(HostAuth.TABLE_NAME, null,
1399                            hostAuth.toContentValues());
1400                    // EAS accounts have no send HostAuth
1401                    if (account.mHostAuthKeySend > 0) {
1402                        hostAuth = restoreHostAuth(fromDatabase, account.mHostAuthKeySend);
1403                        // Belt and suspenders; I can't imagine that this is possible, since we
1404                        // checked the validity of the account above, and the database is now locked
1405                        if (hostAuth == null) continue;
1406                        account.mHostAuthKeySend = toDatabase.insert(HostAuth.TABLE_NAME, null,
1407                                hostAuth.toContentValues());
1408                    }
1409                    // Now, create the account in the "to" database
1410                    toDatabase.insert(Account.TABLE_NAME, null, account.toContentValues());
1411                    copyCount++;
1412                }
1413            } catch (SQLiteException e) {
1414                noErrors = false;
1415                copyCount = -1;
1416            } finally {
1417                fromDatabase.endTransaction();
1418                if (noErrors) {
1419                    // Say it's ok to commit
1420                    toDatabase.setTransactionSuccessful();
1421                }
1422                toDatabase.endTransaction();
1423                c.close();
1424            }
1425        } catch (SQLiteException e) {
1426            copyCount = -1;
1427        }
1428        return copyCount;
1429    }
1430
1431    private static SQLiteDatabase getBackupDatabase(Context context) {
1432        DBHelper.DatabaseHelper helper = new DBHelper.DatabaseHelper(context, BACKUP_DATABASE_NAME);
1433        return helper.getWritableDatabase();
1434    }
1435
1436    /**
1437     * Backup account data, returning the number of accounts backed up
1438     */
1439    private static int backupAccounts(Context context, SQLiteDatabase mainDatabase) {
1440        if (Email.DEBUG) {
1441            Log.d(TAG, "backupAccounts...");
1442        }
1443        SQLiteDatabase backupDatabase = getBackupDatabase(context);
1444        try {
1445            int numBackedUp = copyAccountTables(mainDatabase, backupDatabase);
1446            if (numBackedUp < 0) {
1447                Log.e(TAG, "Account backup failed!");
1448            } else if (Email.DEBUG) {
1449                Log.d(TAG, "Backed up " + numBackedUp + " accounts...");
1450            }
1451            return numBackedUp;
1452        } finally {
1453            if (backupDatabase != null) {
1454                backupDatabase.close();
1455            }
1456        }
1457    }
1458
1459    /**
1460     * Restore account data, returning the number of accounts restored
1461     */
1462    private static int restoreAccounts(Context context, SQLiteDatabase mainDatabase) {
1463        if (Email.DEBUG) {
1464            Log.d(TAG, "restoreAccounts...");
1465        }
1466        SQLiteDatabase backupDatabase = getBackupDatabase(context);
1467        try {
1468            int numRecovered = copyAccountTables(backupDatabase, mainDatabase);
1469            if (numRecovered > 0) {
1470                Log.e(TAG, "Recovered " + numRecovered + " accounts!");
1471            } else if (numRecovered < 0) {
1472                Log.e(TAG, "Account recovery failed?");
1473            } else if (Email.DEBUG) {
1474                Log.d(TAG, "No accounts to restore...");
1475            }
1476            return numRecovered;
1477        } finally {
1478            if (backupDatabase != null) {
1479                backupDatabase.close();
1480            }
1481        }
1482    }
1483
1484    @Override
1485    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
1486        // Handle this special case the fastest possible way
1487        if (uri == INTEGRITY_CHECK_URI) {
1488            checkDatabases();
1489            return 0;
1490        } else if (uri == ACCOUNT_BACKUP_URI) {
1491            return backupAccounts(getContext(), getDatabase(getContext()));
1492        }
1493
1494        // Notify all existing cursors, except for ACCOUNT_RESET_NEW_COUNT(_ID)
1495        Uri notificationUri = EmailContent.CONTENT_URI;
1496
1497        int match = findMatch(uri, "update");
1498        Context context = getContext();
1499        ContentResolver resolver = context.getContentResolver();
1500        // See the comment at delete(), above
1501        SQLiteDatabase db = getDatabase(context);
1502        int table = match >> BASE_SHIFT;
1503        int result;
1504
1505        // We do NOT allow setting of unreadCount/messageCount via the provider
1506        // These columns are maintained via triggers
1507        if (match == MAILBOX_ID || match == MAILBOX) {
1508            values.remove(MailboxColumns.UNREAD_COUNT);
1509            values.remove(MailboxColumns.MESSAGE_COUNT);
1510        }
1511
1512        ContentCache cache = mContentCaches[table];
1513        String tableName = TABLE_NAMES[table];
1514        String id = "0";
1515
1516        try {
1517            if (match == MESSAGE_ID || match == SYNCED_MESSAGE_ID) {
1518                if (!uri.getBooleanQueryParameter(IS_UIPROVIDER, false)) {
1519                    notifyUIConversation(uri);
1520                }
1521            }
1522outer:
1523            switch (match) {
1524                case UI_UPDATEDRAFT:
1525                    return uiUpdateDraft(uri, values);
1526                case UI_SENDDRAFT:
1527                    return uiSendDraft(uri, values);
1528                case UI_MESSAGE:
1529                    return uiUpdateMessage(uri, values);
1530                case MAILBOX_ID_ADD_TO_FIELD:
1531                case ACCOUNT_ID_ADD_TO_FIELD:
1532                    id = uri.getPathSegments().get(1);
1533                    String field = values.getAsString(EmailContent.FIELD_COLUMN_NAME);
1534                    Long add = values.getAsLong(EmailContent.ADD_COLUMN_NAME);
1535                    if (field == null || add == null) {
1536                        throw new IllegalArgumentException("No field/add specified " + uri);
1537                    }
1538                    ContentValues actualValues = new ContentValues();
1539                    if (cache != null) {
1540                        cache.lock(id);
1541                    }
1542                    try {
1543                        db.beginTransaction();
1544                        try {
1545                            Cursor c = db.query(tableName,
1546                                    new String[] {EmailContent.RECORD_ID, field},
1547                                    whereWithId(id, selection),
1548                                    selectionArgs, null, null, null);
1549                            try {
1550                                result = 0;
1551                                String[] bind = new String[1];
1552                                if (c.moveToNext()) {
1553                                    bind[0] = c.getString(0); // _id
1554                                    long value = c.getLong(1) + add;
1555                                    actualValues.put(field, value);
1556                                    result = db.update(tableName, actualValues, ID_EQUALS, bind);
1557                                }
1558                                db.setTransactionSuccessful();
1559                            } finally {
1560                                c.close();
1561                            }
1562                        } finally {
1563                            db.endTransaction();
1564                        }
1565                    } finally {
1566                        if (cache != null) {
1567                            cache.unlock(id, actualValues);
1568                        }
1569                    }
1570                    break;
1571                case SYNCED_MESSAGE_ID:
1572                case UPDATED_MESSAGE_ID:
1573                case MESSAGE_ID:
1574                case BODY_ID:
1575                case ATTACHMENT_ID:
1576                case MAILBOX_ID:
1577                case ACCOUNT_ID:
1578                case HOSTAUTH_ID:
1579                case QUICK_RESPONSE_ID:
1580                case POLICY_ID:
1581                    id = uri.getPathSegments().get(1);
1582                    if (cache != null) {
1583                        cache.lock(id);
1584                    }
1585                    try {
1586                        if (match == SYNCED_MESSAGE_ID) {
1587                            // For synced messages, first copy the old message to the updated table
1588                            // Note the insert or ignore semantics, guaranteeing that only the first
1589                            // update will be reflected in the updated message table; therefore this
1590                            // row will always have the "original" data
1591                            db.execSQL(UPDATED_MESSAGE_INSERT + id);
1592                        } else if (match == MESSAGE_ID) {
1593                            db.execSQL(UPDATED_MESSAGE_DELETE + id);
1594                        }
1595                        result = db.update(tableName, values, whereWithId(id, selection),
1596                                selectionArgs);
1597                    } catch (SQLiteException e) {
1598                        // Null out values (so they aren't cached) and re-throw
1599                        values = null;
1600                        throw e;
1601                    } finally {
1602                        if (cache != null) {
1603                            cache.unlock(id, values);
1604                        }
1605                    }
1606                    if (match == ATTACHMENT_ID) {
1607                        if (values.containsKey(Attachment.FLAGS)) {
1608                            int flags = values.getAsInteger(Attachment.FLAGS);
1609                            mAttachmentService.attachmentChanged(getContext(),
1610                                    Integer.parseInt(id), flags);
1611                        }
1612                    } else if (match == MAILBOX_ID && values.containsKey(Mailbox.UI_SYNC_STATUS)) {
1613                        notifyUI(UIPROVIDER_MAILBOX_NOTIFIER, id);
1614                        // TODO: Remove logging
1615                        Log.d(TAG, "Notifying mailbox " + id + " status: " +
1616                                values.getAsInteger(Mailbox.UI_SYNC_STATUS));
1617                    } else if (match == ACCOUNT_ID) {
1618                        notifyUI(UIPROVIDER_ACCOUNT_NOTIFIER, id);
1619                    }
1620                    break;
1621                case BODY:
1622                case MESSAGE:
1623                case UPDATED_MESSAGE:
1624                case ATTACHMENT:
1625                case MAILBOX:
1626                case ACCOUNT:
1627                case HOSTAUTH:
1628                case POLICY:
1629                    switch(match) {
1630                        // To avoid invalidating the cache on updates, we execute them one at a
1631                        // time using the XXX_ID uri; these are all executed atomically
1632                        case ACCOUNT:
1633                        case MAILBOX:
1634                        case HOSTAUTH:
1635                        case POLICY:
1636                            Cursor c = db.query(tableName, EmailContent.ID_PROJECTION,
1637                                    selection, selectionArgs, null, null, null);
1638                            db.beginTransaction();
1639                            result = 0;
1640                            try {
1641                                while (c.moveToNext()) {
1642                                    update(ContentUris.withAppendedId(
1643                                                uri, c.getLong(EmailContent.ID_PROJECTION_COLUMN)),
1644                                            values, null, null);
1645                                    result++;
1646                                }
1647                                db.setTransactionSuccessful();
1648                            } finally {
1649                                db.endTransaction();
1650                                c.close();
1651                            }
1652                            break outer;
1653                        // Any cached table other than those above should be invalidated here
1654                        case MESSAGE:
1655                            // If we're doing some generic update, the whole cache needs to be
1656                            // invalidated.  This case should be quite rare
1657                            cache.invalidate("Update", uri, selection);
1658                            //$FALL-THROUGH$
1659                        default:
1660                            result = db.update(tableName, values, selection, selectionArgs);
1661                            break outer;
1662                    }
1663                case ACCOUNT_RESET_NEW_COUNT_ID:
1664                    id = uri.getPathSegments().get(1);
1665                    if (cache != null) {
1666                        cache.lock(id);
1667                    }
1668                    ContentValues newMessageCount = CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT;
1669                    if (values != null) {
1670                        Long set = values.getAsLong(EmailContent.SET_COLUMN_NAME);
1671                        if (set != null) {
1672                            newMessageCount = new ContentValues();
1673                            newMessageCount.put(Account.NEW_MESSAGE_COUNT, set);
1674                        }
1675                    }
1676                    try {
1677                        result = db.update(tableName, newMessageCount,
1678                                whereWithId(id, selection), selectionArgs);
1679                    } finally {
1680                        if (cache != null) {
1681                            cache.unlock(id, values);
1682                        }
1683                    }
1684                    notificationUri = Account.CONTENT_URI; // Only notify account cursors.
1685                    break;
1686                case ACCOUNT_RESET_NEW_COUNT:
1687                    result = db.update(tableName, CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT,
1688                            selection, selectionArgs);
1689                    // Affects all accounts.  Just invalidate all account cache.
1690                    cache.invalidate("Reset all new counts", null, null);
1691                    notificationUri = Account.CONTENT_URI; // Only notify account cursors.
1692                    break;
1693                default:
1694                    throw new IllegalArgumentException("Unknown URI " + uri);
1695            }
1696        } catch (SQLiteException e) {
1697            checkDatabases();
1698            throw e;
1699        }
1700
1701        // Notify all notifier cursors
1702        sendNotifierChange(getBaseNotificationUri(match), NOTIFICATION_OP_UPDATE, id);
1703
1704        resolver.notifyChange(notificationUri, null);
1705        return result;
1706    }
1707
1708    /**
1709     * Returns the base notification URI for the given content type.
1710     *
1711     * @param match The type of content that was modified.
1712     */
1713    private Uri getBaseNotificationUri(int match) {
1714        Uri baseUri = null;
1715        switch (match) {
1716            case MESSAGE:
1717            case MESSAGE_ID:
1718            case SYNCED_MESSAGE_ID:
1719                baseUri = Message.NOTIFIER_URI;
1720                break;
1721            case ACCOUNT:
1722            case ACCOUNT_ID:
1723                baseUri = Account.NOTIFIER_URI;
1724                break;
1725        }
1726        return baseUri;
1727    }
1728
1729    /**
1730     * Sends a change notification to any cursors observers of the given base URI. The final
1731     * notification URI is dynamically built to contain the specified information. It will be
1732     * of the format <<baseURI>>/<<op>>/<<id>>; where <<op>> and <<id>> are optional depending
1733     * upon the given values.
1734     * NOTE: If <<op>> is specified, notifications for <<baseURI>>/<<id>> will NOT be invoked.
1735     * If this is necessary, it can be added. However, due to the implementation of
1736     * {@link ContentObserver}, observers of <<baseURI>> will receive multiple notifications.
1737     *
1738     * @param baseUri The base URI to send notifications to. Must be able to take appended IDs.
1739     * @param op Optional operation to be appended to the URI.
1740     * @param id If a positive value, the ID to append to the base URI. Otherwise, no ID will be
1741     *           appended to the base URI.
1742     */
1743    private void sendNotifierChange(Uri baseUri, String op, String id) {
1744        if (baseUri == null) return;
1745
1746        final ContentResolver resolver = getContext().getContentResolver();
1747
1748        // Append the operation, if specified
1749        if (op != null) {
1750            baseUri = baseUri.buildUpon().appendEncodedPath(op).build();
1751        }
1752
1753        long longId = 0L;
1754        try {
1755            longId = Long.valueOf(id);
1756        } catch (NumberFormatException ignore) {}
1757        if (longId > 0) {
1758            resolver.notifyChange(ContentUris.withAppendedId(baseUri, longId), null);
1759        } else {
1760            resolver.notifyChange(baseUri, null);
1761        }
1762
1763        // We want to send the message list changed notification if baseUri is Message.NOTIFIER_URI.
1764        if (baseUri.equals(Message.NOTIFIER_URI)) {
1765            sendMessageListDataChangedNotification();
1766        }
1767    }
1768
1769    private void sendMessageListDataChangedNotification() {
1770        final Context context = getContext();
1771        final Intent intent = new Intent(ACTION_NOTIFY_MESSAGE_LIST_DATASET_CHANGED);
1772        // Ideally this intent would contain information about which account changed, to limit the
1773        // updates to that particular account.  Unfortunately, that information is not available in
1774        // sendNotifierChange().
1775        context.sendBroadcast(intent);
1776    }
1777
1778    @Override
1779    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
1780            throws OperationApplicationException {
1781        Context context = getContext();
1782        SQLiteDatabase db = getDatabase(context);
1783        db.beginTransaction();
1784        try {
1785            ContentProviderResult[] results = super.applyBatch(operations);
1786            db.setTransactionSuccessful();
1787            return results;
1788        } finally {
1789            db.endTransaction();
1790        }
1791    }
1792
1793    /**
1794     * For testing purposes, check whether a given row is cached
1795     * @param baseUri the base uri of the EmailContent
1796     * @param id the row id of the EmailContent
1797     * @return whether or not the row is currently cached
1798     */
1799    @VisibleForTesting
1800    protected boolean isCached(Uri baseUri, long id) {
1801        int match = findMatch(baseUri, "isCached");
1802        int table = match >> BASE_SHIFT;
1803        ContentCache cache = mContentCaches[table];
1804        if (cache == null) return false;
1805        Cursor cc = cache.get(Long.toString(id));
1806        return (cc != null);
1807    }
1808
1809    public static interface AttachmentService {
1810        /**
1811         * Notify the service that an attachment has changed.
1812         */
1813        void attachmentChanged(Context context, long id, int flags);
1814    }
1815
1816    private final AttachmentService DEFAULT_ATTACHMENT_SERVICE = new AttachmentService() {
1817        @Override
1818        public void attachmentChanged(Context context, long id, int flags) {
1819            // The default implementation delegates to the real service.
1820            AttachmentDownloadService.attachmentChanged(context, id, flags);
1821        }
1822    };
1823    private AttachmentService mAttachmentService = DEFAULT_ATTACHMENT_SERVICE;
1824
1825    /**
1826     * Injects a custom attachment service handler. If null is specified, will reset to the
1827     * default service.
1828     */
1829    public void injectAttachmentService(AttachmentService as) {
1830        mAttachmentService = (as == null) ? DEFAULT_ATTACHMENT_SERVICE : as;
1831    }
1832
1833    // SELECT DISTINCT Boxes._id, Boxes.unreadCount count(Message._id) from Message,
1834    //   (SELECT _id, unreadCount, messageCount, lastNotifiedMessageCount, lastNotifiedMessageKey
1835    //   FROM Mailbox WHERE accountKey=6 AND ((type = 0) OR (syncInterval!=0 AND syncInterval!=-1)))
1836    //      AS Boxes
1837    // WHERE Boxes.messageCount!=Boxes.lastNotifiedMessageCount
1838    //   OR (Boxes._id=Message.mailboxKey AND Message._id>Boxes.lastNotifiedMessageKey)
1839    // TODO: This query can be simplified a bit
1840    private static final String NOTIFICATION_QUERY =
1841        "SELECT DISTINCT Boxes." + MailboxColumns.ID + ", Boxes." + MailboxColumns.UNREAD_COUNT +
1842            ", count(" + Message.TABLE_NAME + "." + MessageColumns.ID + ")" +
1843        " FROM " +
1844            Message.TABLE_NAME + "," +
1845            "(SELECT " + MailboxColumns.ID + "," + MailboxColumns.UNREAD_COUNT + "," +
1846                MailboxColumns.MESSAGE_COUNT + "," + MailboxColumns.LAST_NOTIFIED_MESSAGE_COUNT +
1847                "," + MailboxColumns.LAST_NOTIFIED_MESSAGE_KEY + " FROM " + Mailbox.TABLE_NAME +
1848                " WHERE " + MailboxColumns.ACCOUNT_KEY + "=?" +
1849                " AND (" + MailboxColumns.TYPE + "=" + Mailbox.TYPE_INBOX + " OR ("
1850                + MailboxColumns.SYNC_INTERVAL + "!=0 AND " +
1851                MailboxColumns.SYNC_INTERVAL + "!=-1))) AS Boxes " +
1852        "WHERE Boxes." + MailboxColumns.ID + '=' + Message.TABLE_NAME + "." +
1853                MessageColumns.MAILBOX_KEY + " AND " + Message.TABLE_NAME + "." +
1854                MessageColumns.ID + ">Boxes." + MailboxColumns.LAST_NOTIFIED_MESSAGE_KEY +
1855                " AND " + MessageColumns.FLAG_READ + "=0";
1856
1857    public Cursor notificationQuery(Uri uri) {
1858        SQLiteDatabase db = getDatabase(getContext());
1859        String accountId = uri.getLastPathSegment();
1860        return db.rawQuery(NOTIFICATION_QUERY, new String[] {accountId});
1861   }
1862
1863    public Cursor mostRecentMessageQuery(Uri uri) {
1864        SQLiteDatabase db = getDatabase(getContext());
1865        String mailboxId = uri.getLastPathSegment();
1866        return db.rawQuery("select max(_id) from Message where mailboxKey=?",
1867                new String[] {mailboxId});
1868   }
1869
1870    /**
1871     * Support for UnifiedEmail below
1872     */
1873
1874    private static final String NOT_A_DRAFT_STRING =
1875        Integer.toString(UIProvider.DraftType.NOT_A_DRAFT);
1876
1877    /**
1878     * Mapping of UIProvider columns to EmailProvider columns for the message list (called the
1879     * conversation list in UnifiedEmail)
1880     */
1881    private static final ProjectionMap sMessageListMap = ProjectionMap.builder()
1882        .add(BaseColumns._ID, MessageColumns.ID)
1883        .add(UIProvider.ConversationColumns.URI, uriWithId("uimessage"))
1884        .add(UIProvider.ConversationColumns.MESSAGE_LIST_URI, uriWithId("uimessage"))
1885        .add(UIProvider.ConversationColumns.SUBJECT, MessageColumns.SUBJECT)
1886        .add(UIProvider.ConversationColumns.SNIPPET, MessageColumns.SNIPPET)
1887        .add(UIProvider.ConversationColumns.SENDER_INFO, MessageColumns.FROM_LIST)
1888        .add(UIProvider.ConversationColumns.DATE_RECEIVED_MS, MessageColumns.TIMESTAMP)
1889        .add(UIProvider.ConversationColumns.HAS_ATTACHMENTS, MessageColumns.FLAG_ATTACHMENT)
1890        .add(UIProvider.ConversationColumns.NUM_MESSAGES, "1")
1891        .add(UIProvider.ConversationColumns.NUM_DRAFTS, "0")
1892        .add(UIProvider.ConversationColumns.SENDING_STATE,
1893                Integer.toString(ConversationSendingState.OTHER))
1894        .add(UIProvider.ConversationColumns.PRIORITY, Integer.toString(ConversationPriority.LOW))
1895        .add(UIProvider.ConversationColumns.READ, MessageColumns.FLAG_READ)
1896        .add(UIProvider.ConversationColumns.STARRED, MessageColumns.FLAG_FAVORITE)
1897        .add(UIProvider.ConversationColumns.FOLDER_LIST, MessageColumns.MAILBOX_KEY)
1898        .build();
1899
1900    /**
1901     * Mapping of UIProvider columns to EmailProvider columns for a detailed message view in
1902     * UnifiedEmail
1903     */
1904    private static final ProjectionMap sMessageViewMap = ProjectionMap.builder()
1905        .add(BaseColumns._ID, Message.TABLE_NAME + "." + EmailContent.MessageColumns.ID)
1906        .add(UIProvider.MessageColumns.SERVER_ID, SyncColumns.SERVER_ID)
1907        .add(UIProvider.MessageColumns.URI, uriWithFQId("uimessage", Message.TABLE_NAME))
1908        .add(UIProvider.MessageColumns.CONVERSATION_ID,
1909                uriWithFQId("uimessage", Message.TABLE_NAME))
1910        .add(UIProvider.MessageColumns.SUBJECT, EmailContent.MessageColumns.SUBJECT)
1911        .add(UIProvider.MessageColumns.SNIPPET, EmailContent.MessageColumns.SNIPPET)
1912        .add(UIProvider.MessageColumns.FROM, EmailContent.MessageColumns.FROM_LIST)
1913        .add(UIProvider.MessageColumns.TO, EmailContent.MessageColumns.TO_LIST)
1914        .add(UIProvider.MessageColumns.CC, EmailContent.MessageColumns.CC_LIST)
1915        .add(UIProvider.MessageColumns.BCC, EmailContent.MessageColumns.BCC_LIST)
1916        .add(UIProvider.MessageColumns.REPLY_TO, EmailContent.MessageColumns.REPLY_TO_LIST)
1917        .add(UIProvider.MessageColumns.DATE_RECEIVED_MS, EmailContent.MessageColumns.TIMESTAMP)
1918        .add(UIProvider.MessageColumns.BODY_HTML, Body.HTML_CONTENT)
1919        .add(UIProvider.MessageColumns.BODY_TEXT, Body.TEXT_CONTENT)
1920        .add(UIProvider.MessageColumns.EMBEDS_EXTERNAL_RESOURCES, "0")
1921        .add(UIProvider.MessageColumns.REF_MESSAGE_ID, "0")
1922        .add(UIProvider.MessageColumns.DRAFT_TYPE, NOT_A_DRAFT_STRING)
1923        .add(UIProvider.MessageColumns.APPEND_REF_MESSAGE_CONTENT, "0")
1924        .add(UIProvider.MessageColumns.HAS_ATTACHMENTS, EmailContent.MessageColumns.FLAG_ATTACHMENT)
1925        .add(UIProvider.MessageColumns.ATTACHMENT_LIST_URI,
1926                uriWithFQId("uiattachments", Message.TABLE_NAME))
1927        .add(UIProvider.MessageColumns.MESSAGE_FLAGS, "0")
1928        .add(UIProvider.MessageColumns.SAVE_MESSAGE_URI,
1929                uriWithFQId("uiupdatedraft", Message.TABLE_NAME))
1930        .add(UIProvider.MessageColumns.SEND_MESSAGE_URI,
1931                uriWithFQId("uisenddraft", Message.TABLE_NAME))
1932        // TODO(pwestbro): make this actually return valid results.
1933        .add(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, "0")
1934        .build();
1935
1936    /**
1937     * Mapping of UIProvider columns to EmailProvider columns for the folder list in UnifiedEmail
1938     */
1939    private static String getFolderCapabilities() {
1940        return "CASE WHEN (" + MailboxColumns.FLAGS + "&" + Mailbox.FLAG_ACCEPTS_MOVED_MAIL +
1941                ") !=0 THEN " + UIProvider.FolderCapabilities.CAN_ACCEPT_MOVED_MESSAGES +
1942                " ELSE 0 END";
1943    }
1944
1945    private static final ProjectionMap sFolderListMap = ProjectionMap.builder()
1946        .add(BaseColumns._ID, MailboxColumns.ID)
1947        .add(UIProvider.FolderColumns.URI, uriWithId("uifolder"))
1948        .add(UIProvider.FolderColumns.NAME, "displayName")
1949        .add(UIProvider.FolderColumns.HAS_CHILDREN,
1950                MailboxColumns.FLAGS + "&" + Mailbox.FLAG_HAS_CHILDREN)
1951        .add(UIProvider.FolderColumns.CAPABILITIES, getFolderCapabilities())
1952        .add(UIProvider.FolderColumns.SYNC_WINDOW, "3")
1953        .add(UIProvider.FolderColumns.CONVERSATION_LIST_URI, uriWithId("uimessages"))
1954        .add(UIProvider.FolderColumns.CHILD_FOLDERS_LIST_URI, uriWithId("uisubfolders"))
1955        .add(UIProvider.FolderColumns.UNREAD_COUNT, MailboxColumns.UNREAD_COUNT)
1956        .add(UIProvider.FolderColumns.TOTAL_COUNT, MailboxColumns.MESSAGE_COUNT)
1957        .add(UIProvider.FolderColumns.REFRESH_URI, uriWithId("uirefresh"))
1958        .add(UIProvider.FolderColumns.SYNC_STATUS, MailboxColumns.UI_SYNC_STATUS)
1959        .add(UIProvider.FolderColumns.LAST_SYNC_RESULT, MailboxColumns.UI_LAST_SYNC_RESULT)
1960        .build();
1961
1962    private static final ProjectionMap sAccountListMap = ProjectionMap.builder()
1963        .add(BaseColumns._ID, AccountColumns.ID)
1964        .add(UIProvider.AccountColumns.FOLDER_LIST_URI, uriWithId("uifolders"))
1965        .add(UIProvider.AccountColumns.NAME, AccountColumns.DISPLAY_NAME)
1966        .add(UIProvider.AccountColumns.SAVE_DRAFT_URI, uriWithId("uisavedraft"))
1967        .add(UIProvider.AccountColumns.SEND_MAIL_URI, uriWithId("uisendmail"))
1968        .add(UIProvider.AccountColumns.UNDO_URI, uriWithId("uiundo"))
1969        .add(UIProvider.AccountColumns.URI, uriWithId("uiaccount"))
1970        .add(UIProvider.AccountColumns.SETTINGS_QUERY_URI, uriWithId("uisettings"))
1971        // TODO: Is this used?
1972        .add(UIProvider.AccountColumns.PROVIDER_VERSION, "1")
1973        .add(UIProvider.AccountColumns.SYNC_STATUS, "0")
1974        .build();
1975
1976    /**
1977     * The "ORDER BY" clause for top level folders
1978     */
1979    private static final String MAILBOX_ORDER_BY = "CASE " + MailboxColumns.TYPE
1980        + " WHEN " + Mailbox.TYPE_INBOX   + " THEN 0"
1981        + " WHEN " + Mailbox.TYPE_DRAFTS  + " THEN 1"
1982        + " WHEN " + Mailbox.TYPE_OUTBOX  + " THEN 2"
1983        + " WHEN " + Mailbox.TYPE_SENT    + " THEN 3"
1984        + " WHEN " + Mailbox.TYPE_TRASH   + " THEN 4"
1985        + " WHEN " + Mailbox.TYPE_JUNK    + " THEN 5"
1986        // Other mailboxes (i.e. of Mailbox.TYPE_MAIL) are shown in alphabetical order.
1987        + " ELSE 10 END"
1988        + " ," + MailboxColumns.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
1989
1990
1991    /**
1992     * Mapping of UIProvider columns to EmailProvider columns for the message list (called the
1993     * conversation list in UnifiedEmail)
1994     */
1995    private static final ProjectionMap sAccountSettingsMap = ProjectionMap.builder()
1996        .add(UIProvider.SettingsColumns.SIGNATURE, AccountColumns.SIGNATURE)
1997        .add(UIProvider.SettingsColumns.AUTO_ADVANCE,
1998                Integer.toString(UIProvider.AutoAdvance.NEWER))
1999        .add(UIProvider.SettingsColumns.MESSAGE_TEXT_SIZE,
2000                Integer.toString(UIProvider.MessageTextSize.NORMAL))
2001        .add(UIProvider.SettingsColumns.SNAP_HEADERS,
2002                Integer.toString(UIProvider.SnapHeaderValue.ALWAYS))
2003        .add(UIProvider.SettingsColumns.REPLY_BEHAVIOR,
2004                Integer.toString(UIProvider.DefaultReplyBehavior.REPLY))
2005        .add(UIProvider.SettingsColumns.HIDE_CHECKBOXES, "0")
2006        .add(UIProvider.SettingsColumns.CONFIRM_DELETE, "0")
2007        .add(UIProvider.SettingsColumns.CONFIRM_ARCHIVE, "0")
2008        .add(UIProvider.SettingsColumns.CONFIRM_SEND, "0")
2009        .build();
2010
2011    /**
2012     * Mapping of UIProvider columns to EmailProvider columns for a message's attachments
2013     */
2014    private static final ProjectionMap sAttachmentMap = ProjectionMap.builder()
2015        .add(UIProvider.AttachmentColumns.NAME, AttachmentColumns.FILENAME)
2016        .add(UIProvider.AttachmentColumns.SIZE, AttachmentColumns.SIZE)
2017        .add(UIProvider.AttachmentColumns.URI, uriWithId("uiattachment"))
2018        .add(UIProvider.AttachmentColumns.CONTENT_TYPE, AttachmentColumns.MIME_TYPE)
2019        // TODO: What does SYNCED mean?
2020        .add(UIProvider.AttachmentColumns.SYNCED, "0")
2021        .build();
2022
2023    /**
2024     * Generate the SELECT clause using a specified mapping and the original UI projection
2025     * @param map the ProjectionMap to use for this projection
2026     * @param projection the projection as sent by UnifiedEmail
2027     * @param values ContentValues to be used if the ProjectionMap entry is null
2028     * @return a StringBuilder containing the SELECT expression for a SQLite query
2029     */
2030    private StringBuilder genSelect(ProjectionMap map, String[] projection) {
2031        return genSelect(map, projection, EMPTY_CONTENT_VALUES);
2032    }
2033
2034    private StringBuilder genSelect(ProjectionMap map, String[] projection, ContentValues values) {
2035        StringBuilder sb = new StringBuilder("SELECT ");
2036        boolean first = true;
2037        for (String column: projection) {
2038            if (first) {
2039                first = false;
2040            } else {
2041                sb.append(',');
2042            }
2043            String val = map.get(column);
2044            // If we don't have the column, be permissive, returning "0 AS <column>", and warn
2045            if (val == null) {
2046                if (values.containsKey(column)) {
2047                    val = "'" + values.getAsString(column) + "' AS " + column;
2048                } else {
2049                    Log.w(TAG, "UIProvider column not found, returning 0: " + column);
2050                    val = "NULL AS " + column;
2051                }
2052            }
2053            sb.append(val);
2054        }
2055        return sb;
2056    }
2057
2058    /**
2059     * Convenience method to create a Uri string given the "type" of query; we append the type
2060     * of the query and the id column name (_id)
2061     *
2062     * @param type the "type" of the query, as defined by our UriMatcher definitions
2063     * @return a Uri string
2064     */
2065    private static String uriWithId(String type) {
2066        return "'content://" + EmailContent.AUTHORITY + "/" + type + "/' || _id";
2067    }
2068
2069    /**
2070     * Convenience method to create a Uri string given the "type" of query and the table name to
2071     * which it applies; we append the type of the query and the fully qualified (FQ) id column
2072     * (i.e. including the table name); we need this for join queries where _id would otherwise
2073     * be ambiguous
2074     *
2075     * @param type the "type" of the query, as defined by our UriMatcher definitions
2076     * @param tableName the name of the table whose _id is referred to
2077     * @return a Uri string
2078     */
2079    private static String uriWithFQId(String type, String tableName) {
2080        return "'content://" + EmailContent.AUTHORITY + "/" + type + "/' || " + tableName + "._id";
2081    }
2082
2083    /**
2084     * Generate the "view message" SQLite query, given a projection from UnifiedEmail
2085     *
2086     * @param uiProjection as passed from UnifiedEmail
2087     * @return the SQLite query to be executed on the EmailProvider database
2088     */
2089    private String genQueryViewMessage(String[] uiProjection) {
2090        StringBuilder sb = genSelect(sMessageViewMap, uiProjection);
2091        sb.append(" FROM " + Message.TABLE_NAME + "," + Body.TABLE_NAME + " WHERE " +
2092                Body.MESSAGE_KEY + "=" + Message.TABLE_NAME + "." + Message.RECORD_ID + " AND " +
2093                Message.TABLE_NAME + "." + Message.RECORD_ID + "=?");
2094        return sb.toString();
2095    }
2096
2097    /**
2098     * Generate the "message list" SQLite query, given a projection from UnifiedEmail
2099     *
2100     * @param uiProjection as passed from UnifiedEmail
2101     * @return the SQLite query to be executed on the EmailProvider database
2102     */
2103    private String genQueryMailboxMessages(String[] uiProjection) {
2104        StringBuilder sb = genSelect(sMessageListMap, uiProjection);
2105        // Make constant
2106        sb.append(" FROM " + Message.TABLE_NAME + " WHERE " + Message.MAILBOX_KEY + "=? ORDER BY " +
2107                MessageColumns.TIMESTAMP + " DESC");
2108        return sb.toString();
2109    }
2110
2111    /**
2112     * Generate the "top level folder list" SQLite query, given a projection from UnifiedEmail
2113     *
2114     * @param uiProjection as passed from UnifiedEmail
2115     * @return the SQLite query to be executed on the EmailProvider database
2116     */
2117    private String genQueryAccountMailboxes(String[] uiProjection) {
2118        StringBuilder sb = genSelect(sFolderListMap, uiProjection);
2119        // Make constant
2120        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.ACCOUNT_KEY +
2121                "=? AND " + MailboxColumns.TYPE + " < " + Mailbox.TYPE_NOT_EMAIL +
2122                " AND " + MailboxColumns.PARENT_KEY + " < 0 ORDER BY ");
2123        sb.append(MAILBOX_ORDER_BY);
2124        return sb.toString();
2125    }
2126
2127    /**
2128     * Generate a "single mailbox" SQLite query, given a projection from UnifiedEmail
2129     *
2130     * @param uiProjection as passed from UnifiedEmail
2131     * @return the SQLite query to be executed on the EmailProvider database
2132     */
2133    private String genQueryMailbox(String[] uiProjection) {
2134        StringBuilder sb = genSelect(sFolderListMap, uiProjection);
2135        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.ID + "=?");
2136        return sb.toString();
2137    }
2138
2139    private static final long IMAP_CAPABILITIES =
2140            AccountCapabilities.SYNCABLE_FOLDERS |
2141            AccountCapabilities.FOLDER_SERVER_SEARCH |
2142            AccountCapabilities.UNDO;
2143
2144    private static final long POP3_CAPABILITIES = 0;
2145
2146    private static final long EAS_12_CAPABILITIES =
2147            AccountCapabilities.SYNCABLE_FOLDERS |
2148            AccountCapabilities.FOLDER_SERVER_SEARCH |
2149            AccountCapabilities.SANITIZED_HTML |
2150            AccountCapabilities.SMART_REPLY |
2151            AccountCapabilities.SERVER_SEARCH |
2152            AccountCapabilities.UNDO;
2153
2154    private static final long EAS_2_CAPABILITIES =
2155            AccountCapabilities.SYNCABLE_FOLDERS |
2156            AccountCapabilities.SANITIZED_HTML |
2157            AccountCapabilities.SMART_REPLY |
2158            AccountCapabilities.UNDO;
2159
2160    private static final Uri BASE_EXTERNAL_URI = Uri.parse("content://ui.email.android.com");
2161
2162    private static final Uri BASE_EXTERAL_URI2 = Uri.parse("content://ui.email2.android.com");
2163
2164    private static String getExternalUriString(String segment, String account) {
2165        return BASE_EXTERNAL_URI.buildUpon().appendPath(segment)
2166                .appendQueryParameter("account", account).build().toString();
2167    }
2168
2169    private static String getExternalUriStringEmail2(String segment, String account) {
2170        return BASE_EXTERAL_URI2.buildUpon().appendPath(segment)
2171                .appendQueryParameter("account", account).build().toString();
2172    }
2173
2174    /**
2175     * Generate a "single account" SQLite query, given a projection from UnifiedEmail
2176     *
2177     * @param uiProjection as passed from UnifiedEmail
2178     * @return the SQLite query to be executed on the EmailProvider database
2179     */
2180    // TODO: Get protocol specific stuff out of here (it should be in the account)
2181    private String genQueryAccount(String[] uiProjection, String id) {
2182        ContentValues values = new ContentValues();
2183        long accountId = Long.parseLong(id);
2184        String protocol = Account.getProtocol(getContext(), accountId);
2185        if (HostAuth.SCHEME_IMAP.equals(protocol)) {
2186            values.put(UIProvider.AccountColumns.CAPABILITIES, IMAP_CAPABILITIES);
2187        } else if (HostAuth.SCHEME_POP3.equals(protocol)) {
2188            values.put(UIProvider.AccountColumns.CAPABILITIES, POP3_CAPABILITIES);
2189        } else {
2190            Account account = Account.restoreAccountWithId(getContext(), accountId);
2191            String easVersion = account.mProtocolVersion;
2192            Double easVersionDouble = 2.5D;
2193            if (easVersion != null) {
2194                try {
2195                    easVersionDouble = Double.parseDouble(easVersion);
2196                } catch (NumberFormatException e) {
2197                    // Stick with 2.5
2198                }
2199            }
2200            if (easVersionDouble >= 12.0D) {
2201                values.put(UIProvider.AccountColumns.CAPABILITIES, EAS_12_CAPABILITIES);
2202            } else {
2203                values.put(UIProvider.AccountColumns.CAPABILITIES, EAS_2_CAPABILITIES);
2204            }
2205        }
2206        values.put(UIProvider.AccountColumns.SETTINGS_INTENT_URI,
2207                getExternalUriString("settings", id));
2208        values.put(UIProvider.AccountColumns.COMPOSE_URI,
2209                getExternalUriStringEmail2("compose", id));
2210        values.put(UIProvider.AccountColumns.MIME_TYPE, "application/email-ls");
2211        StringBuilder sb = genSelect(sAccountListMap, uiProjection, values);
2212        sb.append(" FROM " + Account.TABLE_NAME + " WHERE " + AccountColumns.ID + "=?");
2213        return sb.toString();
2214    }
2215
2216    /**
2217     * Generate an "account settings" SQLite query, given a projection from UnifiedEmail
2218     *
2219     * @param uiProjection as passed from UnifiedEmail
2220     * @return the SQLite query to be executed on the EmailProvider database
2221     */
2222    private String genQuerySettings(String[] uiProjection, String id) {
2223        ContentValues values = new ContentValues();
2224        long accountId = Long.parseLong(id);
2225        long mailboxId = Mailbox.findMailboxOfType(getContext(), accountId, Mailbox.TYPE_INBOX);
2226        if (mailboxId != Mailbox.NO_MAILBOX) {
2227            values.put(UIProvider.SettingsColumns.DEFAULT_INBOX,
2228                    "content://" + EmailContent.AUTHORITY + "/uifolder/" + mailboxId);
2229        }
2230        StringBuilder sb = genSelect(sAccountSettingsMap, uiProjection, values);
2231        sb.append(" FROM " + Account.TABLE_NAME + " WHERE " + AccountColumns.ID + "=?");
2232        return sb.toString();
2233    }
2234
2235    private Cursor uiAccounts(String[] uiProjection) {
2236        Context context = getContext();
2237        SQLiteDatabase db = getDatabase(context);
2238        Cursor accountIdCursor =
2239                db.rawQuery("select _id from " + Account.TABLE_NAME, new String[0]);
2240        MatrixCursor mc = new MatrixCursor(uiProjection, accountIdCursor.getCount());
2241        Object[] values = new Object[uiProjection.length];
2242        try {
2243            while (accountIdCursor.moveToNext()) {
2244                String id = accountIdCursor.getString(0);
2245                Cursor accountCursor =
2246                        db.rawQuery(genQueryAccount(uiProjection, id), new String[] {id});
2247                if (accountCursor.moveToNext()) {
2248                    for (int i = 0; i < uiProjection.length; i++) {
2249                        values[i] = accountCursor.getString(i);
2250                    }
2251                    mc.addRow(values);
2252                }
2253                accountCursor.close();
2254            }
2255        } finally {
2256            accountIdCursor.close();
2257        }
2258        return mc;
2259    }
2260
2261    /**
2262     * Generate the "attachment list" SQLite query, given a projection from UnifiedEmail
2263     *
2264     * @param uiProjection as passed from UnifiedEmail
2265     * @return the SQLite query to be executed on the EmailProvider database
2266     */
2267    private String genQueryAttachments(String[] uiProjection) {
2268        StringBuilder sb = genSelect(sAttachmentMap, uiProjection);
2269        sb.append(" FROM " + Attachment.TABLE_NAME + " WHERE " + AttachmentColumns.MESSAGE_KEY +
2270                " =? ");
2271        return sb.toString();
2272    }
2273
2274    /**
2275     * Generate the "single attachment" SQLite query, given a projection from UnifiedEmail
2276     *
2277     * @param uiProjection as passed from UnifiedEmail
2278     * @return the SQLite query to be executed on the EmailProvider database
2279     */
2280    private String genQueryAttachment(String[] uiProjection) {
2281        StringBuilder sb = genSelect(sAttachmentMap, uiProjection);
2282        sb.append(" FROM " + Attachment.TABLE_NAME + " WHERE " + AttachmentColumns.ID + " =? ");
2283        return sb.toString();
2284    }
2285
2286    /**
2287     * Generate the "subfolder list" SQLite query, given a projection from UnifiedEmail
2288     *
2289     * @param uiProjection as passed from UnifiedEmail
2290     * @return the SQLite query to be executed on the EmailProvider database
2291     */
2292    private String genQuerySubfolders(String[] uiProjection) {
2293        StringBuilder sb = genSelect(sFolderListMap, uiProjection);
2294        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.PARENT_KEY +
2295                " =? ORDER BY ");
2296        sb.append(MAILBOX_ORDER_BY);
2297        return sb.toString();
2298    }
2299
2300    /**
2301     * Handle UnifiedEmail queries here (dispatched from query())
2302     *
2303     * @param match the UriMatcher match for the original uri passed in from UnifiedEmail
2304     * @param uri the original uri passed in from UnifiedEmail
2305     * @param uiProjection the projection passed in from UnifiedEmail
2306     * @return the result Cursor
2307     */
2308    private Cursor uiQuery(int match, Uri uri, String[] uiProjection) {
2309        Context context = getContext();
2310        ContentResolver resolver = context.getContentResolver();
2311        SQLiteDatabase db = getDatabase(context);
2312        // Should we ever return null, or throw an exception??
2313        Cursor c = null;
2314        String id = uri.getPathSegments().get(1);
2315        Uri notifyUri = null;
2316        switch(match) {
2317            case UI_FOLDERS:
2318                c = db.rawQuery(genQueryAccountMailboxes(uiProjection), new String[] {id});
2319                break;
2320            case UI_SUBFOLDERS:
2321                c = db.rawQuery(genQuerySubfolders(uiProjection), new String[] {id});
2322                break;
2323            case UI_MESSAGES:
2324                c = db.rawQuery(genQueryMailboxMessages(uiProjection), new String[] {id});
2325                notifyUri = UIPROVIDER_CONVERSATION_NOTIFIER.buildUpon().appendPath(id).build();
2326                break;
2327            case UI_MESSAGE:
2328                c = db.rawQuery(genQueryViewMessage(uiProjection), new String[] {id});
2329                break;
2330            case UI_ATTACHMENTS:
2331                c = db.rawQuery(genQueryAttachments(uiProjection), new String[] {id});
2332                break;
2333            case UI_ATTACHMENT:
2334                c = db.rawQuery(genQueryAttachment(uiProjection), new String[] {id});
2335                notifyUri = UIPROVIDER_ATTACHMENT_NOTIFIER.buildUpon().appendPath(id).build();
2336                break;
2337            case UI_FOLDER:
2338                c = db.rawQuery(genQueryMailbox(uiProjection), new String[] {id});
2339                notifyUri = UIPROVIDER_MAILBOX_NOTIFIER.buildUpon().appendPath(id).build();
2340                break;
2341            case UI_ACCOUNT:
2342                c = db.rawQuery(genQueryAccount(uiProjection, id), new String[] {id});
2343                notifyUri = UIPROVIDER_ACCOUNT_NOTIFIER.buildUpon().appendPath(id).build();
2344                break;
2345            case UI_SETTINGS:
2346                c = db.rawQuery(genQuerySettings(uiProjection, id), new String[] {id});
2347                notifyUri = UIPROVIDER_SETTINGS_NOTIFIER.buildUpon().appendPath(id).build();
2348                break;
2349        }
2350        if (notifyUri != null) {
2351            c.setNotificationUri(resolver, notifyUri);
2352        }
2353        return c;
2354    }
2355
2356    /**
2357     * Convert a UIProvider attachment to an EmailProvider attachment (for sending); we only need
2358     * a few of the fields
2359     * @param uiAtt the UIProvider attachment to convert
2360     * @return the EmailProvider attachment
2361     */
2362    private Attachment convertUiAttachmentToAttachment(
2363            com.android.mail.providers.Attachment uiAtt) {
2364        Attachment att = new Attachment();
2365        att.mContentUri = uiAtt.contentUri;
2366        att.mFileName = uiAtt.name;
2367        att.mMimeType = uiAtt.mimeType;
2368        att.mSize = uiAtt.size;
2369        return att;
2370    }
2371
2372    /**
2373     * Create a mailbox given the account and mailboxType.
2374     */
2375    private Mailbox createMailbox(long accountId, int mailboxType) {
2376        Context context = getContext();
2377        int resId = -1;
2378        switch (mailboxType) {
2379            case Mailbox.TYPE_INBOX:
2380                resId = R.string.mailbox_name_server_inbox;
2381                break;
2382            case Mailbox.TYPE_OUTBOX:
2383                resId = R.string.mailbox_name_server_outbox;
2384                break;
2385            case Mailbox.TYPE_DRAFTS:
2386                resId = R.string.mailbox_name_server_drafts;
2387                break;
2388            case Mailbox.TYPE_TRASH:
2389                resId = R.string.mailbox_name_server_trash;
2390                break;
2391            case Mailbox.TYPE_SENT:
2392                resId = R.string.mailbox_name_server_sent;
2393                break;
2394            case Mailbox.TYPE_JUNK:
2395                resId = R.string.mailbox_name_server_junk;
2396                break;
2397            default:
2398                throw new IllegalArgumentException("Illegal mailbox type");
2399        }
2400        Log.d(TAG, "Creating mailbox of type " + mailboxType + " for account " + accountId);
2401        Mailbox box = Mailbox.newSystemMailbox(accountId, mailboxType, context.getString(resId));
2402        box.save(context);
2403        return box;
2404    }
2405
2406    /**
2407     * Given an account name and a mailbox type, return that mailbox, creating it if necessary
2408     * @param accountName the account name to use
2409     * @param mailboxType the type of mailbox we're trying to find
2410     * @return the mailbox of the given type for the account in the uri, or null if not found
2411     */
2412    private Mailbox getMailboxByAccountIdAndType(String accountId, int mailboxType) {
2413        long id = Long.parseLong(accountId);
2414        Mailbox mailbox = Mailbox.restoreMailboxOfType(getContext(), id, mailboxType);
2415        if (mailbox == null) {
2416            mailbox = createMailbox(id, mailboxType);
2417        }
2418        return mailbox;
2419    }
2420
2421    private Message getMessageFromPathSegments(List<String> pathSegments) {
2422        Message msg = null;
2423        if (pathSegments.size() > 2) {
2424            msg = Message.restoreMessageWithId(getContext(), Long.parseLong(pathSegments.get(2)));
2425        }
2426        if (msg == null) {
2427            msg = new Message();
2428        }
2429        return msg;
2430    }
2431    /**
2432     * Given a mailbox and the content values for a message, create/save the message in the mailbox
2433     * @param mailbox the mailbox to use
2434     * @param values the content values that represent message fields
2435     * @return the uri of the newly created message
2436     */
2437    private Uri uiSaveMessage(Message msg, Mailbox mailbox, ContentValues values) {
2438        Context context = getContext();
2439        // Fill in the message
2440        msg.mTo = values.getAsString(UIProvider.MessageColumns.TO);
2441        msg.mCc = values.getAsString(UIProvider.MessageColumns.CC);
2442        msg.mBcc = values.getAsString(UIProvider.MessageColumns.BCC);
2443        msg.mSubject = values.getAsString(UIProvider.MessageColumns.SUBJECT);
2444        msg.mText = values.getAsString(UIProvider.MessageColumns.BODY_TEXT);
2445        msg.mHtml = values.getAsString(UIProvider.MessageColumns.BODY_HTML);
2446        msg.mMailboxKey = mailbox.mId;
2447        msg.mAccountKey = mailbox.mAccountKey;
2448        msg.mDisplayName = msg.mTo;
2449        msg.mFlagLoaded = Message.FLAG_LOADED_COMPLETE;
2450        // Get attachments from the ContentValues
2451        ArrayList<com.android.mail.providers.Attachment> uiAtts =
2452                com.android.mail.providers.Attachment.getAttachmentsFromJoinedAttachmentInfo(
2453                        values.getAsString(UIProvider.MessageColumns.JOINED_ATTACHMENT_INFOS));
2454        ArrayList<Attachment> atts = new ArrayList<Attachment>();
2455        for (com.android.mail.providers.Attachment uiAtt: uiAtts) {
2456            // Convert to our attachments and add to the list; everything else should "just work"
2457            atts.add(convertUiAttachmentToAttachment(uiAtt));
2458        }
2459        if (!atts.isEmpty()) {
2460            msg.mAttachments = atts;
2461        }
2462        // Save it or update it...
2463        if (!msg.isSaved()) {
2464            msg.save(context);
2465        } else {
2466            // This is tricky due to how messages/attachments are saved; rather than putz with
2467            // what's changed, we'll delete/re-add them
2468            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
2469            // Delete all existing attachments
2470            ops.add(ContentProviderOperation.newDelete(
2471                    ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, msg.mId))
2472                    .build());
2473            // Delete the body
2474            ops.add(ContentProviderOperation.newDelete(Body.CONTENT_URI)
2475                    .withSelection(Body.MESSAGE_KEY + "=?", new String[] {Long.toString(msg.mId)})
2476                    .build());
2477            // Add the ops for the message, atts, and body
2478            msg.addSaveOps(ops);
2479            // Do it!
2480            try {
2481                applyBatch(ops);
2482            } catch (OperationApplicationException e) {
2483            }
2484        }
2485        return Uri.parse("content://" + EmailContent.AUTHORITY + "/uimessage/" + msg.mId);
2486    }
2487
2488    /**
2489     * Create and send the message via the account indicated in the uri
2490     * @param uri the incoming uri
2491     * @param values the content values that represent message fields
2492     * @return the uri of the created message
2493     */
2494    private Uri uiSendMail(Uri uri, ContentValues values) {
2495        List<String> pathSegments = uri.getPathSegments();
2496        Mailbox mailbox = getMailboxByAccountIdAndType(pathSegments.get(1), Mailbox.TYPE_OUTBOX);
2497        if (mailbox == null) return null;
2498        Message msg = getMessageFromPathSegments(pathSegments);
2499        try {
2500            return uiSaveMessage(msg, mailbox, values);
2501        } finally {
2502            // Kick observers
2503            getContext().getContentResolver().notifyChange(Mailbox.CONTENT_URI, null);
2504        }
2505    }
2506
2507    /**
2508     * Create a message and save it to the drafts folder of the account indicated in the uri
2509     * @param uri the incoming uri
2510     * @param values the content values that represent message fields
2511     * @return the uri of the created message
2512     */
2513    private Uri uiSaveDraft(Uri uri, ContentValues values) {
2514        List<String> pathSegments = uri.getPathSegments();
2515        Mailbox mailbox = getMailboxByAccountIdAndType(pathSegments.get(1), Mailbox.TYPE_DRAFTS);
2516        if (mailbox == null) return null;
2517        Message msg = getMessageFromPathSegments(pathSegments);
2518        return uiSaveMessage(msg, mailbox, values);
2519    }
2520
2521    private int uiUpdateDraft(Uri uri, ContentValues values) {
2522        Context context = getContext();
2523        Message msg = Message.restoreMessageWithId(context,
2524                Long.parseLong(uri.getPathSegments().get(1)));
2525        if (msg == null) return 0;
2526        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
2527        if (mailbox == null) return 0;
2528        uiSaveMessage(msg, mailbox, values);
2529        return 1;
2530    }
2531
2532    private int uiSendDraft(Uri uri, ContentValues values) {
2533        Context context = getContext();
2534        Message msg = Message.restoreMessageWithId(context,
2535                Long.parseLong(uri.getPathSegments().get(1)));
2536        if (msg == null) return 0;
2537        long mailboxId = Mailbox.findMailboxOfType(context, msg.mAccountKey, Mailbox.TYPE_OUTBOX);
2538        if (mailboxId == Mailbox.NO_MAILBOX) return 0;
2539        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
2540        if (mailbox == null) return 0;
2541        uiSaveMessage(msg, mailbox, values);
2542        // Kick observers
2543        context.getContentResolver().notifyChange(Mailbox.CONTENT_URI, null);
2544        return 1;
2545    }
2546
2547    private void putIntegerLongOrBoolean(ContentValues values, String columnName, Object value) {
2548        if (value instanceof Integer) {
2549            Integer intValue = (Integer)value;
2550            values.put(columnName, intValue);
2551        } else if (value instanceof Boolean) {
2552            Boolean boolValue = (Boolean)value;
2553            values.put(columnName, boolValue ? 1 : 0);
2554        } else if (value instanceof Long) {
2555            Long longValue = (Long)value;
2556            values.put(columnName, longValue);
2557        }
2558    }
2559
2560    private ContentValues convertUiMessageValues(ContentValues values) {
2561        ContentValues ourValues = new ContentValues();
2562        for (String columnName: values.keySet()) {
2563            Object val = values.get(columnName);
2564            if (columnName.equals(UIProvider.ConversationColumns.STARRED)) {
2565                putIntegerLongOrBoolean(ourValues, MessageColumns.FLAG_FAVORITE, val);
2566            } else if (columnName.equals(UIProvider.ConversationColumns.READ)) {
2567                putIntegerLongOrBoolean(ourValues, MessageColumns.FLAG_READ, val);
2568            } else if (columnName.equals(MessageColumns.MAILBOX_KEY)) {
2569                putIntegerLongOrBoolean(ourValues, MessageColumns.MAILBOX_KEY, val);
2570            } else if (columnName.equals(UIProvider.ConversationColumns.FOLDER_LIST)) {
2571                // Convert from folder list uri to mailbox key
2572                Uri uri = Uri.parse((String)val);
2573                Long mailboxId = Long.parseLong(uri.getLastPathSegment());
2574                putIntegerLongOrBoolean(ourValues, MessageColumns.MAILBOX_KEY, mailboxId);
2575            } else {
2576                throw new IllegalArgumentException("Can't update " + columnName + " in message");
2577            }
2578        }
2579        return ourValues;
2580    }
2581
2582    private Uri convertToEmailProviderUri(Uri uri, boolean asProvider) {
2583        String idString = uri.getLastPathSegment();
2584        try {
2585            long id = Long.parseLong(idString);
2586            Uri ourUri = ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, id);
2587            if (asProvider) {
2588                ourUri = ourUri.buildUpon().appendQueryParameter(IS_UIPROVIDER, "true").build();
2589            }
2590            return ourUri;
2591        } catch (NumberFormatException e) {
2592            return null;
2593        }
2594    }
2595
2596    private Message getMessageFromLastSegment(Uri uri) {
2597        long messageId = Long.parseLong(uri.getLastPathSegment());
2598        return Message.restoreMessageWithId(getContext(), messageId);
2599    }
2600
2601    /**
2602     * Add an undo operation for the current sequence; if the sequence is newer than what we've had,
2603     * clear out the undo list and start over
2604     * @param uri the uri we're working on
2605     * @param op the ContentProviderOperation to perform upon undo
2606     */
2607    private void addToSequence(Uri uri, ContentProviderOperation op) {
2608        String sequenceString = uri.getQueryParameter(UIProvider.SEQUENCE_QUERY_PARAMETER);
2609        if (sequenceString != null) {
2610            int sequence = Integer.parseInt(sequenceString);
2611            if (sequence > mLastSequence) {
2612                // Reset sequence
2613                mLastSequenceOps.clear();
2614                mLastSequence = sequence;
2615            }
2616            // TODO: Need something to indicate a change isn't ready (undoable)
2617            mLastSequenceOps.add(op);
2618        }
2619    }
2620
2621    private int uiUpdateMessage(Uri uri, ContentValues values) {
2622        Uri ourUri = convertToEmailProviderUri(uri, true);
2623        if (ourUri == null) return 0;
2624        ContentValues ourValues = convertUiMessageValues(values);
2625        Message msg = getMessageFromLastSegment(uri);
2626        if (msg == null) return 0;
2627        ContentValues undoValues = new ContentValues();
2628        for (String columnName: ourValues.keySet()) {
2629            if (columnName.equals(MessageColumns.MAILBOX_KEY)) {
2630                undoValues.put(MessageColumns.MAILBOX_KEY, msg.mMailboxKey);
2631            } else if (columnName.equals(MessageColumns.FLAG_READ)) {
2632                undoValues.put(MessageColumns.FLAG_READ, msg.mFlagRead);
2633            } else if (columnName.equals(MessageColumns.FLAG_FAVORITE)) {
2634                undoValues.put(MessageColumns.FLAG_FAVORITE, msg.mFlagFavorite);
2635            }
2636        }
2637        ContentProviderOperation op =
2638                ContentProviderOperation.newUpdate(convertToEmailProviderUri(uri, false))
2639                        .withValues(undoValues)
2640                        .build();
2641        addToSequence(uri, op);
2642        return update(ourUri, ourValues, null, null);
2643    }
2644
2645    private int uiDeleteMessage(Uri uri) {
2646        Context context = getContext();
2647        Message msg = getMessageFromLastSegment(uri);
2648        if (msg == null) return 0;
2649        Mailbox mailbox =
2650                Mailbox.restoreMailboxOfType(context, msg.mAccountKey, Mailbox.TYPE_TRASH);
2651        if (mailbox == null) return 0;
2652        ContentProviderOperation op =
2653                ContentProviderOperation.newUpdate(convertToEmailProviderUri(uri, false))
2654                        .withValue(Message.MAILBOX_KEY, msg.mMailboxKey)
2655                        .build();
2656        addToSequence(uri, op);
2657        ContentValues values = new ContentValues();
2658        values.put(Message.MAILBOX_KEY, mailbox.mId);
2659        return uiUpdateMessage(uri, values);
2660    }
2661
2662    private Cursor uiUndo(Uri uri, String[] projection) {
2663        // First see if we have any operations saved
2664        // TODO: Make sure seq matches
2665        if (!mLastSequenceOps.isEmpty()) {
2666            try {
2667                // TODO Always use this projection?  Or what's passed in?
2668                // Not sure if UI wants it, but I'm making a cursor of convo uri's
2669                MatrixCursor c = new MatrixCursor(
2670                        new String[] {UIProvider.ConversationColumns.URI},
2671                        mLastSequenceOps.size());
2672                for (ContentProviderOperation op: mLastSequenceOps) {
2673                    c.addRow(new String[] {op.getUri().toString()});
2674                }
2675                // Just apply the batch and we're done!
2676                applyBatch(mLastSequenceOps);
2677                // But clear the operations
2678                mLastSequenceOps.clear();
2679                // Tell the UI there are changes
2680                getContext().getContentResolver().notifyChange(UIPROVIDER_CONVERSATION_NOTIFIER,
2681                        null);
2682                Log.d(TAG, "[Notify UI: Undo]");
2683                return c;
2684            } catch (OperationApplicationException e) {
2685            }
2686        }
2687        return new MatrixCursor(projection, 0);
2688    }
2689
2690    private void notifyUIConversation(Uri uri) {
2691        String id = uri.getLastPathSegment();
2692        Message msg = Message.restoreMessageWithId(getContext(), Long.parseLong(id));
2693        if (msg != null) {
2694            notifyUI(UIPROVIDER_CONVERSATION_NOTIFIER, Long.toString(msg.mMailboxKey));
2695        }
2696    }
2697
2698    private void notifyUIConversationMailbox(long id) {
2699        notifyUI(UIPROVIDER_CONVERSATION_NOTIFIER, Long.toString(id));
2700    }
2701
2702    private void notifyUI(Uri uri, String id) {
2703        Uri notifyUri = uri.buildUpon().appendPath(id).build();
2704        getContext().getContentResolver().notifyChange(notifyUri, null);
2705        // Temporary
2706        Log.d(TAG, "[Notify UI: " + notifyUri + "]");
2707    }
2708
2709    /**
2710     * Support for services and service notifications
2711     */
2712
2713    private final IEmailServiceCallback.Stub mServiceCallback =
2714            new IEmailServiceCallback.Stub() {
2715
2716        @Override
2717        public void syncMailboxListStatus(long accountId, int statusCode, int progress)
2718                throws RemoteException {
2719        }
2720
2721        @Override
2722        public void syncMailboxStatus(long mailboxId, int statusCode, int progress)
2723                throws RemoteException {
2724            // We'll get callbacks here from the services, which we'll pass back to the UI
2725            Uri uri = ContentUris.withAppendedId(FOLDER_STATUS_URI, mailboxId);
2726            EmailProvider.this.getContext().getContentResolver().notifyChange(uri, null);
2727        }
2728
2729        @Override
2730        public void loadAttachmentStatus(long messageId, long attachmentId, int statusCode,
2731                int progress) throws RemoteException {
2732        }
2733
2734        @Override
2735        public void sendMessageStatus(long messageId, String subject, int statusCode, int progress)
2736                throws RemoteException {
2737        }
2738
2739        @Override
2740        public void loadMessageStatus(long messageId, int statusCode, int progress)
2741                throws RemoteException {
2742        }
2743    };
2744
2745    private Cursor uiFolderRefresh(Uri uri, String[] projection) {
2746        Context context = getContext();
2747        String idString = uri.getPathSegments().get(1);
2748        long id = Long.parseLong(idString);
2749        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
2750        if (mailbox == null) return null;
2751        EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
2752                mServiceCallback, mailbox.mAccountKey);
2753        try {
2754            service.startSync(id, true);
2755        } catch (RemoteException e) {
2756        }
2757        return null;
2758    }
2759}
2760