2009-05-26 23:40:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.provider;
|
|
|
|
|
2009-07-16 21:09:15 +00:00
|
|
|
import com.android.email.Email;
|
2010-10-27 23:50:54 +00:00
|
|
|
import com.android.email.provider.ContentCache.CacheToken;
|
2010-08-10 00:48:53 +00:00
|
|
|
import com.android.email.service.AttachmentDownloadService;
|
2011-02-13 02:56:09 +00:00
|
|
|
import com.android.emailcommon.AccountManagerTypes;
|
2011-02-10 18:26:56 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Account;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.AccountColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Attachment;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Body;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.BodyColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.HostAuth;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.HostAuthColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Mailbox;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.Message;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.MessageColumns;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.SyncColumns;
|
2011-04-22 22:45:11 +00:00
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
2009-05-26 23:40:34 +00:00
|
|
|
|
2009-09-16 02:27:05 +00:00
|
|
|
import android.accounts.AccountManager;
|
2009-05-26 23:40:34 +00:00
|
|
|
import android.content.ContentProvider;
|
2009-05-29 21:24:34 +00:00
|
|
|
import android.content.ContentProviderOperation;
|
|
|
|
import android.content.ContentProviderResult;
|
2010-09-23 16:19:44 +00:00
|
|
|
import android.content.ContentResolver;
|
2009-05-26 23:40:34 +00:00
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
2009-05-29 21:24:34 +00:00
|
|
|
import android.content.OperationApplicationException;
|
2009-05-26 23:40:34 +00:00
|
|
|
import android.content.UriMatcher;
|
|
|
|
import android.database.Cursor;
|
2010-12-30 22:55:27 +00:00
|
|
|
import android.database.MatrixCursor;
|
2009-06-15 21:40:06 +00:00
|
|
|
import android.database.SQLException;
|
2009-05-26 23:40:34 +00:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2009-11-19 01:11:33 +00:00
|
|
|
import android.database.sqlite.SQLiteException;
|
2009-05-26 23:40:34 +00:00
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2009-11-19 01:11:33 +00:00
|
|
|
import java.io.File;
|
2009-06-01 19:55:50 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
public class EmailProvider extends ContentProvider {
|
|
|
|
|
|
|
|
private static final String TAG = "EmailProvider";
|
|
|
|
|
2009-11-19 01:11:33 +00:00
|
|
|
protected static final String DATABASE_NAME = "EmailProvider.db";
|
|
|
|
protected static final String BODY_DATABASE_NAME = "EmailProviderBody.db";
|
|
|
|
|
2010-08-10 00:48:53 +00:00
|
|
|
public static final String ACTION_ATTACHMENT_UPDATED = "com.android.email.ATTACHMENT_UPDATED";
|
|
|
|
public static final String ATTACHMENT_UPDATED_EXTRA_FLAGS =
|
|
|
|
"com.android.email.ATTACHMENT_UPDATED_FLAGS";
|
|
|
|
|
2010-10-14 02:04:46 +00:00
|
|
|
public static final String EMAIL_MESSAGE_MIME_TYPE =
|
|
|
|
"vnd.android.cursor.item/email-message";
|
2010-08-10 00:48:53 +00:00
|
|
|
public static final String EMAIL_ATTACHMENT_MIME_TYPE =
|
|
|
|
"vnd.android.cursor.item/email-attachment";
|
|
|
|
|
2009-11-19 01:11:33 +00:00
|
|
|
public static final Uri INTEGRITY_CHECK_URI =
|
|
|
|
Uri.parse("content://" + EmailContent.AUTHORITY + "/integrityCheck");
|
2009-10-13 23:25:00 +00:00
|
|
|
|
|
|
|
// Definitions for our queries looking for orphaned messages
|
|
|
|
private static final String[] ORPHANS_PROJECTION
|
|
|
|
= new String[] {MessageColumns.ID, MessageColumns.MAILBOX_KEY};
|
|
|
|
private static final int ORPHANS_ID = 0;
|
|
|
|
private static final int ORPHANS_MAILBOX_KEY = 1;
|
|
|
|
|
|
|
|
private static final String WHERE_ID = EmailContent.RECORD_ID + "=?";
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2010-10-27 23:50:54 +00:00
|
|
|
// We'll cache the following four tables; sizes are best estimates of effective values
|
|
|
|
private static final ContentCache sCacheAccount =
|
|
|
|
new ContentCache("Account", Account.CONTENT_PROJECTION, 4);
|
|
|
|
private static final ContentCache sCacheHostAuth =
|
|
|
|
new ContentCache("HostAuth", HostAuth.CONTENT_PROJECTION, 8);
|
|
|
|
/*package*/ static final ContentCache sCacheMailbox =
|
|
|
|
new ContentCache("Mailbox", Mailbox.CONTENT_PROJECTION, 8);
|
|
|
|
private static final ContentCache sCacheMessage =
|
|
|
|
new ContentCache("Message", Message.CONTENT_PROJECTION, 3);
|
|
|
|
|
2009-08-18 19:14:15 +00:00
|
|
|
// Any changes to the database format *must* include update-in-place code.
|
2009-09-07 23:03:02 +00:00
|
|
|
// Original version: 3
|
2009-09-16 02:27:05 +00:00
|
|
|
// Version 4: Database wipe required; changing AccountManager interface w/Exchange
|
2009-09-23 01:31:10 +00:00
|
|
|
// Version 5: Database wipe required; changing AccountManager interface w/Exchange
|
|
|
|
// Version 6: Adding Message.mServerTimeStamp column
|
2009-10-13 23:25:00 +00:00
|
|
|
// Version 7: Replace the mailbox_delete trigger with a version that removes orphaned messages
|
|
|
|
// from the Message_Deletes and Message_Updates tables
|
2010-01-21 19:48:02 +00:00
|
|
|
// Version 8: Add security flags column to accounts table
|
2010-01-27 01:24:15 +00:00
|
|
|
// Version 9: Add security sync key and signature to accounts table
|
2010-02-16 00:01:38 +00:00
|
|
|
// Version 10: Add meeting info to message table
|
2010-02-22 20:57:33 +00:00
|
|
|
// Version 11: Add content and flags to attachment table
|
2010-03-12 21:30:26 +00:00
|
|
|
// Version 12: Add content_bytes to attachment table. content is deprecated.
|
2010-07-30 20:53:59 +00:00
|
|
|
// Version 13: Add messageCount to Mailbox table.
|
2010-09-02 02:06:15 +00:00
|
|
|
// Version 14: Add snippet to Message table
|
2010-09-07 19:36:48 +00:00
|
|
|
// Version 15: Fix upgrade problem in version 14.
|
2010-12-09 01:11:04 +00:00
|
|
|
// Version 16: Add accountKey to Attachment table
|
2011-03-01 18:45:50 +00:00
|
|
|
// Version 17: Add parentKey to Mailbox table
|
2011-04-22 22:45:11 +00:00
|
|
|
// Version 18: Copy Mailbox.displayName to Mailbox.serverId for all IMAP & POP3 mailboxes.
|
|
|
|
// Column Mailbox.serverId is used for the server-side pathname of a mailbox.
|
|
|
|
public static final int DATABASE_VERSION = 18;
|
2009-09-07 23:03:02 +00:00
|
|
|
|
|
|
|
// Any changes to the database format *must* include update-in-place code.
|
|
|
|
// Original version: 2
|
|
|
|
// Version 3: Add "sourceKey" column
|
2009-09-16 02:27:05 +00:00
|
|
|
// Version 4: Database wipe required; changing AccountManager interface w/Exchange
|
2009-09-23 01:31:10 +00:00
|
|
|
// Version 5: Database wipe required; changing AccountManager interface w/Exchange
|
2009-09-23 01:38:28 +00:00
|
|
|
// Version 6: Adding Body.mIntroText column
|
|
|
|
public static final int BODY_DATABASE_VERSION = 6;
|
2009-05-26 23:40:34 +00:00
|
|
|
|
|
|
|
private static final int ACCOUNT_BASE = 0;
|
|
|
|
private static final int ACCOUNT = ACCOUNT_BASE;
|
2010-09-10 22:19:57 +00:00
|
|
|
private static final int ACCOUNT_ID = ACCOUNT_BASE + 1;
|
|
|
|
private static final int ACCOUNT_ID_ADD_TO_FIELD = ACCOUNT_BASE + 2;
|
2010-09-14 23:28:50 +00:00
|
|
|
private static final int ACCOUNT_RESET_NEW_COUNT = ACCOUNT_BASE + 3;
|
|
|
|
private static final int ACCOUNT_RESET_NEW_COUNT_ID = ACCOUNT_BASE + 4;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final int MAILBOX_BASE = 0x1000;
|
|
|
|
private static final int MAILBOX = MAILBOX_BASE;
|
2010-09-10 22:19:57 +00:00
|
|
|
private static final int MAILBOX_ID = MAILBOX_BASE + 1;
|
|
|
|
private static final int MAILBOX_ID_ADD_TO_FIELD = MAILBOX_BASE + 2;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final int MESSAGE_BASE = 0x2000;
|
|
|
|
private static final int MESSAGE = MESSAGE_BASE;
|
2009-07-16 23:03:40 +00:00
|
|
|
private static final int MESSAGE_ID = MESSAGE_BASE + 1;
|
|
|
|
private static final int SYNCED_MESSAGE_ID = MESSAGE_BASE + 2;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final int ATTACHMENT_BASE = 0x3000;
|
|
|
|
private static final int ATTACHMENT = ATTACHMENT_BASE;
|
2010-09-10 22:19:57 +00:00
|
|
|
private static final int ATTACHMENT_ID = ATTACHMENT_BASE + 1;
|
|
|
|
private static final int ATTACHMENTS_MESSAGE_ID = ATTACHMENT_BASE + 2;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final int HOSTAUTH_BASE = 0x4000;
|
|
|
|
private static final int HOSTAUTH = HOSTAUTH_BASE;
|
|
|
|
private static final int HOSTAUTH_ID = HOSTAUTH_BASE + 1;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-06-17 17:22:37 +00:00
|
|
|
private static final int UPDATED_MESSAGE_BASE = 0x5000;
|
|
|
|
private static final int UPDATED_MESSAGE = UPDATED_MESSAGE_BASE;
|
2009-06-27 19:14:14 +00:00
|
|
|
private static final int UPDATED_MESSAGE_ID = UPDATED_MESSAGE_BASE + 1;
|
|
|
|
|
|
|
|
private static final int DELETED_MESSAGE_BASE = 0x6000;
|
|
|
|
private static final int DELETED_MESSAGE = DELETED_MESSAGE_BASE;
|
|
|
|
private static final int DELETED_MESSAGE_ID = DELETED_MESSAGE_BASE + 1;
|
|
|
|
|
|
|
|
// MUST ALWAYS EQUAL THE LAST OF THE PREVIOUS BASE CONSTANTS
|
|
|
|
private static final int LAST_EMAIL_PROVIDER_DB_BASE = DELETED_MESSAGE_BASE;
|
|
|
|
|
|
|
|
// DO NOT CHANGE BODY_BASE!!
|
|
|
|
private static final int BODY_BASE = LAST_EMAIL_PROVIDER_DB_BASE + 0x1000;
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final int BODY = BODY_BASE;
|
|
|
|
private static final int BODY_ID = BODY_BASE + 1;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final int BASE_SHIFT = 12; // 12 bits to the base type: 0, 0x1000, 0x2000, etc.
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2010-10-27 23:50:54 +00:00
|
|
|
// TABLE_NAMES MUST remain in the order of the BASE constants above (e.g. ACCOUNT_BASE = 0x0000,
|
|
|
|
// MESSAGE_BASE = 0x1000, etc.)
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final String[] TABLE_NAMES = {
|
2009-06-15 21:40:06 +00:00
|
|
|
EmailContent.Account.TABLE_NAME,
|
|
|
|
EmailContent.Mailbox.TABLE_NAME,
|
|
|
|
EmailContent.Message.TABLE_NAME,
|
|
|
|
EmailContent.Attachment.TABLE_NAME,
|
|
|
|
EmailContent.HostAuth.TABLE_NAME,
|
2009-06-27 19:14:14 +00:00
|
|
|
EmailContent.Message.UPDATED_TABLE_NAME,
|
|
|
|
EmailContent.Message.DELETED_TABLE_NAME,
|
2009-06-15 21:40:06 +00:00
|
|
|
EmailContent.Body.TABLE_NAME
|
2009-05-26 23:40:34 +00:00
|
|
|
};
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2010-10-27 23:50:54 +00:00
|
|
|
// CONTENT_CACHES MUST remain in the order of the BASE constants above
|
|
|
|
private static final ContentCache[] CONTENT_CACHES = {
|
|
|
|
sCacheAccount,
|
|
|
|
sCacheMailbox,
|
|
|
|
sCacheMessage,
|
|
|
|
null,
|
|
|
|
sCacheHostAuth,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null};
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
2009-06-27 19:14:14 +00:00
|
|
|
/**
|
|
|
|
* Let's only generate these SQL strings once, as they are used frequently
|
|
|
|
* Note that this isn't relevant for table creation strings, since they are used only once
|
|
|
|
*/
|
|
|
|
private static final String UPDATED_MESSAGE_INSERT = "insert or ignore into " +
|
|
|
|
Message.UPDATED_TABLE_NAME + " select * from " + Message.TABLE_NAME + " where " +
|
|
|
|
EmailContent.RECORD_ID + '=';
|
|
|
|
|
|
|
|
private static final String UPDATED_MESSAGE_DELETE = "delete from " +
|
|
|
|
Message.UPDATED_TABLE_NAME + " where " + EmailContent.RECORD_ID + '=';
|
|
|
|
|
|
|
|
private static final String DELETED_MESSAGE_INSERT = "insert or replace into " +
|
|
|
|
Message.DELETED_TABLE_NAME + " select * from " + Message.TABLE_NAME + " where " +
|
|
|
|
EmailContent.RECORD_ID + '=';
|
|
|
|
|
|
|
|
private static final String DELETE_ORPHAN_BODIES = "delete from " + Body.TABLE_NAME +
|
2009-08-03 13:05:50 +00:00
|
|
|
" where " + BodyColumns.MESSAGE_KEY + " in " + "(select " + BodyColumns.MESSAGE_KEY +
|
2009-06-27 19:14:14 +00:00
|
|
|
" from " + Body.TABLE_NAME + " except select " + EmailContent.RECORD_ID + " from " +
|
|
|
|
Message.TABLE_NAME + ')';
|
|
|
|
|
|
|
|
private static final String DELETE_BODY = "delete from " + Body.TABLE_NAME +
|
2009-08-03 13:05:50 +00:00
|
|
|
" where " + BodyColumns.MESSAGE_KEY + '=';
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-08-20 02:07:29 +00:00
|
|
|
private static final String ID_EQUALS = EmailContent.RECORD_ID + "=?";
|
|
|
|
|
2009-10-13 23:25:00 +00:00
|
|
|
private static final String TRIGGER_MAILBOX_DELETE =
|
|
|
|
"create trigger mailbox_delete before delete on " + Mailbox.TABLE_NAME +
|
|
|
|
" begin" +
|
|
|
|
" delete from " + Message.TABLE_NAME +
|
|
|
|
" where " + MessageColumns.MAILBOX_KEY + "=old." + EmailContent.RECORD_ID +
|
|
|
|
"; delete from " + Message.UPDATED_TABLE_NAME +
|
|
|
|
" where " + MessageColumns.MAILBOX_KEY + "=old." + EmailContent.RECORD_ID +
|
|
|
|
"; delete from " + Message.DELETED_TABLE_NAME +
|
|
|
|
" where " + MessageColumns.MAILBOX_KEY + "=old." + EmailContent.RECORD_ID +
|
|
|
|
"; end";
|
2009-10-20 05:15:45 +00:00
|
|
|
|
2010-09-14 23:28:50 +00:00
|
|
|
private static final ContentValues CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT;
|
|
|
|
|
2010-10-14 02:04:46 +00:00
|
|
|
public static final String MESSAGE_URI_PARAMETER_MAILBOX_ID = "mailboxId";
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
static {
|
|
|
|
// Email URI matching table
|
|
|
|
UriMatcher matcher = sURIMatcher;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// All accounts
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "account", ACCOUNT);
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific account
|
2009-07-30 18:41:31 +00:00
|
|
|
// insert into this URI causes a mailbox to be added to the account
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "account/#", ACCOUNT_ID);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2010-09-14 23:28:50 +00:00
|
|
|
// Special URI to reset the new message count. Only update works, and content values
|
|
|
|
// will be ignored.
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "resetNewMessageCount",
|
|
|
|
ACCOUNT_RESET_NEW_COUNT);
|
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "resetNewMessageCount/#",
|
|
|
|
ACCOUNT_RESET_NEW_COUNT_ID);
|
2010-09-14 23:28:50 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// All mailboxes
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "mailbox", MAILBOX);
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific mailbox
|
|
|
|
// insert into this URI causes a message to be added to the mailbox
|
|
|
|
// ** NOTE For now, the accountKey must be set manually in the values!
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "mailbox/#", MAILBOX_ID);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// All messages
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "message", MESSAGE);
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific message
|
2009-07-30 18:41:31 +00:00
|
|
|
// insert into this URI causes an attachment to be added to the message
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "message/#", MESSAGE_ID);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific attachment
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "attachment", ATTACHMENT);
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific attachment (the header information)
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "attachment/#", ATTACHMENT_ID);
|
2009-07-16 23:03:40 +00:00
|
|
|
// The attachments of a specific message (query only) (insert & delete TBD)
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "attachment/message/#",
|
|
|
|
ATTACHMENTS_MESSAGE_ID);
|
2009-05-26 23:40:34 +00:00
|
|
|
|
|
|
|
// All mail bodies
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "body", BODY);
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific mail body
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "body/#", BODY_ID);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-07-16 23:03:40 +00:00
|
|
|
// All hostauth records
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "hostauth", HOSTAUTH);
|
2009-07-16 23:03:40 +00:00
|
|
|
// A specific hostauth
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "hostauth/#", HOSTAUTH_ID);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-08-20 02:07:29 +00:00
|
|
|
// Atomically a constant value to a particular field of a mailbox/account
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "mailboxIdAddToField/#",
|
|
|
|
MAILBOX_ID_ADD_TO_FIELD);
|
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "accountIdAddToField/#",
|
|
|
|
ACCOUNT_ID_ADD_TO_FIELD);
|
2009-08-20 02:07:29 +00:00
|
|
|
|
2009-06-27 19:14:14 +00:00
|
|
|
/**
|
|
|
|
* THIS URI HAS SPECIAL SEMANTICS
|
2009-08-20 02:07:29 +00:00
|
|
|
* ITS USE IS INTENDED FOR THE UI APPLICATION TO MARK CHANGES THAT NEED TO BE SYNCED BACK
|
2009-06-27 19:14:14 +00:00
|
|
|
* TO A SERVER VIA A SYNC ADAPTER
|
|
|
|
*/
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "syncedMessage/#", SYNCED_MESSAGE_ID);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* THE URIs BELOW THIS POINT ARE INTENDED TO BE USED BY SYNC ADAPTERS ONLY
|
|
|
|
* THEY REFER TO DATA CREATED AND MAINTAINED BY CALLS TO THE SYNCED_MESSAGE_ID URI
|
|
|
|
* BY THE UI APPLICATION
|
|
|
|
*/
|
|
|
|
// All deleted messages
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "deletedMessage", DELETED_MESSAGE);
|
2009-06-27 19:14:14 +00:00
|
|
|
// A specific deleted message
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "deletedMessage/#", DELETED_MESSAGE_ID);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
|
|
|
// All updated messages
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "updatedMessage", UPDATED_MESSAGE);
|
2009-06-27 19:14:14 +00:00
|
|
|
// A specific updated message
|
2011-02-11 23:05:17 +00:00
|
|
|
matcher.addURI(EmailContent.AUTHORITY, "updatedMessage/#", UPDATED_MESSAGE_ID);
|
2010-09-14 23:28:50 +00:00
|
|
|
|
|
|
|
CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT = new ContentValues();
|
|
|
|
CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT.put(Account.NEW_MESSAGE_COUNT, 0);
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
2010-12-16 20:28:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap the UriMatcher call so we can throw a runtime exception if an unknown Uri is passed in
|
|
|
|
* @param uri the Uri to match
|
|
|
|
* @return the match value
|
|
|
|
*/
|
|
|
|
private static int findMatch(Uri uri, String methodName) {
|
|
|
|
int match = sURIMatcher.match(uri);
|
|
|
|
if (match < 0) {
|
2010-12-30 04:51:16 +00:00
|
|
|
throw new IllegalArgumentException("Unknown uri: " + uri);
|
2010-12-16 20:28:16 +00:00
|
|
|
} else if (Email.LOGD) {
|
|
|
|
Log.v(TAG, methodName + ": uri=" + uri + ", match is " + match);
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2009-08-12 10:51:26 +00:00
|
|
|
/*
|
|
|
|
* Internal helper method for index creation.
|
|
|
|
* Example:
|
|
|
|
* "create index message_" + MessageColumns.FLAG_READ
|
|
|
|
* + " on " + Message.TABLE_NAME + " (" + MessageColumns.FLAG_READ + ");"
|
|
|
|
*/
|
|
|
|
/* package */
|
|
|
|
static String createIndex(String tableName, String columnName) {
|
|
|
|
return "create index " + tableName.toLowerCase() + '_' + columnName
|
|
|
|
+ " on " + tableName + " (" + columnName + ");";
|
|
|
|
}
|
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
static void createMessageTable(SQLiteDatabase db) {
|
2009-07-05 19:54:49 +00:00
|
|
|
String messageColumns = MessageColumns.DISPLAY_NAME + " text, "
|
|
|
|
+ MessageColumns.TIMESTAMP + " integer, "
|
|
|
|
+ MessageColumns.SUBJECT + " text, "
|
|
|
|
+ MessageColumns.FLAG_READ + " integer, "
|
|
|
|
+ MessageColumns.FLAG_LOADED + " integer, "
|
|
|
|
+ MessageColumns.FLAG_FAVORITE + " integer, "
|
|
|
|
+ MessageColumns.FLAG_ATTACHMENT + " integer, "
|
|
|
|
+ MessageColumns.FLAGS + " integer, "
|
|
|
|
+ MessageColumns.CLIENT_ID + " integer, "
|
|
|
|
+ MessageColumns.MESSAGE_ID + " text, "
|
|
|
|
+ MessageColumns.MAILBOX_KEY + " integer, "
|
|
|
|
+ MessageColumns.ACCOUNT_KEY + " integer, "
|
|
|
|
+ MessageColumns.FROM_LIST + " text, "
|
|
|
|
+ MessageColumns.TO_LIST + " text, "
|
|
|
|
+ MessageColumns.CC_LIST + " text, "
|
|
|
|
+ MessageColumns.BCC_LIST + " text, "
|
2010-02-16 00:01:38 +00:00
|
|
|
+ MessageColumns.REPLY_TO_LIST + " text, "
|
2010-09-02 02:06:15 +00:00
|
|
|
+ MessageColumns.MEETING_INFO + " text, "
|
|
|
|
+ MessageColumns.SNIPPET + " text"
|
2009-07-05 19:54:49 +00:00
|
|
|
+ ");";
|
|
|
|
|
|
|
|
// This String and the following String MUST have the same columns, except for the type
|
|
|
|
// of those columns!
|
|
|
|
String createString = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
2009-09-23 01:31:10 +00:00
|
|
|
+ SyncColumns.SERVER_ID + " text, "
|
|
|
|
+ SyncColumns.SERVER_TIMESTAMP + " integer, "
|
2009-07-05 19:54:49 +00:00
|
|
|
+ messageColumns;
|
|
|
|
|
|
|
|
// For the updated and deleted tables, the id is assigned, but we do want to keep track
|
|
|
|
// of the ORDER of updates using an autoincrement primary key. We use the DATA column
|
|
|
|
// at this point; it has no other function
|
2009-07-14 20:08:19 +00:00
|
|
|
String altCreateString = " (" + EmailContent.RECORD_ID + " integer unique, "
|
2009-09-23 01:31:10 +00:00
|
|
|
+ SyncColumns.SERVER_ID + " text, "
|
|
|
|
+ SyncColumns.SERVER_TIMESTAMP + " integer, "
|
2009-07-05 19:54:49 +00:00
|
|
|
+ messageColumns;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
|
|
|
// The three tables have the same schema
|
2009-07-05 19:54:49 +00:00
|
|
|
db.execSQL("create table " + Message.TABLE_NAME + createString);
|
|
|
|
db.execSQL("create table " + Message.UPDATED_TABLE_NAME + altCreateString);
|
|
|
|
db.execSQL("create table " + Message.DELETED_TABLE_NAME + altCreateString);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-08-12 10:51:26 +00:00
|
|
|
String indexColumns[] = {
|
|
|
|
MessageColumns.TIMESTAMP,
|
|
|
|
MessageColumns.FLAG_READ,
|
|
|
|
MessageColumns.FLAG_LOADED,
|
|
|
|
MessageColumns.MAILBOX_KEY,
|
|
|
|
SyncColumns.SERVER_ID
|
|
|
|
};
|
|
|
|
|
|
|
|
for (String columnName : indexColumns) {
|
|
|
|
db.execSQL(createIndex(Message.TABLE_NAME, columnName));
|
|
|
|
}
|
2009-06-16 19:03:45 +00:00
|
|
|
|
2009-06-27 19:14:14 +00:00
|
|
|
// Deleting a Message deletes all associated Attachments
|
2009-06-16 19:03:45 +00:00
|
|
|
// Deleting the associated Body cannot be done in a trigger, because the Body is stored
|
|
|
|
// in a separate database, and trigger cannot operate on attached databases.
|
2009-07-30 18:41:31 +00:00
|
|
|
db.execSQL("create trigger message_delete before delete on " + Message.TABLE_NAME +
|
2009-06-16 19:03:45 +00:00
|
|
|
" begin delete from " + Attachment.TABLE_NAME +
|
2009-07-30 18:41:31 +00:00
|
|
|
" where " + AttachmentColumns.MESSAGE_KEY + "=old." + EmailContent.RECORD_ID +
|
|
|
|
"; end");
|
|
|
|
|
|
|
|
// Add triggers to keep unread count accurate per mailbox
|
|
|
|
|
2010-07-30 20:53:59 +00:00
|
|
|
// NOTE: SQLite's before triggers are not safe when recursive triggers are involved.
|
|
|
|
// Use caution when changing them.
|
|
|
|
|
2009-07-30 18:41:31 +00:00
|
|
|
// Insert a message; if flagRead is zero, add to the unread count of the message's mailbox
|
|
|
|
db.execSQL("create trigger unread_message_insert before insert on " + Message.TABLE_NAME +
|
|
|
|
" when NEW." + MessageColumns.FLAG_READ + "=0" +
|
|
|
|
" begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
|
|
|
|
'=' + MailboxColumns.UNREAD_COUNT + "+1" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; end");
|
|
|
|
|
|
|
|
// Delete a message; if flagRead is zero, decrement the unread count of the msg's mailbox
|
|
|
|
db.execSQL("create trigger unread_message_delete before delete on " + Message.TABLE_NAME +
|
|
|
|
" when OLD." + MessageColumns.FLAG_READ + "=0" +
|
|
|
|
" begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
|
|
|
|
'=' + MailboxColumns.UNREAD_COUNT + "-1" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; end");
|
|
|
|
|
|
|
|
// Change a message's mailbox
|
|
|
|
db.execSQL("create trigger unread_message_move before update of " +
|
|
|
|
MessageColumns.MAILBOX_KEY + " on " + Message.TABLE_NAME +
|
|
|
|
" when OLD." + MessageColumns.FLAG_READ + "=0" +
|
|
|
|
" begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
|
|
|
|
'=' + MailboxColumns.UNREAD_COUNT + "-1" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
|
|
|
|
'=' + MailboxColumns.UNREAD_COUNT + "+1" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; end");
|
|
|
|
|
|
|
|
// Change a message's read state
|
2009-12-04 02:32:58 +00:00
|
|
|
db.execSQL("create trigger unread_message_read before update of " +
|
2009-07-30 18:41:31 +00:00
|
|
|
MessageColumns.FLAG_READ + " on " + Message.TABLE_NAME +
|
|
|
|
" when OLD." + MessageColumns.FLAG_READ + "!=NEW." + MessageColumns.FLAG_READ +
|
|
|
|
" begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT +
|
|
|
|
'=' + MailboxColumns.UNREAD_COUNT + "+ case OLD." + MessageColumns.FLAG_READ +
|
|
|
|
" when 0 then -1 else 1 end" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; end");
|
2010-07-30 20:53:59 +00:00
|
|
|
|
|
|
|
// Add triggers to update message count per mailbox
|
|
|
|
|
|
|
|
// Insert a message.
|
|
|
|
db.execSQL("create trigger message_count_message_insert after insert on " +
|
|
|
|
Message.TABLE_NAME +
|
|
|
|
" begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT +
|
|
|
|
'=' + MailboxColumns.MESSAGE_COUNT + "+1" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; end");
|
|
|
|
|
|
|
|
// Delete a message; if flagRead is zero, decrement the unread count of the msg's mailbox
|
|
|
|
db.execSQL("create trigger message_count_message_delete after delete on " +
|
|
|
|
Message.TABLE_NAME +
|
|
|
|
" begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT +
|
|
|
|
'=' + MailboxColumns.MESSAGE_COUNT + "-1" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; end");
|
|
|
|
|
|
|
|
// Change a message's mailbox
|
|
|
|
db.execSQL("create trigger message_count_message_move after update of " +
|
|
|
|
MessageColumns.MAILBOX_KEY + " on " + Message.TABLE_NAME +
|
|
|
|
" begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT +
|
|
|
|
'=' + MailboxColumns.MESSAGE_COUNT + "-1" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT +
|
|
|
|
'=' + MailboxColumns.MESSAGE_COUNT + "+1" +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY +
|
|
|
|
"; end");
|
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
|
2009-09-16 02:27:05 +00:00
|
|
|
static void resetMessageTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2009-06-30 06:08:40 +00:00
|
|
|
try {
|
|
|
|
db.execSQL("drop table " + Message.TABLE_NAME);
|
|
|
|
db.execSQL("drop table " + Message.UPDATED_TABLE_NAME);
|
|
|
|
db.execSQL("drop table " + Message.DELETED_TABLE_NAME);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
createMessageTable(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createAccountTable(SQLiteDatabase db) {
|
2009-07-30 18:41:31 +00:00
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ AccountColumns.DISPLAY_NAME + " text, "
|
|
|
|
+ AccountColumns.EMAIL_ADDRESS + " text, "
|
|
|
|
+ AccountColumns.SYNC_KEY + " text, "
|
|
|
|
+ AccountColumns.SYNC_LOOKBACK + " integer, "
|
2009-07-22 22:13:30 +00:00
|
|
|
+ AccountColumns.SYNC_INTERVAL + " text, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ AccountColumns.HOST_AUTH_KEY_RECV + " integer, "
|
|
|
|
+ AccountColumns.HOST_AUTH_KEY_SEND + " integer, "
|
|
|
|
+ AccountColumns.FLAGS + " integer, "
|
|
|
|
+ AccountColumns.IS_DEFAULT + " integer, "
|
|
|
|
+ AccountColumns.COMPATIBILITY_UUID + " text, "
|
|
|
|
+ AccountColumns.SENDER_NAME + " text, "
|
2009-07-17 23:29:35 +00:00
|
|
|
+ AccountColumns.RINGTONE_URI + " text, "
|
2009-08-18 19:14:15 +00:00
|
|
|
+ AccountColumns.PROTOCOL_VERSION + " text, "
|
2010-01-21 19:48:02 +00:00
|
|
|
+ AccountColumns.NEW_MESSAGE_COUNT + " integer, "
|
2010-01-27 01:24:15 +00:00
|
|
|
+ AccountColumns.SECURITY_FLAGS + " integer, "
|
|
|
|
+ AccountColumns.SECURITY_SYNC_KEY + " text, "
|
|
|
|
+ AccountColumns.SIGNATURE + " text "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ ");";
|
|
|
|
db.execSQL("create table " + Account.TABLE_NAME + s);
|
2009-06-16 19:03:45 +00:00
|
|
|
// Deleting an account deletes associated Mailboxes and HostAuth's
|
2009-12-04 02:32:58 +00:00
|
|
|
db.execSQL("create trigger account_delete before delete on " + Account.TABLE_NAME +
|
2009-06-16 19:03:45 +00:00
|
|
|
" begin delete from " + Mailbox.TABLE_NAME +
|
2009-07-30 18:41:31 +00:00
|
|
|
" where " + MailboxColumns.ACCOUNT_KEY + "=old." + EmailContent.RECORD_ID +
|
2009-06-16 19:03:45 +00:00
|
|
|
"; delete from " + HostAuth.TABLE_NAME +
|
2009-07-31 01:17:52 +00:00
|
|
|
" where " + EmailContent.RECORD_ID + "=old." + AccountColumns.HOST_AUTH_KEY_RECV +
|
|
|
|
"; delete from " + HostAuth.TABLE_NAME +
|
|
|
|
" where " + EmailContent.RECORD_ID + "=old." + AccountColumns.HOST_AUTH_KEY_SEND +
|
2009-06-27 19:14:14 +00:00
|
|
|
"; end");
|
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
|
2009-09-16 02:27:05 +00:00
|
|
|
static void resetAccountTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2009-06-15 21:40:06 +00:00
|
|
|
try {
|
|
|
|
db.execSQL("drop table " + Account.TABLE_NAME);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
|
|
|
createAccountTable(db);
|
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
static void createHostAuthTable(SQLiteDatabase db) {
|
2009-07-30 18:41:31 +00:00
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ HostAuthColumns.PROTOCOL + " text, "
|
|
|
|
+ HostAuthColumns.ADDRESS + " text, "
|
|
|
|
+ HostAuthColumns.PORT + " integer, "
|
|
|
|
+ HostAuthColumns.FLAGS + " integer, "
|
|
|
|
+ HostAuthColumns.LOGIN + " text, "
|
|
|
|
+ HostAuthColumns.PASSWORD + " text, "
|
|
|
|
+ HostAuthColumns.DOMAIN + " text, "
|
|
|
|
+ HostAuthColumns.ACCOUNT_KEY + " integer"
|
|
|
|
+ ");";
|
|
|
|
db.execSQL("create table " + HostAuth.TABLE_NAME + s);
|
|
|
|
}
|
|
|
|
|
2009-09-16 02:27:05 +00:00
|
|
|
static void resetHostAuthTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2009-06-15 21:40:06 +00:00
|
|
|
try {
|
|
|
|
db.execSQL("drop table " + HostAuth.TABLE_NAME);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
|
|
|
createHostAuthTable(db);
|
|
|
|
}
|
|
|
|
|
2009-06-27 19:14:14 +00:00
|
|
|
static void createMailboxTable(SQLiteDatabase db) {
|
2009-07-30 18:41:31 +00:00
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ MailboxColumns.DISPLAY_NAME + " text, "
|
2009-09-03 23:42:20 +00:00
|
|
|
+ MailboxColumns.SERVER_ID + " text, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ MailboxColumns.PARENT_SERVER_ID + " text, "
|
2011-03-01 18:45:50 +00:00
|
|
|
+ MailboxColumns.PARENT_KEY + " integer, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ MailboxColumns.ACCOUNT_KEY + " integer, "
|
|
|
|
+ MailboxColumns.TYPE + " integer, "
|
|
|
|
+ MailboxColumns.DELIMITER + " integer, "
|
|
|
|
+ MailboxColumns.SYNC_KEY + " text, "
|
|
|
|
+ MailboxColumns.SYNC_LOOKBACK + " integer, "
|
2009-07-22 22:13:30 +00:00
|
|
|
+ MailboxColumns.SYNC_INTERVAL + " integer, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ MailboxColumns.SYNC_TIME + " integer, "
|
|
|
|
+ MailboxColumns.UNREAD_COUNT + " integer, "
|
|
|
|
+ MailboxColumns.FLAG_VISIBLE + " integer, "
|
|
|
|
+ MailboxColumns.FLAGS + " integer, "
|
2009-08-05 15:41:16 +00:00
|
|
|
+ MailboxColumns.VISIBLE_LIMIT + " integer, "
|
2010-07-30 20:53:59 +00:00
|
|
|
+ MailboxColumns.SYNC_STATUS + " text, "
|
|
|
|
+ MailboxColumns.MESSAGE_COUNT + " integer not null default 0"
|
2009-07-05 19:54:49 +00:00
|
|
|
+ ");";
|
2009-06-27 19:14:14 +00:00
|
|
|
db.execSQL("create table " + Mailbox.TABLE_NAME + s);
|
2009-07-30 18:41:31 +00:00
|
|
|
db.execSQL("create index mailbox_" + MailboxColumns.SERVER_ID
|
2009-06-15 21:40:06 +00:00
|
|
|
+ " on " + Mailbox.TABLE_NAME + " (" + MailboxColumns.SERVER_ID + ")");
|
2009-07-30 18:41:31 +00:00
|
|
|
db.execSQL("create index mailbox_" + MailboxColumns.ACCOUNT_KEY
|
2009-06-15 21:40:06 +00:00
|
|
|
+ " on " + Mailbox.TABLE_NAME + " (" + MailboxColumns.ACCOUNT_KEY + ")");
|
2009-10-13 23:25:00 +00:00
|
|
|
// Deleting a Mailbox deletes associated Messages in all three tables
|
|
|
|
db.execSQL(TRIGGER_MAILBOX_DELETE);
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 02:27:05 +00:00
|
|
|
static void resetMailboxTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2009-06-15 21:40:06 +00:00
|
|
|
try {
|
|
|
|
db.execSQL("drop table " + Mailbox.TABLE_NAME);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
|
|
|
createMailboxTable(db);
|
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
static void createAttachmentTable(SQLiteDatabase db) {
|
2009-07-30 18:41:31 +00:00
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ AttachmentColumns.FILENAME + " text, "
|
|
|
|
+ AttachmentColumns.MIME_TYPE + " text, "
|
|
|
|
+ AttachmentColumns.SIZE + " integer, "
|
|
|
|
+ AttachmentColumns.CONTENT_ID + " text, "
|
|
|
|
+ AttachmentColumns.CONTENT_URI + " text, "
|
|
|
|
+ AttachmentColumns.MESSAGE_KEY + " integer, "
|
|
|
|
+ AttachmentColumns.LOCATION + " text, "
|
2010-02-22 20:57:33 +00:00
|
|
|
+ AttachmentColumns.ENCODING + " text, "
|
|
|
|
+ AttachmentColumns.CONTENT + " text, "
|
2010-03-12 21:30:26 +00:00
|
|
|
+ AttachmentColumns.FLAGS + " integer, "
|
2010-12-09 01:11:04 +00:00
|
|
|
+ AttachmentColumns.CONTENT_BYTES + " blob, "
|
|
|
|
+ AttachmentColumns.ACCOUNT_KEY + " integer"
|
2009-06-15 21:40:06 +00:00
|
|
|
+ ");";
|
|
|
|
db.execSQL("create table " + Attachment.TABLE_NAME + s);
|
2009-08-12 10:51:26 +00:00
|
|
|
db.execSQL(createIndex(Attachment.TABLE_NAME, AttachmentColumns.MESSAGE_KEY));
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 02:27:05 +00:00
|
|
|
static void resetAttachmentTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2009-06-15 21:40:06 +00:00
|
|
|
try {
|
|
|
|
db.execSQL("drop table " + Attachment.TABLE_NAME);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
|
|
|
createAttachmentTable(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createBodyTable(SQLiteDatabase db) {
|
2009-07-30 18:41:31 +00:00
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
2009-06-15 21:40:06 +00:00
|
|
|
+ BodyColumns.MESSAGE_KEY + " integer, "
|
|
|
|
+ BodyColumns.HTML_CONTENT + " text, "
|
2009-09-02 06:19:12 +00:00
|
|
|
+ BodyColumns.TEXT_CONTENT + " text, "
|
|
|
|
+ BodyColumns.HTML_REPLY + " text, "
|
2009-09-07 23:03:02 +00:00
|
|
|
+ BodyColumns.TEXT_REPLY + " text, "
|
2009-09-23 01:38:28 +00:00
|
|
|
+ BodyColumns.SOURCE_MESSAGE_KEY + " text, "
|
|
|
|
+ BodyColumns.INTRO_TEXT + " text"
|
2009-06-15 21:40:06 +00:00
|
|
|
+ ");";
|
|
|
|
db.execSQL("create table " + Body.TABLE_NAME + s);
|
2009-08-12 10:51:26 +00:00
|
|
|
db.execSQL(createIndex(Body.TABLE_NAME, BodyColumns.MESSAGE_KEY));
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void upgradeBodyTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2009-09-19 03:44:26 +00:00
|
|
|
if (oldVersion < 5) {
|
2009-09-07 23:03:02 +00:00
|
|
|
try {
|
|
|
|
db.execSQL("drop table " + Body.TABLE_NAME);
|
|
|
|
createBodyTable(db);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
2009-09-23 01:38:28 +00:00
|
|
|
} else if (oldVersion == 5) {
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Body.TABLE_NAME
|
|
|
|
+ " add " + BodyColumns.INTRO_TEXT + " text");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProviderBody.db from v5 to v6", e);
|
|
|
|
}
|
|
|
|
oldVersion = 6;
|
2009-09-16 02:27:05 +00:00
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private SQLiteDatabase mDatabase;
|
2009-06-15 21:40:06 +00:00
|
|
|
private SQLiteDatabase mBodyDatabase;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-08-24 06:45:54 +00:00
|
|
|
public synchronized SQLiteDatabase getDatabase(Context context) {
|
2009-11-19 01:11:33 +00:00
|
|
|
// Always return the cached database, if we've got one
|
|
|
|
if (mDatabase != null) {
|
2009-05-26 23:40:34 +00:00
|
|
|
return mDatabase;
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
|
|
|
|
// Whenever we create or re-cache the databases, make sure that we haven't lost one
|
|
|
|
// to corruption
|
|
|
|
checkDatabases();
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
DatabaseHelper helper = new DatabaseHelper(context, DATABASE_NAME);
|
|
|
|
mDatabase = helper.getWritableDatabase();
|
2009-06-15 21:40:06 +00:00
|
|
|
if (mDatabase != null) {
|
2009-05-26 23:40:34 +00:00
|
|
|
mDatabase.setLockingEnabled(true);
|
2009-08-18 18:44:27 +00:00
|
|
|
BodyDatabaseHelper bodyHelper = new BodyDatabaseHelper(context, BODY_DATABASE_NAME);
|
|
|
|
mBodyDatabase = bodyHelper.getWritableDatabase();
|
|
|
|
if (mBodyDatabase != null) {
|
|
|
|
mBodyDatabase.setLockingEnabled(true);
|
|
|
|
String bodyFileName = mBodyDatabase.getPath();
|
|
|
|
mDatabase.execSQL("attach \"" + bodyFileName + "\" as BodyDatabase");
|
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
2009-10-13 23:25:00 +00:00
|
|
|
|
|
|
|
// Check for any orphaned Messages in the updated/deleted tables
|
|
|
|
deleteOrphans(mDatabase, Message.UPDATED_TABLE_NAME);
|
|
|
|
deleteOrphans(mDatabase, Message.DELETED_TABLE_NAME);
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
return mDatabase;
|
|
|
|
}
|
|
|
|
|
2009-10-13 23:25:00 +00:00
|
|
|
/*package*/ static SQLiteDatabase getReadableDatabase(Context context) {
|
|
|
|
DatabaseHelper helper = new EmailProvider().new DatabaseHelper(context, DATABASE_NAME);
|
|
|
|
return helper.getReadableDatabase();
|
|
|
|
}
|
|
|
|
|
2010-08-20 21:56:44 +00:00
|
|
|
/** {@inheritDoc} */
|
|
|
|
@Override
|
|
|
|
public void shutdown() {
|
|
|
|
if (mDatabase != null) {
|
|
|
|
mDatabase.close();
|
|
|
|
mDatabase = null;
|
|
|
|
}
|
|
|
|
if (mBodyDatabase != null) {
|
|
|
|
mBodyDatabase.close();
|
|
|
|
mBodyDatabase = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-13 23:25:00 +00:00
|
|
|
/*package*/ static void deleteOrphans(SQLiteDatabase database, String tableName) {
|
|
|
|
if (database != null) {
|
|
|
|
// We'll look at all of the items in the table; there won't be many typically
|
|
|
|
Cursor c = database.query(tableName, ORPHANS_PROJECTION, null, null, null, null, null);
|
|
|
|
// Usually, there will be nothing in these tables, so make a quick check
|
|
|
|
try {
|
|
|
|
if (c.getCount() == 0) return;
|
|
|
|
ArrayList<Long> foundMailboxes = new ArrayList<Long>();
|
|
|
|
ArrayList<Long> notFoundMailboxes = new ArrayList<Long>();
|
|
|
|
ArrayList<Long> deleteList = new ArrayList<Long>();
|
|
|
|
String[] bindArray = new String[1];
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
// Get the mailbox key and see if we've already found this mailbox
|
|
|
|
// If so, we're fine
|
|
|
|
long mailboxId = c.getLong(ORPHANS_MAILBOX_KEY);
|
|
|
|
// If we already know this mailbox doesn't exist, mark the message for deletion
|
|
|
|
if (notFoundMailboxes.contains(mailboxId)) {
|
|
|
|
deleteList.add(c.getLong(ORPHANS_ID));
|
|
|
|
// If we don't know about this mailbox, we'll try to find it
|
|
|
|
} else if (!foundMailboxes.contains(mailboxId)) {
|
|
|
|
bindArray[0] = Long.toString(mailboxId);
|
|
|
|
Cursor boxCursor = database.query(Mailbox.TABLE_NAME,
|
|
|
|
Mailbox.ID_PROJECTION, WHERE_ID, bindArray, null, null, null);
|
|
|
|
try {
|
|
|
|
// If it exists, we'll add it to the "found" mailboxes
|
|
|
|
if (boxCursor.moveToFirst()) {
|
|
|
|
foundMailboxes.add(mailboxId);
|
|
|
|
// Otherwise, we'll add to "not found" and mark the message for deletion
|
|
|
|
} else {
|
|
|
|
notFoundMailboxes.add(mailboxId);
|
|
|
|
deleteList.add(c.getLong(ORPHANS_ID));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
boxCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now, delete the orphan messages
|
|
|
|
for (long messageId: deleteList) {
|
|
|
|
bindArray[0] = Long.toString(messageId);
|
|
|
|
database.delete(tableName, WHERE_ID, bindArray);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
private class BodyDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
BodyDatabaseHelper(Context context, String name) {
|
2009-09-23 01:31:10 +00:00
|
|
|
super(context, name, null, BODY_DATABASE_VERSION);
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
2009-11-19 01:11:33 +00:00
|
|
|
Log.d(TAG, "Creating EmailProviderBody database");
|
2009-06-15 21:40:06 +00:00
|
|
|
createBodyTable(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
upgradeBodyTable(db, oldVersion, newVersion);
|
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
@Override
|
|
|
|
public void onOpen(SQLiteDatabase db) {
|
|
|
|
}
|
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private class DatabaseHelper extends SQLiteOpenHelper {
|
2009-09-16 02:27:05 +00:00
|
|
|
Context mContext;
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
DatabaseHelper(Context context, String name) {
|
2009-09-23 01:31:10 +00:00
|
|
|
super(context, name, null, DATABASE_VERSION);
|
2009-09-16 02:27:05 +00:00
|
|
|
mContext = context;
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
2009-11-19 01:11:33 +00:00
|
|
|
Log.d(TAG, "Creating EmailProvider database");
|
2009-05-26 23:40:34 +00:00
|
|
|
// Create all tables here; each class has its own method
|
2009-06-15 21:40:06 +00:00
|
|
|
createMessageTable(db);
|
|
|
|
createAttachmentTable(db);
|
|
|
|
createMailboxTable(db);
|
|
|
|
createHostAuthTable(db);
|
|
|
|
createAccountTable(db);
|
2009-06-27 19:14:14 +00:00
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2009-09-19 03:44:26 +00:00
|
|
|
// For versions prior to 5, delete all data
|
|
|
|
// Versions >= 5 require that data be preserved!
|
|
|
|
if (oldVersion < 5) {
|
2010-02-01 23:34:03 +00:00
|
|
|
android.accounts.Account[] accounts = AccountManager.get(mContext)
|
2011-02-13 02:56:09 +00:00
|
|
|
.getAccountsByType(AccountManagerTypes.TYPE_EXCHANGE);
|
2009-09-16 02:27:05 +00:00
|
|
|
for (android.accounts.Account account: accounts) {
|
|
|
|
AccountManager.get(mContext).removeAccount(account, null, null);
|
|
|
|
}
|
|
|
|
resetMessageTable(db, oldVersion, newVersion);
|
|
|
|
resetAttachmentTable(db, oldVersion, newVersion);
|
|
|
|
resetMailboxTable(db, oldVersion, newVersion);
|
|
|
|
resetHostAuthTable(db, oldVersion, newVersion);
|
|
|
|
resetAccountTable(db, oldVersion, newVersion);
|
2009-09-23 01:31:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (oldVersion == 5) {
|
|
|
|
// Message Tables: Add SyncColumns.SERVER_TIMESTAMP
|
2009-09-23 01:38:28 +00:00
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Message.TABLE_NAME
|
|
|
|
+ " add column " + SyncColumns.SERVER_TIMESTAMP + " integer" + ";");
|
|
|
|
db.execSQL("alter table " + Message.UPDATED_TABLE_NAME
|
|
|
|
+ " add column " + SyncColumns.SERVER_TIMESTAMP + " integer" + ";");
|
|
|
|
db.execSQL("alter table " + Message.DELETED_TABLE_NAME
|
|
|
|
+ " add column " + SyncColumns.SERVER_TIMESTAMP + " integer" + ";");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from v5 to v6", e);
|
|
|
|
}
|
2009-09-23 01:31:10 +00:00
|
|
|
oldVersion = 6;
|
2009-09-16 02:27:05 +00:00
|
|
|
}
|
2009-10-13 23:25:00 +00:00
|
|
|
if (oldVersion == 6) {
|
|
|
|
// Use the newer mailbox_delete trigger
|
2009-10-20 05:15:45 +00:00
|
|
|
db.execSQL("drop trigger mailbox_delete;");
|
2009-10-13 23:25:00 +00:00
|
|
|
db.execSQL(TRIGGER_MAILBOX_DELETE);
|
|
|
|
oldVersion = 7;
|
|
|
|
}
|
2010-01-21 19:48:02 +00:00
|
|
|
if (oldVersion == 7) {
|
|
|
|
// add the security (provisioning) column
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Account.TABLE_NAME
|
|
|
|
+ " add column " + AccountColumns.SECURITY_FLAGS + " integer" + ";");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 7 to 8 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 8;
|
|
|
|
}
|
2010-01-27 01:24:15 +00:00
|
|
|
if (oldVersion == 8) {
|
|
|
|
// accounts: add security sync key & user signature columns
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Account.TABLE_NAME
|
|
|
|
+ " add column " + AccountColumns.SECURITY_SYNC_KEY + " text" + ";");
|
|
|
|
db.execSQL("alter table " + Account.TABLE_NAME
|
|
|
|
+ " add column " + AccountColumns.SIGNATURE + " text" + ";");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 8 to 9 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 9;
|
|
|
|
}
|
2010-02-16 00:01:38 +00:00
|
|
|
if (oldVersion == 9) {
|
|
|
|
// Message: add meeting info column into Message tables
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Message.TABLE_NAME
|
|
|
|
+ " add column " + MessageColumns.MEETING_INFO + " text" + ";");
|
|
|
|
db.execSQL("alter table " + Message.UPDATED_TABLE_NAME
|
|
|
|
+ " add column " + MessageColumns.MEETING_INFO + " text" + ";");
|
|
|
|
db.execSQL("alter table " + Message.DELETED_TABLE_NAME
|
|
|
|
+ " add column " + MessageColumns.MEETING_INFO + " text" + ";");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 9 to 10 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 10;
|
|
|
|
}
|
2010-02-22 20:57:33 +00:00
|
|
|
if (oldVersion == 10) {
|
|
|
|
// Attachment: add content and flags columns
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Attachment.TABLE_NAME
|
|
|
|
+ " add column " + AttachmentColumns.CONTENT + " text" + ";");
|
|
|
|
db.execSQL("alter table " + Attachment.TABLE_NAME
|
|
|
|
+ " add column " + AttachmentColumns.FLAGS + " integer" + ";");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 10 to 11 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 11;
|
|
|
|
}
|
2010-03-12 21:30:26 +00:00
|
|
|
if (oldVersion == 11) {
|
|
|
|
// Attachment: add content_bytes
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Attachment.TABLE_NAME
|
|
|
|
+ " add column " + AttachmentColumns.CONTENT_BYTES + " blob" + ";");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 11 to 12 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 12;
|
|
|
|
}
|
2010-07-30 20:53:59 +00:00
|
|
|
if (oldVersion == 12) {
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Mailbox.TABLE_NAME
|
|
|
|
+ " add column " + Mailbox.MESSAGE_COUNT
|
|
|
|
+" integer not null default 0" + ";");
|
|
|
|
recalculateMessageCount(db);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 12 to 13 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 13;
|
|
|
|
}
|
2010-09-02 02:06:15 +00:00
|
|
|
if (oldVersion == 13) {
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Message.TABLE_NAME
|
|
|
|
+ " add column " + Message.SNIPPET
|
|
|
|
+" text" + ";");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 13 to 14 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 14;
|
|
|
|
}
|
2010-09-07 19:36:48 +00:00
|
|
|
if (oldVersion == 14) {
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Message.DELETED_TABLE_NAME
|
|
|
|
+ " add column " + Message.SNIPPET +" text" + ";");
|
|
|
|
db.execSQL("alter table " + Message.UPDATED_TABLE_NAME
|
|
|
|
+ " add column " + Message.SNIPPET +" text" + ";");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 14 to 15 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 15;
|
|
|
|
}
|
2010-12-09 01:11:04 +00:00
|
|
|
if (oldVersion == 15) {
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Attachment.TABLE_NAME
|
|
|
|
+ " add column " + Attachment.ACCOUNT_KEY +" integer" + ";");
|
|
|
|
// Update all existing attachments to add the accountKey data
|
|
|
|
db.execSQL("update " + Attachment.TABLE_NAME + " set " +
|
|
|
|
Attachment.ACCOUNT_KEY + "= (SELECT " + Message.TABLE_NAME + "." +
|
|
|
|
Message.ACCOUNT_KEY + " from " + Message.TABLE_NAME + " where " +
|
|
|
|
Message.TABLE_NAME + "." + Message.RECORD_ID + " = " +
|
|
|
|
Attachment.TABLE_NAME + "." + Attachment.MESSAGE_KEY + ")");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 15 to 16 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 16;
|
|
|
|
}
|
2011-03-01 18:45:50 +00:00
|
|
|
if (oldVersion == 16) {
|
|
|
|
try {
|
|
|
|
db.execSQL("alter table " + Mailbox.TABLE_NAME
|
|
|
|
+ " add column " + Mailbox.PARENT_KEY + " integer;");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 16 to 17 " + e);
|
|
|
|
}
|
|
|
|
oldVersion = 17;
|
|
|
|
}
|
2011-04-22 22:45:11 +00:00
|
|
|
if (oldVersion == 17) {
|
|
|
|
upgradeFromVersion17ToVersion18(db);
|
|
|
|
oldVersion = 18;
|
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onOpen(SQLiteDatabase db) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
2010-12-16 20:28:16 +00:00
|
|
|
final int match = findMatch(uri, "delete");
|
2009-06-15 21:40:06 +00:00
|
|
|
Context context = getContext();
|
2009-08-06 00:31:50 +00:00
|
|
|
// Pick the correct database for this operation
|
|
|
|
// If we're in a transaction already (which would happen during applyBatch), then the
|
|
|
|
// body database is already attached to the email database and any attempt to use the
|
|
|
|
// body database directly will result in a SQLiteException (the database is locked)
|
2009-08-18 18:44:27 +00:00
|
|
|
SQLiteDatabase db = getDatabase(context);
|
2009-05-26 23:40:34 +00:00
|
|
|
int table = match >> BASE_SHIFT;
|
2009-06-16 19:03:45 +00:00
|
|
|
String id = "0";
|
2009-08-06 00:31:50 +00:00
|
|
|
boolean messageDeletion = false;
|
2010-09-23 16:19:44 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2010-10-27 23:50:54 +00:00
|
|
|
ContentCache cache = CONTENT_CACHES[table];
|
|
|
|
String tableName = TABLE_NAMES[table];
|
2009-07-05 19:54:49 +00:00
|
|
|
int result = -1;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-06-16 19:03:45 +00:00
|
|
|
try {
|
|
|
|
switch (match) {
|
|
|
|
// These are cases in which one or more Messages might get deleted, either by
|
|
|
|
// cascade or explicitly
|
|
|
|
case MAILBOX_ID:
|
|
|
|
case MAILBOX:
|
|
|
|
case ACCOUNT_ID:
|
|
|
|
case ACCOUNT:
|
|
|
|
case MESSAGE:
|
2009-06-27 19:14:14 +00:00
|
|
|
case SYNCED_MESSAGE_ID:
|
2009-06-16 19:03:45 +00:00
|
|
|
case MESSAGE_ID:
|
|
|
|
// Handle lost Body records here, since this cannot be done in a trigger
|
|
|
|
// The process is:
|
2009-08-18 18:44:27 +00:00
|
|
|
// 1) Begin a transaction, ensuring that both databases are affected atomically
|
|
|
|
// 2) Do the requested deletion, with cascading deletions handled in triggers
|
|
|
|
// 3) End the transaction, committing all changes atomically
|
2009-09-10 18:52:36 +00:00
|
|
|
//
|
|
|
|
// Bodies are auto-deleted here; Attachments are auto-deleted via trigger
|
2009-08-06 00:31:50 +00:00
|
|
|
messageDeletion = true;
|
2009-09-19 03:36:15 +00:00
|
|
|
db.beginTransaction();
|
2010-09-23 16:19:44 +00:00
|
|
|
resolver.notifyChange(Message.NOTIFIER_URI, null);
|
2009-06-16 19:03:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (match) {
|
|
|
|
case BODY_ID:
|
2009-06-27 19:14:14 +00:00
|
|
|
case DELETED_MESSAGE_ID:
|
|
|
|
case SYNCED_MESSAGE_ID:
|
2009-06-16 19:03:45 +00:00
|
|
|
case MESSAGE_ID:
|
|
|
|
case UPDATED_MESSAGE_ID:
|
|
|
|
case ATTACHMENT_ID:
|
|
|
|
case MAILBOX_ID:
|
|
|
|
case ACCOUNT_ID:
|
|
|
|
case HOSTAUTH_ID:
|
|
|
|
id = uri.getPathSegments().get(1);
|
2009-06-27 19:14:14 +00:00
|
|
|
if (match == SYNCED_MESSAGE_ID) {
|
|
|
|
// For synced messages, first copy the old message to the deleted table and
|
|
|
|
// delete it from the updated table (in case it was updated first)
|
|
|
|
// Note that this is all within a transaction, for atomicity
|
|
|
|
db.execSQL(DELETED_MESSAGE_INSERT + id);
|
|
|
|
db.execSQL(UPDATED_MESSAGE_DELETE + id);
|
|
|
|
}
|
2010-10-27 23:50:54 +00:00
|
|
|
if (cache != null) {
|
|
|
|
cache.lock(id);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
result = db.delete(tableName, whereWithId(id, selection), selectionArgs);
|
|
|
|
if (cache != null) {
|
|
|
|
switch(match) {
|
|
|
|
case ACCOUNT_ID:
|
|
|
|
// Account deletion will clear all of the caches, as HostAuth's,
|
|
|
|
// Mailboxes, and Messages will be deleted in the process
|
|
|
|
sCacheMailbox.invalidate("Delete", uri, selection);
|
|
|
|
sCacheHostAuth.invalidate("Delete", uri, selection);
|
|
|
|
//$FALL-THROUGH$
|
|
|
|
case MAILBOX_ID:
|
|
|
|
// Mailbox deletion will clear the Message cache
|
|
|
|
sCacheMessage.invalidate("Delete", uri, selection);
|
|
|
|
//$FALL-THROUGH$
|
|
|
|
case SYNCED_MESSAGE_ID:
|
|
|
|
case MESSAGE_ID:
|
|
|
|
case HOSTAUTH_ID:
|
|
|
|
cache.invalidate("Delete", uri, selection);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cache != null) {
|
|
|
|
cache.unlock(id);
|
|
|
|
}
|
|
|
|
}
|
2009-06-16 19:03:45 +00:00
|
|
|
break;
|
2009-09-10 18:52:36 +00:00
|
|
|
case ATTACHMENTS_MESSAGE_ID:
|
|
|
|
// All attachments for the given message
|
|
|
|
id = uri.getPathSegments().get(2);
|
2010-10-27 23:50:54 +00:00
|
|
|
result = db.delete(tableName,
|
2009-09-10 18:52:36 +00:00
|
|
|
whereWith(Attachment.MESSAGE_KEY + "=" + id, selection), selectionArgs);
|
|
|
|
break;
|
|
|
|
|
2009-06-16 19:03:45 +00:00
|
|
|
case BODY:
|
|
|
|
case MESSAGE:
|
2009-06-27 19:14:14 +00:00
|
|
|
case DELETED_MESSAGE:
|
2009-06-16 19:03:45 +00:00
|
|
|
case UPDATED_MESSAGE:
|
|
|
|
case ATTACHMENT:
|
|
|
|
case MAILBOX:
|
|
|
|
case ACCOUNT:
|
|
|
|
case HOSTAUTH:
|
2010-10-27 23:50:54 +00:00
|
|
|
switch(match) {
|
|
|
|
// See the comments above for deletion of ACCOUNT_ID, etc
|
|
|
|
case ACCOUNT:
|
|
|
|
sCacheMailbox.invalidate("Delete", uri, selection);
|
|
|
|
sCacheHostAuth.invalidate("Delete", uri, selection);
|
|
|
|
//$FALL-THROUGH$
|
|
|
|
case MAILBOX:
|
|
|
|
sCacheMessage.invalidate("Delete", uri, selection);
|
|
|
|
//$FALL-THROUGH$
|
|
|
|
case MESSAGE:
|
|
|
|
case HOSTAUTH:
|
|
|
|
cache.invalidate("Delete", uri, selection);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = db.delete(tableName, selection, selectionArgs);
|
2009-06-16 19:03:45 +00:00
|
|
|
break;
|
2009-09-10 18:52:36 +00:00
|
|
|
|
2009-06-16 19:03:45 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
2009-08-06 00:31:50 +00:00
|
|
|
if (messageDeletion) {
|
2009-08-03 13:05:50 +00:00
|
|
|
if (match == MESSAGE_ID) {
|
2009-06-24 19:48:57 +00:00
|
|
|
// Delete the Body record associated with the deleted message
|
2009-06-27 19:14:14 +00:00
|
|
|
db.execSQL(DELETE_BODY + id);
|
2009-08-03 13:05:50 +00:00
|
|
|
} else {
|
|
|
|
// Delete any orphaned Body records
|
|
|
|
db.execSQL(DELETE_ORPHAN_BODIES);
|
2009-08-06 00:31:50 +00:00
|
|
|
}
|
2009-09-19 03:36:15 +00:00
|
|
|
db.setTransactionSuccessful();
|
2009-06-24 19:48:57 +00:00
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
} catch (SQLiteException e) {
|
|
|
|
checkDatabases();
|
|
|
|
throw e;
|
2009-06-16 19:03:45 +00:00
|
|
|
} finally {
|
2009-08-06 00:31:50 +00:00
|
|
|
if (messageDeletion) {
|
2009-09-19 03:36:15 +00:00
|
|
|
db.endTransaction();
|
2009-06-24 19:48:57 +00:00
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
2010-09-14 23:28:50 +00:00
|
|
|
|
|
|
|
// Notify all existing cursors.
|
2010-09-23 16:19:44 +00:00
|
|
|
resolver.notifyChange(EmailContent.CONTENT_URI, null);
|
2009-06-01 21:34:16 +00:00
|
|
|
return result;
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
// Use the email- prefix because message, mailbox, and account are so generic (e.g. SMS, IM)
|
|
|
|
public String getType(Uri uri) {
|
2010-12-16 20:28:16 +00:00
|
|
|
int match = findMatch(uri, "getType");
|
2009-05-26 23:40:34 +00:00
|
|
|
switch (match) {
|
2009-06-15 21:40:06 +00:00
|
|
|
case BODY_ID:
|
|
|
|
return "vnd.android.cursor.item/email-body";
|
|
|
|
case BODY:
|
2010-10-14 02:04:46 +00:00
|
|
|
return "vnd.android.cursor.dir/email-body";
|
2009-06-17 17:22:37 +00:00
|
|
|
case UPDATED_MESSAGE_ID:
|
2009-06-15 21:40:06 +00:00
|
|
|
case MESSAGE_ID:
|
2010-10-14 02:04:46 +00:00
|
|
|
// NOTE: According to the framework folks, we're supposed to invent mime types as
|
|
|
|
// a way of passing information to drag & drop recipients.
|
|
|
|
// If there's a mailboxId parameter in the url, we respond with a mime type that
|
|
|
|
// has -n appended, where n is the mailboxId of the message. The drag & drop code
|
|
|
|
// uses this information to know not to allow dragging the item to its own mailbox
|
|
|
|
String mimeType = EMAIL_MESSAGE_MIME_TYPE;
|
|
|
|
String mailboxId = uri.getQueryParameter(MESSAGE_URI_PARAMETER_MAILBOX_ID);
|
|
|
|
if (mailboxId != null) {
|
|
|
|
mimeType += "-" + mailboxId;
|
|
|
|
}
|
|
|
|
return mimeType;
|
2009-06-17 17:22:37 +00:00
|
|
|
case UPDATED_MESSAGE:
|
2009-05-29 21:24:34 +00:00
|
|
|
case MESSAGE:
|
2009-06-15 21:40:06 +00:00
|
|
|
return "vnd.android.cursor.dir/email-message";
|
2009-05-29 21:24:34 +00:00
|
|
|
case MAILBOX:
|
|
|
|
return "vnd.android.cursor.dir/email-mailbox";
|
|
|
|
case MAILBOX_ID:
|
|
|
|
return "vnd.android.cursor.item/email-mailbox";
|
|
|
|
case ACCOUNT:
|
|
|
|
return "vnd.android.cursor.dir/email-account";
|
|
|
|
case ACCOUNT_ID:
|
|
|
|
return "vnd.android.cursor.item/email-account";
|
2009-07-16 23:03:40 +00:00
|
|
|
case ATTACHMENTS_MESSAGE_ID:
|
2009-05-29 21:24:34 +00:00
|
|
|
case ATTACHMENT:
|
|
|
|
return "vnd.android.cursor.dir/email-attachment";
|
|
|
|
case ATTACHMENT_ID:
|
2010-08-10 00:48:53 +00:00
|
|
|
return EMAIL_ATTACHMENT_MIME_TYPE;
|
2009-05-29 21:24:34 +00:00
|
|
|
case HOSTAUTH:
|
|
|
|
return "vnd.android.cursor.dir/email-hostauth";
|
|
|
|
case HOSTAUTH_ID:
|
|
|
|
return "vnd.android.cursor.item/email-hostauth";
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
2010-12-16 20:28:16 +00:00
|
|
|
int match = findMatch(uri, "insert");
|
2009-06-15 21:40:06 +00:00
|
|
|
Context context = getContext();
|
2010-09-23 16:19:44 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
|
2009-08-06 00:31:50 +00:00
|
|
|
// See the comment at delete(), above
|
2009-08-18 18:44:27 +00:00
|
|
|
SQLiteDatabase db = getDatabase(context);
|
2009-05-26 23:40:34 +00:00
|
|
|
int table = match >> BASE_SHIFT;
|
2009-06-15 21:40:06 +00:00
|
|
|
long id;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2010-09-10 21:37:01 +00:00
|
|
|
// We do NOT allow setting of unreadCount/messageCount via the provider
|
|
|
|
// These columns are maintained via triggers
|
|
|
|
if (match == MAILBOX_ID || match == MAILBOX) {
|
|
|
|
values.put(MailboxColumns.UNREAD_COUNT, 0);
|
|
|
|
values.put(MailboxColumns.MESSAGE_COUNT, 0);
|
|
|
|
}
|
2010-09-10 20:06:07 +00:00
|
|
|
|
2009-06-01 21:34:16 +00:00
|
|
|
Uri resultUri = null;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-11-19 01:11:33 +00:00
|
|
|
try {
|
|
|
|
switch (match) {
|
2010-09-23 16:19:44 +00:00
|
|
|
case MESSAGE:
|
|
|
|
resolver.notifyChange(Message.NOTIFIER_URI, null);
|
|
|
|
//$FALL-THROUGH$
|
2009-11-19 01:11:33 +00:00
|
|
|
case UPDATED_MESSAGE:
|
|
|
|
case DELETED_MESSAGE:
|
|
|
|
case BODY:
|
|
|
|
case ATTACHMENT:
|
|
|
|
case MAILBOX:
|
|
|
|
case ACCOUNT:
|
|
|
|
case HOSTAUTH:
|
|
|
|
id = db.insert(TABLE_NAMES[table], "foo", values);
|
|
|
|
resultUri = ContentUris.withAppendedId(uri, id);
|
|
|
|
// Clients shouldn't normally be adding rows to these tables, as they are
|
|
|
|
// maintained by triggers. However, we need to be able to do this for unit
|
|
|
|
// testing, so we allow the insert and then throw the same exception that we
|
|
|
|
// would if this weren't allowed.
|
|
|
|
if (match == UPDATED_MESSAGE || match == DELETED_MESSAGE) {
|
|
|
|
throw new IllegalArgumentException("Unknown URL " + uri);
|
|
|
|
}
|
2010-08-10 00:48:53 +00:00
|
|
|
if (match == ATTACHMENT) {
|
2010-12-09 01:11:04 +00:00
|
|
|
int flags = 0;
|
2010-08-10 00:48:53 +00:00
|
|
|
if (values.containsKey(Attachment.FLAGS)) {
|
2010-12-09 01:11:04 +00:00
|
|
|
flags = values.getAsInteger(Attachment.FLAGS);
|
2010-08-10 00:48:53 +00:00
|
|
|
}
|
2010-12-09 01:11:04 +00:00
|
|
|
// Report all new attachments to the download service
|
|
|
|
AttachmentDownloadService.attachmentChanged(id, flags);
|
2010-08-10 00:48:53 +00:00
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
break;
|
|
|
|
case MAILBOX_ID:
|
|
|
|
// This implies adding a message to a mailbox
|
|
|
|
// Hmm, a problem here is that we can't link the account as well, so it must be
|
|
|
|
// already in the values...
|
|
|
|
id = Long.parseLong(uri.getPathSegments().get(1));
|
|
|
|
values.put(MessageColumns.MAILBOX_KEY, id);
|
2010-09-14 23:28:50 +00:00
|
|
|
return insert(Message.CONTENT_URI, values); // Recurse
|
2009-11-19 01:11:33 +00:00
|
|
|
case MESSAGE_ID:
|
|
|
|
// This implies adding an attachment to a message.
|
|
|
|
id = Long.parseLong(uri.getPathSegments().get(1));
|
|
|
|
values.put(AttachmentColumns.MESSAGE_KEY, id);
|
2010-09-14 23:28:50 +00:00
|
|
|
return insert(Attachment.CONTENT_URI, values); // Recurse
|
2009-11-19 01:11:33 +00:00
|
|
|
case ACCOUNT_ID:
|
|
|
|
// This implies adding a mailbox to an account.
|
|
|
|
id = Long.parseLong(uri.getPathSegments().get(1));
|
|
|
|
values.put(MailboxColumns.ACCOUNT_KEY, id);
|
2010-09-14 23:28:50 +00:00
|
|
|
return insert(Mailbox.CONTENT_URI, values); // Recurse
|
2009-11-19 01:11:33 +00:00
|
|
|
case ATTACHMENTS_MESSAGE_ID:
|
|
|
|
id = db.insert(TABLE_NAMES[table], "foo", values);
|
|
|
|
resultUri = ContentUris.withAppendedId(Attachment.CONTENT_URI, id);
|
|
|
|
break;
|
|
|
|
default:
|
2009-10-13 23:25:00 +00:00
|
|
|
throw new IllegalArgumentException("Unknown URL " + uri);
|
2009-11-19 01:11:33 +00:00
|
|
|
}
|
|
|
|
} catch (SQLiteException e) {
|
|
|
|
checkDatabases();
|
|
|
|
throw e;
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2010-09-14 23:28:50 +00:00
|
|
|
// Notify all existing cursors.
|
2010-09-23 16:19:44 +00:00
|
|
|
resolver.notifyChange(EmailContent.CONTENT_URI, null);
|
2009-06-01 21:34:16 +00:00
|
|
|
return resultUri;
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
2009-11-19 01:11:33 +00:00
|
|
|
checkDatabases();
|
2009-05-26 23:40:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-19 01:11:33 +00:00
|
|
|
/**
|
|
|
|
* The idea here is that the two databases (EmailProvider.db and EmailProviderBody.db must
|
|
|
|
* always be in sync (i.e. there are two database or NO databases). This code will delete
|
|
|
|
* any "orphan" database, so that both will be created together. Note that an "orphan" database
|
|
|
|
* will exist after either of the individual databases is deleted due to data corruption.
|
|
|
|
*/
|
|
|
|
public void checkDatabases() {
|
|
|
|
// Uncache the databases
|
|
|
|
if (mDatabase != null) {
|
|
|
|
mDatabase = null;
|
|
|
|
}
|
|
|
|
if (mBodyDatabase != null) {
|
|
|
|
mBodyDatabase = null;
|
|
|
|
}
|
|
|
|
// Look for orphans, and delete as necessary; these must always be in sync
|
|
|
|
File databaseFile = getContext().getDatabasePath(DATABASE_NAME);
|
|
|
|
File bodyFile = getContext().getDatabasePath(BODY_DATABASE_NAME);
|
|
|
|
|
|
|
|
// TODO Make sure attachments are deleted
|
|
|
|
if (databaseFile.exists() && !bodyFile.exists()) {
|
|
|
|
Log.w(TAG, "Deleting orphaned EmailProvider database...");
|
|
|
|
databaseFile.delete();
|
|
|
|
} else if (bodyFile.exists() && !databaseFile.exists()) {
|
|
|
|
Log.w(TAG, "Deleting orphaned EmailProviderBody database...");
|
|
|
|
bodyFile.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
@Override
|
2009-07-30 18:41:31 +00:00
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
2009-05-29 21:24:34 +00:00
|
|
|
String sortOrder) {
|
2010-10-27 23:50:54 +00:00
|
|
|
long time = 0L;
|
|
|
|
if (Email.DEBUG) {
|
|
|
|
time = System.nanoTime();
|
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
Cursor c = null;
|
2010-12-30 22:55:27 +00:00
|
|
|
int match;
|
|
|
|
try {
|
|
|
|
match = findMatch(uri, "query");
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
String uriString = uri.toString();
|
|
|
|
// If we were passed an illegal uri, see if it ends in /-1
|
|
|
|
// if so, and if substituting 0 for -1 results in a valid uri, return an empty cursor
|
|
|
|
if (uriString != null && uriString.endsWith("/-1")) {
|
|
|
|
uri = Uri.parse(uriString.substring(0, uriString.length() - 2) + "0");
|
|
|
|
match = findMatch(uri, "query");
|
|
|
|
switch (match) {
|
|
|
|
case BODY_ID:
|
|
|
|
case MESSAGE_ID:
|
|
|
|
case DELETED_MESSAGE_ID:
|
|
|
|
case UPDATED_MESSAGE_ID:
|
|
|
|
case ATTACHMENT_ID:
|
|
|
|
case MAILBOX_ID:
|
|
|
|
case ACCOUNT_ID:
|
|
|
|
case HOSTAUTH_ID:
|
|
|
|
return new MatrixCursor(projection, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
Context context = getContext();
|
2009-08-06 00:31:50 +00:00
|
|
|
// See the comment at delete(), above
|
2009-08-18 18:44:27 +00:00
|
|
|
SQLiteDatabase db = getDatabase(context);
|
2009-05-26 23:40:34 +00:00
|
|
|
int table = match >> BASE_SHIFT;
|
2010-09-29 15:43:31 +00:00
|
|
|
String limit = uri.getQueryParameter(EmailContent.PARAMETER_LIMIT);
|
2009-05-26 23:40:34 +00:00
|
|
|
String id;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2010-10-27 23:50:54 +00:00
|
|
|
// Find the cache for this query's table (if any)
|
|
|
|
ContentCache cache = null;
|
|
|
|
String tableName = TABLE_NAMES[table];
|
|
|
|
// We can only use the cache if there's no selection
|
|
|
|
if (selection == null) {
|
|
|
|
cache = CONTENT_CACHES[table];
|
|
|
|
}
|
|
|
|
if (cache == null) {
|
|
|
|
ContentCache.notCacheable(uri, selection);
|
|
|
|
}
|
|
|
|
|
2009-11-19 01:11:33 +00:00
|
|
|
try {
|
|
|
|
switch (match) {
|
|
|
|
case BODY:
|
|
|
|
case MESSAGE:
|
|
|
|
case UPDATED_MESSAGE:
|
|
|
|
case DELETED_MESSAGE:
|
|
|
|
case ATTACHMENT:
|
|
|
|
case MAILBOX:
|
|
|
|
case ACCOUNT:
|
|
|
|
case HOSTAUTH:
|
2010-10-27 23:50:54 +00:00
|
|
|
c = db.query(tableName, projection,
|
2010-09-28 01:29:50 +00:00
|
|
|
selection, selectionArgs, null, null, sortOrder, limit);
|
2009-11-19 01:11:33 +00:00
|
|
|
break;
|
|
|
|
case BODY_ID:
|
|
|
|
case MESSAGE_ID:
|
|
|
|
case DELETED_MESSAGE_ID:
|
|
|
|
case UPDATED_MESSAGE_ID:
|
|
|
|
case ATTACHMENT_ID:
|
|
|
|
case MAILBOX_ID:
|
|
|
|
case ACCOUNT_ID:
|
|
|
|
case HOSTAUTH_ID:
|
|
|
|
id = uri.getPathSegments().get(1);
|
2010-10-27 23:50:54 +00:00
|
|
|
if (cache != null) {
|
|
|
|
c = cache.getCachedCursor(id, projection);
|
|
|
|
}
|
|
|
|
if (c == null) {
|
|
|
|
CacheToken token = null;
|
|
|
|
if (cache != null) {
|
|
|
|
token = cache.getCacheToken(id);
|
|
|
|
}
|
|
|
|
c = db.query(tableName, projection, whereWithId(id, selection),
|
|
|
|
selectionArgs, null, null, sortOrder, limit);
|
|
|
|
if (cache != null) {
|
|
|
|
c = cache.putCursor(c, id, projection, token);
|
|
|
|
}
|
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
break;
|
|
|
|
case ATTACHMENTS_MESSAGE_ID:
|
|
|
|
// All attachments for the given message
|
|
|
|
id = uri.getPathSegments().get(2);
|
|
|
|
c = db.query(Attachment.TABLE_NAME, projection,
|
|
|
|
whereWith(Attachment.MESSAGE_KEY + "=" + id, selection),
|
2010-09-28 01:29:50 +00:00
|
|
|
selectionArgs, null, null, sortOrder, limit);
|
2009-11-19 01:11:33 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
} catch (SQLiteException e) {
|
|
|
|
checkDatabases();
|
|
|
|
throw e;
|
2010-10-27 23:50:54 +00:00
|
|
|
} catch (RuntimeException e) {
|
|
|
|
checkDatabases();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
if (cache != null && Email.DEBUG) {
|
|
|
|
cache.recordQueryTime(c, System.nanoTime() - time);
|
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((c != null) && !isTemporary()) {
|
2010-09-14 23:28:50 +00:00
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String whereWithId(String id, String selection) {
|
|
|
|
StringBuilder sb = new StringBuilder(256);
|
|
|
|
sb.append("_id=");
|
|
|
|
sb.append(id);
|
|
|
|
if (selection != null) {
|
2009-09-10 18:52:36 +00:00
|
|
|
sb.append(" AND (");
|
2009-05-26 23:40:34 +00:00
|
|
|
sb.append(selection);
|
2009-09-10 18:52:36 +00:00
|
|
|
sb.append(')');
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-09-10 18:52:36 +00:00
|
|
|
/**
|
|
|
|
* Combine a locally-generated selection with a user-provided selection
|
|
|
|
*
|
|
|
|
* This introduces risk that the local selection might insert incorrect chars
|
|
|
|
* into the SQL, so use caution.
|
|
|
|
*
|
|
|
|
* @param where locally-generated selection, must not be null
|
|
|
|
* @param selection user-provided selection, may be null
|
|
|
|
* @return a single selection string
|
|
|
|
*/
|
2009-05-26 23:40:34 +00:00
|
|
|
private String whereWith(String where, String selection) {
|
2009-09-10 18:52:36 +00:00
|
|
|
if (selection == null) {
|
|
|
|
return where;
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
2009-09-10 18:52:36 +00:00
|
|
|
StringBuilder sb = new StringBuilder(where);
|
|
|
|
sb.append(" AND (");
|
|
|
|
sb.append(selection);
|
|
|
|
sb.append(')');
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
return sb.toString();
|
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
@Override
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
2010-09-10 20:06:07 +00:00
|
|
|
// Handle this special case the fastest possible way
|
|
|
|
if (uri == INTEGRITY_CHECK_URI) {
|
|
|
|
checkDatabases();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-14 23:28:50 +00:00
|
|
|
// Notify all existing cursors, except for ACCOUNT_RESET_NEW_COUNT(_ID)
|
|
|
|
Uri notificationUri = EmailContent.CONTENT_URI;
|
|
|
|
|
2010-12-16 20:28:16 +00:00
|
|
|
int match = findMatch(uri, "update");
|
2009-06-15 21:40:06 +00:00
|
|
|
Context context = getContext();
|
2010-09-23 16:19:44 +00:00
|
|
|
ContentResolver resolver = context.getContentResolver();
|
2009-08-06 00:31:50 +00:00
|
|
|
// See the comment at delete(), above
|
2009-08-18 18:44:27 +00:00
|
|
|
SQLiteDatabase db = getDatabase(context);
|
2009-05-26 23:40:34 +00:00
|
|
|
int table = match >> BASE_SHIFT;
|
2009-06-01 21:34:16 +00:00
|
|
|
int result;
|
2009-06-15 21:40:06 +00:00
|
|
|
|
2010-09-10 21:37:01 +00:00
|
|
|
// We do NOT allow setting of unreadCount/messageCount via the provider
|
|
|
|
// These columns are maintained via triggers
|
|
|
|
if (match == MAILBOX_ID || match == MAILBOX) {
|
|
|
|
values.remove(MailboxColumns.UNREAD_COUNT);
|
|
|
|
values.remove(MailboxColumns.MESSAGE_COUNT);
|
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
|
2010-10-27 23:50:54 +00:00
|
|
|
ContentCache cache = CONTENT_CACHES[table];
|
|
|
|
String tableName = TABLE_NAMES[table];
|
2009-08-20 02:07:29 +00:00
|
|
|
String id;
|
2010-10-27 23:50:54 +00:00
|
|
|
|
2009-11-19 01:11:33 +00:00
|
|
|
try {
|
|
|
|
switch (match) {
|
|
|
|
case MAILBOX_ID_ADD_TO_FIELD:
|
|
|
|
case ACCOUNT_ID_ADD_TO_FIELD:
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
String field = values.getAsString(EmailContent.FIELD_COLUMN_NAME);
|
|
|
|
Long add = values.getAsLong(EmailContent.ADD_COLUMN_NAME);
|
|
|
|
if (field == null || add == null) {
|
|
|
|
throw new IllegalArgumentException("No field/add specified " + uri);
|
2009-08-20 02:07:29 +00:00
|
|
|
}
|
2010-12-03 00:33:49 +00:00
|
|
|
ContentValues actualValues = new ContentValues();
|
2010-12-02 23:19:14 +00:00
|
|
|
if (cache != null) {
|
|
|
|
cache.lock(id);
|
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
try {
|
2010-12-03 00:33:49 +00:00
|
|
|
db.beginTransaction();
|
2010-12-02 23:19:14 +00:00
|
|
|
try {
|
2010-12-03 00:33:49 +00:00
|
|
|
Cursor c = db.query(tableName,
|
|
|
|
new String[] {EmailContent.RECORD_ID, field},
|
|
|
|
whereWithId(id, selection),
|
|
|
|
selectionArgs, null, null, null);
|
|
|
|
try {
|
|
|
|
result = 0;
|
|
|
|
String[] bind = new String[1];
|
|
|
|
if (c.moveToNext()) {
|
|
|
|
bind[0] = c.getString(0); // _id
|
|
|
|
long value = c.getLong(1) + add;
|
|
|
|
actualValues.put(field, value);
|
|
|
|
result = db.update(tableName, actualValues, ID_EQUALS, bind);
|
|
|
|
}
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
c.close();
|
2010-12-02 23:19:14 +00:00
|
|
|
}
|
|
|
|
} finally {
|
2010-12-03 00:33:49 +00:00
|
|
|
db.endTransaction();
|
2009-11-19 01:11:33 +00:00
|
|
|
}
|
|
|
|
} finally {
|
2010-12-02 23:19:14 +00:00
|
|
|
if (cache != null) {
|
|
|
|
cache.unlock(id, actualValues);
|
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SYNCED_MESSAGE_ID:
|
2010-09-23 16:19:44 +00:00
|
|
|
resolver.notifyChange(Message.NOTIFIER_URI, null);
|
|
|
|
//$FALL-THROUGH$
|
2009-11-19 01:11:33 +00:00
|
|
|
case UPDATED_MESSAGE_ID:
|
2010-09-23 16:19:44 +00:00
|
|
|
case MESSAGE_ID:
|
|
|
|
case BODY_ID:
|
2009-11-19 01:11:33 +00:00
|
|
|
case ATTACHMENT_ID:
|
|
|
|
case MAILBOX_ID:
|
|
|
|
case ACCOUNT_ID:
|
|
|
|
case HOSTAUTH_ID:
|
|
|
|
id = uri.getPathSegments().get(1);
|
2010-10-27 23:50:54 +00:00
|
|
|
if (cache != null) {
|
|
|
|
cache.lock(id);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (match == SYNCED_MESSAGE_ID) {
|
|
|
|
// For synced messages, first copy the old message to the updated table
|
|
|
|
// Note the insert or ignore semantics, guaranteeing that only the first
|
|
|
|
// update will be reflected in the updated message table; therefore this
|
|
|
|
// row will always have the "original" data
|
|
|
|
db.execSQL(UPDATED_MESSAGE_INSERT + id);
|
|
|
|
} else if (match == MESSAGE_ID) {
|
|
|
|
db.execSQL(UPDATED_MESSAGE_DELETE + id);
|
|
|
|
}
|
|
|
|
result = db.update(tableName, values, whereWithId(id, selection),
|
|
|
|
selectionArgs);
|
|
|
|
} catch (SQLiteException e) {
|
|
|
|
// Null out values (so they aren't cached) and re-throw
|
|
|
|
values = null;
|
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
if (cache != null) {
|
|
|
|
cache.unlock(id, values);
|
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
}
|
2010-08-10 00:48:53 +00:00
|
|
|
if (match == ATTACHMENT_ID) {
|
|
|
|
if (values.containsKey(Attachment.FLAGS)) {
|
|
|
|
int flags = values.getAsInteger(Attachment.FLAGS);
|
|
|
|
AttachmentDownloadService.attachmentChanged(
|
|
|
|
Integer.parseInt(id), flags);
|
|
|
|
}
|
|
|
|
}
|
2009-11-19 01:11:33 +00:00
|
|
|
break;
|
|
|
|
case BODY:
|
|
|
|
case MESSAGE:
|
|
|
|
case UPDATED_MESSAGE:
|
|
|
|
case ATTACHMENT:
|
|
|
|
case MAILBOX:
|
|
|
|
case ACCOUNT:
|
|
|
|
case HOSTAUTH:
|
2010-10-27 23:50:54 +00:00
|
|
|
switch(match) {
|
|
|
|
case MESSAGE:
|
|
|
|
case ACCOUNT:
|
|
|
|
case MAILBOX:
|
|
|
|
case HOSTAUTH:
|
|
|
|
// If we're doing some generic update, the whole cache needs to be
|
|
|
|
// invalidated. This case should be quite rare
|
|
|
|
cache.invalidate("Update", uri, selection);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = db.update(tableName, values, selection, selectionArgs);
|
2009-11-19 01:11:33 +00:00
|
|
|
break;
|
2010-09-14 23:28:50 +00:00
|
|
|
case ACCOUNT_RESET_NEW_COUNT_ID:
|
|
|
|
id = uri.getPathSegments().get(1);
|
2010-12-02 23:19:14 +00:00
|
|
|
if (cache != null) {
|
|
|
|
cache.lock(id);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
result = db.update(tableName, CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT,
|
|
|
|
whereWithId(id, selection), selectionArgs);
|
|
|
|
} finally {
|
|
|
|
if (cache != null) {
|
|
|
|
cache.unlock(id, values);
|
|
|
|
}
|
|
|
|
}
|
2010-09-14 23:28:50 +00:00
|
|
|
notificationUri = Account.CONTENT_URI; // Only notify account cursors.
|
|
|
|
break;
|
|
|
|
case ACCOUNT_RESET_NEW_COUNT:
|
2010-10-27 23:50:54 +00:00
|
|
|
result = db.update(tableName, CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT,
|
2010-09-14 23:28:50 +00:00
|
|
|
selection, selectionArgs);
|
2010-12-02 23:19:14 +00:00
|
|
|
// Affects all accounts. Just invalidate all account cache.
|
|
|
|
cache.invalidate("Reset all new counts", null, null);
|
2010-09-14 23:28:50 +00:00
|
|
|
notificationUri = Account.CONTENT_URI; // Only notify account cursors.
|
|
|
|
break;
|
2009-11-19 01:11:33 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
} catch (SQLiteException e) {
|
|
|
|
checkDatabases();
|
|
|
|
throw e;
|
2009-05-29 21:24:34 +00:00
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
|
2010-09-23 16:19:44 +00:00
|
|
|
resolver.notifyChange(notificationUri, null);
|
2009-06-01 21:34:16 +00:00
|
|
|
return result;
|
2009-05-29 21:24:34 +00:00
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-29 21:24:34 +00:00
|
|
|
/* (non-Javadoc)
|
2009-06-01 19:55:50 +00:00
|
|
|
* @see android.content.ContentProvider#applyBatch(android.content.ContentProviderOperation)
|
2009-05-29 21:24:34 +00:00
|
|
|
*/
|
2009-07-30 18:41:31 +00:00
|
|
|
@Override
|
2009-06-01 19:55:50 +00:00
|
|
|
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
|
2009-07-05 19:54:49 +00:00
|
|
|
throws OperationApplicationException {
|
2009-08-06 00:31:50 +00:00
|
|
|
Context context = getContext();
|
|
|
|
SQLiteDatabase db = getDatabase(context);
|
2009-05-29 21:24:34 +00:00
|
|
|
db.beginTransaction();
|
|
|
|
try {
|
|
|
|
ContentProviderResult[] results = super.applyBatch(operations);
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
return results;
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-30 20:53:59 +00:00
|
|
|
|
2011-04-22 22:45:11 +00:00
|
|
|
/** Counts the number of messages in each mailbox, and updates the message count column. */
|
|
|
|
@VisibleForTesting
|
|
|
|
static void recalculateMessageCount(SQLiteDatabase db) {
|
2010-07-30 20:53:59 +00:00
|
|
|
db.execSQL("update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT +
|
|
|
|
"= (select count(*) from " + Message.TABLE_NAME +
|
|
|
|
" where " + Message.MAILBOX_KEY + " = " +
|
|
|
|
Mailbox.TABLE_NAME + "." + EmailContent.RECORD_ID + ")");
|
|
|
|
}
|
2011-04-22 22:45:11 +00:00
|
|
|
|
|
|
|
/** Upgrades the database from v17 to v18 */
|
|
|
|
@VisibleForTesting
|
|
|
|
static void upgradeFromVersion17ToVersion18(SQLiteDatabase db) {
|
|
|
|
// Copy the displayName column to the serverId column. In v18 of the database,
|
|
|
|
// we use the serverId for IMAP/POP3 mailboxes instead of overloading the
|
|
|
|
// display name.
|
|
|
|
//
|
|
|
|
// For posterity; this is the command we're executing:
|
|
|
|
//sqlite> UPDATE mailbox SET serverid=displayname WHERE mailbox._id in (
|
|
|
|
// ...> SELECT mailbox._id FROM mailbox,account,hostauth WHERE
|
|
|
|
// ...> mailbox.parentkey=0 AND mailbox.accountkey=account._id AND
|
|
|
|
// ...> account.hostauthkeyrecv=hostauth._id AND
|
|
|
|
// ...> (hostauth.protocol='imap' OR hostauth.protocol='pop3'));
|
|
|
|
try {
|
|
|
|
db.execSQL(
|
|
|
|
"UPDATE " + Mailbox.TABLE_NAME + " SET "
|
|
|
|
+ MailboxColumns.SERVER_ID + "=" + MailboxColumns.DISPLAY_NAME
|
|
|
|
+ " WHERE "
|
|
|
|
+ Mailbox.TABLE_NAME + "." + MailboxColumns.ID + " IN ( SELECT "
|
|
|
|
+ Mailbox.TABLE_NAME + "." + MailboxColumns.ID + " FROM "
|
|
|
|
+ Mailbox.TABLE_NAME + "," + Account.TABLE_NAME + ","
|
|
|
|
+ HostAuth.TABLE_NAME + " WHERE "
|
|
|
|
+ Mailbox.TABLE_NAME + "." + MailboxColumns.PARENT_KEY + "=0 AND "
|
|
|
|
+ Mailbox.TABLE_NAME + "." + MailboxColumns.ACCOUNT_KEY + "="
|
|
|
|
+ Account.TABLE_NAME + "." + AccountColumns.ID + " AND "
|
|
|
|
+ Account.TABLE_NAME + "." + AccountColumns.HOST_AUTH_KEY_RECV + "="
|
|
|
|
+ HostAuth.TABLE_NAME + "." + HostAuthColumns.ID + " AND ( "
|
|
|
|
+ HostAuth.TABLE_NAME + "." + HostAuthColumns.PROTOCOL + "='imap' OR "
|
|
|
|
+ HostAuth.TABLE_NAME + "." + HostAuthColumns.PROTOCOL + "='pop3' ) )");
|
|
|
|
} catch (SQLException e) {
|
|
|
|
// Shouldn't be needed unless we're debugging and interrupt the process
|
|
|
|
Log.w(TAG, "Exception upgrading EmailProvider.db from 17 to 18 " + e);
|
|
|
|
}
|
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|