ContactsDatabaseHelper.java revision 23ba865a6d204ba4aa29d2fad9989e9c44351e81
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17package com.android.providers.contacts; 18 19import com.android.common.content.SyncStateContentProviderHelper; 20 21import android.content.ContentResolver; 22import android.content.ContentValues; 23import android.content.Context; 24import android.content.pm.ApplicationInfo; 25import android.content.pm.PackageManager; 26import android.content.pm.PackageManager.NameNotFoundException; 27import android.content.res.Resources; 28import android.database.CharArrayBuffer; 29import android.database.Cursor; 30import android.database.DatabaseUtils; 31import android.database.SQLException; 32import android.database.sqlite.SQLiteConstraintException; 33import android.database.sqlite.SQLiteDatabase; 34import android.database.sqlite.SQLiteDoneException; 35import android.database.sqlite.SQLiteException; 36import android.database.sqlite.SQLiteOpenHelper; 37import android.database.sqlite.SQLiteQueryBuilder; 38import android.database.sqlite.SQLiteStatement; 39import android.location.CountryDetector; 40import android.net.Uri; 41import android.os.Binder; 42import android.os.Bundle; 43import android.os.SystemClock; 44import android.provider.BaseColumns; 45import android.provider.CallLog.Calls; 46import android.provider.ContactsContract; 47import android.provider.ContactsContract.AggregationExceptions; 48import android.provider.ContactsContract.CommonDataKinds.Email; 49import android.provider.ContactsContract.CommonDataKinds.GroupMembership; 50import android.provider.ContactsContract.CommonDataKinds.Im; 51import android.provider.ContactsContract.CommonDataKinds.Nickname; 52import android.provider.ContactsContract.CommonDataKinds.Organization; 53import android.provider.ContactsContract.CommonDataKinds.Phone; 54import android.provider.ContactsContract.CommonDataKinds.SipAddress; 55import android.provider.ContactsContract.CommonDataKinds.StructuredName; 56import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; 57import android.provider.ContactsContract.Contacts; 58import android.provider.ContactsContract.Contacts.Photo; 59import android.provider.ContactsContract.Data; 60import android.provider.ContactsContract.Directory; 61import android.provider.ContactsContract.DisplayNameSources; 62import android.provider.ContactsContract.DisplayPhoto; 63import android.provider.ContactsContract.FullNameStyle; 64import android.provider.ContactsContract.Groups; 65import android.provider.ContactsContract.PhoneticNameStyle; 66import android.provider.ContactsContract.PhotoFiles; 67import android.provider.ContactsContract.RawContacts; 68import android.provider.ContactsContract.Settings; 69import android.provider.ContactsContract.StatusUpdates; 70import android.provider.ContactsContract.StreamItemPhotos; 71import android.provider.ContactsContract.StreamItems; 72import android.provider.SocialContract.Activities; 73import android.provider.VoicemailContract; 74import android.provider.VoicemailContract.Voicemails; 75import android.telephony.PhoneNumberUtils; 76import android.text.TextUtils; 77import android.text.util.Rfc822Token; 78import android.text.util.Rfc822Tokenizer; 79import android.util.Log; 80 81import java.util.HashMap; 82import java.util.Locale; 83 84/** 85 * Database helper for contacts. Designed as a singleton to make sure that all 86 * {@link android.content.ContentProvider} users get the same reference. 87 * Provides handy methods for maintaining package and mime-type lookup tables. 88 */ 89/* package */ class ContactsDatabaseHelper extends SQLiteOpenHelper { 90 private static final String TAG = "ContactsDatabaseHelper"; 91 92 /** 93 * Contacts DB version ranges: 94 * <pre> 95 * 0-98 Cupcake/Donut 96 * 100-199 Eclair 97 * 200-299 Eclair-MR1 98 * 300-349 Froyo 99 * 350-399 Gingerbread 100 * 400-499 Honeycomb 101 * 500-549 Honeycomb-MR1 102 * 550-599 Honeycomb-MR2 103 * 600-699 Ice Cream Sandwich 104 * </pre> 105 */ 106 static final int DATABASE_VERSION = 623; 107 108 private static final String DATABASE_NAME = "contacts2.db"; 109 private static final String DATABASE_PRESENCE = "presence_db"; 110 111 public interface Tables { 112 public static final String CONTACTS = "contacts"; 113 public static final String RAW_CONTACTS = "raw_contacts"; 114 public static final String STREAM_ITEMS = "stream_items"; 115 public static final String STREAM_ITEM_PHOTOS = "stream_item_photos"; 116 public static final String PHOTO_FILES = "photo_files"; 117 public static final String PACKAGES = "packages"; 118 public static final String MIMETYPES = "mimetypes"; 119 public static final String PHONE_LOOKUP = "phone_lookup"; 120 public static final String NAME_LOOKUP = "name_lookup"; 121 public static final String AGGREGATION_EXCEPTIONS = "agg_exceptions"; 122 public static final String SETTINGS = "settings"; 123 public static final String DATA = "data"; 124 public static final String GROUPS = "groups"; 125 public static final String PRESENCE = "presence"; 126 public static final String AGGREGATED_PRESENCE = "agg_presence"; 127 public static final String NICKNAME_LOOKUP = "nickname_lookup"; 128 public static final String CALLS = "calls"; 129 public static final String STATUS_UPDATES = "status_updates"; 130 public static final String PROPERTIES = "properties"; 131 public static final String ACCOUNTS = "accounts"; 132 public static final String VISIBLE_CONTACTS = "visible_contacts"; 133 public static final String DIRECTORIES = "directories"; 134 public static final String DEFAULT_DIRECTORY = "default_directory"; 135 public static final String SEARCH_INDEX = "search_index"; 136 public static final String VOICEMAIL_STATUS = "voicemail_status"; 137 138 // This list of tables contains auto-incremented sequences. 139 public static final String[] SEQUENCE_TABLES = new String[] { 140 CONTACTS, 141 RAW_CONTACTS, 142 STREAM_ITEMS, 143 STREAM_ITEM_PHOTOS, 144 PHOTO_FILES, 145 DATA, 146 GROUPS, 147 CALLS, 148 DIRECTORIES 149 }; 150 151 /** 152 * For {@link ContactsContract.DataUsageFeedback}. The table structure itself 153 * is not exposed outside. 154 */ 155 public static final String DATA_USAGE_STAT = "data_usage_stat"; 156 157 public static final String DATA_JOIN_MIMETYPES = "data " 158 + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id)"; 159 160 public static final String DATA_JOIN_RAW_CONTACTS = "data " 161 + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)"; 162 163 public static final String DATA_JOIN_MIMETYPE_RAW_CONTACTS = "data " 164 + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) " 165 + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)"; 166 167 // NOTE: This requires late binding of GroupMembership MIME-type 168 public static final String RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS = "raw_contacts " 169 + "LEFT OUTER JOIN settings ON (" 170 + "raw_contacts.account_name = settings.account_name AND " 171 + "raw_contacts.account_type = settings.account_type AND " 172 + "((raw_contacts.data_set IS NULL AND settings.data_set IS NULL) " 173 + "OR (raw_contacts.data_set = settings.data_set))) " 174 + "LEFT OUTER JOIN data ON (data.mimetype_id=? AND " 175 + "data.raw_contact_id = raw_contacts._id) " 176 + "LEFT OUTER JOIN groups ON (groups._id = data." + GroupMembership.GROUP_ROW_ID 177 + ")"; 178 179 // NOTE: This requires late binding of GroupMembership MIME-type 180 public static final String SETTINGS_JOIN_RAW_CONTACTS_DATA_MIMETYPES_CONTACTS = "settings " 181 + "LEFT OUTER JOIN raw_contacts ON (" 182 + "raw_contacts.account_name = settings.account_name AND " 183 + "raw_contacts.account_type = settings.account_type) " 184 + "LEFT OUTER JOIN data ON (data.mimetype_id=? AND " 185 + "data.raw_contact_id = raw_contacts._id) " 186 + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)"; 187 188 public static final String CONTACTS_JOIN_RAW_CONTACTS_DATA_FILTERED_BY_GROUPMEMBERSHIP = 189 Tables.CONTACTS 190 + " INNER JOIN " + Tables.RAW_CONTACTS 191 + " ON (" + RawContactsColumns.CONCRETE_CONTACT_ID + "=" 192 + ContactsColumns.CONCRETE_ID 193 + ")" 194 + " INNER JOIN " + Tables.DATA 195 + " ON (" + DataColumns.CONCRETE_DATA1 + "=" + GroupsColumns.CONCRETE_ID 196 + " AND " 197 + DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID 198 + " AND " 199 + DataColumns.CONCRETE_MIMETYPE_ID + "=" 200 + "(SELECT " + MimetypesColumns._ID 201 + " FROM " + Tables.MIMETYPES 202 + " WHERE " 203 + MimetypesColumns.CONCRETE_MIMETYPE + "=" 204 + "'" + GroupMembership.CONTENT_ITEM_TYPE + "'" 205 + ")" 206 + ")"; 207 208 public static final String DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_GROUPS = "data " 209 + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) " 210 + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) " 211 + "LEFT OUTER JOIN packages ON (data.package_id = packages._id) " 212 + "LEFT OUTER JOIN groups " 213 + " ON (mimetypes.mimetype='" + GroupMembership.CONTENT_ITEM_TYPE + "' " 214 + " AND groups._id = data." + GroupMembership.GROUP_ROW_ID + ") "; 215 216 public static final String GROUPS_JOIN_PACKAGES = "groups " 217 + "LEFT OUTER JOIN packages ON (groups.package_id = packages._id)"; 218 219 220 public static final String ACTIVITIES = "activities"; 221 222 public static final String ACTIVITIES_JOIN_MIMETYPES = "activities " 223 + "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id)"; 224 225 public static final String ACTIVITIES_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_CONTACTS = 226 "activities " 227 + "LEFT OUTER JOIN packages ON (activities.package_id = packages._id) " 228 + "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id) " 229 + "LEFT OUTER JOIN raw_contacts ON (activities.author_contact_id = " + 230 "raw_contacts._id) " 231 + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)"; 232 233 public static final String NAME_LOOKUP_JOIN_RAW_CONTACTS = "name_lookup " 234 + "INNER JOIN view_raw_contacts ON (name_lookup.raw_contact_id = " 235 + "view_raw_contacts._id)"; 236 } 237 238 public interface Joins { 239 /** 240 * Join string intended to be used with the GROUPS table/view. The main table must be named 241 * as "groups". 242 * 243 * Adds the "group_member_count column" to the query, which will be null if a group has 244 * no members. Use ifnull(group_member_count, 0) if 0 is needed instead. 245 */ 246 public static final String GROUP_MEMBER_COUNT = 247 " LEFT OUTER JOIN (SELECT " 248 + "data.data1 AS member_count_group_id, " 249 + "COUNT(data.raw_contact_id) AS group_member_count " 250 + "FROM data " 251 + "WHERE " 252 + "data.mimetype_id = (SELECT _id FROM mimetypes WHERE " 253 + "mimetypes.mimetype = '" + GroupMembership.CONTENT_ITEM_TYPE + "')" 254 + "GROUP BY member_count_group_id) AS member_count_table" // End of inner query 255 + " ON (groups._id = member_count_table.member_count_group_id)"; 256 } 257 258 public interface Views { 259 public static final String DATA = "view_data"; 260 public static final String RAW_CONTACTS = "view_raw_contacts"; 261 public static final String CONTACTS = "view_contacts"; 262 public static final String ENTITIES = "view_entities"; 263 public static final String RAW_ENTITIES = "view_raw_entities"; 264 public static final String GROUPS = "view_groups"; 265 public static final String DATA_USAGE_STAT = "view_data_usage_stat"; 266 public static final String STREAM_ITEMS = "view_stream_items"; 267 } 268 269 public interface Clauses { 270 final String HAVING_NO_GROUPS = "COUNT(" + DataColumns.CONCRETE_GROUP_ID + ") == 0"; 271 272 final String GROUP_BY_ACCOUNT_CONTACT_ID = SettingsColumns.CONCRETE_ACCOUNT_NAME + "," 273 + SettingsColumns.CONCRETE_ACCOUNT_TYPE + "," + RawContacts.CONTACT_ID; 274 275 final String RAW_CONTACT_IS_LOCAL = RawContactsColumns.CONCRETE_ACCOUNT_NAME 276 + " IS NULL AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " IS NULL AND " 277 + RawContactsColumns.CONCRETE_DATA_SET + " IS NULL"; 278 279 final String ZERO_GROUP_MEMBERSHIPS = "COUNT(" + GroupsColumns.CONCRETE_ID + ")=0"; 280 281 final String OUTER_RAW_CONTACTS = "outer_raw_contacts"; 282 final String OUTER_RAW_CONTACTS_ID = OUTER_RAW_CONTACTS + "." + RawContacts._ID; 283 284 final String CONTACT_IS_VISIBLE = 285 "SELECT " + 286 "MAX((SELECT (CASE WHEN " + 287 "(CASE" + 288 " WHEN " + RAW_CONTACT_IS_LOCAL + 289 " THEN 1 " + 290 " WHEN " + ZERO_GROUP_MEMBERSHIPS + 291 " THEN " + Settings.UNGROUPED_VISIBLE + 292 " ELSE MAX(" + Groups.GROUP_VISIBLE + ")" + 293 "END)=1 THEN 1 ELSE 0 END)" + 294 " FROM " + Tables.RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS + 295 " WHERE " + RawContactsColumns.CONCRETE_ID + "=" + OUTER_RAW_CONTACTS_ID + "))" + 296 " FROM " + Tables.RAW_CONTACTS + " AS " + OUTER_RAW_CONTACTS + 297 " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + 298 " GROUP BY " + RawContacts.CONTACT_ID; 299 300 final String GROUP_HAS_ACCOUNT_AND_SOURCE_ID = Groups.SOURCE_ID + "=? AND " 301 + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=? AND " 302 + Groups.DATA_SET + " IS NULL"; 303 304 final String GROUP_HAS_ACCOUNT_AND_DATA_SET_AND_SOURCE_ID = Groups.SOURCE_ID + "=? AND " 305 + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=? AND " 306 + Groups.DATA_SET + "=?"; 307 308 public static final String CONTACT_VISIBLE = 309 "EXISTS (SELECT _id FROM " + Tables.VISIBLE_CONTACTS 310 + " WHERE " + Tables.CONTACTS +"." + Contacts._ID 311 + "=" + Tables.VISIBLE_CONTACTS +"." + Contacts._ID + ")"; 312 } 313 314 public interface ContactsColumns { 315 public static final String LAST_STATUS_UPDATE_ID = "status_update_id"; 316 317 public static final String CONCRETE_ID = Tables.CONTACTS + "." + BaseColumns._ID; 318 319 public static final String CONCRETE_PHOTO_FILE_ID = Tables.CONTACTS + "." 320 + Contacts.PHOTO_FILE_ID; 321 public static final String CONCRETE_TIMES_CONTACTED = Tables.CONTACTS + "." 322 + Contacts.TIMES_CONTACTED; 323 public static final String CONCRETE_LAST_TIME_CONTACTED = Tables.CONTACTS + "." 324 + Contacts.LAST_TIME_CONTACTED; 325 public static final String CONCRETE_STARRED = Tables.CONTACTS + "." + Contacts.STARRED; 326 public static final String CONCRETE_CUSTOM_RINGTONE = Tables.CONTACTS + "." 327 + Contacts.CUSTOM_RINGTONE; 328 public static final String CONCRETE_SEND_TO_VOICEMAIL = Tables.CONTACTS + "." 329 + Contacts.SEND_TO_VOICEMAIL; 330 public static final String CONCRETE_LOOKUP_KEY = Tables.CONTACTS + "." 331 + Contacts.LOOKUP_KEY; 332 } 333 334 public interface RawContactsColumns { 335 public static final String CONCRETE_ID = 336 Tables.RAW_CONTACTS + "." + BaseColumns._ID; 337 public static final String CONCRETE_ACCOUNT_NAME = 338 Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_NAME; 339 public static final String CONCRETE_ACCOUNT_TYPE = 340 Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_TYPE; 341 public static final String CONCRETE_DATA_SET = 342 Tables.RAW_CONTACTS + "." + RawContacts.DATA_SET; 343 public static final String CONCRETE_ACCOUNT_TYPE_AND_DATA_SET = 344 Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_TYPE_AND_DATA_SET; 345 public static final String CONCRETE_SOURCE_ID = 346 Tables.RAW_CONTACTS + "." + RawContacts.SOURCE_ID; 347 public static final String CONCRETE_VERSION = 348 Tables.RAW_CONTACTS + "." + RawContacts.VERSION; 349 public static final String CONCRETE_DIRTY = 350 Tables.RAW_CONTACTS + "." + RawContacts.DIRTY; 351 public static final String CONCRETE_DELETED = 352 Tables.RAW_CONTACTS + "." + RawContacts.DELETED; 353 public static final String CONCRETE_SYNC1 = 354 Tables.RAW_CONTACTS + "." + RawContacts.SYNC1; 355 public static final String CONCRETE_SYNC2 = 356 Tables.RAW_CONTACTS + "." + RawContacts.SYNC2; 357 public static final String CONCRETE_SYNC3 = 358 Tables.RAW_CONTACTS + "." + RawContacts.SYNC3; 359 public static final String CONCRETE_SYNC4 = 360 Tables.RAW_CONTACTS + "." + RawContacts.SYNC4; 361 public static final String CONCRETE_CUSTOM_RINGTONE = 362 Tables.RAW_CONTACTS + "." + RawContacts.CUSTOM_RINGTONE; 363 public static final String CONCRETE_SEND_TO_VOICEMAIL = 364 Tables.RAW_CONTACTS + "." + RawContacts.SEND_TO_VOICEMAIL; 365 public static final String CONCRETE_LAST_TIME_CONTACTED = 366 Tables.RAW_CONTACTS + "." + RawContacts.LAST_TIME_CONTACTED; 367 public static final String CONCRETE_TIMES_CONTACTED = 368 Tables.RAW_CONTACTS + "." + RawContacts.TIMES_CONTACTED; 369 public static final String CONCRETE_STARRED = 370 Tables.RAW_CONTACTS + "." + RawContacts.STARRED; 371 372 public static final String DISPLAY_NAME = RawContacts.DISPLAY_NAME_PRIMARY; 373 public static final String DISPLAY_NAME_SOURCE = RawContacts.DISPLAY_NAME_SOURCE; 374 public static final String AGGREGATION_NEEDED = "aggregation_needed"; 375 376 public static final String CONCRETE_DISPLAY_NAME = 377 Tables.RAW_CONTACTS + "." + DISPLAY_NAME; 378 public static final String CONCRETE_CONTACT_ID = 379 Tables.RAW_CONTACTS + "." + RawContacts.CONTACT_ID; 380 public static final String CONCRETE_NAME_VERIFIED = 381 Tables.RAW_CONTACTS + "." + RawContacts.NAME_VERIFIED; 382 } 383 384 public interface DataColumns { 385 public static final String PACKAGE_ID = "package_id"; 386 public static final String MIMETYPE_ID = "mimetype_id"; 387 388 public static final String CONCRETE_ID = Tables.DATA + "." + BaseColumns._ID; 389 public static final String CONCRETE_MIMETYPE_ID = Tables.DATA + "." + MIMETYPE_ID; 390 public static final String CONCRETE_RAW_CONTACT_ID = Tables.DATA + "." 391 + Data.RAW_CONTACT_ID; 392 public static final String CONCRETE_GROUP_ID = Tables.DATA + "." 393 + GroupMembership.GROUP_ROW_ID; 394 395 public static final String CONCRETE_DATA1 = Tables.DATA + "." + Data.DATA1; 396 public static final String CONCRETE_DATA2 = Tables.DATA + "." + Data.DATA2; 397 public static final String CONCRETE_DATA3 = Tables.DATA + "." + Data.DATA3; 398 public static final String CONCRETE_DATA4 = Tables.DATA + "." + Data.DATA4; 399 public static final String CONCRETE_DATA5 = Tables.DATA + "." + Data.DATA5; 400 public static final String CONCRETE_DATA6 = Tables.DATA + "." + Data.DATA6; 401 public static final String CONCRETE_DATA7 = Tables.DATA + "." + Data.DATA7; 402 public static final String CONCRETE_DATA8 = Tables.DATA + "." + Data.DATA8; 403 public static final String CONCRETE_DATA9 = Tables.DATA + "." + Data.DATA9; 404 public static final String CONCRETE_DATA10 = Tables.DATA + "." + Data.DATA10; 405 public static final String CONCRETE_DATA11 = Tables.DATA + "." + Data.DATA11; 406 public static final String CONCRETE_DATA12 = Tables.DATA + "." + Data.DATA12; 407 public static final String CONCRETE_DATA13 = Tables.DATA + "." + Data.DATA13; 408 public static final String CONCRETE_DATA14 = Tables.DATA + "." + Data.DATA14; 409 public static final String CONCRETE_DATA15 = Tables.DATA + "." + Data.DATA15; 410 public static final String CONCRETE_IS_PRIMARY = Tables.DATA + "." + Data.IS_PRIMARY; 411 public static final String CONCRETE_PACKAGE_ID = Tables.DATA + "." + PACKAGE_ID; 412 } 413 414 // Used only for legacy API support 415 public interface ExtensionsColumns { 416 public static final String NAME = Data.DATA1; 417 public static final String VALUE = Data.DATA2; 418 } 419 420 public interface GroupMembershipColumns { 421 public static final String RAW_CONTACT_ID = Data.RAW_CONTACT_ID; 422 public static final String GROUP_ROW_ID = GroupMembership.GROUP_ROW_ID; 423 } 424 425 public interface PhoneColumns { 426 public static final String NORMALIZED_NUMBER = Data.DATA4; 427 public static final String CONCRETE_NORMALIZED_NUMBER = DataColumns.CONCRETE_DATA4; 428 } 429 430 public interface GroupsColumns { 431 public static final String PACKAGE_ID = "package_id"; 432 433 public static final String CONCRETE_ID = Tables.GROUPS + "." + BaseColumns._ID; 434 public static final String CONCRETE_SOURCE_ID = Tables.GROUPS + "." + Groups.SOURCE_ID; 435 public static final String CONCRETE_ACCOUNT_NAME = 436 Tables.GROUPS + "." + Groups.ACCOUNT_NAME; 437 public static final String CONCRETE_ACCOUNT_TYPE = 438 Tables.GROUPS + "." + Groups.ACCOUNT_TYPE; 439 public static final String CONCRETE_DATA_SET = Tables.GROUPS + "." + Groups.DATA_SET; 440 public static final String CONCRETE_ACCOUNT_TYPE_AND_DATA_SET = Tables.GROUPS + "." + 441 Groups.ACCOUNT_TYPE_AND_DATA_SET; 442 } 443 444 public interface ActivitiesColumns { 445 public static final String PACKAGE_ID = "package_id"; 446 public static final String MIMETYPE_ID = "mimetype_id"; 447 } 448 449 public interface PhoneLookupColumns { 450 public static final String _ID = BaseColumns._ID; 451 public static final String DATA_ID = "data_id"; 452 public static final String RAW_CONTACT_ID = "raw_contact_id"; 453 public static final String NORMALIZED_NUMBER = "normalized_number"; 454 public static final String MIN_MATCH = "min_match"; 455 } 456 457 public interface NameLookupColumns { 458 public static final String RAW_CONTACT_ID = "raw_contact_id"; 459 public static final String DATA_ID = "data_id"; 460 public static final String NORMALIZED_NAME = "normalized_name"; 461 public static final String NAME_TYPE = "name_type"; 462 } 463 464 public final static class NameLookupType { 465 public static final int NAME_EXACT = 0; 466 public static final int NAME_VARIANT = 1; 467 public static final int NAME_COLLATION_KEY = 2; 468 public static final int NICKNAME = 3; 469 public static final int EMAIL_BASED_NICKNAME = 4; 470 471 // This is the highest name lookup type code plus one 472 public static final int TYPE_COUNT = 5; 473 474 public static boolean isBasedOnStructuredName(int nameLookupType) { 475 return nameLookupType == NameLookupType.NAME_EXACT 476 || nameLookupType == NameLookupType.NAME_VARIANT 477 || nameLookupType == NameLookupType.NAME_COLLATION_KEY; 478 } 479 } 480 481 public interface PackagesColumns { 482 public static final String _ID = BaseColumns._ID; 483 public static final String PACKAGE = "package"; 484 485 public static final String CONCRETE_ID = Tables.PACKAGES + "." + _ID; 486 } 487 488 public interface MimetypesColumns { 489 public static final String _ID = BaseColumns._ID; 490 public static final String MIMETYPE = "mimetype"; 491 492 public static final String CONCRETE_ID = Tables.MIMETYPES + "." + BaseColumns._ID; 493 public static final String CONCRETE_MIMETYPE = Tables.MIMETYPES + "." + MIMETYPE; 494 } 495 496 public interface AggregationExceptionColumns { 497 public static final String _ID = BaseColumns._ID; 498 } 499 500 public interface NicknameLookupColumns { 501 public static final String NAME = "name"; 502 public static final String CLUSTER = "cluster"; 503 } 504 505 public interface SettingsColumns { 506 public static final String CONCRETE_ACCOUNT_NAME = Tables.SETTINGS + "." 507 + Settings.ACCOUNT_NAME; 508 public static final String CONCRETE_ACCOUNT_TYPE = Tables.SETTINGS + "." 509 + Settings.ACCOUNT_TYPE; 510 public static final String CONCRETE_DATA_SET = Tables.SETTINGS + "." 511 + Settings.DATA_SET; 512 } 513 514 public interface PresenceColumns { 515 String RAW_CONTACT_ID = "presence_raw_contact_id"; 516 String CONTACT_ID = "presence_contact_id"; 517 } 518 519 public interface AggregatedPresenceColumns { 520 String CONTACT_ID = "presence_contact_id"; 521 522 String CONCRETE_CONTACT_ID = Tables.AGGREGATED_PRESENCE + "." + CONTACT_ID; 523 } 524 525 public interface StatusUpdatesColumns { 526 String DATA_ID = "status_update_data_id"; 527 528 String CONCRETE_DATA_ID = Tables.STATUS_UPDATES + "." + DATA_ID; 529 530 String CONCRETE_PRESENCE = Tables.STATUS_UPDATES + "." + StatusUpdates.PRESENCE; 531 String CONCRETE_STATUS = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS; 532 String CONCRETE_STATUS_TIMESTAMP = Tables.STATUS_UPDATES + "." 533 + StatusUpdates.STATUS_TIMESTAMP; 534 String CONCRETE_STATUS_RES_PACKAGE = Tables.STATUS_UPDATES + "." 535 + StatusUpdates.STATUS_RES_PACKAGE; 536 String CONCRETE_STATUS_LABEL = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS_LABEL; 537 String CONCRETE_STATUS_ICON = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS_ICON; 538 } 539 540 public interface ContactsStatusUpdatesColumns { 541 String ALIAS = "contacts_" + Tables.STATUS_UPDATES; 542 543 String CONCRETE_DATA_ID = ALIAS + "." + StatusUpdatesColumns.DATA_ID; 544 545 String CONCRETE_PRESENCE = ALIAS + "." + StatusUpdates.PRESENCE; 546 String CONCRETE_STATUS = ALIAS + "." + StatusUpdates.STATUS; 547 String CONCRETE_STATUS_TIMESTAMP = ALIAS + "." + StatusUpdates.STATUS_TIMESTAMP; 548 String CONCRETE_STATUS_RES_PACKAGE = ALIAS + "." + StatusUpdates.STATUS_RES_PACKAGE; 549 String CONCRETE_STATUS_LABEL = ALIAS + "." + StatusUpdates.STATUS_LABEL; 550 String CONCRETE_STATUS_ICON = ALIAS + "." + StatusUpdates.STATUS_ICON; 551 } 552 553 public interface StreamItemsColumns { 554 final String CONCRETE_ID = Tables.STREAM_ITEMS + "." + BaseColumns._ID; 555 final String CONCRETE_RAW_CONTACT_ID = 556 Tables.STREAM_ITEMS + "." + StreamItems.RAW_CONTACT_ID; 557 final String CONCRETE_PACKAGE = Tables.STREAM_ITEMS + "." + StreamItems.RES_PACKAGE; 558 final String CONCRETE_ICON = Tables.STREAM_ITEMS + "." + StreamItems.RES_ICON; 559 final String CONCRETE_LABEL = Tables.STREAM_ITEMS + "." + StreamItems.RES_LABEL; 560 final String CONCRETE_TEXT = Tables.STREAM_ITEMS + "." + StreamItems.TEXT; 561 final String CONCRETE_TIMESTAMP = Tables.STREAM_ITEMS + "." + StreamItems.TIMESTAMP; 562 final String CONCRETE_COMMENTS = Tables.STREAM_ITEMS + "." + StreamItems.COMMENTS; 563 final String CONCRETE_SYNC1 = Tables.STREAM_ITEMS + "." + StreamItems.SYNC1; 564 final String CONCRETE_SYNC2 = Tables.STREAM_ITEMS + "." + StreamItems.SYNC2; 565 final String CONCRETE_SYNC3 = Tables.STREAM_ITEMS + "." + StreamItems.SYNC3; 566 final String CONCRETE_SYNC4 = Tables.STREAM_ITEMS + "." + StreamItems.SYNC4; 567 } 568 569 public interface StreamItemPhotosColumns { 570 final String CONCRETE_ID = Tables.STREAM_ITEM_PHOTOS + "." + BaseColumns._ID; 571 final String CONCRETE_STREAM_ITEM_ID = Tables.STREAM_ITEM_PHOTOS + "." 572 + StreamItemPhotos.STREAM_ITEM_ID; 573 final String CONCRETE_SORT_INDEX = 574 Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.SORT_INDEX; 575 final String CONCRETE_PHOTO_FILE_ID = Tables.STREAM_ITEM_PHOTOS + "." 576 + StreamItemPhotos.PHOTO_FILE_ID; 577 final String CONCRETE_SYNC1 = Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.SYNC1; 578 final String CONCRETE_SYNC2 = Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.SYNC2; 579 final String CONCRETE_SYNC3 = Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.SYNC3; 580 final String CONCRETE_SYNC4 = Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.SYNC4; 581 } 582 583 public interface PhotoFilesColumns { 584 String CONCRETE_ID = Tables.PHOTO_FILES + "." + BaseColumns._ID; 585 String CONCRETE_HEIGHT = Tables.PHOTO_FILES + "." + PhotoFiles.HEIGHT; 586 String CONCRETE_WIDTH = Tables.PHOTO_FILES + "." + PhotoFiles.WIDTH; 587 String CONCRETE_FILESIZE = Tables.PHOTO_FILES + "." + PhotoFiles.FILESIZE; 588 } 589 590 public interface PropertiesColumns { 591 String PROPERTY_KEY = "property_key"; 592 String PROPERTY_VALUE = "property_value"; 593 } 594 595 public interface AccountsColumns { 596 String ACCOUNT_NAME = RawContacts.ACCOUNT_NAME; 597 String ACCOUNT_TYPE = RawContacts.ACCOUNT_TYPE; 598 String DATA_SET = RawContacts.DATA_SET; 599 } 600 601 public static final class DirectoryColumns { 602 public static final String TYPE_RESOURCE_NAME = "typeResourceName"; 603 } 604 605 public static final class SearchIndexColumns { 606 public static final String CONTACT_ID = "contact_id"; 607 public static final String CONTENT = "content"; 608 public static final String NAME = "name"; 609 public static final String TOKENS = "tokens"; 610 } 611 612 /** 613 * Private table for calculating per-contact-method ranking. 614 */ 615 public static final class DataUsageStatColumns { 616 /** type: INTEGER (long) */ 617 public static final String _ID = "stat_id"; 618 public static final String CONCRETE_ID = Tables.DATA_USAGE_STAT + "." + _ID; 619 620 /** type: INTEGER (long) */ 621 public static final String DATA_ID = "data_id"; 622 public static final String CONCRETE_DATA_ID = Tables.DATA_USAGE_STAT + "." + DATA_ID; 623 624 /** type: INTEGER (long) */ 625 public static final String LAST_TIME_USED = "last_time_used"; 626 public static final String CONCRETE_LAST_TIME_USED = 627 Tables.DATA_USAGE_STAT + "." + LAST_TIME_USED; 628 629 /** type: INTEGER */ 630 public static final String TIMES_USED = "times_used"; 631 public static final String CONCRETE_TIMES_USED = 632 Tables.DATA_USAGE_STAT + "." + TIMES_USED; 633 634 /** type: INTEGER */ 635 public static final String USAGE_TYPE_INT = "usage_type"; 636 public static final String CONCRETE_USAGE_TYPE = 637 Tables.DATA_USAGE_STAT + "." + USAGE_TYPE_INT; 638 639 /** 640 * Integer values for USAGE_TYPE. 641 * 642 * @see ContactsContract.DataUsageFeedback#USAGE_TYPE 643 */ 644 public static final int USAGE_TYPE_INT_CALL = 0; 645 public static final int USAGE_TYPE_INT_LONG_TEXT = 1; 646 public static final int USAGE_TYPE_INT_SHORT_TEXT = 2; 647 } 648 649 /** In-memory cache of previously found MIME-type mappings */ 650 private final HashMap<String, Long> mMimetypeCache = new HashMap<String, Long>(); 651 /** In-memory cache of previously found package name mappings */ 652 private final HashMap<String, Long> mPackageCache = new HashMap<String, Long>(); 653 654 private long mMimeTypeIdEmail; 655 private long mMimeTypeIdIm; 656 private long mMimeTypeIdNickname; 657 private long mMimeTypeIdOrganization; 658 private long mMimeTypeIdPhone; 659 private long mMimeTypeIdSip; 660 private long mMimeTypeIdStructuredName; 661 private long mMimeTypeIdStructuredPostal; 662 663 /** Compiled statements for querying and inserting mappings */ 664 private SQLiteStatement mContactIdQuery; 665 private SQLiteStatement mAggregationModeQuery; 666 private SQLiteStatement mDataMimetypeQuery; 667 private SQLiteStatement mActivitiesMimetypeQuery; 668 669 /** Precompiled sql statement for setting a data record to the primary. */ 670 private SQLiteStatement mSetPrimaryStatement; 671 /** Precompiled sql statement for setting a data record to the super primary. */ 672 private SQLiteStatement mSetSuperPrimaryStatement; 673 /** Precompiled sql statement for clearing super primary of a single record. */ 674 private SQLiteStatement mClearSuperPrimaryStatement; 675 /** Precompiled sql statement for updating a contact display name */ 676 private SQLiteStatement mRawContactDisplayNameUpdate; 677 678 private SQLiteStatement mNameLookupInsert; 679 private SQLiteStatement mNameLookupDelete; 680 private SQLiteStatement mStatusUpdateAutoTimestamp; 681 private SQLiteStatement mStatusUpdateInsert; 682 private SQLiteStatement mStatusUpdateReplace; 683 private SQLiteStatement mStatusAttributionUpdate; 684 private SQLiteStatement mStatusUpdateDelete; 685 private SQLiteStatement mResetNameVerifiedForOtherRawContacts; 686 private SQLiteStatement mContactInDefaultDirectoryQuery; 687 688 private final Context mContext; 689 private final boolean mDatabaseOptimizationEnabled; 690 private final SyncStateContentProviderHelper mSyncState; 691 private final CountryMonitor mCountryMonitor; 692 private StringBuilder mSb = new StringBuilder(); 693 694 private boolean mReopenDatabase = false; 695 696 private static ContactsDatabaseHelper sSingleton = null; 697 698 private boolean mUseStrictPhoneNumberComparison; 699 700 private String[] mSelectionArgs1 = new String[1]; 701 private NameSplitter.Name mName = new NameSplitter.Name(); 702 private CharArrayBuffer mCharArrayBuffer = new CharArrayBuffer(128); 703 private NameSplitter mNameSplitter; 704 705 public static synchronized ContactsDatabaseHelper getInstance(Context context) { 706 if (sSingleton == null) { 707 sSingleton = new ContactsDatabaseHelper(context, DATABASE_NAME, true); 708 } 709 return sSingleton; 710 } 711 712 /** 713 * Private constructor, callers except unit tests should obtain an instance through 714 * {@link #getInstance(android.content.Context)} instead. 715 */ 716 ContactsDatabaseHelper(Context context) { 717 this(context, null, false); 718 } 719 720 protected ContactsDatabaseHelper( 721 Context context, String databaseName, boolean optimizationEnabled) { 722 super(context, databaseName, null, DATABASE_VERSION); 723 mDatabaseOptimizationEnabled = optimizationEnabled; 724 Resources resources = context.getResources(); 725 726 mContext = context; 727 mSyncState = new SyncStateContentProviderHelper(); 728 mCountryMonitor = new CountryMonitor(context); 729 mUseStrictPhoneNumberComparison = 730 resources.getBoolean( 731 com.android.internal.R.bool.config_use_strict_phone_number_comparation); 732 } 733 734 private void refreshDatabaseCaches(SQLiteDatabase db) { 735 mStatusUpdateDelete = null; 736 mStatusUpdateReplace = null; 737 mStatusUpdateInsert = null; 738 mStatusUpdateAutoTimestamp = null; 739 mStatusAttributionUpdate = null; 740 mResetNameVerifiedForOtherRawContacts = null; 741 mRawContactDisplayNameUpdate = null; 742 mSetPrimaryStatement = null; 743 mClearSuperPrimaryStatement = null; 744 mSetSuperPrimaryStatement = null; 745 mNameLookupInsert = null; 746 mNameLookupDelete = null; 747 mDataMimetypeQuery = null; 748 mActivitiesMimetypeQuery = null; 749 mContactIdQuery = null; 750 mAggregationModeQuery = null; 751 mContactInDefaultDirectoryQuery = null; 752 753 populateMimeTypeCache(db); 754 } 755 756 private void populateMimeTypeCache(SQLiteDatabase db) { 757 mMimetypeCache.clear(); 758 mPackageCache.clear(); 759 760 // TODO: This could be optimized into one query instead of 7 761 // Also: We shouldn't have those fields in the first place. This should just be 762 // in the cache 763 mMimeTypeIdEmail = lookupMimeTypeId(Email.CONTENT_ITEM_TYPE, db); 764 mMimeTypeIdIm = lookupMimeTypeId(Im.CONTENT_ITEM_TYPE, db); 765 mMimeTypeIdNickname = lookupMimeTypeId(Nickname.CONTENT_ITEM_TYPE, db); 766 mMimeTypeIdOrganization = lookupMimeTypeId(Organization.CONTENT_ITEM_TYPE, db); 767 mMimeTypeIdPhone = lookupMimeTypeId(Phone.CONTENT_ITEM_TYPE, db); 768 mMimeTypeIdSip = lookupMimeTypeId(SipAddress.CONTENT_ITEM_TYPE, db); 769 mMimeTypeIdStructuredName = lookupMimeTypeId(StructuredName.CONTENT_ITEM_TYPE, db); 770 mMimeTypeIdStructuredPostal = lookupMimeTypeId(StructuredPostal.CONTENT_ITEM_TYPE, db); 771 } 772 773 @Override 774 public void onOpen(SQLiteDatabase db) { 775 refreshDatabaseCaches(db); 776 777 mSyncState.onDatabaseOpened(db); 778 779 db.execSQL("ATTACH DATABASE ':memory:' AS " + DATABASE_PRESENCE + ";"); 780 db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_PRESENCE + "." + Tables.PRESENCE + " ("+ 781 StatusUpdates.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," + 782 StatusUpdates.PROTOCOL + " INTEGER NOT NULL," + 783 StatusUpdates.CUSTOM_PROTOCOL + " TEXT," + 784 StatusUpdates.IM_HANDLE + " TEXT," + 785 StatusUpdates.IM_ACCOUNT + " TEXT," + 786 PresenceColumns.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," + 787 PresenceColumns.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," + 788 StatusUpdates.PRESENCE + " INTEGER," + 789 StatusUpdates.CHAT_CAPABILITY + " INTEGER NOT NULL DEFAULT 0," + 790 "UNIQUE(" + StatusUpdates.PROTOCOL + ", " + StatusUpdates.CUSTOM_PROTOCOL 791 + ", " + StatusUpdates.IM_HANDLE + ", " + StatusUpdates.IM_ACCOUNT + ")" + 792 ");"); 793 794 db.execSQL("CREATE INDEX IF NOT EXISTS " + DATABASE_PRESENCE + ".presenceIndex" + " ON " 795 + Tables.PRESENCE + " (" + PresenceColumns.RAW_CONTACT_ID + ");"); 796 db.execSQL("CREATE INDEX IF NOT EXISTS " + DATABASE_PRESENCE + ".presenceIndex2" + " ON " 797 + Tables.PRESENCE + " (" + PresenceColumns.CONTACT_ID + ");"); 798 799 db.execSQL("CREATE TABLE IF NOT EXISTS " 800 + DATABASE_PRESENCE + "." + Tables.AGGREGATED_PRESENCE + " ("+ 801 AggregatedPresenceColumns.CONTACT_ID 802 + " INTEGER PRIMARY KEY REFERENCES contacts(_id)," + 803 StatusUpdates.PRESENCE + " INTEGER," + 804 StatusUpdates.CHAT_CAPABILITY + " INTEGER NOT NULL DEFAULT 0" + 805 ");"); 806 807 808 db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_deleted" 809 + " BEFORE DELETE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE 810 + " BEGIN " 811 + " DELETE FROM " + Tables.AGGREGATED_PRESENCE 812 + " WHERE " + AggregatedPresenceColumns.CONTACT_ID + " = " + 813 "(SELECT " + PresenceColumns.CONTACT_ID + 814 " FROM " + Tables.PRESENCE + 815 " WHERE " + PresenceColumns.RAW_CONTACT_ID 816 + "=OLD." + PresenceColumns.RAW_CONTACT_ID + 817 " AND NOT EXISTS" + 818 "(SELECT " + PresenceColumns.RAW_CONTACT_ID + 819 " FROM " + Tables.PRESENCE + 820 " WHERE " + PresenceColumns.CONTACT_ID 821 + "=OLD." + PresenceColumns.CONTACT_ID + 822 " AND " + PresenceColumns.RAW_CONTACT_ID 823 + "!=OLD." + PresenceColumns.RAW_CONTACT_ID + "));" 824 + " END"); 825 826 final String replaceAggregatePresenceSql = 827 "INSERT OR REPLACE INTO " + Tables.AGGREGATED_PRESENCE + "(" 828 + AggregatedPresenceColumns.CONTACT_ID + ", " 829 + StatusUpdates.PRESENCE + ", " 830 + StatusUpdates.CHAT_CAPABILITY + ")" 831 + " SELECT " 832 + PresenceColumns.CONTACT_ID + "," 833 + StatusUpdates.PRESENCE + "," 834 + StatusUpdates.CHAT_CAPABILITY 835 + " FROM " + Tables.PRESENCE 836 + " WHERE " 837 + " (ifnull(" + StatusUpdates.PRESENCE + ",0) * 10 " 838 + "+ ifnull(" + StatusUpdates.CHAT_CAPABILITY + ", 0))" 839 + " = (SELECT " 840 + "MAX (ifnull(" + StatusUpdates.PRESENCE + ",0) * 10 " 841 + "+ ifnull(" + StatusUpdates.CHAT_CAPABILITY + ", 0))" 842 + " FROM " + Tables.PRESENCE 843 + " WHERE " + PresenceColumns.CONTACT_ID 844 + "=NEW." + PresenceColumns.CONTACT_ID 845 + ")" 846 + " AND " + PresenceColumns.CONTACT_ID + "=NEW." + PresenceColumns.CONTACT_ID + ";"; 847 848 db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_inserted" 849 + " AFTER INSERT ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE 850 + " BEGIN " 851 + replaceAggregatePresenceSql 852 + " END"); 853 854 db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_updated" 855 + " AFTER UPDATE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE 856 + " BEGIN " 857 + replaceAggregatePresenceSql 858 + " END"); 859 } 860 861 @Override 862 public void onCreate(SQLiteDatabase db) { 863 Log.i(TAG, "Bootstrapping database version: " + DATABASE_VERSION); 864 865 mSyncState.createDatabase(db); 866 867 // One row per group of contacts corresponding to the same person 868 db.execSQL("CREATE TABLE " + Tables.CONTACTS + " (" + 869 BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 870 Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," + 871 Contacts.PHOTO_ID + " INTEGER REFERENCES data(_id)," + 872 Contacts.PHOTO_FILE_ID + " INTEGER REFERENCES photo_files(_id)," + 873 Contacts.CUSTOM_RINGTONE + " TEXT," + 874 Contacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," + 875 Contacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," + 876 Contacts.LAST_TIME_CONTACTED + " INTEGER," + 877 Contacts.STARRED + " INTEGER NOT NULL DEFAULT 0," + 878 Contacts.HAS_PHONE_NUMBER + " INTEGER NOT NULL DEFAULT 0," + 879 Contacts.LOOKUP_KEY + " TEXT," + 880 ContactsColumns.LAST_STATUS_UPDATE_ID + " INTEGER REFERENCES data(_id)" + 881 ");"); 882 883 db.execSQL("CREATE INDEX contacts_has_phone_index ON " + Tables.CONTACTS + " (" + 884 Contacts.HAS_PHONE_NUMBER + 885 ");"); 886 887 db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" + 888 Contacts.NAME_RAW_CONTACT_ID + 889 ");"); 890 891 // Contacts table 892 db.execSQL("CREATE TABLE " + Tables.RAW_CONTACTS + " (" + 893 RawContacts._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 894 RawContacts.ACCOUNT_NAME + " STRING DEFAULT NULL, " + 895 RawContacts.ACCOUNT_TYPE + " STRING DEFAULT NULL, " + 896 RawContacts.DATA_SET + " STRING DEFAULT NULL, " + 897 RawContacts.SOURCE_ID + " TEXT," + 898 RawContacts.RAW_CONTACT_IS_READ_ONLY + " INTEGER NOT NULL DEFAULT 0," + 899 RawContacts.VERSION + " INTEGER NOT NULL DEFAULT 1," + 900 RawContacts.DIRTY + " INTEGER NOT NULL DEFAULT 0," + 901 RawContacts.DELETED + " INTEGER NOT NULL DEFAULT 0," + 902 RawContacts.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," + 903 RawContacts.AGGREGATION_MODE + " INTEGER NOT NULL DEFAULT " + 904 RawContacts.AGGREGATION_MODE_DEFAULT + "," + 905 RawContactsColumns.AGGREGATION_NEEDED + " INTEGER NOT NULL DEFAULT 1," + 906 RawContacts.CUSTOM_RINGTONE + " TEXT," + 907 RawContacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," + 908 RawContacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," + 909 RawContacts.LAST_TIME_CONTACTED + " INTEGER," + 910 RawContacts.STARRED + " INTEGER NOT NULL DEFAULT 0," + 911 RawContacts.DISPLAY_NAME_PRIMARY + " TEXT," + 912 RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT," + 913 RawContacts.DISPLAY_NAME_SOURCE + " INTEGER NOT NULL DEFAULT " + 914 DisplayNameSources.UNDEFINED + "," + 915 RawContacts.PHONETIC_NAME + " TEXT," + 916 RawContacts.PHONETIC_NAME_STYLE + " TEXT," + 917 RawContacts.SORT_KEY_PRIMARY + " TEXT COLLATE " + 918 ContactsProvider2.PHONEBOOK_COLLATOR_NAME + "," + 919 RawContacts.SORT_KEY_ALTERNATIVE + " TEXT COLLATE " + 920 ContactsProvider2.PHONEBOOK_COLLATOR_NAME + "," + 921 RawContacts.NAME_VERIFIED + " INTEGER NOT NULL DEFAULT 0," + 922 RawContacts.SYNC1 + " TEXT, " + 923 RawContacts.SYNC2 + " TEXT, " + 924 RawContacts.SYNC3 + " TEXT, " + 925 RawContacts.SYNC4 + " TEXT " + 926 ");"); 927 928 db.execSQL("CREATE INDEX raw_contacts_contact_id_index ON " + Tables.RAW_CONTACTS + " (" + 929 RawContacts.CONTACT_ID + 930 ");"); 931 932 db.execSQL("CREATE INDEX raw_contacts_source_id_index ON " + Tables.RAW_CONTACTS + " (" + 933 RawContacts.SOURCE_ID + ", " + 934 RawContacts.ACCOUNT_TYPE + ", " + 935 RawContacts.ACCOUNT_NAME + 936 ");"); 937 938 db.execSQL("CREATE INDEX raw_contacts_source_id_data_set_index ON " + 939 Tables.RAW_CONTACTS + " (" + 940 RawContacts.SOURCE_ID + ", " + 941 RawContacts.ACCOUNT_TYPE + ", " + 942 RawContacts.ACCOUNT_NAME + ", " + 943 RawContacts.DATA_SET + 944 ");"); 945 946 db.execSQL("CREATE TABLE " + Tables.STREAM_ITEMS + " (" + 947 StreamItems._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 948 StreamItems.RAW_CONTACT_ID + " INTEGER NOT NULL, " + 949 StreamItems.RES_PACKAGE + " TEXT, " + 950 StreamItems.RES_ICON + " TEXT, " + 951 StreamItems.RES_LABEL + " TEXT, " + 952 StreamItems.TEXT + " TEXT, " + 953 StreamItems.TIMESTAMP + " INTEGER NOT NULL, " + 954 StreamItems.COMMENTS + " TEXT, " + 955 StreamItems.SYNC1 + " TEXT, " + 956 StreamItems.SYNC2 + " TEXT, " + 957 StreamItems.SYNC3 + " TEXT, " + 958 StreamItems.SYNC4 + " TEXT, " + 959 "FOREIGN KEY(" + StreamItems.RAW_CONTACT_ID + ") REFERENCES " + 960 Tables.RAW_CONTACTS + "(" + RawContacts._ID + "));"); 961 962 db.execSQL("CREATE TABLE " + Tables.STREAM_ITEM_PHOTOS + " (" + 963 StreamItemPhotos._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 964 StreamItemPhotos.STREAM_ITEM_ID + " INTEGER NOT NULL, " + 965 StreamItemPhotos.SORT_INDEX + " INTEGER, " + 966 StreamItemPhotos.PHOTO_FILE_ID + " INTEGER NOT NULL, " + 967 StreamItemPhotos.SYNC1 + " TEXT, " + 968 StreamItemPhotos.SYNC2 + " TEXT, " + 969 StreamItemPhotos.SYNC3 + " TEXT, " + 970 StreamItemPhotos.SYNC4 + " TEXT, " + 971 "FOREIGN KEY(" + StreamItemPhotos.STREAM_ITEM_ID + ") REFERENCES " + 972 Tables.STREAM_ITEMS + "(" + StreamItems._ID + "));"); 973 974 db.execSQL("CREATE TABLE " + Tables.PHOTO_FILES + " (" + 975 PhotoFiles._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 976 PhotoFiles.HEIGHT + " INTEGER NOT NULL, " + 977 PhotoFiles.WIDTH + " INTEGER NOT NULL, " + 978 PhotoFiles.FILESIZE + " INTEGER NOT NULL);"); 979 980 // TODO readd the index and investigate a controlled use of it 981// db.execSQL("CREATE INDEX raw_contacts_agg_index ON " + Tables.RAW_CONTACTS + " (" + 982// RawContactsColumns.AGGREGATION_NEEDED + 983// ");"); 984 985 // Package name mapping table 986 db.execSQL("CREATE TABLE " + Tables.PACKAGES + " (" + 987 PackagesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 988 PackagesColumns.PACKAGE + " TEXT NOT NULL" + 989 ");"); 990 991 // Mimetype mapping table 992 db.execSQL("CREATE TABLE " + Tables.MIMETYPES + " (" + 993 MimetypesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 994 MimetypesColumns.MIMETYPE + " TEXT NOT NULL" + 995 ");"); 996 997 // Mimetype table requires an index on mime type 998 db.execSQL("CREATE UNIQUE INDEX mime_type ON " + Tables.MIMETYPES + " (" + 999 MimetypesColumns.MIMETYPE + 1000 ");"); 1001 1002 // Public generic data table 1003 db.execSQL("CREATE TABLE " + Tables.DATA + " (" + 1004 Data._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 1005 DataColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," + 1006 DataColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," + 1007 Data.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," + 1008 Data.IS_READ_ONLY + " INTEGER NOT NULL DEFAULT 0," + 1009 Data.IS_PRIMARY + " INTEGER NOT NULL DEFAULT 0," + 1010 Data.IS_SUPER_PRIMARY + " INTEGER NOT NULL DEFAULT 0," + 1011 Data.DATA_VERSION + " INTEGER NOT NULL DEFAULT 0," + 1012 Data.DATA1 + " TEXT," + 1013 Data.DATA2 + " TEXT," + 1014 Data.DATA3 + " TEXT," + 1015 Data.DATA4 + " TEXT," + 1016 Data.DATA5 + " TEXT," + 1017 Data.DATA6 + " TEXT," + 1018 Data.DATA7 + " TEXT," + 1019 Data.DATA8 + " TEXT," + 1020 Data.DATA9 + " TEXT," + 1021 Data.DATA10 + " TEXT," + 1022 Data.DATA11 + " TEXT," + 1023 Data.DATA12 + " TEXT," + 1024 Data.DATA13 + " TEXT," + 1025 Data.DATA14 + " TEXT," + 1026 Data.DATA15 + " TEXT," + 1027 Data.SYNC1 + " TEXT, " + 1028 Data.SYNC2 + " TEXT, " + 1029 Data.SYNC3 + " TEXT, " + 1030 Data.SYNC4 + " TEXT " + 1031 ");"); 1032 1033 db.execSQL("CREATE INDEX data_raw_contact_id ON " + Tables.DATA + " (" + 1034 Data.RAW_CONTACT_ID + 1035 ");"); 1036 1037 /** 1038 * For email lookup and similar queries. 1039 */ 1040 db.execSQL("CREATE INDEX data_mimetype_data1_index ON " + Tables.DATA + " (" + 1041 DataColumns.MIMETYPE_ID + "," + 1042 Data.DATA1 + 1043 ");"); 1044 1045 // Private phone numbers table used for lookup 1046 db.execSQL("CREATE TABLE " + Tables.PHONE_LOOKUP + " (" + 1047 PhoneLookupColumns.DATA_ID 1048 + " INTEGER REFERENCES data(_id) NOT NULL," + 1049 PhoneLookupColumns.RAW_CONTACT_ID 1050 + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," + 1051 PhoneLookupColumns.NORMALIZED_NUMBER + " TEXT NOT NULL," + 1052 PhoneLookupColumns.MIN_MATCH + " TEXT NOT NULL" + 1053 ");"); 1054 1055 db.execSQL("CREATE INDEX phone_lookup_index ON " + Tables.PHONE_LOOKUP + " (" + 1056 PhoneLookupColumns.NORMALIZED_NUMBER + "," + 1057 PhoneLookupColumns.RAW_CONTACT_ID + "," + 1058 PhoneLookupColumns.DATA_ID + 1059 ");"); 1060 1061 db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" + 1062 PhoneLookupColumns.MIN_MATCH + "," + 1063 PhoneLookupColumns.RAW_CONTACT_ID + "," + 1064 PhoneLookupColumns.DATA_ID + 1065 ");"); 1066 1067 db.execSQL("CREATE INDEX phone_lookup_data_id_min_match_index ON " + Tables.PHONE_LOOKUP + 1068 " (" + PhoneLookupColumns.DATA_ID + ", " + PhoneLookupColumns.MIN_MATCH + ");"); 1069 1070 // Private name/nickname table used for lookup 1071 db.execSQL("CREATE TABLE " + Tables.NAME_LOOKUP + " (" + 1072 NameLookupColumns.DATA_ID 1073 + " INTEGER REFERENCES data(_id) NOT NULL," + 1074 NameLookupColumns.RAW_CONTACT_ID 1075 + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," + 1076 NameLookupColumns.NORMALIZED_NAME + " TEXT NOT NULL," + 1077 NameLookupColumns.NAME_TYPE + " INTEGER NOT NULL," + 1078 "PRIMARY KEY (" 1079 + NameLookupColumns.DATA_ID + ", " 1080 + NameLookupColumns.NORMALIZED_NAME + ", " 1081 + NameLookupColumns.NAME_TYPE + ")" + 1082 ");"); 1083 1084 db.execSQL("CREATE INDEX name_lookup_raw_contact_id_index ON " + Tables.NAME_LOOKUP + " (" + 1085 NameLookupColumns.RAW_CONTACT_ID + 1086 ");"); 1087 1088 db.execSQL("CREATE TABLE " + Tables.NICKNAME_LOOKUP + " (" + 1089 NicknameLookupColumns.NAME + " TEXT," + 1090 NicknameLookupColumns.CLUSTER + " TEXT" + 1091 ");"); 1092 1093 db.execSQL("CREATE UNIQUE INDEX nickname_lookup_index ON " + Tables.NICKNAME_LOOKUP + " (" + 1094 NicknameLookupColumns.NAME + ", " + 1095 NicknameLookupColumns.CLUSTER + 1096 ");"); 1097 1098 // Groups table 1099 db.execSQL("CREATE TABLE " + Tables.GROUPS + " (" + 1100 Groups._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 1101 GroupsColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," + 1102 Groups.ACCOUNT_NAME + " STRING DEFAULT NULL, " + 1103 Groups.ACCOUNT_TYPE + " STRING DEFAULT NULL, " + 1104 Groups.DATA_SET + " STRING DEFAULT NULL, " + 1105 Groups.SOURCE_ID + " TEXT," + 1106 Groups.VERSION + " INTEGER NOT NULL DEFAULT 1," + 1107 Groups.DIRTY + " INTEGER NOT NULL DEFAULT 0," + 1108 Groups.TITLE + " TEXT," + 1109 Groups.TITLE_RES + " INTEGER," + 1110 Groups.NOTES + " TEXT," + 1111 Groups.SYSTEM_ID + " TEXT," + 1112 Groups.DELETED + " INTEGER NOT NULL DEFAULT 0," + 1113 Groups.GROUP_VISIBLE + " INTEGER NOT NULL DEFAULT 0," + 1114 Groups.SHOULD_SYNC + " INTEGER NOT NULL DEFAULT 1," + 1115 Groups.AUTO_ADD + " INTEGER NOT NULL DEFAULT 0," + 1116 Groups.FAVORITES + " INTEGER NOT NULL DEFAULT 0," + 1117 Groups.GROUP_IS_READ_ONLY + " INTEGER NOT NULL DEFAULT 0," + 1118 Groups.SYNC1 + " TEXT, " + 1119 Groups.SYNC2 + " TEXT, " + 1120 Groups.SYNC3 + " TEXT, " + 1121 Groups.SYNC4 + " TEXT " + 1122 ");"); 1123 1124 db.execSQL("CREATE INDEX groups_source_id_index ON " + Tables.GROUPS + " (" + 1125 Groups.SOURCE_ID + ", " + 1126 Groups.ACCOUNT_TYPE + ", " + 1127 Groups.ACCOUNT_NAME + 1128 ");"); 1129 1130 db.execSQL("CREATE INDEX groups_source_id_data_set_index ON " + Tables.GROUPS + " (" + 1131 Groups.SOURCE_ID + ", " + 1132 Groups.ACCOUNT_TYPE + ", " + 1133 Groups.ACCOUNT_NAME + ", " + 1134 Groups.DATA_SET + 1135 ");"); 1136 1137 db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.AGGREGATION_EXCEPTIONS + " (" + 1138 AggregationExceptionColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 1139 AggregationExceptions.TYPE + " INTEGER NOT NULL, " + 1140 AggregationExceptions.RAW_CONTACT_ID1 1141 + " INTEGER REFERENCES raw_contacts(_id), " + 1142 AggregationExceptions.RAW_CONTACT_ID2 1143 + " INTEGER REFERENCES raw_contacts(_id)" + 1144 ");"); 1145 1146 db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS aggregation_exception_index1 ON " + 1147 Tables.AGGREGATION_EXCEPTIONS + " (" + 1148 AggregationExceptions.RAW_CONTACT_ID1 + ", " + 1149 AggregationExceptions.RAW_CONTACT_ID2 + 1150 ");"); 1151 1152 db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS aggregation_exception_index2 ON " + 1153 Tables.AGGREGATION_EXCEPTIONS + " (" + 1154 AggregationExceptions.RAW_CONTACT_ID2 + ", " + 1155 AggregationExceptions.RAW_CONTACT_ID1 + 1156 ");"); 1157 1158 db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.SETTINGS + " (" + 1159 Settings.ACCOUNT_NAME + " STRING NOT NULL," + 1160 Settings.ACCOUNT_TYPE + " STRING NOT NULL," + 1161 Settings.DATA_SET + " STRING," + 1162 Settings.UNGROUPED_VISIBLE + " INTEGER NOT NULL DEFAULT 0," + 1163 Settings.SHOULD_SYNC + " INTEGER NOT NULL DEFAULT 1" + 1164 ");"); 1165 1166 db.execSQL("CREATE TABLE " + Tables.VISIBLE_CONTACTS + " (" + 1167 Contacts._ID + " INTEGER PRIMARY KEY" + 1168 ");"); 1169 1170 db.execSQL("CREATE TABLE " + Tables.DEFAULT_DIRECTORY + " (" + 1171 Contacts._ID + " INTEGER PRIMARY KEY" + 1172 ");"); 1173 1174 // The table for recent calls is here so we can do table joins 1175 // on people, phones, and calls all in one place. 1176 db.execSQL("CREATE TABLE " + Tables.CALLS + " (" + 1177 Calls._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 1178 Calls.NUMBER + " TEXT," + 1179 Calls.DATE + " INTEGER," + 1180 Calls.DURATION + " INTEGER," + 1181 Calls.TYPE + " INTEGER," + 1182 Calls.NEW + " INTEGER," + 1183 Calls.CACHED_NAME + " TEXT," + 1184 Calls.CACHED_NUMBER_TYPE + " INTEGER," + 1185 Calls.CACHED_NUMBER_LABEL + " TEXT," + 1186 Calls.COUNTRY_ISO + " TEXT," + 1187 Calls.VOICEMAIL_URI + " TEXT," + 1188 Calls.IS_READ + " INTEGER," + 1189 Calls.GEOCODED_LOCATION + " TEXT," + 1190 Calls.CACHED_LOOKUP_URI + " TEXT," + 1191 Calls.CACHED_MATCHED_NUMBER + " TEXT," + 1192 Calls.CACHED_NORMALIZED_NUMBER + " TEXT," + 1193 Calls.CACHED_PHOTO_ID + " INTEGER NOT NULL DEFAULT 0," + 1194 Calls.CACHED_FORMATTED_NUMBER + " TEXT," + 1195 Voicemails._DATA + " TEXT," + 1196 Voicemails.HAS_CONTENT + " INTEGER," + 1197 Voicemails.MIME_TYPE + " TEXT," + 1198 Voicemails.SOURCE_DATA + " TEXT," + 1199 Voicemails.SOURCE_PACKAGE + " TEXT," + 1200 Voicemails.STATE + " INTEGER" + 1201 ");"); 1202 1203 // Voicemail source status table. 1204 db.execSQL("CREATE TABLE " + Tables.VOICEMAIL_STATUS + " (" + 1205 VoicemailContract.Status._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 1206 VoicemailContract.Status.SOURCE_PACKAGE + " TEXT UNIQUE NOT NULL," + 1207 VoicemailContract.Status.SETTINGS_URI + " TEXT," + 1208 VoicemailContract.Status.VOICEMAIL_ACCESS_URI + " TEXT," + 1209 VoicemailContract.Status.CONFIGURATION_STATE + " INTEGER," + 1210 VoicemailContract.Status.DATA_CHANNEL_STATE + " INTEGER," + 1211 VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE + " INTEGER" + 1212 ");"); 1213 1214 // Activities table 1215 db.execSQL("CREATE TABLE " + Tables.ACTIVITIES + " (" + 1216 Activities._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 1217 ActivitiesColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," + 1218 ActivitiesColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," + 1219 Activities.RAW_ID + " TEXT," + 1220 Activities.IN_REPLY_TO + " TEXT," + 1221 Activities.AUTHOR_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," + 1222 Activities.TARGET_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," + 1223 Activities.PUBLISHED + " INTEGER NOT NULL," + 1224 Activities.THREAD_PUBLISHED + " INTEGER NOT NULL," + 1225 Activities.TITLE + " TEXT NOT NULL," + 1226 Activities.SUMMARY + " TEXT," + 1227 Activities.LINK + " TEXT, " + 1228 Activities.THUMBNAIL + " BLOB" + 1229 ");"); 1230 1231 db.execSQL("CREATE TABLE " + Tables.STATUS_UPDATES + " (" + 1232 StatusUpdatesColumns.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," + 1233 StatusUpdates.STATUS + " TEXT," + 1234 StatusUpdates.STATUS_TIMESTAMP + " INTEGER," + 1235 StatusUpdates.STATUS_RES_PACKAGE + " TEXT, " + 1236 StatusUpdates.STATUS_LABEL + " INTEGER, " + 1237 StatusUpdates.STATUS_ICON + " INTEGER" + 1238 ");"); 1239 1240 db.execSQL("CREATE TABLE " + Tables.PROPERTIES + " (" + 1241 PropertiesColumns.PROPERTY_KEY + " TEXT PRIMARY KEY, " + 1242 PropertiesColumns.PROPERTY_VALUE + " TEXT " + 1243 ");"); 1244 1245 db.execSQL("CREATE TABLE " + Tables.ACCOUNTS + " (" + 1246 AccountsColumns.ACCOUNT_NAME + " TEXT, " + 1247 AccountsColumns.ACCOUNT_TYPE + " TEXT, " + 1248 AccountsColumns.DATA_SET + " TEXT" + 1249 ");"); 1250 1251 // Allow contacts without any account to be created for now. Achieve that 1252 // by inserting a fake account with both type and name as NULL. 1253 db.execSQL("INSERT INTO " + Tables.ACCOUNTS + " VALUES(NULL, NULL, NULL)"); 1254 1255 createDirectoriesTable(db); 1256 createSearchIndexTable(db); 1257 1258 db.execSQL("CREATE TABLE " + Tables.DATA_USAGE_STAT + "(" + 1259 DataUsageStatColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 1260 DataUsageStatColumns.DATA_ID + " INTEGER NOT NULL, " + 1261 DataUsageStatColumns.USAGE_TYPE_INT + " INTEGER NOT NULL DEFAULT 0, " + 1262 DataUsageStatColumns.TIMES_USED + " INTEGER NOT NULL DEFAULT 0, " + 1263 DataUsageStatColumns.LAST_TIME_USED + " INTERGER NOT NULL DEFAULT 0, " + 1264 "FOREIGN KEY(" + DataUsageStatColumns.DATA_ID + ") REFERENCES " 1265 + Tables.DATA + "(" + Data._ID + ")" + 1266 ");"); 1267 db.execSQL("CREATE UNIQUE INDEX data_usage_stat_index ON " + 1268 Tables.DATA_USAGE_STAT + " (" + 1269 DataUsageStatColumns.DATA_ID + ", " + 1270 DataUsageStatColumns.USAGE_TYPE_INT + 1271 ");"); 1272 1273 createContactsViews(db); 1274 createGroupsView(db); 1275 createContactsTriggers(db); 1276 createContactsIndexes(db); 1277 1278 loadNicknameLookupTable(db); 1279 1280 // Set sequence starts. 1281 initializeAutoIncrementSequences(db); 1282 1283 // Add the legacy API support views, etc 1284 LegacyApiSupport.createDatabase(db); 1285 1286 if (mDatabaseOptimizationEnabled) { 1287 // This will create a sqlite_stat1 table that is used for query optimization 1288 db.execSQL("ANALYZE;"); 1289 1290 updateSqliteStats(db); 1291 1292 // We need to close and reopen the database connection so that the stats are 1293 // taken into account. Make a note of it and do the actual reopening in the 1294 // getWritableDatabase method. 1295 mReopenDatabase = true; 1296 } 1297 1298 ContentResolver.requestSync(null /* all accounts */, 1299 ContactsContract.AUTHORITY, new Bundle()); 1300 } 1301 1302 protected void initializeAutoIncrementSequences(SQLiteDatabase db) { 1303 // Default implementation does nothing. 1304 } 1305 1306 private void createDirectoriesTable(SQLiteDatabase db) { 1307 db.execSQL("CREATE TABLE " + Tables.DIRECTORIES + "(" + 1308 Directory._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 1309 Directory.PACKAGE_NAME + " TEXT NOT NULL," + 1310 Directory.DIRECTORY_AUTHORITY + " TEXT NOT NULL," + 1311 Directory.TYPE_RESOURCE_ID + " INTEGER," + 1312 DirectoryColumns.TYPE_RESOURCE_NAME + " TEXT," + 1313 Directory.ACCOUNT_TYPE + " TEXT," + 1314 Directory.ACCOUNT_NAME + " TEXT," + 1315 Directory.DISPLAY_NAME + " TEXT, " + 1316 Directory.EXPORT_SUPPORT + " INTEGER NOT NULL" + 1317 " DEFAULT " + Directory.EXPORT_SUPPORT_NONE + "," + 1318 Directory.SHORTCUT_SUPPORT + " INTEGER NOT NULL" + 1319 " DEFAULT " + Directory.SHORTCUT_SUPPORT_NONE + "," + 1320 Directory.PHOTO_SUPPORT + " INTEGER NOT NULL" + 1321 " DEFAULT " + Directory.PHOTO_SUPPORT_NONE + 1322 ");"); 1323 1324 // Trigger a full scan of directories in the system 1325 setProperty(db, ContactDirectoryManager.PROPERTY_DIRECTORY_SCAN_COMPLETE, "0"); 1326 } 1327 1328 public void createSearchIndexTable(SQLiteDatabase db) { 1329 db.execSQL("DROP TABLE IF EXISTS " + Tables.SEARCH_INDEX); 1330 db.execSQL("CREATE VIRTUAL TABLE " + Tables.SEARCH_INDEX 1331 + " USING FTS4 (" 1332 + SearchIndexColumns.CONTACT_ID + " INTEGER REFERENCES contacts(_id) NOT NULL," 1333 + SearchIndexColumns.CONTENT + " TEXT, " 1334 + SearchIndexColumns.NAME + " TEXT, " 1335 + SearchIndexColumns.TOKENS + " TEXT" 1336 + ")"); 1337 } 1338 1339 private void createContactsTriggers(SQLiteDatabase db) { 1340 1341 /* 1342 * Automatically delete Data rows when a raw contact is deleted. 1343 */ 1344 db.execSQL("DROP TRIGGER IF EXISTS " + Tables.RAW_CONTACTS + "_deleted;"); 1345 db.execSQL("CREATE TRIGGER " + Tables.RAW_CONTACTS + "_deleted " 1346 + " BEFORE DELETE ON " + Tables.RAW_CONTACTS 1347 + " BEGIN " 1348 + " DELETE FROM " + Tables.DATA 1349 + " WHERE " + Data.RAW_CONTACT_ID 1350 + "=OLD." + RawContacts._ID + ";" 1351 + " DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS 1352 + " WHERE " + AggregationExceptions.RAW_CONTACT_ID1 1353 + "=OLD." + RawContacts._ID 1354 + " OR " + AggregationExceptions.RAW_CONTACT_ID2 1355 + "=OLD." + RawContacts._ID + ";" 1356 + " DELETE FROM " + Tables.VISIBLE_CONTACTS 1357 + " WHERE " + Contacts._ID + "=OLD." + RawContacts.CONTACT_ID 1358 + " AND (SELECT COUNT(*) FROM " + Tables.RAW_CONTACTS 1359 + " WHERE " + RawContacts.CONTACT_ID + "=OLD." + RawContacts.CONTACT_ID 1360 + " )=1;" 1361 + " DELETE FROM " + Tables.DEFAULT_DIRECTORY 1362 + " WHERE " + Contacts._ID + "=OLD." + RawContacts.CONTACT_ID 1363 + " AND (SELECT COUNT(*) FROM " + Tables.RAW_CONTACTS 1364 + " WHERE " + RawContacts.CONTACT_ID + "=OLD." + RawContacts.CONTACT_ID 1365 + " )=1;" 1366 + " DELETE FROM " + Tables.CONTACTS 1367 + " WHERE " + Contacts._ID + "=OLD." + RawContacts.CONTACT_ID 1368 + " AND (SELECT COUNT(*) FROM " + Tables.RAW_CONTACTS 1369 + " WHERE " + RawContacts.CONTACT_ID + "=OLD." + RawContacts.CONTACT_ID 1370 + " )=1;" 1371 + " END"); 1372 1373 1374 db.execSQL("DROP TRIGGER IF EXISTS contacts_times_contacted;"); 1375 db.execSQL("DROP TRIGGER IF EXISTS raw_contacts_times_contacted;"); 1376 1377 /* 1378 * Triggers that update {@link RawContacts#VERSION} when the contact is 1379 * marked for deletion or any time a data row is inserted, updated or 1380 * deleted. 1381 */ 1382 db.execSQL("DROP TRIGGER IF EXISTS " + Tables.RAW_CONTACTS + "_marked_deleted;"); 1383 db.execSQL("CREATE TRIGGER " + Tables.RAW_CONTACTS + "_marked_deleted " 1384 + " AFTER UPDATE ON " + Tables.RAW_CONTACTS 1385 + " BEGIN " 1386 + " UPDATE " + Tables.RAW_CONTACTS 1387 + " SET " 1388 + RawContacts.VERSION + "=OLD." + RawContacts.VERSION + "+1 " 1389 + " WHERE " + RawContacts._ID + "=OLD." + RawContacts._ID 1390 + " AND NEW." + RawContacts.DELETED + "!= OLD." + RawContacts.DELETED + ";" 1391 + " END"); 1392 1393 db.execSQL("DROP TRIGGER IF EXISTS " + Tables.DATA + "_updated;"); 1394 db.execSQL("CREATE TRIGGER " + Tables.DATA + "_updated AFTER UPDATE ON " + Tables.DATA 1395 + " BEGIN " 1396 + " UPDATE " + Tables.DATA 1397 + " SET " + Data.DATA_VERSION + "=OLD." + Data.DATA_VERSION + "+1 " 1398 + " WHERE " + Data._ID + "=OLD." + Data._ID + ";" 1399 + " UPDATE " + Tables.RAW_CONTACTS 1400 + " SET " + RawContacts.VERSION + "=" + RawContacts.VERSION + "+1 " 1401 + " WHERE " + RawContacts._ID + "=OLD." + Data.RAW_CONTACT_ID + ";" 1402 + " END"); 1403 1404 db.execSQL("DROP TRIGGER IF EXISTS " + Tables.DATA + "_deleted;"); 1405 db.execSQL("CREATE TRIGGER " + Tables.DATA + "_deleted BEFORE DELETE ON " + Tables.DATA 1406 + " BEGIN " 1407 + " UPDATE " + Tables.RAW_CONTACTS 1408 + " SET " + RawContacts.VERSION + "=" + RawContacts.VERSION + "+1 " 1409 + " WHERE " + RawContacts._ID + "=OLD." + Data.RAW_CONTACT_ID + ";" 1410 + " DELETE FROM " + Tables.PHONE_LOOKUP 1411 + " WHERE " + PhoneLookupColumns.DATA_ID + "=OLD." + Data._ID + ";" 1412 + " DELETE FROM " + Tables.STATUS_UPDATES 1413 + " WHERE " + StatusUpdatesColumns.DATA_ID + "=OLD." + Data._ID + ";" 1414 + " DELETE FROM " + Tables.NAME_LOOKUP 1415 + " WHERE " + NameLookupColumns.DATA_ID + "=OLD." + Data._ID + ";" 1416 + " END"); 1417 1418 1419 db.execSQL("DROP TRIGGER IF EXISTS " + Tables.GROUPS + "_updated1;"); 1420 db.execSQL("CREATE TRIGGER " + Tables.GROUPS + "_updated1 " 1421 + " AFTER UPDATE ON " + Tables.GROUPS 1422 + " BEGIN " 1423 + " UPDATE " + Tables.GROUPS 1424 + " SET " 1425 + Groups.VERSION + "=OLD." + Groups.VERSION + "+1" 1426 + " WHERE " + Groups._ID + "=OLD." + Groups._ID + ";" 1427 + " END"); 1428 1429 // Update DEFAULT_FILTER table per AUTO_ADD column update. 1430 // See also upgradeToVersion411(). 1431 final String insertContactsWithoutAccount = ( 1432 " INSERT OR IGNORE INTO " + Tables.DEFAULT_DIRECTORY + 1433 " SELECT " + RawContacts.CONTACT_ID + 1434 " FROM " + Tables.RAW_CONTACTS + 1435 " WHERE " + RawContactsColumns.CONCRETE_ACCOUNT_NAME + " IS NULL " + 1436 " AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " IS NULL; "); 1437 final String insertContactsWithAccountNoDefaultGroup = ( 1438 " INSERT OR IGNORE INTO " + Tables.DEFAULT_DIRECTORY + 1439 " SELECT " + RawContacts.CONTACT_ID + 1440 " FROM " + Tables.RAW_CONTACTS + 1441 " WHERE NOT EXISTS" + 1442 " (SELECT " + Groups._ID + 1443 " FROM " + Tables.GROUPS + 1444 " WHERE " + RawContactsColumns.CONCRETE_ACCOUNT_NAME + " = " + 1445 GroupsColumns.CONCRETE_ACCOUNT_NAME + 1446 " AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " = " + 1447 GroupsColumns.CONCRETE_ACCOUNT_TYPE + 1448 " AND " + Groups.AUTO_ADD + " != 0" + ");"); 1449 final String insertContactsWithAccountDefaultGroup = ( 1450 " INSERT OR IGNORE INTO " + Tables.DEFAULT_DIRECTORY + 1451 " SELECT " + RawContacts.CONTACT_ID + 1452 " FROM " + Tables.RAW_CONTACTS + 1453 " JOIN " + Tables.DATA + 1454 " ON (" + RawContactsColumns.CONCRETE_ID + "=" + 1455 Data.RAW_CONTACT_ID + ")" + 1456 " WHERE " + DataColumns.MIMETYPE_ID + "=" + 1457 "(SELECT " + MimetypesColumns._ID + " FROM " + Tables.MIMETYPES + 1458 " WHERE " + MimetypesColumns.MIMETYPE + 1459 "='" + GroupMembership.CONTENT_ITEM_TYPE + "')" + 1460 " AND EXISTS" + 1461 " (SELECT " + Groups._ID + 1462 " FROM " + Tables.GROUPS + 1463 " WHERE " + RawContactsColumns.CONCRETE_ACCOUNT_NAME + " = " + 1464 GroupsColumns.CONCRETE_ACCOUNT_NAME + 1465 " AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " = " + 1466 GroupsColumns.CONCRETE_ACCOUNT_TYPE + 1467 " AND " + Groups.AUTO_ADD + " != 0" + ");"); 1468 1469 db.execSQL("DROP TRIGGER IF EXISTS " + Tables.GROUPS + "_auto_add_updated1;"); 1470 db.execSQL("CREATE TRIGGER " + Tables.GROUPS + "_auto_add_updated1 " 1471 + " AFTER UPDATE OF " + Groups.AUTO_ADD + " ON " + Tables.GROUPS 1472 + " BEGIN " 1473 + " DELETE FROM " + Tables.DEFAULT_DIRECTORY + ";" 1474 + insertContactsWithoutAccount 1475 + insertContactsWithAccountNoDefaultGroup 1476 + insertContactsWithAccountDefaultGroup 1477 + " END"); 1478 } 1479 1480 private void createContactsIndexes(SQLiteDatabase db) { 1481 db.execSQL("DROP INDEX IF EXISTS name_lookup_index"); 1482 db.execSQL("CREATE INDEX name_lookup_index ON " + Tables.NAME_LOOKUP + " (" + 1483 NameLookupColumns.NORMALIZED_NAME + "," + 1484 NameLookupColumns.NAME_TYPE + ", " + 1485 NameLookupColumns.RAW_CONTACT_ID + ", " + 1486 NameLookupColumns.DATA_ID + 1487 ");"); 1488 1489 db.execSQL("DROP INDEX IF EXISTS raw_contact_sort_key1_index"); 1490 db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" + 1491 RawContacts.SORT_KEY_PRIMARY + 1492 ");"); 1493 1494 db.execSQL("DROP INDEX IF EXISTS raw_contact_sort_key2_index"); 1495 db.execSQL("CREATE INDEX raw_contact_sort_key2_index ON " + Tables.RAW_CONTACTS + " (" + 1496 RawContacts.SORT_KEY_ALTERNATIVE + 1497 ");"); 1498 } 1499 1500 private void createContactsViews(SQLiteDatabase db) { 1501 db.execSQL("DROP VIEW IF EXISTS " + Views.CONTACTS + ";"); 1502 db.execSQL("DROP VIEW IF EXISTS " + Views.DATA + ";"); 1503 db.execSQL("DROP VIEW IF EXISTS " + Views.RAW_CONTACTS + ";"); 1504 db.execSQL("DROP VIEW IF EXISTS " + Views.RAW_ENTITIES + ";"); 1505 db.execSQL("DROP VIEW IF EXISTS " + Views.ENTITIES + ";"); 1506 db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_USAGE_STAT + ";"); 1507 db.execSQL("DROP VIEW IF EXISTS " + Views.STREAM_ITEMS + ";"); 1508 1509 String dataColumns = 1510 Data.IS_PRIMARY + ", " 1511 + Data.IS_SUPER_PRIMARY + ", " 1512 + Data.DATA_VERSION + ", " 1513 + DataColumns.CONCRETE_PACKAGE_ID + "," 1514 + PackagesColumns.PACKAGE + " AS " + Data.RES_PACKAGE + "," 1515 + DataColumns.CONCRETE_MIMETYPE_ID + "," 1516 + MimetypesColumns.MIMETYPE + " AS " + Data.MIMETYPE + ", " 1517 + Data.IS_READ_ONLY + ", " 1518 + Data.DATA1 + ", " 1519 + Data.DATA2 + ", " 1520 + Data.DATA3 + ", " 1521 + Data.DATA4 + ", " 1522 + Data.DATA5 + ", " 1523 + Data.DATA6 + ", " 1524 + Data.DATA7 + ", " 1525 + Data.DATA8 + ", " 1526 + Data.DATA9 + ", " 1527 + Data.DATA10 + ", " 1528 + Data.DATA11 + ", " 1529 + Data.DATA12 + ", " 1530 + Data.DATA13 + ", " 1531 + Data.DATA14 + ", " 1532 + Data.DATA15 + ", " 1533 + Data.SYNC1 + ", " 1534 + Data.SYNC2 + ", " 1535 + Data.SYNC3 + ", " 1536 + Data.SYNC4; 1537 1538 String syncColumns = 1539 RawContactsColumns.CONCRETE_ACCOUNT_NAME + " AS " + RawContacts.ACCOUNT_NAME + "," 1540 + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " AS " + RawContacts.ACCOUNT_TYPE + "," 1541 + RawContactsColumns.CONCRETE_DATA_SET + " AS " + RawContacts.DATA_SET + "," 1542 + "(CASE WHEN " + RawContactsColumns.CONCRETE_DATA_SET + " IS NULL THEN " 1543 + RawContactsColumns.CONCRETE_ACCOUNT_TYPE 1544 + " ELSE " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + "||'/'||" 1545 + RawContactsColumns.CONCRETE_DATA_SET + " END) AS " 1546 + RawContacts.ACCOUNT_TYPE_AND_DATA_SET + "," 1547 + RawContactsColumns.CONCRETE_SOURCE_ID + " AS " + RawContacts.SOURCE_ID + "," 1548 + RawContactsColumns.CONCRETE_NAME_VERIFIED + " AS " 1549 + RawContacts.NAME_VERIFIED + "," 1550 + RawContactsColumns.CONCRETE_VERSION + " AS " + RawContacts.VERSION + "," 1551 + RawContactsColumns.CONCRETE_DIRTY + " AS " + RawContacts.DIRTY + "," 1552 + RawContactsColumns.CONCRETE_SYNC1 + " AS " + RawContacts.SYNC1 + "," 1553 + RawContactsColumns.CONCRETE_SYNC2 + " AS " + RawContacts.SYNC2 + "," 1554 + RawContactsColumns.CONCRETE_SYNC3 + " AS " + RawContacts.SYNC3 + "," 1555 + RawContactsColumns.CONCRETE_SYNC4 + " AS " + RawContacts.SYNC4; 1556 1557 String baseContactColumns = 1558 Contacts.HAS_PHONE_NUMBER + ", " 1559 + Contacts.NAME_RAW_CONTACT_ID + ", " 1560 + Contacts.LOOKUP_KEY + ", " 1561 + Contacts.PHOTO_ID + ", " 1562 + Contacts.PHOTO_FILE_ID + ", " 1563 + "CAST(" + Clauses.CONTACT_VISIBLE + " AS INTEGER) AS " 1564 + Contacts.IN_VISIBLE_GROUP + ", " 1565 + ContactsColumns.LAST_STATUS_UPDATE_ID; 1566 1567 String contactOptionColumns = 1568 ContactsColumns.CONCRETE_CUSTOM_RINGTONE 1569 + " AS " + RawContacts.CUSTOM_RINGTONE + "," 1570 + ContactsColumns.CONCRETE_SEND_TO_VOICEMAIL 1571 + " AS " + RawContacts.SEND_TO_VOICEMAIL + "," 1572 + ContactsColumns.CONCRETE_LAST_TIME_CONTACTED 1573 + " AS " + RawContacts.LAST_TIME_CONTACTED + "," 1574 + ContactsColumns.CONCRETE_TIMES_CONTACTED 1575 + " AS " + RawContacts.TIMES_CONTACTED + "," 1576 + ContactsColumns.CONCRETE_STARRED 1577 + " AS " + RawContacts.STARRED; 1578 1579 String contactNameColumns = 1580 "name_raw_contact." + RawContacts.DISPLAY_NAME_SOURCE 1581 + " AS " + Contacts.DISPLAY_NAME_SOURCE + ", " 1582 + "name_raw_contact." + RawContacts.DISPLAY_NAME_PRIMARY 1583 + " AS " + Contacts.DISPLAY_NAME_PRIMARY + ", " 1584 + "name_raw_contact." + RawContacts.DISPLAY_NAME_ALTERNATIVE 1585 + " AS " + Contacts.DISPLAY_NAME_ALTERNATIVE + ", " 1586 + "name_raw_contact." + RawContacts.PHONETIC_NAME 1587 + " AS " + Contacts.PHONETIC_NAME + ", " 1588 + "name_raw_contact." + RawContacts.PHONETIC_NAME_STYLE 1589 + " AS " + Contacts.PHONETIC_NAME_STYLE + ", " 1590 + "name_raw_contact." + RawContacts.SORT_KEY_PRIMARY 1591 + " AS " + Contacts.SORT_KEY_PRIMARY + ", " 1592 + "name_raw_contact." + RawContacts.SORT_KEY_ALTERNATIVE 1593 + " AS " + Contacts.SORT_KEY_ALTERNATIVE; 1594 1595 String dataSelect = "SELECT " 1596 + DataColumns.CONCRETE_ID + " AS " + Data._ID + "," 1597 + Data.RAW_CONTACT_ID + ", " 1598 + RawContactsColumns.CONCRETE_CONTACT_ID + " AS " + RawContacts.CONTACT_ID + ", " 1599 + syncColumns + ", " 1600 + dataColumns + ", " 1601 + contactOptionColumns + ", " 1602 + contactNameColumns + ", " 1603 + baseContactColumns + ", " 1604 + buildDisplayPhotoUriAlias(RawContactsColumns.CONCRETE_CONTACT_ID, 1605 Contacts.PHOTO_URI) + ", " 1606 + buildThumbnailPhotoUriAlias(RawContactsColumns.CONCRETE_CONTACT_ID, 1607 Contacts.PHOTO_THUMBNAIL_URI) + ", " 1608 + dbForProfile() + " AS " + RawContacts.RAW_CONTACT_IS_USER_PROFILE + ", " 1609 + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID 1610 + " FROM " + Tables.DATA 1611 + " JOIN " + Tables.MIMETYPES + " ON (" 1612 + DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")" 1613 + " JOIN " + Tables.RAW_CONTACTS + " ON (" 1614 + DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")" 1615 + " JOIN " + Tables.CONTACTS + " ON (" 1616 + RawContactsColumns.CONCRETE_CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + ")" 1617 + " JOIN " + Tables.RAW_CONTACTS + " AS name_raw_contact ON(" 1618 + Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")" 1619 + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON (" 1620 + DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")" 1621 + " LEFT OUTER JOIN " + Tables.GROUPS + " ON (" 1622 + MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE 1623 + "' AND " + GroupsColumns.CONCRETE_ID + "=" 1624 + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")"; 1625 1626 db.execSQL("CREATE VIEW " + Views.DATA + " AS " + dataSelect); 1627 1628 String rawContactOptionColumns = 1629 RawContacts.CUSTOM_RINGTONE + "," 1630 + RawContacts.SEND_TO_VOICEMAIL + "," 1631 + RawContacts.LAST_TIME_CONTACTED + "," 1632 + RawContacts.TIMES_CONTACTED + "," 1633 + RawContacts.STARRED; 1634 1635 String rawContactsSelect = "SELECT " 1636 + RawContactsColumns.CONCRETE_ID + " AS " + RawContacts._ID + "," 1637 + RawContacts.CONTACT_ID + ", " 1638 + RawContacts.AGGREGATION_MODE + ", " 1639 + RawContacts.RAW_CONTACT_IS_READ_ONLY + ", " 1640 + RawContacts.DELETED + ", " 1641 + RawContacts.DISPLAY_NAME_SOURCE + ", " 1642 + RawContacts.DISPLAY_NAME_PRIMARY + ", " 1643 + RawContacts.DISPLAY_NAME_ALTERNATIVE + ", " 1644 + RawContacts.PHONETIC_NAME + ", " 1645 + RawContacts.PHONETIC_NAME_STYLE + ", " 1646 + RawContacts.SORT_KEY_PRIMARY + ", " 1647 + RawContacts.SORT_KEY_ALTERNATIVE + ", " 1648 + dbForProfile() + " AS " + RawContacts.RAW_CONTACT_IS_USER_PROFILE + ", " 1649 + rawContactOptionColumns + ", " 1650 + syncColumns 1651 + " FROM " + Tables.RAW_CONTACTS; 1652 1653 db.execSQL("CREATE VIEW " + Views.RAW_CONTACTS + " AS " + rawContactsSelect); 1654 1655 String contactsColumns = 1656 ContactsColumns.CONCRETE_CUSTOM_RINGTONE 1657 + " AS " + Contacts.CUSTOM_RINGTONE + ", " 1658 + contactNameColumns + ", " 1659 + baseContactColumns + ", " 1660 + ContactsColumns.CONCRETE_LAST_TIME_CONTACTED 1661 + " AS " + Contacts.LAST_TIME_CONTACTED + ", " 1662 + ContactsColumns.CONCRETE_SEND_TO_VOICEMAIL 1663 + " AS " + Contacts.SEND_TO_VOICEMAIL + ", " 1664 + ContactsColumns.CONCRETE_STARRED 1665 + " AS " + Contacts.STARRED + ", " 1666 + ContactsColumns.CONCRETE_TIMES_CONTACTED 1667 + " AS " + Contacts.TIMES_CONTACTED; 1668 1669 String contactsSelect = "SELECT " 1670 + ContactsColumns.CONCRETE_ID + " AS " + Contacts._ID + "," 1671 + contactsColumns + ", " 1672 + buildDisplayPhotoUriAlias(ContactsColumns.CONCRETE_ID, Contacts.PHOTO_URI) + ", " 1673 + buildThumbnailPhotoUriAlias(ContactsColumns.CONCRETE_ID, 1674 Contacts.PHOTO_THUMBNAIL_URI) + ", " 1675 + dbForProfile() + " AS " + Contacts.IS_USER_PROFILE 1676 + " FROM " + Tables.CONTACTS 1677 + " JOIN " + Tables.RAW_CONTACTS + " AS name_raw_contact ON(" 1678 + Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")"; 1679 1680 db.execSQL("CREATE VIEW " + Views.CONTACTS + " AS " + contactsSelect); 1681 1682 String rawEntitiesSelect = "SELECT " 1683 + RawContacts.CONTACT_ID + ", " 1684 + RawContactsColumns.CONCRETE_DELETED + " AS " + RawContacts.DELETED + "," 1685 + dataColumns + ", " 1686 + syncColumns + ", " 1687 + Data.SYNC1 + ", " 1688 + Data.SYNC2 + ", " 1689 + Data.SYNC3 + ", " 1690 + Data.SYNC4 + ", " 1691 + RawContactsColumns.CONCRETE_ID + " AS " + RawContacts._ID + ", " 1692 + DataColumns.CONCRETE_ID + " AS " + RawContacts.Entity.DATA_ID + "," 1693 + RawContactsColumns.CONCRETE_STARRED + " AS " + RawContacts.STARRED + "," 1694 + dbForProfile() + " AS " + RawContacts.RAW_CONTACT_IS_USER_PROFILE + "," 1695 + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID 1696 + " FROM " + Tables.RAW_CONTACTS 1697 + " LEFT OUTER JOIN " + Tables.DATA + " ON (" 1698 + DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")" 1699 + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON (" 1700 + DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")" 1701 + " LEFT OUTER JOIN " + Tables.MIMETYPES + " ON (" 1702 + DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")" 1703 + " LEFT OUTER JOIN " + Tables.GROUPS + " ON (" 1704 + MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE 1705 + "' AND " + GroupsColumns.CONCRETE_ID + "=" 1706 + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")"; 1707 1708 db.execSQL("CREATE VIEW " + Views.RAW_ENTITIES + " AS " 1709 + rawEntitiesSelect); 1710 1711 String entitiesSelect = "SELECT " 1712 + RawContactsColumns.CONCRETE_CONTACT_ID + " AS " + Contacts._ID + ", " 1713 + RawContactsColumns.CONCRETE_CONTACT_ID + " AS " + RawContacts.CONTACT_ID + ", " 1714 + RawContactsColumns.CONCRETE_DELETED + " AS " + RawContacts.DELETED + "," 1715 + dataColumns + ", " 1716 + syncColumns + ", " 1717 + contactsColumns + ", " 1718 + buildDisplayPhotoUriAlias(RawContactsColumns.CONCRETE_CONTACT_ID, 1719 Contacts.PHOTO_URI) + ", " 1720 + buildThumbnailPhotoUriAlias(RawContactsColumns.CONCRETE_CONTACT_ID, 1721 Contacts.PHOTO_THUMBNAIL_URI) + ", " 1722 + dbForProfile() + " AS " + Contacts.IS_USER_PROFILE + ", " 1723 + Data.SYNC1 + ", " 1724 + Data.SYNC2 + ", " 1725 + Data.SYNC3 + ", " 1726 + Data.SYNC4 + ", " 1727 + RawContactsColumns.CONCRETE_ID + " AS " + Contacts.Entity.RAW_CONTACT_ID + ", " 1728 + DataColumns.CONCRETE_ID + " AS " + Contacts.Entity.DATA_ID + "," 1729 + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID 1730 + " FROM " + Tables.RAW_CONTACTS 1731 + " JOIN " + Tables.CONTACTS + " ON (" 1732 + RawContactsColumns.CONCRETE_CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + ")" 1733 + " JOIN " + Tables.RAW_CONTACTS + " AS name_raw_contact ON(" 1734 + Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")" 1735 + " LEFT OUTER JOIN " + Tables.DATA + " ON (" 1736 + DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")" 1737 + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON (" 1738 + DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")" 1739 + " LEFT OUTER JOIN " + Tables.MIMETYPES + " ON (" 1740 + DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")" 1741 + " LEFT OUTER JOIN " + Tables.GROUPS + " ON (" 1742 + MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE 1743 + "' AND " + GroupsColumns.CONCRETE_ID + "=" 1744 + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")"; 1745 1746 db.execSQL("CREATE VIEW " + Views.ENTITIES + " AS " 1747 + entitiesSelect); 1748 1749 String dataUsageStatSelect = "SELECT " 1750 + DataUsageStatColumns.CONCRETE_ID + " AS " + DataUsageStatColumns._ID + ", " 1751 + DataUsageStatColumns.DATA_ID + ", " 1752 + RawContactsColumns.CONCRETE_CONTACT_ID + " AS " + RawContacts.CONTACT_ID + ", " 1753 + MimetypesColumns.CONCRETE_MIMETYPE + " AS " + Data.MIMETYPE + ", " 1754 + DataUsageStatColumns.USAGE_TYPE_INT + ", " 1755 + DataUsageStatColumns.TIMES_USED + ", " 1756 + DataUsageStatColumns.LAST_TIME_USED 1757 + " FROM " + Tables.DATA_USAGE_STAT 1758 + " JOIN " + Tables.DATA + " ON (" 1759 + DataColumns.CONCRETE_ID + "=" + DataUsageStatColumns.CONCRETE_DATA_ID + ")" 1760 + " JOIN " + Tables.RAW_CONTACTS + " ON (" 1761 + RawContactsColumns.CONCRETE_ID + "=" + DataColumns.CONCRETE_RAW_CONTACT_ID 1762 + " )" 1763 + " JOIN " + Tables.MIMETYPES + " ON (" 1764 + MimetypesColumns.CONCRETE_ID + "=" + DataColumns.CONCRETE_MIMETYPE_ID + ")"; 1765 1766 db.execSQL("CREATE VIEW " + Views.DATA_USAGE_STAT + " AS " + dataUsageStatSelect); 1767 1768 String streamItemSelect = "SELECT " + 1769 StreamItemsColumns.CONCRETE_ID + ", " + 1770 ContactsColumns.CONCRETE_ID + " AS " + StreamItems.CONTACT_ID + ", " + 1771 ContactsColumns.CONCRETE_LOOKUP_KEY + 1772 " AS " + StreamItems.CONTACT_LOOKUP_KEY + ", " + 1773 RawContactsColumns.CONCRETE_ACCOUNT_NAME + ", " + 1774 RawContactsColumns.CONCRETE_ACCOUNT_TYPE + ", " + 1775 RawContactsColumns.CONCRETE_DATA_SET + ", " + 1776 StreamItemsColumns.CONCRETE_RAW_CONTACT_ID + 1777 " as " + StreamItems.RAW_CONTACT_ID + ", " + 1778 RawContactsColumns.CONCRETE_SOURCE_ID + 1779 " as " + StreamItems.RAW_CONTACT_SOURCE_ID + ", " + 1780 StreamItemsColumns.CONCRETE_PACKAGE + ", " + 1781 StreamItemsColumns.CONCRETE_ICON + ", " + 1782 StreamItemsColumns.CONCRETE_LABEL + ", " + 1783 StreamItemsColumns.CONCRETE_TEXT + ", " + 1784 StreamItemsColumns.CONCRETE_TIMESTAMP + ", " + 1785 StreamItemsColumns.CONCRETE_COMMENTS + ", " + 1786 StreamItemsColumns.CONCRETE_SYNC1 + ", " + 1787 StreamItemsColumns.CONCRETE_SYNC2 + ", " + 1788 StreamItemsColumns.CONCRETE_SYNC3 + ", " + 1789 StreamItemsColumns.CONCRETE_SYNC4 + 1790 " FROM " + Tables.STREAM_ITEMS 1791 + " JOIN " + Tables.RAW_CONTACTS + " ON (" 1792 + StreamItemsColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID 1793 + ") JOIN " + Tables.CONTACTS + " ON (" 1794 + RawContactsColumns.CONCRETE_CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + ")"; 1795 1796 db.execSQL("CREATE VIEW " + Views.STREAM_ITEMS + " AS " + streamItemSelect); 1797 } 1798 1799 private static String buildDisplayPhotoUriAlias(String contactIdColumn, String alias) { 1800 return "(CASE WHEN " + Contacts.PHOTO_FILE_ID + " IS NULL THEN (CASE WHEN " 1801 + Contacts.PHOTO_ID + " IS NULL" 1802 + " OR " + Contacts.PHOTO_ID + "=0" 1803 + " THEN NULL" 1804 + " ELSE '" + Contacts.CONTENT_URI + "/'||" 1805 + contactIdColumn + "|| '/" + Photo.CONTENT_DIRECTORY + "'" 1806 + " END) ELSE '" + DisplayPhoto.CONTENT_URI + "/'||" 1807 + Contacts.PHOTO_FILE_ID + " END)" 1808 + " AS " + alias; 1809 } 1810 1811 private static String buildThumbnailPhotoUriAlias(String contactIdColumn, String alias) { 1812 return "(CASE WHEN " 1813 + Contacts.PHOTO_ID + " IS NULL" 1814 + " OR " + Contacts.PHOTO_ID + "=0" 1815 + " THEN NULL" 1816 + " ELSE '" + Contacts.CONTENT_URI + "/'||" 1817 + contactIdColumn + "|| '/" + Photo.CONTENT_DIRECTORY + "'" 1818 + " END)" 1819 + " AS " + alias; 1820 } 1821 1822 /** 1823 * Returns the value to be returned when querying the column indicating that the contact 1824 * or raw contact belongs to the user's personal profile. Overridden in the profile 1825 * DB helper subclass. 1826 */ 1827 protected int dbForProfile() { 1828 return 0; 1829 } 1830 1831 private void createGroupsView(SQLiteDatabase db) { 1832 db.execSQL("DROP VIEW IF EXISTS " + Views.GROUPS + ";"); 1833 String groupsColumns = 1834 Groups.ACCOUNT_NAME + "," 1835 + Groups.ACCOUNT_TYPE + "," 1836 + Groups.DATA_SET + "," 1837 + "(CASE WHEN " + Groups.DATA_SET + " IS NULL THEN " + Groups.ACCOUNT_TYPE 1838 + " ELSE " + Groups.ACCOUNT_TYPE + "||" + Groups.DATA_SET + " END) AS " 1839 + Groups.ACCOUNT_TYPE_AND_DATA_SET + "," 1840 + Groups.SOURCE_ID + "," 1841 + Groups.VERSION + "," 1842 + Groups.DIRTY + "," 1843 + Groups.TITLE + "," 1844 + Groups.TITLE_RES + "," 1845 + Groups.NOTES + "," 1846 + Groups.SYSTEM_ID + "," 1847 + Groups.DELETED + "," 1848 + Groups.GROUP_VISIBLE + "," 1849 + Groups.SHOULD_SYNC + "," 1850 + Groups.AUTO_ADD + "," 1851 + Groups.FAVORITES + "," 1852 + Groups.GROUP_IS_READ_ONLY + "," 1853 + Groups.SYNC1 + "," 1854 + Groups.SYNC2 + "," 1855 + Groups.SYNC3 + "," 1856 + Groups.SYNC4 + "," 1857 + PackagesColumns.PACKAGE + " AS " + Groups.RES_PACKAGE; 1858 1859 String groupsSelect = "SELECT " 1860 + GroupsColumns.CONCRETE_ID + " AS " + Groups._ID + "," 1861 + groupsColumns 1862 + " FROM " + Tables.GROUPS_JOIN_PACKAGES; 1863 1864 db.execSQL("CREATE VIEW " + Views.GROUPS + " AS " + groupsSelect); 1865 } 1866 1867 @Override 1868 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 1869 if (oldVersion < 99) { 1870 Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion 1871 + ", data will be lost!"); 1872 1873 db.execSQL("DROP TABLE IF EXISTS " + Tables.CONTACTS + ";"); 1874 db.execSQL("DROP TABLE IF EXISTS " + Tables.RAW_CONTACTS + ";"); 1875 db.execSQL("DROP TABLE IF EXISTS " + Tables.PACKAGES + ";"); 1876 db.execSQL("DROP TABLE IF EXISTS " + Tables.MIMETYPES + ";"); 1877 db.execSQL("DROP TABLE IF EXISTS " + Tables.DATA + ";"); 1878 db.execSQL("DROP TABLE IF EXISTS " + Tables.PHONE_LOOKUP + ";"); 1879 db.execSQL("DROP TABLE IF EXISTS " + Tables.NAME_LOOKUP + ";"); 1880 db.execSQL("DROP TABLE IF EXISTS " + Tables.NICKNAME_LOOKUP + ";"); 1881 db.execSQL("DROP TABLE IF EXISTS " + Tables.GROUPS + ";"); 1882 db.execSQL("DROP TABLE IF EXISTS " + Tables.ACTIVITIES + ";"); 1883 db.execSQL("DROP TABLE IF EXISTS " + Tables.CALLS + ";"); 1884 db.execSQL("DROP TABLE IF EXISTS " + Tables.SETTINGS + ";"); 1885 db.execSQL("DROP TABLE IF EXISTS " + Tables.STATUS_UPDATES + ";"); 1886 1887 // TODO: we should not be dropping agg_exceptions and contact_options. In case that 1888 // table's schema changes, we should try to preserve the data, because it was entered 1889 // by the user and has never been synched to the server. 1890 db.execSQL("DROP TABLE IF EXISTS " + Tables.AGGREGATION_EXCEPTIONS + ";"); 1891 1892 onCreate(db); 1893 return; 1894 } 1895 1896 Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion); 1897 1898 boolean upgradeViewsAndTriggers = false; 1899 boolean upgradeNameLookup = false; 1900 boolean upgradeLegacyApiSupport = false; 1901 boolean upgradeSearchIndex = false; 1902 1903 if (oldVersion == 99) { 1904 upgradeViewsAndTriggers = true; 1905 oldVersion++; 1906 } 1907 1908 if (oldVersion == 100) { 1909 db.execSQL("CREATE INDEX IF NOT EXISTS mimetypes_mimetype_index ON " 1910 + Tables.MIMETYPES + " (" 1911 + MimetypesColumns.MIMETYPE + "," 1912 + MimetypesColumns._ID + ");"); 1913 updateIndexStats(db, Tables.MIMETYPES, 1914 "mimetypes_mimetype_index", "50 1 1"); 1915 1916 upgradeViewsAndTriggers = true; 1917 oldVersion++; 1918 } 1919 1920 if (oldVersion == 101) { 1921 upgradeViewsAndTriggers = true; 1922 oldVersion++; 1923 } 1924 1925 if (oldVersion == 102) { 1926 upgradeViewsAndTriggers = true; 1927 oldVersion++; 1928 } 1929 1930 if (oldVersion == 103) { 1931 upgradeViewsAndTriggers = true; 1932 oldVersion++; 1933 } 1934 1935 if (oldVersion == 104 || oldVersion == 201) { 1936 LegacyApiSupport.createSettingsTable(db); 1937 upgradeViewsAndTriggers = true; 1938 oldVersion++; 1939 } 1940 1941 if (oldVersion == 105) { 1942 upgradeToVersion202(db); 1943 upgradeNameLookup = true; 1944 oldVersion = 202; 1945 } 1946 1947 if (oldVersion == 202) { 1948 upgradeToVersion203(db); 1949 upgradeViewsAndTriggers = true; 1950 oldVersion++; 1951 } 1952 1953 if (oldVersion == 203) { 1954 upgradeViewsAndTriggers = true; 1955 oldVersion++; 1956 } 1957 1958 if (oldVersion == 204) { 1959 upgradeToVersion205(db); 1960 upgradeViewsAndTriggers = true; 1961 oldVersion++; 1962 } 1963 1964 if (oldVersion == 205) { 1965 upgrateToVersion206(db); 1966 upgradeViewsAndTriggers = true; 1967 oldVersion++; 1968 } 1969 1970 if (oldVersion == 206) { 1971 upgradeToVersion300(db); 1972 oldVersion = 300; 1973 } 1974 1975 if (oldVersion == 300) { 1976 upgradeViewsAndTriggers = true; 1977 oldVersion = 301; 1978 } 1979 1980 if (oldVersion == 301) { 1981 upgradeViewsAndTriggers = true; 1982 oldVersion = 302; 1983 } 1984 1985 if (oldVersion == 302) { 1986 upgradeEmailToVersion303(db); 1987 upgradeNicknameToVersion303(db); 1988 oldVersion = 303; 1989 } 1990 1991 if (oldVersion == 303) { 1992 upgradeToVersion304(db); 1993 oldVersion = 304; 1994 } 1995 1996 if (oldVersion == 304) { 1997 upgradeNameLookup = true; 1998 oldVersion = 305; 1999 } 2000 2001 if (oldVersion == 305) { 2002 upgradeToVersion306(db); 2003 oldVersion = 306; 2004 } 2005 2006 if (oldVersion == 306) { 2007 upgradeToVersion307(db); 2008 oldVersion = 307; 2009 } 2010 2011 if (oldVersion == 307) { 2012 upgradeToVersion308(db); 2013 oldVersion = 308; 2014 } 2015 2016 // Gingerbread upgrades 2017 if (oldVersion < 350) { 2018 upgradeViewsAndTriggers = true; 2019 oldVersion = 351; 2020 } 2021 2022 if (oldVersion == 351) { 2023 upgradeNameLookup = true; 2024 oldVersion = 352; 2025 } 2026 2027 if (oldVersion == 352) { 2028 upgradeToVersion353(db); 2029 oldVersion = 353; 2030 } 2031 2032 // Honeycomb upgrades 2033 if (oldVersion < 400) { 2034 upgradeViewsAndTriggers = true; 2035 upgradeToVersion400(db); 2036 oldVersion = 400; 2037 } 2038 2039 if (oldVersion == 400) { 2040 upgradeViewsAndTriggers = true; 2041 upgradeToVersion401(db); 2042 oldVersion = 401; 2043 } 2044 2045 if (oldVersion == 401) { 2046 upgradeToVersion402(db); 2047 oldVersion = 402; 2048 } 2049 2050 if (oldVersion == 402) { 2051 upgradeViewsAndTriggers = true; 2052 upgradeToVersion403(db); 2053 oldVersion = 403; 2054 } 2055 2056 if (oldVersion == 403) { 2057 upgradeViewsAndTriggers = true; 2058 oldVersion = 404; 2059 } 2060 2061 if (oldVersion == 404) { 2062 upgradeViewsAndTriggers = true; 2063 upgradeToVersion405(db); 2064 oldVersion = 405; 2065 } 2066 2067 if (oldVersion == 405) { 2068 upgradeViewsAndTriggers = true; 2069 upgradeToVersion406(db); 2070 oldVersion = 406; 2071 } 2072 2073 if (oldVersion == 406) { 2074 upgradeViewsAndTriggers = true; 2075 oldVersion = 407; 2076 } 2077 2078 if (oldVersion == 407) { 2079 // Obsolete 2080 oldVersion = 408; 2081 } 2082 2083 if (oldVersion == 408) { 2084 upgradeViewsAndTriggers = true; 2085 upgradeToVersion409(db); 2086 oldVersion = 409; 2087 } 2088 2089 if (oldVersion == 409) { 2090 upgradeViewsAndTriggers = true; 2091 oldVersion = 410; 2092 } 2093 2094 if (oldVersion == 410) { 2095 upgradeToVersion411(db); 2096 oldVersion = 411; 2097 } 2098 2099 if (oldVersion == 411) { 2100 // Same upgrade as 353, only on Honeycomb devices 2101 upgradeToVersion353(db); 2102 oldVersion = 412; 2103 } 2104 2105 if (oldVersion == 412) { 2106 upgradeToVersion413(db); 2107 oldVersion = 413; 2108 } 2109 2110 if (oldVersion == 413) { 2111 upgradeNameLookup = true; 2112 oldVersion = 414; 2113 } 2114 2115 if (oldVersion == 414) { 2116 upgradeToVersion415(db); 2117 upgradeViewsAndTriggers = true; 2118 oldVersion = 415; 2119 } 2120 2121 if (oldVersion == 415) { 2122 upgradeToVersion416(db); 2123 oldVersion = 416; 2124 } 2125 2126 if (oldVersion == 416) { 2127 upgradeLegacyApiSupport = true; 2128 oldVersion = 417; 2129 } 2130 2131 // Honeycomb-MR1 upgrades 2132 if (oldVersion < 500) { 2133 upgradeSearchIndex = true; 2134 } 2135 2136 if (oldVersion < 501) { 2137 upgradeSearchIndex = true; 2138 upgradeToVersion501(db); 2139 oldVersion = 501; 2140 } 2141 2142 if (oldVersion < 502) { 2143 upgradeSearchIndex = true; 2144 upgradeToVersion502(db); 2145 oldVersion = 502; 2146 } 2147 2148 if (oldVersion < 503) { 2149 upgradeSearchIndex = true; 2150 oldVersion = 503; 2151 } 2152 2153 if (oldVersion < 504) { 2154 upgradeToVersion504(db); 2155 oldVersion = 504; 2156 } 2157 2158 if (oldVersion < 600) { 2159 upgradeToVersion600(db); 2160 upgradeViewsAndTriggers = true; 2161 oldVersion = 600; 2162 } 2163 2164 if (oldVersion < 601) { 2165 upgradeToVersion601(db); 2166 oldVersion = 601; 2167 } 2168 2169 if (oldVersion < 602) { 2170 upgradeToVersion602(db); 2171 oldVersion = 602; 2172 } 2173 2174 if (oldVersion < 603) { 2175 upgradeViewsAndTriggers = true; 2176 oldVersion = 603; 2177 } 2178 2179 if (oldVersion < 604) { 2180 upgradeToVersion604(db); 2181 oldVersion = 604; 2182 } 2183 2184 if (oldVersion < 605) { 2185 upgradeViewsAndTriggers = true; 2186 upgradeToVersion605(db); 2187 oldVersion = 605; 2188 } 2189 2190 if (oldVersion < 606) { 2191 upgradeViewsAndTriggers = true; 2192 upgradeLegacyApiSupport = true; 2193 upgradeToVersion606(db); 2194 oldVersion = 606; 2195 } 2196 2197 if (oldVersion < 607) { 2198 upgradeViewsAndTriggers = true; 2199 upgradeToVersion607(db); 2200 oldVersion = 607; 2201 } 2202 2203 if (oldVersion < 608) { 2204 upgradeViewsAndTriggers = true; 2205 upgradeToVersion608(db); 2206 oldVersion = 608; 2207 } 2208 2209 if (oldVersion < 609) { 2210 upgradeToVersion609(db); 2211 oldVersion = 609; 2212 } 2213 2214 if (oldVersion < 610) { 2215 upgradeToVersion610(db); 2216 oldVersion = 610; 2217 } 2218 2219 if (oldVersion < 611) { 2220 upgradeViewsAndTriggers = true; 2221 upgradeToVersion611(db); 2222 oldVersion = 611; 2223 } 2224 2225 if (oldVersion < 612) { 2226 upgradeViewsAndTriggers = true; 2227 upgradeToVersion612(db); 2228 oldVersion = 612; 2229 } 2230 2231 if (oldVersion < 613) { 2232 upgradeToVersion613(db); 2233 oldVersion = 613; 2234 } 2235 2236 if (oldVersion < 614) { 2237 // this creates the view "view_stream_items" 2238 upgradeViewsAndTriggers = true; 2239 oldVersion = 614; 2240 } 2241 2242 if (oldVersion < 615) { 2243 upgradeToVersion615(db); 2244 oldVersion = 615; 2245 } 2246 2247 if (oldVersion < 616) { 2248 // this updates the "view_stream_items" view 2249 upgradeViewsAndTriggers = true; 2250 oldVersion = 616; 2251 } 2252 2253 if (oldVersion < 617) { 2254 // This version upgrade obsoleted the profile_raw_contact_id field of the Accounts 2255 // table, but we aren't removing the column because it is very little data (and not 2256 // referenced anymore). We do need to upgrade the views to handle the simplified 2257 // per-database "is profile" columns. 2258 upgradeViewsAndTriggers = true; 2259 oldVersion = 617; 2260 } 2261 2262 if (oldVersion < 618) { 2263 upgradeToVersion618(db); 2264 oldVersion = 618; 2265 } 2266 2267 if (oldVersion < 619) { 2268 upgradeViewsAndTriggers = true; 2269 oldVersion = 619; 2270 } 2271 2272 if (oldVersion < 620) { 2273 upgradeViewsAndTriggers = true; 2274 oldVersion = 620; 2275 } 2276 2277 if (oldVersion < 621) { 2278 upgradeSearchIndex = true; 2279 oldVersion = 621; 2280 } 2281 2282 if (oldVersion < 622) { 2283 upgradeToVersion622(db); 2284 oldVersion = 622; 2285 } 2286 2287 if (oldVersion < 623) { 2288 // change FTS to normalize names using collation key 2289 upgradeSearchIndex = true; 2290 oldVersion = 623; 2291 } 2292 2293 if (upgradeViewsAndTriggers) { 2294 createContactsViews(db); 2295 createGroupsView(db); 2296 createContactsTriggers(db); 2297 createContactsIndexes(db); 2298 updateSqliteStats(db); 2299 upgradeLegacyApiSupport = true; 2300 mReopenDatabase = true; 2301 } 2302 2303 if (upgradeLegacyApiSupport) { 2304 LegacyApiSupport.createViews(db); 2305 } 2306 2307 if (upgradeNameLookup) { 2308 rebuildNameLookup(db); 2309 } 2310 2311 if (upgradeSearchIndex) { 2312 createSearchIndexTable(db); 2313 setProperty(db, SearchIndexManager.PROPERTY_SEARCH_INDEX_VERSION, "0"); 2314 } 2315 2316 if (oldVersion != newVersion) { 2317 throw new IllegalStateException( 2318 "error upgrading the database to version " + newVersion); 2319 } 2320 } 2321 2322 private void upgradeToVersion202(SQLiteDatabase db) { 2323 db.execSQL( 2324 "ALTER TABLE " + Tables.PHONE_LOOKUP + 2325 " ADD " + PhoneLookupColumns.MIN_MATCH + " TEXT;"); 2326 2327 db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" + 2328 PhoneLookupColumns.MIN_MATCH + "," + 2329 PhoneLookupColumns.RAW_CONTACT_ID + "," + 2330 PhoneLookupColumns.DATA_ID + 2331 ");"); 2332 2333 updateIndexStats(db, Tables.PHONE_LOOKUP, 2334 "phone_lookup_min_match_index", "10000 2 2 1"); 2335 2336 SQLiteStatement update = db.compileStatement( 2337 "UPDATE " + Tables.PHONE_LOOKUP + 2338 " SET " + PhoneLookupColumns.MIN_MATCH + "=?" + 2339 " WHERE " + PhoneLookupColumns.DATA_ID + "=?"); 2340 2341 // Populate the new column 2342 Cursor c = db.query(Tables.PHONE_LOOKUP + " JOIN " + Tables.DATA + 2343 " ON (" + PhoneLookupColumns.DATA_ID + "=" + DataColumns.CONCRETE_ID + ")", 2344 new String[]{Data._ID, Phone.NUMBER}, null, null, null, null, null); 2345 try { 2346 while (c.moveToNext()) { 2347 long dataId = c.getLong(0); 2348 String number = c.getString(1); 2349 if (!TextUtils.isEmpty(number)) { 2350 update.bindString(1, PhoneNumberUtils.toCallerIDMinMatch(number)); 2351 update.bindLong(2, dataId); 2352 update.execute(); 2353 } 2354 } 2355 } finally { 2356 c.close(); 2357 } 2358 } 2359 2360 private void upgradeToVersion203(SQLiteDatabase db) { 2361 // Garbage-collect first. A bug in Eclair was sometimes leaving 2362 // raw_contacts in the database that no longer had contacts associated 2363 // with them. To avoid failures during this database upgrade, drop 2364 // the orphaned raw_contacts. 2365 db.execSQL( 2366 "DELETE FROM raw_contacts" + 2367 " WHERE contact_id NOT NULL" + 2368 " AND contact_id NOT IN (SELECT _id FROM contacts)"); 2369 2370 db.execSQL( 2371 "ALTER TABLE " + Tables.CONTACTS + 2372 " ADD " + Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)"); 2373 db.execSQL( 2374 "ALTER TABLE " + Tables.RAW_CONTACTS + 2375 " ADD contact_in_visible_group INTEGER NOT NULL DEFAULT 0"); 2376 2377 // For each Contact, find the RawContact that contributed the display name 2378 db.execSQL( 2379 "UPDATE " + Tables.CONTACTS + 2380 " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" + 2381 " SELECT " + RawContacts._ID + 2382 " FROM " + Tables.RAW_CONTACTS + 2383 " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + 2384 " AND " + RawContactsColumns.CONCRETE_DISPLAY_NAME + "=" + 2385 Tables.CONTACTS + "." + Contacts.DISPLAY_NAME + 2386 " ORDER BY " + RawContacts._ID + 2387 " LIMIT 1)" 2388 ); 2389 2390 db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" + 2391 Contacts.NAME_RAW_CONTACT_ID + 2392 ");"); 2393 2394 // If for some unknown reason we missed some names, let's make sure there are 2395 // no contacts without a name, picking a raw contact "at random". 2396 db.execSQL( 2397 "UPDATE " + Tables.CONTACTS + 2398 " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" + 2399 " SELECT " + RawContacts._ID + 2400 " FROM " + Tables.RAW_CONTACTS + 2401 " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + 2402 " ORDER BY " + RawContacts._ID + 2403 " LIMIT 1)" + 2404 " WHERE " + Contacts.NAME_RAW_CONTACT_ID + " IS NULL" 2405 ); 2406 2407 // Wipe out DISPLAY_NAME on the Contacts table as it is no longer in use. 2408 db.execSQL( 2409 "UPDATE " + Tables.CONTACTS + 2410 " SET " + Contacts.DISPLAY_NAME + "=NULL" 2411 ); 2412 2413 // Copy the IN_VISIBLE_GROUP flag down to all raw contacts to allow 2414 // indexing on (display_name, in_visible_group) 2415 db.execSQL( 2416 "UPDATE " + Tables.RAW_CONTACTS + 2417 " SET contact_in_visible_group=(" + 2418 "SELECT " + Contacts.IN_VISIBLE_GROUP + 2419 " FROM " + Tables.CONTACTS + 2420 " WHERE " + Contacts._ID + "=" + RawContacts.CONTACT_ID + ")" + 2421 " WHERE " + RawContacts.CONTACT_ID + " NOT NULL" 2422 ); 2423 2424 db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" + 2425 "contact_in_visible_group" + "," + 2426 RawContactsColumns.DISPLAY_NAME + " COLLATE LOCALIZED ASC" + 2427 ");"); 2428 2429 db.execSQL("DROP INDEX contacts_visible_index"); 2430 db.execSQL("CREATE INDEX contacts_visible_index ON " + Tables.CONTACTS + " (" + 2431 Contacts.IN_VISIBLE_GROUP + 2432 ");"); 2433 } 2434 2435 private void upgradeToVersion205(SQLiteDatabase db) { 2436 db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS 2437 + " ADD " + RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT;"); 2438 db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS 2439 + " ADD " + RawContacts.PHONETIC_NAME + " TEXT;"); 2440 db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS 2441 + " ADD " + RawContacts.PHONETIC_NAME_STYLE + " INTEGER;"); 2442 db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS 2443 + " ADD " + RawContacts.SORT_KEY_PRIMARY 2444 + " TEXT COLLATE " + ContactsProvider2.PHONEBOOK_COLLATOR_NAME + ";"); 2445 db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS 2446 + " ADD " + RawContacts.SORT_KEY_ALTERNATIVE 2447 + " TEXT COLLATE " + ContactsProvider2.PHONEBOOK_COLLATOR_NAME + ";"); 2448 2449 final Locale locale = Locale.getDefault(); 2450 2451 NameSplitter splitter = createNameSplitter(); 2452 2453 SQLiteStatement rawContactUpdate = db.compileStatement( 2454 "UPDATE " + Tables.RAW_CONTACTS + 2455 " SET " + 2456 RawContacts.DISPLAY_NAME_PRIMARY + "=?," + 2457 RawContacts.DISPLAY_NAME_ALTERNATIVE + "=?," + 2458 RawContacts.PHONETIC_NAME + "=?," + 2459 RawContacts.PHONETIC_NAME_STYLE + "=?," + 2460 RawContacts.SORT_KEY_PRIMARY + "=?," + 2461 RawContacts.SORT_KEY_ALTERNATIVE + "=?" + 2462 " WHERE " + RawContacts._ID + "=?"); 2463 2464 upgradeStructuredNamesToVersion205(db, rawContactUpdate, splitter); 2465 upgradeOrganizationsToVersion205(db, rawContactUpdate, splitter); 2466 2467 db.execSQL("DROP INDEX raw_contact_sort_key1_index"); 2468 db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" + 2469 "contact_in_visible_group" + "," + 2470 RawContacts.SORT_KEY_PRIMARY + 2471 ");"); 2472 2473 db.execSQL("CREATE INDEX raw_contact_sort_key2_index ON " + Tables.RAW_CONTACTS + " (" + 2474 "contact_in_visible_group" + "," + 2475 RawContacts.SORT_KEY_ALTERNATIVE + 2476 ");"); 2477 } 2478 2479 private interface StructName205Query { 2480 String TABLE = Tables.DATA_JOIN_RAW_CONTACTS; 2481 2482 String COLUMNS[] = { 2483 DataColumns.CONCRETE_ID, 2484 Data.RAW_CONTACT_ID, 2485 RawContacts.DISPLAY_NAME_SOURCE, 2486 RawContacts.DISPLAY_NAME_PRIMARY, 2487 StructuredName.PREFIX, 2488 StructuredName.GIVEN_NAME, 2489 StructuredName.MIDDLE_NAME, 2490 StructuredName.FAMILY_NAME, 2491 StructuredName.SUFFIX, 2492 StructuredName.PHONETIC_FAMILY_NAME, 2493 StructuredName.PHONETIC_MIDDLE_NAME, 2494 StructuredName.PHONETIC_GIVEN_NAME, 2495 }; 2496 2497 int ID = 0; 2498 int RAW_CONTACT_ID = 1; 2499 int DISPLAY_NAME_SOURCE = 2; 2500 int DISPLAY_NAME = 3; 2501 int PREFIX = 4; 2502 int GIVEN_NAME = 5; 2503 int MIDDLE_NAME = 6; 2504 int FAMILY_NAME = 7; 2505 int SUFFIX = 8; 2506 int PHONETIC_FAMILY_NAME = 9; 2507 int PHONETIC_MIDDLE_NAME = 10; 2508 int PHONETIC_GIVEN_NAME = 11; 2509 } 2510 2511 private void upgradeStructuredNamesToVersion205(SQLiteDatabase db, 2512 SQLiteStatement rawContactUpdate, NameSplitter splitter) { 2513 2514 // Process structured names to detect the style of the full name and phonetic name 2515 2516 long mMimeType; 2517 try { 2518 mMimeType = DatabaseUtils.longForQuery(db, 2519 "SELECT " + MimetypesColumns._ID + 2520 " FROM " + Tables.MIMETYPES + 2521 " WHERE " + MimetypesColumns.MIMETYPE 2522 + "='" + StructuredName.CONTENT_ITEM_TYPE + "'", null); 2523 } catch (SQLiteDoneException e) { 2524 // No structured names in the database 2525 return; 2526 } 2527 2528 SQLiteStatement structuredNameUpdate = db.compileStatement( 2529 "UPDATE " + Tables.DATA + 2530 " SET " + 2531 StructuredName.FULL_NAME_STYLE + "=?," + 2532 StructuredName.DISPLAY_NAME + "=?," + 2533 StructuredName.PHONETIC_NAME_STYLE + "=?" + 2534 " WHERE " + Data._ID + "=?"); 2535 2536 NameSplitter.Name name = new NameSplitter.Name(); 2537 StringBuilder sb = new StringBuilder(); 2538 Cursor cursor = db.query(StructName205Query.TABLE, 2539 StructName205Query.COLUMNS, 2540 DataColumns.MIMETYPE_ID + "=" + mMimeType, null, null, null, null); 2541 try { 2542 while (cursor.moveToNext()) { 2543 long dataId = cursor.getLong(StructName205Query.ID); 2544 long rawContactId = cursor.getLong(StructName205Query.RAW_CONTACT_ID); 2545 int displayNameSource = cursor.getInt(StructName205Query.DISPLAY_NAME_SOURCE); 2546 String displayName = cursor.getString(StructName205Query.DISPLAY_NAME); 2547 2548 name.clear(); 2549 name.prefix = cursor.getString(StructName205Query.PREFIX); 2550 name.givenNames = cursor.getString(StructName205Query.GIVEN_NAME); 2551 name.middleName = cursor.getString(StructName205Query.MIDDLE_NAME); 2552 name.familyName = cursor.getString(StructName205Query.FAMILY_NAME); 2553 name.suffix = cursor.getString(StructName205Query.SUFFIX); 2554 name.phoneticFamilyName = cursor.getString(StructName205Query.PHONETIC_FAMILY_NAME); 2555 name.phoneticMiddleName = cursor.getString(StructName205Query.PHONETIC_MIDDLE_NAME); 2556 name.phoneticGivenName = cursor.getString(StructName205Query.PHONETIC_GIVEN_NAME); 2557 2558 upgradeNameToVersion205(dataId, rawContactId, displayNameSource, displayName, name, 2559 structuredNameUpdate, rawContactUpdate, splitter, sb); 2560 } 2561 } finally { 2562 cursor.close(); 2563 } 2564 } 2565 2566 private void upgradeNameToVersion205(long dataId, long rawContactId, int displayNameSource, 2567 String currentDisplayName, NameSplitter.Name name, 2568 SQLiteStatement structuredNameUpdate, SQLiteStatement rawContactUpdate, 2569 NameSplitter splitter, StringBuilder sb) { 2570 2571 splitter.guessNameStyle(name); 2572 int unadjustedFullNameStyle = name.fullNameStyle; 2573 name.fullNameStyle = splitter.getAdjustedFullNameStyle(name.fullNameStyle); 2574 String displayName = splitter.join(name, true, true); 2575 2576 // Don't update database with the adjusted fullNameStyle as it is locale 2577 // related 2578 structuredNameUpdate.bindLong(1, unadjustedFullNameStyle); 2579 DatabaseUtils.bindObjectToProgram(structuredNameUpdate, 2, displayName); 2580 structuredNameUpdate.bindLong(3, name.phoneticNameStyle); 2581 structuredNameUpdate.bindLong(4, dataId); 2582 structuredNameUpdate.execute(); 2583 2584 if (displayNameSource == DisplayNameSources.STRUCTURED_NAME) { 2585 String displayNameAlternative = splitter.join(name, false, false); 2586 String phoneticName = splitter.joinPhoneticName(name); 2587 String sortKey = null; 2588 String sortKeyAlternative = null; 2589 2590 if (phoneticName != null) { 2591 sortKey = sortKeyAlternative = phoneticName; 2592 } else if (name.fullNameStyle == FullNameStyle.CHINESE || 2593 name.fullNameStyle == FullNameStyle.CJK) { 2594 sortKey = sortKeyAlternative = ContactLocaleUtils.getIntance() 2595 .getSortKey(displayName, name.fullNameStyle); 2596 } 2597 2598 if (sortKey == null) { 2599 sortKey = displayName; 2600 sortKeyAlternative = displayNameAlternative; 2601 } 2602 2603 updateRawContact205(rawContactUpdate, rawContactId, displayName, 2604 displayNameAlternative, name.phoneticNameStyle, phoneticName, sortKey, 2605 sortKeyAlternative); 2606 } 2607 } 2608 2609 private interface Organization205Query { 2610 String TABLE = Tables.DATA_JOIN_RAW_CONTACTS; 2611 2612 String COLUMNS[] = { 2613 DataColumns.CONCRETE_ID, 2614 Data.RAW_CONTACT_ID, 2615 Organization.COMPANY, 2616 Organization.PHONETIC_NAME, 2617 }; 2618 2619 int ID = 0; 2620 int RAW_CONTACT_ID = 1; 2621 int COMPANY = 2; 2622 int PHONETIC_NAME = 3; 2623 } 2624 2625 private void upgradeOrganizationsToVersion205(SQLiteDatabase db, 2626 SQLiteStatement rawContactUpdate, NameSplitter splitter) { 2627 final long mimeType = lookupMimeTypeId(db, Organization.CONTENT_ITEM_TYPE); 2628 2629 SQLiteStatement organizationUpdate = db.compileStatement( 2630 "UPDATE " + Tables.DATA + 2631 " SET " + 2632 Organization.PHONETIC_NAME_STYLE + "=?" + 2633 " WHERE " + Data._ID + "=?"); 2634 2635 Cursor cursor = db.query(Organization205Query.TABLE, Organization205Query.COLUMNS, 2636 DataColumns.MIMETYPE_ID + "=" + mimeType + " AND " 2637 + RawContacts.DISPLAY_NAME_SOURCE + "=" + DisplayNameSources.ORGANIZATION, 2638 null, null, null, null); 2639 try { 2640 while (cursor.moveToNext()) { 2641 long dataId = cursor.getLong(Organization205Query.ID); 2642 long rawContactId = cursor.getLong(Organization205Query.RAW_CONTACT_ID); 2643 String company = cursor.getString(Organization205Query.COMPANY); 2644 String phoneticName = cursor.getString(Organization205Query.PHONETIC_NAME); 2645 2646 int phoneticNameStyle = splitter.guessPhoneticNameStyle(phoneticName); 2647 2648 organizationUpdate.bindLong(1, phoneticNameStyle); 2649 organizationUpdate.bindLong(2, dataId); 2650 organizationUpdate.execute(); 2651 2652 String sortKey = null; 2653 if (phoneticName == null && company != null) { 2654 int nameStyle = splitter.guessFullNameStyle(company); 2655 nameStyle = splitter.getAdjustedFullNameStyle(nameStyle); 2656 if (nameStyle == FullNameStyle.CHINESE || 2657 nameStyle == FullNameStyle.CJK ) { 2658 sortKey = ContactLocaleUtils.getIntance() 2659 .getSortKey(company, nameStyle); 2660 } 2661 } 2662 2663 if (sortKey == null) { 2664 sortKey = company; 2665 } 2666 2667 updateRawContact205(rawContactUpdate, rawContactId, company, 2668 company, phoneticNameStyle, phoneticName, sortKey, sortKey); 2669 } 2670 } finally { 2671 cursor.close(); 2672 } 2673 } 2674 2675 private void updateRawContact205(SQLiteStatement rawContactUpdate, long rawContactId, 2676 String displayName, String displayNameAlternative, int phoneticNameStyle, 2677 String phoneticName, String sortKeyPrimary, String sortKeyAlternative) { 2678 bindString(rawContactUpdate, 1, displayName); 2679 bindString(rawContactUpdate, 2, displayNameAlternative); 2680 bindString(rawContactUpdate, 3, phoneticName); 2681 rawContactUpdate.bindLong(4, phoneticNameStyle); 2682 bindString(rawContactUpdate, 5, sortKeyPrimary); 2683 bindString(rawContactUpdate, 6, sortKeyAlternative); 2684 rawContactUpdate.bindLong(7, rawContactId); 2685 rawContactUpdate.execute(); 2686 } 2687 2688 private void upgrateToVersion206(SQLiteDatabase db) { 2689 db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS 2690 + " ADD " + RawContacts.NAME_VERIFIED + " INTEGER NOT NULL DEFAULT 0;"); 2691 } 2692 2693 /** 2694 * Fix for the bug where name lookup records for organizations would get removed by 2695 * unrelated updates of the data rows. 2696 */ 2697 private void upgradeToVersion300(SQLiteDatabase db) { 2698 // No longer needed 2699 } 2700 2701 private static final class Upgrade303Query { 2702 public static final String TABLE = Tables.DATA; 2703 2704 public static final String SELECTION = 2705 DataColumns.MIMETYPE_ID + "=?" + 2706 " AND " + Data._ID + " NOT IN " + 2707 "(SELECT " + NameLookupColumns.DATA_ID + " FROM " + Tables.NAME_LOOKUP + ")" + 2708 " AND " + Data.DATA1 + " NOT NULL"; 2709 2710 public static final String COLUMNS[] = { 2711 Data._ID, 2712 Data.RAW_CONTACT_ID, 2713 Data.DATA1, 2714 }; 2715 2716 public static final int ID = 0; 2717 public static final int RAW_CONTACT_ID = 1; 2718 public static final int DATA1 = 2; 2719 } 2720 2721 /** 2722 * The {@link ContactsProvider2#update} method was deleting name lookup for new 2723 * emails during the sync. We need to restore the lost name lookup rows. 2724 */ 2725 private void upgradeEmailToVersion303(SQLiteDatabase db) { 2726 final long mimeTypeId = lookupMimeTypeId(db, Email.CONTENT_ITEM_TYPE); 2727 if (mimeTypeId == -1) { 2728 return; 2729 } 2730 2731 ContentValues values = new ContentValues(); 2732 2733 // Find all data rows with the mime type "email" that are missing name lookup 2734 Cursor cursor = db.query(Upgrade303Query.TABLE, Upgrade303Query.COLUMNS, 2735 Upgrade303Query.SELECTION, new String[] {String.valueOf(mimeTypeId)}, 2736 null, null, null); 2737 try { 2738 while (cursor.moveToNext()) { 2739 long dataId = cursor.getLong(Upgrade303Query.ID); 2740 long rawContactId = cursor.getLong(Upgrade303Query.RAW_CONTACT_ID); 2741 String value = cursor.getString(Upgrade303Query.DATA1); 2742 value = extractHandleFromEmailAddress(value); 2743 2744 if (value != null) { 2745 values.put(NameLookupColumns.DATA_ID, dataId); 2746 values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId); 2747 values.put(NameLookupColumns.NAME_TYPE, NameLookupType.EMAIL_BASED_NICKNAME); 2748 values.put(NameLookupColumns.NORMALIZED_NAME, NameNormalizer.normalize(value)); 2749 db.insert(Tables.NAME_LOOKUP, null, values); 2750 } 2751 } 2752 } finally { 2753 cursor.close(); 2754 } 2755 } 2756 2757 /** 2758 * The {@link ContactsProvider2#update} method was deleting name lookup for new 2759 * nicknames during the sync. We need to restore the lost name lookup rows. 2760 */ 2761 private void upgradeNicknameToVersion303(SQLiteDatabase db) { 2762 final long mimeTypeId = lookupMimeTypeId(db, Nickname.CONTENT_ITEM_TYPE); 2763 if (mimeTypeId == -1) { 2764 return; 2765 } 2766 2767 ContentValues values = new ContentValues(); 2768 2769 // Find all data rows with the mime type "nickname" that are missing name lookup 2770 Cursor cursor = db.query(Upgrade303Query.TABLE, Upgrade303Query.COLUMNS, 2771 Upgrade303Query.SELECTION, new String[] {String.valueOf(mimeTypeId)}, 2772 null, null, null); 2773 try { 2774 while (cursor.moveToNext()) { 2775 long dataId = cursor.getLong(Upgrade303Query.ID); 2776 long rawContactId = cursor.getLong(Upgrade303Query.RAW_CONTACT_ID); 2777 String value = cursor.getString(Upgrade303Query.DATA1); 2778 2779 values.put(NameLookupColumns.DATA_ID, dataId); 2780 values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId); 2781 values.put(NameLookupColumns.NAME_TYPE, NameLookupType.NICKNAME); 2782 values.put(NameLookupColumns.NORMALIZED_NAME, NameNormalizer.normalize(value)); 2783 db.insert(Tables.NAME_LOOKUP, null, values); 2784 } 2785 } finally { 2786 cursor.close(); 2787 } 2788 } 2789 2790 private void upgradeToVersion304(SQLiteDatabase db) { 2791 // Mimetype table requires an index on mime type 2792 db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS mime_type ON " + Tables.MIMETYPES + " (" + 2793 MimetypesColumns.MIMETYPE + 2794 ");"); 2795 } 2796 2797 private void upgradeToVersion306(SQLiteDatabase db) { 2798 // Fix invalid lookup that was used for Exchange contacts (it was not escaped) 2799 // It happened when a new contact was created AND synchronized 2800 final StringBuilder lookupKeyBuilder = new StringBuilder(); 2801 final SQLiteStatement updateStatement = db.compileStatement( 2802 "UPDATE contacts " + 2803 "SET lookup=? " + 2804 "WHERE _id=?"); 2805 final Cursor contactIdCursor = db.rawQuery( 2806 "SELECT DISTINCT contact_id " + 2807 "FROM raw_contacts " + 2808 "WHERE deleted=0 AND account_type='com.android.exchange'", 2809 null); 2810 try { 2811 while (contactIdCursor.moveToNext()) { 2812 final long contactId = contactIdCursor.getLong(0); 2813 lookupKeyBuilder.setLength(0); 2814 final Cursor c = db.rawQuery( 2815 "SELECT account_type, account_name, _id, sourceid, display_name " + 2816 "FROM raw_contacts " + 2817 "WHERE contact_id=? " + 2818 "ORDER BY _id", 2819 new String[] { String.valueOf(contactId) }); 2820 try { 2821 while (c.moveToNext()) { 2822 ContactLookupKey.appendToLookupKey(lookupKeyBuilder, 2823 c.getString(0), 2824 c.getString(1), 2825 c.getLong(2), 2826 c.getString(3), 2827 c.getString(4)); 2828 } 2829 } finally { 2830 c.close(); 2831 } 2832 2833 if (lookupKeyBuilder.length() == 0) { 2834 updateStatement.bindNull(1); 2835 } else { 2836 updateStatement.bindString(1, Uri.encode(lookupKeyBuilder.toString())); 2837 } 2838 updateStatement.bindLong(2, contactId); 2839 2840 updateStatement.execute(); 2841 } 2842 } finally { 2843 updateStatement.close(); 2844 contactIdCursor.close(); 2845 } 2846 } 2847 2848 private void upgradeToVersion307(SQLiteDatabase db) { 2849 db.execSQL("CREATE TABLE properties (" + 2850 "property_key TEXT PRIMARY_KEY, " + 2851 "property_value TEXT" + 2852 ");"); 2853 } 2854 2855 private void upgradeToVersion308(SQLiteDatabase db) { 2856 db.execSQL("CREATE TABLE accounts (" + 2857 "account_name TEXT, " + 2858 "account_type TEXT " + 2859 ");"); 2860 2861 db.execSQL("INSERT INTO accounts " + 2862 "SELECT DISTINCT account_name, account_type FROM raw_contacts"); 2863 } 2864 2865 private void upgradeToVersion400(SQLiteDatabase db) { 2866 db.execSQL("ALTER TABLE " + Tables.GROUPS 2867 + " ADD " + Groups.FAVORITES + " INTEGER NOT NULL DEFAULT 0;"); 2868 db.execSQL("ALTER TABLE " + Tables.GROUPS 2869 + " ADD " + Groups.AUTO_ADD + " INTEGER NOT NULL DEFAULT 0;"); 2870 } 2871 2872 private void upgradeToVersion353(SQLiteDatabase db) { 2873 db.execSQL("DELETE FROM contacts " + 2874 "WHERE NOT EXISTS (SELECT 1 FROM raw_contacts WHERE contact_id=contacts._id)"); 2875 } 2876 2877 private void rebuildNameLookup(SQLiteDatabase db) { 2878 db.execSQL("DROP INDEX IF EXISTS name_lookup_index"); 2879 insertNameLookup(db); 2880 createContactsIndexes(db); 2881 } 2882 2883 /** 2884 * Regenerates all locale-sensitive data: nickname_lookup, name_lookup and sort keys. 2885 */ 2886 public void setLocale(ContactsProvider2 provider, Locale locale) { 2887 Log.i(TAG, "Switching to locale " + locale); 2888 2889 long start = SystemClock.uptimeMillis(); 2890 SQLiteDatabase db = getWritableDatabase(); 2891 db.setLocale(locale); 2892 db.beginTransaction(); 2893 try { 2894 db.execSQL("DROP INDEX raw_contact_sort_key1_index"); 2895 db.execSQL("DROP INDEX raw_contact_sort_key2_index"); 2896 db.execSQL("DROP INDEX IF EXISTS name_lookup_index"); 2897 2898 loadNicknameLookupTable(db); 2899 insertNameLookup(db); 2900 rebuildSortKeys(db, provider); 2901 createContactsIndexes(db); 2902 db.setTransactionSuccessful(); 2903 } finally { 2904 db.endTransaction(); 2905 } 2906 2907 Log.i(TAG, "Locale change completed in " + (SystemClock.uptimeMillis() - start) + "ms"); 2908 } 2909 2910 /** 2911 * Regenerates sort keys for all contacts. 2912 */ 2913 private void rebuildSortKeys(SQLiteDatabase db, ContactsProvider2 provider) { 2914 Cursor cursor = db.query(Tables.RAW_CONTACTS, new String[]{RawContacts._ID}, 2915 null, null, null, null, null); 2916 try { 2917 while (cursor.moveToNext()) { 2918 long rawContactId = cursor.getLong(0); 2919 updateRawContactDisplayName(db, rawContactId); 2920 } 2921 } finally { 2922 cursor.close(); 2923 } 2924 } 2925 2926 private void insertNameLookup(SQLiteDatabase db) { 2927 db.execSQL("DELETE FROM " + Tables.NAME_LOOKUP); 2928 2929 SQLiteStatement nameLookupInsert = db.compileStatement( 2930 "INSERT OR IGNORE INTO " + Tables.NAME_LOOKUP + "(" 2931 + NameLookupColumns.RAW_CONTACT_ID + "," 2932 + NameLookupColumns.DATA_ID + "," 2933 + NameLookupColumns.NAME_TYPE + "," 2934 + NameLookupColumns.NORMALIZED_NAME + 2935 ") VALUES (?,?,?,?)"); 2936 2937 try { 2938 insertStructuredNameLookup(db, nameLookupInsert); 2939 insertEmailLookup(db, nameLookupInsert); 2940 insertNicknameLookup(db, nameLookupInsert); 2941 } finally { 2942 nameLookupInsert.close(); 2943 } 2944 } 2945 2946 private static final class StructuredNameQuery { 2947 public static final String TABLE = Tables.DATA; 2948 2949 public static final String SELECTION = 2950 DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL"; 2951 2952 public static final String COLUMNS[] = { 2953 StructuredName._ID, 2954 StructuredName.RAW_CONTACT_ID, 2955 StructuredName.DISPLAY_NAME, 2956 }; 2957 2958 public static final int ID = 0; 2959 public static final int RAW_CONTACT_ID = 1; 2960 public static final int DISPLAY_NAME = 2; 2961 } 2962 2963 private class StructuredNameLookupBuilder extends NameLookupBuilder { 2964 2965 private final SQLiteStatement mNameLookupInsert; 2966 private final CommonNicknameCache mCommonNicknameCache; 2967 2968 public StructuredNameLookupBuilder(NameSplitter splitter, 2969 CommonNicknameCache commonNicknameCache, SQLiteStatement nameLookupInsert) { 2970 super(splitter); 2971 this.mCommonNicknameCache = commonNicknameCache; 2972 this.mNameLookupInsert = nameLookupInsert; 2973 } 2974 2975 @Override 2976 protected void insertNameLookup(long rawContactId, long dataId, int lookupType, 2977 String name) { 2978 if (!TextUtils.isEmpty(name)) { 2979 ContactsDatabaseHelper.this.insertNormalizedNameLookup(mNameLookupInsert, 2980 rawContactId, dataId, lookupType, name); 2981 } 2982 } 2983 2984 @Override 2985 protected String[] getCommonNicknameClusters(String normalizedName) { 2986 return mCommonNicknameCache.getCommonNicknameClusters(normalizedName); 2987 } 2988 } 2989 2990 /** 2991 * Inserts name lookup rows for all structured names in the database. 2992 */ 2993 private void insertStructuredNameLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) { 2994 NameSplitter nameSplitter = createNameSplitter(); 2995 NameLookupBuilder nameLookupBuilder = new StructuredNameLookupBuilder(nameSplitter, 2996 new CommonNicknameCache(db), nameLookupInsert); 2997 final long mimeTypeId = lookupMimeTypeId(db, StructuredName.CONTENT_ITEM_TYPE); 2998 Cursor cursor = db.query(StructuredNameQuery.TABLE, StructuredNameQuery.COLUMNS, 2999 StructuredNameQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)}, 3000 null, null, null); 3001 try { 3002 while (cursor.moveToNext()) { 3003 long dataId = cursor.getLong(StructuredNameQuery.ID); 3004 long rawContactId = cursor.getLong(StructuredNameQuery.RAW_CONTACT_ID); 3005 String name = cursor.getString(StructuredNameQuery.DISPLAY_NAME); 3006 int fullNameStyle = nameSplitter.guessFullNameStyle(name); 3007 fullNameStyle = nameSplitter.getAdjustedFullNameStyle(fullNameStyle); 3008 nameLookupBuilder.insertNameLookup(rawContactId, dataId, name, fullNameStyle); 3009 } 3010 } finally { 3011 cursor.close(); 3012 } 3013 } 3014 3015 private static final class OrganizationQuery { 3016 public static final String TABLE = Tables.DATA; 3017 3018 public static final String SELECTION = 3019 DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL"; 3020 3021 public static final String COLUMNS[] = { 3022 Organization._ID, 3023 Organization.RAW_CONTACT_ID, 3024 Organization.COMPANY, 3025 Organization.TITLE, 3026 }; 3027 3028 public static final int ID = 0; 3029 public static final int RAW_CONTACT_ID = 1; 3030 public static final int COMPANY = 2; 3031 public static final int TITLE = 3; 3032 } 3033 3034 private static final class EmailQuery { 3035 public static final String TABLE = Tables.DATA; 3036 3037 public static final String SELECTION = 3038 DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL"; 3039 3040 public static final String COLUMNS[] = { 3041 Email._ID, 3042 Email.RAW_CONTACT_ID, 3043 Email.ADDRESS, 3044 }; 3045 3046 public static final int ID = 0; 3047 public static final int RAW_CONTACT_ID = 1; 3048 public static final int ADDRESS = 2; 3049 } 3050 3051 /** 3052 * Inserts name lookup rows for all email addresses in the database. 3053 */ 3054 private void insertEmailLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) { 3055 final long mimeTypeId = lookupMimeTypeId(db, Email.CONTENT_ITEM_TYPE); 3056 Cursor cursor = db.query(EmailQuery.TABLE, EmailQuery.COLUMNS, 3057 EmailQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)}, 3058 null, null, null); 3059 try { 3060 while (cursor.moveToNext()) { 3061 long dataId = cursor.getLong(EmailQuery.ID); 3062 long rawContactId = cursor.getLong(EmailQuery.RAW_CONTACT_ID); 3063 String address = cursor.getString(EmailQuery.ADDRESS); 3064 address = extractHandleFromEmailAddress(address); 3065 insertNameLookup(nameLookupInsert, rawContactId, dataId, 3066 NameLookupType.EMAIL_BASED_NICKNAME, address); 3067 } 3068 } finally { 3069 cursor.close(); 3070 } 3071 } 3072 3073 private static final class NicknameQuery { 3074 public static final String TABLE = Tables.DATA; 3075 3076 public static final String SELECTION = 3077 DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL"; 3078 3079 public static final String COLUMNS[] = { 3080 Nickname._ID, 3081 Nickname.RAW_CONTACT_ID, 3082 Nickname.NAME, 3083 }; 3084 3085 public static final int ID = 0; 3086 public static final int RAW_CONTACT_ID = 1; 3087 public static final int NAME = 2; 3088 } 3089 3090 /** 3091 * Inserts name lookup rows for all nicknames in the database. 3092 */ 3093 private void insertNicknameLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) { 3094 final long mimeTypeId = lookupMimeTypeId(db, Nickname.CONTENT_ITEM_TYPE); 3095 Cursor cursor = db.query(NicknameQuery.TABLE, NicknameQuery.COLUMNS, 3096 NicknameQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)}, 3097 null, null, null); 3098 try { 3099 while (cursor.moveToNext()) { 3100 long dataId = cursor.getLong(NicknameQuery.ID); 3101 long rawContactId = cursor.getLong(NicknameQuery.RAW_CONTACT_ID); 3102 String nickname = cursor.getString(NicknameQuery.NAME); 3103 insertNameLookup(nameLookupInsert, rawContactId, dataId, 3104 NameLookupType.NICKNAME, nickname); 3105 } 3106 } finally { 3107 cursor.close(); 3108 } 3109 } 3110 3111 /** 3112 * Inserts a record in the {@link Tables#NAME_LOOKUP} table. 3113 */ 3114 public void insertNameLookup(SQLiteStatement stmt, long rawContactId, long dataId, 3115 int lookupType, String name) { 3116 if (TextUtils.isEmpty(name)) { 3117 return; 3118 } 3119 3120 String normalized = NameNormalizer.normalize(name); 3121 if (TextUtils.isEmpty(normalized)) { 3122 return; 3123 } 3124 3125 insertNormalizedNameLookup(stmt, rawContactId, dataId, lookupType, normalized); 3126 } 3127 3128 private void insertNormalizedNameLookup(SQLiteStatement stmt, long rawContactId, long dataId, 3129 int lookupType, String normalizedName) { 3130 stmt.bindLong(1, rawContactId); 3131 stmt.bindLong(2, dataId); 3132 stmt.bindLong(3, lookupType); 3133 stmt.bindString(4, normalizedName); 3134 stmt.executeInsert(); 3135 } 3136 3137 /** 3138 * Changing the VISIBLE bit from a field on both RawContacts and Contacts to a separate table. 3139 */ 3140 private void upgradeToVersion401(SQLiteDatabase db) { 3141 db.execSQL("CREATE TABLE " + Tables.VISIBLE_CONTACTS + " (" + 3142 Contacts._ID + " INTEGER PRIMARY KEY" + 3143 ");"); 3144 db.execSQL("INSERT INTO " + Tables.VISIBLE_CONTACTS + 3145 " SELECT " + Contacts._ID + 3146 " FROM " + Tables.CONTACTS + 3147 " WHERE " + Contacts.IN_VISIBLE_GROUP + "!=0"); 3148 db.execSQL("DROP INDEX contacts_visible_index"); 3149 } 3150 3151 /** 3152 * Introducing a new table: directories. 3153 */ 3154 private void upgradeToVersion402(SQLiteDatabase db) { 3155 createDirectoriesTable(db); 3156 } 3157 3158 private void upgradeToVersion403(SQLiteDatabase db) { 3159 db.execSQL("DROP TABLE IF EXISTS directories;"); 3160 createDirectoriesTable(db); 3161 3162 db.execSQL("ALTER TABLE raw_contacts" 3163 + " ADD raw_contact_is_read_only INTEGER NOT NULL DEFAULT 0;"); 3164 3165 db.execSQL("ALTER TABLE data" 3166 + " ADD is_read_only INTEGER NOT NULL DEFAULT 0;"); 3167 } 3168 3169 private void upgradeToVersion405(SQLiteDatabase db) { 3170 db.execSQL("DROP TABLE IF EXISTS phone_lookup;"); 3171 // Private phone numbers table used for lookup 3172 db.execSQL("CREATE TABLE " + Tables.PHONE_LOOKUP + " (" + 3173 PhoneLookupColumns.DATA_ID 3174 + " INTEGER REFERENCES data(_id) NOT NULL," + 3175 PhoneLookupColumns.RAW_CONTACT_ID 3176 + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," + 3177 PhoneLookupColumns.NORMALIZED_NUMBER + " TEXT NOT NULL," + 3178 PhoneLookupColumns.MIN_MATCH + " TEXT NOT NULL" + 3179 ");"); 3180 3181 db.execSQL("CREATE INDEX phone_lookup_index ON " + Tables.PHONE_LOOKUP + " (" + 3182 PhoneLookupColumns.NORMALIZED_NUMBER + "," + 3183 PhoneLookupColumns.RAW_CONTACT_ID + "," + 3184 PhoneLookupColumns.DATA_ID + 3185 ");"); 3186 3187 db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" + 3188 PhoneLookupColumns.MIN_MATCH + "," + 3189 PhoneLookupColumns.RAW_CONTACT_ID + "," + 3190 PhoneLookupColumns.DATA_ID + 3191 ");"); 3192 3193 final long mimeTypeId = lookupMimeTypeId(db, Phone.CONTENT_ITEM_TYPE); 3194 if (mimeTypeId == -1) { 3195 return; 3196 } 3197 3198 Cursor cursor = db.rawQuery( 3199 "SELECT _id, " + Phone.RAW_CONTACT_ID + ", " + Phone.NUMBER + 3200 " FROM " + Tables.DATA + 3201 " WHERE " + DataColumns.MIMETYPE_ID + "=" + mimeTypeId 3202 + " AND " + Phone.NUMBER + " NOT NULL", null); 3203 3204 ContentValues phoneValues = new ContentValues(); 3205 try { 3206 while (cursor.moveToNext()) { 3207 long dataID = cursor.getLong(0); 3208 long rawContactID = cursor.getLong(1); 3209 String number = cursor.getString(2); 3210 String normalizedNumber = PhoneNumberUtils.normalizeNumber(number); 3211 if (!TextUtils.isEmpty(normalizedNumber)) { 3212 phoneValues.clear(); 3213 phoneValues.put(PhoneLookupColumns.RAW_CONTACT_ID, rawContactID); 3214 phoneValues.put(PhoneLookupColumns.DATA_ID, dataID); 3215 phoneValues.put(PhoneLookupColumns.NORMALIZED_NUMBER, normalizedNumber); 3216 phoneValues.put(PhoneLookupColumns.MIN_MATCH, 3217 PhoneNumberUtils.toCallerIDMinMatch(normalizedNumber)); 3218 db.insert(Tables.PHONE_LOOKUP, null, phoneValues); 3219 } 3220 } 3221 } finally { 3222 cursor.close(); 3223 } 3224 } 3225 3226 private void upgradeToVersion406(SQLiteDatabase db) { 3227 db.execSQL("ALTER TABLE calls ADD countryiso TEXT;"); 3228 } 3229 3230 private void upgradeToVersion409(SQLiteDatabase db) { 3231 db.execSQL("DROP TABLE IF EXISTS directories;"); 3232 createDirectoriesTable(db); 3233 } 3234 3235 /** 3236 * Adding DEFAULT_DIRECTORY table. 3237 * DEFAULT_DIRECTORY should contain every contact which should be shown to users in default. 3238 * - if a contact doesn't belong to any account (local contact), it should be in 3239 * default_directory 3240 * - if a contact belongs to an account that doesn't have a "default" group, it should be in 3241 * default_directory 3242 * - if a contact belongs to an account that has a "default" group (like Google directory, 3243 * which has "My contacts" group as default), it should be in default_directory. 3244 * 3245 * This logic assumes that accounts with the "default" group should have at least one 3246 * group with AUTO_ADD (implying it is the default group) flag in the groups table. 3247 */ 3248 private void upgradeToVersion411(SQLiteDatabase db) { 3249 db.execSQL("DROP TABLE IF EXISTS " + Tables.DEFAULT_DIRECTORY); 3250 db.execSQL("CREATE TABLE default_directory (_id INTEGER PRIMARY KEY);"); 3251 3252 // Process contacts without an account 3253 db.execSQL("INSERT OR IGNORE INTO default_directory " + 3254 " SELECT contact_id " + 3255 " FROM raw_contacts " + 3256 " WHERE raw_contacts.account_name IS NULL " + 3257 " AND raw_contacts.account_type IS NULL "); 3258 3259 // Process accounts that don't have a default group (e.g. Exchange). 3260 db.execSQL("INSERT OR IGNORE INTO default_directory " + 3261 " SELECT contact_id " + 3262 " FROM raw_contacts " + 3263 " WHERE NOT EXISTS" + 3264 " (SELECT _id " + 3265 " FROM groups " + 3266 " WHERE raw_contacts.account_name = groups.account_name" + 3267 " AND raw_contacts.account_type = groups.account_type" + 3268 " AND groups.auto_add != 0)"); 3269 3270 final long mimetype = lookupMimeTypeId(db, GroupMembership.CONTENT_ITEM_TYPE); 3271 3272 // Process accounts that do have a default group (e.g. Google) 3273 db.execSQL("INSERT OR IGNORE INTO default_directory " + 3274 " SELECT contact_id " + 3275 " FROM raw_contacts " + 3276 " JOIN data " + 3277 " ON (raw_contacts._id=raw_contact_id)" + 3278 " WHERE mimetype_id=" + mimetype + 3279 " AND EXISTS" + 3280 " (SELECT _id" + 3281 " FROM groups" + 3282 " WHERE raw_contacts.account_name = groups.account_name" + 3283 " AND raw_contacts.account_type = groups.account_type" + 3284 " AND groups.auto_add != 0)"); 3285 } 3286 3287 private void upgradeToVersion413(SQLiteDatabase db) { 3288 db.execSQL("DROP TABLE IF EXISTS directories;"); 3289 createDirectoriesTable(db); 3290 } 3291 3292 private void upgradeToVersion415(SQLiteDatabase db) { 3293 db.execSQL( 3294 "ALTER TABLE " + Tables.GROUPS + 3295 " ADD " + Groups.GROUP_IS_READ_ONLY + " INTEGER NOT NULL DEFAULT 0"); 3296 db.execSQL( 3297 "UPDATE " + Tables.GROUPS + 3298 " SET " + Groups.GROUP_IS_READ_ONLY + "=1" + 3299 " WHERE " + Groups.SYSTEM_ID + " NOT NULL"); 3300 } 3301 3302 private void upgradeToVersion416(SQLiteDatabase db) { 3303 db.execSQL("CREATE INDEX phone_lookup_data_id_min_match_index ON " + Tables.PHONE_LOOKUP + 3304 " (" + PhoneLookupColumns.DATA_ID + ", " + PhoneLookupColumns.MIN_MATCH + ");"); 3305 } 3306 3307 private void upgradeToVersion501(SQLiteDatabase db) { 3308 // Remove organization rows from the name lookup, we now use search index for that 3309 db.execSQL("DELETE FROM name_lookup WHERE name_type=5"); 3310 } 3311 3312 private void upgradeToVersion502(SQLiteDatabase db) { 3313 // Remove Chinese and Korean name lookup - this data is now in the search index 3314 db.execSQL("DELETE FROM name_lookup WHERE name_type IN (6, 7)"); 3315 } 3316 3317 private void upgradeToVersion504(SQLiteDatabase db) { 3318 populateMimeTypeCache(db); 3319 3320 // Find all names with prefixes and recreate display name 3321 Cursor cursor = db.rawQuery( 3322 "SELECT " + StructuredName.RAW_CONTACT_ID + 3323 " FROM " + Tables.DATA + 3324 " WHERE " + DataColumns.MIMETYPE_ID + "=?" 3325 + " AND " + StructuredName.PREFIX + " NOT NULL", 3326 new String[]{ String.valueOf(mMimeTypeIdStructuredName) }); 3327 3328 try { 3329 while(cursor.moveToNext()) { 3330 long rawContactId = cursor.getLong(0); 3331 updateRawContactDisplayName(db, rawContactId); 3332 } 3333 3334 } finally { 3335 cursor.close(); 3336 } 3337 } 3338 3339 private void upgradeToVersion600(SQLiteDatabase db) { 3340 // This change used to add the profile raw contact ID to the Accounts table. That 3341 // column is no longer needed (as of version 614) since the profile records are stored in 3342 // a separate copy of the database for security reasons. So this change is now a no-op. 3343 } 3344 3345 private void upgradeToVersion601(SQLiteDatabase db) { 3346 db.execSQL("CREATE TABLE data_usage_stat(" + 3347 "stat_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 3348 "data_id INTEGER NOT NULL, " + 3349 "usage_type INTEGER NOT NULL DEFAULT 0, " + 3350 "times_used INTEGER NOT NULL DEFAULT 0, " + 3351 "last_time_used INTERGER NOT NULL DEFAULT 0, " + 3352 "FOREIGN KEY(data_id) REFERENCES data(_id));"); 3353 db.execSQL("CREATE UNIQUE INDEX data_usage_stat_index ON " + 3354 "data_usage_stat (data_id, usage_type)"); 3355 } 3356 3357 private void upgradeToVersion602(SQLiteDatabase db) { 3358 db.execSQL("ALTER TABLE calls ADD voicemail_uri TEXT;"); 3359 db.execSQL("ALTER TABLE calls ADD _data TEXT;"); 3360 db.execSQL("ALTER TABLE calls ADD has_content INTEGER;"); 3361 db.execSQL("ALTER TABLE calls ADD mime_type TEXT;"); 3362 db.execSQL("ALTER TABLE calls ADD source_data TEXT;"); 3363 db.execSQL("ALTER TABLE calls ADD source_package TEXT;"); 3364 db.execSQL("ALTER TABLE calls ADD state INTEGER;"); 3365 } 3366 3367 private void upgradeToVersion604(SQLiteDatabase db) { 3368 db.execSQL("CREATE TABLE voicemail_status (" + 3369 "_id INTEGER PRIMARY KEY AUTOINCREMENT," + 3370 "source_package TEXT UNIQUE NOT NULL," + 3371 "settings_uri TEXT," + 3372 "voicemail_access_uri TEXT," + 3373 "configuration_state INTEGER," + 3374 "data_channel_state INTEGER," + 3375 "notification_channel_state INTEGER" + 3376 ");"); 3377 } 3378 3379 private void upgradeToVersion605(SQLiteDatabase db) { 3380 // This version used to create the stream item and stream item photos tables, but a newer 3381 // version of those tables is created in version 609 below. So omitting the creation in 3382 // this upgrade step to avoid a create->drop->create. 3383 } 3384 3385 private void upgradeToVersion606(SQLiteDatabase db) { 3386 db.execSQL("DROP VIEW IF EXISTS view_contacts_restricted;"); 3387 db.execSQL("DROP VIEW IF EXISTS view_data_restricted;"); 3388 db.execSQL("DROP VIEW IF EXISTS view_raw_contacts_restricted;"); 3389 db.execSQL("DROP VIEW IF EXISTS view_raw_entities_restricted;"); 3390 db.execSQL("DROP VIEW IF EXISTS view_entities_restricted;"); 3391 db.execSQL("DROP VIEW IF EXISTS view_data_usage_stat_restricted;"); 3392 db.execSQL("DROP INDEX IF EXISTS contacts_restricted_index"); 3393 3394 // We should remove the restricted columns here as well, but unfortunately SQLite doesn't 3395 // provide ALTER TABLE DROP COLUMN. As they have DEFAULT 0, we can keep but ignore them 3396 } 3397 3398 private void upgradeToVersion607(SQLiteDatabase db) { 3399 // We added "action" and "action_uri" to groups here, but realized this was not a smart 3400 // move. This upgrade step has been removed (all dogfood phones that executed this step 3401 // will have those columns, but that shouldn't hurt. Unfortunately, SQLite makes it hard 3402 // to remove columns) 3403 } 3404 3405 private void upgradeToVersion608(SQLiteDatabase db) { 3406 db.execSQL("ALTER TABLE contacts ADD photo_file_id INTEGER REFERENCES photo_files(_id);"); 3407 3408 db.execSQL("CREATE TABLE photo_files(" + 3409 "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 3410 "height INTEGER NOT NULL, " + 3411 "width INTEGER NOT NULL, " + 3412 "filesize INTEGER NOT NULL);"); 3413 } 3414 3415 private void upgradeToVersion609(SQLiteDatabase db) { 3416 // This version used to create the stream item and stream item photos tables, but a newer 3417 // version of those tables is created in version 613 below. So omitting the creation in 3418 // this upgrade step to avoid a create->drop->create. 3419 } 3420 3421 private void upgradeToVersion610(SQLiteDatabase db) { 3422 db.execSQL("ALTER TABLE calls ADD is_read INTEGER;"); 3423 } 3424 3425 private void upgradeToVersion611(SQLiteDatabase db) { 3426 db.execSQL("ALTER TABLE raw_contacts ADD data_set TEXT DEFAULT NULL;"); 3427 db.execSQL("ALTER TABLE groups ADD data_set TEXT DEFAULT NULL;"); 3428 db.execSQL("ALTER TABLE accounts ADD data_set TEXT DEFAULT NULL;"); 3429 3430 db.execSQL("CREATE INDEX raw_contacts_source_id_data_set_index ON raw_contacts " + 3431 "(sourceid, account_type, account_name, data_set);"); 3432 3433 db.execSQL("CREATE INDEX groups_source_id_data_set_index ON groups " + 3434 "(sourceid, account_type, account_name, data_set);"); 3435 } 3436 3437 private void upgradeToVersion612(SQLiteDatabase db) { 3438 db.execSQL("ALTER TABLE calls ADD geocoded_location TEXT DEFAULT NULL;"); 3439 // Old calls will not have a geocoded location; new calls will get it when inserted. 3440 } 3441 3442 private void upgradeToVersion613(SQLiteDatabase db) { 3443 // The stream item and stream item photos APIs were not in-use by anyone in the time 3444 // between their initial creation (in v609) and this update. So we're just dropping 3445 // and re-creating them to get appropriate columns. The delta is as follows: 3446 // - In stream_items, package_id was replaced by res_package. 3447 // - In stream_item_photos, picture was replaced by photo_file_id. 3448 // - Instead of resource ids for icon and label, we use resource name strings now 3449 // - Added sync columns 3450 // - Removed action and action_uri 3451 // - Text and comments are now nullable 3452 3453 db.execSQL("DROP TABLE IF EXISTS stream_items"); 3454 db.execSQL("DROP TABLE IF EXISTS stream_item_photos"); 3455 3456 db.execSQL("CREATE TABLE stream_items(" + 3457 "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 3458 "raw_contact_id INTEGER NOT NULL, " + 3459 "res_package TEXT, " + 3460 "icon TEXT, " + 3461 "label TEXT, " + 3462 "text TEXT, " + 3463 "timestamp INTEGER NOT NULL, " + 3464 "comments TEXT, " + 3465 "stream_item_sync1 TEXT, " + 3466 "stream_item_sync2 TEXT, " + 3467 "stream_item_sync3 TEXT, " + 3468 "stream_item_sync4 TEXT, " + 3469 "FOREIGN KEY(raw_contact_id) REFERENCES raw_contacts(_id));"); 3470 3471 db.execSQL("CREATE TABLE stream_item_photos(" + 3472 "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 3473 "stream_item_id INTEGER NOT NULL, " + 3474 "sort_index INTEGER, " + 3475 "photo_file_id INTEGER NOT NULL, " + 3476 "stream_item_photo_sync1 TEXT, " + 3477 "stream_item_photo_sync2 TEXT, " + 3478 "stream_item_photo_sync3 TEXT, " + 3479 "stream_item_photo_sync4 TEXT, " + 3480 "FOREIGN KEY(stream_item_id) REFERENCES stream_items(_id));"); 3481 } 3482 3483 private void upgradeToVersion615(SQLiteDatabase db) { 3484 // Old calls will not have up to date values for these columns, they will be filled in 3485 // as needed. 3486 db.execSQL("ALTER TABLE calls ADD lookup_uri TEXT DEFAULT NULL;"); 3487 db.execSQL("ALTER TABLE calls ADD matched_number TEXT DEFAULT NULL;"); 3488 db.execSQL("ALTER TABLE calls ADD normalized_number TEXT DEFAULT NULL;"); 3489 db.execSQL("ALTER TABLE calls ADD photo_id INTEGER NOT NULL DEFAULT 0;"); 3490 } 3491 3492 private void upgradeToVersion618(SQLiteDatabase db) { 3493 // The Settings table needs a data_set column which technically should be part of the 3494 // primary key but can't be because it may be null. Since SQLite doesn't support nuking 3495 // the primary key, we'll drop the old table, re-create it, and copy the settings back in. 3496 db.execSQL("CREATE TEMPORARY TABLE settings_backup(" + 3497 "account_name STRING NOT NULL," + 3498 "account_type STRING NOT NULL," + 3499 "ungrouped_visible INTEGER NOT NULL DEFAULT 0," + 3500 "should_sync INTEGER NOT NULL DEFAULT 1" + 3501 ");"); 3502 db.execSQL("INSERT INTO settings_backup " + 3503 "SELECT account_name, account_type, ungrouped_visible, should_sync" + 3504 " FROM settings"); 3505 db.execSQL("DROP TABLE settings"); 3506 db.execSQL("CREATE TABLE settings (" + 3507 "account_name STRING NOT NULL," + 3508 "account_type STRING NOT NULL," + 3509 "data_set STRING," + 3510 "ungrouped_visible INTEGER NOT NULL DEFAULT 0," + 3511 "should_sync INTEGER NOT NULL DEFAULT 1" + 3512 ");"); 3513 db.execSQL("INSERT INTO settings " + 3514 "SELECT account_name, account_type, NULL, ungrouped_visible, should_sync " + 3515 "FROM settings_backup"); 3516 db.execSQL("DROP TABLE settings_backup"); 3517 } 3518 3519 private void upgradeToVersion622(SQLiteDatabase db) { 3520 db.execSQL("ALTER TABLE calls ADD formatted_number TEXT DEFAULT NULL;"); 3521 } 3522 3523 public String extractHandleFromEmailAddress(String email) { 3524 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(email); 3525 if (tokens.length == 0) { 3526 return null; 3527 } 3528 3529 String address = tokens[0].getAddress(); 3530 int at = address.indexOf('@'); 3531 if (at != -1) { 3532 return address.substring(0, at); 3533 } 3534 return null; 3535 } 3536 3537 public String extractAddressFromEmailAddress(String email) { 3538 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(email); 3539 if (tokens.length == 0) { 3540 return null; 3541 } 3542 3543 return tokens[0].getAddress().trim(); 3544 } 3545 3546 private static long lookupMimeTypeId(SQLiteDatabase db, String mimeType) { 3547 try { 3548 return DatabaseUtils.longForQuery(db, 3549 "SELECT " + MimetypesColumns._ID + 3550 " FROM " + Tables.MIMETYPES + 3551 " WHERE " + MimetypesColumns.MIMETYPE 3552 + "='" + mimeType + "'", null); 3553 } catch (SQLiteDoneException e) { 3554 // No rows of this type in the database 3555 return -1; 3556 } 3557 } 3558 3559 private void bindString(SQLiteStatement stmt, int index, String value) { 3560 if (value == null) { 3561 stmt.bindNull(index); 3562 } else { 3563 stmt.bindString(index, value); 3564 } 3565 } 3566 3567 private void bindLong(SQLiteStatement stmt, int index, Number value) { 3568 if (value == null) { 3569 stmt.bindNull(index); 3570 } else { 3571 stmt.bindLong(index, value.longValue()); 3572 } 3573 } 3574 3575 /** 3576 * Adds index stats into the SQLite database to force it to always use the lookup indexes. 3577 */ 3578 private void updateSqliteStats(SQLiteDatabase db) { 3579 3580 // Specific stats strings are based on an actual large database after running ANALYZE 3581 try { 3582 updateIndexStats(db, Tables.CONTACTS, 3583 "contacts_has_phone_index", "10000 500"); 3584 3585 updateIndexStats(db, Tables.RAW_CONTACTS, 3586 "raw_contacts_source_id_index", "10000 1 1 1"); 3587 updateIndexStats(db, Tables.RAW_CONTACTS, 3588 "raw_contacts_contact_id_index", "10000 2"); 3589 3590 updateIndexStats(db, Tables.NAME_LOOKUP, 3591 "name_lookup_raw_contact_id_index", "10000 3"); 3592 updateIndexStats(db, Tables.NAME_LOOKUP, 3593 "name_lookup_index", "10000 3 2 2 1"); 3594 updateIndexStats(db, Tables.NAME_LOOKUP, 3595 "sqlite_autoindex_name_lookup_1", "10000 3 2 1"); 3596 3597 updateIndexStats(db, Tables.PHONE_LOOKUP, 3598 "phone_lookup_index", "10000 2 2 1"); 3599 updateIndexStats(db, Tables.PHONE_LOOKUP, 3600 "phone_lookup_min_match_index", "10000 2 2 1"); 3601 3602 updateIndexStats(db, Tables.DATA, 3603 "data_mimetype_data1_index", "60000 5000 2"); 3604 updateIndexStats(db, Tables.DATA, 3605 "data_raw_contact_id", "60000 10"); 3606 3607 updateIndexStats(db, Tables.GROUPS, 3608 "groups_source_id_index", "50 1 1 1"); 3609 3610 updateIndexStats(db, Tables.NICKNAME_LOOKUP, 3611 "sqlite_autoindex_name_lookup_1", "500 2 1"); 3612 3613 } catch (SQLException e) { 3614 Log.e(TAG, "Could not update index stats", e); 3615 } 3616 } 3617 3618 /** 3619 * Stores statistics for a given index. 3620 * 3621 * @param stats has the following structure: the first index is the expected size of 3622 * the table. The following integer(s) are the expected number of records selected with the 3623 * index. There should be one integer per indexed column. 3624 */ 3625 private void updateIndexStats(SQLiteDatabase db, String table, String index, 3626 String stats) { 3627 db.execSQL("DELETE FROM sqlite_stat1 WHERE tbl='" + table + "' AND idx='" + index + "';"); 3628 db.execSQL("INSERT INTO sqlite_stat1 (tbl,idx,stat)" 3629 + " VALUES ('" + table + "','" + index + "','" + stats + "');"); 3630 } 3631 3632 @Override 3633 public synchronized SQLiteDatabase getWritableDatabase() { 3634 SQLiteDatabase db = super.getWritableDatabase(); 3635 if (mReopenDatabase) { 3636 mReopenDatabase = false; 3637 close(); 3638 db = super.getWritableDatabase(); 3639 } 3640 return db; 3641 } 3642 3643 /** 3644 * Wipes all data except mime type and package lookup tables. 3645 */ 3646 public void wipeData() { 3647 SQLiteDatabase db = getWritableDatabase(); 3648 3649 db.execSQL("DELETE FROM " + Tables.ACCOUNTS + ";"); 3650 db.execSQL("INSERT INTO " + Tables.ACCOUNTS + " VALUES(NULL, NULL, NULL)"); 3651 3652 db.execSQL("DELETE FROM " + Tables.CONTACTS + ";"); 3653 db.execSQL("DELETE FROM " + Tables.RAW_CONTACTS + ";"); 3654 db.execSQL("DELETE FROM " + Tables.STREAM_ITEMS + ";"); 3655 db.execSQL("DELETE FROM " + Tables.STREAM_ITEM_PHOTOS + ";"); 3656 db.execSQL("DELETE FROM " + Tables.PHOTO_FILES + ";"); 3657 db.execSQL("DELETE FROM " + Tables.DATA + ";"); 3658 db.execSQL("DELETE FROM " + Tables.PHONE_LOOKUP + ";"); 3659 db.execSQL("DELETE FROM " + Tables.NAME_LOOKUP + ";"); 3660 db.execSQL("DELETE FROM " + Tables.GROUPS + ";"); 3661 db.execSQL("DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS + ";"); 3662 db.execSQL("DELETE FROM " + Tables.SETTINGS + ";"); 3663 db.execSQL("DELETE FROM " + Tables.ACTIVITIES + ";"); 3664 db.execSQL("DELETE FROM " + Tables.CALLS + ";"); 3665 db.execSQL("DELETE FROM " + Tables.DIRECTORIES + ";"); 3666 db.execSQL("DELETE FROM " + Tables.SEARCH_INDEX + ";"); 3667 3668 // Note: we are not removing reference data from Tables.NICKNAME_LOOKUP 3669 } 3670 3671 public NameSplitter createNameSplitter() { 3672 mNameSplitter = new NameSplitter( 3673 mContext.getString(com.android.internal.R.string.common_name_prefixes), 3674 mContext.getString(com.android.internal.R.string.common_last_name_prefixes), 3675 mContext.getString(com.android.internal.R.string.common_name_suffixes), 3676 mContext.getString(com.android.internal.R.string.common_name_conjunctions), 3677 Locale.getDefault()); 3678 return mNameSplitter; 3679 } 3680 3681 /** 3682 * Return the {@link ApplicationInfo#uid} for the given package name. 3683 */ 3684 public static int getUidForPackageName(PackageManager pm, String packageName) { 3685 try { 3686 ApplicationInfo clientInfo = pm.getApplicationInfo(packageName, 0 /* no flags */); 3687 return clientInfo.uid; 3688 } catch (NameNotFoundException e) { 3689 throw new RuntimeException(e); 3690 } 3691 } 3692 3693 /** 3694 * Perform an internal string-to-integer lookup using the compiled 3695 * {@link SQLiteStatement} provided. If a mapping isn't found in database, it will be 3696 * created. All new, uncached answers are added to the cache automatically. 3697 * 3698 * @param query Compiled statement used to query for the mapping. 3699 * @param insert Compiled statement used to insert a new mapping when no 3700 * existing one is found in cache or from query. 3701 * @param value Value to find mapping for. 3702 * @param cache In-memory cache of previous answers. 3703 * @return An unique integer mapping for the given value. 3704 */ 3705 private long lookupAndCacheId(SQLiteStatement query, SQLiteStatement insert, 3706 String value, HashMap<String, Long> cache) { 3707 long id = -1; 3708 try { 3709 // Try searching database for mapping 3710 DatabaseUtils.bindObjectToProgram(query, 1, value); 3711 id = query.simpleQueryForLong(); 3712 } catch (SQLiteDoneException e) { 3713 // Nothing found, so try inserting new mapping 3714 DatabaseUtils.bindObjectToProgram(insert, 1, value); 3715 id = insert.executeInsert(); 3716 } 3717 if (id != -1) { 3718 // Cache and return the new answer 3719 cache.put(value, id); 3720 return id; 3721 } else { 3722 // Otherwise throw if no mapping found or created 3723 throw new IllegalStateException("Couldn't find or create internal " 3724 + "lookup table entry for value " + value); 3725 } 3726 } 3727 3728 /** 3729 * Convert a package name into an integer, using {@link Tables#PACKAGES} for 3730 * lookups and possible allocation of new IDs as needed. 3731 */ 3732 public long getPackageId(String packageName) { 3733 // Try an in-memory cache lookup 3734 if (mPackageCache.containsKey(packageName)) return mPackageCache.get(packageName); 3735 3736 final SQLiteStatement packageQuery = getWritableDatabase().compileStatement( 3737 "SELECT " + PackagesColumns._ID + 3738 " FROM " + Tables.PACKAGES + 3739 " WHERE " + PackagesColumns.PACKAGE + "=?"); 3740 3741 final SQLiteStatement packageInsert = getWritableDatabase().compileStatement( 3742 "INSERT INTO " + Tables.PACKAGES + "(" 3743 + PackagesColumns.PACKAGE + 3744 ") VALUES (?)"); 3745 try { 3746 return lookupAndCacheId(packageQuery, packageInsert, packageName, mPackageCache); 3747 } finally { 3748 packageQuery.close(); 3749 packageInsert.close(); 3750 } 3751 } 3752 3753 /** 3754 * Convert a mimetype into an integer, using {@link Tables#MIMETYPES} for 3755 * lookups and possible allocation of new IDs as needed. 3756 */ 3757 public long getMimeTypeId(String mimetype) { 3758 // Try an in-memory cache lookup 3759 if (mMimetypeCache.containsKey(mimetype)) return mMimetypeCache.get(mimetype); 3760 3761 return lookupMimeTypeId(mimetype, getWritableDatabase()); 3762 } 3763 3764 private long lookupMimeTypeId(String mimetype, SQLiteDatabase db) { 3765 final SQLiteStatement mimetypeQuery = db.compileStatement( 3766 "SELECT " + MimetypesColumns._ID + 3767 " FROM " + Tables.MIMETYPES + 3768 " WHERE " + MimetypesColumns.MIMETYPE + "=?"); 3769 3770 final SQLiteStatement mimetypeInsert = db.compileStatement( 3771 "INSERT INTO " + Tables.MIMETYPES + "(" 3772 + MimetypesColumns.MIMETYPE + 3773 ") VALUES (?)"); 3774 3775 try { 3776 return lookupAndCacheId(mimetypeQuery, mimetypeInsert, mimetype, mMimetypeCache); 3777 } finally { 3778 mimetypeQuery.close(); 3779 mimetypeInsert.close(); 3780 } 3781 } 3782 3783 public long getMimeTypeIdForStructuredName() { 3784 return mMimeTypeIdStructuredName; 3785 } 3786 3787 public long getMimeTypeIdForStructuredPostal() { 3788 return mMimeTypeIdStructuredPostal; 3789 } 3790 3791 public long getMimeTypeIdForOrganization() { 3792 return mMimeTypeIdOrganization; 3793 } 3794 3795 public long getMimeTypeIdForIm() { 3796 return mMimeTypeIdIm; 3797 } 3798 3799 public long getMimeTypeIdForEmail() { 3800 return mMimeTypeIdEmail; 3801 } 3802 3803 public long getMimeTypeIdForPhone() { 3804 return mMimeTypeIdPhone; 3805 } 3806 3807 public long getMimeTypeIdForSip() { 3808 return mMimeTypeIdSip; 3809 } 3810 3811 public int getDisplayNameSourceForMimeTypeId(int mimeTypeId) { 3812 if (mimeTypeId == mMimeTypeIdStructuredName) { 3813 return DisplayNameSources.STRUCTURED_NAME; 3814 } else if (mimeTypeId == mMimeTypeIdEmail) { 3815 return DisplayNameSources.EMAIL; 3816 } else if (mimeTypeId == mMimeTypeIdPhone) { 3817 return DisplayNameSources.PHONE; 3818 } else if (mimeTypeId == mMimeTypeIdOrganization) { 3819 return DisplayNameSources.ORGANIZATION; 3820 } else if (mimeTypeId == mMimeTypeIdNickname) { 3821 return DisplayNameSources.NICKNAME; 3822 } else { 3823 return DisplayNameSources.UNDEFINED; 3824 } 3825 } 3826 3827 /** 3828 * Find the mimetype for the given {@link Data#_ID}. 3829 */ 3830 public String getDataMimeType(long dataId) { 3831 if (mDataMimetypeQuery == null) { 3832 mDataMimetypeQuery = getWritableDatabase().compileStatement( 3833 "SELECT " + MimetypesColumns.MIMETYPE + 3834 " FROM " + Tables.DATA_JOIN_MIMETYPES + 3835 " WHERE " + Tables.DATA + "." + Data._ID + "=?"); 3836 } 3837 try { 3838 // Try database query to find mimetype 3839 DatabaseUtils.bindObjectToProgram(mDataMimetypeQuery, 1, dataId); 3840 String mimetype = mDataMimetypeQuery.simpleQueryForString(); 3841 return mimetype; 3842 } catch (SQLiteDoneException e) { 3843 // No valid mapping found, so return null 3844 return null; 3845 } 3846 } 3847 3848 /** 3849 * Find the mime-type for the given {@link Activities#_ID}. 3850 */ 3851 public String getActivityMimeType(long activityId) { 3852 if (mActivitiesMimetypeQuery == null) { 3853 mActivitiesMimetypeQuery = getWritableDatabase().compileStatement( 3854 "SELECT " + MimetypesColumns.MIMETYPE + 3855 " FROM " + Tables.ACTIVITIES_JOIN_MIMETYPES + 3856 " WHERE " + Tables.ACTIVITIES + "." + Activities._ID + "=?"); 3857 } 3858 try { 3859 // Try database query to find mimetype 3860 DatabaseUtils.bindObjectToProgram(mActivitiesMimetypeQuery, 1, activityId); 3861 String mimetype = mActivitiesMimetypeQuery.simpleQueryForString(); 3862 return mimetype; 3863 } catch (SQLiteDoneException e) { 3864 // No valid mapping found, so return null 3865 return null; 3866 } 3867 } 3868 3869 /** 3870 * Update {@link Contacts#IN_VISIBLE_GROUP} for all contacts. 3871 */ 3872 public void updateAllVisible() { 3873 updateCustomContactVisibility(getWritableDatabase(), ""); 3874 } 3875 3876 /** 3877 * Updates contact visibility and return true iff the visibility was actually changed. 3878 */ 3879 public boolean updateContactVisibleOnlyIfChanged(TransactionContext txContext, long contactId) { 3880 return updateContactVisible(txContext, contactId, true); 3881 } 3882 3883 /** 3884 * Update {@link Contacts#IN_VISIBLE_GROUP} and 3885 * {@link Tables#DEFAULT_DIRECTORY} for a specific contact. 3886 */ 3887 public void updateContactVisible(TransactionContext txContext, long contactId) { 3888 updateContactVisible(txContext, contactId, false); 3889 } 3890 3891 public boolean updateContactVisible( 3892 TransactionContext txContext, long contactId, boolean onlyIfChanged) { 3893 SQLiteDatabase db = getWritableDatabase(); 3894 updateCustomContactVisibility(db, " AND " + Contacts._ID + "=" + contactId); 3895 3896 String contactIdAsString = String.valueOf(contactId); 3897 long mimetype = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE); 3898 3899 // The contact will be included in the default directory if contains 3900 // a raw contact that is in any group or in an account that 3901 // does not have any AUTO_ADD groups. 3902 boolean newVisibility = DatabaseUtils.longForQuery(db, 3903 "SELECT EXISTS (" + 3904 "SELECT " + RawContacts.CONTACT_ID + 3905 " FROM " + Tables.RAW_CONTACTS + 3906 " JOIN " + Tables.DATA + 3907 " ON (" + RawContactsColumns.CONCRETE_ID + "=" 3908 + Data.RAW_CONTACT_ID + ")" + 3909 " WHERE " + RawContacts.CONTACT_ID + "=?" + 3910 " AND " + DataColumns.MIMETYPE_ID + "=?" + 3911 ") OR EXISTS (" + 3912 "SELECT " + RawContacts._ID + 3913 " FROM " + Tables.RAW_CONTACTS + 3914 " WHERE " + RawContacts.CONTACT_ID + "=?" + 3915 " AND NOT EXISTS" + 3916 " (SELECT " + Groups._ID + 3917 " FROM " + Tables.GROUPS + 3918 " WHERE " + RawContactsColumns.CONCRETE_ACCOUNT_NAME + " = " 3919 + GroupsColumns.CONCRETE_ACCOUNT_NAME + 3920 " AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " = " 3921 + GroupsColumns.CONCRETE_ACCOUNT_TYPE + 3922 " AND (" + RawContactsColumns.CONCRETE_DATA_SET + " = " 3923 + GroupsColumns.CONCRETE_DATA_SET 3924 + " OR " + RawContactsColumns.CONCRETE_DATA_SET + " IS NULL AND " 3925 + GroupsColumns.CONCRETE_DATA_SET + " IS NULL)" + 3926 " AND " + Groups.AUTO_ADD + " != 0" + 3927 ")" + 3928 ") OR EXISTS (" + 3929 "SELECT " + RawContacts._ID + 3930 " FROM " + Tables.RAW_CONTACTS + 3931 " WHERE " + RawContacts.CONTACT_ID + "=?" + 3932 " AND " + RawContactsColumns.CONCRETE_ACCOUNT_NAME + " IS NULL " + 3933 " AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " IS NULL" + 3934 " AND " + RawContactsColumns.CONCRETE_DATA_SET + " IS NULL" + 3935 ")", 3936 new String[] { 3937 contactIdAsString, 3938 String.valueOf(mimetype), 3939 contactIdAsString, 3940 contactIdAsString 3941 }) != 0; 3942 3943 if (onlyIfChanged) { 3944 boolean oldVisibility = isContactInDefaultDirectory(db, contactId); 3945 if (oldVisibility == newVisibility) { 3946 return false; 3947 } 3948 } 3949 3950 if (newVisibility) { 3951 db.execSQL("INSERT OR IGNORE INTO " + Tables.DEFAULT_DIRECTORY + " VALUES(?)", 3952 new String[] { contactIdAsString }); 3953 txContext.invalidateSearchIndexForContact(contactId); 3954 } else { 3955 db.execSQL("DELETE FROM " + Tables.DEFAULT_DIRECTORY + 3956 " WHERE " + Contacts._ID + "=?", 3957 new String[] { contactIdAsString }); 3958 db.execSQL("DELETE FROM " + Tables.SEARCH_INDEX + 3959 " WHERE " + SearchIndexColumns.CONTACT_ID + "=CAST(? AS int)", 3960 new String[] { contactIdAsString }); 3961 } 3962 return true; 3963 } 3964 3965 public boolean isContactInDefaultDirectory(SQLiteDatabase db, long contactId) { 3966 if (mContactInDefaultDirectoryQuery == null) { 3967 mContactInDefaultDirectoryQuery = db.compileStatement( 3968 "SELECT EXISTS (" + 3969 "SELECT 1 FROM " + Tables.DEFAULT_DIRECTORY + 3970 " WHERE " + Contacts._ID + "=?)"); 3971 } 3972 mContactInDefaultDirectoryQuery.bindLong(1, contactId); 3973 return mContactInDefaultDirectoryQuery.simpleQueryForLong() != 0; 3974 } 3975 3976 private void updateCustomContactVisibility(SQLiteDatabase db, String selection) { 3977 final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE); 3978 String[] selectionArgs = new String[]{String.valueOf(groupMembershipMimetypeId)}; 3979 3980 // First delete what needs to be deleted, then insert what needs to be added. 3981 // Since flash writes are very expensive, this approach is much better than 3982 // delete-all-insert-all. 3983 db.execSQL("DELETE FROM " + Tables.VISIBLE_CONTACTS + 3984 " WHERE " + "_id NOT IN" + 3985 "(SELECT " + Contacts._ID + 3986 " FROM " + Tables.CONTACTS + 3987 " WHERE (" + Clauses.CONTACT_IS_VISIBLE + ")=1) " + selection, 3988 selectionArgs); 3989 3990 db.execSQL("INSERT INTO " + Tables.VISIBLE_CONTACTS + 3991 " SELECT " + Contacts._ID + 3992 " FROM " + Tables.CONTACTS + 3993 " WHERE " + Contacts._ID + 3994 " NOT IN " + Tables.VISIBLE_CONTACTS + 3995 " AND (" + Clauses.CONTACT_IS_VISIBLE + ")=1 " + selection, 3996 selectionArgs); 3997 } 3998 3999 /** 4000 * Returns contact ID for the given contact or zero if it is NULL. 4001 */ 4002 public long getContactId(long rawContactId) { 4003 if (mContactIdQuery == null) { 4004 mContactIdQuery = getWritableDatabase().compileStatement( 4005 "SELECT " + RawContacts.CONTACT_ID + 4006 " FROM " + Tables.RAW_CONTACTS + 4007 " WHERE " + RawContacts._ID + "=?"); 4008 } 4009 try { 4010 DatabaseUtils.bindObjectToProgram(mContactIdQuery, 1, rawContactId); 4011 return mContactIdQuery.simpleQueryForLong(); 4012 } catch (SQLiteDoneException e) { 4013 // No valid mapping found, so return 0 4014 return 0; 4015 } 4016 } 4017 4018 public int getAggregationMode(long rawContactId) { 4019 if (mAggregationModeQuery == null) { 4020 mAggregationModeQuery = getWritableDatabase().compileStatement( 4021 "SELECT " + RawContacts.AGGREGATION_MODE + 4022 " FROM " + Tables.RAW_CONTACTS + 4023 " WHERE " + RawContacts._ID + "=?"); 4024 } 4025 try { 4026 DatabaseUtils.bindObjectToProgram(mAggregationModeQuery, 1, rawContactId); 4027 return (int)mAggregationModeQuery.simpleQueryForLong(); 4028 } catch (SQLiteDoneException e) { 4029 // No valid row found, so return "disabled" 4030 return RawContacts.AGGREGATION_MODE_DISABLED; 4031 } 4032 } 4033 4034 public void buildPhoneLookupAndContactQuery( 4035 SQLiteQueryBuilder qb, String normalizedNumber, String numberE164) { 4036 String minMatch = PhoneNumberUtils.toCallerIDMinMatch(normalizedNumber); 4037 StringBuilder sb = new StringBuilder(); 4038 appendPhoneLookupTables(sb, minMatch, true); 4039 qb.setTables(sb.toString()); 4040 4041 sb = new StringBuilder(); 4042 appendPhoneLookupSelection(sb, normalizedNumber, numberE164); 4043 qb.appendWhere(sb.toString()); 4044 } 4045 4046 public String buildPhoneLookupAsNestedQuery(String number) { 4047 StringBuilder sb = new StringBuilder(); 4048 final String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number); 4049 sb.append("(SELECT DISTINCT raw_contact_id" + " FROM "); 4050 appendPhoneLookupTables(sb, minMatch, false); 4051 sb.append(" WHERE "); 4052 appendPhoneLookupSelection(sb, number, null); 4053 sb.append(")"); 4054 return sb.toString(); 4055 } 4056 4057 private void appendPhoneLookupTables(StringBuilder sb, final String minMatch, 4058 boolean joinContacts) { 4059 sb.append(Tables.RAW_CONTACTS); 4060 if (joinContacts) { 4061 sb.append(" JOIN " + Views.CONTACTS + " contacts_view" 4062 + " ON (contacts_view._id = raw_contacts.contact_id)"); 4063 } 4064 sb.append(", (SELECT data_id, normalized_number, length(normalized_number) as len " 4065 + " FROM phone_lookup " + " WHERE (" + Tables.PHONE_LOOKUP + "." 4066 + PhoneLookupColumns.MIN_MATCH + " = '"); 4067 sb.append(minMatch); 4068 sb.append("')) AS lookup, " + Tables.DATA); 4069 } 4070 4071 private void appendPhoneLookupSelection(StringBuilder sb, String number, String numberE164) { 4072 sb.append("lookup.data_id=data._id AND data.raw_contact_id=raw_contacts._id"); 4073 boolean hasNumberE164 = !TextUtils.isEmpty(numberE164); 4074 boolean hasNumber = !TextUtils.isEmpty(number); 4075 if (hasNumberE164 || hasNumber) { 4076 sb.append(" AND ( "); 4077 if (hasNumberE164) { 4078 sb.append(" lookup.normalized_number = "); 4079 DatabaseUtils.appendEscapedSQLString(sb, numberE164); 4080 } 4081 if (hasNumberE164 && hasNumber) { 4082 sb.append(" OR "); 4083 } 4084 if (hasNumber) { 4085 int numberLen = number.length(); 4086 sb.append(" lookup.len <= "); 4087 sb.append(numberLen); 4088 sb.append(" AND substr("); 4089 DatabaseUtils.appendEscapedSQLString(sb, number); 4090 sb.append(','); 4091 sb.append(numberLen); 4092 sb.append(" - lookup.len + 1) = lookup.normalized_number"); 4093 // Some countries (e.g. Brazil) can have incoming calls which contain only the local 4094 // number (no country calling code and no area code). This case is handled below. 4095 // Details see b/5197612. 4096 if (!hasNumberE164) { 4097 sb.append(" OR ("); 4098 sb.append(" lookup.len > "); 4099 sb.append(numberLen); 4100 sb.append(" AND substr(lookup.normalized_number,"); 4101 sb.append("lookup.len + 1 - "); 4102 sb.append(numberLen); 4103 sb.append(") = "); 4104 DatabaseUtils.appendEscapedSQLString(sb, number); 4105 sb.append(")"); 4106 } 4107 } 4108 sb.append(')'); 4109 } 4110 } 4111 4112 public String getUseStrictPhoneNumberComparisonParameter() { 4113 return mUseStrictPhoneNumberComparison ? "1" : "0"; 4114 } 4115 4116 /** 4117 * Loads common nickname mappings into the database. 4118 */ 4119 private void loadNicknameLookupTable(SQLiteDatabase db) { 4120 db.execSQL("DELETE FROM " + Tables.NICKNAME_LOOKUP); 4121 4122 String[] strings = mContext.getResources().getStringArray( 4123 com.android.internal.R.array.common_nicknames); 4124 if (strings == null || strings.length == 0) { 4125 return; 4126 } 4127 4128 SQLiteStatement nicknameLookupInsert = db.compileStatement("INSERT INTO " 4129 + Tables.NICKNAME_LOOKUP + "(" + NicknameLookupColumns.NAME + "," 4130 + NicknameLookupColumns.CLUSTER + ") VALUES (?,?)"); 4131 4132 try { 4133 for (int clusterId = 0; clusterId < strings.length; clusterId++) { 4134 String[] names = strings[clusterId].split(","); 4135 for (int j = 0; j < names.length; j++) { 4136 String name = NameNormalizer.normalize(names[j]); 4137 try { 4138 DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 1, name); 4139 DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 2, 4140 String.valueOf(clusterId)); 4141 nicknameLookupInsert.executeInsert(); 4142 } catch (SQLiteException e) { 4143 4144 // Print the exception and keep going - this is not a fatal error 4145 Log.e(TAG, "Cannot insert nickname: " + names[j], e); 4146 } 4147 } 4148 } 4149 } finally { 4150 nicknameLookupInsert.close(); 4151 } 4152 } 4153 4154 public static void copyStringValue(ContentValues toValues, String toKey, 4155 ContentValues fromValues, String fromKey) { 4156 if (fromValues.containsKey(fromKey)) { 4157 toValues.put(toKey, fromValues.getAsString(fromKey)); 4158 } 4159 } 4160 4161 public static void copyLongValue(ContentValues toValues, String toKey, 4162 ContentValues fromValues, String fromKey) { 4163 if (fromValues.containsKey(fromKey)) { 4164 long longValue; 4165 Object value = fromValues.get(fromKey); 4166 if (value instanceof Boolean) { 4167 if ((Boolean)value) { 4168 longValue = 1; 4169 } else { 4170 longValue = 0; 4171 } 4172 } else if (value instanceof String) { 4173 longValue = Long.parseLong((String)value); 4174 } else { 4175 longValue = ((Number)value).longValue(); 4176 } 4177 toValues.put(toKey, longValue); 4178 } 4179 } 4180 4181 public SyncStateContentProviderHelper getSyncState() { 4182 return mSyncState; 4183 } 4184 4185 /** 4186 * Delete the aggregate contact if it has no constituent raw contacts other 4187 * than the supplied one. 4188 */ 4189 public void removeContactIfSingleton(long rawContactId) { 4190 SQLiteDatabase db = getWritableDatabase(); 4191 4192 // Obtain contact ID from the supplied raw contact ID 4193 String contactIdFromRawContactId = "(SELECT " + RawContacts.CONTACT_ID + " FROM " 4194 + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=" + rawContactId + ")"; 4195 4196 // Find other raw contacts in the same aggregate contact 4197 String otherRawContacts = "(SELECT contacts1." + RawContacts._ID + " FROM " 4198 + Tables.RAW_CONTACTS + " contacts1 JOIN " + Tables.RAW_CONTACTS + " contacts2 ON (" 4199 + "contacts1." + RawContacts.CONTACT_ID + "=contacts2." + RawContacts.CONTACT_ID 4200 + ") WHERE contacts1." + RawContacts._ID + "!=" + rawContactId + "" 4201 + " AND contacts2." + RawContacts._ID + "=" + rawContactId + ")"; 4202 4203 db.execSQL("DELETE FROM " + Tables.CONTACTS 4204 + " WHERE " + Contacts._ID + "=" + contactIdFromRawContactId 4205 + " AND NOT EXISTS " + otherRawContacts + ";"); 4206 } 4207 4208 /** 4209 * Returns the value from the {@link Tables#PROPERTIES} table. 4210 */ 4211 public String getProperty(String key, String defaultValue) { 4212 Cursor cursor = getReadableDatabase().query(Tables.PROPERTIES, 4213 new String[]{PropertiesColumns.PROPERTY_VALUE}, 4214 PropertiesColumns.PROPERTY_KEY + "=?", 4215 new String[]{key}, null, null, null); 4216 String value = null; 4217 try { 4218 if (cursor.moveToFirst()) { 4219 value = cursor.getString(0); 4220 } 4221 } finally { 4222 cursor.close(); 4223 } 4224 4225 return value != null ? value : defaultValue; 4226 } 4227 4228 /** 4229 * Stores a key-value pair in the {@link Tables#PROPERTIES} table. 4230 */ 4231 public void setProperty(String key, String value) { 4232 setProperty(getWritableDatabase(), key, value); 4233 } 4234 4235 private void setProperty(SQLiteDatabase db, String key, String value) { 4236 ContentValues values = new ContentValues(); 4237 values.put(PropertiesColumns.PROPERTY_KEY, key); 4238 values.put(PropertiesColumns.PROPERTY_VALUE, value); 4239 db.replace(Tables.PROPERTIES, null, values); 4240 } 4241 4242 /** 4243 * Test if any of the columns appear in the given projection. 4244 */ 4245 public boolean isInProjection(String[] projection, String... columns) { 4246 if (projection == null) { 4247 return true; 4248 } 4249 4250 // Optimized for a single-column test 4251 if (columns.length == 1) { 4252 String column = columns[0]; 4253 for (String test : projection) { 4254 if (column.equals(test)) { 4255 return true; 4256 } 4257 } 4258 } else { 4259 for (String test : projection) { 4260 for (String column : columns) { 4261 if (column.equals(test)) { 4262 return true; 4263 } 4264 } 4265 } 4266 } 4267 return false; 4268 } 4269 4270 /** 4271 * Returns a detailed exception message for the supplied URI. It includes the calling 4272 * user and calling package(s). 4273 */ 4274 public String exceptionMessage(Uri uri) { 4275 return exceptionMessage(null, uri); 4276 } 4277 4278 /** 4279 * Returns a detailed exception message for the supplied URI. It includes the calling 4280 * user and calling package(s). 4281 */ 4282 public String exceptionMessage(String message, Uri uri) { 4283 StringBuilder sb = new StringBuilder(); 4284 if (message != null) { 4285 sb.append(message).append("; "); 4286 } 4287 sb.append("URI: ").append(uri); 4288 final PackageManager pm = mContext.getPackageManager(); 4289 int callingUid = Binder.getCallingUid(); 4290 sb.append(", calling user: "); 4291 String userName = pm.getNameForUid(callingUid); 4292 if (userName != null) { 4293 sb.append(userName); 4294 } else { 4295 sb.append(callingUid); 4296 } 4297 4298 final String[] callerPackages = pm.getPackagesForUid(callingUid); 4299 if (callerPackages != null && callerPackages.length > 0) { 4300 if (callerPackages.length == 1) { 4301 sb.append(", calling package:"); 4302 sb.append(callerPackages[0]); 4303 } else { 4304 sb.append(", calling package is one of: ["); 4305 for (int i = 0; i < callerPackages.length; i++) { 4306 if (i != 0) { 4307 sb.append(", "); 4308 } 4309 sb.append(callerPackages[i]); 4310 } 4311 sb.append("]"); 4312 } 4313 } 4314 4315 return sb.toString(); 4316 } 4317 4318 protected String getCountryIso() { 4319 CountryDetector detector = 4320 (CountryDetector) mContext.getSystemService(Context.COUNTRY_DETECTOR); 4321 return detector.detectCountry().getCountryIso(); 4322 } 4323 4324 public void deleteStatusUpdate(long dataId) { 4325 if (mStatusUpdateDelete == null) { 4326 mStatusUpdateDelete = getWritableDatabase().compileStatement( 4327 "DELETE FROM " + Tables.STATUS_UPDATES + 4328 " WHERE " + StatusUpdatesColumns.DATA_ID + "=?"); 4329 } 4330 mStatusUpdateDelete.bindLong(1, dataId); 4331 mStatusUpdateDelete.execute(); 4332 } 4333 4334 public void replaceStatusUpdate(Long dataId, long timestamp, String status, String resPackage, 4335 Integer iconResource, Integer labelResource) { 4336 if (mStatusUpdateReplace == null) { 4337 mStatusUpdateReplace = getWritableDatabase().compileStatement( 4338 "INSERT OR REPLACE INTO " + Tables.STATUS_UPDATES + "(" 4339 + StatusUpdatesColumns.DATA_ID + ", " 4340 + StatusUpdates.STATUS_TIMESTAMP + "," 4341 + StatusUpdates.STATUS + "," 4342 + StatusUpdates.STATUS_RES_PACKAGE + "," 4343 + StatusUpdates.STATUS_ICON + "," 4344 + StatusUpdates.STATUS_LABEL + ")" + 4345 " VALUES (?,?,?,?,?,?)"); 4346 } 4347 mStatusUpdateReplace.bindLong(1, dataId); 4348 mStatusUpdateReplace.bindLong(2, timestamp); 4349 bindString(mStatusUpdateReplace, 3, status); 4350 bindString(mStatusUpdateReplace, 4, resPackage); 4351 bindLong(mStatusUpdateReplace, 5, iconResource); 4352 bindLong(mStatusUpdateReplace, 6, labelResource); 4353 mStatusUpdateReplace.execute(); 4354 } 4355 4356 public void insertStatusUpdate(Long dataId, String status, String resPackage, 4357 Integer iconResource, Integer labelResource) { 4358 if (mStatusUpdateInsert == null) { 4359 mStatusUpdateInsert = getWritableDatabase().compileStatement( 4360 "INSERT INTO " + Tables.STATUS_UPDATES + "(" 4361 + StatusUpdatesColumns.DATA_ID + ", " 4362 + StatusUpdates.STATUS + "," 4363 + StatusUpdates.STATUS_RES_PACKAGE + "," 4364 + StatusUpdates.STATUS_ICON + "," 4365 + StatusUpdates.STATUS_LABEL + ")" + 4366 " VALUES (?,?,?,?,?)"); 4367 } 4368 try { 4369 mStatusUpdateInsert.bindLong(1, dataId); 4370 bindString(mStatusUpdateInsert, 2, status); 4371 bindString(mStatusUpdateInsert, 3, resPackage); 4372 bindLong(mStatusUpdateInsert, 4, iconResource); 4373 bindLong(mStatusUpdateInsert, 5, labelResource); 4374 mStatusUpdateInsert.executeInsert(); 4375 } catch (SQLiteConstraintException e) { 4376 // The row already exists - update it 4377 if (mStatusUpdateAutoTimestamp == null) { 4378 mStatusUpdateAutoTimestamp = getWritableDatabase().compileStatement( 4379 "UPDATE " + Tables.STATUS_UPDATES + 4380 " SET " + StatusUpdates.STATUS_TIMESTAMP + "=?," 4381 + StatusUpdates.STATUS + "=?" + 4382 " WHERE " + StatusUpdatesColumns.DATA_ID + "=?" 4383 + " AND " + StatusUpdates.STATUS + "!=?"); 4384 } 4385 4386 long timestamp = System.currentTimeMillis(); 4387 mStatusUpdateAutoTimestamp.bindLong(1, timestamp); 4388 bindString(mStatusUpdateAutoTimestamp, 2, status); 4389 mStatusUpdateAutoTimestamp.bindLong(3, dataId); 4390 bindString(mStatusUpdateAutoTimestamp, 4, status); 4391 mStatusUpdateAutoTimestamp.execute(); 4392 4393 if (mStatusAttributionUpdate == null) { 4394 mStatusAttributionUpdate = getWritableDatabase().compileStatement( 4395 "UPDATE " + Tables.STATUS_UPDATES + 4396 " SET " + StatusUpdates.STATUS_RES_PACKAGE + "=?," 4397 + StatusUpdates.STATUS_ICON + "=?," 4398 + StatusUpdates.STATUS_LABEL + "=?" + 4399 " WHERE " + StatusUpdatesColumns.DATA_ID + "=?"); 4400 } 4401 bindString(mStatusAttributionUpdate, 1, resPackage); 4402 bindLong(mStatusAttributionUpdate, 2, iconResource); 4403 bindLong(mStatusAttributionUpdate, 3, labelResource); 4404 mStatusAttributionUpdate.bindLong(4, dataId); 4405 mStatusAttributionUpdate.execute(); 4406 } 4407 } 4408 4409 /** 4410 * Resets the {@link RawContacts#NAME_VERIFIED} flag to 0 on all other raw 4411 * contacts in the same aggregate 4412 */ 4413 public void resetNameVerifiedForOtherRawContacts(long rawContactId) { 4414 if (mResetNameVerifiedForOtherRawContacts == null) { 4415 mResetNameVerifiedForOtherRawContacts = getWritableDatabase().compileStatement( 4416 "UPDATE " + Tables.RAW_CONTACTS + 4417 " SET " + RawContacts.NAME_VERIFIED + "=0" + 4418 " WHERE " + RawContacts.CONTACT_ID + "=(" + 4419 "SELECT " + RawContacts.CONTACT_ID + 4420 " FROM " + Tables.RAW_CONTACTS + 4421 " WHERE " + RawContacts._ID + "=?)" + 4422 " AND " + RawContacts._ID + "!=?"); 4423 } 4424 mResetNameVerifiedForOtherRawContacts.bindLong(1, rawContactId); 4425 mResetNameVerifiedForOtherRawContacts.bindLong(2, rawContactId); 4426 mResetNameVerifiedForOtherRawContacts.execute(); 4427 } 4428 4429 private interface RawContactNameQuery { 4430 public static final String RAW_SQL = 4431 "SELECT " 4432 + DataColumns.MIMETYPE_ID + "," 4433 + Data.IS_PRIMARY + "," 4434 + Data.DATA1 + "," 4435 + Data.DATA2 + "," 4436 + Data.DATA3 + "," 4437 + Data.DATA4 + "," 4438 + Data.DATA5 + "," 4439 + Data.DATA6 + "," 4440 + Data.DATA7 + "," 4441 + Data.DATA8 + "," 4442 + Data.DATA9 + "," 4443 + Data.DATA10 + "," 4444 + Data.DATA11 + 4445 " FROM " + Tables.DATA + 4446 " WHERE " + Data.RAW_CONTACT_ID + "=?" + 4447 " AND (" + Data.DATA1 + " NOT NULL OR " + 4448 Organization.TITLE + " NOT NULL)"; 4449 4450 public static final int MIMETYPE = 0; 4451 public static final int IS_PRIMARY = 1; 4452 public static final int DATA1 = 2; 4453 public static final int GIVEN_NAME = 3; // data2 4454 public static final int FAMILY_NAME = 4; // data3 4455 public static final int PREFIX = 5; // data4 4456 public static final int TITLE = 5; // data4 4457 public static final int MIDDLE_NAME = 6; // data5 4458 public static final int SUFFIX = 7; // data6 4459 public static final int PHONETIC_GIVEN_NAME = 8; // data7 4460 public static final int PHONETIC_MIDDLE_NAME = 9; // data8 4461 public static final int ORGANIZATION_PHONETIC_NAME = 9; // data8 4462 public static final int PHONETIC_FAMILY_NAME = 10; // data9 4463 public static final int FULL_NAME_STYLE = 11; // data10 4464 public static final int ORGANIZATION_PHONETIC_NAME_STYLE = 11; // data10 4465 public static final int PHONETIC_NAME_STYLE = 12; // data11 4466 } 4467 4468 /** 4469 * Updates a raw contact display name based on data rows, e.g. structured name, 4470 * organization, email etc. 4471 */ 4472 public void updateRawContactDisplayName(SQLiteDatabase db, long rawContactId) { 4473 if (mNameSplitter == null) { 4474 createNameSplitter(); 4475 } 4476 4477 int bestDisplayNameSource = DisplayNameSources.UNDEFINED; 4478 NameSplitter.Name bestName = null; 4479 String bestDisplayName = null; 4480 String bestPhoneticName = null; 4481 int bestPhoneticNameStyle = PhoneticNameStyle.UNDEFINED; 4482 4483 mSelectionArgs1[0] = String.valueOf(rawContactId); 4484 Cursor c = db.rawQuery(RawContactNameQuery.RAW_SQL, mSelectionArgs1); 4485 try { 4486 while (c.moveToNext()) { 4487 int mimeType = c.getInt(RawContactNameQuery.MIMETYPE); 4488 int source = getDisplayNameSourceForMimeTypeId(mimeType); 4489 if (source < bestDisplayNameSource || source == DisplayNameSources.UNDEFINED) { 4490 continue; 4491 } 4492 4493 if (source == bestDisplayNameSource 4494 && c.getInt(RawContactNameQuery.IS_PRIMARY) == 0) { 4495 continue; 4496 } 4497 4498 if (mimeType == getMimeTypeIdForStructuredName()) { 4499 NameSplitter.Name name; 4500 if (bestName != null) { 4501 name = new NameSplitter.Name(); 4502 } else { 4503 name = mName; 4504 name.clear(); 4505 } 4506 name.prefix = c.getString(RawContactNameQuery.PREFIX); 4507 name.givenNames = c.getString(RawContactNameQuery.GIVEN_NAME); 4508 name.middleName = c.getString(RawContactNameQuery.MIDDLE_NAME); 4509 name.familyName = c.getString(RawContactNameQuery.FAMILY_NAME); 4510 name.suffix = c.getString(RawContactNameQuery.SUFFIX); 4511 name.fullNameStyle = c.isNull(RawContactNameQuery.FULL_NAME_STYLE) 4512 ? FullNameStyle.UNDEFINED 4513 : c.getInt(RawContactNameQuery.FULL_NAME_STYLE); 4514 name.phoneticFamilyName = c.getString(RawContactNameQuery.PHONETIC_FAMILY_NAME); 4515 name.phoneticMiddleName = c.getString(RawContactNameQuery.PHONETIC_MIDDLE_NAME); 4516 name.phoneticGivenName = c.getString(RawContactNameQuery.PHONETIC_GIVEN_NAME); 4517 name.phoneticNameStyle = c.isNull(RawContactNameQuery.PHONETIC_NAME_STYLE) 4518 ? PhoneticNameStyle.UNDEFINED 4519 : c.getInt(RawContactNameQuery.PHONETIC_NAME_STYLE); 4520 if (!name.isEmpty()) { 4521 bestDisplayNameSource = source; 4522 bestName = name; 4523 } 4524 } else if (mimeType == getMimeTypeIdForOrganization()) { 4525 mCharArrayBuffer.sizeCopied = 0; 4526 c.copyStringToBuffer(RawContactNameQuery.DATA1, mCharArrayBuffer); 4527 if (mCharArrayBuffer.sizeCopied != 0) { 4528 bestDisplayNameSource = source; 4529 bestDisplayName = new String(mCharArrayBuffer.data, 0, 4530 mCharArrayBuffer.sizeCopied); 4531 bestPhoneticName = c.getString( 4532 RawContactNameQuery.ORGANIZATION_PHONETIC_NAME); 4533 bestPhoneticNameStyle = 4534 c.isNull(RawContactNameQuery.ORGANIZATION_PHONETIC_NAME_STYLE) 4535 ? PhoneticNameStyle.UNDEFINED 4536 : c.getInt(RawContactNameQuery.ORGANIZATION_PHONETIC_NAME_STYLE); 4537 } else { 4538 c.copyStringToBuffer(RawContactNameQuery.TITLE, mCharArrayBuffer); 4539 if (mCharArrayBuffer.sizeCopied != 0) { 4540 bestDisplayNameSource = source; 4541 bestDisplayName = new String(mCharArrayBuffer.data, 0, 4542 mCharArrayBuffer.sizeCopied); 4543 bestPhoneticName = null; 4544 bestPhoneticNameStyle = PhoneticNameStyle.UNDEFINED; 4545 } 4546 } 4547 } else { 4548 // Display name is at DATA1 in all other types. 4549 // This is ensured in the constructor. 4550 4551 mCharArrayBuffer.sizeCopied = 0; 4552 c.copyStringToBuffer(RawContactNameQuery.DATA1, mCharArrayBuffer); 4553 if (mCharArrayBuffer.sizeCopied != 0) { 4554 bestDisplayNameSource = source; 4555 bestDisplayName = new String(mCharArrayBuffer.data, 0, 4556 mCharArrayBuffer.sizeCopied); 4557 bestPhoneticName = null; 4558 bestPhoneticNameStyle = PhoneticNameStyle.UNDEFINED; 4559 } 4560 } 4561 } 4562 4563 } finally { 4564 c.close(); 4565 } 4566 4567 String displayNamePrimary; 4568 String displayNameAlternative; 4569 String sortNamePrimary; 4570 String sortNameAlternative; 4571 String sortKeyPrimary = null; 4572 String sortKeyAlternative = null; 4573 int displayNameStyle = FullNameStyle.UNDEFINED; 4574 4575 if (bestDisplayNameSource == DisplayNameSources.STRUCTURED_NAME) { 4576 displayNameStyle = bestName.fullNameStyle; 4577 if (displayNameStyle == FullNameStyle.CJK 4578 || displayNameStyle == FullNameStyle.UNDEFINED) { 4579 displayNameStyle = mNameSplitter.getAdjustedFullNameStyle(displayNameStyle); 4580 bestName.fullNameStyle = displayNameStyle; 4581 } 4582 4583 displayNamePrimary = mNameSplitter.join(bestName, true, true); 4584 displayNameAlternative = mNameSplitter.join(bestName, false, true); 4585 4586 if (TextUtils.isEmpty(bestName.prefix)) { 4587 sortNamePrimary = displayNamePrimary; 4588 sortNameAlternative = displayNameAlternative; 4589 } else { 4590 sortNamePrimary = mNameSplitter.join(bestName, true, false); 4591 sortNameAlternative = mNameSplitter.join(bestName, false, false); 4592 } 4593 4594 bestPhoneticName = mNameSplitter.joinPhoneticName(bestName); 4595 bestPhoneticNameStyle = bestName.phoneticNameStyle; 4596 } else { 4597 displayNamePrimary = displayNameAlternative = bestDisplayName; 4598 sortNamePrimary = sortNameAlternative = bestDisplayName; 4599 } 4600 4601 if (bestPhoneticName != null) { 4602 sortKeyPrimary = sortKeyAlternative = bestPhoneticName; 4603 if (bestPhoneticNameStyle == PhoneticNameStyle.UNDEFINED) { 4604 bestPhoneticNameStyle = mNameSplitter.guessPhoneticNameStyle(bestPhoneticName); 4605 } 4606 } else { 4607 if (displayNameStyle == FullNameStyle.UNDEFINED) { 4608 displayNameStyle = mNameSplitter.guessFullNameStyle(bestDisplayName); 4609 if (displayNameStyle == FullNameStyle.UNDEFINED 4610 || displayNameStyle == FullNameStyle.CJK) { 4611 displayNameStyle = mNameSplitter.getAdjustedNameStyleBasedOnPhoneticNameStyle( 4612 displayNameStyle, bestPhoneticNameStyle); 4613 } 4614 displayNameStyle = mNameSplitter.getAdjustedFullNameStyle(displayNameStyle); 4615 } 4616 if (displayNameStyle == FullNameStyle.CHINESE || 4617 displayNameStyle == FullNameStyle.CJK) { 4618 sortKeyPrimary = sortKeyAlternative = 4619 ContactLocaleUtils.getIntance().getSortKey( 4620 sortNamePrimary, displayNameStyle); 4621 } 4622 } 4623 4624 if (sortKeyPrimary == null) { 4625 sortKeyPrimary = sortNamePrimary; 4626 sortKeyAlternative = sortNameAlternative; 4627 } 4628 4629 if (mRawContactDisplayNameUpdate == null) { 4630 mRawContactDisplayNameUpdate = db.compileStatement( 4631 "UPDATE " + Tables.RAW_CONTACTS + 4632 " SET " + 4633 RawContacts.DISPLAY_NAME_SOURCE + "=?," + 4634 RawContacts.DISPLAY_NAME_PRIMARY + "=?," + 4635 RawContacts.DISPLAY_NAME_ALTERNATIVE + "=?," + 4636 RawContacts.PHONETIC_NAME + "=?," + 4637 RawContacts.PHONETIC_NAME_STYLE + "=?," + 4638 RawContacts.SORT_KEY_PRIMARY + "=?," + 4639 RawContacts.SORT_KEY_ALTERNATIVE + "=?" + 4640 " WHERE " + RawContacts._ID + "=?"); 4641 } 4642 4643 mRawContactDisplayNameUpdate.bindLong(1, bestDisplayNameSource); 4644 bindString(mRawContactDisplayNameUpdate, 2, displayNamePrimary); 4645 bindString(mRawContactDisplayNameUpdate, 3, displayNameAlternative); 4646 bindString(mRawContactDisplayNameUpdate, 4, bestPhoneticName); 4647 mRawContactDisplayNameUpdate.bindLong(5, bestPhoneticNameStyle); 4648 bindString(mRawContactDisplayNameUpdate, 6, sortKeyPrimary); 4649 bindString(mRawContactDisplayNameUpdate, 7, sortKeyAlternative); 4650 mRawContactDisplayNameUpdate.bindLong(8, rawContactId); 4651 mRawContactDisplayNameUpdate.execute(); 4652 } 4653 4654 /* 4655 * Sets the given dataId record in the "data" table to primary, and resets all data records of 4656 * the same mimetype and under the same contact to not be primary. 4657 * 4658 * @param dataId the id of the data record to be set to primary. Pass -1 to clear the primary 4659 * flag of all data items of this raw contacts 4660 */ 4661 public void setIsPrimary(long rawContactId, long dataId, long mimeTypeId) { 4662 if (mSetPrimaryStatement == null) { 4663 mSetPrimaryStatement = getWritableDatabase().compileStatement( 4664 "UPDATE " + Tables.DATA + 4665 " SET " + Data.IS_PRIMARY + "=(_id=?)" + 4666 " WHERE " + DataColumns.MIMETYPE_ID + "=?" + 4667 " AND " + Data.RAW_CONTACT_ID + "=?"); 4668 } 4669 mSetPrimaryStatement.bindLong(1, dataId); 4670 mSetPrimaryStatement.bindLong(2, mimeTypeId); 4671 mSetPrimaryStatement.bindLong(3, rawContactId); 4672 mSetPrimaryStatement.execute(); 4673 } 4674 4675 /* 4676 * Clears the super primary of all data items of the given raw contact. does not touch 4677 * other raw contacts of the same joined aggregate 4678 */ 4679 public void clearSuperPrimary(long rawContactId, long mimeTypeId) { 4680 if (mClearSuperPrimaryStatement == null) { 4681 mClearSuperPrimaryStatement = getWritableDatabase().compileStatement( 4682 "UPDATE " + Tables.DATA + 4683 " SET " + Data.IS_SUPER_PRIMARY + "=0" + 4684 " WHERE " + DataColumns.MIMETYPE_ID + "=?" + 4685 " AND " + Data.RAW_CONTACT_ID + "=?"); 4686 } 4687 mClearSuperPrimaryStatement.bindLong(1, mimeTypeId); 4688 mClearSuperPrimaryStatement.bindLong(2, rawContactId); 4689 mClearSuperPrimaryStatement.execute(); 4690 } 4691 4692 /* 4693 * Sets the given dataId record in the "data" table to "super primary", and resets all data 4694 * records of the same mimetype and under the same aggregate to not be "super primary". 4695 * 4696 * @param dataId the id of the data record to be set to primary. 4697 */ 4698 public void setIsSuperPrimary(long rawContactId, long dataId, long mimeTypeId) { 4699 if (mSetSuperPrimaryStatement == null) { 4700 mSetSuperPrimaryStatement = getWritableDatabase().compileStatement( 4701 "UPDATE " + Tables.DATA + 4702 " SET " + Data.IS_SUPER_PRIMARY + "=(" + Data._ID + "=?)" + 4703 " WHERE " + DataColumns.MIMETYPE_ID + "=?" + 4704 " AND " + Data.RAW_CONTACT_ID + " IN (" + 4705 "SELECT " + RawContacts._ID + 4706 " FROM " + Tables.RAW_CONTACTS + 4707 " WHERE " + RawContacts.CONTACT_ID + " =(" + 4708 "SELECT " + RawContacts.CONTACT_ID + 4709 " FROM " + Tables.RAW_CONTACTS + 4710 " WHERE " + RawContacts._ID + "=?))"); 4711 } 4712 mSetSuperPrimaryStatement.bindLong(1, dataId); 4713 mSetSuperPrimaryStatement.bindLong(2, mimeTypeId); 4714 mSetSuperPrimaryStatement.bindLong(3, rawContactId); 4715 mSetSuperPrimaryStatement.execute(); 4716 } 4717 4718 /** 4719 * Inserts a record in the {@link Tables#NAME_LOOKUP} table. 4720 */ 4721 public void insertNameLookup(long rawContactId, long dataId, int lookupType, String name) { 4722 if (TextUtils.isEmpty(name)) { 4723 return; 4724 } 4725 4726 if (mNameLookupInsert == null) { 4727 mNameLookupInsert = getWritableDatabase().compileStatement( 4728 "INSERT OR IGNORE INTO " + Tables.NAME_LOOKUP + "(" 4729 + NameLookupColumns.RAW_CONTACT_ID + "," 4730 + NameLookupColumns.DATA_ID + "," 4731 + NameLookupColumns.NAME_TYPE + "," 4732 + NameLookupColumns.NORMALIZED_NAME 4733 + ") VALUES (?,?,?,?)"); 4734 } 4735 mNameLookupInsert.bindLong(1, rawContactId); 4736 mNameLookupInsert.bindLong(2, dataId); 4737 mNameLookupInsert.bindLong(3, lookupType); 4738 bindString(mNameLookupInsert, 4, name); 4739 mNameLookupInsert.executeInsert(); 4740 } 4741 4742 /** 4743 * Deletes all {@link Tables#NAME_LOOKUP} table rows associated with the specified data element. 4744 */ 4745 public void deleteNameLookup(long dataId) { 4746 if (mNameLookupDelete == null) { 4747 mNameLookupDelete = getWritableDatabase().compileStatement( 4748 "DELETE FROM " + Tables.NAME_LOOKUP + 4749 " WHERE " + NameLookupColumns.DATA_ID + "=?"); 4750 } 4751 mNameLookupDelete.bindLong(1, dataId); 4752 mNameLookupDelete.execute(); 4753 } 4754 4755 public String insertNameLookupForEmail(long rawContactId, long dataId, String email) { 4756 if (TextUtils.isEmpty(email)) { 4757 return null; 4758 } 4759 4760 String address = extractHandleFromEmailAddress(email); 4761 if (address == null) { 4762 return null; 4763 } 4764 4765 insertNameLookup(rawContactId, dataId, 4766 NameLookupType.EMAIL_BASED_NICKNAME, NameNormalizer.normalize(address)); 4767 return address; 4768 } 4769 4770 /** 4771 * Normalizes the nickname and inserts it in the name lookup table. 4772 */ 4773 public void insertNameLookupForNickname(long rawContactId, long dataId, String nickname) { 4774 if (TextUtils.isEmpty(nickname)) { 4775 return; 4776 } 4777 4778 insertNameLookup(rawContactId, dataId, 4779 NameLookupType.NICKNAME, NameNormalizer.normalize(nickname)); 4780 } 4781 4782 public void insertNameLookupForPhoneticName(long rawContactId, long dataId, String familyName, 4783 String middleName, String givenName) { 4784 mSb.setLength(0); 4785 if (familyName != null) { 4786 mSb.append(familyName.trim()); 4787 } 4788 if (middleName != null) { 4789 mSb.append(middleName.trim()); 4790 } 4791 if (givenName != null) { 4792 mSb.append(givenName.trim()); 4793 } 4794 4795 if (mSb.length() > 0) { 4796 insertNameLookup(rawContactId, dataId, NameLookupType.NAME_COLLATION_KEY, 4797 NameNormalizer.normalize(mSb.toString())); 4798 } 4799 } 4800 4801 /** 4802 * Performs a query and returns true if any Data item of the raw contact with the given 4803 * id and mimetype is marked as super-primary 4804 */ 4805 public boolean rawContactHasSuperPrimary(long rawContactId, long mimeTypeId) { 4806 final Cursor existsCursor = getReadableDatabase().rawQuery( 4807 "SELECT EXISTS(SELECT 1 FROM " + Tables.DATA + 4808 " WHERE " + Data.RAW_CONTACT_ID + "=?" + 4809 " AND " + DataColumns.MIMETYPE_ID + "=?" + 4810 " AND " + Data.IS_SUPER_PRIMARY + "<>0)", 4811 new String[] { String.valueOf(rawContactId), String.valueOf(mimeTypeId) }); 4812 try { 4813 if (!existsCursor.moveToFirst()) throw new IllegalStateException(); 4814 return existsCursor.getInt(0) != 0; 4815 } finally { 4816 existsCursor.close(); 4817 } 4818 } 4819 4820 public String getCurrentCountryIso() { 4821 return mCountryMonitor.getCountryIso(); 4822 } 4823 4824 /* package */ String querySearchIndexContentForTest(long contactId) { 4825 return DatabaseUtils.stringForQuery(getReadableDatabase(), 4826 "SELECT " + SearchIndexColumns.CONTENT + 4827 " FROM " + Tables.SEARCH_INDEX + 4828 " WHERE " + SearchIndexColumns.CONTACT_ID + "=CAST(? AS int)", 4829 new String[] { String.valueOf(contactId) }); 4830 } 4831 4832 /* package */ String querySearchIndexTokensForTest(long contactId) { 4833 return DatabaseUtils.stringForQuery(getReadableDatabase(), 4834 "SELECT " + SearchIndexColumns.TOKENS + 4835 " FROM " + Tables.SEARCH_INDEX + 4836 " WHERE " + SearchIndexColumns.CONTACT_ID + "=CAST(? AS int)", 4837 new String[] { String.valueOf(contactId) }); 4838 } 4839} 4840