EmailProvider.java revision 8587aa61211d288d05b5fb2ddf02d69cabe6a9e2
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 com.android.email.Email;
20import com.android.email.provider.EmailContent.Account;
21import com.android.email.provider.EmailContent.AccountColumns;
22import com.android.email.provider.EmailContent.Attachment;
23import com.android.email.provider.EmailContent.AttachmentColumns;
24import com.android.email.provider.EmailContent.Body;
25import com.android.email.provider.EmailContent.BodyColumns;
26import com.android.email.provider.EmailContent.HostAuth;
27import com.android.email.provider.EmailContent.HostAuthColumns;
28import com.android.email.provider.EmailContent.Mailbox;
29import com.android.email.provider.EmailContent.MailboxColumns;
30import com.android.email.provider.EmailContent.Message;
31import com.android.email.provider.EmailContent.MessageColumns;
32import com.android.email.provider.EmailContent.SyncColumns;
33import com.android.exchange.Eas;
34
35import android.accounts.AccountManager;
36import android.content.ContentProvider;
37import android.content.ContentProviderOperation;
38import android.content.ContentProviderResult;
39import android.content.ContentUris;
40import android.content.ContentValues;
41import android.content.Context;
42import android.content.OperationApplicationException;
43import android.content.UriMatcher;
44import android.database.Cursor;
45import android.database.SQLException;
46import android.database.sqlite.SQLiteDatabase;
47import android.database.sqlite.SQLiteOpenHelper;
48import android.net.Uri;
49import android.util.Log;
50
51import java.util.ArrayList;
52
53public class EmailProvider extends ContentProvider {
54
55    private static final String TAG = "EmailProvider";
56
57    static final String DATABASE_NAME = "EmailProvider.db";
58    static final String BODY_DATABASE_NAME = "EmailProviderBody.db";
59
60    // Any changes to the database format *must* include update-in-place code.
61    // Original version: 3
62    // Version 4: Database wipe required; changing AccountManager interface w/Exchange
63    public static final int DATABASE_VERSION = 5;
64
65    // Any changes to the database format *must* include update-in-place code.
66    // Original version: 2
67    // Version 3: Add "sourceKey" column
68    // Version 4: Database wipe required; changing AccountManager interface w/Exchange
69    public static final int BODY_DATABASE_VERSION = 5;
70
71    public static final String EMAIL_AUTHORITY = "com.android.email.provider";
72
73    private static final int ACCOUNT_BASE = 0;
74    private static final int ACCOUNT = ACCOUNT_BASE;
75    private static final int ACCOUNT_MAILBOXES = ACCOUNT_BASE + 1;
76    private static final int ACCOUNT_ID = ACCOUNT_BASE + 2;
77    private static final int ACCOUNT_ID_ADD_TO_FIELD = ACCOUNT_BASE + 3;
78
79    private static final int MAILBOX_BASE = 0x1000;
80    private static final int MAILBOX = MAILBOX_BASE;
81    private static final int MAILBOX_MESSAGES = MAILBOX_BASE + 1;
82    private static final int MAILBOX_ID = MAILBOX_BASE + 2;
83    private static final int MAILBOX_ID_ADD_TO_FIELD = MAILBOX_BASE + 3;
84
85    private static final int MESSAGE_BASE = 0x2000;
86    private static final int MESSAGE = MESSAGE_BASE;
87    private static final int MESSAGE_ID = MESSAGE_BASE + 1;
88    private static final int SYNCED_MESSAGE_ID = MESSAGE_BASE + 2;
89
90    private static final int ATTACHMENT_BASE = 0x3000;
91    private static final int ATTACHMENT = ATTACHMENT_BASE;
92    private static final int ATTACHMENT_CONTENT = ATTACHMENT_BASE + 1;
93    private static final int ATTACHMENT_ID = ATTACHMENT_BASE + 2;
94    private static final int ATTACHMENTS_MESSAGE_ID = ATTACHMENT_BASE + 3;
95
96    private static final int HOSTAUTH_BASE = 0x4000;
97    private static final int HOSTAUTH = HOSTAUTH_BASE;
98    private static final int HOSTAUTH_ID = HOSTAUTH_BASE + 1;
99
100    private static final int UPDATED_MESSAGE_BASE = 0x5000;
101    private static final int UPDATED_MESSAGE = UPDATED_MESSAGE_BASE;
102    private static final int UPDATED_MESSAGE_ID = UPDATED_MESSAGE_BASE + 1;
103
104    private static final int DELETED_MESSAGE_BASE = 0x6000;
105    private static final int DELETED_MESSAGE = DELETED_MESSAGE_BASE;
106    private static final int DELETED_MESSAGE_ID = DELETED_MESSAGE_BASE + 1;
107    private static final int DELETED_MESSAGE_MAILBOX = DELETED_MESSAGE_BASE + 2;
108
109    // MUST ALWAYS EQUAL THE LAST OF THE PREVIOUS BASE CONSTANTS
110    private static final int LAST_EMAIL_PROVIDER_DB_BASE = DELETED_MESSAGE_BASE;
111
112    // DO NOT CHANGE BODY_BASE!!
113    private static final int BODY_BASE = LAST_EMAIL_PROVIDER_DB_BASE + 0x1000;
114    private static final int BODY = BODY_BASE;
115    private static final int BODY_ID = BODY_BASE + 1;
116    private static final int BODY_MESSAGE_ID = BODY_BASE + 2;
117    private static final int BODY_HTML = BODY_BASE + 3;
118    private static final int BODY_TEXT = BODY_BASE + 4;
119
120
121    private static final int BASE_SHIFT = 12;  // 12 bits to the base type: 0, 0x1000, 0x2000, etc.
122
123    private static final String[] TABLE_NAMES = {
124        EmailContent.Account.TABLE_NAME,
125        EmailContent.Mailbox.TABLE_NAME,
126        EmailContent.Message.TABLE_NAME,
127        EmailContent.Attachment.TABLE_NAME,
128        EmailContent.HostAuth.TABLE_NAME,
129        EmailContent.Message.UPDATED_TABLE_NAME,
130        EmailContent.Message.DELETED_TABLE_NAME,
131        EmailContent.Body.TABLE_NAME
132    };
133
134    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
135
136    /**
137     * Let's only generate these SQL strings once, as they are used frequently
138     * Note that this isn't relevant for table creation strings, since they are used only once
139     */
140    private static final String UPDATED_MESSAGE_INSERT = "insert or ignore into " +
141        Message.UPDATED_TABLE_NAME + " select * from " + Message.TABLE_NAME + " where " +
142        EmailContent.RECORD_ID + '=';
143
144    private static final String UPDATED_MESSAGE_DELETE = "delete from " +
145        Message.UPDATED_TABLE_NAME + " where " + EmailContent.RECORD_ID + '=';
146
147    private static final String DELETED_MESSAGE_INSERT = "insert or replace into " +
148        Message.DELETED_TABLE_NAME + " select * from " + Message.TABLE_NAME + " where " +
149        EmailContent.RECORD_ID + '=';
150
151    private static final String DELETE_ORPHAN_BODIES = "delete from " + Body.TABLE_NAME +
152        " where " + BodyColumns.MESSAGE_KEY + " in " + "(select " + BodyColumns.MESSAGE_KEY +
153        " from " + Body.TABLE_NAME + " except select " + EmailContent.RECORD_ID + " from " +
154        Message.TABLE_NAME + ')';
155
156    private static final String DELETE_BODY = "delete from " + Body.TABLE_NAME +
157        " where " + BodyColumns.MESSAGE_KEY + '=';
158
159    private static final String ID_EQUALS = EmailContent.RECORD_ID + "=?";
160
161    static {
162        // Email URI matching table
163        UriMatcher matcher = sURIMatcher;
164
165        // All accounts
166        matcher.addURI(EMAIL_AUTHORITY, "account", ACCOUNT);
167        // A specific account
168        // insert into this URI causes a mailbox to be added to the account
169        matcher.addURI(EMAIL_AUTHORITY, "account/#", ACCOUNT_ID);
170        // The mailboxes in a specific account
171        matcher.addURI(EMAIL_AUTHORITY, "account/#/mailbox", ACCOUNT_MAILBOXES);
172
173        // All mailboxes
174        matcher.addURI(EMAIL_AUTHORITY, "mailbox", MAILBOX);
175        // A specific mailbox
176        // insert into this URI causes a message to be added to the mailbox
177        // ** NOTE For now, the accountKey must be set manually in the values!
178        matcher.addURI(EMAIL_AUTHORITY, "mailbox/#", MAILBOX_ID);
179        // The messages in a specific mailbox
180        matcher.addURI(EMAIL_AUTHORITY, "mailbox/#/message", MAILBOX_MESSAGES);
181
182        // All messages
183        matcher.addURI(EMAIL_AUTHORITY, "message", MESSAGE);
184        // A specific message
185        // insert into this URI causes an attachment to be added to the message
186        matcher.addURI(EMAIL_AUTHORITY, "message/#", MESSAGE_ID);
187
188        // A specific attachment
189        matcher.addURI(EMAIL_AUTHORITY, "attachment", ATTACHMENT);
190        // A specific attachment (the header information)
191        matcher.addURI(EMAIL_AUTHORITY, "attachment/#", ATTACHMENT_ID);
192        // The content for a specific attachment
193        // NOT IMPLEMENTED
194        matcher.addURI(EMAIL_AUTHORITY, "attachment/content/*", ATTACHMENT_CONTENT);
195        // The attachments of a specific message (query only) (insert & delete TBD)
196        matcher.addURI(EMAIL_AUTHORITY, "attachment/message/#", ATTACHMENTS_MESSAGE_ID);
197
198        // All mail bodies
199        matcher.addURI(EMAIL_AUTHORITY, "body", BODY);
200        // A specific mail body
201        matcher.addURI(EMAIL_AUTHORITY, "body/#", BODY_ID);
202        // The body for a specific message
203        matcher.addURI(EMAIL_AUTHORITY, "body/message/#", BODY_MESSAGE_ID);
204        // The HTML part of a specific mail body
205        matcher.addURI(EMAIL_AUTHORITY, "body/#/html", BODY_HTML);
206        // The plain text part of a specific mail body
207        matcher.addURI(EMAIL_AUTHORITY, "body/#/text", BODY_TEXT);
208
209        // All hostauth records
210        matcher.addURI(EMAIL_AUTHORITY, "hostauth", HOSTAUTH);
211        // A specific hostauth
212        matcher.addURI(EMAIL_AUTHORITY, "hostauth/#", HOSTAUTH_ID);
213
214        // Atomically a constant value to a particular field of a mailbox/account
215        matcher.addURI(EMAIL_AUTHORITY, "mailboxIdAddToField/#", MAILBOX_ID_ADD_TO_FIELD);
216        matcher.addURI(EMAIL_AUTHORITY, "accountIdAddToField/#", ACCOUNT_ID_ADD_TO_FIELD);
217
218        /**
219         * THIS URI HAS SPECIAL SEMANTICS
220         * ITS USE IS INTENDED FOR THE UI APPLICATION TO MARK CHANGES THAT NEED TO BE SYNCED BACK
221         * TO A SERVER VIA A SYNC ADAPTER
222         */
223        matcher.addURI(EMAIL_AUTHORITY, "syncedMessage/#", SYNCED_MESSAGE_ID);
224
225        /**
226         * THE URIs BELOW THIS POINT ARE INTENDED TO BE USED BY SYNC ADAPTERS ONLY
227         * THEY REFER TO DATA CREATED AND MAINTAINED BY CALLS TO THE SYNCED_MESSAGE_ID URI
228         * BY THE UI APPLICATION
229         */
230        // All deleted messages
231        matcher.addURI(EMAIL_AUTHORITY, "deletedMessage", DELETED_MESSAGE);
232        // A specific deleted message
233        matcher.addURI(EMAIL_AUTHORITY, "deletedMessage/#", DELETED_MESSAGE_ID);
234        // All deleted messages from a specific mailbox
235        // NOT IMPLEMENTED; do we need this as a convenience?
236        matcher.addURI(EMAIL_AUTHORITY, "deletedMessage/mailbox/#", DELETED_MESSAGE_MAILBOX);
237
238        // All updated messages
239        matcher.addURI(EMAIL_AUTHORITY, "updatedMessage", UPDATED_MESSAGE);
240        // A specific updated message
241        matcher.addURI(EMAIL_AUTHORITY, "updatedMessage/#", UPDATED_MESSAGE_ID);
242    }
243
244    /*
245     * Internal helper method for index creation.
246     * Example:
247     * "create index message_" + MessageColumns.FLAG_READ
248     * + " on " + Message.TABLE_NAME + " (" + MessageColumns.FLAG_READ + ");"
249     */
250    /* package */
251    static String createIndex(String tableName, String columnName) {
252        return "create index " + tableName.toLowerCase() + '_' + columnName
253            + " on " + tableName + " (" + columnName + ");";
254    }
255
256    static void createMessageTable(SQLiteDatabase db) {
257        String messageColumns = MessageColumns.DISPLAY_NAME + " text, "
258            + MessageColumns.TIMESTAMP + " integer, "
259            + MessageColumns.SUBJECT + " text, "
260            + MessageColumns.FLAG_READ + " integer, "
261            + MessageColumns.FLAG_LOADED + " integer, "
262            + MessageColumns.FLAG_FAVORITE + " integer, "
263            + MessageColumns.FLAG_ATTACHMENT + " integer, "
264            + MessageColumns.FLAGS + " integer, "
265            + MessageColumns.CLIENT_ID + " integer, "
266            + MessageColumns.MESSAGE_ID + " text, "
267            + MessageColumns.MAILBOX_KEY + " integer, "
268            + MessageColumns.ACCOUNT_KEY + " integer, "
269            + MessageColumns.FROM_LIST + " text, "
270            + MessageColumns.TO_LIST + " text, "
271            + MessageColumns.CC_LIST + " text, "
272            + MessageColumns.BCC_LIST + " text, "
273            + MessageColumns.REPLY_TO_LIST + " text"
274            + ");";
275
276        // This String and the following String MUST have the same columns, except for the type
277        // of those columns!
278        String createString = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
279            + SyncColumns.SERVER_ID + " integer, "
280            + messageColumns;
281
282        // For the updated and deleted tables, the id is assigned, but we do want to keep track
283        // of the ORDER of updates using an autoincrement primary key.  We use the DATA column
284        // at this point; it has no other function
285        String altCreateString = " (" + EmailContent.RECORD_ID + " integer unique, "
286            + SyncColumns.SERVER_ID + " integer, "
287            + messageColumns;
288
289        // The three tables have the same schema
290        db.execSQL("create table " + Message.TABLE_NAME + createString);
291        db.execSQL("create table " + Message.UPDATED_TABLE_NAME + altCreateString);
292        db.execSQL("create table " + Message.DELETED_TABLE_NAME + altCreateString);
293
294        String indexColumns[] = {
295            MessageColumns.TIMESTAMP,
296            MessageColumns.FLAG_READ,
297            MessageColumns.FLAG_LOADED,
298            MessageColumns.MAILBOX_KEY,
299            SyncColumns.SERVER_ID
300        };
301
302        for (String columnName : indexColumns) {
303            db.execSQL(createIndex(Message.TABLE_NAME, columnName));
304        }
305
306        // Deleting a Message deletes all associated Attachments
307        // Deleting the associated Body cannot be done in a trigger, because the Body is stored
308        // in a separate database, and trigger cannot operate on attached databases.
309        db.execSQL("create trigger message_delete before delete on " + Message.TABLE_NAME +
310                " begin delete from " + Attachment.TABLE_NAME +
311                "  where " + AttachmentColumns.MESSAGE_KEY + "=old." + EmailContent.RECORD_ID +
312                "; end");
313
314        // Add triggers to keep unread count accurate per mailbox
315
316        // Insert a message; if flagRead is zero, add to the unread count of the message's mailbox
317        db.execSQL("create trigger unread_message_insert before insert on " + Message.TABLE_NAME +
318                " when NEW." + MessageColumns.FLAG_READ + "=0" +
319                " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
320                '=' + MailboxColumns.UNREAD_COUNT + "+1" +
321                "  where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY +
322                "; end");
323
324        // Delete a message; if flagRead is zero, decrement the unread count of the msg's mailbox
325        db.execSQL("create trigger unread_message_delete before delete on " + Message.TABLE_NAME +
326                " when OLD." + MessageColumns.FLAG_READ + "=0" +
327                " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
328                '=' + MailboxColumns.UNREAD_COUNT + "-1" +
329                "  where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY +
330                "; end");
331
332        // Change a message's mailbox
333        db.execSQL("create trigger unread_message_move before update of " +
334                MessageColumns.MAILBOX_KEY + " on " + Message.TABLE_NAME +
335                " when OLD." + MessageColumns.FLAG_READ + "=0" +
336                " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
337                '=' + MailboxColumns.UNREAD_COUNT + "-1" +
338                "  where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY +
339                "; update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
340                '=' + MailboxColumns.UNREAD_COUNT + "+1" +
341                " where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY +
342                "; end");
343
344        // Change a message's read state
345        db.execSQL("create trigger unread_message_read before update of " +
346                MessageColumns.FLAG_READ + " on " + Message.TABLE_NAME +
347                " when OLD." + MessageColumns.FLAG_READ + "!=NEW." + MessageColumns.FLAG_READ +
348                " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
349                '=' + MailboxColumns.UNREAD_COUNT + "+ case OLD." + MessageColumns.FLAG_READ +
350                " when 0 then -1 else 1 end" +
351                "  where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY +
352                "; end");
353   }
354
355    static void resetMessageTable(SQLiteDatabase db, int oldVersion, int newVersion) {
356        try {
357            db.execSQL("drop table " + Message.TABLE_NAME);
358            db.execSQL("drop table " + Message.UPDATED_TABLE_NAME);
359            db.execSQL("drop table " + Message.DELETED_TABLE_NAME);
360        } catch (SQLException e) {
361        }
362        createMessageTable(db);
363    }
364
365    static void createAccountTable(SQLiteDatabase db) {
366        String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
367            + AccountColumns.DISPLAY_NAME + " text, "
368            + AccountColumns.EMAIL_ADDRESS + " text, "
369            + AccountColumns.SYNC_KEY + " text, "
370            + AccountColumns.SYNC_LOOKBACK + " integer, "
371            + AccountColumns.SYNC_INTERVAL + " text, "
372            + AccountColumns.HOST_AUTH_KEY_RECV + " integer, "
373            + AccountColumns.HOST_AUTH_KEY_SEND + " integer, "
374            + AccountColumns.FLAGS + " integer, "
375            + AccountColumns.IS_DEFAULT + " integer, "
376            + AccountColumns.COMPATIBILITY_UUID + " text, "
377            + AccountColumns.SENDER_NAME + " text, "
378            + AccountColumns.RINGTONE_URI + " text, "
379            + AccountColumns.PROTOCOL_VERSION + " text, "
380            + AccountColumns.NEW_MESSAGE_COUNT + " integer"
381            + ");";
382        db.execSQL("create table " + Account.TABLE_NAME + s);
383        // Deleting an account deletes associated Mailboxes and HostAuth's
384        db.execSQL("create trigger account_delete before delete on " + Account.TABLE_NAME +
385                " begin delete from " + Mailbox.TABLE_NAME +
386                " where " + MailboxColumns.ACCOUNT_KEY + "=old." + EmailContent.RECORD_ID +
387                "; delete from " + HostAuth.TABLE_NAME +
388                " where " + EmailContent.RECORD_ID + "=old." + AccountColumns.HOST_AUTH_KEY_RECV +
389                "; delete from " + HostAuth.TABLE_NAME +
390                " where " + EmailContent.RECORD_ID + "=old." + AccountColumns.HOST_AUTH_KEY_SEND +
391        "; end");
392    }
393
394    static void resetAccountTable(SQLiteDatabase db, int oldVersion, int newVersion) {
395        try {
396            db.execSQL("drop table " +  Account.TABLE_NAME);
397        } catch (SQLException e) {
398        }
399        createAccountTable(db);
400    }
401
402    static void createHostAuthTable(SQLiteDatabase db) {
403        String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
404            + HostAuthColumns.PROTOCOL + " text, "
405            + HostAuthColumns.ADDRESS + " text, "
406            + HostAuthColumns.PORT + " integer, "
407            + HostAuthColumns.FLAGS + " integer, "
408            + HostAuthColumns.LOGIN + " text, "
409            + HostAuthColumns.PASSWORD + " text, "
410            + HostAuthColumns.DOMAIN + " text, "
411            + HostAuthColumns.ACCOUNT_KEY + " integer"
412            + ");";
413        db.execSQL("create table " + HostAuth.TABLE_NAME + s);
414    }
415
416    static void resetHostAuthTable(SQLiteDatabase db, int oldVersion, int newVersion) {
417        try {
418            db.execSQL("drop table " + HostAuth.TABLE_NAME);
419        } catch (SQLException e) {
420        }
421        createHostAuthTable(db);
422    }
423
424    static void createMailboxTable(SQLiteDatabase db) {
425        String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
426            + MailboxColumns.DISPLAY_NAME + " text, "
427            + MailboxColumns.SERVER_ID + " text, "
428            + MailboxColumns.PARENT_SERVER_ID + " text, "
429            + MailboxColumns.ACCOUNT_KEY + " integer, "
430            + MailboxColumns.TYPE + " integer, "
431            + MailboxColumns.DELIMITER + " integer, "
432            + MailboxColumns.SYNC_KEY + " text, "
433            + MailboxColumns.SYNC_LOOKBACK + " integer, "
434            + MailboxColumns.SYNC_INTERVAL + " integer, "
435            + MailboxColumns.SYNC_TIME + " integer, "
436            + MailboxColumns.UNREAD_COUNT + " integer, "
437            + MailboxColumns.FLAG_VISIBLE + " integer, "
438            + MailboxColumns.FLAGS + " integer, "
439            + MailboxColumns.VISIBLE_LIMIT + " integer, "
440            + MailboxColumns.SYNC_STATUS + " text"
441            + ");";
442        db.execSQL("create table " + Mailbox.TABLE_NAME + s);
443        db.execSQL("create index mailbox_" + MailboxColumns.SERVER_ID
444                + " on " + Mailbox.TABLE_NAME + " (" + MailboxColumns.SERVER_ID + ")");
445        db.execSQL("create index mailbox_" + MailboxColumns.ACCOUNT_KEY
446                + " on " + Mailbox.TABLE_NAME + " (" + MailboxColumns.ACCOUNT_KEY + ")");
447        // Deleting a Mailbox deletes associated Messages
448        db.execSQL("create trigger mailbox_delete before delete on " + Mailbox.TABLE_NAME +
449                " begin delete from " + Message.TABLE_NAME +
450                "  where " + MessageColumns.MAILBOX_KEY + "=old." + EmailContent.RECORD_ID +
451                "; end");
452    }
453
454    static void resetMailboxTable(SQLiteDatabase db, int oldVersion, int newVersion) {
455        try {
456            db.execSQL("drop table " + Mailbox.TABLE_NAME);
457        } catch (SQLException e) {
458        }
459        createMailboxTable(db);
460    }
461
462    static void createAttachmentTable(SQLiteDatabase db) {
463        String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
464            + AttachmentColumns.FILENAME + " text, "
465            + AttachmentColumns.MIME_TYPE + " text, "
466            + AttachmentColumns.SIZE + " integer, "
467            + AttachmentColumns.CONTENT_ID + " text, "
468            + AttachmentColumns.CONTENT_URI + " text, "
469            + AttachmentColumns.MESSAGE_KEY + " integer, "
470            + AttachmentColumns.LOCATION + " text, "
471            + AttachmentColumns.ENCODING + " text"
472            + ");";
473        db.execSQL("create table " + Attachment.TABLE_NAME + s);
474        db.execSQL(createIndex(Attachment.TABLE_NAME, AttachmentColumns.MESSAGE_KEY));
475    }
476
477    static void resetAttachmentTable(SQLiteDatabase db, int oldVersion, int newVersion) {
478        try {
479            db.execSQL("drop table " + Attachment.TABLE_NAME);
480        } catch (SQLException e) {
481        }
482        createAttachmentTable(db);
483    }
484
485    static void createBodyTable(SQLiteDatabase db) {
486        String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
487            + BodyColumns.MESSAGE_KEY + " integer, "
488            + BodyColumns.HTML_CONTENT + " text, "
489            + BodyColumns.TEXT_CONTENT + " text, "
490            + BodyColumns.HTML_REPLY + " text, "
491            + BodyColumns.TEXT_REPLY + " text, "
492            + BodyColumns.SOURCE_MESSAGE_KEY + " text"
493            + ");";
494        db.execSQL("create table " + Body.TABLE_NAME + s);
495        db.execSQL(createIndex(Body.TABLE_NAME, BodyColumns.MESSAGE_KEY));
496    }
497
498    static void upgradeBodyTable(SQLiteDatabase db, int oldVersion, int newVersion) {
499        if (oldVersion < 5) {
500            try {
501                db.execSQL("drop table " + Body.TABLE_NAME);
502                createBodyTable(db);
503            } catch (SQLException e) {
504            }
505        }
506    }
507
508    private final int mDatabaseVersion = DATABASE_VERSION;
509    private final int mBodyDatabaseVersion = BODY_DATABASE_VERSION;
510
511    private SQLiteDatabase mDatabase;
512    private SQLiteDatabase mBodyDatabase;
513
514    public synchronized SQLiteDatabase getDatabase(Context context) {
515        if (mDatabase !=  null) {
516            return mDatabase;
517        }
518        DatabaseHelper helper = new DatabaseHelper(context, DATABASE_NAME);
519        mDatabase = helper.getWritableDatabase();
520        if (mDatabase != null) {
521            mDatabase.setLockingEnabled(true);
522            BodyDatabaseHelper bodyHelper = new BodyDatabaseHelper(context, BODY_DATABASE_NAME);
523            mBodyDatabase = bodyHelper.getWritableDatabase();
524            if (mBodyDatabase != null) {
525                mBodyDatabase.setLockingEnabled(true);
526                String bodyFileName = mBodyDatabase.getPath();
527                mDatabase.execSQL("attach \"" + bodyFileName + "\" as BodyDatabase");
528            }
529        }
530        return mDatabase;
531    }
532
533    private class BodyDatabaseHelper extends SQLiteOpenHelper {
534        BodyDatabaseHelper(Context context, String name) {
535            super(context, name, null, mBodyDatabaseVersion);
536        }
537
538        @Override
539        public void onCreate(SQLiteDatabase db) {
540            // Create all tables here; each class has its own method
541            createBodyTable(db);
542        }
543
544        @Override
545        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
546            upgradeBodyTable(db, oldVersion, newVersion);
547        }
548
549        @Override
550        public void onOpen(SQLiteDatabase db) {
551        }
552    }
553
554    private class DatabaseHelper extends SQLiteOpenHelper {
555        Context mContext;
556
557        DatabaseHelper(Context context, String name) {
558            super(context, name, null, mDatabaseVersion);
559            mContext = context;
560        }
561
562        @Override
563        public void onCreate(SQLiteDatabase db) {
564            // Create all tables here; each class has its own method
565            createMessageTable(db);
566            createAttachmentTable(db);
567            createMailboxTable(db);
568            createHostAuthTable(db);
569            createAccountTable(db);
570        }
571
572        @Override
573        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
574            // For versions prior to 5, delete all data
575            // Versions >= 5 require that data be preserved!
576            if (oldVersion < 5) {
577                android.accounts.Account[] accounts =
578                    AccountManager.get(mContext).getAccountsByType(Eas.ACCOUNT_MANAGER_TYPE);
579                for (android.accounts.Account account: accounts) {
580                    AccountManager.get(mContext).removeAccount(account, null, null);
581                }
582                resetMessageTable(db, oldVersion, newVersion);
583                resetAttachmentTable(db, oldVersion, newVersion);
584                resetMailboxTable(db, oldVersion, newVersion);
585                resetHostAuthTable(db, oldVersion, newVersion);
586                resetAccountTable(db, oldVersion, newVersion);
587            }
588        }
589
590        @Override
591        public void onOpen(SQLiteDatabase db) {
592        }
593    }
594
595    @Override
596    public int delete(Uri uri, String selection, String[] selectionArgs) {
597        final int match = sURIMatcher.match(uri);
598        Context context = getContext();
599        // Pick the correct database for this operation
600        // If we're in a transaction already (which would happen during applyBatch), then the
601        // body database is already attached to the email database and any attempt to use the
602        // body database directly will result in a SQLiteException (the database is locked)
603        SQLiteDatabase db = getDatabase(context);
604        int table = match >> BASE_SHIFT;
605        String id = "0";
606        boolean messageDeletion = false;
607
608        if (Email.LOGD) {
609            Log.v(TAG, "EmailProvider.delete: uri=" + uri + ", match is " + match);
610        }
611
612        int result = -1;
613
614        try {
615            switch (match) {
616                // These are cases in which one or more Messages might get deleted, either by
617                // cascade or explicitly
618                case MAILBOX_ID:
619                case MAILBOX:
620                case ACCOUNT_ID:
621                case ACCOUNT:
622                case MESSAGE:
623                case SYNCED_MESSAGE_ID:
624                case MESSAGE_ID:
625                    // Handle lost Body records here, since this cannot be done in a trigger
626                    // The process is:
627                    //  1) Begin a transaction, ensuring that both databases are affected atomically
628                    //  2) Do the requested deletion, with cascading deletions handled in triggers
629                    //  3) End the transaction, committing all changes atomically
630                    //
631                    // Bodies are auto-deleted here;  Attachments are auto-deleted via trigger
632
633                    messageDeletion = true;
634                    db.beginTransaction();
635                    break;
636            }
637            switch (match) {
638                case BODY_ID:
639                case DELETED_MESSAGE_ID:
640                case SYNCED_MESSAGE_ID:
641                case MESSAGE_ID:
642                case UPDATED_MESSAGE_ID:
643                case ATTACHMENT_ID:
644                case MAILBOX_ID:
645                case ACCOUNT_ID:
646                case HOSTAUTH_ID:
647                    id = uri.getPathSegments().get(1);
648                    if (match == SYNCED_MESSAGE_ID) {
649                        // For synced messages, first copy the old message to the deleted table and
650                        // delete it from the updated table (in case it was updated first)
651                        // Note that this is all within a transaction, for atomicity
652                        db.execSQL(DELETED_MESSAGE_INSERT + id);
653                        db.execSQL(UPDATED_MESSAGE_DELETE + id);
654                    }
655                    result = db.delete(TABLE_NAMES[table], whereWithId(id, selection),
656                            selectionArgs);
657                    break;
658                case ATTACHMENTS_MESSAGE_ID:
659                    // All attachments for the given message
660                    id = uri.getPathSegments().get(2);
661                    result = db.delete(TABLE_NAMES[table],
662                            whereWith(Attachment.MESSAGE_KEY + "=" + id, selection), selectionArgs);
663                    break;
664
665                case BODY:
666                case MESSAGE:
667                case DELETED_MESSAGE:
668                case UPDATED_MESSAGE:
669                case ATTACHMENT:
670                case MAILBOX:
671                case ACCOUNT:
672                case HOSTAUTH:
673                    result = db.delete(TABLE_NAMES[table], selection, selectionArgs);
674                    break;
675
676                default:
677                    throw new IllegalArgumentException("Unknown URI " + uri);
678            }
679            if (messageDeletion) {
680                if (match == MESSAGE_ID) {
681                    // Delete the Body record associated with the deleted message
682                    db.execSQL(DELETE_BODY + id);
683                } else {
684                    // Delete any orphaned Body records
685                    db.execSQL(DELETE_ORPHAN_BODIES);
686                }
687                db.setTransactionSuccessful();
688            }
689        } finally {
690            if (messageDeletion) {
691                db.endTransaction();
692            }
693        }
694        getContext().getContentResolver().notifyChange(uri, null);
695        return result;
696    }
697
698    @Override
699    // Use the email- prefix because message, mailbox, and account are so generic (e.g. SMS, IM)
700    public String getType(Uri uri) {
701        int match = sURIMatcher.match(uri);
702        switch (match) {
703            case BODY_ID:
704                return "vnd.android.cursor.item/email-body";
705            case BODY:
706                return "vnd.android.cursor.dir/email-message";
707            case UPDATED_MESSAGE_ID:
708            case MESSAGE_ID:
709                return "vnd.android.cursor.item/email-message";
710            case MAILBOX_MESSAGES:
711            case UPDATED_MESSAGE:
712            case MESSAGE:
713                return "vnd.android.cursor.dir/email-message";
714            case ACCOUNT_MAILBOXES:
715            case MAILBOX:
716                return "vnd.android.cursor.dir/email-mailbox";
717            case MAILBOX_ID:
718                return "vnd.android.cursor.item/email-mailbox";
719            case ACCOUNT:
720                return "vnd.android.cursor.dir/email-account";
721            case ACCOUNT_ID:
722                return "vnd.android.cursor.item/email-account";
723            case ATTACHMENTS_MESSAGE_ID:
724            case ATTACHMENT:
725                return "vnd.android.cursor.dir/email-attachment";
726            case ATTACHMENT_ID:
727                return "vnd.android.cursor.item/email-attachment";
728            case HOSTAUTH:
729                return "vnd.android.cursor.dir/email-hostauth";
730            case HOSTAUTH_ID:
731                return "vnd.android.cursor.item/email-hostauth";
732            default:
733                throw new IllegalArgumentException("Unknown URI " + uri);
734        }
735    }
736
737    @Override
738    public Uri insert(Uri uri, ContentValues values) {
739        int match = sURIMatcher.match(uri);
740        Context context = getContext();
741        // See the comment at delete(), above
742        SQLiteDatabase db = getDatabase(context);
743        int table = match >> BASE_SHIFT;
744        long id;
745
746        if (Email.LOGD) {
747            Log.v(TAG, "EmailProvider.insert: uri=" + uri + ", match is " + match);
748        }
749
750        Uri resultUri = null;
751
752        switch (match) {
753            case BODY:
754            case MESSAGE:
755            case ATTACHMENT:
756            case MAILBOX:
757            case ACCOUNT:
758            case HOSTAUTH:
759                id = db.insert(TABLE_NAMES[table], "foo", values);
760                resultUri = ContentUris.withAppendedId(uri, id);
761                break;
762            case MAILBOX_ID:
763                // This implies adding a message to a mailbox
764                // Hmm, one problem here is that we can't link the account as well, so it must be
765                // already in the values...
766                id = Long.parseLong(uri.getPathSegments().get(1));
767                values.put(MessageColumns.MAILBOX_KEY, id);
768                resultUri = insert(Message.CONTENT_URI, values);
769                break;
770            case MESSAGE_ID:
771                // This implies adding an attachment to a message.
772                id = Long.parseLong(uri.getPathSegments().get(1));
773                values.put(AttachmentColumns.MESSAGE_KEY, id);
774                resultUri = insert(Attachment.CONTENT_URI, values);
775                break;
776            case ACCOUNT_ID:
777                // This implies adding a mailbox to an account.
778                id = Long.parseLong(uri.getPathSegments().get(1));
779                values.put(MailboxColumns.ACCOUNT_KEY, id);
780                resultUri = insert(Mailbox.CONTENT_URI, values);
781                break;
782            case ATTACHMENTS_MESSAGE_ID:
783                id = db.insert(TABLE_NAMES[table], "foo", values);
784                resultUri = ContentUris.withAppendedId(Attachment.CONTENT_URI, id);
785                break;
786            default:
787                throw new IllegalArgumentException("Unknown URL " + uri);
788        }
789
790        // Notify with the base uri, not the new uri (nobody is watching a new record)
791        getContext().getContentResolver().notifyChange(uri, null);
792        return resultUri;
793    }
794
795    @Override
796    public boolean onCreate() {
797        // TODO Auto-generated method stub
798        return false;
799    }
800
801    @Override
802    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
803            String sortOrder) {
804        Cursor c = null;
805        Uri notificationUri = EmailContent.CONTENT_URI;
806        int match = sURIMatcher.match(uri);
807        Context context = getContext();
808        // See the comment at delete(), above
809        SQLiteDatabase db = getDatabase(context);
810        int table = match >> BASE_SHIFT;
811        String id;
812
813        if (Email.LOGD) {
814            Log.v(TAG, "EmailProvider.query: uri=" + uri + ", match is " + match);
815        }
816
817        switch (match) {
818            case BODY:
819            case MESSAGE:
820            case UPDATED_MESSAGE:
821            case DELETED_MESSAGE:
822            case ATTACHMENT:
823            case MAILBOX:
824            case ACCOUNT:
825            case HOSTAUTH:
826                c = db.query(TABLE_NAMES[table], projection,
827                        selection, selectionArgs, null, null, sortOrder);
828                break;
829            case BODY_ID:
830            case MESSAGE_ID:
831            case DELETED_MESSAGE_ID:
832            case UPDATED_MESSAGE_ID:
833            case ATTACHMENT_ID:
834            case MAILBOX_ID:
835            case ACCOUNT_ID:
836            case HOSTAUTH_ID:
837                id = uri.getPathSegments().get(1);
838                c = db.query(TABLE_NAMES[table], projection,
839                        whereWithId(id, selection), selectionArgs, null, null, sortOrder);
840                break;
841            case ATTACHMENTS_MESSAGE_ID:
842                // All attachments for the given message
843                id = uri.getPathSegments().get(2);
844                c = db.query(Attachment.TABLE_NAME, projection,
845                        whereWith(Attachment.MESSAGE_KEY + "=" + id, selection),
846                        selectionArgs, null, null, sortOrder);
847                break;
848            default:
849                throw new IllegalArgumentException("Unknown URI " + uri);
850        }
851
852        if ((c != null) && !isTemporary()) {
853            c.setNotificationUri(getContext().getContentResolver(), notificationUri);
854        }
855        return c;
856    }
857
858    private String whereWithId(String id, String selection) {
859        StringBuilder sb = new StringBuilder(256);
860        sb.append("_id=");
861        sb.append(id);
862        if (selection != null) {
863            sb.append(" AND (");
864            sb.append(selection);
865            sb.append(')');
866        }
867        return sb.toString();
868    }
869
870    /**
871     * Combine a locally-generated selection with a user-provided selection
872     *
873     * This introduces risk that the local selection might insert incorrect chars
874     * into the SQL, so use caution.
875     *
876     * @param where locally-generated selection, must not be null
877     * @param selection user-provided selection, may be null
878     * @return a single selection string
879     */
880    private String whereWith(String where, String selection) {
881        if (selection == null) {
882            return where;
883        }
884        StringBuilder sb = new StringBuilder(where);
885        sb.append(" AND (");
886        sb.append(selection);
887        sb.append(')');
888
889        return sb.toString();
890    }
891
892    @Override
893    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
894        int match = sURIMatcher.match(uri);
895        Context context = getContext();
896        // See the comment at delete(), above
897        SQLiteDatabase db = getDatabase(context);
898        int table = match >> BASE_SHIFT;
899        int result;
900
901        if (Email.LOGD) {
902            Log.v(TAG, "EmailProvider.update: uri=" + uri + ", match is " + match);
903        }
904
905        // We do NOT allow setting of unreadCount via the provider
906        // This column is maintained via triggers
907        if (match == MAILBOX_ID || match == MAILBOX) {
908            values.remove(MailboxColumns.UNREAD_COUNT);
909        }
910
911        String id;
912        switch (match) {
913            case MAILBOX_ID_ADD_TO_FIELD:
914            case ACCOUNT_ID_ADD_TO_FIELD:
915                db.beginTransaction();
916                id = uri.getPathSegments().get(1);
917                String field = values.getAsString(EmailContent.FIELD_COLUMN_NAME);
918                Long add = values.getAsLong(EmailContent.ADD_COLUMN_NAME);
919                if (field == null || add == null) {
920                    throw new IllegalArgumentException("No field/add specified " + uri);
921                }
922                Cursor c = db.query(TABLE_NAMES[table],
923                        new String[] {EmailContent.RECORD_ID, field}, whereWithId(id, selection),
924                        selectionArgs, null, null, null);
925                try {
926                    result = 0;
927                    ContentValues cv = new ContentValues();
928                    String[] bind = new String[1];
929                    while (c.moveToNext()) {
930                        bind[0] = c.getString(0);
931                        long value = c.getLong(1) + add;
932                        cv.put(field, value);
933                        result = db.update(TABLE_NAMES[table], cv, ID_EQUALS, bind);
934                    }
935                } finally {
936                    c.close();
937                }
938                db.setTransactionSuccessful();
939                db.endTransaction();
940                break;
941            case BODY_ID:
942            case MESSAGE_ID:
943            case SYNCED_MESSAGE_ID:
944            case UPDATED_MESSAGE_ID:
945            case ATTACHMENT_ID:
946            case MAILBOX_ID:
947            case ACCOUNT_ID:
948            case HOSTAUTH_ID:
949                id = uri.getPathSegments().get(1);
950                if (match == SYNCED_MESSAGE_ID) {
951                    // For synced messages, first copy the old message to the updated table
952                    // Note the insert or ignore semantics, guaranteeing that only the first
953                    // update will be reflected in the updated message table; therefore this row
954                    // will always have the "original" data
955                    db.execSQL(UPDATED_MESSAGE_INSERT + id);
956                } else if (match == MESSAGE_ID) {
957                    db.execSQL(UPDATED_MESSAGE_DELETE + id);
958                }
959                result = db.update(TABLE_NAMES[table], values, whereWithId(id, selection),
960                        selectionArgs);
961                break;
962            case BODY:
963            case MESSAGE:
964            case UPDATED_MESSAGE:
965            case ATTACHMENT:
966            case MAILBOX:
967            case ACCOUNT:
968            case HOSTAUTH:
969                result = db.update(TABLE_NAMES[table], values, selection, selectionArgs);
970                break;
971            default:
972                throw new IllegalArgumentException("Unknown URI " + uri);
973        }
974
975        getContext().getContentResolver().notifyChange(uri, null);
976        return result;
977    }
978
979    /* (non-Javadoc)
980     * @see android.content.ContentProvider#applyBatch(android.content.ContentProviderOperation)
981     */
982    @Override
983    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
984            throws OperationApplicationException {
985        Context context = getContext();
986        SQLiteDatabase db = getDatabase(context);
987        db.beginTransaction();
988        try {
989            ContentProviderResult[] results = super.applyBatch(operations);
990            db.setTransactionSuccessful();
991            return results;
992        } finally {
993            db.endTransaction();
994        }
995    }
996}
997