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-06-15 21:40:06 +00:00
|
|
|
import com.android.email.provider.EmailContent.Account;
|
|
|
|
import com.android.email.provider.EmailContent.AccountColumns;
|
|
|
|
import com.android.email.provider.EmailContent.Attachment;
|
|
|
|
import com.android.email.provider.EmailContent.AttachmentColumns;
|
|
|
|
import com.android.email.provider.EmailContent.Body;
|
|
|
|
import com.android.email.provider.EmailContent.BodyColumns;
|
|
|
|
import com.android.email.provider.EmailContent.HostAuth;
|
|
|
|
import com.android.email.provider.EmailContent.HostAuthColumns;
|
|
|
|
import com.android.email.provider.EmailContent.Mailbox;
|
|
|
|
import com.android.email.provider.EmailContent.MailboxColumns;
|
|
|
|
import com.android.email.provider.EmailContent.Message;
|
|
|
|
import com.android.email.provider.EmailContent.MessageColumns;
|
|
|
|
import com.android.email.provider.EmailContent.SyncColumns;
|
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;
|
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;
|
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;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Config;
|
|
|
|
import android.util.Log;
|
|
|
|
|
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-05-29 21:24:34 +00:00
|
|
|
static final String DATABASE_NAME = "EmailProvider.db";
|
2009-06-15 21:40:06 +00:00
|
|
|
static final String BODY_DATABASE_NAME = "EmailProviderBody.db";
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// In these early versions, updating the database version will cause all tables to be deleted
|
|
|
|
// Obviously, we'll handle upgrades differently once things are a bit stable
|
2009-06-27 19:14:14 +00:00
|
|
|
public static final int DATABASE_VERSION = 14;
|
2009-06-15 21:40:06 +00:00
|
|
|
public static final int BODY_DATABASE_VERSION = 1;
|
2009-05-26 23:40:34 +00:00
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
public static final String EMAIL_AUTHORITY = "com.android.email.provider";
|
2009-05-26 23:40:34 +00:00
|
|
|
|
|
|
|
private static final int ACCOUNT_BASE = 0;
|
|
|
|
private static final int ACCOUNT = ACCOUNT_BASE;
|
|
|
|
private static final int ACCOUNT_MAILBOXES = ACCOUNT_BASE + 1;
|
|
|
|
private static final int ACCOUNT_ID = ACCOUNT_BASE + 2;
|
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;
|
|
|
|
private static final int MAILBOX_MESSAGES = MAILBOX_BASE + 1;
|
|
|
|
private static final int MAILBOX_ID = 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;
|
|
|
|
private static final int MESSAGE_ATTACHMENTS = MESSAGE_BASE + 1;
|
|
|
|
private static final int MESSAGE_ID = MESSAGE_BASE + 2;
|
2009-06-27 19:14:14 +00:00
|
|
|
private static final int SYNCED_MESSAGE_ID = MESSAGE_BASE + 3;
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final int ATTACHMENT_BASE = 0x3000;
|
|
|
|
private static final int ATTACHMENT = ATTACHMENT_BASE;
|
|
|
|
private static final int ATTACHMENT_CONTENT = ATTACHMENT_BASE + 1;
|
|
|
|
private static final int ATTACHMENT_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;
|
|
|
|
private static final int DELETED_MESSAGE_MAILBOX = DELETED_MESSAGE_BASE + 2;
|
|
|
|
|
|
|
|
// 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-21 21:37:35 +00:00
|
|
|
private static final int BODY_MESSAGE_ID = BODY_BASE + 2;
|
|
|
|
private static final int BODY_HTML = BODY_BASE + 3;
|
2009-05-26 23:40:34 +00:00
|
|
|
private static final int BODY_TEXT = BODY_BASE + 4;
|
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
|
|
|
|
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
|
|
|
|
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 +
|
|
|
|
" where " + EmailContent.RECORD_ID + " in " + "(select " + EmailContent.RECORD_ID +
|
|
|
|
" from " + Body.TABLE_NAME + " except select " + EmailContent.RECORD_ID + " from " +
|
|
|
|
Message.TABLE_NAME + ')';
|
|
|
|
|
|
|
|
private static final String DELETE_BODY = "delete from " + Body.TABLE_NAME +
|
|
|
|
" where " + EmailContent.RECORD_ID + '=';
|
|
|
|
|
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
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "account", ACCOUNT);
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific account
|
|
|
|
// insert into this URI causes a mailbox to be added to the account
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "account/#", ACCOUNT_ID);
|
2009-05-26 23:40:34 +00:00
|
|
|
// The mailboxes in a specific account
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "account/#/mailbox", ACCOUNT_MAILBOXES);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// All mailboxes
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_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!
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "mailbox/#", MAILBOX_ID);
|
2009-05-26 23:40:34 +00:00
|
|
|
// The messages in a specific mailbox
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "mailbox/#/message", MAILBOX_MESSAGES);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// All messages
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "message", MESSAGE);
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific message
|
|
|
|
// insert into this URI causes an attachment to be added to the message
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "message/#", MESSAGE_ID);
|
2009-05-26 23:40:34 +00:00
|
|
|
// The attachments of a specific message
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "message/#/attachment", MESSAGE_ATTACHMENTS);
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific attachment
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "attachment", ATTACHMENT);
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific attachment (the header information)
|
2009-06-27 19:14:14 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "attachment/#", ATTACHMENT_ID);
|
2009-05-26 23:40:34 +00:00
|
|
|
// The content for a specific attachment
|
2009-06-27 19:14:14 +00:00
|
|
|
// NOT IMPLEMENTED
|
2009-05-26 23:40:34 +00:00
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "attachment/content/*", ATTACHMENT_CONTENT);
|
|
|
|
|
|
|
|
// All mail bodies
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "body", BODY);
|
|
|
|
// A specific mail body
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "body/#", BODY_ID);
|
2009-06-21 21:37:35 +00:00
|
|
|
// The body for a specific message
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "body/message/#", BODY_MESSAGE_ID);
|
2009-05-26 23:40:34 +00:00
|
|
|
// The HTML part of a specific mail body
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "body/#/html", BODY_HTML);
|
|
|
|
// The plain text part of a specific mail body
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "body/#/text", BODY_TEXT);
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
// A specific attachment
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "hostauth", HOSTAUTH); // IMPLEMENTED
|
|
|
|
// A specific attachment (the header information)
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "hostauth/#", HOSTAUTH_ID); // IMPLEMENTED
|
2009-06-27 19:14:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* THIS URI HAS SPECIAL SEMANTICS
|
|
|
|
* ITS USE IS INDENTED FOR THE UI APPLICATION TO MARK CHANGES THAT NEED TO BE SYNCED BACK
|
|
|
|
* TO A SERVER VIA A SYNC ADAPTER
|
|
|
|
*/
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "syncedMessage/#", SYNCED_MESSAGE_ID);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "deletedMessage", DELETED_MESSAGE);
|
|
|
|
// A specific deleted message
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "deletedMessage/#", DELETED_MESSAGE_ID);
|
|
|
|
// All deleted messages from a specific mailbox
|
|
|
|
// NOT IMPLEMENTED; do we need this as a convenience?
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "deletedMessage/mailbox/#", DELETED_MESSAGE_MAILBOX);
|
|
|
|
|
|
|
|
// All updated messages
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "updatedMessage", UPDATED_MESSAGE);
|
|
|
|
// A specific updated message
|
|
|
|
matcher.addURI(EMAIL_AUTHORITY, "updatedMessage/#", UPDATED_MESSAGE_ID);
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
static void createMessageTable(SQLiteDatabase db) {
|
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
2009-06-27 19:14:14 +00:00
|
|
|
+ SyncColumns.ACCOUNT_KEY + " integer, "
|
|
|
|
+ SyncColumns.SERVER_ID + " integer, "
|
|
|
|
+ SyncColumns.SERVER_VERSION + " integer, "
|
|
|
|
+ SyncColumns.DATA + " text, "
|
|
|
|
+ SyncColumns.DIRTY_COUNT + " integer, "
|
|
|
|
+ MessageColumns.DISPLAY_NAME + " text, "
|
|
|
|
+ MessageColumns.TIMESTAMP + " integer, "
|
|
|
|
+ MessageColumns.SUBJECT + " text, "
|
|
|
|
+ MessageColumns.PREVIEW + " text, "
|
|
|
|
+ MessageColumns.FLAG_READ + " integer, "
|
|
|
|
+ MessageColumns.FLAG_LOADED + " integer, "
|
|
|
|
+ MessageColumns.FLAG_FAVORITE + " integer, "
|
|
|
|
+ MessageColumns.FLAG_ATTACHMENT + " integer, "
|
|
|
|
+ MessageColumns.FLAGS + " integer, "
|
|
|
|
+ MessageColumns.TEXT_INFO + " text, "
|
|
|
|
+ MessageColumns.HTML_INFO + " text, "
|
|
|
|
+ MessageColumns.CLIENT_ID + " integer, "
|
|
|
|
+ MessageColumns.MESSAGE_ID + " text, "
|
|
|
|
+ MessageColumns.THREAD_ID + " text, "
|
|
|
|
+ MessageColumns.MAILBOX_KEY + " integer, "
|
|
|
|
+ MessageColumns.ACCOUNT_KEY + " integer, "
|
|
|
|
+ MessageColumns.REFERENCE_KEY + " integer, "
|
|
|
|
+ MessageColumns.SENDER_LIST + " text, "
|
|
|
|
+ MessageColumns.FROM_LIST + " text, "
|
|
|
|
+ MessageColumns.TO_LIST + " text, "
|
|
|
|
+ MessageColumns.CC_LIST + " text, "
|
|
|
|
+ MessageColumns.BCC_LIST + " text, "
|
|
|
|
+ MessageColumns.REPLY_TO_LIST + " text"
|
|
|
|
+ ");";
|
|
|
|
|
|
|
|
// The three tables have the same schema
|
2009-06-15 21:40:06 +00:00
|
|
|
db.execSQL("create table " + Message.TABLE_NAME + s);
|
2009-06-27 19:14:14 +00:00
|
|
|
db.execSQL("create table " + Message.UPDATED_TABLE_NAME + s);
|
|
|
|
db.execSQL("create table " + Message.DELETED_TABLE_NAME + s);
|
|
|
|
|
|
|
|
// For now, indices only on the Message table
|
2009-06-15 21:40:06 +00:00
|
|
|
db.execSQL("create index message_" + MessageColumns.TIMESTAMP
|
|
|
|
+ " on " + Message.TABLE_NAME + " (" + MessageColumns.TIMESTAMP + ");");
|
|
|
|
db.execSQL("create index message_" + MessageColumns.FLAG_READ
|
|
|
|
+ " on " + Message.TABLE_NAME + " (" + MessageColumns.FLAG_READ + ");");
|
|
|
|
db.execSQL("create index message_" + MessageColumns.FLAG_LOADED
|
|
|
|
+ " on " + Message.TABLE_NAME + " (" + MessageColumns.FLAG_LOADED + ");");
|
|
|
|
db.execSQL("create index message_" + MessageColumns.MAILBOX_KEY
|
|
|
|
+ " on " + Message.TABLE_NAME + " (" + MessageColumns.MAILBOX_KEY + ");");
|
|
|
|
db.execSQL("create index message_" + SyncColumns.SERVER_ID
|
|
|
|
+ " on " + Message.TABLE_NAME + " (" + SyncColumns.SERVER_ID + ");");
|
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.
|
|
|
|
db.execSQL("create trigger message_delete before delete on " + Message.TABLE_NAME +
|
|
|
|
" begin delete from " + Attachment.TABLE_NAME +
|
|
|
|
" where " + AttachmentColumns.MESSAGE_KEY + "=old." + EmailContent.RECORD_ID +
|
2009-06-27 19:14:14 +00:00
|
|
|
"; end");
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void upgradeMessageTable(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) {
|
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
|
|
|
+ AccountColumns.DISPLAY_NAME + " text, "
|
|
|
|
+ AccountColumns.EMAIL_ADDRESS + " text, "
|
|
|
|
+ AccountColumns.SYNC_KEY + " text, "
|
|
|
|
+ AccountColumns.SYNC_LOOKBACK + " integer, "
|
|
|
|
+ AccountColumns.SYNC_FREQUENCY + " text, "
|
|
|
|
+ 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, "
|
|
|
|
+ AccountColumns.RINGTONE_URI + " text "
|
|
|
|
+ ");";
|
|
|
|
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
|
|
|
|
db.execSQL("create trigger account_delete before delete on " + Account.TABLE_NAME +
|
|
|
|
" begin delete from " + Mailbox.TABLE_NAME +
|
|
|
|
" where " + MailboxColumns.ACCOUNT_KEY + "=old." + EmailContent.RECORD_ID +
|
|
|
|
"; delete from " + HostAuth.TABLE_NAME +
|
|
|
|
" where " + HostAuthColumns.ACCOUNT_KEY + "=old." + EmailContent.RECORD_ID +
|
2009-06-27 19:14:14 +00:00
|
|
|
"; end");
|
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
|
|
|
|
static void upgradeAccountTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
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) {
|
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
|
|
|
+ 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void upgradeHostAuthTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
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-06-15 21:40:06 +00:00
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
|
|
|
+ MailboxColumns.DISPLAY_NAME + " text, "
|
|
|
|
+ MailboxColumns.SERVER_ID + " text, "
|
|
|
|
+ MailboxColumns.PARENT_SERVER_ID + " text, "
|
|
|
|
+ MailboxColumns.ACCOUNT_KEY + " integer, "
|
|
|
|
+ MailboxColumns.TYPE + " integer, "
|
|
|
|
+ MailboxColumns.DELIMITER + " integer, "
|
|
|
|
+ MailboxColumns.SYNC_KEY + " text, "
|
|
|
|
+ MailboxColumns.SYNC_LOOKBACK + " integer, "
|
|
|
|
+ MailboxColumns.SYNC_FREQUENCY+ " integer, "
|
|
|
|
+ MailboxColumns.SYNC_TIME + " integer, "
|
|
|
|
+ MailboxColumns.UNREAD_COUNT + " integer, "
|
|
|
|
+ MailboxColumns.FLAG_VISIBLE + " integer, "
|
|
|
|
+ MailboxColumns.FLAGS + " integer, "
|
|
|
|
+ MailboxColumns.VISIBLE_LIMIT + " integer"
|
2009-06-27 19:14:14 +00:00
|
|
|
+ ");";
|
|
|
|
db.execSQL("create table " + Mailbox.TABLE_NAME + s);
|
2009-06-15 21:40:06 +00:00
|
|
|
db.execSQL("create index mailbox_" + MailboxColumns.SERVER_ID
|
|
|
|
+ " on " + Mailbox.TABLE_NAME + " (" + MailboxColumns.SERVER_ID + ")");
|
|
|
|
db.execSQL("create index mailbox_" + MailboxColumns.ACCOUNT_KEY
|
|
|
|
+ " on " + Mailbox.TABLE_NAME + " (" + MailboxColumns.ACCOUNT_KEY + ")");
|
2009-06-16 19:03:45 +00:00
|
|
|
// Deleting a Mailbox deletes associated Messages
|
|
|
|
db.execSQL("create trigger mailbox_delete before delete on " + Mailbox.TABLE_NAME +
|
|
|
|
" begin delete from " + Message.TABLE_NAME +
|
|
|
|
" where " + MessageColumns.MAILBOX_KEY + "=old." + EmailContent.RECORD_ID +
|
2009-06-27 19:14:14 +00:00
|
|
|
"; end");
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void upgradeMailboxTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
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) {
|
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
|
|
|
+ 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, "
|
|
|
|
+ AttachmentColumns.ENCODING + " text"
|
|
|
|
+ ");";
|
|
|
|
db.execSQL("create table " + Attachment.TABLE_NAME + s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void upgradeAttachmentTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
try {
|
|
|
|
db.execSQL("drop table " + Attachment.TABLE_NAME);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
|
|
|
createAttachmentTable(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createBodyTable(SQLiteDatabase db) {
|
|
|
|
String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, "
|
|
|
|
+ BodyColumns.MESSAGE_KEY + " integer, "
|
|
|
|
+ BodyColumns.HTML_CONTENT + " text, "
|
|
|
|
+ BodyColumns.TEXT_CONTENT + " text"
|
|
|
|
+ ");";
|
|
|
|
db.execSQL("create table " + Body.TABLE_NAME + s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void upgradeBodyTable(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2009-06-30 06:08:40 +00:00
|
|
|
try {
|
|
|
|
db.execSQL("drop table " + Body.TABLE_NAME);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
createBodyTable(db);
|
|
|
|
}
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private final int mDatabaseVersion = DATABASE_VERSION;
|
2009-06-15 21:40:06 +00:00
|
|
|
private final int mBodyDatabaseVersion = BODY_DATABASE_VERSION;
|
2009-06-27 19:14:14 +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-05-26 23:40:34 +00:00
|
|
|
public SQLiteDatabase getDatabase(Context context) {
|
2009-06-15 21:40:06 +00:00
|
|
|
if (mDatabase != null) {
|
2009-05-26 23:40:34 +00:00
|
|
|
return mDatabase;
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
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-06-15 21:40:06 +00:00
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
return mDatabase;
|
|
|
|
}
|
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
public SQLiteDatabase getBodyDatabase(Context context) {
|
|
|
|
if (mBodyDatabase != null) {
|
|
|
|
return mBodyDatabase;
|
|
|
|
}
|
|
|
|
BodyDatabaseHelper helper = new BodyDatabaseHelper(context, BODY_DATABASE_NAME);
|
|
|
|
mBodyDatabase = helper.getWritableDatabase();
|
|
|
|
if (mBodyDatabase != null) {
|
|
|
|
mBodyDatabase.setLockingEnabled(true);
|
|
|
|
}
|
|
|
|
return mBodyDatabase;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class BodyDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
BodyDatabaseHelper(Context context, String name) {
|
2009-06-27 19:14:14 +00:00
|
|
|
super(context, name, null, mBodyDatabaseVersion);
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
// Create all tables here; each class has its own method
|
|
|
|
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 {
|
|
|
|
DatabaseHelper(Context context, String name) {
|
2009-06-27 19:14:14 +00:00
|
|
|
super(context, name, null, mDatabaseVersion);
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
|
|
|
// 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-06-15 21:40:06 +00:00
|
|
|
upgradeMessageTable(db, oldVersion, newVersion);
|
|
|
|
upgradeAttachmentTable(db, oldVersion, newVersion);
|
|
|
|
upgradeMailboxTable(db, oldVersion, newVersion);
|
|
|
|
upgradeHostAuthTable(db, oldVersion, newVersion);
|
|
|
|
upgradeAccountTable(db, oldVersion, newVersion);
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onOpen(SQLiteDatabase db) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
int match = sURIMatcher.match(uri);
|
2009-06-15 21:40:06 +00:00
|
|
|
Context context = getContext();
|
|
|
|
SQLiteDatabase db = (match >= BODY_BASE) ? getBodyDatabase(context) : 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-06-24 19:48:57 +00:00
|
|
|
boolean attachBodyDb = false;
|
|
|
|
boolean deleteOrphanedBodies = false;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-29 21:24:34 +00:00
|
|
|
if (Config.LOGV) {
|
2009-05-26 23:40:34 +00:00
|
|
|
Log.v(TAG, "EmailProvider.delete: uri=" + uri + ", match is " + match);
|
2009-05-29 21:24:34 +00:00
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
|
2009-06-01 21:34:16 +00:00
|
|
|
int result;
|
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-06-24 19:48:57 +00:00
|
|
|
// 0) Activate the body database (bootstrap step, if doesn't exist yet)
|
2009-06-16 19:03:45 +00:00
|
|
|
// 1) Attach the Body database
|
|
|
|
// 2) Begin a transaction, ensuring that both databases are affected atomically
|
|
|
|
// 3) Do the requested deletion, with cascading deletions handled in triggers
|
|
|
|
// 4) End the transaction, committing all changes atomically
|
|
|
|
// 5) Detach the Body database
|
2009-06-24 19:48:57 +00:00
|
|
|
attachBodyDb = true;
|
|
|
|
getBodyDatabase(context);
|
2009-06-30 06:08:40 +00:00
|
|
|
String bodyFileName = mBodyDatabase.getPath();
|
2009-06-16 19:03:45 +00:00
|
|
|
db.execSQL("attach \"" + bodyFileName + "\" as BodyDatabase");
|
|
|
|
db.beginTransaction();
|
|
|
|
if (match != MESSAGE_ID) {
|
2009-06-24 19:48:57 +00:00
|
|
|
deleteOrphanedBodies = true;
|
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);
|
|
|
|
}
|
2009-06-16 19:03:45 +00:00
|
|
|
result = db.delete(TABLE_NAMES[table], whereWithId(id, selection),
|
|
|
|
selectionArgs);
|
|
|
|
break;
|
|
|
|
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:
|
|
|
|
result = db.delete(TABLE_NAMES[table], selection, selectionArgs);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
2009-06-24 19:48:57 +00:00
|
|
|
if (attachBodyDb) {
|
|
|
|
if (deleteOrphanedBodies) {
|
|
|
|
// Delete any orphaned Body records
|
2009-06-27 19:14:14 +00:00
|
|
|
db.execSQL(DELETE_ORPHAN_BODIES);
|
2009-06-24 19:48:57 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} else {
|
|
|
|
// Delete the Body record associated with the deleted message
|
2009-06-27 19:14:14 +00:00
|
|
|
db.execSQL(DELETE_BODY + id);
|
2009-06-24 19:48:57 +00:00
|
|
|
db.setTransactionSuccessful();
|
2009-06-27 19:14:14 +00:00
|
|
|
}
|
2009-06-24 19:48:57 +00:00
|
|
|
}
|
2009-06-16 19:03:45 +00:00
|
|
|
} finally {
|
2009-06-24 19:48:57 +00:00
|
|
|
if (attachBodyDb) {
|
2009-06-16 19:03:45 +00:00
|
|
|
db.endTransaction();
|
|
|
|
db.execSQL("detach BodyDatabase");
|
2009-06-24 19:48:57 +00:00
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
2009-06-01 21:34:16 +00:00
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
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) {
|
|
|
|
int match = sURIMatcher.match(uri);
|
|
|
|
switch (match) {
|
2009-06-15 21:40:06 +00:00
|
|
|
case BODY_ID:
|
|
|
|
return "vnd.android.cursor.item/email-body";
|
|
|
|
case BODY:
|
2009-05-29 21:24:34 +00:00
|
|
|
return "vnd.android.cursor.dir/email-message";
|
2009-06-17 17:22:37 +00:00
|
|
|
case UPDATED_MESSAGE_ID:
|
2009-06-15 21:40:06 +00:00
|
|
|
case MESSAGE_ID:
|
|
|
|
return "vnd.android.cursor.item/email-message";
|
2009-05-29 21:24:34 +00:00
|
|
|
case MAILBOX_MESSAGES:
|
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 ACCOUNT_MAILBOXES:
|
|
|
|
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";
|
|
|
|
case MESSAGE_ATTACHMENTS:
|
|
|
|
case ATTACHMENT:
|
|
|
|
return "vnd.android.cursor.dir/email-attachment";
|
|
|
|
case ATTACHMENT_ID:
|
|
|
|
return "vnd.android.cursor.item/email-attachment";
|
|
|
|
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) {
|
|
|
|
int match = sURIMatcher.match(uri);
|
2009-06-15 21:40:06 +00:00
|
|
|
Context context = getContext();
|
|
|
|
SQLiteDatabase db = (match >= BODY_BASE) ? getBodyDatabase(context) : 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
|
|
|
|
2009-05-29 21:24:34 +00:00
|
|
|
if (Config.LOGV) {
|
2009-05-26 23:40:34 +00:00
|
|
|
Log.v(TAG, "EmailProvider.insert: uri=" + uri + ", match is " + match);
|
2009-05-29 21:24:34 +00:00
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
|
2009-06-01 21:34:16 +00:00
|
|
|
Uri resultUri = null;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
switch (match) {
|
2009-06-15 21:40:06 +00:00
|
|
|
case BODY:
|
2009-05-29 21:24:34 +00:00
|
|
|
case MESSAGE:
|
|
|
|
case ATTACHMENT:
|
|
|
|
case MAILBOX:
|
|
|
|
case ACCOUNT:
|
|
|
|
case HOSTAUTH:
|
2009-06-15 21:40:06 +00:00
|
|
|
// Make sure all new message records have dirty count of 0
|
|
|
|
if (match == MESSAGE) {
|
|
|
|
values.put(SyncColumns.DIRTY_COUNT, 0);
|
|
|
|
}
|
2009-05-29 21:24:34 +00:00
|
|
|
id = db.insert(TABLE_NAMES[table], "foo", values);
|
2009-06-01 21:34:16 +00:00
|
|
|
resultUri = ContentUris.withAppendedId(uri, id);
|
|
|
|
break;
|
2009-05-29 21:24:34 +00:00
|
|
|
case MAILBOX_ID:
|
|
|
|
// This implies adding a message to a mailbox
|
2009-06-01 21:34:16 +00:00
|
|
|
// Hmm, one problem here is that we can't link the account as well, so it must be
|
|
|
|
// already in the values...
|
2009-05-29 21:24:34 +00:00
|
|
|
id = Long.parseLong(uri.getPathSegments().get(1));
|
|
|
|
values.put(MessageColumns.MAILBOX_KEY, id);
|
2009-06-01 21:34:16 +00:00
|
|
|
resultUri = insert(Message.CONTENT_URI, values);
|
|
|
|
break;
|
2009-05-29 21:24:34 +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);
|
2009-06-01 21:34:16 +00:00
|
|
|
resultUri = insert(Attachment.CONTENT_URI, values);
|
|
|
|
break;
|
2009-05-29 21:24:34 +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);
|
2009-06-01 21:34:16 +00:00
|
|
|
resultUri = insert(Mailbox.CONTENT_URI, values);
|
|
|
|
break;
|
2009-05-29 21:24:34 +00:00
|
|
|
case MESSAGE_ATTACHMENTS:
|
|
|
|
id = db.insert(TABLE_NAMES[table], "foo", values);
|
2009-06-15 21:40:06 +00:00
|
|
|
resultUri = ContentUris.withAppendedId(Attachment.CONTENT_URI, id);
|
2009-06-01 21:34:16 +00:00
|
|
|
break;
|
2009-05-29 21:24:34 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URL " + uri);
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-06-15 21:40:06 +00:00
|
|
|
// Notify with the base uri, not the new uri (nobody is watching a new record)
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
2009-06-01 21:34:16 +00:00
|
|
|
return resultUri;
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-05-29 21:24:34 +00:00
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
String sortOrder) {
|
2009-05-26 23:40:34 +00:00
|
|
|
Cursor c = null;
|
2009-06-15 21:40:06 +00:00
|
|
|
Uri notificationUri = EmailContent.CONTENT_URI;
|
2009-05-26 23:40:34 +00:00
|
|
|
int match = sURIMatcher.match(uri);
|
2009-06-15 21:40:06 +00:00
|
|
|
Context context = getContext();
|
|
|
|
SQLiteDatabase db = (match >= BODY_BASE) ? getBodyDatabase(context) : getDatabase(context);
|
2009-05-26 23:40:34 +00:00
|
|
|
int table = match >> BASE_SHIFT;
|
|
|
|
String id;
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-29 21:24:34 +00:00
|
|
|
if (Config.LOGV) {
|
2009-05-26 23:40:34 +00:00
|
|
|
Log.v(TAG, "EmailProvider.query: uri=" + uri + ", match is " + match);
|
2009-05-29 21:24:34 +00:00
|
|
|
}
|
2009-05-26 23:40:34 +00:00
|
|
|
|
|
|
|
switch (match) {
|
2009-06-15 21:40:06 +00:00
|
|
|
case BODY:
|
2009-05-29 21:24:34 +00:00
|
|
|
case MESSAGE:
|
2009-06-30 06:08:40 +00:00
|
|
|
case DELETED_MESSAGE:
|
2009-06-17 17:22:37 +00:00
|
|
|
case UPDATED_MESSAGE:
|
2009-05-29 21:24:34 +00:00
|
|
|
case ATTACHMENT:
|
|
|
|
case MAILBOX:
|
|
|
|
case ACCOUNT:
|
|
|
|
case HOSTAUTH:
|
|
|
|
c = db.query(TABLE_NAMES[table], projection,
|
|
|
|
selection, selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
2009-06-15 21:40:06 +00:00
|
|
|
case BODY_ID:
|
2009-05-29 21:24:34 +00:00
|
|
|
case MESSAGE_ID:
|
2009-06-30 06:08:40 +00:00
|
|
|
case DELETED_MESSAGE_ID:
|
2009-06-17 17:22:37 +00:00
|
|
|
case UPDATED_MESSAGE_ID:
|
2009-05-29 21:24:34 +00:00
|
|
|
case ATTACHMENT_ID:
|
|
|
|
case MAILBOX_ID:
|
|
|
|
case ACCOUNT_ID:
|
|
|
|
case HOSTAUTH_ID:
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
c = db.query(TABLE_NAMES[table], projection,
|
|
|
|
whereWithId(id, selection), selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
|
|
|
case MESSAGE_ATTACHMENTS:
|
|
|
|
// All attachments for the given message
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
c = db.query(Attachment.TABLE_NAME, projection,
|
|
|
|
whereWith(Attachment.MESSAGE_KEY + "=" + id, selection),
|
|
|
|
selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((c != null) && !isTemporary()) {
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), notificationUri);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String whereWithId(String id, String selection) {
|
|
|
|
StringBuilder sb = new StringBuilder(256);
|
|
|
|
sb.append("_id=");
|
|
|
|
sb.append(id);
|
|
|
|
if (selection != null) {
|
|
|
|
sb.append(" AND ");
|
|
|
|
sb.append(selection);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2009-06-27 19:14:14 +00:00
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
private String whereWith(String where, String selection) {
|
|
|
|
StringBuilder sb = new StringBuilder(where);
|
|
|
|
if (selection != null) {
|
|
|
|
sb.append(" AND ");
|
|
|
|
sb.append(selection);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
int match = sURIMatcher.match(uri);
|
2009-06-15 21:40:06 +00:00
|
|
|
Context context = getContext();
|
|
|
|
SQLiteDatabase db = (match >= BODY_BASE) ? getBodyDatabase(context) : 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
|
|
|
|
2009-06-27 19:14:14 +00:00
|
|
|
if (Config.LOGV) {
|
|
|
|
Log.v(TAG, "EmailProvider.update: uri=" + uri + ", match is " + match);
|
2009-06-15 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 23:40:34 +00:00
|
|
|
switch (match) {
|
2009-06-15 21:40:06 +00:00
|
|
|
case BODY_ID:
|
2009-05-29 21:24:34 +00:00
|
|
|
case MESSAGE_ID:
|
2009-06-27 19:14:14 +00:00
|
|
|
case SYNCED_MESSAGE_ID:
|
2009-06-17 17:22:37 +00:00
|
|
|
case UPDATED_MESSAGE_ID:
|
2009-05-29 21:24:34 +00:00
|
|
|
case ATTACHMENT_ID:
|
|
|
|
case MAILBOX_ID:
|
|
|
|
case ACCOUNT_ID:
|
2009-06-03 23:11:36 +00:00
|
|
|
case HOSTAUTH_ID:
|
2009-05-29 21:24:34 +00:00
|
|
|
String 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 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);
|
|
|
|
}
|
2009-06-01 21:34:16 +00:00
|
|
|
result = db.update(TABLE_NAMES[table], values, whereWithId(id, selection),
|
|
|
|
selectionArgs);
|
|
|
|
break;
|
2009-06-15 21:40:06 +00:00
|
|
|
case BODY:
|
2009-05-29 21:24:34 +00:00
|
|
|
case MESSAGE:
|
2009-06-17 17:22:37 +00:00
|
|
|
case UPDATED_MESSAGE:
|
2009-05-29 21:24:34 +00:00
|
|
|
case ATTACHMENT:
|
|
|
|
case MAILBOX:
|
|
|
|
case ACCOUNT:
|
2009-06-03 23:11:36 +00:00
|
|
|
case HOSTAUTH:
|
2009-06-01 21:34:16 +00:00
|
|
|
result = db.update(TABLE_NAMES[table], values, selection, selectionArgs);
|
|
|
|
break;
|
2009-05-29 21:24:34 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
2009-06-15 21:40:06 +00:00
|
|
|
|
2009-06-01 21:34:16 +00:00
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
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-06-01 21:34:16 +00:00
|
|
|
*
|
|
|
|
* TODO: How do we call notifyChange() or do we need to - does this call the various
|
|
|
|
* update/insert/delete calls?
|
2009-05-29 21:24:34 +00:00
|
|
|
*/
|
2009-06-01 19:55:50 +00:00
|
|
|
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
|
2009-06-27 19:14:14 +00:00
|
|
|
throws OperationApplicationException {
|
2009-05-29 21:24:34 +00:00
|
|
|
SQLiteDatabase db = getDatabase(getContext());
|
|
|
|
db.beginTransaction();
|
|
|
|
try {
|
|
|
|
ContentProviderResult[] results = super.applyBatch(operations);
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
return results;
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2009-05-26 23:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|