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