DBHelper.java revision c6089bc01f2ae49fb11904a4b4f222811358254f
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.email.provider; 18 19import android.accounts.AccountManager; 20import android.content.ContentResolver; 21import android.content.ContentValues; 22import android.content.Context; 23import android.database.Cursor; 24import android.database.SQLException; 25import android.database.sqlite.SQLiteDatabase; 26import android.database.sqlite.SQLiteOpenHelper; 27import android.provider.CalendarContract; 28import android.provider.ContactsContract; 29import android.util.Log; 30 31import com.android.email2.ui.MailActivityEmail; 32import com.android.emailcommon.AccountManagerTypes; 33import com.android.emailcommon.mail.Address; 34import com.android.emailcommon.provider.Account; 35import com.android.emailcommon.provider.EmailContent; 36import com.android.emailcommon.provider.EmailContent.AccountColumns; 37import com.android.emailcommon.provider.EmailContent.Attachment; 38import com.android.emailcommon.provider.EmailContent.AttachmentColumns; 39import com.android.emailcommon.provider.EmailContent.Body; 40import com.android.emailcommon.provider.EmailContent.BodyColumns; 41import com.android.emailcommon.provider.EmailContent.HostAuthColumns; 42import com.android.emailcommon.provider.EmailContent.MailboxColumns; 43import com.android.emailcommon.provider.EmailContent.Message; 44import com.android.emailcommon.provider.EmailContent.MessageColumns; 45import com.android.emailcommon.provider.EmailContent.PolicyColumns; 46import com.android.emailcommon.provider.EmailContent.QuickResponseColumns; 47import com.android.emailcommon.provider.EmailContent.SyncColumns; 48import com.android.emailcommon.provider.HostAuth; 49import com.android.emailcommon.provider.Mailbox; 50import com.android.emailcommon.provider.Policy; 51import com.android.emailcommon.provider.QuickResponse; 52import com.android.emailcommon.service.LegacyPolicySet; 53import com.android.mail.providers.UIProvider; 54import com.google.common.annotations.VisibleForTesting; 55 56public final class DBHelper { 57 private static final String TAG = "EmailProvider"; 58 59 private static final String WHERE_ID = EmailContent.RECORD_ID + "=?"; 60 61 private static final String TRIGGER_MAILBOX_DELETE = 62 "create trigger mailbox_delete before delete on " + Mailbox.TABLE_NAME + 63 " begin" + 64 " delete from " + Message.TABLE_NAME + 65 " where " + MessageColumns.MAILBOX_KEY + "=old." + EmailContent.RECORD_ID + 66 "; delete from " + Message.UPDATED_TABLE_NAME + 67 " where " + MessageColumns.MAILBOX_KEY + "=old." + EmailContent.RECORD_ID + 68 "; delete from " + Message.DELETED_TABLE_NAME + 69 " where " + MessageColumns.MAILBOX_KEY + "=old." + EmailContent.RECORD_ID + 70 "; end"; 71 72 private static final String TRIGGER_ACCOUNT_DELETE = 73 "create trigger account_delete before delete on " + Account.TABLE_NAME + 74 " begin delete from " + Mailbox.TABLE_NAME + 75 " where " + MailboxColumns.ACCOUNT_KEY + "=old." + EmailContent.RECORD_ID + 76 "; delete from " + HostAuth.TABLE_NAME + 77 " where " + EmailContent.RECORD_ID + "=old." + AccountColumns.HOST_AUTH_KEY_RECV + 78 "; delete from " + HostAuth.TABLE_NAME + 79 " where " + EmailContent.RECORD_ID + "=old." + AccountColumns.HOST_AUTH_KEY_SEND + 80 "; delete from " + Policy.TABLE_NAME + 81 " where " + EmailContent.RECORD_ID + "=old." + AccountColumns.POLICY_KEY + 82 "; end"; 83 84 // Any changes to the database format *must* include update-in-place code. 85 // Original version: 3 86 // Version 4: Database wipe required; changing AccountManager interface w/Exchange 87 // Version 5: Database wipe required; changing AccountManager interface w/Exchange 88 // Version 6: Adding Message.mServerTimeStamp column 89 // Version 7: Replace the mailbox_delete trigger with a version that removes orphaned messages 90 // from the Message_Deletes and Message_Updates tables 91 // Version 8: Add security flags column to accounts table 92 // Version 9: Add security sync key and signature to accounts table 93 // Version 10: Add meeting info to message table 94 // Version 11: Add content and flags to attachment table 95 // Version 12: Add content_bytes to attachment table. content is deprecated. 96 // Version 13: Add messageCount to Mailbox table. 97 // Version 14: Add snippet to Message table 98 // Version 15: Fix upgrade problem in version 14. 99 // Version 16: Add accountKey to Attachment table 100 // Version 17: Add parentKey to Mailbox table 101 // Version 18: Copy Mailbox.displayName to Mailbox.serverId for all IMAP & POP3 mailboxes. 102 // Column Mailbox.serverId is used for the server-side pathname of a mailbox. 103 // Version 19: Add Policy table; add policyKey to Account table and trigger to delete an 104 // Account's policy when the Account is deleted 105 // Version 20: Add new policies to Policy table 106 // Version 21: Add lastSeenMessageKey column to Mailbox table 107 // Version 22: Upgrade path for IMAP/POP accounts to integrate with AccountManager 108 // Version 23: Add column to mailbox table for time of last access 109 // Version 24: Add column to hostauth table for client cert alias 110 // Version 25: Added QuickResponse table 111 // Version 26: Update IMAP accounts to add FLAG_SUPPORTS_SEARCH flag 112 // Version 27: Add protocolSearchInfo to Message table 113 // Version 28: Add notifiedMessageId and notifiedMessageCount to Account 114 // Version 29: Add protocolPoliciesEnforced and protocolPoliciesUnsupported to Policy 115 // Version 30: Use CSV of RFC822 addresses instead of "packed" values 116 // Version 31: Add columns to mailbox for ui status/last result 117 // Version 32: Add columns to mailbox for last notified message key/count; insure not null 118 // for "notified" columns 119 // Version 33: Add columns to attachment for ui provider columns 120 // Version 34: Add total count to mailbox 121 // Version 35: Set up defaults for lastTouchedCount for drafts and sent 122 // Version 36: mblank intentionally left this space 123 // Version 37: Add flag for settings support in folders 124 // Version 38&39: Add threadTopic to message (for future support) 125 // Version 39 is last Email1 version 126 // Version 100 is first Email2 version 127 // Version 101 SHOULD NOT BE USED 128 // Version 102&103: Add hierarchicalName to Mailbox 129 // Version 104&105: add syncData to Message 130 131 public static final int DATABASE_VERSION = 105; 132 133 // Any changes to the database format *must* include update-in-place code. 134 // Original version: 2 135 // Version 3: Add "sourceKey" column 136 // Version 4: Database wipe required; changing AccountManager interface w/Exchange 137 // Version 5: Database wipe required; changing AccountManager interface w/Exchange 138 // Version 6: Adding Body.mIntroText column 139 // Version 7/8: Adding quoted text start pos 140 // Version 8 is last Email1 version 141 public static final int BODY_DATABASE_VERSION = 100; 142 143 /* 144 * Internal helper method for index creation. 145 * Example: 146 * "create index message_" + MessageColumns.FLAG_READ 147 * + " on " + Message.TABLE_NAME + " (" + MessageColumns.FLAG_READ + ");" 148 */ 149 /* package */ 150 static String createIndex(String tableName, String columnName) { 151 return "create index " + tableName.toLowerCase() + '_' + columnName 152 + " on " + tableName + " (" + columnName + ");"; 153 } 154 155 static void createMessageTable(SQLiteDatabase db) { 156 String messageColumns = MessageColumns.DISPLAY_NAME + " text, " 157 + MessageColumns.TIMESTAMP + " integer, " 158 + MessageColumns.SUBJECT + " text, " 159 + MessageColumns.FLAG_READ + " integer, " 160 + MessageColumns.FLAG_LOADED + " integer, " 161 + MessageColumns.FLAG_FAVORITE + " integer, " 162 + MessageColumns.FLAG_ATTACHMENT + " integer, " 163 + MessageColumns.FLAGS + " integer, " 164 + MessageColumns.DRAFT_INFO + " integer, " 165 + MessageColumns.MESSAGE_ID + " text, " 166 + MessageColumns.MAILBOX_KEY + " integer, " 167 + MessageColumns.ACCOUNT_KEY + " integer, " 168 + MessageColumns.FROM_LIST + " text, " 169 + MessageColumns.TO_LIST + " text, " 170 + MessageColumns.CC_LIST + " text, " 171 + MessageColumns.BCC_LIST + " text, " 172 + MessageColumns.REPLY_TO_LIST + " text, " 173 + MessageColumns.MEETING_INFO + " text, " 174 + MessageColumns.SNIPPET + " text, " 175 + MessageColumns.PROTOCOL_SEARCH_INFO + " text, " 176 + MessageColumns.THREAD_TOPIC + " text, " 177 + MessageColumns.SYNC_DATA + " text" 178 + ");"; 179 180 // This String and the following String MUST have the same columns, except for the type 181 // of those columns! 182 String createString = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, " 183 + SyncColumns.SERVER_ID + " text, " 184 + SyncColumns.SERVER_TIMESTAMP + " integer, " 185 + messageColumns; 186 187 // For the updated and deleted tables, the id is assigned, but we do want to keep track 188 // of the ORDER of updates using an autoincrement primary key. We use the DATA column 189 // at this point; it has no other function 190 String altCreateString = " (" + EmailContent.RECORD_ID + " integer unique, " 191 + SyncColumns.SERVER_ID + " text, " 192 + SyncColumns.SERVER_TIMESTAMP + " integer, " 193 + messageColumns; 194 195 // The three tables have the same schema 196 db.execSQL("create table " + Message.TABLE_NAME + createString); 197 db.execSQL("create table " + Message.UPDATED_TABLE_NAME + altCreateString); 198 db.execSQL("create table " + Message.DELETED_TABLE_NAME + altCreateString); 199 200 String indexColumns[] = { 201 MessageColumns.TIMESTAMP, 202 MessageColumns.FLAG_READ, 203 MessageColumns.FLAG_LOADED, 204 MessageColumns.MAILBOX_KEY, 205 SyncColumns.SERVER_ID 206 }; 207 208 for (String columnName : indexColumns) { 209 db.execSQL(createIndex(Message.TABLE_NAME, columnName)); 210 } 211 212 // Deleting a Message deletes all associated Attachments 213 // Deleting the associated Body cannot be done in a trigger, because the Body is stored 214 // in a separate database, and trigger cannot operate on attached databases. 215 db.execSQL("create trigger message_delete before delete on " + Message.TABLE_NAME + 216 " begin delete from " + Attachment.TABLE_NAME + 217 " where " + AttachmentColumns.MESSAGE_KEY + "=old." + EmailContent.RECORD_ID + 218 "; end"); 219 220 // Add triggers to keep unread count accurate per mailbox 221 222 // NOTE: SQLite's before triggers are not safe when recursive triggers are involved. 223 // Use caution when changing them. 224 225 // Insert a message; if flagRead is zero, add to the unread count of the message's mailbox 226 db.execSQL("create trigger unread_message_insert before insert on " + Message.TABLE_NAME + 227 " when NEW." + MessageColumns.FLAG_READ + "=0" + 228 " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT + 229 '=' + MailboxColumns.UNREAD_COUNT + "+1" + 230 " where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY + 231 "; end"); 232 233 // Delete a message; if flagRead is zero, decrement the unread count of the msg's mailbox 234 db.execSQL("create trigger unread_message_delete before delete on " + Message.TABLE_NAME + 235 " when OLD." + MessageColumns.FLAG_READ + "=0" + 236 " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT + 237 '=' + MailboxColumns.UNREAD_COUNT + "-1" + 238 " where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY + 239 "; end"); 240 241 // Change a message's mailbox 242 db.execSQL("create trigger unread_message_move before update of " + 243 MessageColumns.MAILBOX_KEY + " on " + Message.TABLE_NAME + 244 " when OLD." + MessageColumns.FLAG_READ + "=0" + 245 " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT + 246 '=' + MailboxColumns.UNREAD_COUNT + "-1" + 247 " where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY + 248 "; update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT + 249 '=' + MailboxColumns.UNREAD_COUNT + "+1" + 250 " where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY + 251 "; end"); 252 253 // Change a message's read state 254 db.execSQL("create trigger unread_message_read before update of " + 255 MessageColumns.FLAG_READ + " on " + Message.TABLE_NAME + 256 " when OLD." + MessageColumns.FLAG_READ + "!=NEW." + MessageColumns.FLAG_READ + 257 " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.UNREAD_COUNT + 258 '=' + MailboxColumns.UNREAD_COUNT + "+ case OLD." + MessageColumns.FLAG_READ + 259 " when 0 then -1 else 1 end" + 260 " where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY + 261 "; end"); 262 263 // Add triggers to update message count per mailbox 264 265 // Insert a message. 266 db.execSQL("create trigger message_count_message_insert after insert on " + 267 Message.TABLE_NAME + 268 " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT + 269 '=' + MailboxColumns.MESSAGE_COUNT + "+1" + 270 " where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY + 271 "; end"); 272 273 // Delete a message; if flagRead is zero, decrement the unread count of the msg's mailbox 274 db.execSQL("create trigger message_count_message_delete after delete on " + 275 Message.TABLE_NAME + 276 " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT + 277 '=' + MailboxColumns.MESSAGE_COUNT + "-1" + 278 " where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY + 279 "; end"); 280 281 // Change a message's mailbox 282 db.execSQL("create trigger message_count_message_move after update of " + 283 MessageColumns.MAILBOX_KEY + " on " + Message.TABLE_NAME + 284 " begin update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT + 285 '=' + MailboxColumns.MESSAGE_COUNT + "-1" + 286 " where " + EmailContent.RECORD_ID + "=OLD." + MessageColumns.MAILBOX_KEY + 287 "; update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT + 288 '=' + MailboxColumns.MESSAGE_COUNT + "+1" + 289 " where " + EmailContent.RECORD_ID + "=NEW." + MessageColumns.MAILBOX_KEY + 290 "; end"); 291 } 292 293 static void resetMessageTable(SQLiteDatabase db, int oldVersion, int newVersion) { 294 try { 295 db.execSQL("drop table " + Message.TABLE_NAME); 296 db.execSQL("drop table " + Message.UPDATED_TABLE_NAME); 297 db.execSQL("drop table " + Message.DELETED_TABLE_NAME); 298 } catch (SQLException e) { 299 } 300 createMessageTable(db); 301 } 302 303 @SuppressWarnings("deprecation") 304 static void createAccountTable(SQLiteDatabase db) { 305 String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, " 306 + AccountColumns.DISPLAY_NAME + " text, " 307 + AccountColumns.EMAIL_ADDRESS + " text, " 308 + AccountColumns.SYNC_KEY + " text, " 309 + AccountColumns.SYNC_LOOKBACK + " integer, " 310 + AccountColumns.SYNC_INTERVAL + " text, " 311 + AccountColumns.HOST_AUTH_KEY_RECV + " integer, " 312 + AccountColumns.HOST_AUTH_KEY_SEND + " integer, " 313 + AccountColumns.FLAGS + " integer, " 314 + AccountColumns.IS_DEFAULT + " integer, " 315 + AccountColumns.COMPATIBILITY_UUID + " text, " 316 + AccountColumns.SENDER_NAME + " text, " 317 + AccountColumns.RINGTONE_URI + " text, " 318 + AccountColumns.PROTOCOL_VERSION + " text, " 319 + AccountColumns.NEW_MESSAGE_COUNT + " integer, " 320 + AccountColumns.SECURITY_FLAGS + " integer, " 321 + AccountColumns.SECURITY_SYNC_KEY + " text, " 322 + AccountColumns.SIGNATURE + " text, " 323 + AccountColumns.POLICY_KEY + " integer" 324 + ");"; 325 db.execSQL("create table " + Account.TABLE_NAME + s); 326 // Deleting an account deletes associated Mailboxes and HostAuth's 327 db.execSQL(TRIGGER_ACCOUNT_DELETE); 328 } 329 330 static void resetAccountTable(SQLiteDatabase db, int oldVersion, int newVersion) { 331 try { 332 db.execSQL("drop table " + Account.TABLE_NAME); 333 } catch (SQLException e) { 334 } 335 createAccountTable(db); 336 } 337 338 static void createPolicyTable(SQLiteDatabase db) { 339 String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, " 340 + PolicyColumns.PASSWORD_MODE + " integer, " 341 + PolicyColumns.PASSWORD_MIN_LENGTH + " integer, " 342 + PolicyColumns.PASSWORD_EXPIRATION_DAYS + " integer, " 343 + PolicyColumns.PASSWORD_HISTORY + " integer, " 344 + PolicyColumns.PASSWORD_COMPLEX_CHARS + " integer, " 345 + PolicyColumns.PASSWORD_MAX_FAILS + " integer, " 346 + PolicyColumns.MAX_SCREEN_LOCK_TIME + " integer, " 347 + PolicyColumns.REQUIRE_REMOTE_WIPE + " integer, " 348 + PolicyColumns.REQUIRE_ENCRYPTION + " integer, " 349 + PolicyColumns.REQUIRE_ENCRYPTION_EXTERNAL + " integer, " 350 + PolicyColumns.REQUIRE_MANUAL_SYNC_WHEN_ROAMING + " integer, " 351 + PolicyColumns.DONT_ALLOW_CAMERA + " integer, " 352 + PolicyColumns.DONT_ALLOW_ATTACHMENTS + " integer, " 353 + PolicyColumns.DONT_ALLOW_HTML + " integer, " 354 + PolicyColumns.MAX_ATTACHMENT_SIZE + " integer, " 355 + PolicyColumns.MAX_TEXT_TRUNCATION_SIZE + " integer, " 356 + PolicyColumns.MAX_HTML_TRUNCATION_SIZE + " integer, " 357 + PolicyColumns.MAX_EMAIL_LOOKBACK + " integer, " 358 + PolicyColumns.MAX_CALENDAR_LOOKBACK + " integer, " 359 + PolicyColumns.PASSWORD_RECOVERY_ENABLED + " integer, " 360 + PolicyColumns.PROTOCOL_POLICIES_ENFORCED + " text, " 361 + PolicyColumns.PROTOCOL_POLICIES_UNSUPPORTED + " text" 362 + ");"; 363 db.execSQL("create table " + Policy.TABLE_NAME + s); 364 } 365 366 static void createHostAuthTable(SQLiteDatabase db) { 367 String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, " 368 + HostAuthColumns.PROTOCOL + " text, " 369 + HostAuthColumns.ADDRESS + " text, " 370 + HostAuthColumns.PORT + " integer, " 371 + HostAuthColumns.FLAGS + " integer, " 372 + HostAuthColumns.LOGIN + " text, " 373 + HostAuthColumns.PASSWORD + " text, " 374 + HostAuthColumns.DOMAIN + " text, " 375 + HostAuthColumns.ACCOUNT_KEY + " integer," 376 + HostAuthColumns.CLIENT_CERT_ALIAS + " text" 377 + ");"; 378 db.execSQL("create table " + HostAuth.TABLE_NAME + s); 379 } 380 381 static void resetHostAuthTable(SQLiteDatabase db, int oldVersion, int newVersion) { 382 try { 383 db.execSQL("drop table " + HostAuth.TABLE_NAME); 384 } catch (SQLException e) { 385 } 386 createHostAuthTable(db); 387 } 388 389 static void createMailboxTable(SQLiteDatabase db) { 390 String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, " 391 + MailboxColumns.DISPLAY_NAME + " text, " 392 + MailboxColumns.SERVER_ID + " text, " 393 + MailboxColumns.PARENT_SERVER_ID + " text, " 394 + MailboxColumns.PARENT_KEY + " integer, " 395 + MailboxColumns.ACCOUNT_KEY + " integer, " 396 + MailboxColumns.TYPE + " integer, " 397 + MailboxColumns.DELIMITER + " integer, " 398 + MailboxColumns.SYNC_KEY + " text, " 399 + MailboxColumns.SYNC_LOOKBACK + " integer, " 400 + MailboxColumns.SYNC_INTERVAL + " integer, " 401 + MailboxColumns.SYNC_TIME + " integer, " 402 + MailboxColumns.UNREAD_COUNT + " integer, " 403 + MailboxColumns.FLAG_VISIBLE + " integer, " 404 + MailboxColumns.FLAGS + " integer, " 405 + MailboxColumns.VISIBLE_LIMIT + " integer, " 406 + MailboxColumns.SYNC_STATUS + " text, " 407 + MailboxColumns.MESSAGE_COUNT + " integer not null default 0, " 408 + MailboxColumns.LAST_TOUCHED_TIME + " integer default 0, " 409 + MailboxColumns.UI_SYNC_STATUS + " integer default 0, " 410 + MailboxColumns.UI_LAST_SYNC_RESULT + " integer default 0, " 411 + MailboxColumns.LAST_NOTIFIED_MESSAGE_KEY + " integer not null default 0, " 412 + MailboxColumns.LAST_NOTIFIED_MESSAGE_COUNT + " integer not null default 0, " 413 + MailboxColumns.TOTAL_COUNT + " integer, " 414 + MailboxColumns.HIERARCHICAL_NAME + " text" 415 + ");"; 416 db.execSQL("create table " + Mailbox.TABLE_NAME + s); 417 db.execSQL("create index mailbox_" + MailboxColumns.SERVER_ID 418 + " on " + Mailbox.TABLE_NAME + " (" + MailboxColumns.SERVER_ID + ")"); 419 db.execSQL("create index mailbox_" + MailboxColumns.ACCOUNT_KEY 420 + " on " + Mailbox.TABLE_NAME + " (" + MailboxColumns.ACCOUNT_KEY + ")"); 421 // Deleting a Mailbox deletes associated Messages in all three tables 422 db.execSQL(TRIGGER_MAILBOX_DELETE); 423 } 424 425 static void resetMailboxTable(SQLiteDatabase db, int oldVersion, int newVersion) { 426 try { 427 db.execSQL("drop table " + Mailbox.TABLE_NAME); 428 } catch (SQLException e) { 429 } 430 createMailboxTable(db); 431 } 432 433 static void createAttachmentTable(SQLiteDatabase db) { 434 String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, " 435 + AttachmentColumns.FILENAME + " text, " 436 + AttachmentColumns.MIME_TYPE + " text, " 437 + AttachmentColumns.SIZE + " integer, " 438 + AttachmentColumns.CONTENT_ID + " text, " 439 + AttachmentColumns.CONTENT_URI + " text, " 440 + AttachmentColumns.MESSAGE_KEY + " integer, " 441 + AttachmentColumns.LOCATION + " text, " 442 + AttachmentColumns.ENCODING + " text, " 443 + AttachmentColumns.CONTENT + " text, " 444 + AttachmentColumns.FLAGS + " integer, " 445 + AttachmentColumns.CONTENT_BYTES + " blob, " 446 + AttachmentColumns.ACCOUNT_KEY + " integer, " 447 + AttachmentColumns.UI_STATE + " integer, " 448 + AttachmentColumns.UI_DESTINATION + " integer, " 449 + AttachmentColumns.UI_DOWNLOADED_SIZE + " integer" 450 + ");"; 451 db.execSQL("create table " + Attachment.TABLE_NAME + s); 452 db.execSQL(createIndex(Attachment.TABLE_NAME, AttachmentColumns.MESSAGE_KEY)); 453 } 454 455 static void resetAttachmentTable(SQLiteDatabase db, int oldVersion, int newVersion) { 456 try { 457 db.execSQL("drop table " + Attachment.TABLE_NAME); 458 } catch (SQLException e) { 459 } 460 createAttachmentTable(db); 461 } 462 463 static void createQuickResponseTable(SQLiteDatabase db) { 464 String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, " 465 + QuickResponseColumns.TEXT + " text, " 466 + QuickResponseColumns.ACCOUNT_KEY + " integer" 467 + ");"; 468 db.execSQL("create table " + QuickResponse.TABLE_NAME + s); 469 } 470 471 static void createBodyTable(SQLiteDatabase db) { 472 String s = " (" + EmailContent.RECORD_ID + " integer primary key autoincrement, " 473 + BodyColumns.MESSAGE_KEY + " integer, " 474 + BodyColumns.HTML_CONTENT + " text, " 475 + BodyColumns.TEXT_CONTENT + " text, " 476 + BodyColumns.HTML_REPLY + " text, " 477 + BodyColumns.TEXT_REPLY + " text, " 478 + BodyColumns.SOURCE_MESSAGE_KEY + " text, " 479 + BodyColumns.INTRO_TEXT + " text, " 480 + BodyColumns.QUOTED_TEXT_START_POS + " integer" 481 + ");"; 482 db.execSQL("create table " + Body.TABLE_NAME + s); 483 db.execSQL(createIndex(Body.TABLE_NAME, BodyColumns.MESSAGE_KEY)); 484 } 485 486 static void upgradeBodyTable(SQLiteDatabase db, int oldVersion, int newVersion) { 487 if (oldVersion < 5) { 488 try { 489 db.execSQL("drop table " + Body.TABLE_NAME); 490 createBodyTable(db); 491 oldVersion = 5; 492 } catch (SQLException e) { 493 } 494 } 495 if (oldVersion == 5) { 496 try { 497 db.execSQL("alter table " + Body.TABLE_NAME 498 + " add " + BodyColumns.INTRO_TEXT + " text"); 499 } catch (SQLException e) { 500 // Shouldn't be needed unless we're debugging and interrupt the process 501 Log.w(TAG, "Exception upgrading EmailProviderBody.db from v5 to v6", e); 502 } 503 oldVersion = 6; 504 } 505 if (oldVersion == 6 || oldVersion == 7) { 506 try { 507 db.execSQL("alter table " + Body.TABLE_NAME 508 + " add " + BodyColumns.QUOTED_TEXT_START_POS + " integer"); 509 } catch (SQLException e) { 510 // Shouldn't be needed unless we're debugging and interrupt the process 511 Log.w(TAG, "Exception upgrading EmailProviderBody.db from v6 to v8", e); 512 } 513 oldVersion = 8; 514 } 515 if (oldVersion == 8) { 516 // Move to Email2 version 517 oldVersion = 100; 518 } 519 } 520 521 protected static class BodyDatabaseHelper extends SQLiteOpenHelper { 522 BodyDatabaseHelper(Context context, String name) { 523 super(context, name, null, BODY_DATABASE_VERSION); 524 } 525 526 @Override 527 public void onCreate(SQLiteDatabase db) { 528 Log.d(TAG, "Creating EmailProviderBody database"); 529 createBodyTable(db); 530 } 531 532 @Override 533 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 534 upgradeBodyTable(db, oldVersion, newVersion); 535 } 536 537 @Override 538 public void onOpen(SQLiteDatabase db) { 539 } 540 } 541 542 /** Counts the number of messages in each mailbox, and updates the message count column. */ 543 @VisibleForTesting 544 static void recalculateMessageCount(SQLiteDatabase db) { 545 db.execSQL("update " + Mailbox.TABLE_NAME + " set " + MailboxColumns.MESSAGE_COUNT + 546 "= (select count(*) from " + Message.TABLE_NAME + 547 " where " + Message.MAILBOX_KEY + " = " + 548 Mailbox.TABLE_NAME + "." + EmailContent.RECORD_ID + ")"); 549 } 550 551 protected static class DatabaseHelper extends SQLiteOpenHelper { 552 Context mContext; 553 554 DatabaseHelper(Context context, String name) { 555 super(context, name, null, DATABASE_VERSION); 556 mContext = context; 557 } 558 559 @Override 560 public void onCreate(SQLiteDatabase db) { 561 Log.d(TAG, "Creating EmailProvider database"); 562 // Create all tables here; each class has its own method 563 createMessageTable(db); 564 createAttachmentTable(db); 565 createMailboxTable(db); 566 createHostAuthTable(db); 567 createAccountTable(db); 568 createPolicyTable(db); 569 createQuickResponseTable(db); 570 } 571 572 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 573 if (oldVersion == 101 && newVersion == 100) { 574 Log.d(TAG, "Downgrade from v101 to v100"); 575 } else { 576 super.onDowngrade(db, oldVersion, newVersion); 577 } 578 } 579 580 @Override 581 @SuppressWarnings("deprecation") 582 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 583 // For versions prior to 5, delete all data 584 // Versions >= 5 require that data be preserved! 585 if (oldVersion < 5) { 586 android.accounts.Account[] accounts = AccountManager.get(mContext) 587 .getAccountsByType(AccountManagerTypes.TYPE_EXCHANGE); 588 for (android.accounts.Account account: accounts) { 589 AccountManager.get(mContext).removeAccount(account, null, null); 590 } 591 resetMessageTable(db, oldVersion, newVersion); 592 resetAttachmentTable(db, oldVersion, newVersion); 593 resetMailboxTable(db, oldVersion, newVersion); 594 resetHostAuthTable(db, oldVersion, newVersion); 595 resetAccountTable(db, oldVersion, newVersion); 596 return; 597 } 598 if (oldVersion == 5) { 599 // Message Tables: Add SyncColumns.SERVER_TIMESTAMP 600 try { 601 db.execSQL("alter table " + Message.TABLE_NAME 602 + " add column " + SyncColumns.SERVER_TIMESTAMP + " integer" + ";"); 603 db.execSQL("alter table " + Message.UPDATED_TABLE_NAME 604 + " add column " + SyncColumns.SERVER_TIMESTAMP + " integer" + ";"); 605 db.execSQL("alter table " + Message.DELETED_TABLE_NAME 606 + " add column " + SyncColumns.SERVER_TIMESTAMP + " integer" + ";"); 607 } catch (SQLException e) { 608 // Shouldn't be needed unless we're debugging and interrupt the process 609 Log.w(TAG, "Exception upgrading EmailProvider.db from v5 to v6", e); 610 } 611 oldVersion = 6; 612 } 613 if (oldVersion == 6) { 614 // Use the newer mailbox_delete trigger 615 db.execSQL("drop trigger mailbox_delete;"); 616 db.execSQL(TRIGGER_MAILBOX_DELETE); 617 oldVersion = 7; 618 } 619 if (oldVersion == 7) { 620 // add the security (provisioning) column 621 try { 622 db.execSQL("alter table " + Account.TABLE_NAME 623 + " add column " + AccountColumns.SECURITY_FLAGS + " integer" + ";"); 624 } catch (SQLException e) { 625 // Shouldn't be needed unless we're debugging and interrupt the process 626 Log.w(TAG, "Exception upgrading EmailProvider.db from 7 to 8 " + e); 627 } 628 oldVersion = 8; 629 } 630 if (oldVersion == 8) { 631 // accounts: add security sync key & user signature columns 632 try { 633 db.execSQL("alter table " + Account.TABLE_NAME 634 + " add column " + AccountColumns.SECURITY_SYNC_KEY + " text" + ";"); 635 db.execSQL("alter table " + Account.TABLE_NAME 636 + " add column " + AccountColumns.SIGNATURE + " text" + ";"); 637 } catch (SQLException e) { 638 // Shouldn't be needed unless we're debugging and interrupt the process 639 Log.w(TAG, "Exception upgrading EmailProvider.db from 8 to 9 " + e); 640 } 641 oldVersion = 9; 642 } 643 if (oldVersion == 9) { 644 // Message: add meeting info column into Message tables 645 try { 646 db.execSQL("alter table " + Message.TABLE_NAME 647 + " add column " + MessageColumns.MEETING_INFO + " text" + ";"); 648 db.execSQL("alter table " + Message.UPDATED_TABLE_NAME 649 + " add column " + MessageColumns.MEETING_INFO + " text" + ";"); 650 db.execSQL("alter table " + Message.DELETED_TABLE_NAME 651 + " add column " + MessageColumns.MEETING_INFO + " text" + ";"); 652 } catch (SQLException e) { 653 // Shouldn't be needed unless we're debugging and interrupt the process 654 Log.w(TAG, "Exception upgrading EmailProvider.db from 9 to 10 " + e); 655 } 656 oldVersion = 10; 657 } 658 if (oldVersion == 10) { 659 // Attachment: add content and flags columns 660 try { 661 db.execSQL("alter table " + Attachment.TABLE_NAME 662 + " add column " + AttachmentColumns.CONTENT + " text" + ";"); 663 db.execSQL("alter table " + Attachment.TABLE_NAME 664 + " add column " + AttachmentColumns.FLAGS + " integer" + ";"); 665 } catch (SQLException e) { 666 // Shouldn't be needed unless we're debugging and interrupt the process 667 Log.w(TAG, "Exception upgrading EmailProvider.db from 10 to 11 " + e); 668 } 669 oldVersion = 11; 670 } 671 if (oldVersion == 11) { 672 // Attachment: add content_bytes 673 try { 674 db.execSQL("alter table " + Attachment.TABLE_NAME 675 + " add column " + AttachmentColumns.CONTENT_BYTES + " blob" + ";"); 676 } catch (SQLException e) { 677 // Shouldn't be needed unless we're debugging and interrupt the process 678 Log.w(TAG, "Exception upgrading EmailProvider.db from 11 to 12 " + e); 679 } 680 oldVersion = 12; 681 } 682 if (oldVersion == 12) { 683 try { 684 db.execSQL("alter table " + Mailbox.TABLE_NAME 685 + " add column " + Mailbox.MESSAGE_COUNT 686 +" integer not null default 0" + ";"); 687 recalculateMessageCount(db); 688 } catch (SQLException e) { 689 // Shouldn't be needed unless we're debugging and interrupt the process 690 Log.w(TAG, "Exception upgrading EmailProvider.db from 12 to 13 " + e); 691 } 692 oldVersion = 13; 693 } 694 if (oldVersion == 13) { 695 try { 696 db.execSQL("alter table " + Message.TABLE_NAME 697 + " add column " + Message.SNIPPET 698 +" text" + ";"); 699 } catch (SQLException e) { 700 // Shouldn't be needed unless we're debugging and interrupt the process 701 Log.w(TAG, "Exception upgrading EmailProvider.db from 13 to 14 " + e); 702 } 703 oldVersion = 14; 704 } 705 if (oldVersion == 14) { 706 try { 707 db.execSQL("alter table " + Message.DELETED_TABLE_NAME 708 + " add column " + Message.SNIPPET +" text" + ";"); 709 db.execSQL("alter table " + Message.UPDATED_TABLE_NAME 710 + " add column " + Message.SNIPPET +" text" + ";"); 711 } catch (SQLException e) { 712 // Shouldn't be needed unless we're debugging and interrupt the process 713 Log.w(TAG, "Exception upgrading EmailProvider.db from 14 to 15 " + e); 714 } 715 oldVersion = 15; 716 } 717 if (oldVersion == 15) { 718 try { 719 db.execSQL("alter table " + Attachment.TABLE_NAME 720 + " add column " + Attachment.ACCOUNT_KEY +" integer" + ";"); 721 // Update all existing attachments to add the accountKey data 722 db.execSQL("update " + Attachment.TABLE_NAME + " set " + 723 Attachment.ACCOUNT_KEY + "= (SELECT " + Message.TABLE_NAME + "." + 724 Message.ACCOUNT_KEY + " from " + Message.TABLE_NAME + " where " + 725 Message.TABLE_NAME + "." + Message.RECORD_ID + " = " + 726 Attachment.TABLE_NAME + "." + Attachment.MESSAGE_KEY + ")"); 727 } catch (SQLException e) { 728 // Shouldn't be needed unless we're debugging and interrupt the process 729 Log.w(TAG, "Exception upgrading EmailProvider.db from 15 to 16 " + e); 730 } 731 oldVersion = 16; 732 } 733 if (oldVersion == 16) { 734 try { 735 db.execSQL("alter table " + Mailbox.TABLE_NAME 736 + " add column " + Mailbox.PARENT_KEY + " integer;"); 737 } catch (SQLException e) { 738 // Shouldn't be needed unless we're debugging and interrupt the process 739 Log.w(TAG, "Exception upgrading EmailProvider.db from 16 to 17 " + e); 740 } 741 oldVersion = 17; 742 } 743 if (oldVersion == 17) { 744 upgradeFromVersion17ToVersion18(db); 745 oldVersion = 18; 746 } 747 if (oldVersion == 18) { 748 try { 749 db.execSQL("alter table " + Account.TABLE_NAME 750 + " add column " + Account.POLICY_KEY + " integer;"); 751 db.execSQL("drop trigger account_delete;"); 752 db.execSQL(TRIGGER_ACCOUNT_DELETE); 753 createPolicyTable(db); 754 convertPolicyFlagsToPolicyTable(db); 755 } catch (SQLException e) { 756 // Shouldn't be needed unless we're debugging and interrupt the process 757 Log.w(TAG, "Exception upgrading EmailProvider.db from 18 to 19 " + e); 758 } 759 oldVersion = 19; 760 } 761 if (oldVersion == 19) { 762 try { 763 db.execSQL("alter table " + Policy.TABLE_NAME 764 + " add column " + PolicyColumns.REQUIRE_MANUAL_SYNC_WHEN_ROAMING + 765 " integer;"); 766 db.execSQL("alter table " + Policy.TABLE_NAME 767 + " add column " + PolicyColumns.DONT_ALLOW_CAMERA + " integer;"); 768 db.execSQL("alter table " + Policy.TABLE_NAME 769 + " add column " + PolicyColumns.DONT_ALLOW_ATTACHMENTS + " integer;"); 770 db.execSQL("alter table " + Policy.TABLE_NAME 771 + " add column " + PolicyColumns.DONT_ALLOW_HTML + " integer;"); 772 db.execSQL("alter table " + Policy.TABLE_NAME 773 + " add column " + PolicyColumns.MAX_ATTACHMENT_SIZE + " integer;"); 774 db.execSQL("alter table " + Policy.TABLE_NAME 775 + " add column " + PolicyColumns.MAX_TEXT_TRUNCATION_SIZE + 776 " integer;"); 777 db.execSQL("alter table " + Policy.TABLE_NAME 778 + " add column " + PolicyColumns.MAX_HTML_TRUNCATION_SIZE + 779 " integer;"); 780 db.execSQL("alter table " + Policy.TABLE_NAME 781 + " add column " + PolicyColumns.MAX_EMAIL_LOOKBACK + " integer;"); 782 db.execSQL("alter table " + Policy.TABLE_NAME 783 + " add column " + PolicyColumns.MAX_CALENDAR_LOOKBACK + " integer;"); 784 db.execSQL("alter table " + Policy.TABLE_NAME 785 + " add column " + PolicyColumns.PASSWORD_RECOVERY_ENABLED + 786 " integer;"); 787 } catch (SQLException e) { 788 // Shouldn't be needed unless we're debugging and interrupt the process 789 Log.w(TAG, "Exception upgrading EmailProvider.db from 19 to 20 " + e); 790 } 791 oldVersion = 20; 792 } 793 if (oldVersion == 20) { 794 oldVersion = 21; 795 } 796 if (oldVersion == 21) { 797 upgradeFromVersion21ToVersion22(db, mContext); 798 oldVersion = 22; 799 } 800 if (oldVersion == 22) { 801 upgradeFromVersion22ToVersion23(db); 802 oldVersion = 23; 803 } 804 if (oldVersion == 23) { 805 upgradeFromVersion23ToVersion24(db); 806 oldVersion = 24; 807 } 808 if (oldVersion == 24) { 809 upgradeFromVersion24ToVersion25(db); 810 oldVersion = 25; 811 } 812 if (oldVersion == 25) { 813 upgradeFromVersion25ToVersion26(db); 814 oldVersion = 26; 815 } 816 if (oldVersion == 26) { 817 try { 818 db.execSQL("alter table " + Message.TABLE_NAME 819 + " add column " + Message.PROTOCOL_SEARCH_INFO + " text;"); 820 db.execSQL("alter table " + Message.DELETED_TABLE_NAME 821 + " add column " + Message.PROTOCOL_SEARCH_INFO +" text" + ";"); 822 db.execSQL("alter table " + Message.UPDATED_TABLE_NAME 823 + " add column " + Message.PROTOCOL_SEARCH_INFO +" text" + ";"); 824 } catch (SQLException e) { 825 // Shouldn't be needed unless we're debugging and interrupt the process 826 Log.w(TAG, "Exception upgrading EmailProvider.db from 26 to 27 " + e); 827 } 828 oldVersion = 27; 829 } 830 if (oldVersion == 27) { 831 oldVersion = 28; 832 } 833 if (oldVersion == 28) { 834 try { 835 db.execSQL("alter table " + Policy.TABLE_NAME 836 + " add column " + Policy.PROTOCOL_POLICIES_ENFORCED + " text;"); 837 db.execSQL("alter table " + Policy.TABLE_NAME 838 + " add column " + Policy.PROTOCOL_POLICIES_UNSUPPORTED + " text;"); 839 } catch (SQLException e) { 840 // Shouldn't be needed unless we're debugging and interrupt the process 841 Log.w(TAG, "Exception upgrading EmailProvider.db from 28 to 29 " + e); 842 } 843 oldVersion = 29; 844 } 845 if (oldVersion == 29) { 846 upgradeFromVersion29ToVersion30(db); 847 oldVersion = 30; 848 } 849 if (oldVersion == 30) { 850 try { 851 db.execSQL("alter table " + Mailbox.TABLE_NAME 852 + " add column " + Mailbox.UI_SYNC_STATUS + " integer;"); 853 db.execSQL("alter table " + Mailbox.TABLE_NAME 854 + " add column " + Mailbox.UI_LAST_SYNC_RESULT + " integer;"); 855 } catch (SQLException e) { 856 // Shouldn't be needed unless we're debugging and interrupt the process 857 Log.w(TAG, "Exception upgrading EmailProvider.db from 30 to 31 " + e); 858 } 859 oldVersion = 31; 860 } 861 if (oldVersion == 31) { 862 try { 863 db.execSQL("alter table " + Mailbox.TABLE_NAME 864 + " add column " + Mailbox.LAST_NOTIFIED_MESSAGE_KEY + " integer;"); 865 db.execSQL("alter table " + Mailbox.TABLE_NAME 866 + " add column " + Mailbox.LAST_NOTIFIED_MESSAGE_COUNT + " integer;"); 867 db.execSQL("update Mailbox set " + Mailbox.LAST_NOTIFIED_MESSAGE_KEY + 868 "=0 where " + Mailbox.LAST_NOTIFIED_MESSAGE_KEY + " IS NULL"); 869 db.execSQL("update Mailbox set " + Mailbox.LAST_NOTIFIED_MESSAGE_COUNT + 870 "=0 where " + Mailbox.LAST_NOTIFIED_MESSAGE_COUNT + " IS NULL"); 871 } catch (SQLException e) { 872 // Shouldn't be needed unless we're debugging and interrupt the process 873 Log.w(TAG, "Exception upgrading EmailProvider.db from 31 to 32 " + e); 874 } 875 oldVersion = 32; 876 } 877 if (oldVersion == 32) { 878 try { 879 db.execSQL("alter table " + Attachment.TABLE_NAME 880 + " add column " + Attachment.UI_STATE + " integer;"); 881 db.execSQL("alter table " + Attachment.TABLE_NAME 882 + " add column " + Attachment.UI_DESTINATION + " integer;"); 883 db.execSQL("alter table " + Attachment.TABLE_NAME 884 + " add column " + Attachment.UI_DOWNLOADED_SIZE + " integer;"); 885 // If we have a contentUri then the attachment is saved 886 // uiDestination of 0 = "cache", so we don't have to set this 887 db.execSQL("update " + Attachment.TABLE_NAME + " set " + Attachment.UI_STATE + 888 "=" + UIProvider.AttachmentState.SAVED + " where " + 889 AttachmentColumns.CONTENT_URI + " is not null;"); 890 } catch (SQLException e) { 891 // Shouldn't be needed unless we're debugging and interrupt the process 892 Log.w(TAG, "Exception upgrading EmailProvider.db from 32 to 33 " + e); 893 } 894 oldVersion = 33; 895 } 896 if (oldVersion == 33) { 897 try { 898 db.execSQL("alter table " + Mailbox.TABLE_NAME 899 + " add column " + MailboxColumns.TOTAL_COUNT + " integer;"); 900 } catch (SQLException e) { 901 // Shouldn't be needed unless we're debugging and interrupt the process 902 Log.w(TAG, "Exception upgrading EmailProvider.db from 33 to 34 " + e); 903 } 904 oldVersion = 34; 905 } 906 if (oldVersion == 34) { 907 try { 908 db.execSQL("update " + Mailbox.TABLE_NAME + " set " + 909 MailboxColumns.LAST_TOUCHED_TIME + " = " + 910 Mailbox.DRAFTS_DEFAULT_TOUCH_TIME + " WHERE " + MailboxColumns.TYPE + 911 " = " + Mailbox.TYPE_DRAFTS); 912 db.execSQL("update " + Mailbox.TABLE_NAME + " set " + 913 MailboxColumns.LAST_TOUCHED_TIME + " = " + 914 Mailbox.SENT_DEFAULT_TOUCH_TIME + " WHERE " + MailboxColumns.TYPE + 915 " = " + Mailbox.TYPE_SENT); 916 } catch (SQLException e) { 917 // Shouldn't be needed unless we're debugging and interrupt the process 918 Log.w(TAG, "Exception upgrading EmailProvider.db from 34 to 35 " + e); 919 } 920 oldVersion = 35; 921 } 922 if (oldVersion == 35 || oldVersion == 36) { 923 try { 924 // Set "supports settings" for EAS mailboxes 925 db.execSQL("update " + Mailbox.TABLE_NAME + " set " + 926 MailboxColumns.FLAGS + "=" + MailboxColumns.FLAGS + "|" + 927 Mailbox.FLAG_SUPPORTS_SETTINGS + " where (" + 928 MailboxColumns.FLAGS + "&" + Mailbox.FLAG_HOLDS_MAIL + ")!=0 and " + 929 MailboxColumns.ACCOUNT_KEY + " IN (SELECT " + Account.TABLE_NAME + 930 "." + AccountColumns.ID + " from " + Account.TABLE_NAME + "," + 931 HostAuth.TABLE_NAME + " where " + Account.TABLE_NAME + "." + 932 AccountColumns.HOST_AUTH_KEY_RECV + "=" + HostAuth.TABLE_NAME + "." + 933 HostAuthColumns.ID + " and " + HostAuthColumns.PROTOCOL + "='" + 934 HostAuth.LEGACY_SCHEME_EAS + "')"); 935 } catch (SQLException e) { 936 // Shouldn't be needed unless we're debugging and interrupt the process 937 Log.w(TAG, "Exception upgrading EmailProvider.db from 35 to 36 " + e); 938 } 939 oldVersion = 37; 940 } 941 if (oldVersion == 37) { 942 try { 943 db.execSQL("alter table " + Message.TABLE_NAME 944 + " add column " + MessageColumns.THREAD_TOPIC + " text;"); 945 } catch (SQLException e) { 946 // Shouldn't be needed unless we're debugging and interrupt the process 947 Log.w(TAG, "Exception upgrading EmailProvider.db from 37 to 38 " + e); 948 } 949 oldVersion = 38; 950 } 951 if (oldVersion == 38) { 952 try { 953 db.execSQL("alter table " + Message.DELETED_TABLE_NAME 954 + " add column " + MessageColumns.THREAD_TOPIC + " text;"); 955 db.execSQL("alter table " + Message.UPDATED_TABLE_NAME 956 + " add column " + MessageColumns.THREAD_TOPIC + " text;"); 957 } catch (SQLException e) { 958 // Shouldn't be needed unless we're debugging and interrupt the process 959 Log.w(TAG, "Exception upgrading EmailProvider.db from 38 to 39 " + e); 960 } 961 oldVersion = 39; 962 } 963 if (oldVersion == 39) { 964 upgradeToEmail2(db); 965 oldVersion = 100; 966 } 967 if (oldVersion >= 100 && oldVersion < 103) { 968 try { 969 db.execSQL("alter table " + Mailbox.TABLE_NAME 970 + " add " + MailboxColumns.HIERARCHICAL_NAME + " text"); 971 } catch (SQLException e) { 972 // Shouldn't be needed unless we're debugging and interrupt the process 973 Log.w(TAG, "Exception upgrading EmailProviderBody.db from v10x to v103", e); 974 } 975 oldVersion = 103; 976 } 977 if (oldVersion == 103) { 978 try { 979 db.execSQL("alter table " + Message.TABLE_NAME 980 + " add " + MessageColumns.SYNC_DATA + " text"); 981 } catch (SQLException e) { 982 // Shouldn't be needed unless we're debugging and interrupt the process 983 Log.w(TAG, "Exception upgrading EmailProviderBody.db from v103 to v104", e); 984 } 985 oldVersion = 104; 986 } 987 if (oldVersion == 104) { 988 try { 989 db.execSQL("alter table " + Message.UPDATED_TABLE_NAME 990 + " add " + MessageColumns.SYNC_DATA + " text"); 991 db.execSQL("alter table " + Message.DELETED_TABLE_NAME 992 + " add " + MessageColumns.SYNC_DATA + " text"); 993 } catch (SQLException e) { 994 // Shouldn't be needed unless we're debugging and interrupt the process 995 Log.w(TAG, "Exception upgrading EmailProviderBody.db from v104 to v105", e); 996 } 997 oldVersion = 105; 998 } 999 } 1000 1001 @Override 1002 public void onOpen(SQLiteDatabase db) { 1003 } 1004 } 1005 1006 @VisibleForTesting 1007 @SuppressWarnings("deprecation") 1008 static void convertPolicyFlagsToPolicyTable(SQLiteDatabase db) { 1009 Cursor c = db.query(Account.TABLE_NAME, 1010 new String[] {EmailContent.RECORD_ID /*0*/, AccountColumns.SECURITY_FLAGS /*1*/}, 1011 AccountColumns.SECURITY_FLAGS + ">0", null, null, null, null); 1012 ContentValues cv = new ContentValues(); 1013 String[] args = new String[1]; 1014 while (c.moveToNext()) { 1015 long securityFlags = c.getLong(1 /*SECURITY_FLAGS*/); 1016 Policy policy = LegacyPolicySet.flagsToPolicy(securityFlags); 1017 long policyId = db.insert(Policy.TABLE_NAME, null, policy.toContentValues()); 1018 cv.put(AccountColumns.POLICY_KEY, policyId); 1019 cv.putNull(AccountColumns.SECURITY_FLAGS); 1020 args[0] = Long.toString(c.getLong(0 /*RECORD_ID*/)); 1021 db.update(Account.TABLE_NAME, cv, EmailContent.RECORD_ID + "=?", args); 1022 } 1023 } 1024 1025 /** Upgrades the database from v17 to v18 */ 1026 @VisibleForTesting 1027 static void upgradeFromVersion17ToVersion18(SQLiteDatabase db) { 1028 // Copy the displayName column to the serverId column. In v18 of the database, 1029 // we use the serverId for IMAP/POP3 mailboxes instead of overloading the 1030 // display name. 1031 // 1032 // For posterity; this is the command we're executing: 1033 //sqlite> UPDATE mailbox SET serverid=displayname WHERE mailbox._id in ( 1034 // ...> SELECT mailbox._id FROM mailbox,account,hostauth WHERE 1035 // ...> (mailbox.parentkey isnull OR mailbox.parentkey=0) AND 1036 // ...> mailbox.accountkey=account._id AND 1037 // ...> account.hostauthkeyrecv=hostauth._id AND 1038 // ...> (hostauth.protocol='imap' OR hostauth.protocol='pop3')); 1039 try { 1040 db.execSQL( 1041 "UPDATE " + Mailbox.TABLE_NAME + " SET " 1042 + MailboxColumns.SERVER_ID + "=" + MailboxColumns.DISPLAY_NAME 1043 + " WHERE " 1044 + Mailbox.TABLE_NAME + "." + MailboxColumns.ID + " IN ( SELECT " 1045 + Mailbox.TABLE_NAME + "." + MailboxColumns.ID + " FROM " 1046 + Mailbox.TABLE_NAME + "," + Account.TABLE_NAME + "," 1047 + HostAuth.TABLE_NAME + " WHERE " 1048 + "(" 1049 + Mailbox.TABLE_NAME + "." + MailboxColumns.PARENT_KEY + " isnull OR " 1050 + Mailbox.TABLE_NAME + "." + MailboxColumns.PARENT_KEY + "=0 " 1051 + ") AND " 1052 + Mailbox.TABLE_NAME + "." + MailboxColumns.ACCOUNT_KEY + "=" 1053 + Account.TABLE_NAME + "." + AccountColumns.ID + " AND " 1054 + Account.TABLE_NAME + "." + AccountColumns.HOST_AUTH_KEY_RECV + "=" 1055 + HostAuth.TABLE_NAME + "." + HostAuthColumns.ID + " AND ( " 1056 + HostAuth.TABLE_NAME + "." + HostAuthColumns.PROTOCOL + "='imap' OR " 1057 + HostAuth.TABLE_NAME + "." + HostAuthColumns.PROTOCOL + "='pop3' ) )"); 1058 } catch (SQLException e) { 1059 // Shouldn't be needed unless we're debugging and interrupt the process 1060 Log.w(TAG, "Exception upgrading EmailProvider.db from 17 to 18 " + e); 1061 } 1062 ContentCache.invalidateAllCaches(); 1063 } 1064 1065 /** 1066 * Upgrade the database from v21 to v22 1067 * This entails creating AccountManager accounts for all pop3 and imap accounts 1068 */ 1069 1070 private static final String[] V21_ACCOUNT_PROJECTION = 1071 new String[] {AccountColumns.HOST_AUTH_KEY_RECV, AccountColumns.EMAIL_ADDRESS}; 1072 private static final int V21_ACCOUNT_RECV = 0; 1073 private static final int V21_ACCOUNT_EMAIL = 1; 1074 1075 private static final String[] V21_HOSTAUTH_PROJECTION = 1076 new String[] {HostAuthColumns.PROTOCOL, HostAuthColumns.PASSWORD}; 1077 private static final int V21_HOSTAUTH_PROTOCOL = 0; 1078 private static final int V21_HOSTAUTH_PASSWORD = 1; 1079 1080 static private void createAccountManagerAccount(Context context, String login, 1081 String password) { 1082 AccountManager accountManager = AccountManager.get(context); 1083 android.accounts.Account amAccount = 1084 new android.accounts.Account(login, AccountManagerTypes.TYPE_POP_IMAP); 1085 accountManager.addAccountExplicitly(amAccount, password, null); 1086 ContentResolver.setIsSyncable(amAccount, EmailContent.AUTHORITY, 1); 1087 ContentResolver.setSyncAutomatically(amAccount, EmailContent.AUTHORITY, true); 1088 ContentResolver.setIsSyncable(amAccount, ContactsContract.AUTHORITY, 0); 1089 ContentResolver.setIsSyncable(amAccount, CalendarContract.AUTHORITY, 0); 1090 } 1091 1092 @VisibleForTesting 1093 static void upgradeFromVersion21ToVersion22(SQLiteDatabase db, Context accountManagerContext) { 1094 try { 1095 // Loop through accounts, looking for pop/imap accounts 1096 Cursor accountCursor = db.query(Account.TABLE_NAME, V21_ACCOUNT_PROJECTION, null, 1097 null, null, null, null); 1098 try { 1099 String[] hostAuthArgs = new String[1]; 1100 while (accountCursor.moveToNext()) { 1101 hostAuthArgs[0] = accountCursor.getString(V21_ACCOUNT_RECV); 1102 // Get the "receive" HostAuth for this account 1103 Cursor hostAuthCursor = db.query(HostAuth.TABLE_NAME, 1104 V21_HOSTAUTH_PROJECTION, HostAuth.RECORD_ID + "=?", hostAuthArgs, 1105 null, null, null); 1106 try { 1107 if (hostAuthCursor.moveToFirst()) { 1108 String protocol = hostAuthCursor.getString(V21_HOSTAUTH_PROTOCOL); 1109 // If this is a pop3 or imap account, create the account manager account 1110 if (HostAuth.LEGACY_SCHEME_IMAP.equals(protocol) || 1111 HostAuth.LEGACY_SCHEME_POP3.equals(protocol)) { 1112 if (MailActivityEmail.DEBUG) { 1113 Log.d(TAG, "Create AccountManager account for " + protocol + 1114 "account: " + 1115 accountCursor.getString(V21_ACCOUNT_EMAIL)); 1116 } 1117 createAccountManagerAccount(accountManagerContext, 1118 accountCursor.getString(V21_ACCOUNT_EMAIL), 1119 hostAuthCursor.getString(V21_HOSTAUTH_PASSWORD)); 1120 // If an EAS account, make Email sync automatically (equivalent of 1121 // checking the "Sync Email" box in settings 1122 } else if (HostAuth.LEGACY_SCHEME_EAS.equals(protocol)) { 1123 android.accounts.Account amAccount = 1124 new android.accounts.Account( 1125 accountCursor.getString(V21_ACCOUNT_EMAIL), 1126 AccountManagerTypes.TYPE_EXCHANGE); 1127 ContentResolver.setIsSyncable(amAccount, EmailContent.AUTHORITY, 1); 1128 ContentResolver.setSyncAutomatically(amAccount, 1129 EmailContent.AUTHORITY, true); 1130 1131 } 1132 } 1133 } finally { 1134 hostAuthCursor.close(); 1135 } 1136 } 1137 } finally { 1138 accountCursor.close(); 1139 } 1140 } catch (SQLException e) { 1141 // Shouldn't be needed unless we're debugging and interrupt the process 1142 Log.w(TAG, "Exception upgrading EmailProvider.db from 20 to 21 " + e); 1143 } 1144 } 1145 1146 /** Upgrades the database from v22 to v23 */ 1147 private static void upgradeFromVersion22ToVersion23(SQLiteDatabase db) { 1148 try { 1149 db.execSQL("alter table " + Mailbox.TABLE_NAME 1150 + " add column " + Mailbox.LAST_TOUCHED_TIME + " integer default 0;"); 1151 } catch (SQLException e) { 1152 // Shouldn't be needed unless we're debugging and interrupt the process 1153 Log.w(TAG, "Exception upgrading EmailProvider.db from 22 to 23 " + e); 1154 } 1155 } 1156 1157 /** Adds in a column for information about a client certificate to use. */ 1158 private static void upgradeFromVersion23ToVersion24(SQLiteDatabase db) { 1159 try { 1160 db.execSQL("alter table " + HostAuth.TABLE_NAME 1161 + " add column " + HostAuth.CLIENT_CERT_ALIAS + " text;"); 1162 } catch (SQLException e) { 1163 // Shouldn't be needed unless we're debugging and interrupt the process 1164 Log.w(TAG, "Exception upgrading EmailProvider.db from 23 to 24 " + e); 1165 } 1166 } 1167 1168 /** Upgrades the database from v24 to v25 by creating table for quick responses */ 1169 private static void upgradeFromVersion24ToVersion25(SQLiteDatabase db) { 1170 try { 1171 createQuickResponseTable(db); 1172 } catch (SQLException e) { 1173 // Shouldn't be needed unless we're debugging and interrupt the process 1174 Log.w(TAG, "Exception upgrading EmailProvider.db from 24 to 25 " + e); 1175 } 1176 } 1177 1178 private static final String[] V25_ACCOUNT_PROJECTION = 1179 new String[] {AccountColumns.ID, AccountColumns.FLAGS, AccountColumns.HOST_AUTH_KEY_RECV}; 1180 private static final int V25_ACCOUNT_ID = 0; 1181 private static final int V25_ACCOUNT_FLAGS = 1; 1182 private static final int V25_ACCOUNT_RECV = 2; 1183 1184 private static final String[] V25_HOSTAUTH_PROJECTION = new String[] {HostAuthColumns.PROTOCOL}; 1185 private static final int V25_HOSTAUTH_PROTOCOL = 0; 1186 1187 /** Upgrades the database from v25 to v26 by adding FLAG_SUPPORTS_SEARCH to IMAP accounts */ 1188 private static void upgradeFromVersion25ToVersion26(SQLiteDatabase db) { 1189 try { 1190 // Loop through accounts, looking for imap accounts 1191 Cursor accountCursor = db.query(Account.TABLE_NAME, V25_ACCOUNT_PROJECTION, null, 1192 null, null, null, null); 1193 ContentValues cv = new ContentValues(); 1194 try { 1195 String[] hostAuthArgs = new String[1]; 1196 while (accountCursor.moveToNext()) { 1197 hostAuthArgs[0] = accountCursor.getString(V25_ACCOUNT_RECV); 1198 // Get the "receive" HostAuth for this account 1199 Cursor hostAuthCursor = db.query(HostAuth.TABLE_NAME, 1200 V25_HOSTAUTH_PROJECTION, HostAuth.RECORD_ID + "=?", hostAuthArgs, 1201 null, null, null); 1202 try { 1203 if (hostAuthCursor.moveToFirst()) { 1204 String protocol = hostAuthCursor.getString(V25_HOSTAUTH_PROTOCOL); 1205 // If this is an imap account, add the search flag 1206 if (HostAuth.LEGACY_SCHEME_IMAP.equals(protocol)) { 1207 String id = accountCursor.getString(V25_ACCOUNT_ID); 1208 int flags = accountCursor.getInt(V25_ACCOUNT_FLAGS); 1209 cv.put(AccountColumns.FLAGS, flags | Account.FLAGS_SUPPORTS_SEARCH); 1210 db.update(Account.TABLE_NAME, cv, Account.RECORD_ID + "=?", 1211 new String[] {id}); 1212 } 1213 } 1214 } finally { 1215 hostAuthCursor.close(); 1216 } 1217 } 1218 } finally { 1219 accountCursor.close(); 1220 } 1221 } catch (SQLException e) { 1222 // Shouldn't be needed unless we're debugging and interrupt the process 1223 Log.w(TAG, "Exception upgrading EmailProvider.db from 25 to 26 " + e); 1224 } 1225 } 1226 1227 /** Upgrades the database from v29 to v30 by updating all address fields in Message */ 1228 private static final int[] ADDRESS_COLUMN_INDICES = new int[] { 1229 Message.CONTENT_BCC_LIST_COLUMN, Message.CONTENT_CC_LIST_COLUMN, 1230 Message.CONTENT_FROM_LIST_COLUMN, Message.CONTENT_REPLY_TO_COLUMN, 1231 Message.CONTENT_TO_LIST_COLUMN 1232 }; 1233 private static final String[] ADDRESS_COLUMN_NAMES = new String[] { 1234 Message.BCC_LIST, Message.CC_LIST, Message.FROM_LIST, Message.REPLY_TO_LIST, Message.TO_LIST 1235 }; 1236 1237 private static void upgradeFromVersion29ToVersion30(SQLiteDatabase db) { 1238 try { 1239 // Loop through all messages, updating address columns to new format (CSV, RFC822) 1240 Cursor messageCursor = db.query(Message.TABLE_NAME, Message.CONTENT_PROJECTION, null, 1241 null, null, null, null); 1242 ContentValues cv = new ContentValues(); 1243 String[] whereArgs = new String[1]; 1244 try { 1245 while (messageCursor.moveToNext()) { 1246 for (int i = 0; i < ADDRESS_COLUMN_INDICES.length; i++) { 1247 Address[] addrs = 1248 Address.unpack(messageCursor.getString(ADDRESS_COLUMN_INDICES[i])); 1249 cv.put(ADDRESS_COLUMN_NAMES[i], Address.pack(addrs)); 1250 } 1251 whereArgs[0] = messageCursor.getString(Message.CONTENT_ID_COLUMN); 1252 db.update(Message.TABLE_NAME, cv, WHERE_ID, whereArgs); 1253 } 1254 } finally { 1255 messageCursor.close(); 1256 } 1257 } catch (SQLException e) { 1258 // Shouldn't be needed unless we're debugging and interrupt the process 1259 Log.w(TAG, "Exception upgrading EmailProvider.db from 29 to 30 " + e); 1260 } 1261 } 1262 1263 private static void upgradeToEmail2(SQLiteDatabase db) { 1264 // Perform cleanup operations from Email1 to Email2; Email1 will have added new 1265 // data that won't conform to what's expected in Email2 1266 1267 // From 31->32 upgrade 1268 try { 1269 db.execSQL("update Mailbox set " + Mailbox.LAST_NOTIFIED_MESSAGE_KEY + 1270 "=0 where " + Mailbox.LAST_NOTIFIED_MESSAGE_KEY + " IS NULL"); 1271 db.execSQL("update Mailbox set " + Mailbox.LAST_NOTIFIED_MESSAGE_COUNT + 1272 "=0 where " + Mailbox.LAST_NOTIFIED_MESSAGE_COUNT + " IS NULL"); 1273 } catch (SQLException e) { 1274 Log.w(TAG, "Exception upgrading EmailProvider.db from 31 to 32/100 " + e); 1275 } 1276 1277 // From 32->33 upgrade 1278 try { 1279 db.execSQL("update " + Attachment.TABLE_NAME + " set " + Attachment.UI_STATE + 1280 "=" + UIProvider.AttachmentState.SAVED + " where " + 1281 AttachmentColumns.CONTENT_URI + " is not null;"); 1282 } catch (SQLException e) { 1283 Log.w(TAG, "Exception upgrading EmailProvider.db from 32 to 33/100 " + e); 1284 } 1285 1286 // From 34->35 upgrade 1287 try { 1288 db.execSQL("update " + Mailbox.TABLE_NAME + " set " + 1289 MailboxColumns.LAST_TOUCHED_TIME + " = " + 1290 Mailbox.DRAFTS_DEFAULT_TOUCH_TIME + " WHERE " + MailboxColumns.TYPE + 1291 " = " + Mailbox.TYPE_DRAFTS); 1292 db.execSQL("update " + Mailbox.TABLE_NAME + " set " + 1293 MailboxColumns.LAST_TOUCHED_TIME + " = " + 1294 Mailbox.SENT_DEFAULT_TOUCH_TIME + " WHERE " + MailboxColumns.TYPE + 1295 " = " + Mailbox.TYPE_SENT); 1296 } catch (SQLException e) { 1297 Log.w(TAG, "Exception upgrading EmailProvider.db from 34 to 35/100 " + e); 1298 } 1299 1300 // From 35/36->37 1301 try { 1302 db.execSQL("update " + Mailbox.TABLE_NAME + " set " + 1303 MailboxColumns.FLAGS + "=" + MailboxColumns.FLAGS + "|" + 1304 Mailbox.FLAG_SUPPORTS_SETTINGS + " where (" + 1305 MailboxColumns.FLAGS + "&" + Mailbox.FLAG_HOLDS_MAIL + ")!=0 and " + 1306 MailboxColumns.ACCOUNT_KEY + " IN (SELECT " + Account.TABLE_NAME + 1307 "." + AccountColumns.ID + " from " + Account.TABLE_NAME + "," + 1308 HostAuth.TABLE_NAME + " where " + Account.TABLE_NAME + "." + 1309 AccountColumns.HOST_AUTH_KEY_RECV + "=" + HostAuth.TABLE_NAME + "." + 1310 HostAuthColumns.ID + " and " + HostAuthColumns.PROTOCOL + "='" + 1311 HostAuth.LEGACY_SCHEME_EAS + "')"); 1312 } catch (SQLException e) { 1313 Log.w(TAG, "Exception upgrading EmailProvider.db from 35/36 to 37/100 " + e); 1314 } 1315 } 1316} 1317