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