EmailProvider.java revision d47027b92d07342223b6094748787414ce22e549
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)g
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            if (combinedAccount) {
3092                addCombinedAccountRow(mc);
3093            }
3094            while (accountIdCursor.moveToNext()) {
3095                String id = accountIdCursor.getString(0);
3096                Cursor accountCursor =
3097                        db.rawQuery(genQueryAccount(uiProjection, id), new String[] {id});
3098                if (accountCursor.moveToNext()) {
3099                    for (int i = 0; i < uiProjection.length; i++) {
3100                        values[i] = accountCursor.getString(i);
3101                    }
3102                    mc.addRow(values);
3103                }
3104                accountCursor.close();
3105            }
3106            if (combinedAccount) {
3107                addCombinedAccountRow(mc);
3108            }
3109        } finally {
3110            accountIdCursor.close();
3111        }
3112        mc.setNotificationUri(context.getContentResolver(), UIPROVIDER_ALL_ACCOUNTS_NOTIFIER);
3113        return mc;
3114    }
3115
3116    /**
3117     * Generate the "attachment list" SQLite query, given a projection from UnifiedEmail
3118     *
3119     * @param uiProjection as passed from UnifiedEmail
3120     * @param contentTypeQueryParameters list of mimeTypes, used as a filter for the attachments
3121     * or null if there are no query parameters
3122     * @return the SQLite query to be executed on the EmailProvider database
3123     */
3124    private String genQueryAttachments(String[] uiProjection,
3125            List<String> contentTypeQueryParameters) {
3126        StringBuilder sb = genSelect(getAttachmentMap(), uiProjection);
3127        sb.append(" FROM " + Attachment.TABLE_NAME + " WHERE " + AttachmentColumns.MESSAGE_KEY +
3128                " =? ");
3129
3130        // Filter for certain content types.
3131        // The filter works by adding LIKE operators for each
3132        // content type you wish to request. Content types
3133        // are filtered by performing a case-insensitive "starts with"
3134        // filter. IE, "image/" would return "image/png" as well as "image/jpeg".
3135        if (contentTypeQueryParameters != null && !contentTypeQueryParameters.isEmpty()) {
3136            final int size = contentTypeQueryParameters.size();
3137            sb.append("AND (");
3138            for (int i = 0; i < size; i++) {
3139                final String contentType = contentTypeQueryParameters.get(i);
3140                sb.append(AttachmentColumns.MIME_TYPE + " LIKE '" + contentType + "%'");
3141
3142                if (i != size - 1) {
3143                    sb.append(" OR ");
3144                }
3145            }
3146            sb.append(")");
3147        }
3148        return sb.toString();
3149    }
3150
3151    /**
3152     * Generate the "single attachment" SQLite query, given a projection from UnifiedEmail
3153     *
3154     * @param uiProjection as passed from UnifiedEmail
3155     * @return the SQLite query to be executed on the EmailProvider database
3156     */
3157    private String genQueryAttachment(String[] uiProjection, String idString) {
3158        Long id = Long.parseLong(idString);
3159        Attachment att = Attachment.restoreAttachmentWithId(getContext(), id);
3160        ContentValues values = new ContentValues();
3161        values.put(AttachmentColumns.CONTENT_URI,
3162                AttachmentUtilities.getAttachmentUri(att.mAccountKey, id).toString());
3163        StringBuilder sb = genSelect(getAttachmentMap(), uiProjection, values);
3164        sb.append(" FROM " + Attachment.TABLE_NAME + " WHERE " + AttachmentColumns.ID + " =? ");
3165        return sb.toString();
3166    }
3167
3168    /**
3169     * Generate the "subfolder list" SQLite query, given a projection from UnifiedEmail
3170     *
3171     * @param uiProjection as passed from UnifiedEmail
3172     * @return the SQLite query to be executed on the EmailProvider database
3173     */
3174    private String genQuerySubfolders(String[] uiProjection) {
3175        StringBuilder sb = genSelect(getFolderListMap(), uiProjection);
3176        sb.append(" FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.PARENT_KEY +
3177                " =? ORDER BY ");
3178        sb.append(MAILBOX_ORDER_BY);
3179        return sb.toString();
3180    }
3181
3182    private static final String COMBINED_ACCOUNT_ID_STRING = Long.toString(COMBINED_ACCOUNT_ID);
3183
3184    /**
3185     * Returns a cursor over all the folders for a specific URI which corresponds to a single
3186     * account.
3187     * @param uri
3188     * @param uiProjection
3189     * @return
3190     */
3191    private Cursor uiFolders(Uri uri, String[] uiProjection) {
3192        Context context = getContext();
3193        SQLiteDatabase db = getDatabase(context);
3194        String id = uri.getPathSegments().get(1);
3195        if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3196            MatrixCursor mc = new MatrixCursor(UIProvider.FOLDERS_PROJECTION, 2);
3197            Object[] row = getVirtualMailboxRow(COMBINED_ACCOUNT_ID, Mailbox.TYPE_INBOX);
3198            int numUnread = EmailContent.count(context, Message.CONTENT_URI,
3199                     MessageColumns.MAILBOX_KEY + " IN (SELECT " + MailboxColumns.ID +
3200                    " FROM " + Mailbox.TABLE_NAME + " WHERE " + MailboxColumns.TYPE +
3201                    "=" + Mailbox.TYPE_INBOX + ") AND " + MessageColumns.FLAG_READ + "=0", null);
3202            row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numUnread;
3203            mc.addRow(row);
3204            int numStarred = EmailContent.count(context, Message.CONTENT_URI,
3205                    MessageColumns.FLAG_FAVORITE + "=1", null);
3206            if (numStarred > 0) {
3207                row = getVirtualMailboxRow(COMBINED_ACCOUNT_ID, Mailbox.TYPE_STARRED);
3208                row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numStarred;
3209                mc.addRow(row);
3210            }
3211            return mc;
3212        } else {
3213            Cursor c = db.rawQuery(genQueryAccountMailboxes(uiProjection), new String[] {id});
3214            c = getFolderListCursor(db, c, uiProjection);
3215            int numStarred = EmailContent.count(context, Message.CONTENT_URI,
3216                    MessageColumns.ACCOUNT_KEY + "=? AND " + MessageColumns.FLAG_FAVORITE + "=1",
3217                    new String[] {id});
3218            if (numStarred == 0) {
3219                return c;
3220            } else {
3221                // Add starred virtual folder to the cursor
3222                // Show number of messages as unread count (for backward compatibility)
3223                MatrixCursor starCursor = new MatrixCursor(uiProjection, 1);
3224                Object[] row = getVirtualMailboxRow(Long.parseLong(id), Mailbox.TYPE_STARRED);
3225                row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numStarred;
3226                row[UIProvider.FOLDER_ICON_RES_ID_COLUMN] = R.drawable.ic_menu_star_holo_light;
3227                starCursor.addRow(row);
3228                Cursor[] cursors = new Cursor[] {starCursor, c};
3229                return new MergeCursor(cursors);
3230            }
3231        }
3232    }
3233
3234    /**
3235     * Returns an array of the default recent folders for a given URI which is unique for an
3236     * account. Some accounts might not have default recent folders, in which case an empty array
3237     * is returned.
3238     * @param id
3239     * @return
3240     */
3241    private Uri[] defaultRecentFolders(final String id) {
3242        final SQLiteDatabase db = getDatabase(getContext());
3243        if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3244            // We don't have default recents for the combined view.
3245            return new Uri[0];
3246        }
3247        // We search for the types we want, and find corresponding IDs.
3248        final String[] idAndType = { BaseColumns._ID, UIProvider.FolderColumns.TYPE };
3249
3250        // Sent, Drafts, and Starred are the default recents.
3251        final StringBuilder sb = genSelect(getFolderListMap(), idAndType);
3252        sb.append(" FROM " + Mailbox.TABLE_NAME
3253                + " WHERE " + MailboxColumns.ACCOUNT_KEY + " = " + id
3254                + " AND "
3255                + MailboxColumns.TYPE + " IN (" + Mailbox.TYPE_SENT +
3256                    ", " + Mailbox.TYPE_DRAFTS +
3257                    ", " + Mailbox.TYPE_STARRED
3258                + ")");
3259        LogUtils.d(TAG, "defaultRecentFolders: Query is %s", sb);
3260        final Cursor c = db.rawQuery(sb.toString(), null);
3261        if (c == null || c.getCount() <= 0 || !c.moveToFirst()) {
3262            return new Uri[0];
3263        }
3264        // Read all the IDs of the mailboxes, and turn them into URIs.
3265        final Uri[] recentFolders = new Uri[c.getCount()];
3266        int i = 0;
3267        do {
3268            final long folderId = c.getLong(0);
3269            recentFolders[i] = uiUri("uifolder", folderId);
3270            LogUtils.d(TAG, "Default recent folder: %d, with uri %s", folderId, recentFolders[i]);
3271            ++i;
3272        } while (c.moveToNext());
3273        return recentFolders;
3274    }
3275
3276    /**
3277     * Wrapper that handles the visibility feature (i.e. the conversation list is visible, so
3278     * any pending notifications for the corresponding mailbox should be canceled). We also handle
3279     * getExtras() to provide a snapshot of the mailbox's status
3280     */
3281    static class VisibilityCursor extends CursorWrapper {
3282        private final long mMailboxId;
3283        private final Context mContext;
3284        private final Bundle mExtras = new Bundle();
3285
3286        public VisibilityCursor(Context context, Cursor cursor, long mailboxId) {
3287            super(cursor);
3288            mMailboxId = mailboxId;
3289            mContext = context;
3290            Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
3291            if (mailbox != null) {
3292                mExtras.putInt(UIProvider.CursorExtraKeys.EXTRA_STATUS, mailbox.mUiSyncStatus);
3293                if (mailbox.mUiLastSyncResult != UIProvider.LastSyncResult.SUCCESS) {
3294                    mExtras.putInt(UIProvider.CursorExtraKeys.EXTRA_ERROR,
3295                            mailbox.mUiLastSyncResult);
3296                }
3297            }
3298        }
3299
3300        @Override
3301        public Bundle getExtras() {
3302            return mExtras;
3303        }
3304
3305        @Override
3306        public Bundle respond(Bundle params) {
3307            final String setVisibilityKey =
3308                    UIProvider.ConversationCursorCommand.COMMAND_KEY_SET_VISIBILITY;
3309            if (params.containsKey(setVisibilityKey)) {
3310                final boolean visible = params.getBoolean(setVisibilityKey);
3311                if (visible) {
3312                    NotificationController.getInstance(mContext).cancelNewMessageNotification(
3313                            mMailboxId);
3314                    // Clear the visible limit of the mailbox (if any) on entry
3315                    if (params.containsKey(
3316                            UIProvider.ConversationCursorCommand.COMMAND_KEY_ENTERED_FOLDER)) {
3317                        Mailbox mailbox = Mailbox.restoreMailboxWithId(mContext, mMailboxId);
3318                        if (mailbox != null && mailbox.mVisibleLimit > 0) {
3319                            ContentValues values = new ContentValues();
3320                            values.put(MailboxColumns.VISIBLE_LIMIT, 0);
3321                            mContext.getContentResolver().update(
3322                                    ContentUris.withAppendedId(Mailbox.CONTENT_URI, mMailboxId),
3323                                    values, null, null);
3324                        }
3325                    }
3326                }
3327            }
3328            // Return success
3329            Bundle response = new Bundle();
3330            response.putString(setVisibilityKey,
3331                    UIProvider.ConversationCursorCommand.COMMAND_RESPONSE_OK);
3332            return response;
3333        }
3334    }
3335
3336    static class AttachmentsCursor extends CursorWrapper {
3337        private final int mContentUriIndex;
3338        private final int mUriIndex;
3339        private final Context mContext;
3340
3341        public AttachmentsCursor(Context context, Cursor cursor) {
3342            super(cursor);
3343            mContentUriIndex = cursor.getColumnIndex(UIProvider.AttachmentColumns.CONTENT_URI);
3344            mUriIndex = cursor.getColumnIndex(UIProvider.AttachmentColumns.URI);
3345            mContext = context;
3346        }
3347
3348        @Override
3349        public String getString(int column) {
3350            if (column == mContentUriIndex) {
3351                Uri uri = Uri.parse(getString(mUriIndex));
3352                long id = Long.parseLong(uri.getLastPathSegment());
3353                Attachment att = Attachment.restoreAttachmentWithId(mContext, id);
3354                if (att == null) return "";
3355                return AttachmentUtilities.getAttachmentUri(att.mAccountKey, id).toString();
3356            } else {
3357                return super.getString(column);
3358            }
3359        }
3360    }
3361
3362    /**
3363     * For debugging purposes; shouldn't be used in production code
3364     */
3365    static class CloseDetectingCursor extends CursorWrapper {
3366
3367        public CloseDetectingCursor(Cursor cursor) {
3368            super(cursor);
3369        }
3370
3371        @Override
3372        public void close() {
3373            super.close();
3374            Log.d(TAG, "Closing cursor", new Error());
3375        }
3376    }
3377
3378    /**
3379     * We need to do individual queries for the mailboxes in order to get correct
3380     * folder capabilities.
3381     */
3382    Cursor getFolderListCursor(SQLiteDatabase db, Cursor c, String[] uiProjection) {
3383        final MatrixCursor mc = new MatrixCursor(uiProjection);
3384        Object[] values = new Object[uiProjection.length];
3385        String[] args = new String[1];
3386        try {
3387            // Loop through mailboxes, building matrix cursor
3388            while (c.moveToNext()) {
3389                String id = c.getString(0);
3390                args[0] = id;
3391                Cursor mailboxCursor = db.rawQuery(genQueryMailbox(uiProjection, id), args);
3392                if (mailboxCursor.moveToNext()) {
3393                    for (int i = 0; i < uiProjection.length; i++) {
3394                        values[i] = mailboxCursor.getString(i);
3395                    }
3396                    mc.addRow(values);
3397                }
3398            }
3399        } finally {
3400            c.close();
3401        }
3402       return mc;
3403    }
3404
3405    /**
3406     * Handle UnifiedEmail queries here (dispatched from query())
3407     *
3408     * @param match the UriMatcher match for the original uri passed in from UnifiedEmail
3409     * @param uri the original uri passed in from UnifiedEmail
3410     * @param uiProjection the projection passed in from UnifiedEmail
3411     * @return the result Cursor
3412     */
3413    private Cursor uiQuery(int match, Uri uri, String[] uiProjection) {
3414        Context context = getContext();
3415        ContentResolver resolver = context.getContentResolver();
3416        SQLiteDatabase db = getDatabase(context);
3417        // Should we ever return null, or throw an exception??
3418        Cursor c = null;
3419        String id = uri.getPathSegments().get(1);
3420        Uri notifyUri = null;
3421        switch(match) {
3422            case UI_ALL_FOLDERS:
3423                c = db.rawQuery(genQueryAccountAllMailboxes(uiProjection), new String[] {id});
3424                c = getFolderListCursor(db, c, uiProjection);
3425                break;
3426            case UI_RECENT_FOLDERS:
3427                c = db.rawQuery(genQueryRecentMailboxes(uiProjection), new String[] {id});
3428                notifyUri = UIPROVIDER_RECENT_FOLDERS_NOTIFIER.buildUpon().appendPath(id).build();
3429                break;
3430            case UI_SUBFOLDERS:
3431                c = db.rawQuery(genQuerySubfolders(uiProjection), new String[] {id});
3432                c = getFolderListCursor(db, c, uiProjection);
3433                break;
3434            case UI_MESSAGES:
3435                long mailboxId = Long.parseLong(id);
3436                if (isVirtualMailbox(mailboxId)) {
3437                    c = getVirtualMailboxMessagesCursor(db, uiProjection, mailboxId);
3438                } else {
3439                    c = db.rawQuery(genQueryMailboxMessages(uiProjection), new String[] {id});
3440                }
3441                notifyUri = UIPROVIDER_CONVERSATION_NOTIFIER.buildUpon().appendPath(id).build();
3442                c = new VisibilityCursor(context, c, mailboxId);
3443                break;
3444            case UI_MESSAGE:
3445                MessageQuery qq = genQueryViewMessage(uiProjection, id);
3446                String sql = qq.query;
3447                String attJson = qq.attachmentJson;
3448                // With attachments, we have another argument to bind
3449                if (attJson != null) {
3450                    c = db.rawQuery(sql, new String[] {attJson, id});
3451                } else {
3452                    c = db.rawQuery(sql, new String[] {id});
3453                }
3454                break;
3455            case UI_ATTACHMENTS:
3456                final List<String> contentTypeQueryParameters =
3457                        uri.getQueryParameters(PhotoContract.ContentTypeParameters.CONTENT_TYPE);
3458                c = db.rawQuery(genQueryAttachments(uiProjection, contentTypeQueryParameters),
3459                        new String[] {id});
3460                c = new AttachmentsCursor(context, c);
3461                notifyUri = UIPROVIDER_ATTACHMENTS_NOTIFIER.buildUpon().appendPath(id).build();
3462                break;
3463            case UI_ATTACHMENT:
3464                c = db.rawQuery(genQueryAttachment(uiProjection, id), new String[] {id});
3465                notifyUri = UIPROVIDER_ATTACHMENT_NOTIFIER.buildUpon().appendPath(id).build();
3466                break;
3467            case UI_FOLDER:
3468                mailboxId = Long.parseLong(id);
3469                if (isVirtualMailbox(mailboxId)) {
3470                    c = getVirtualMailboxCursor(mailboxId);
3471                } else {
3472                    c = db.rawQuery(genQueryMailbox(uiProjection, id), new String[] {id});
3473                    notifyUri = UIPROVIDER_FOLDER_NOTIFIER.buildUpon().appendPath(id).build();
3474                }
3475                break;
3476            case UI_ACCOUNT:
3477                if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
3478                    MatrixCursor mc = new MatrixCursor(uiProjection, 1);
3479                    addCombinedAccountRow(mc);
3480                    c = mc;
3481                } else {
3482                    c = db.rawQuery(genQueryAccount(uiProjection, id), new String[] {id});
3483                }
3484                notifyUri = UIPROVIDER_ACCOUNT_NOTIFIER.buildUpon().appendPath(id).build();
3485                break;
3486            case UI_CONVERSATION:
3487                c = db.rawQuery(genQueryConversation(uiProjection), new String[] {id});
3488                break;
3489        }
3490        if (notifyUri != null) {
3491            c.setNotificationUri(resolver, notifyUri);
3492        }
3493        return c;
3494    }
3495
3496    /**
3497     * Convert a UIProvider attachment to an EmailProvider attachment (for sending); we only need
3498     * a few of the fields
3499     * @param uiAtt the UIProvider attachment to convert
3500     * @return the EmailProvider attachment
3501     */
3502    private Attachment convertUiAttachmentToAttachment(
3503            com.android.mail.providers.Attachment uiAtt) {
3504        Attachment att = new Attachment();
3505        att.setContentUri(uiAtt.contentUri.toString());
3506        att.mFileName = uiAtt.name;
3507        att.mMimeType = uiAtt.contentType;
3508        att.mSize = uiAtt.size;
3509        return att;
3510    }
3511
3512    private String getMailboxNameForType(int mailboxType) {
3513        Context context = getContext();
3514        int resId;
3515        switch (mailboxType) {
3516            case Mailbox.TYPE_INBOX:
3517                resId = R.string.mailbox_name_server_inbox;
3518                break;
3519            case Mailbox.TYPE_OUTBOX:
3520                resId = R.string.mailbox_name_server_outbox;
3521                break;
3522            case Mailbox.TYPE_DRAFTS:
3523                resId = R.string.mailbox_name_server_drafts;
3524                break;
3525            case Mailbox.TYPE_TRASH:
3526                resId = R.string.mailbox_name_server_trash;
3527                break;
3528            case Mailbox.TYPE_SENT:
3529                resId = R.string.mailbox_name_server_sent;
3530                break;
3531            case Mailbox.TYPE_JUNK:
3532                resId = R.string.mailbox_name_server_junk;
3533                break;
3534            case Mailbox.TYPE_STARRED:
3535                resId = R.string.widget_starred;
3536                break;
3537            default:
3538                throw new IllegalArgumentException("Illegal mailbox type");
3539        }
3540        return context.getString(resId);
3541    }
3542
3543    /**
3544     * Create a mailbox given the account and mailboxType.
3545     */
3546    private Mailbox createMailbox(long accountId, int mailboxType) {
3547        Context context = getContext();
3548        Mailbox box = Mailbox.newSystemMailbox(accountId, mailboxType,
3549                getMailboxNameForType(mailboxType));
3550        // Make sure drafts and save will show up in recents...
3551        // If these already exist (from old Email app), they will have touch times
3552        switch (mailboxType) {
3553            case Mailbox.TYPE_DRAFTS:
3554                box.mLastTouchedTime = Mailbox.DRAFTS_DEFAULT_TOUCH_TIME;
3555                break;
3556            case Mailbox.TYPE_SENT:
3557                box.mLastTouchedTime = Mailbox.SENT_DEFAULT_TOUCH_TIME;
3558                break;
3559        }
3560        box.save(context);
3561        return box;
3562    }
3563
3564    /**
3565     * Given an account name and a mailbox type, return that mailbox, creating it if necessary
3566     * @param accountName the account name to use
3567     * @param mailboxType the type of mailbox we're trying to find
3568     * @return the mailbox of the given type for the account in the uri, or null if not found
3569     */
3570    private Mailbox getMailboxByAccountIdAndType(String accountId, int mailboxType) {
3571        long id = Long.parseLong(accountId);
3572        Mailbox mailbox = Mailbox.restoreMailboxOfType(getContext(), id, mailboxType);
3573        if (mailbox == null) {
3574            mailbox = createMailbox(id, mailboxType);
3575        }
3576        return mailbox;
3577    }
3578
3579    private Message getMessageFromPathSegments(List<String> pathSegments) {
3580        Message msg = null;
3581        if (pathSegments.size() > 2) {
3582            msg = Message.restoreMessageWithId(getContext(), Long.parseLong(pathSegments.get(2)));
3583        }
3584        if (msg == null) {
3585            msg = new Message();
3586        }
3587        return msg;
3588    }
3589    /**
3590     * Given a mailbox and the content values for a message, create/save the message in the mailbox
3591     * @param mailbox the mailbox to use
3592     * @param values the content values that represent message fields
3593     * @return the uri of the newly created message
3594     */
3595    private Uri uiSaveMessage(Message msg, Mailbox mailbox, ContentValues values) {
3596        Context context = getContext();
3597        // Fill in the message
3598        Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey);
3599        if (account == null) return null;
3600        msg.mFrom = account.mEmailAddress;
3601        msg.mTimeStamp = System.currentTimeMillis();
3602        msg.mTo = values.getAsString(UIProvider.MessageColumns.TO);
3603        msg.mCc = values.getAsString(UIProvider.MessageColumns.CC);
3604        msg.mBcc = values.getAsString(UIProvider.MessageColumns.BCC);
3605        msg.mSubject = values.getAsString(UIProvider.MessageColumns.SUBJECT);
3606        msg.mText = values.getAsString(UIProvider.MessageColumns.BODY_TEXT);
3607        msg.mHtml = values.getAsString(UIProvider.MessageColumns.BODY_HTML);
3608        msg.mMailboxKey = mailbox.mId;
3609        msg.mAccountKey = mailbox.mAccountKey;
3610        msg.mDisplayName = msg.mTo;
3611        msg.mFlagLoaded = Message.FLAG_LOADED_COMPLETE;
3612        msg.mFlagRead = true;
3613        Integer quoteStartPos = values.getAsInteger(UIProvider.MessageColumns.QUOTE_START_POS);
3614        msg.mQuotedTextStartPos = quoteStartPos == null ? 0 : quoteStartPos;
3615        int flags = 0;
3616        int draftType = values.getAsInteger(UIProvider.MessageColumns.DRAFT_TYPE);
3617        switch(draftType) {
3618            case DraftType.FORWARD:
3619                flags |= Message.FLAG_TYPE_FORWARD;
3620                break;
3621            case DraftType.REPLY_ALL:
3622                flags |= Message.FLAG_TYPE_REPLY_ALL;
3623                // Fall through
3624            case DraftType.REPLY:
3625                flags |= Message.FLAG_TYPE_REPLY;
3626                break;
3627            case DraftType.COMPOSE:
3628                flags |= Message.FLAG_TYPE_ORIGINAL;
3629                break;
3630        }
3631        int draftInfo = 0;
3632        if (values.containsKey(UIProvider.MessageColumns.QUOTE_START_POS)) {
3633            draftInfo = values.getAsInteger(UIProvider.MessageColumns.QUOTE_START_POS);
3634            if (values.getAsInteger(UIProvider.MessageColumns.APPEND_REF_MESSAGE_CONTENT) != 0) {
3635                draftInfo |= Message.DRAFT_INFO_APPEND_REF_MESSAGE;
3636            }
3637        }
3638        if (!values.containsKey(UIProvider.MessageColumns.APPEND_REF_MESSAGE_CONTENT)) {
3639            flags |= Message.FLAG_NOT_INCLUDE_QUOTED_TEXT;
3640        }
3641        msg.mDraftInfo = draftInfo;
3642        msg.mFlags = flags;
3643
3644        String ref = values.getAsString(UIProvider.MessageColumns.REF_MESSAGE_ID);
3645        if (ref != null && msg.mQuotedTextStartPos >= 0) {
3646            String refId = Uri.parse(ref).getLastPathSegment();
3647            try {
3648                long sourceKey = Long.parseLong(refId);
3649                msg.mSourceKey = sourceKey;
3650            } catch (NumberFormatException e) {
3651                // This will be zero; the default
3652            }
3653        }
3654
3655        // Get attachments from the ContentValues
3656        List<com.android.mail.providers.Attachment> uiAtts =
3657                com.android.mail.providers.Attachment.fromJSONArray(
3658                        values.getAsString(UIProvider.MessageColumns.JOINED_ATTACHMENT_INFOS));
3659        ArrayList<Attachment> atts = new ArrayList<Attachment>();
3660        boolean hasUnloadedAttachments = false;
3661        for (com.android.mail.providers.Attachment uiAtt: uiAtts) {
3662            Uri attUri = uiAtt.uri;
3663            if (attUri != null && attUri.getAuthority().equals(EmailContent.AUTHORITY)) {
3664                // If it's one of ours, retrieve the attachment and add it to the list
3665                long attId = Long.parseLong(attUri.getLastPathSegment());
3666                Attachment att = Attachment.restoreAttachmentWithId(context, attId);
3667                if (att != null) {
3668                    // We must clone the attachment into a new one for this message; easiest to
3669                    // use a parcel here
3670                    Parcel p = Parcel.obtain();
3671                    att.writeToParcel(p, 0);
3672                    p.setDataPosition(0);
3673                    Attachment attClone = new Attachment(p);
3674                    p.recycle();
3675                    // Clear the messageKey (this is going to be a new attachment)
3676                    attClone.mMessageKey = 0;
3677                    // If we're sending this, it's not loaded, and we're not smart forwarding
3678                    // add the download flag, so that ADS will start up
3679                    if (mailbox.mType == Mailbox.TYPE_OUTBOX && att.getContentUri() == null &&
3680                            ((account.mFlags & Account.FLAGS_SUPPORTS_SMART_FORWARD) == 0)) {
3681                        attClone.mFlags |= Attachment.FLAG_DOWNLOAD_FORWARD;
3682                        hasUnloadedAttachments = true;
3683                    }
3684                    atts.add(attClone);
3685                }
3686            } else {
3687                // Convert external attachment to one of ours and add to the list
3688                atts.add(convertUiAttachmentToAttachment(uiAtt));
3689            }
3690        }
3691        if (!atts.isEmpty()) {
3692            msg.mAttachments = atts;
3693            msg.mFlagAttachment = true;
3694            if (hasUnloadedAttachments) {
3695                Utility.showToast(context, R.string.message_view_attachment_background_load);
3696            }
3697        }
3698        // Save it or update it...
3699        if (!msg.isSaved()) {
3700            msg.save(context);
3701        } else {
3702            // This is tricky due to how messages/attachments are saved; rather than putz with
3703            // what's changed, we'll delete/re-add them
3704            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
3705            // Delete all existing attachments
3706            ops.add(ContentProviderOperation.newDelete(
3707                    ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, msg.mId))
3708                    .build());
3709            // Delete the body
3710            ops.add(ContentProviderOperation.newDelete(Body.CONTENT_URI)
3711                    .withSelection(Body.MESSAGE_KEY + "=?", new String[] {Long.toString(msg.mId)})
3712                    .build());
3713            // Add the ops for the message, atts, and body
3714            msg.addSaveOps(ops);
3715            // Do it!
3716            try {
3717                applyBatch(ops);
3718            } catch (OperationApplicationException e) {
3719            }
3720        }
3721        if (mailbox.mType == Mailbox.TYPE_OUTBOX) {
3722            EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
3723                    mServiceCallback, mailbox.mAccountKey);
3724            try {
3725                service.startSync(mailbox.mId, true);
3726            } catch (RemoteException e) {
3727            }
3728            long originalMsgId = msg.mSourceKey;
3729            if (originalMsgId != 0) {
3730                Message originalMsg = Message.restoreMessageWithId(context, originalMsgId);
3731                // If the original message exists, set its forwarded/replied to flags
3732                if (originalMsg != null) {
3733                    ContentValues cv = new ContentValues();
3734                    flags = originalMsg.mFlags;
3735                    switch(draftType) {
3736                        case DraftType.FORWARD:
3737                            flags |= Message.FLAG_FORWARDED;
3738                            break;
3739                        case DraftType.REPLY_ALL:
3740                        case DraftType.REPLY:
3741                            flags |= Message.FLAG_REPLIED_TO;
3742                            break;
3743                    }
3744                    cv.put(Message.FLAGS, flags);
3745                    context.getContentResolver().update(ContentUris.withAppendedId(
3746                            Message.CONTENT_URI, originalMsgId), cv, null, null);
3747                }
3748            }
3749        }
3750        return uiUri("uimessage", msg.mId);
3751    }
3752
3753    /**
3754     * Create and send the message via the account indicated in the uri
3755     * @param uri the incoming uri
3756     * @param values the content values that represent message fields
3757     * @return the uri of the created message
3758     */
3759    private Uri uiSendMail(Uri uri, ContentValues values) {
3760        List<String> pathSegments = uri.getPathSegments();
3761        Mailbox mailbox = getMailboxByAccountIdAndType(pathSegments.get(1), Mailbox.TYPE_OUTBOX);
3762        if (mailbox == null) return null;
3763        Message msg = getMessageFromPathSegments(pathSegments);
3764        try {
3765            return uiSaveMessage(msg, mailbox, values);
3766        } finally {
3767            // Kick observers
3768            getContext().getContentResolver().notifyChange(Mailbox.CONTENT_URI, null);
3769        }
3770    }
3771
3772    /**
3773     * Create a message and save it to the drafts folder of the account indicated in the uri
3774     * @param uri the incoming uri
3775     * @param values the content values that represent message fields
3776     * @return the uri of the created message
3777     */
3778    private Uri uiSaveDraft(Uri uri, ContentValues values) {
3779        List<String> pathSegments = uri.getPathSegments();
3780        Mailbox mailbox = getMailboxByAccountIdAndType(pathSegments.get(1), Mailbox.TYPE_DRAFTS);
3781        if (mailbox == null) return null;
3782        Message msg = getMessageFromPathSegments(pathSegments);
3783        return uiSaveMessage(msg, mailbox, values);
3784    }
3785
3786    private int uiUpdateDraft(Uri uri, ContentValues values) {
3787        Context context = getContext();
3788        Message msg = Message.restoreMessageWithId(context,
3789                Long.parseLong(uri.getPathSegments().get(1)));
3790        if (msg == null) return 0;
3791        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
3792        if (mailbox == null) return 0;
3793        uiSaveMessage(msg, mailbox, values);
3794        return 1;
3795    }
3796
3797    private int uiSendDraft(Uri uri, ContentValues values) {
3798        Context context = getContext();
3799        Message msg = Message.restoreMessageWithId(context,
3800                Long.parseLong(uri.getPathSegments().get(1)));
3801        if (msg == null) return 0;
3802        long mailboxId = Mailbox.findMailboxOfType(context, msg.mAccountKey, Mailbox.TYPE_OUTBOX);
3803        if (mailboxId == Mailbox.NO_MAILBOX) return 0;
3804        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, mailboxId);
3805        if (mailbox == null) return 0;
3806        uiSaveMessage(msg, mailbox, values);
3807        // Kick observers
3808        context.getContentResolver().notifyChange(Mailbox.CONTENT_URI, null);
3809        return 1;
3810    }
3811
3812    private void putIntegerLongOrBoolean(ContentValues values, String columnName, Object value) {
3813        if (value instanceof Integer) {
3814            Integer intValue = (Integer)value;
3815            values.put(columnName, intValue);
3816        } else if (value instanceof Boolean) {
3817            Boolean boolValue = (Boolean)value;
3818            values.put(columnName, boolValue ? 1 : 0);
3819        } else if (value instanceof Long) {
3820            Long longValue = (Long)value;
3821            values.put(columnName, longValue);
3822        }
3823    }
3824
3825    /**
3826     * Update the timestamps for the folders specified and notifies on the recent folder URI.
3827     * @param folders
3828     * @return number of folders updated
3829     */
3830    private int updateTimestamp(final Context context, String id, Uri[] folders){
3831        int updated = 0;
3832        final long now = System.currentTimeMillis();
3833        final ContentResolver resolver = context.getContentResolver();
3834        final ContentValues touchValues = new ContentValues();
3835        for (int i=0, size=folders.length; i < size; ++i) {
3836            touchValues.put(MailboxColumns.LAST_TOUCHED_TIME, now);
3837            LogUtils.d(TAG, "updateStamp: %s updated", folders[i]);
3838            updated += resolver.update(folders[i], touchValues, null, null);
3839        }
3840        final Uri toNotify =
3841                UIPROVIDER_RECENT_FOLDERS_NOTIFIER.buildUpon().appendPath(id).build();
3842        LogUtils.d(TAG, "updateTimestamp: Notifying on %s", toNotify);
3843        resolver.notifyChange(toNotify, null);
3844        return updated;
3845    }
3846
3847    /**
3848     * Updates the recent folders. The values to be updated are specified as ContentValues pairs
3849     * of (Folder URI, access timestamp). Returns nonzero if successful, always.
3850     * @param uri
3851     * @param values
3852     * @return nonzero value always.
3853     */
3854    private int uiUpdateRecentFolders(Uri uri, ContentValues values) {
3855        final int numFolders = values.size();
3856        final String id = uri.getPathSegments().get(1);
3857        final Uri[] folders = new Uri[numFolders];
3858        final Context context = getContext();
3859        final NotificationController controller = NotificationController.getInstance(context);
3860        int i = 0;
3861        for (final String uriString: values.keySet()) {
3862            folders[i] = Uri.parse(uriString);
3863            try {
3864                final String mailboxIdString = folders[i].getLastPathSegment();
3865                final long mailboxId = Long.parseLong(mailboxIdString);
3866                controller.cancelNewMessageNotification(mailboxId);
3867            } catch (NumberFormatException e) {
3868                // Keep on going...
3869            }
3870        }
3871        return updateTimestamp(context, id, folders);
3872    }
3873
3874    /**
3875     * Populates the recent folders according to the design.
3876     * @param uri
3877     * @return the number of recent folders were populated.
3878     */
3879    private int uiPopulateRecentFolders(Uri uri) {
3880        final Context context = getContext();
3881        final String id = uri.getLastPathSegment();
3882        final Uri[] recentFolders = defaultRecentFolders(id);
3883        final int numFolders = recentFolders.length;
3884        if (numFolders <= 0) {
3885            return 0;
3886        }
3887        final int rowsUpdated = updateTimestamp(context, id, recentFolders);
3888        LogUtils.d(TAG, "uiPopulateRecentFolders: %d folders changed", rowsUpdated);
3889        return rowsUpdated;
3890    }
3891
3892    private int uiUpdateAttachment(Uri uri, ContentValues uiValues) {
3893        Integer stateValue = uiValues.getAsInteger(UIProvider.AttachmentColumns.STATE);
3894        if (stateValue != null) {
3895            // This is a command from UIProvider
3896            long attachmentId = Long.parseLong(uri.getLastPathSegment());
3897            Context context = getContext();
3898            Attachment attachment =
3899                    Attachment.restoreAttachmentWithId(context, attachmentId);
3900            if (attachment == null) {
3901                // Went away; ah, well...
3902                return 0;
3903            }
3904            ContentValues values = new ContentValues();
3905            switch (stateValue.intValue()) {
3906                case UIProvider.AttachmentState.NOT_SAVED:
3907                    // Set state, try to cancel request
3908                    values.put(AttachmentColumns.UI_STATE, stateValue);
3909                    values.put(AttachmentColumns.FLAGS,
3910                            attachment.mFlags &= ~Attachment.FLAG_DOWNLOAD_USER_REQUEST);
3911                    attachment.update(context, values);
3912                    return 1;
3913                case UIProvider.AttachmentState.DOWNLOADING:
3914                    // Set state and destination; request download
3915                    values.put(AttachmentColumns.UI_STATE, stateValue);
3916                    Integer destinationValue =
3917                        uiValues.getAsInteger(UIProvider.AttachmentColumns.DESTINATION);
3918                    values.put(AttachmentColumns.UI_DESTINATION,
3919                            destinationValue == null ? 0 : destinationValue);
3920                    values.put(AttachmentColumns.FLAGS,
3921                            attachment.mFlags | Attachment.FLAG_DOWNLOAD_USER_REQUEST);
3922                    attachment.update(context, values);
3923                    return 1;
3924                case UIProvider.AttachmentState.SAVED:
3925                    // If this is an inline attachment, notify message has changed
3926                    if (!TextUtils.isEmpty(attachment.mContentId)) {
3927                        notifyUI(UIPROVIDER_MESSAGE_NOTIFIER, attachment.mMessageKey);
3928                    }
3929                    return 1;
3930            }
3931        }
3932        return 0;
3933    }
3934
3935    private int uiUpdateFolder(Uri uri, ContentValues uiValues) {
3936        Uri ourUri = convertToEmailProviderUri(uri, Mailbox.CONTENT_URI, true);
3937        if (ourUri == null) return 0;
3938        ContentValues ourValues = new ContentValues();
3939        // This should only be called via update to "recent folders"
3940        for (String columnName: uiValues.keySet()) {
3941            if (columnName.equals(MailboxColumns.LAST_TOUCHED_TIME)) {
3942                ourValues.put(MailboxColumns.LAST_TOUCHED_TIME, uiValues.getAsLong(columnName));
3943            }
3944        }
3945        return update(ourUri, ourValues, null, null);
3946    }
3947
3948    private ContentValues convertUiMessageValues(Message message, ContentValues values) {
3949        ContentValues ourValues = new ContentValues();
3950        for (String columnName : values.keySet()) {
3951            Object val = values.get(columnName);
3952            if (columnName.equals(UIProvider.ConversationColumns.STARRED)) {
3953                putIntegerLongOrBoolean(ourValues, MessageColumns.FLAG_FAVORITE, val);
3954            } else if (columnName.equals(UIProvider.ConversationColumns.READ)) {
3955                putIntegerLongOrBoolean(ourValues, MessageColumns.FLAG_READ, val);
3956            } else if (columnName.equals(MessageColumns.MAILBOX_KEY)) {
3957                putIntegerLongOrBoolean(ourValues, MessageColumns.MAILBOX_KEY, val);
3958            } else if (columnName.equals(UIProvider.ConversationColumns.RAW_FOLDERS)) {
3959                // Convert from folder list uri to mailbox key
3960                ArrayList<Folder> folders = Folder.getFoldersArray((String) val);
3961                if (folders == null || folders.size() == 0 || folders.size() > 1) {
3962                    LogUtils.d(TAG,
3963                            "Incorrect number of folders for this message: Message is %s",
3964                            message.mId);
3965                } else {
3966                    Folder f = folders.get(0);
3967                    Uri uri = f.uri;
3968                    Long mailboxId = Long.parseLong(uri.getLastPathSegment());
3969                    putIntegerLongOrBoolean(ourValues, MessageColumns.MAILBOX_KEY, mailboxId);
3970                }
3971            } else if (columnName.equals(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES)) {
3972                Address[] fromList = Address.unpack(message.mFrom);
3973                Preferences prefs = Preferences.getPreferences(getContext());
3974                for (Address sender : fromList) {
3975                    String email = sender.getAddress();
3976                    prefs.setSenderAsTrusted(email);
3977                }
3978            } else if (columnName.equals(UIProvider.ConversationColumns.VIEWED)) {
3979                // Ignore for now
3980            } else {
3981                throw new IllegalArgumentException("Can't update " + columnName + " in message");
3982            }
3983        }
3984        return ourValues;
3985    }
3986
3987    private Uri convertToEmailProviderUri(Uri uri, Uri newBaseUri, boolean asProvider) {
3988        String idString = uri.getLastPathSegment();
3989        try {
3990            long id = Long.parseLong(idString);
3991            Uri ourUri = ContentUris.withAppendedId(newBaseUri, id);
3992            if (asProvider) {
3993                ourUri = ourUri.buildUpon().appendQueryParameter(IS_UIPROVIDER, "true").build();
3994            }
3995            return ourUri;
3996        } catch (NumberFormatException e) {
3997            return null;
3998        }
3999    }
4000
4001    private Message getMessageFromLastSegment(Uri uri) {
4002        long messageId = Long.parseLong(uri.getLastPathSegment());
4003        return Message.restoreMessageWithId(getContext(), messageId);
4004    }
4005
4006    /**
4007     * Add an undo operation for the current sequence; if the sequence is newer than what we've had,
4008     * clear out the undo list and start over
4009     * @param uri the uri we're working on
4010     * @param op the ContentProviderOperation to perform upon undo
4011     */
4012    private void addToSequence(Uri uri, ContentProviderOperation op) {
4013        String sequenceString = uri.getQueryParameter(UIProvider.SEQUENCE_QUERY_PARAMETER);
4014        if (sequenceString != null) {
4015            int sequence = Integer.parseInt(sequenceString);
4016            if (sequence > mLastSequence) {
4017                // Reset sequence
4018                mLastSequenceOps.clear();
4019                mLastSequence = sequence;
4020            }
4021            // TODO: Need something to indicate a change isn't ready (undoable)
4022            mLastSequenceOps.add(op);
4023        }
4024    }
4025
4026    // TODO: This should depend on flags on the mailbox...
4027    private boolean uploadsToServer(Context context, Mailbox m) {
4028        if (m.mType == Mailbox.TYPE_DRAFTS || m.mType == Mailbox.TYPE_OUTBOX ||
4029                m.mType == Mailbox.TYPE_SEARCH) {
4030            return false;
4031        }
4032        String protocol = Account.getProtocol(context, m.mAccountKey);
4033        EmailServiceInfo info = EmailServiceUtils.getServiceInfo(context, protocol);
4034        return (info != null && info.syncChanges);
4035    }
4036
4037    private int uiUpdateMessage(Uri uri, ContentValues values) {
4038        return uiUpdateMessage(uri, values, false);
4039    }
4040
4041    private int uiUpdateMessage(Uri uri, ContentValues values, boolean forceSync) {
4042        Context context = getContext();
4043        Message msg = getMessageFromLastSegment(uri);
4044        if (msg == null) return 0;
4045        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
4046        if (mailbox == null) return 0;
4047        Uri ourBaseUri =
4048                (forceSync || uploadsToServer(context, mailbox)) ? Message.SYNCED_CONTENT_URI :
4049                    Message.CONTENT_URI;
4050        Uri ourUri = convertToEmailProviderUri(uri, ourBaseUri, true);
4051        if (ourUri == null) return 0;
4052
4053        // Special case - meeting response
4054        if (values.containsKey(UIProvider.MessageOperations.RESPOND_COLUMN)) {
4055            EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
4056                    mServiceCallback, mailbox.mAccountKey);
4057            try {
4058                service.sendMeetingResponse(msg.mId,
4059                        values.getAsInteger(UIProvider.MessageOperations.RESPOND_COLUMN));
4060                // Delete the message immediately
4061                uiDeleteMessage(uri);
4062                Utility.showToast(context, R.string.confirm_response);
4063                // Notify box has changed so the deletion is reflected in the UI
4064                notifyUIConversationMailbox(mailbox.mId);
4065            } catch (RemoteException e) {
4066            }
4067            return 1;
4068        }
4069
4070        ContentValues undoValues = new ContentValues();
4071        ContentValues ourValues = convertUiMessageValues(msg, values);
4072        for (String columnName: ourValues.keySet()) {
4073            if (columnName.equals(MessageColumns.MAILBOX_KEY)) {
4074                undoValues.put(MessageColumns.MAILBOX_KEY, msg.mMailboxKey);
4075            } else if (columnName.equals(MessageColumns.FLAG_READ)) {
4076                undoValues.put(MessageColumns.FLAG_READ, msg.mFlagRead);
4077            } else if (columnName.equals(MessageColumns.FLAG_FAVORITE)) {
4078                undoValues.put(MessageColumns.FLAG_FAVORITE, msg.mFlagFavorite);
4079            }
4080        }
4081        if (undoValues == null || undoValues.size() == 0) {
4082            return -1;
4083        }
4084        ContentProviderOperation op =
4085                ContentProviderOperation.newUpdate(convertToEmailProviderUri(
4086                        uri, ourBaseUri, false))
4087                        .withValues(undoValues)
4088                        .build();
4089        addToSequence(uri, op);
4090        return update(ourUri, ourValues, null, null);
4091    }
4092
4093    public static final String PICKER_UI_ACCOUNT = "picker_ui_account";
4094    public static final String PICKER_MAILBOX_TYPE = "picker_mailbox_type";
4095    public static final String PICKER_MESSAGE_ID = "picker_message_id";
4096    public static final String PICKER_HEADER_ID = "picker_header_id";
4097
4098    private int uiDeleteMessage(Uri uri) {
4099        final Context context = getContext();
4100        Message msg = getMessageFromLastSegment(uri);
4101        if (msg == null) return 0;
4102        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, msg.mMailboxKey);
4103        if (mailbox == null) return 0;
4104        if (mailbox.mType == Mailbox.TYPE_TRASH || mailbox.mType == Mailbox.TYPE_DRAFTS) {
4105            // We actually delete these, including attachments
4106            AttachmentUtilities.deleteAllAttachmentFiles(context, msg.mAccountKey, msg.mId);
4107            notifyUI(UIPROVIDER_FOLDER_NOTIFIER, mailbox.mId);
4108            return context.getContentResolver().delete(
4109                    ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg.mId), null, null);
4110        }
4111        Mailbox trashMailbox =
4112                Mailbox.restoreMailboxOfType(context, msg.mAccountKey, Mailbox.TYPE_TRASH);
4113        if (trashMailbox == null) {
4114            return 0;
4115        }
4116        ContentValues values = new ContentValues();
4117        values.put(MessageColumns.MAILBOX_KEY, trashMailbox.mId);
4118        notifyUI(UIPROVIDER_FOLDER_NOTIFIER, mailbox.mId);
4119        return uiUpdateMessage(uri, values, true);
4120    }
4121
4122    private int pickFolder(Uri uri, int type, int headerId) {
4123        Context context = getContext();
4124        Long acctId = Long.parseLong(uri.getLastPathSegment());
4125        // For push imap, for example, we want the user to select the trash mailbox
4126        Cursor ac = query(uiUri("uiaccount", acctId), UIProvider.ACCOUNTS_PROJECTION,
4127                null, null, null);
4128        try {
4129            if (ac.moveToFirst()) {
4130                final com.android.mail.providers.Account uiAccount =
4131                        new com.android.mail.providers.Account(ac);
4132                Intent intent = new Intent(context, FolderPickerActivity.class);
4133                intent.putExtra(PICKER_UI_ACCOUNT, uiAccount);
4134                intent.putExtra(PICKER_MAILBOX_TYPE, type);
4135                intent.putExtra(PICKER_HEADER_ID, headerId);
4136                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
4137                context.startActivity(intent);
4138                return 1;
4139            }
4140            return 0;
4141        } finally {
4142            ac.close();
4143        }
4144    }
4145
4146    private int pickTrashFolder(Uri uri) {
4147        return pickFolder(uri, Mailbox.TYPE_TRASH, R.string.trash_folder_selection_title);
4148    }
4149
4150    private int pickSentFolder(Uri uri) {
4151        return pickFolder(uri, Mailbox.TYPE_SENT, R.string.sent_folder_selection_title);
4152    }
4153
4154    private Cursor uiUndo(String[] projection) {
4155        // First see if we have any operations saved
4156        // TODO: Make sure seq matches
4157        if (!mLastSequenceOps.isEmpty()) {
4158            try {
4159                // TODO Always use this projection?  Or what's passed in?
4160                // Not sure if UI wants it, but I'm making a cursor of convo uri's
4161                MatrixCursor c = new MatrixCursor(
4162                        new String[] {UIProvider.ConversationColumns.URI},
4163                        mLastSequenceOps.size());
4164                for (ContentProviderOperation op: mLastSequenceOps) {
4165                    c.addRow(new String[] {op.getUri().toString()});
4166                }
4167                // Just apply the batch and we're done!
4168                applyBatch(mLastSequenceOps);
4169                // But clear the operations
4170                mLastSequenceOps.clear();
4171                // Tell the UI there are changes
4172                ContentResolver resolver = getContext().getContentResolver();
4173                resolver.notifyChange(UIPROVIDER_CONVERSATION_NOTIFIER, null);
4174                resolver.notifyChange(UIPROVIDER_FOLDER_NOTIFIER, null);
4175                return c;
4176            } catch (OperationApplicationException e) {
4177            }
4178        }
4179        return new MatrixCursor(projection, 0);
4180    }
4181
4182    private void notifyUIConversation(Uri uri) {
4183        String id = uri.getLastPathSegment();
4184        Message msg = Message.restoreMessageWithId(getContext(), Long.parseLong(id));
4185        if (msg != null) {
4186            notifyUIConversationMailbox(msg.mMailboxKey);
4187        }
4188    }
4189
4190    /**
4191     * Notify about the Mailbox id passed in
4192     * @param id the Mailbox id to be notified
4193     */
4194    private void notifyUIConversationMailbox(long id) {
4195        notifyUI(UIPROVIDER_CONVERSATION_NOTIFIER, Long.toString(id));
4196        Mailbox mailbox = Mailbox.restoreMailboxWithId(getContext(), id);
4197        if (mailbox == null) {
4198            Log.w(TAG, "No mailbox for notification: " + id);
4199            return;
4200        }
4201        // Notify combined inbox...
4202        if (mailbox.mType == Mailbox.TYPE_INBOX) {
4203            notifyUI(UIPROVIDER_CONVERSATION_NOTIFIER,
4204                    EmailProvider.combinedMailboxId(Mailbox.TYPE_INBOX));
4205        }
4206        notifyWidgets(id);
4207    }
4208
4209    private void notifyUI(Uri uri, String id) {
4210        Uri notifyUri = uri.buildUpon().appendPath(id).build();
4211        getContext().getContentResolver().notifyChange(notifyUri, null);
4212    }
4213
4214    private void notifyUI(Uri uri, long id) {
4215        notifyUI(uri, Long.toString(id));
4216    }
4217
4218    /**
4219     * Support for services and service notifications
4220     */
4221
4222    private final IEmailServiceCallback.Stub mServiceCallback =
4223            new IEmailServiceCallback.Stub() {
4224
4225        @Override
4226        public void syncMailboxListStatus(long accountId, int statusCode, int progress)
4227                throws RemoteException {
4228        }
4229
4230        @Override
4231        public void syncMailboxStatus(long mailboxId, int statusCode, int progress)
4232                throws RemoteException {
4233            // We'll get callbacks here from the services, which we'll pass back to the UI
4234            Uri uri = ContentUris.withAppendedId(FOLDER_STATUS_URI, mailboxId);
4235            EmailProvider.this.getContext().getContentResolver().notifyChange(uri, null);
4236        }
4237
4238        @Override
4239        public void loadAttachmentStatus(long messageId, long attachmentId, int statusCode,
4240                int progress) throws RemoteException {
4241        }
4242
4243        @Override
4244        public void sendMessageStatus(long messageId, String subject, int statusCode, int progress)
4245                throws RemoteException {
4246        }
4247
4248        @Override
4249        public void loadMessageStatus(long messageId, int statusCode, int progress)
4250                throws RemoteException {
4251        }
4252    };
4253
4254    private Cursor uiFolderRefresh(Uri uri) {
4255        Context context = getContext();
4256        String idString = uri.getLastPathSegment();
4257        long id = Long.parseLong(idString);
4258        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
4259        if (mailbox == null) return null;
4260        EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
4261                mServiceCallback, mailbox.mAccountKey);
4262        try {
4263            service.startSync(id, true);
4264        } catch (RemoteException e) {
4265        }
4266        return null;
4267    }
4268
4269    //Number of additional messages to load when a user selects "Load more..." in POP/IMAP boxes
4270    public static final int VISIBLE_LIMIT_INCREMENT = 10;
4271    //Number of additional messages to load when a user selects "Load more..." in a search
4272    public static final int SEARCH_MORE_INCREMENT = 10;
4273
4274    private Cursor uiFolderLoadMore(Uri uri) {
4275        Context context = getContext();
4276        String idString = uri.getLastPathSegment();
4277        long id = Long.parseLong(idString);
4278        Mailbox mailbox = Mailbox.restoreMailboxWithId(context, id);
4279        if (mailbox == null) return null;
4280        if (mailbox.mType == Mailbox.TYPE_SEARCH) {
4281            // Ask for 10 more messages
4282            mSearchParams.mOffset += SEARCH_MORE_INCREMENT;
4283            runSearchQuery(context, mailbox.mAccountKey, id);
4284        } else {
4285            ContentValues values = new ContentValues();
4286            values.put(EmailContent.FIELD_COLUMN_NAME, MailboxColumns.VISIBLE_LIMIT);
4287            values.put(EmailContent.ADD_COLUMN_NAME, VISIBLE_LIMIT_INCREMENT);
4288            Uri mailboxUri = ContentUris.withAppendedId(Mailbox.ADD_TO_FIELD_URI, id);
4289            // Increase the limit
4290            context.getContentResolver().update(mailboxUri, values, null, null);
4291            // And order a refresh
4292            uiFolderRefresh(uri);
4293        }
4294        return null;
4295    }
4296
4297    private static final String SEARCH_MAILBOX_SERVER_ID = "__search_mailbox__";
4298    private SearchParams mSearchParams;
4299
4300    /**
4301     * Returns the search mailbox for the specified account, creating one if necessary
4302     * @return the search mailbox for the passed in account
4303     */
4304    private Mailbox getSearchMailbox(long accountId) {
4305        Context context = getContext();
4306        Mailbox m = Mailbox.restoreMailboxOfType(context, accountId, Mailbox.TYPE_SEARCH);
4307        if (m == null) {
4308            m = new Mailbox();
4309            m.mAccountKey = accountId;
4310            m.mServerId = SEARCH_MAILBOX_SERVER_ID;
4311            m.mFlagVisible = false;
4312            m.mDisplayName = SEARCH_MAILBOX_SERVER_ID;
4313            m.mSyncInterval = Mailbox.CHECK_INTERVAL_NEVER;
4314            m.mType = Mailbox.TYPE_SEARCH;
4315            m.mFlags = Mailbox.FLAG_HOLDS_MAIL;
4316            m.mParentKey = Mailbox.NO_MAILBOX;
4317            m.save(context);
4318        }
4319        return m;
4320    }
4321
4322    private void runSearchQuery(final Context context, final long accountId,
4323            final long searchMailboxId) {
4324        // Start the search running in the background
4325        new Thread(new Runnable() {
4326            @Override
4327            public void run() {
4328                 try {
4329                    EmailServiceProxy service = EmailServiceUtils.getServiceForAccount(context,
4330                            mServiceCallback, accountId);
4331                    if (service != null) {
4332                        try {
4333                            // Save away the total count
4334                            mSearchParams.mTotalCount = service.searchMessages(accountId,
4335                                    mSearchParams, searchMailboxId);
4336                            //Log.d(TAG, "TotalCount to UI: " + mSearchParams.mTotalCount);
4337                            notifyUI(UIPROVIDER_FOLDER_NOTIFIER, searchMailboxId);
4338                        } catch (RemoteException e) {
4339                            Log.e("searchMessages", "RemoteException", e);
4340                        }
4341                    }
4342                } finally {
4343                }
4344            }}).start();
4345
4346    }
4347
4348    // TODO: Handle searching for more...
4349    private Cursor uiSearch(Uri uri, String[] projection) {
4350        final long accountId = Long.parseLong(uri.getLastPathSegment());
4351
4352        // TODO: Check the actual mailbox
4353        Mailbox inbox = Mailbox.restoreMailboxOfType(getContext(), accountId, Mailbox.TYPE_INBOX);
4354        if (inbox == null) {
4355            Log.w(Logging.LOG_TAG, "In uiSearch, inbox doesn't exist for account " + accountId);
4356            return null;
4357        }
4358
4359        String filter = uri.getQueryParameter(UIProvider.SearchQueryParameters.QUERY);
4360        if (filter == null) {
4361            throw new IllegalArgumentException("No query parameter in search query");
4362        }
4363
4364        // Find/create our search mailbox
4365        Mailbox searchMailbox = getSearchMailbox(accountId);
4366        final long searchMailboxId = searchMailbox.mId;
4367
4368        mSearchParams = new SearchParams(inbox.mId, filter, searchMailboxId);
4369
4370        final Context context = getContext();
4371        if (mSearchParams.mOffset == 0) {
4372            // Delete existing contents of search mailbox
4373            ContentResolver resolver = context.getContentResolver();
4374            resolver.delete(Message.CONTENT_URI, Message.MAILBOX_KEY + "=" + searchMailboxId,
4375                    null);
4376            ContentValues cv = new ContentValues();
4377            // For now, use the actual query as the name of the mailbox
4378            cv.put(Mailbox.DISPLAY_NAME, mSearchParams.mFilter);
4379            resolver.update(ContentUris.withAppendedId(Mailbox.CONTENT_URI, searchMailboxId),
4380                    cv, null, null);
4381        }
4382
4383        // Start the search running in the background
4384        runSearchQuery(context, accountId, searchMailboxId);
4385
4386        // This will look just like a "normal" folder
4387        return uiQuery(UI_FOLDER, ContentUris.withAppendedId(Mailbox.CONTENT_URI,
4388                searchMailbox.mId), projection);
4389    }
4390
4391    private static final String MAILBOXES_FOR_ACCOUNT_SELECTION = MailboxColumns.ACCOUNT_KEY + "=?";
4392    private static final String MAILBOXES_FOR_ACCOUNT_EXCEPT_ACCOUNT_MAILBOX_SELECTION =
4393        MAILBOXES_FOR_ACCOUNT_SELECTION + " AND " + MailboxColumns.TYPE + "!=" +
4394        Mailbox.TYPE_EAS_ACCOUNT_MAILBOX;
4395    private static final String MESSAGES_FOR_ACCOUNT_SELECTION = MessageColumns.ACCOUNT_KEY + "=?";
4396
4397    /**
4398     * Delete an account and clean it up
4399     */
4400    private int uiDeleteAccount(Uri uri) {
4401        Context context = getContext();
4402        long accountId = Long.parseLong(uri.getLastPathSegment());
4403        try {
4404            // Get the account URI.
4405            final Account account = Account.restoreAccountWithId(context, accountId);
4406            if (account == null) {
4407                return 0; // Already deleted?
4408            }
4409
4410            deleteAccountData(context, accountId);
4411
4412            // Now delete the account itself
4413            uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId);
4414            context.getContentResolver().delete(uri, null, null);
4415
4416            // Clean up
4417            AccountBackupRestore.backup(context);
4418            SecurityPolicy.getInstance(context).reducePolicies();
4419            MailActivityEmail.setServicesEnabledSync(context);
4420            return 1;
4421        } catch (Exception e) {
4422            Log.w(Logging.LOG_TAG, "Exception while deleting account", e);
4423        }
4424        return 0;
4425    }
4426
4427    private int uiDeleteAccountData(Uri uri) {
4428        Context context = getContext();
4429        long accountId = Long.parseLong(uri.getLastPathSegment());
4430        // Get the account URI.
4431        final Account account = Account.restoreAccountWithId(context, accountId);
4432        if (account == null) {
4433            return 0; // Already deleted?
4434        }
4435        deleteAccountData(context, accountId);
4436        return 1;
4437    }
4438
4439    private void deleteAccountData(Context context, long accountId) {
4440        // Delete synced attachments
4441        AttachmentUtilities.deleteAllAccountAttachmentFiles(context, accountId);
4442
4443        // Delete synced email, leaving only an empty inbox.  We do this in two phases:
4444        // 1. Delete all non-inbox mailboxes (which will delete all of their messages)
4445        // 2. Delete all remaining messages (which will be the inbox messages)
4446        ContentResolver resolver = context.getContentResolver();
4447        String[] accountIdArgs = new String[] { Long.toString(accountId) };
4448        resolver.delete(Mailbox.CONTENT_URI,
4449                MAILBOXES_FOR_ACCOUNT_EXCEPT_ACCOUNT_MAILBOX_SELECTION,
4450                accountIdArgs);
4451        resolver.delete(Message.CONTENT_URI, MESSAGES_FOR_ACCOUNT_SELECTION, accountIdArgs);
4452
4453        // Delete sync keys on remaining items
4454        ContentValues cv = new ContentValues();
4455        cv.putNull(Account.SYNC_KEY);
4456        resolver.update(Account.CONTENT_URI, cv, Account.ID_SELECTION, accountIdArgs);
4457        cv.clear();
4458        cv.putNull(Mailbox.SYNC_KEY);
4459        resolver.update(Mailbox.CONTENT_URI, cv,
4460                MAILBOXES_FOR_ACCOUNT_SELECTION, accountIdArgs);
4461
4462        // Delete PIM data (contacts, calendar), stop syncs, etc. if applicable
4463        IEmailService service = EmailServiceUtils.getServiceForAccount(context, null, accountId);
4464        if (service != null) {
4465            try {
4466                service.deleteAccountPIMData(accountId);
4467            } catch (RemoteException e) {
4468                // Can't do anything about this
4469            }
4470        }
4471    }
4472
4473    private int[] mSavedWidgetIds = new int[0];
4474    private ArrayList<Long> mWidgetNotifyMailboxes = new ArrayList<Long>();
4475    private AppWidgetManager mAppWidgetManager;
4476    private ComponentName mEmailComponent;
4477
4478    private void notifyWidgets(long mailboxId) {
4479        Context context = getContext();
4480        // Lazily initialize these
4481        if (mAppWidgetManager == null) {
4482            mAppWidgetManager = AppWidgetManager.getInstance(context);
4483            mEmailComponent = new ComponentName(context, WidgetProvider.PROVIDER_NAME);
4484        }
4485
4486        // See if we have to populate our array of mailboxes used in widgets
4487        int[] widgetIds = mAppWidgetManager.getAppWidgetIds(mEmailComponent);
4488        if (!Arrays.equals(widgetIds, mSavedWidgetIds)) {
4489            mSavedWidgetIds = widgetIds;
4490            String[][] widgetInfos = BaseWidgetProvider.getWidgetInfo(context, widgetIds);
4491            // widgetInfo now has pairs of account uri/folder uri
4492            mWidgetNotifyMailboxes.clear();
4493            for (String[] widgetInfo: widgetInfos) {
4494                try {
4495                    if (widgetInfo == null) continue;
4496                    long id = Long.parseLong(Uri.parse(widgetInfo[1]).getLastPathSegment());
4497                    if (!isCombinedMailbox(id)) {
4498                        // For a regular mailbox, just add it to the list
4499                        if (!mWidgetNotifyMailboxes.contains(id)) {
4500                            mWidgetNotifyMailboxes.add(id);
4501                        }
4502                    } else {
4503                        switch (getVirtualMailboxType(id)) {
4504                            // We only handle the combined inbox in widgets
4505                            case Mailbox.TYPE_INBOX:
4506                                Cursor c = query(Mailbox.CONTENT_URI, Mailbox.ID_PROJECTION,
4507                                        MailboxColumns.TYPE + "=?",
4508                                        new String[] {Integer.toString(Mailbox.TYPE_INBOX)}, null);
4509                                try {
4510                                    while (c.moveToNext()) {
4511                                        mWidgetNotifyMailboxes.add(
4512                                                c.getLong(Mailbox.ID_PROJECTION_COLUMN));
4513                                    }
4514                                } finally {
4515                                    c.close();
4516                                }
4517                                break;
4518                        }
4519                    }
4520                } catch (NumberFormatException e) {
4521                    // Move along
4522                }
4523            }
4524        }
4525
4526        // If our mailbox needs to be notified, do so...
4527        if (mWidgetNotifyMailboxes.contains(mailboxId)) {
4528            Intent intent = new Intent(Utils.ACTION_NOTIFY_DATASET_CHANGED);
4529            intent.putExtra(Utils.EXTRA_FOLDER_URI, uiUri("uifolder", mailboxId));
4530            intent.setType(EMAIL_APP_MIME_TYPE);
4531            context.sendBroadcast(intent);
4532         }
4533    }
4534
4535    public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
4536        Context context = getContext();
4537        writer.println("Installed services:");
4538        for (EmailServiceInfo info: EmailServiceUtils.getServiceInfoList(context)) {
4539            writer.println("  " + info);
4540        }
4541        writer.println();
4542        writer.println("Accounts: ");
4543        Cursor cursor = query(Account.CONTENT_URI, Account.CONTENT_PROJECTION, null, null, null);
4544        if (cursor.getCount() == 0) {
4545            writer.println("  None");
4546        }
4547        try {
4548            while (cursor.moveToNext()) {
4549                Account account = new Account();
4550                account.restore(cursor);
4551                writer.println("  Account " + account.mDisplayName);
4552                HostAuth hostAuth =
4553                        HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv);
4554                if (hostAuth != null) {
4555                    writer.println("    Protocol = " + hostAuth.mProtocol +
4556                            (TextUtils.isEmpty(account.mProtocolVersion) ? "" : " version " +
4557                                    account.mProtocolVersion));
4558                }
4559            }
4560        } finally {
4561            cursor.close();
4562        }
4563    }
4564}
4565