Merge "Cleaned up upgrade steps for versions older than JB"
diff --git a/src/com/android/providers/contacts/ContactsDatabaseHelper.java b/src/com/android/providers/contacts/ContactsDatabaseHelper.java
index 83f6195..b001653 100644
--- a/src/com/android/providers/contacts/ContactsDatabaseHelper.java
+++ b/src/com/android/providers/contacts/ContactsDatabaseHelper.java
@@ -137,6 +137,7 @@
* </pre>
*/
static final int DATABASE_VERSION = 1200;
+ private static final int MINIMUM_SUPPORTED_VERSION = 700;
public interface Tables {
public static final String CONTACTS = "contacts";
@@ -781,26 +782,6 @@
public static final int ADDRESS = 2;
}
- private interface Upgrade303Query {
- public static final String TABLE = Tables.DATA;
-
- public static final String SELECTION =
- DataColumns.MIMETYPE_ID + "=?" +
- " AND " + Data._ID + " NOT IN " +
- "(SELECT " + NameLookupColumns.DATA_ID + " FROM " + Tables.NAME_LOOKUP + ")" +
- " AND " + Data.DATA1 + " NOT NULL";
-
- public static final String COLUMNS[] = {
- Data._ID,
- Data.RAW_CONTACT_ID,
- Data.DATA1,
- };
-
- public static final int ID = 0;
- public static final int RAW_CONTACT_ID = 1;
- public static final int DATA1 = 2;
- }
-
private interface StructuredNameQuery {
public static final String TABLE = Tables.DATA;
@@ -834,37 +815,6 @@
public static final int NAME = 2;
}
- private interface StructName205Query {
- String TABLE = Tables.DATA_JOIN_RAW_CONTACTS;
- String COLUMNS[] = {
- DataColumns.CONCRETE_ID,
- Data.RAW_CONTACT_ID,
- RawContacts.DISPLAY_NAME_SOURCE,
- RawContacts.DISPLAY_NAME_PRIMARY,
- StructuredName.PREFIX,
- StructuredName.GIVEN_NAME,
- StructuredName.MIDDLE_NAME,
- StructuredName.FAMILY_NAME,
- StructuredName.SUFFIX,
- StructuredName.PHONETIC_FAMILY_NAME,
- StructuredName.PHONETIC_MIDDLE_NAME,
- StructuredName.PHONETIC_GIVEN_NAME,
- };
-
- int ID = 0;
- int RAW_CONTACT_ID = 1;
- int DISPLAY_NAME_SOURCE = 2;
- int DISPLAY_NAME = 3;
- int PREFIX = 4;
- int GIVEN_NAME = 5;
- int MIDDLE_NAME = 6;
- int FAMILY_NAME = 7;
- int SUFFIX = 8;
- int PHONETIC_FAMILY_NAME = 9;
- int PHONETIC_MIDDLE_NAME = 10;
- int PHONETIC_GIVEN_NAME = 11;
- }
-
private interface RawContactNameQuery {
public static final String RAW_SQL =
"SELECT "
@@ -907,21 +857,6 @@
public static final int PHONETIC_NAME_STYLE = 12; // data11
}
- private interface Organization205Query {
- String TABLE = Tables.DATA_JOIN_RAW_CONTACTS;
- String COLUMNS[] = {
- DataColumns.CONCRETE_ID,
- Data.RAW_CONTACT_ID,
- Organization.COMPANY,
- Organization.PHONETIC_NAME,
- };
-
- int ID = 0;
- int RAW_CONTACT_ID = 1;
- int COMPANY = 2;
- int PHONETIC_NAME = 3;
- }
-
public final static class NameLookupType {
public static final int NAME_EXACT = 0;
public static final int NAME_VARIANT = 1;
@@ -1039,7 +974,7 @@
protected ContactsDatabaseHelper(
Context context, String databaseName, boolean optimizationEnabled,
boolean isTestInstance) {
- super(context, databaseName, null, DATABASE_VERSION);
+ super(context, databaseName, null, DATABASE_VERSION, MINIMUM_SUPPORTED_VERSION, null);
boolean enableWal = android.provider.Settings.Global.getInt(context.getContentResolver(),
android.provider.Settings.Global.CONTACTS_DATABASE_WAL_ENABLED, 1) == 1;
setWriteAheadLoggingEnabled(enableWal);
@@ -1070,6 +1005,12 @@
}
@Override
+ public void onBeforeDelete(SQLiteDatabase db) {
+ Log.w(TAG, "Database version " + db.getVersion() + " for " + DATABASE_NAME
+ + " is no longer supported. Data will be lost on upgrading to " + DATABASE_VERSION);
+ }
+
+ @Override
public void onOpen(SQLiteDatabase db) {
Log.d(TAG, "WAL enabled for " + getDatabaseName() + ": " + db.isWriteAheadLoggingEnabled());
prepopulateCommonMimeTypes(db);
@@ -1163,7 +1104,7 @@
@Override
public void onCreate(SQLiteDatabase db) {
- Log.i(TAG, "Bootstrapping database version: " + DATABASE_VERSION);
+ Log.i(TAG, "Bootstrapping database " + DATABASE_NAME + " version: " + DATABASE_VERSION);
mSyncState.createDatabase(db);
@@ -1795,6 +1736,7 @@
db.execSQL("DROP VIEW IF EXISTS " + Views.ENTITIES + ";");
db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_USAGE_STAT + ";");
db.execSQL("DROP VIEW IF EXISTS " + Views.STREAM_ITEMS + ";");
+ db.execSQL("DROP VIEW IF EXISTS " + Views.METADATA_SYNC_STATE + ";");
db.execSQL("DROP VIEW IF EXISTS " + Views.METADATA_SYNC + ";");
String dataColumns =
@@ -2244,33 +2186,8 @@
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- if (oldVersion < 99) {
- Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion
- + ", data will be lost!");
-
- db.execSQL("DROP TABLE IF EXISTS " + Tables.CONTACTS + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.RAW_CONTACTS + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.PACKAGES + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.MIMETYPES + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.DATA + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.PHONE_LOOKUP + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.NAME_LOOKUP + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.NICKNAME_LOOKUP + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.GROUPS + ";");
- db.execSQL("DROP TABLE IF EXISTS activities;");
- db.execSQL("DROP TABLE IF EXISTS calls;");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.SETTINGS + ";");
- db.execSQL("DROP TABLE IF EXISTS " + Tables.STATUS_UPDATES + ";");
-
- // TODO: we should not be dropping agg_exceptions and contact_options. In case that
- // table's schema changes, we should try to preserve the data, because it was entered
- // by the user and has never been synched to the server.
- db.execSQL("DROP TABLE IF EXISTS " + Tables.AGGREGATION_EXCEPTIONS + ";");
- onCreate(db);
- return;
- }
-
- Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion);
+ Log.i(TAG,
+ "Upgrading " + DATABASE_NAME + " from version " + oldVersion + " to " + newVersion);
prepopulateCommonMimeTypes(db);
@@ -2282,428 +2199,6 @@
boolean rebuildSqliteStats = false;
boolean upgradeLocaleSpecificData = false;
- if (oldVersion == 99) {
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 100) {
- db.execSQL("CREATE INDEX IF NOT EXISTS mimetypes_mimetype_index ON "
- + Tables.MIMETYPES + " ("
- + MimetypesColumns.MIMETYPE + ","
- + MimetypesColumns._ID + ");");
- updateIndexStats(db, Tables.MIMETYPES,
- "mimetypes_mimetype_index", "50 1 1");
-
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 101) {
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 102) {
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 103) {
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 104 || oldVersion == 201) {
- LegacyApiSupport.createSettingsTable(db);
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 105) {
- upgradeToVersion202(db);
- upgradeNameLookup = true;
- oldVersion = 202;
- }
-
- if (oldVersion == 202) {
- upgradeToVersion203(db);
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 203) {
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 204) {
- upgradeToVersion205(db);
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 205) {
- upgradeToVersion206(db);
- upgradeViewsAndTriggers = true;
- oldVersion++;
- }
-
- if (oldVersion == 206) {
- // Fix for the bug where name lookup records for organizations would get removed by
- // unrelated updates of the data rows. No longer needed.
- oldVersion = 300;
- }
-
- if (oldVersion == 300) {
- upgradeViewsAndTriggers = true;
- oldVersion = 301;
- }
-
- if (oldVersion == 301) {
- upgradeViewsAndTriggers = true;
- oldVersion = 302;
- }
-
- if (oldVersion == 302) {
- upgradeEmailToVersion303(db);
- upgradeNicknameToVersion303(db);
- oldVersion = 303;
- }
-
- if (oldVersion == 303) {
- upgradeToVersion304(db);
- oldVersion = 304;
- }
-
- if (oldVersion == 304) {
- upgradeNameLookup = true;
- oldVersion = 305;
- }
-
- if (oldVersion == 305) {
- upgradeToVersion306(db);
- oldVersion = 306;
- }
-
- if (oldVersion == 306) {
- upgradeToVersion307(db);
- oldVersion = 307;
- }
-
- if (oldVersion == 307) {
- upgradeToVersion308(db);
- oldVersion = 308;
- }
-
- // Gingerbread upgrades.
- if (oldVersion < 350) {
- upgradeViewsAndTriggers = true;
- oldVersion = 351;
- }
-
- if (oldVersion == 351) {
- upgradeNameLookup = true;
- oldVersion = 352;
- }
-
- if (oldVersion == 352) {
- upgradeToVersion353(db);
- oldVersion = 353;
- }
-
- // Honeycomb upgrades.
- if (oldVersion < 400) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion400(db);
- oldVersion = 400;
- }
-
- if (oldVersion == 400) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion401(db);
- oldVersion = 401;
- }
-
- if (oldVersion == 401) {
- upgradeToVersion402(db);
- oldVersion = 402;
- }
-
- if (oldVersion == 402) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion403(db);
- oldVersion = 403;
- }
-
- if (oldVersion == 403) {
- upgradeViewsAndTriggers = true;
- oldVersion = 404;
- }
-
- if (oldVersion == 404) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion405(db);
- oldVersion = 405;
- }
-
- if (oldVersion == 405) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion406(db);
- oldVersion = 406;
- }
-
- if (oldVersion == 406) {
- upgradeViewsAndTriggers = true;
- oldVersion = 407;
- }
-
- if (oldVersion == 407) {
- oldVersion = 408; // Obsolete.
- }
-
- if (oldVersion == 408) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion409(db);
- oldVersion = 409;
- }
-
- if (oldVersion == 409) {
- upgradeViewsAndTriggers = true;
- oldVersion = 410;
- }
-
- if (oldVersion == 410) {
- upgradeToVersion411(db);
- oldVersion = 411;
- }
-
- if (oldVersion == 411) {
- // Same upgrade as 353, only on Honeycomb devices.
- upgradeToVersion353(db);
- oldVersion = 412;
- }
-
- if (oldVersion == 412) {
- upgradeToVersion413(db);
- oldVersion = 413;
- }
-
- if (oldVersion == 413) {
- upgradeNameLookup = true;
- oldVersion = 414;
- }
-
- if (oldVersion == 414) {
- upgradeToVersion415(db);
- upgradeViewsAndTriggers = true;
- oldVersion = 415;
- }
-
- if (oldVersion == 415) {
- upgradeToVersion416(db);
- oldVersion = 416;
- }
-
- if (oldVersion == 416) {
- upgradeLegacyApiSupport = true;
- oldVersion = 417;
- }
-
- // Honeycomb-MR1 upgrades.
- if (oldVersion < 500) {
- upgradeSearchIndex = true;
- }
-
- if (oldVersion < 501) {
- upgradeSearchIndex = true;
- upgradeToVersion501(db);
- oldVersion = 501;
- }
-
- if (oldVersion < 502) {
- upgradeSearchIndex = true;
- upgradeToVersion502(db);
- oldVersion = 502;
- }
-
- if (oldVersion < 503) {
- upgradeSearchIndex = true;
- oldVersion = 503;
- }
-
- if (oldVersion < 504) {
- upgradeToVersion504(db);
- oldVersion = 504;
- }
-
- if (oldVersion < 600) {
- // This change used to add the profile raw contact ID to the Accounts table. That
- // column is no longer needed (as of version 614) since the profile records are stored in
- // a separate copy of the database for security reasons. So this change is now a no-op.
- upgradeViewsAndTriggers = true;
- oldVersion = 600;
- }
-
- if (oldVersion < 601) {
- upgradeToVersion601(db);
- oldVersion = 601;
- }
-
- if (oldVersion < 602) {
- upgradeToVersion602(db);
- oldVersion = 602;
- }
-
- if (oldVersion < 603) {
- upgradeViewsAndTriggers = true;
- oldVersion = 603;
- }
-
- if (oldVersion < 604) {
- upgradeToVersion604(db);
- oldVersion = 604;
- }
-
- if (oldVersion < 605) {
- upgradeViewsAndTriggers = true;
- // This version used to create the stream item and stream item photos tables, but
- // a newer version of those tables is created in version 609 below. So omitting the
- // creation in this upgrade step to avoid a create->drop->create.
- oldVersion = 605;
- }
-
- if (oldVersion < 606) {
- upgradeViewsAndTriggers = true;
- upgradeLegacyApiSupport = true;
- upgradeToVersion606(db);
- oldVersion = 606;
- }
-
- if (oldVersion < 607) {
- upgradeViewsAndTriggers = true;
- // We added "action" and "action_uri" to groups here, but realized this was not a smart
- // move. This upgrade step has been removed (all dogfood phones that executed this step
- // will have those columns, but that shouldn't hurt. Unfortunately, SQLite makes it
- // hard to remove columns).
- oldVersion = 607;
- }
-
- if (oldVersion < 608) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion608(db);
- oldVersion = 608;
- }
-
- if (oldVersion < 609) {
- // This version used to create the stream item and stream item photos tables, but a
- // newer version of those tables is created in version 613 below. So omitting the
- // creation in this upgrade step to avoid a create->drop->create.
- oldVersion = 609;
- }
-
- if (oldVersion < 610) {
- upgradeToVersion610(db);
- oldVersion = 610;
- }
-
- if (oldVersion < 611) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion611(db);
- oldVersion = 611;
- }
-
- if (oldVersion < 612) {
- upgradeViewsAndTriggers = true;
- upgradeToVersion612(db);
- oldVersion = 612;
- }
-
- if (oldVersion < 613) {
- upgradeToVersion613(db);
- oldVersion = 613;
- }
-
- if (oldVersion < 614) {
- // This creates the "view_stream_items" view.
- upgradeViewsAndTriggers = true;
- oldVersion = 614;
- }
-
- if (oldVersion < 615) {
- upgradeToVersion615(db);
- oldVersion = 615;
- }
-
- if (oldVersion < 616) {
- // This updates the "view_stream_items" view.
- upgradeViewsAndTriggers = true;
- oldVersion = 616;
- }
-
- if (oldVersion < 617) {
- // This version upgrade obsoleted the profile_raw_contact_id field of the Accounts
- // table, but we aren't removing the column because it is very little data (and not
- // referenced anymore). We do need to upgrade the views to handle the simplified
- // per-database "is profile" columns.
- upgradeViewsAndTriggers = true;
- oldVersion = 617;
- }
-
- if (oldVersion < 618) {
- upgradeToVersion618(db);
- oldVersion = 618;
- }
-
- if (oldVersion < 619) {
- upgradeViewsAndTriggers = true;
- oldVersion = 619;
- }
-
- if (oldVersion < 620) {
- upgradeViewsAndTriggers = true;
- oldVersion = 620;
- }
-
- if (oldVersion < 621) {
- upgradeSearchIndex = true;
- oldVersion = 621;
- }
-
- if (oldVersion < 622) {
- upgradeToVersion622(db);
- oldVersion = 622;
- }
-
- if (oldVersion < 623) {
- // Change FTS to normalize names using collation key.
- upgradeSearchIndex = true;
- oldVersion = 623;
- }
-
- if (oldVersion < 624) {
- // Upgraded the SQLite index stats.
- upgradeViewsAndTriggers = true;
- oldVersion = 624;
- }
-
- if (oldVersion < 625) {
- // Fix for search for hyphenated names
- upgradeSearchIndex = true;
- oldVersion = 625;
- }
-
- if (oldVersion < 626) {
- upgradeToVersion626(db);
- upgradeViewsAndTriggers = true;
- oldVersion = 626;
- }
-
- if (oldVersion < 700) {
- rescanDirectories = true;
- oldVersion = 700;
- }
-
if (oldVersion < 701) {
upgradeToVersion701(db);
oldVersion = 701;
@@ -3017,471 +2512,6 @@
return oldVersion < version && newVersion >= version;
}
- private void upgradeToVersion202(SQLiteDatabase db) {
- db.execSQL(
- "ALTER TABLE " + Tables.PHONE_LOOKUP +
- " ADD " + PhoneLookupColumns.MIN_MATCH + " TEXT;");
-
- db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" +
- PhoneLookupColumns.MIN_MATCH + "," +
- PhoneLookupColumns.RAW_CONTACT_ID + "," +
- PhoneLookupColumns.DATA_ID +
- ");");
-
- updateIndexStats(db, Tables.PHONE_LOOKUP,
- "phone_lookup_min_match_index", "10000 2 2 1");
-
- final SQLiteStatement update = db.compileStatement(
- "UPDATE " + Tables.PHONE_LOOKUP +
- " SET " + PhoneLookupColumns.MIN_MATCH + "=?" +
- " WHERE " + PhoneLookupColumns.DATA_ID + "=?");
-
- // Populate the new column
- Cursor c = db.query(Tables.PHONE_LOOKUP + " JOIN " + Tables.DATA +
- " ON (" + PhoneLookupColumns.DATA_ID + "=" + DataColumns.CONCRETE_ID + ")",
- new String[] {Data._ID, Phone.NUMBER}, null, null, null, null, null);
- try {
- while (c.moveToNext()) {
- long dataId = c.getLong(0);
- String number = c.getString(1);
- if (!TextUtils.isEmpty(number)) {
- update.bindString(1, PhoneNumberUtils.toCallerIDMinMatch(number));
- update.bindLong(2, dataId);
- update.execute();
- }
- }
- } finally {
- c.close();
- }
- }
-
- private void upgradeToVersion203(SQLiteDatabase db) {
- // Garbage-collect first. A bug in Eclair was sometimes leaving
- // raw_contacts in the database that no longer had contacts associated
- // with them. To avoid failures during this database upgrade, drop
- // the orphaned raw_contacts.
- db.execSQL(
- "DELETE FROM raw_contacts" +
- " WHERE contact_id NOT NULL" +
- " AND contact_id NOT IN (SELECT _id FROM contacts)");
-
- db.execSQL(
- "ALTER TABLE " + Tables.CONTACTS +
- " ADD " + Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)");
- db.execSQL(
- "ALTER TABLE " + Tables.RAW_CONTACTS +
- " ADD contact_in_visible_group INTEGER NOT NULL DEFAULT 0");
-
- // For each Contact, find the RawContact that contributed the display name
- db.execSQL(
- "UPDATE " + Tables.CONTACTS +
- " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" +
- " SELECT " + RawContacts._ID +
- " FROM " + Tables.RAW_CONTACTS +
- " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
- " AND " + RawContactsColumns.CONCRETE_DISPLAY_NAME + "=" +
- Tables.CONTACTS + "." + Contacts.DISPLAY_NAME +
- " ORDER BY " + RawContacts._ID +
- " LIMIT 1)"
- );
-
- db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" +
- Contacts.NAME_RAW_CONTACT_ID +
- ");");
-
- // If for some unknown reason we missed some names, let's make sure there are
- // no contacts without a name, picking a raw contact "at random".
- db.execSQL(
- "UPDATE " + Tables.CONTACTS +
- " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" +
- " SELECT " + RawContacts._ID +
- " FROM " + Tables.RAW_CONTACTS +
- " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
- " ORDER BY " + RawContacts._ID +
- " LIMIT 1)" +
- " WHERE " + Contacts.NAME_RAW_CONTACT_ID + " IS NULL"
- );
-
- // Wipe out DISPLAY_NAME on the Contacts table as it is no longer in use.
- db.execSQL(
- "UPDATE " + Tables.CONTACTS +
- " SET " + Contacts.DISPLAY_NAME + "=NULL"
- );
-
- // Copy the IN_VISIBLE_GROUP flag down to all raw contacts to allow
- // indexing on (display_name, in_visible_group)
- db.execSQL(
- "UPDATE " + Tables.RAW_CONTACTS +
- " SET contact_in_visible_group=(" +
- "SELECT " + Contacts.IN_VISIBLE_GROUP +
- " FROM " + Tables.CONTACTS +
- " WHERE " + Contacts._ID + "=" + RawContacts.CONTACT_ID + ")" +
- " WHERE " + RawContacts.CONTACT_ID + " NOT NULL"
- );
-
- db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
- "contact_in_visible_group" + "," +
- RawContactsColumns.DISPLAY_NAME + " COLLATE LOCALIZED ASC" +
- ");");
-
- db.execSQL("DROP INDEX contacts_visible_index");
- db.execSQL("CREATE INDEX contacts_visible_index ON " + Tables.CONTACTS + " (" +
- Contacts.IN_VISIBLE_GROUP +
- ");");
- }
-
- private void upgradeToVersion205(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
- + " ADD " + RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT;");
- db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
- + " ADD " + RawContacts.PHONETIC_NAME + " TEXT;");
- db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
- + " ADD " + RawContacts.PHONETIC_NAME_STYLE + " INTEGER;");
- db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
- + " ADD " + RawContacts.SORT_KEY_PRIMARY
- + " TEXT COLLATE " + ContactsProvider2.PHONEBOOK_COLLATOR_NAME + ";");
- db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
- + " ADD " + RawContacts.SORT_KEY_ALTERNATIVE
- + " TEXT COLLATE " + ContactsProvider2.PHONEBOOK_COLLATOR_NAME + ";");
-
- NameSplitter splitter = createNameSplitter();
-
- final SQLiteStatement rawContactUpdate = db.compileStatement(
- "UPDATE " + Tables.RAW_CONTACTS +
- " SET " +
- RawContacts.DISPLAY_NAME_PRIMARY + "=?," +
- RawContacts.DISPLAY_NAME_ALTERNATIVE + "=?," +
- RawContacts.PHONETIC_NAME + "=?," +
- RawContacts.PHONETIC_NAME_STYLE + "=?," +
- RawContacts.SORT_KEY_PRIMARY + "=?," +
- RawContacts.SORT_KEY_ALTERNATIVE + "=?" +
- " WHERE " + RawContacts._ID + "=?");
-
- upgradeStructuredNamesToVersion205(db, rawContactUpdate, splitter);
- upgradeOrganizationsToVersion205(db, rawContactUpdate, splitter);
-
- db.execSQL("DROP INDEX raw_contact_sort_key1_index");
- db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
- "contact_in_visible_group" + "," +
- RawContacts.SORT_KEY_PRIMARY +
- ");");
-
- db.execSQL("CREATE INDEX raw_contact_sort_key2_index ON " + Tables.RAW_CONTACTS + " (" +
- "contact_in_visible_group" + "," +
- RawContacts.SORT_KEY_ALTERNATIVE +
- ");");
- }
-
- private void upgradeStructuredNamesToVersion205(
- SQLiteDatabase db, SQLiteStatement rawContactUpdate, NameSplitter splitter) {
-
- // Process structured names to detect the style of the full name and phonetic name.
- long mMimeType;
- try {
- mMimeType = DatabaseUtils.longForQuery(db,
- "SELECT " + MimetypesColumns._ID +
- " FROM " + Tables.MIMETYPES +
- " WHERE " + MimetypesColumns.MIMETYPE
- + "='" + StructuredName.CONTENT_ITEM_TYPE + "'", null);
-
- } catch (SQLiteDoneException e) {
- // No structured names in the database.
- return;
- }
-
- final SQLiteStatement structuredNameUpdate = db.compileStatement(
- "UPDATE " + Tables.DATA +
- " SET " +
- StructuredName.FULL_NAME_STYLE + "=?," +
- StructuredName.DISPLAY_NAME + "=?," +
- StructuredName.PHONETIC_NAME_STYLE + "=?" +
- " WHERE " + Data._ID + "=?");
-
- NameSplitter.Name name = new NameSplitter.Name();
- Cursor cursor = db.query(StructName205Query.TABLE,
- StructName205Query.COLUMNS,
- DataColumns.MIMETYPE_ID + "=" + mMimeType, null, null, null, null);
- try {
- while (cursor.moveToNext()) {
- long dataId = cursor.getLong(StructName205Query.ID);
- long rawContactId = cursor.getLong(StructName205Query.RAW_CONTACT_ID);
- int displayNameSource = cursor.getInt(StructName205Query.DISPLAY_NAME_SOURCE);
-
- name.clear();
- name.prefix = cursor.getString(StructName205Query.PREFIX);
- name.givenNames = cursor.getString(StructName205Query.GIVEN_NAME);
- name.middleName = cursor.getString(StructName205Query.MIDDLE_NAME);
- name.familyName = cursor.getString(StructName205Query.FAMILY_NAME);
- name.suffix = cursor.getString(StructName205Query.SUFFIX);
- name.phoneticFamilyName = cursor.getString(StructName205Query.PHONETIC_FAMILY_NAME);
- name.phoneticMiddleName = cursor.getString(StructName205Query.PHONETIC_MIDDLE_NAME);
- name.phoneticGivenName = cursor.getString(StructName205Query.PHONETIC_GIVEN_NAME);
-
- upgradeNameToVersion205(dataId, rawContactId, displayNameSource, name,
- structuredNameUpdate, rawContactUpdate, splitter);
- }
- } finally {
- cursor.close();
- }
- }
-
- private void upgradeNameToVersion205(
- long dataId,
- long rawContactId,
- int displayNameSource,
- NameSplitter.Name name,
- SQLiteStatement structuredNameUpdate,
- SQLiteStatement rawContactUpdate,
- NameSplitter splitter) {
-
- splitter.guessNameStyle(name);
- int unadjustedFullNameStyle = name.fullNameStyle;
- name.fullNameStyle = splitter.getAdjustedFullNameStyle(name.fullNameStyle);
- String displayName = splitter.join(name, true, true);
-
- // Don't update database with the adjusted fullNameStyle as it is locale
- // related
- structuredNameUpdate.bindLong(1, unadjustedFullNameStyle);
- DatabaseUtils.bindObjectToProgram(structuredNameUpdate, 2, displayName);
- structuredNameUpdate.bindLong(3, name.phoneticNameStyle);
- structuredNameUpdate.bindLong(4, dataId);
- structuredNameUpdate.execute();
-
- if (displayNameSource == DisplayNameSources.STRUCTURED_NAME) {
- String displayNameAlternative = splitter.join(name, false, false);
- String phoneticName = splitter.joinPhoneticName(name);
- String sortKey = null;
- String sortKeyAlternative = null;
-
- if (phoneticName != null) {
- sortKey = sortKeyAlternative = phoneticName;
- } else if (name.fullNameStyle == FullNameStyle.CHINESE ||
- name.fullNameStyle == FullNameStyle.CJK) {
- sortKey = sortKeyAlternative = displayName;
- }
-
- if (sortKey == null) {
- sortKey = displayName;
- sortKeyAlternative = displayNameAlternative;
- }
-
- updateRawContact205(rawContactUpdate, rawContactId, displayName,
- displayNameAlternative, name.phoneticNameStyle, phoneticName, sortKey,
- sortKeyAlternative);
- }
- }
-
- private void upgradeOrganizationsToVersion205(
- SQLiteDatabase db, SQLiteStatement rawContactUpdate, NameSplitter splitter) {
-
- final long mimeType = lookupMimeTypeId(db, Organization.CONTENT_ITEM_TYPE);
- final SQLiteStatement organizationUpdate = db.compileStatement(
- "UPDATE " + Tables.DATA +
- " SET " +
- Organization.PHONETIC_NAME_STYLE + "=?" +
- " WHERE " + Data._ID + "=?");
-
- Cursor cursor = db.query(Organization205Query.TABLE, Organization205Query.COLUMNS,
- DataColumns.MIMETYPE_ID + "=" + mimeType + " AND "
- + RawContacts.DISPLAY_NAME_SOURCE + "=" + DisplayNameSources.ORGANIZATION,
- null, null, null, null);
- try {
- while (cursor.moveToNext()) {
- long dataId = cursor.getLong(Organization205Query.ID);
- long rawContactId = cursor.getLong(Organization205Query.RAW_CONTACT_ID);
- String company = cursor.getString(Organization205Query.COMPANY);
- String phoneticName = cursor.getString(Organization205Query.PHONETIC_NAME);
-
- int phoneticNameStyle = splitter.guessPhoneticNameStyle(phoneticName);
-
- organizationUpdate.bindLong(1, phoneticNameStyle);
- organizationUpdate.bindLong(2, dataId);
- organizationUpdate.execute();
-
- String sortKey = company;
-
- updateRawContact205(rawContactUpdate, rawContactId, company,
- company, phoneticNameStyle, phoneticName, sortKey, sortKey);
- }
- } finally {
- cursor.close();
- }
- }
-
- private void updateRawContact205(SQLiteStatement rawContactUpdate, long rawContactId,
- String displayName, String displayNameAlternative, int phoneticNameStyle,
- String phoneticName, String sortKeyPrimary, String sortKeyAlternative) {
- bindString(rawContactUpdate, 1, displayName);
- bindString(rawContactUpdate, 2, displayNameAlternative);
- bindString(rawContactUpdate, 3, phoneticName);
- rawContactUpdate.bindLong(4, phoneticNameStyle);
- bindString(rawContactUpdate, 5, sortKeyPrimary);
- bindString(rawContactUpdate, 6, sortKeyAlternative);
- rawContactUpdate.bindLong(7, rawContactId);
- rawContactUpdate.execute();
- }
-
- private void upgradeToVersion206(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
- + " ADD name_verified INTEGER NOT NULL DEFAULT 0;");
- }
-
- /**
- * The {@link ContactsProvider2#update} method was deleting name lookup for new
- * emails during the sync. We need to restore the lost name lookup rows.
- */
- private void upgradeEmailToVersion303(SQLiteDatabase db) {
- final long mimeTypeId = lookupMimeTypeId(db, Email.CONTENT_ITEM_TYPE);
- if (mimeTypeId == -1) {
- return;
- }
-
- ContentValues values = new ContentValues();
-
- // Find all data rows with the mime type "email" that are missing name lookup
- Cursor cursor = db.query(Upgrade303Query.TABLE, Upgrade303Query.COLUMNS,
- Upgrade303Query.SELECTION, new String[] {String.valueOf(mimeTypeId)},
- null, null, null);
- try {
- while (cursor.moveToNext()) {
- long dataId = cursor.getLong(Upgrade303Query.ID);
- long rawContactId = cursor.getLong(Upgrade303Query.RAW_CONTACT_ID);
- String value = cursor.getString(Upgrade303Query.DATA1);
- value = extractHandleFromEmailAddress(value);
-
- if (value != null) {
- values.put(NameLookupColumns.DATA_ID, dataId);
- values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId);
- values.put(NameLookupColumns.NAME_TYPE, NameLookupType.EMAIL_BASED_NICKNAME);
- values.put(NameLookupColumns.NORMALIZED_NAME, NameNormalizer.normalize(value));
- db.insert(Tables.NAME_LOOKUP, null, values);
- }
- }
- } finally {
- cursor.close();
- }
- }
-
- /**
- * The {@link ContactsProvider2#update} method was deleting name lookup for new
- * nicknames during the sync. We need to restore the lost name lookup rows.
- */
- private void upgradeNicknameToVersion303(SQLiteDatabase db) {
- final long mimeTypeId = lookupMimeTypeId(db, Nickname.CONTENT_ITEM_TYPE);
- if (mimeTypeId == -1) {
- return;
- }
-
- ContentValues values = new ContentValues();
-
- // Find all data rows with the mime type "nickname" that are missing name lookup
- Cursor cursor = db.query(Upgrade303Query.TABLE, Upgrade303Query.COLUMNS,
- Upgrade303Query.SELECTION, new String[] {String.valueOf(mimeTypeId)},
- null, null, null);
- try {
- while (cursor.moveToNext()) {
- long dataId = cursor.getLong(Upgrade303Query.ID);
- long rawContactId = cursor.getLong(Upgrade303Query.RAW_CONTACT_ID);
- String value = cursor.getString(Upgrade303Query.DATA1);
-
- values.put(NameLookupColumns.DATA_ID, dataId);
- values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId);
- values.put(NameLookupColumns.NAME_TYPE, NameLookupType.NICKNAME);
- values.put(NameLookupColumns.NORMALIZED_NAME, NameNormalizer.normalize(value));
- db.insert(Tables.NAME_LOOKUP, null, values);
- }
- } finally {
- cursor.close();
- }
- }
-
- private void upgradeToVersion304(SQLiteDatabase db) {
- // Mimetype table requires an index on mime type.
- db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS mime_type ON " + Tables.MIMETYPES + " (" +
- MimetypesColumns.MIMETYPE +
- ");");
- }
-
- private void upgradeToVersion306(SQLiteDatabase db) {
- // Fix invalid lookup that was used for Exchange contacts (it was not escaped)
- // It happened when a new contact was created AND synchronized
- final StringBuilder lookupKeyBuilder = new StringBuilder();
- final SQLiteStatement updateStatement = db.compileStatement(
- "UPDATE contacts " +
- "SET lookup=? " +
- "WHERE _id=?");
- final Cursor contactIdCursor = db.rawQuery(
- "SELECT DISTINCT contact_id " +
- "FROM raw_contacts " +
- "WHERE deleted=0 AND account_type='com.android.exchange'",
- null);
- try {
- while (contactIdCursor.moveToNext()) {
- final long contactId = contactIdCursor.getLong(0);
- lookupKeyBuilder.setLength(0);
- final Cursor c = db.rawQuery(
- "SELECT account_type, account_name, _id, sourceid, display_name " +
- "FROM raw_contacts " +
- "WHERE contact_id=? " +
- "ORDER BY _id",
- new String[] {String.valueOf(contactId)});
- try {
- while (c.moveToNext()) {
- ContactLookupKey.appendToLookupKey(lookupKeyBuilder,
- c.getString(0),
- c.getString(1),
- c.getLong(2),
- c.getString(3),
- c.getString(4));
- }
- } finally {
- c.close();
- }
-
- if (lookupKeyBuilder.length() == 0) {
- updateStatement.bindNull(1);
- } else {
- updateStatement.bindString(1, Uri.encode(lookupKeyBuilder.toString()));
- }
- updateStatement.bindLong(2, contactId);
-
- updateStatement.execute();
- }
- } finally {
- updateStatement.close();
- contactIdCursor.close();
- }
- }
-
- private void upgradeToVersion307(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE properties (" +
- "property_key TEXT PRIMARY_KEY, " +
- "property_value TEXT" +
- ");");
- }
-
- private void upgradeToVersion308(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE accounts (" +
- "account_name TEXT, " +
- "account_type TEXT " +
- ");");
-
- db.execSQL("INSERT INTO accounts " +
- "SELECT DISTINCT account_name, account_type FROM raw_contacts");
- }
-
- private void upgradeToVersion400(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE " + Tables.GROUPS
- + " ADD " + Groups.FAVORITES + " INTEGER NOT NULL DEFAULT 0;");
- db.execSQL("ALTER TABLE " + Tables.GROUPS
- + " ADD " + Groups.AUTO_ADD + " INTEGER NOT NULL DEFAULT 0;");
- }
-
- private void upgradeToVersion353(SQLiteDatabase db) {
- db.execSQL("DELETE FROM contacts " +
- "WHERE NOT EXISTS (SELECT 1 FROM raw_contacts WHERE contact_id=contacts._id)");
- }
-
private void rebuildNameLookup(SQLiteDatabase db, boolean rebuildSqliteStats) {
db.execSQL("DROP INDEX IF EXISTS name_lookup_index");
insertNameLookup(db);
@@ -3699,443 +2729,6 @@
stmt.executeInsert();
}
- /**
- * Changing the VISIBLE bit from a field on both RawContacts and Contacts to a separate table.
- */
- private void upgradeToVersion401(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE " + Tables.VISIBLE_CONTACTS + " (" +
- Contacts._ID + " INTEGER PRIMARY KEY" +
- ");");
- db.execSQL("INSERT INTO " + Tables.VISIBLE_CONTACTS +
- " SELECT " + Contacts._ID +
- " FROM " + Tables.CONTACTS +
- " WHERE " + Contacts.IN_VISIBLE_GROUP + "!=0");
- db.execSQL("DROP INDEX contacts_visible_index");
- }
-
- /**
- * Introducing a new table: directories.
- */
- private void upgradeToVersion402(SQLiteDatabase db) {
- createDirectoriesTable(db);
- }
-
- private void upgradeToVersion403(SQLiteDatabase db) {
- db.execSQL("DROP TABLE IF EXISTS directories;");
- createDirectoriesTable(db);
-
- db.execSQL("ALTER TABLE raw_contacts"
- + " ADD raw_contact_is_read_only INTEGER NOT NULL DEFAULT 0;");
-
- db.execSQL("ALTER TABLE data"
- + " ADD is_read_only INTEGER NOT NULL DEFAULT 0;");
- }
-
- private void upgradeToVersion405(SQLiteDatabase db) {
- db.execSQL("DROP TABLE IF EXISTS phone_lookup;");
- // Private phone numbers table used for lookup
- db.execSQL("CREATE TABLE " + Tables.PHONE_LOOKUP + " (" +
- PhoneLookupColumns.DATA_ID
- + " INTEGER REFERENCES data(_id) NOT NULL," +
- PhoneLookupColumns.RAW_CONTACT_ID
- + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
- PhoneLookupColumns.NORMALIZED_NUMBER + " TEXT NOT NULL," +
- PhoneLookupColumns.MIN_MATCH + " TEXT NOT NULL" +
- ");");
-
- db.execSQL("CREATE INDEX phone_lookup_index ON " + Tables.PHONE_LOOKUP + " (" +
- PhoneLookupColumns.NORMALIZED_NUMBER + "," +
- PhoneLookupColumns.RAW_CONTACT_ID + "," +
- PhoneLookupColumns.DATA_ID +
- ");");
-
- db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" +
- PhoneLookupColumns.MIN_MATCH + "," +
- PhoneLookupColumns.RAW_CONTACT_ID + "," +
- PhoneLookupColumns.DATA_ID +
- ");");
-
- final long mimeTypeId = lookupMimeTypeId(db, Phone.CONTENT_ITEM_TYPE);
- if (mimeTypeId == -1) {
- return;
- }
-
- Cursor cursor = db.rawQuery(
- "SELECT _id, " + Phone.RAW_CONTACT_ID + ", " + Phone.NUMBER +
- " FROM " + Tables.DATA +
- " WHERE " + DataColumns.MIMETYPE_ID + "=" + mimeTypeId
- + " AND " + Phone.NUMBER + " NOT NULL", null);
-
- ContentValues phoneValues = new ContentValues();
- try {
- while (cursor.moveToNext()) {
- long dataID = cursor.getLong(0);
- long rawContactID = cursor.getLong(1);
- String number = cursor.getString(2);
- String normalizedNumber = PhoneNumberUtils.normalizeNumber(number);
- if (!TextUtils.isEmpty(normalizedNumber)) {
- phoneValues.clear();
- phoneValues.put(PhoneLookupColumns.RAW_CONTACT_ID, rawContactID);
- phoneValues.put(PhoneLookupColumns.DATA_ID, dataID);
- phoneValues.put(PhoneLookupColumns.NORMALIZED_NUMBER, normalizedNumber);
- phoneValues.put(PhoneLookupColumns.MIN_MATCH,
- PhoneNumberUtils.toCallerIDMinMatch(normalizedNumber));
- db.insert(Tables.PHONE_LOOKUP, null, phoneValues);
- }
- }
- } finally {
- cursor.close();
- }
- }
-
- private void upgradeToVersion406(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE calls ADD countryiso TEXT;");
- }
-
- private void upgradeToVersion409(SQLiteDatabase db) {
- db.execSQL("DROP TABLE IF EXISTS directories;");
- createDirectoriesTable(db);
- }
-
- /**
- * Adding DEFAULT_DIRECTORY table.
- * DEFAULT_DIRECTORY should contain every contact which should be shown to users in default.
- * - if a contact doesn't belong to any account (local contact), it should be in
- * default_directory
- * - if a contact belongs to an account that doesn't have a "default" group, it should be in
- * default_directory
- * - if a contact belongs to an account that has a "default" group (like Google directory,
- * which has "My contacts" group as default), it should be in default_directory.
- *
- * This logic assumes that accounts with the "default" group should have at least one
- * group with AUTO_ADD (implying it is the default group) flag in the groups table.
- */
- private void upgradeToVersion411(SQLiteDatabase db) {
- db.execSQL("DROP TABLE IF EXISTS " + Tables.DEFAULT_DIRECTORY);
- db.execSQL("CREATE TABLE default_directory (_id INTEGER PRIMARY KEY);");
-
- // Process contacts without an account
- db.execSQL("INSERT OR IGNORE INTO default_directory " +
- " SELECT contact_id " +
- " FROM raw_contacts " +
- " WHERE raw_contacts.account_name IS NULL " +
- " AND raw_contacts.account_type IS NULL ");
-
- // Process accounts that don't have a default group (e.g. Exchange).
- db.execSQL("INSERT OR IGNORE INTO default_directory " +
- " SELECT contact_id " +
- " FROM raw_contacts " +
- " WHERE NOT EXISTS" +
- " (SELECT _id " +
- " FROM groups " +
- " WHERE raw_contacts.account_name = groups.account_name" +
- " AND raw_contacts.account_type = groups.account_type" +
- " AND groups.auto_add != 0)");
-
- final long mimetype = lookupMimeTypeId(db, GroupMembership.CONTENT_ITEM_TYPE);
-
- // Process accounts that do have a default group (e.g. Google)
- db.execSQL("INSERT OR IGNORE INTO default_directory " +
- " SELECT contact_id " +
- " FROM raw_contacts " +
- " JOIN data " +
- " ON (raw_contacts._id=raw_contact_id)" +
- " WHERE mimetype_id=" + mimetype +
- " AND EXISTS" +
- " (SELECT _id" +
- " FROM groups" +
- " WHERE raw_contacts.account_name = groups.account_name" +
- " AND raw_contacts.account_type = groups.account_type" +
- " AND groups.auto_add != 0)");
- }
-
- private void upgradeToVersion413(SQLiteDatabase db) {
- db.execSQL("DROP TABLE IF EXISTS directories;");
- createDirectoriesTable(db);
- }
-
- private void upgradeToVersion415(SQLiteDatabase db) {
- db.execSQL(
- "ALTER TABLE " + Tables.GROUPS +
- " ADD " + Groups.GROUP_IS_READ_ONLY + " INTEGER NOT NULL DEFAULT 0");
- db.execSQL(
- "UPDATE " + Tables.GROUPS +
- " SET " + Groups.GROUP_IS_READ_ONLY + "=1" +
- " WHERE " + Groups.SYSTEM_ID + " NOT NULL");
- }
-
- private void upgradeToVersion416(SQLiteDatabase db) {
- db.execSQL("CREATE INDEX phone_lookup_data_id_min_match_index ON " + Tables.PHONE_LOOKUP +
- " (" + PhoneLookupColumns.DATA_ID + ", " + PhoneLookupColumns.MIN_MATCH + ");");
- }
-
- private void upgradeToVersion501(SQLiteDatabase db) {
- // Remove organization rows from the name lookup, we now use search index for that
- db.execSQL("DELETE FROM name_lookup WHERE name_type=5");
- }
-
- private void upgradeToVersion502(SQLiteDatabase db) {
- // Remove Chinese and Korean name lookup - this data is now in the search index
- db.execSQL("DELETE FROM name_lookup WHERE name_type IN (6, 7)");
- }
-
- private void upgradeToVersion504(SQLiteDatabase db) {
- // Find all names with prefixes and recreate display name
- Cursor cursor = db.rawQuery(
- "SELECT " + StructuredName.RAW_CONTACT_ID +
- " FROM " + Tables.DATA +
- " WHERE " + DataColumns.MIMETYPE_ID + "=?"
- + " AND " + StructuredName.PREFIX + " NOT NULL",
- new String[] {
- String.valueOf(
- mCommonMimeTypeIdsCache.get(StructuredName.CONTENT_ITEM_TYPE))
- });
-
- try {
- while(cursor.moveToNext()) {
- long rawContactId = cursor.getLong(0);
- updateRawContactDisplayName(db, rawContactId);
- }
-
- } finally {
- cursor.close();
- }
- }
-
- private void upgradeToVersion601(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE data_usage_stat(" +
- "stat_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
- "data_id INTEGER NOT NULL, " +
- "usage_type INTEGER NOT NULL DEFAULT 0, " +
- "times_used INTEGER NOT NULL DEFAULT 0, " +
- "last_time_used INTEGER NOT NULL DEFAULT 0, " +
- "FOREIGN KEY(data_id) REFERENCES data(_id));");
- db.execSQL("CREATE UNIQUE INDEX data_usage_stat_index ON " +
- "data_usage_stat (data_id, usage_type)");
- }
-
- private void upgradeToVersion602(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE calls ADD voicemail_uri TEXT;");
- db.execSQL("ALTER TABLE calls ADD _data TEXT;");
- db.execSQL("ALTER TABLE calls ADD has_content INTEGER;");
- db.execSQL("ALTER TABLE calls ADD mime_type TEXT;");
- db.execSQL("ALTER TABLE calls ADD source_data TEXT;");
- db.execSQL("ALTER TABLE calls ADD source_package TEXT;");
- db.execSQL("ALTER TABLE calls ADD state INTEGER;");
- }
-
- private void upgradeToVersion604(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE voicemail_status (" +
- "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
- "source_package TEXT UNIQUE NOT NULL," +
- "settings_uri TEXT," +
- "voicemail_access_uri TEXT," +
- "configuration_state INTEGER," +
- "data_channel_state INTEGER," +
- "notification_channel_state INTEGER" +
- ");");
- }
-
- private void upgradeToVersion606(SQLiteDatabase db) {
- db.execSQL("DROP VIEW IF EXISTS view_contacts_restricted;");
- db.execSQL("DROP VIEW IF EXISTS view_data_restricted;");
- db.execSQL("DROP VIEW IF EXISTS view_raw_contacts_restricted;");
- db.execSQL("DROP VIEW IF EXISTS view_raw_entities_restricted;");
- db.execSQL("DROP VIEW IF EXISTS view_entities_restricted;");
- db.execSQL("DROP VIEW IF EXISTS view_data_usage_stat_restricted;");
- db.execSQL("DROP INDEX IF EXISTS contacts_restricted_index");
-
- // We should remove the restricted columns here as well, but unfortunately SQLite doesn't
- // provide ALTER TABLE DROP COLUMN. As they have DEFAULT 0, we can keep but ignore them
- }
-
- private void upgradeToVersion608(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE contacts ADD photo_file_id INTEGER REFERENCES photo_files(_id);");
-
- db.execSQL("CREATE TABLE photo_files(" +
- "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
- "height INTEGER NOT NULL, " +
- "width INTEGER NOT NULL, " +
- "filesize INTEGER NOT NULL);");
- }
-
- private void upgradeToVersion610(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE calls ADD is_read INTEGER;");
- }
-
- private void upgradeToVersion611(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE raw_contacts ADD data_set TEXT DEFAULT NULL;");
- db.execSQL("ALTER TABLE groups ADD data_set TEXT DEFAULT NULL;");
- db.execSQL("ALTER TABLE accounts ADD data_set TEXT DEFAULT NULL;");
-
- db.execSQL("CREATE INDEX raw_contacts_source_id_data_set_index ON raw_contacts " +
- "(sourceid, account_type, account_name, data_set);");
-
- db.execSQL("CREATE INDEX groups_source_id_data_set_index ON groups " +
- "(sourceid, account_type, account_name, data_set);");
- }
-
- private void upgradeToVersion612(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE calls ADD geocoded_location TEXT DEFAULT NULL;");
- // Old calls will not have a geocoded location; new calls will get it when inserted.
- }
-
- private void upgradeToVersion613(SQLiteDatabase db) {
- // The stream item and stream item photos APIs were not in-use by anyone in the time
- // between their initial creation (in v609) and this update. So we're just dropping
- // and re-creating them to get appropriate columns. The delta is as follows:
- // - In stream_items, package_id was replaced by res_package.
- // - In stream_item_photos, picture was replaced by photo_file_id.
- // - Instead of resource IDs for icon and label, we use resource name strings now
- // - Added sync columns
- // - Removed action and action_uri
- // - Text and comments are now nullable
-
- db.execSQL("DROP TABLE IF EXISTS stream_items");
- db.execSQL("DROP TABLE IF EXISTS stream_item_photos");
-
- db.execSQL("CREATE TABLE stream_items(" +
- "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
- "raw_contact_id INTEGER NOT NULL, " +
- "res_package TEXT, " +
- "icon TEXT, " +
- "label TEXT, " +
- "text TEXT, " +
- "timestamp INTEGER NOT NULL, " +
- "comments TEXT, " +
- "stream_item_sync1 TEXT, " +
- "stream_item_sync2 TEXT, " +
- "stream_item_sync3 TEXT, " +
- "stream_item_sync4 TEXT, " +
- "FOREIGN KEY(raw_contact_id) REFERENCES raw_contacts(_id));");
-
- db.execSQL("CREATE TABLE stream_item_photos(" +
- "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
- "stream_item_id INTEGER NOT NULL, " +
- "sort_index INTEGER, " +
- "photo_file_id INTEGER NOT NULL, " +
- "stream_item_photo_sync1 TEXT, " +
- "stream_item_photo_sync2 TEXT, " +
- "stream_item_photo_sync3 TEXT, " +
- "stream_item_photo_sync4 TEXT, " +
- "FOREIGN KEY(stream_item_id) REFERENCES stream_items(_id));");
- }
-
- private void upgradeToVersion615(SQLiteDatabase db) {
- // Old calls will not have up to date values for these columns, they will be filled in
- // as needed.
- db.execSQL("ALTER TABLE calls ADD lookup_uri TEXT DEFAULT NULL;");
- db.execSQL("ALTER TABLE calls ADD matched_number TEXT DEFAULT NULL;");
- db.execSQL("ALTER TABLE calls ADD normalized_number TEXT DEFAULT NULL;");
- db.execSQL("ALTER TABLE calls ADD photo_id INTEGER NOT NULL DEFAULT 0;");
- }
-
- private void upgradeToVersion618(SQLiteDatabase db) {
- // The Settings table needs a data_set column which technically should be part of the
- // primary key but can't be because it may be null. Since SQLite doesn't support nuking
- // the primary key, we'll drop the old table, re-create it, and copy the settings back in.
- db.execSQL("CREATE TEMPORARY TABLE settings_backup(" +
- "account_name STRING NOT NULL," +
- "account_type STRING NOT NULL," +
- "ungrouped_visible INTEGER NOT NULL DEFAULT 0," +
- "should_sync INTEGER NOT NULL DEFAULT 1" +
- ");");
- db.execSQL("INSERT INTO settings_backup " +
- "SELECT account_name, account_type, ungrouped_visible, should_sync" +
- " FROM settings");
- db.execSQL("DROP TABLE settings");
- db.execSQL("CREATE TABLE settings (" +
- "account_name STRING NOT NULL," +
- "account_type STRING NOT NULL," +
- "data_set STRING," +
- "ungrouped_visible INTEGER NOT NULL DEFAULT 0," +
- "should_sync INTEGER NOT NULL DEFAULT 1" +
- ");");
- db.execSQL("INSERT INTO settings " +
- "SELECT account_name, account_type, NULL, ungrouped_visible, should_sync " +
- "FROM settings_backup");
- db.execSQL("DROP TABLE settings_backup");
- }
-
- private void upgradeToVersion622(SQLiteDatabase db) {
- db.execSQL("ALTER TABLE calls ADD formatted_number TEXT DEFAULT NULL;");
- }
-
- private void upgradeToVersion626(SQLiteDatabase db) {
- db.execSQL("DROP TABLE IF EXISTS accounts");
-
- db.execSQL("CREATE TABLE accounts (" +
- "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
- "account_name TEXT, " +
- "account_type TEXT, " +
- "data_set TEXT" +
- ");");
-
- // Add "account_id" column to groups and raw_contacts
- db.execSQL("ALTER TABLE raw_contacts ADD " +
- "account_id INTEGER REFERENCES accounts(_id)");
- db.execSQL("ALTER TABLE groups ADD " +
- "account_id INTEGER REFERENCES accounts(_id)");
-
- // Update indexes.
- db.execSQL("DROP INDEX IF EXISTS raw_contacts_source_id_index");
- db.execSQL("DROP INDEX IF EXISTS raw_contacts_source_id_data_set_index");
- db.execSQL("DROP INDEX IF EXISTS groups_source_id_index");
- db.execSQL("DROP INDEX IF EXISTS groups_source_id_data_set_index");
-
- db.execSQL("CREATE INDEX raw_contacts_source_id_account_id_index ON raw_contacts ("
- + "sourceid, account_id);");
- db.execSQL("CREATE INDEX groups_source_id_account_id_index ON groups ("
- + "sourceid, account_id);");
-
- // Migrate account_name/account_type/data_set to accounts table
-
- final ArraySet<AccountWithDataSet> accountsWithDataSets = new ArraySet<>();
- upgradeToVersion626_findAccountsWithDataSets(accountsWithDataSets, db, "raw_contacts");
- upgradeToVersion626_findAccountsWithDataSets(accountsWithDataSets, db, "groups");
-
- for (AccountWithDataSet accountWithDataSet : accountsWithDataSets) {
- db.execSQL("INSERT INTO accounts (account_name,account_type,data_set)VALUES(?, ?, ?)",
- new String[] {
- accountWithDataSet.getAccountName(),
- accountWithDataSet.getAccountType(),
- accountWithDataSet.getDataSet()
- });
- }
- upgradeToVersion626_fillAccountId(db, "raw_contacts");
- upgradeToVersion626_fillAccountId(db, "groups");
- }
-
- private static void upgradeToVersion626_findAccountsWithDataSets(
- Set<AccountWithDataSet> result, SQLiteDatabase db, String table) {
- Cursor c = db.rawQuery(
- "SELECT DISTINCT account_name, account_type, data_set FROM " + table, null);
- try {
- while (c.moveToNext()) {
- result.add(AccountWithDataSet.get(c.getString(0), c.getString(1), c.getString(2)));
- }
- } finally {
- c.close();
- }
- }
-
- private static void upgradeToVersion626_fillAccountId(SQLiteDatabase db, String table) {
- StringBuilder sb = new StringBuilder();
-
- // Set account_id and null out account_name, account_type and data_set
-
- sb.append("UPDATE " + table + " SET account_id = (SELECT _id FROM accounts WHERE ");
-
- addJoinExpressionAllowingNull(sb, table + ".account_name", "accounts.account_name");
- sb.append("AND");
- addJoinExpressionAllowingNull(sb, table + ".account_type", "accounts.account_type");
- sb.append("AND");
- addJoinExpressionAllowingNull(sb, table + ".data_set", "accounts.data_set");
-
- sb.append("), account_name = null, account_type = null, data_set = null");
- db.execSQL(sb.toString());
- }
-
private void upgradeToVersion701(SQLiteDatabase db) {
db.execSQL("UPDATE raw_contacts SET last_time_contacted =" +
" max(ifnull(last_time_contacted, 0), " +
@@ -4744,18 +3337,6 @@
}
/**
- * Add a string like "(((column1) = (column2)) OR ((column1) IS NULL AND (column2) IS NULL))"
- */
- private static StringBuilder addJoinExpressionAllowingNull(
- StringBuilder sb, String column1, String column2) {
-
- sb.append("(((").append(column1).append(")=(").append(column2);
- sb.append("))OR((");
- sb.append(column1).append(") IS NULL AND (").append(column2).append(") IS NULL))");
- return sb;
- }
-
- /**
* Adds index stats into the SQLite database to force it to always use the lookup indexes.
*
* Note if you drop a table or an index, the corresponding row will be removed from this table.