ContactsDatabaseHelper.java revision 08768a0f3434130fa46379c1bbfec93a19094939
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.internal.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.Cursor;
29import android.database.DatabaseUtils;
30import android.database.SQLException;
31import android.database.sqlite.SQLiteDatabase;
32import android.database.sqlite.SQLiteDoneException;
33import android.database.sqlite.SQLiteException;
34import android.database.sqlite.SQLiteOpenHelper;
35import android.database.sqlite.SQLiteQueryBuilder;
36import android.database.sqlite.SQLiteStatement;
37import android.net.Uri;
38import android.os.Binder;
39import android.os.Bundle;
40import android.provider.BaseColumns;
41import android.provider.ContactsContract;
42import android.provider.CallLog.Calls;
43import android.provider.ContactsContract.AggregationExceptions;
44import android.provider.ContactsContract.Contacts;
45import android.provider.ContactsContract.Data;
46import android.provider.ContactsContract.DisplayNameSources;
47import android.provider.ContactsContract.FullNameStyle;
48import android.provider.ContactsContract.Groups;
49import android.provider.ContactsContract.RawContacts;
50import android.provider.ContactsContract.Settings;
51import android.provider.ContactsContract.StatusUpdates;
52import android.provider.ContactsContract.CommonDataKinds.Email;
53import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
54import android.provider.ContactsContract.CommonDataKinds.Nickname;
55import android.provider.ContactsContract.CommonDataKinds.Organization;
56import android.provider.ContactsContract.CommonDataKinds.Phone;
57import android.provider.ContactsContract.CommonDataKinds.StructuredName;
58import android.provider.SocialContract.Activities;
59import android.telephony.PhoneNumberUtils;
60import android.text.TextUtils;
61import android.text.util.Rfc822Token;
62import android.text.util.Rfc822Tokenizer;
63import android.util.Log;
64
65import java.util.HashMap;
66import java.util.Locale;
67
68/**
69 * Database helper for contacts. Designed as a singleton to make sure that all
70 * {@link android.content.ContentProvider} users get the same reference.
71 * Provides handy methods for maintaining package and mime-type lookup tables.
72 */
73/* package */ class ContactsDatabaseHelper extends SQLiteOpenHelper {
74    private static final String TAG = "ContactsDatabaseHelper";
75
76    private static final int DATABASE_VERSION = 304;
77
78    private static final String DATABASE_NAME = "contacts2.db";
79    private static final String DATABASE_PRESENCE = "presence_db";
80
81    public interface Tables {
82        public static final String CONTACTS = "contacts";
83        public static final String RAW_CONTACTS = "raw_contacts";
84        public static final String PACKAGES = "packages";
85        public static final String MIMETYPES = "mimetypes";
86        public static final String PHONE_LOOKUP = "phone_lookup";
87        public static final String NAME_LOOKUP = "name_lookup";
88        public static final String AGGREGATION_EXCEPTIONS = "agg_exceptions";
89        public static final String SETTINGS = "settings";
90        public static final String DATA = "data";
91        public static final String GROUPS = "groups";
92        public static final String PRESENCE = "presence";
93        public static final String AGGREGATED_PRESENCE = "agg_presence";
94        public static final String NICKNAME_LOOKUP = "nickname_lookup";
95        public static final String CALLS = "calls";
96        public static final String CONTACT_ENTITIES = "contact_entities_view";
97        public static final String CONTACT_ENTITIES_RESTRICTED = "contact_entities_view_restricted";
98        public static final String STATUS_UPDATES = "status_updates";
99
100        public static final String DATA_JOIN_MIMETYPES = "data "
101                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id)";
102
103        public static final String DATA_JOIN_RAW_CONTACTS = "data "
104                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)";
105
106        public static final String DATA_JOIN_MIMETYPE_RAW_CONTACTS = "data "
107                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
108                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)";
109
110        // NOTE: This requires late binding of GroupMembership MIME-type
111        public static final String RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS = "raw_contacts "
112                + "LEFT OUTER JOIN settings ON ("
113                    + "raw_contacts.account_name = settings.account_name AND "
114                    + "raw_contacts.account_type = settings.account_type) "
115                + "LEFT OUTER JOIN data ON (data.mimetype_id=? AND "
116                    + "data.raw_contact_id = raw_contacts._id) "
117                + "LEFT OUTER JOIN groups ON (groups._id = data." + GroupMembership.GROUP_ROW_ID
118                + ")";
119
120        // NOTE: This requires late binding of GroupMembership MIME-type
121        public static final String SETTINGS_JOIN_RAW_CONTACTS_DATA_MIMETYPES_CONTACTS = "settings "
122                + "LEFT OUTER JOIN raw_contacts ON ("
123                    + "raw_contacts.account_name = settings.account_name AND "
124                    + "raw_contacts.account_type = settings.account_type) "
125                + "LEFT OUTER JOIN data ON (data.mimetype_id=? AND "
126                    + "data.raw_contact_id = raw_contacts._id) "
127                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
128
129        public static final String DATA_JOIN_MIMETYPES_RAW_CONTACTS_CONTACTS = "data "
130                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
131                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
132                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
133
134        public static final String DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_GROUPS = "data "
135                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
136                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
137                + "LEFT OUTER JOIN packages ON (data.package_id = packages._id) "
138                + "LEFT OUTER JOIN groups "
139                + "  ON (mimetypes.mimetype='" + GroupMembership.CONTENT_ITEM_TYPE + "' "
140                + "      AND groups._id = data." + GroupMembership.GROUP_ROW_ID + ") ";
141
142        public static final String GROUPS_JOIN_PACKAGES = "groups "
143                + "LEFT OUTER JOIN packages ON (groups.package_id = packages._id)";
144
145        public static final String ACTIVITIES = "activities";
146
147        public static final String ACTIVITIES_JOIN_MIMETYPES = "activities "
148                + "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id)";
149
150        public static final String ACTIVITIES_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_CONTACTS =
151                "activities "
152                + "LEFT OUTER JOIN packages ON (activities.package_id = packages._id) "
153                + "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id) "
154                + "LEFT OUTER JOIN raw_contacts ON (activities.author_contact_id = " +
155                        "raw_contacts._id) "
156                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
157
158        public static final String NAME_LOOKUP_JOIN_RAW_CONTACTS = "name_lookup "
159                + "INNER JOIN raw_contacts ON (name_lookup.raw_contact_id = raw_contacts._id)";
160    }
161
162    public interface Views {
163        public static final String DATA_ALL = "view_data";
164        public static final String DATA_RESTRICTED = "view_data_restricted";
165
166        public static final String RAW_CONTACTS_ALL = "view_raw_contacts";
167        public static final String RAW_CONTACTS_RESTRICTED = "view_raw_contacts_restricted";
168
169        public static final String CONTACTS_ALL = "view_contacts";
170        public static final String CONTACTS_RESTRICTED = "view_contacts_restricted";
171
172        public static final String GROUPS_ALL = "view_groups";
173    }
174
175    public interface Clauses {
176        final String MIMETYPE_IS_GROUP_MEMBERSHIP = MimetypesColumns.CONCRETE_MIMETYPE + "='"
177                + GroupMembership.CONTENT_ITEM_TYPE + "'";
178
179        final String BELONGS_TO_GROUP = DataColumns.CONCRETE_GROUP_ID + "="
180                + GroupsColumns.CONCRETE_ID;
181
182        final String HAVING_NO_GROUPS = "COUNT(" + DataColumns.CONCRETE_GROUP_ID + ") == 0";
183
184        final String GROUP_BY_ACCOUNT_CONTACT_ID = SettingsColumns.CONCRETE_ACCOUNT_NAME + ","
185                + SettingsColumns.CONCRETE_ACCOUNT_TYPE + "," + RawContacts.CONTACT_ID;
186
187        final String RAW_CONTACT_IS_LOCAL = RawContactsColumns.CONCRETE_ACCOUNT_NAME
188                + " IS NULL AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " IS NULL";
189
190        final String ZERO_GROUP_MEMBERSHIPS = "COUNT(" + GroupsColumns.CONCRETE_ID + ")=0";
191
192        final String OUTER_RAW_CONTACTS = "outer_raw_contacts";
193        final String OUTER_RAW_CONTACTS_ID = OUTER_RAW_CONTACTS + "." + RawContacts._ID;
194
195        final String CONTACT_IS_VISIBLE =
196                "SELECT " +
197                    "MAX((SELECT (CASE WHEN " +
198                        "(CASE" +
199                            " WHEN " + RAW_CONTACT_IS_LOCAL +
200                            " THEN 1 " +
201                            " WHEN " + ZERO_GROUP_MEMBERSHIPS +
202                            " THEN " + Settings.UNGROUPED_VISIBLE +
203                            " ELSE MAX(" + Groups.GROUP_VISIBLE + ")" +
204                         "END)=1 THEN 1 ELSE 0 END)" +
205                " FROM " + Tables.RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS +
206                " WHERE " + RawContactsColumns.CONCRETE_ID + "=" + OUTER_RAW_CONTACTS_ID + "))" +
207                " FROM " + Tables.RAW_CONTACTS + " AS " + OUTER_RAW_CONTACTS +
208                " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
209                " GROUP BY " + RawContacts.CONTACT_ID;
210
211        final String GROUP_HAS_ACCOUNT_AND_SOURCE_ID = Groups.SOURCE_ID + "=? AND "
212                + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?";
213    }
214
215    public interface ContactsColumns {
216        /**
217         * This flag is set for a contact if it has only one constituent raw contact and
218         * it is restricted.
219         */
220        public static final String SINGLE_IS_RESTRICTED = "single_is_restricted";
221
222        public static final String LAST_STATUS_UPDATE_ID = "status_update_id";
223
224        public static final String CONCRETE_ID = Tables.CONTACTS + "." + BaseColumns._ID;
225
226        public static final String CONCRETE_TIMES_CONTACTED = Tables.CONTACTS + "."
227                + Contacts.TIMES_CONTACTED;
228        public static final String CONCRETE_LAST_TIME_CONTACTED = Tables.CONTACTS + "."
229                + Contacts.LAST_TIME_CONTACTED;
230        public static final String CONCRETE_STARRED = Tables.CONTACTS + "." + Contacts.STARRED;
231        public static final String CONCRETE_CUSTOM_RINGTONE = Tables.CONTACTS + "."
232                + Contacts.CUSTOM_RINGTONE;
233        public static final String CONCRETE_SEND_TO_VOICEMAIL = Tables.CONTACTS + "."
234                + Contacts.SEND_TO_VOICEMAIL;
235        public static final String CONCRETE_LOOKUP_KEY = Tables.CONTACTS + "."
236                + Contacts.LOOKUP_KEY;
237    }
238
239    public interface RawContactsColumns {
240        public static final String CONCRETE_ID =
241                Tables.RAW_CONTACTS + "." + BaseColumns._ID;
242        public static final String CONCRETE_ACCOUNT_NAME =
243                Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_NAME;
244        public static final String CONCRETE_ACCOUNT_TYPE =
245                Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_TYPE;
246        public static final String CONCRETE_SOURCE_ID =
247                Tables.RAW_CONTACTS + "." + RawContacts.SOURCE_ID;
248        public static final String CONCRETE_VERSION =
249                Tables.RAW_CONTACTS + "." + RawContacts.VERSION;
250        public static final String CONCRETE_DIRTY =
251                Tables.RAW_CONTACTS + "." + RawContacts.DIRTY;
252        public static final String CONCRETE_DELETED =
253                Tables.RAW_CONTACTS + "." + RawContacts.DELETED;
254        public static final String CONCRETE_SYNC1 =
255                Tables.RAW_CONTACTS + "." + RawContacts.SYNC1;
256        public static final String CONCRETE_SYNC2 =
257                Tables.RAW_CONTACTS + "." + RawContacts.SYNC2;
258        public static final String CONCRETE_SYNC3 =
259                Tables.RAW_CONTACTS + "." + RawContacts.SYNC3;
260        public static final String CONCRETE_SYNC4 =
261                Tables.RAW_CONTACTS + "." + RawContacts.SYNC4;
262        public static final String CONCRETE_STARRED =
263                Tables.RAW_CONTACTS + "." + RawContacts.STARRED;
264        public static final String CONCRETE_IS_RESTRICTED =
265                Tables.RAW_CONTACTS + "." + RawContacts.IS_RESTRICTED;
266
267        public static final String DISPLAY_NAME = RawContacts.DISPLAY_NAME_PRIMARY;
268        public static final String DISPLAY_NAME_SOURCE = RawContacts.DISPLAY_NAME_SOURCE;
269        public static final String AGGREGATION_NEEDED = "aggregation_needed";
270        public static final String CONTACT_IN_VISIBLE_GROUP = "contact_in_visible_group";
271
272        public static final String CONCRETE_DISPLAY_NAME =
273                Tables.RAW_CONTACTS + "." + DISPLAY_NAME;
274        public static final String CONCRETE_CONTACT_ID =
275                Tables.RAW_CONTACTS + "." + RawContacts.CONTACT_ID;
276        public static final String CONCRETE_NAME_VERIFIED =
277                Tables.RAW_CONTACTS + "." + RawContacts.NAME_VERIFIED;
278    }
279
280    public interface DataColumns {
281        public static final String PACKAGE_ID = "package_id";
282        public static final String MIMETYPE_ID = "mimetype_id";
283
284        public static final String CONCRETE_ID = Tables.DATA + "." + BaseColumns._ID;
285        public static final String CONCRETE_MIMETYPE_ID = Tables.DATA + "." + MIMETYPE_ID;
286        public static final String CONCRETE_RAW_CONTACT_ID = Tables.DATA + "."
287                + Data.RAW_CONTACT_ID;
288        public static final String CONCRETE_GROUP_ID = Tables.DATA + "."
289                + GroupMembership.GROUP_ROW_ID;
290
291        public static final String CONCRETE_DATA1 = Tables.DATA + "." + Data.DATA1;
292        public static final String CONCRETE_DATA2 = Tables.DATA + "." + Data.DATA2;
293        public static final String CONCRETE_DATA3 = Tables.DATA + "." + Data.DATA3;
294        public static final String CONCRETE_DATA4 = Tables.DATA + "." + Data.DATA4;
295        public static final String CONCRETE_DATA5 = Tables.DATA + "." + Data.DATA5;
296        public static final String CONCRETE_DATA6 = Tables.DATA + "." + Data.DATA6;
297        public static final String CONCRETE_DATA7 = Tables.DATA + "." + Data.DATA7;
298        public static final String CONCRETE_DATA8 = Tables.DATA + "." + Data.DATA8;
299        public static final String CONCRETE_DATA9 = Tables.DATA + "." + Data.DATA9;
300        public static final String CONCRETE_DATA10 = Tables.DATA + "." + Data.DATA10;
301        public static final String CONCRETE_DATA11 = Tables.DATA + "." + Data.DATA11;
302        public static final String CONCRETE_DATA12 = Tables.DATA + "." + Data.DATA12;
303        public static final String CONCRETE_DATA13 = Tables.DATA + "." + Data.DATA13;
304        public static final String CONCRETE_DATA14 = Tables.DATA + "." + Data.DATA14;
305        public static final String CONCRETE_DATA15 = Tables.DATA + "." + Data.DATA15;
306        public static final String CONCRETE_IS_PRIMARY = Tables.DATA + "." + Data.IS_PRIMARY;
307        public static final String CONCRETE_PACKAGE_ID = Tables.DATA + "." + PACKAGE_ID;
308    }
309
310    // Used only for legacy API support
311    public interface ExtensionsColumns {
312        public static final String NAME = Data.DATA1;
313        public static final String VALUE = Data.DATA2;
314    }
315
316    public interface GroupMembershipColumns {
317        public static final String RAW_CONTACT_ID = Data.RAW_CONTACT_ID;
318        public static final String GROUP_ROW_ID = GroupMembership.GROUP_ROW_ID;
319    }
320
321    public interface PhoneColumns {
322        public static final String NORMALIZED_NUMBER = Data.DATA4;
323        public static final String CONCRETE_NORMALIZED_NUMBER = DataColumns.CONCRETE_DATA4;
324    }
325
326    public interface GroupsColumns {
327        public static final String PACKAGE_ID = "package_id";
328
329        public static final String CONCRETE_ID = Tables.GROUPS + "." + BaseColumns._ID;
330        public static final String CONCRETE_SOURCE_ID = Tables.GROUPS + "." + Groups.SOURCE_ID;
331        public static final String CONCRETE_ACCOUNT_NAME = Tables.GROUPS + "." + Groups.ACCOUNT_NAME;
332        public static final String CONCRETE_ACCOUNT_TYPE = Tables.GROUPS + "." + Groups.ACCOUNT_TYPE;
333    }
334
335    public interface ActivitiesColumns {
336        public static final String PACKAGE_ID = "package_id";
337        public static final String MIMETYPE_ID = "mimetype_id";
338    }
339
340    public interface PhoneLookupColumns {
341        public static final String _ID = BaseColumns._ID;
342        public static final String DATA_ID = "data_id";
343        public static final String RAW_CONTACT_ID = "raw_contact_id";
344        public static final String NORMALIZED_NUMBER = "normalized_number";
345        public static final String MIN_MATCH = "min_match";
346    }
347
348    public interface NameLookupColumns {
349        public static final String RAW_CONTACT_ID = "raw_contact_id";
350        public static final String DATA_ID = "data_id";
351        public static final String NORMALIZED_NAME = "normalized_name";
352        public static final String NAME_TYPE = "name_type";
353    }
354
355    public final static class NameLookupType {
356        public static final int NAME_EXACT = 0;
357        public static final int NAME_VARIANT = 1;
358        public static final int NAME_COLLATION_KEY = 2;
359        public static final int NICKNAME = 3;
360        public static final int EMAIL_BASED_NICKNAME = 4;
361        public static final int ORGANIZATION = 5;
362        public static final int NAME_SHORTHAND = 6;
363
364        // This is the highest name lookup type code plus one
365        public static final int TYPE_COUNT = 7;
366
367        public static boolean isBasedOnStructuredName(int nameLookupType) {
368            return nameLookupType == NameLookupType.NAME_EXACT
369                    || nameLookupType == NameLookupType.NAME_VARIANT
370                    || nameLookupType == NameLookupType.NAME_COLLATION_KEY;
371        }
372    }
373
374    public interface PackagesColumns {
375        public static final String _ID = BaseColumns._ID;
376        public static final String PACKAGE = "package";
377
378        public static final String CONCRETE_ID = Tables.PACKAGES + "." + _ID;
379    }
380
381    public interface MimetypesColumns {
382        public static final String _ID = BaseColumns._ID;
383        public static final String MIMETYPE = "mimetype";
384
385        public static final String CONCRETE_ID = Tables.MIMETYPES + "." + BaseColumns._ID;
386        public static final String CONCRETE_MIMETYPE = Tables.MIMETYPES + "." + MIMETYPE;
387    }
388
389    public interface AggregationExceptionColumns {
390        public static final String _ID = BaseColumns._ID;
391    }
392
393    public interface NicknameLookupColumns {
394        public static final String NAME = "name";
395        public static final String CLUSTER = "cluster";
396    }
397
398    public interface SettingsColumns {
399        public static final String CONCRETE_ACCOUNT_NAME = Tables.SETTINGS + "."
400                + Settings.ACCOUNT_NAME;
401        public static final String CONCRETE_ACCOUNT_TYPE = Tables.SETTINGS + "."
402                + Settings.ACCOUNT_TYPE;
403    }
404
405    public interface PresenceColumns {
406        String RAW_CONTACT_ID = "presence_raw_contact_id";
407        String CONTACT_ID = "presence_contact_id";
408    }
409
410    public interface AggregatedPresenceColumns {
411        String CONTACT_ID = "presence_contact_id";
412
413        String CONCRETE_CONTACT_ID = Tables.AGGREGATED_PRESENCE + "." + CONTACT_ID;
414    }
415
416    public interface StatusUpdatesColumns {
417        String DATA_ID = "status_update_data_id";
418
419        String CONCRETE_DATA_ID = Tables.STATUS_UPDATES + "." + DATA_ID;
420
421        String CONCRETE_PRESENCE = Tables.STATUS_UPDATES + "." + StatusUpdates.PRESENCE;
422        String CONCRETE_STATUS = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS;
423        String CONCRETE_STATUS_TIMESTAMP = Tables.STATUS_UPDATES + "."
424                + StatusUpdates.STATUS_TIMESTAMP;
425        String CONCRETE_STATUS_RES_PACKAGE = Tables.STATUS_UPDATES + "."
426                + StatusUpdates.STATUS_RES_PACKAGE;
427        String CONCRETE_STATUS_LABEL = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS_LABEL;
428        String CONCRETE_STATUS_ICON = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS_ICON;
429    }
430
431    public interface ContactsStatusUpdatesColumns {
432        String ALIAS = "contacts_" + Tables.STATUS_UPDATES;
433
434        String CONCRETE_DATA_ID = ALIAS + "." + StatusUpdatesColumns.DATA_ID;
435
436        String CONCRETE_PRESENCE = ALIAS + "." + StatusUpdates.PRESENCE;
437        String CONCRETE_STATUS = ALIAS + "." + StatusUpdates.STATUS;
438        String CONCRETE_STATUS_TIMESTAMP = ALIAS + "." + StatusUpdates.STATUS_TIMESTAMP;
439        String CONCRETE_STATUS_RES_PACKAGE = ALIAS + "." + StatusUpdates.STATUS_RES_PACKAGE;
440        String CONCRETE_STATUS_LABEL = ALIAS + "." + StatusUpdates.STATUS_LABEL;
441        String CONCRETE_STATUS_ICON = ALIAS + "." + StatusUpdates.STATUS_ICON;
442    }
443
444    /** In-memory cache of previously found MIME-type mappings */
445    private final HashMap<String, Long> mMimetypeCache = new HashMap<String, Long>();
446    /** In-memory cache of previously found package name mappings */
447    private final HashMap<String, Long> mPackageCache = new HashMap<String, Long>();
448
449
450    /** Compiled statements for querying and inserting mappings */
451    private SQLiteStatement mMimetypeQuery;
452    private SQLiteStatement mPackageQuery;
453    private SQLiteStatement mContactIdQuery;
454    private SQLiteStatement mAggregationModeQuery;
455    private SQLiteStatement mMimetypeInsert;
456    private SQLiteStatement mPackageInsert;
457    private SQLiteStatement mDataMimetypeQuery;
458    private SQLiteStatement mActivitiesMimetypeQuery;
459
460    private final Context mContext;
461    private final SyncStateContentProviderHelper mSyncState;
462
463
464    /** Compiled statements for updating {@link Contacts#IN_VISIBLE_GROUP}. */
465    private SQLiteStatement mVisibleSpecificUpdate;
466    private SQLiteStatement mVisibleUpdateRawContacts;
467    private SQLiteStatement mVisibleSpecificUpdateRawContacts;
468
469    private boolean mReopenDatabase = false;
470
471    private static ContactsDatabaseHelper sSingleton = null;
472
473    private boolean mUseStrictPhoneNumberComparison;
474
475    /**
476     * List of package names with access to {@link RawContacts#IS_RESTRICTED} data.
477     */
478    private String[] mUnrestrictedPackages;
479
480    public static synchronized ContactsDatabaseHelper getInstance(Context context) {
481        if (sSingleton == null) {
482            sSingleton = new ContactsDatabaseHelper(context);
483        }
484        return sSingleton;
485    }
486
487    /**
488     * Private constructor, callers except unit tests should obtain an instance through
489     * {@link #getInstance(android.content.Context)} instead.
490     */
491    ContactsDatabaseHelper(Context context) {
492        super(context, DATABASE_NAME, null, DATABASE_VERSION);
493        if (false) Log.i(TAG, "Creating OpenHelper");
494        Resources resources = context.getResources();
495
496        mContext = context;
497        mSyncState = new SyncStateContentProviderHelper();
498        mUseStrictPhoneNumberComparison =
499                resources.getBoolean(
500                        com.android.internal.R.bool.config_use_strict_phone_number_comparation);
501        int resourceId = resources.getIdentifier("unrestricted_packages", "array",
502                context.getPackageName());
503        if (resourceId != 0) {
504            mUnrestrictedPackages = resources.getStringArray(resourceId);
505        } else {
506            mUnrestrictedPackages = new String[0];
507        }
508    }
509
510    @Override
511    public void onOpen(SQLiteDatabase db) {
512        mSyncState.onDatabaseOpened(db);
513
514        // Create compiled statements for package and mimetype lookups
515        mMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns._ID + " FROM "
516                + Tables.MIMETYPES + " WHERE " + MimetypesColumns.MIMETYPE + "=?");
517        mPackageQuery = db.compileStatement("SELECT " + PackagesColumns._ID + " FROM "
518                + Tables.PACKAGES + " WHERE " + PackagesColumns.PACKAGE + "=?");
519        mContactIdQuery = db.compileStatement("SELECT " + RawContacts.CONTACT_ID + " FROM "
520                + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=?");
521        mAggregationModeQuery = db.compileStatement("SELECT " + RawContacts.AGGREGATION_MODE
522                + " FROM " + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=?");
523        mMimetypeInsert = db.compileStatement("INSERT INTO " + Tables.MIMETYPES + "("
524                + MimetypesColumns.MIMETYPE + ") VALUES (?)");
525        mPackageInsert = db.compileStatement("INSERT INTO " + Tables.PACKAGES + "("
526                + PackagesColumns.PACKAGE + ") VALUES (?)");
527
528        mDataMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns.MIMETYPE + " FROM "
529                + Tables.DATA_JOIN_MIMETYPES + " WHERE " + Tables.DATA + "." + Data._ID + "=?");
530        mActivitiesMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns.MIMETYPE
531                + " FROM " + Tables.ACTIVITIES_JOIN_MIMETYPES + " WHERE " + Tables.ACTIVITIES + "."
532                + Activities._ID + "=?");
533
534        // Change visibility of a specific contact
535        mVisibleSpecificUpdate = db.compileStatement(
536                "UPDATE " + Tables.CONTACTS +
537                " SET " + Contacts.IN_VISIBLE_GROUP + "=(" + Clauses.CONTACT_IS_VISIBLE + ")" +
538                " WHERE " + ContactsColumns.CONCRETE_ID + "=?");
539
540        // Return visibility of the aggregate contact joined with the raw contact
541        String contactVisibility =
542                "SELECT " + Contacts.IN_VISIBLE_GROUP +
543                " FROM " + Tables.CONTACTS +
544                " WHERE " + Contacts._ID + "=" + RawContacts.CONTACT_ID;
545
546        // Set visibility of raw contacts to the visibility of corresponding aggregate contacts
547        mVisibleUpdateRawContacts = db.compileStatement(
548                "UPDATE " + Tables.RAW_CONTACTS +
549                " SET " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "=("
550                        + contactVisibility + ")" +
551                " WHERE " + RawContacts.DELETED + "=0" +
552                " AND " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "!=("
553                        + contactVisibility + ")=1");
554
555        // Set visibility of a raw contact to the visibility of corresponding aggregate contact
556        mVisibleSpecificUpdateRawContacts = db.compileStatement(
557                "UPDATE " + Tables.RAW_CONTACTS +
558                " SET " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "=("
559                        + contactVisibility + ")" +
560                " WHERE " + RawContacts.DELETED + "=0 AND " + RawContacts.CONTACT_ID + "=?");
561
562        db.execSQL("ATTACH DATABASE ':memory:' AS " + DATABASE_PRESENCE + ";");
563        db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_PRESENCE + "." + Tables.PRESENCE + " ("+
564                StatusUpdates.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," +
565                StatusUpdates.PROTOCOL + " INTEGER NOT NULL," +
566                StatusUpdates.CUSTOM_PROTOCOL + " TEXT," +
567                StatusUpdates.IM_HANDLE + " TEXT," +
568                StatusUpdates.IM_ACCOUNT + " TEXT," +
569                PresenceColumns.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," +
570                PresenceColumns.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
571                StatusUpdates.PRESENCE + " INTEGER," +
572                "UNIQUE(" + StatusUpdates.PROTOCOL + ", " + StatusUpdates.CUSTOM_PROTOCOL
573                    + ", " + StatusUpdates.IM_HANDLE + ", " + StatusUpdates.IM_ACCOUNT + ")" +
574        ");");
575
576        db.execSQL("CREATE INDEX IF NOT EXISTS " + DATABASE_PRESENCE + ".presenceIndex" + " ON "
577                + Tables.PRESENCE + " (" + PresenceColumns.RAW_CONTACT_ID + ");");
578
579        db.execSQL("CREATE TABLE IF NOT EXISTS "
580                        + DATABASE_PRESENCE + "." + Tables.AGGREGATED_PRESENCE + " ("+
581                AggregatedPresenceColumns.CONTACT_ID
582                        + " INTEGER PRIMARY KEY REFERENCES contacts(_id)," +
583                StatusUpdates.PRESENCE_STATUS + " INTEGER" +
584        ");");
585
586
587        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_deleted"
588                + " BEFORE DELETE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
589                + " BEGIN "
590                + "   DELETE FROM " + Tables.AGGREGATED_PRESENCE
591                + "     WHERE " + AggregatedPresenceColumns.CONTACT_ID + " = " +
592                        "(SELECT " + PresenceColumns.CONTACT_ID +
593                        " FROM " + Tables.PRESENCE +
594                        " WHERE " + PresenceColumns.RAW_CONTACT_ID
595                                + "=OLD." + PresenceColumns.RAW_CONTACT_ID +
596                        " AND NOT EXISTS" +
597                                "(SELECT " + PresenceColumns.RAW_CONTACT_ID +
598                                " FROM " + Tables.PRESENCE +
599                                " WHERE " + PresenceColumns.CONTACT_ID
600                                        + "=OLD." + PresenceColumns.CONTACT_ID +
601                                " AND " + PresenceColumns.RAW_CONTACT_ID
602                                        + "!=OLD." + PresenceColumns.RAW_CONTACT_ID + "));"
603                + " END");
604
605        String replaceAggregatePresenceSql =
606            "INSERT OR REPLACE INTO " + Tables.AGGREGATED_PRESENCE + "("
607                    + AggregatedPresenceColumns.CONTACT_ID + ", "
608                    + StatusUpdates.PRESENCE_STATUS + ")" +
609            " SELECT " + PresenceColumns.CONTACT_ID + ","
610                        + "MAX(" + StatusUpdates.PRESENCE_STATUS + ")" +
611                    " FROM " + Tables.PRESENCE +
612                    " WHERE " + PresenceColumns.CONTACT_ID
613                        + "=NEW." + PresenceColumns.CONTACT_ID + ";";
614
615        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_inserted"
616                + " AFTER INSERT ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
617                + " BEGIN "
618                + replaceAggregatePresenceSql
619                + " END");
620
621        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_updated"
622                + " AFTER UPDATE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
623                + " BEGIN "
624                + replaceAggregatePresenceSql
625                + " END");
626    }
627
628    @Override
629    public void onCreate(SQLiteDatabase db) {
630        Log.i(TAG, "Bootstrapping database");
631
632        mSyncState.createDatabase(db);
633
634        // One row per group of contacts corresponding to the same person
635        db.execSQL("CREATE TABLE " + Tables.CONTACTS + " (" +
636                BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
637                Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
638                Contacts.PHOTO_ID + " INTEGER REFERENCES data(_id)," +
639                Contacts.CUSTOM_RINGTONE + " TEXT," +
640                Contacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," +
641                Contacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
642                Contacts.LAST_TIME_CONTACTED + " INTEGER," +
643                Contacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
644                Contacts.IN_VISIBLE_GROUP + " INTEGER NOT NULL DEFAULT 1," +
645                Contacts.HAS_PHONE_NUMBER + " INTEGER NOT NULL DEFAULT 0," +
646                Contacts.LOOKUP_KEY + " TEXT," +
647                ContactsColumns.LAST_STATUS_UPDATE_ID + " INTEGER REFERENCES data(_id)," +
648                ContactsColumns.SINGLE_IS_RESTRICTED + " INTEGER NOT NULL DEFAULT 0" +
649        ");");
650
651        db.execSQL("CREATE INDEX contacts_visible_index ON " + Tables.CONTACTS + " (" +
652                Contacts.IN_VISIBLE_GROUP +
653        ");");
654
655        db.execSQL("CREATE INDEX contacts_has_phone_index ON " + Tables.CONTACTS + " (" +
656                Contacts.HAS_PHONE_NUMBER +
657        ");");
658
659        db.execSQL("CREATE INDEX contacts_restricted_index ON " + Tables.CONTACTS + " (" +
660                ContactsColumns.SINGLE_IS_RESTRICTED +
661        ");");
662
663        db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" +
664                Contacts.NAME_RAW_CONTACT_ID +
665        ");");
666
667        // Contacts table
668        db.execSQL("CREATE TABLE " + Tables.RAW_CONTACTS + " (" +
669                RawContacts._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
670                RawContacts.IS_RESTRICTED + " INTEGER DEFAULT 0," +
671                RawContacts.ACCOUNT_NAME + " STRING DEFAULT NULL, " +
672                RawContacts.ACCOUNT_TYPE + " STRING DEFAULT NULL, " +
673                RawContacts.SOURCE_ID + " TEXT," +
674                RawContacts.VERSION + " INTEGER NOT NULL DEFAULT 1," +
675                RawContacts.DIRTY + " INTEGER NOT NULL DEFAULT 0," +
676                RawContacts.DELETED + " INTEGER NOT NULL DEFAULT 0," +
677                RawContacts.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," +
678                RawContacts.AGGREGATION_MODE + " INTEGER NOT NULL DEFAULT " +
679                        RawContacts.AGGREGATION_MODE_DEFAULT + "," +
680                RawContactsColumns.AGGREGATION_NEEDED + " INTEGER NOT NULL DEFAULT 1," +
681                RawContacts.CUSTOM_RINGTONE + " TEXT," +
682                RawContacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," +
683                RawContacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
684                RawContacts.LAST_TIME_CONTACTED + " INTEGER," +
685                RawContacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
686                RawContacts.DISPLAY_NAME_PRIMARY + " TEXT," +
687                RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT," +
688                RawContacts.DISPLAY_NAME_SOURCE + " INTEGER NOT NULL DEFAULT " +
689                        DisplayNameSources.UNDEFINED + "," +
690                RawContacts.PHONETIC_NAME + " TEXT," +
691                RawContacts.PHONETIC_NAME_STYLE + " TEXT," +
692                RawContacts.SORT_KEY_PRIMARY + " TEXT COLLATE LOCALIZED," +
693                RawContacts.SORT_KEY_ALTERNATIVE + " TEXT COLLATE LOCALIZED," +
694                RawContacts.NAME_VERIFIED + " INTEGER NOT NULL DEFAULT 0," +
695                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + " INTEGER NOT NULL DEFAULT 0," +
696                RawContacts.SYNC1 + " TEXT, " +
697                RawContacts.SYNC2 + " TEXT, " +
698                RawContacts.SYNC3 + " TEXT, " +
699                RawContacts.SYNC4 + " TEXT " +
700        ");");
701
702        db.execSQL("CREATE INDEX raw_contacts_contact_id_index ON " + Tables.RAW_CONTACTS + " (" +
703                RawContacts.CONTACT_ID +
704        ");");
705
706        db.execSQL("CREATE INDEX raw_contacts_source_id_index ON " + Tables.RAW_CONTACTS + " (" +
707                RawContacts.SOURCE_ID + ", " +
708                RawContacts.ACCOUNT_TYPE + ", " +
709                RawContacts.ACCOUNT_NAME +
710        ");");
711
712        // TODO readd the index and investigate a controlled use of it
713//        db.execSQL("CREATE INDEX raw_contacts_agg_index ON " + Tables.RAW_CONTACTS + " (" +
714//                RawContactsColumns.AGGREGATION_NEEDED +
715//        ");");
716
717        // Package name mapping table
718        db.execSQL("CREATE TABLE " + Tables.PACKAGES + " (" +
719                PackagesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
720                PackagesColumns.PACKAGE + " TEXT NOT NULL" +
721        ");");
722
723        // Mimetype mapping table
724        db.execSQL("CREATE TABLE " + Tables.MIMETYPES + " (" +
725                MimetypesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
726                MimetypesColumns.MIMETYPE + " TEXT NOT NULL" +
727        ");");
728
729        // Mimetype table requires an index on mime type
730        db.execSQL("CREATE UNIQUE INDEX mime_type ON " + Tables.MIMETYPES + " (" +
731                MimetypesColumns.MIMETYPE +
732        ");");
733
734        // Public generic data table
735        db.execSQL("CREATE TABLE " + Tables.DATA + " (" +
736                Data._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
737                DataColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
738                DataColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," +
739                Data.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
740                Data.IS_PRIMARY + " INTEGER NOT NULL DEFAULT 0," +
741                Data.IS_SUPER_PRIMARY + " INTEGER NOT NULL DEFAULT 0," +
742                Data.DATA_VERSION + " INTEGER NOT NULL DEFAULT 0," +
743                Data.DATA1 + " TEXT," +
744                Data.DATA2 + " TEXT," +
745                Data.DATA3 + " TEXT," +
746                Data.DATA4 + " TEXT," +
747                Data.DATA5 + " TEXT," +
748                Data.DATA6 + " TEXT," +
749                Data.DATA7 + " TEXT," +
750                Data.DATA8 + " TEXT," +
751                Data.DATA9 + " TEXT," +
752                Data.DATA10 + " TEXT," +
753                Data.DATA11 + " TEXT," +
754                Data.DATA12 + " TEXT," +
755                Data.DATA13 + " TEXT," +
756                Data.DATA14 + " TEXT," +
757                Data.DATA15 + " TEXT," +
758                Data.SYNC1 + " TEXT, " +
759                Data.SYNC2 + " TEXT, " +
760                Data.SYNC3 + " TEXT, " +
761                Data.SYNC4 + " TEXT " +
762        ");");
763
764        db.execSQL("CREATE INDEX data_raw_contact_id ON " + Tables.DATA + " (" +
765                Data.RAW_CONTACT_ID +
766        ");");
767
768        /**
769         * For email lookup and similar queries.
770         */
771        db.execSQL("CREATE INDEX data_mimetype_data1_index ON " + Tables.DATA + " (" +
772                DataColumns.MIMETYPE_ID + "," +
773                Data.DATA1 +
774        ");");
775
776        // Private phone numbers table used for lookup
777        db.execSQL("CREATE TABLE " + Tables.PHONE_LOOKUP + " (" +
778                PhoneLookupColumns.DATA_ID
779                        + " INTEGER PRIMARY KEY REFERENCES data(_id) NOT NULL," +
780                PhoneLookupColumns.RAW_CONTACT_ID
781                        + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
782                PhoneLookupColumns.NORMALIZED_NUMBER + " TEXT NOT NULL," +
783                PhoneLookupColumns.MIN_MATCH + " TEXT NOT NULL" +
784        ");");
785
786        db.execSQL("CREATE INDEX phone_lookup_index ON " + Tables.PHONE_LOOKUP + " (" +
787                PhoneLookupColumns.NORMALIZED_NUMBER + "," +
788                PhoneLookupColumns.RAW_CONTACT_ID + "," +
789                PhoneLookupColumns.DATA_ID +
790        ");");
791
792        db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" +
793                PhoneLookupColumns.MIN_MATCH + "," +
794                PhoneLookupColumns.RAW_CONTACT_ID + "," +
795                PhoneLookupColumns.DATA_ID +
796        ");");
797
798        // Private name/nickname table used for lookup
799        db.execSQL("CREATE TABLE " + Tables.NAME_LOOKUP + " (" +
800                NameLookupColumns.DATA_ID
801                        + " INTEGER REFERENCES data(_id) NOT NULL," +
802                NameLookupColumns.RAW_CONTACT_ID
803                        + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
804                NameLookupColumns.NORMALIZED_NAME + " TEXT NOT NULL," +
805                NameLookupColumns.NAME_TYPE + " INTEGER NOT NULL," +
806                "PRIMARY KEY ("
807                        + NameLookupColumns.DATA_ID + ", "
808                        + NameLookupColumns.NORMALIZED_NAME + ", "
809                        + NameLookupColumns.NAME_TYPE + ")" +
810        ");");
811
812        db.execSQL("CREATE INDEX name_lookup_raw_contact_id_index ON " + Tables.NAME_LOOKUP + " (" +
813                NameLookupColumns.RAW_CONTACT_ID +
814        ");");
815
816        db.execSQL("CREATE TABLE " + Tables.NICKNAME_LOOKUP + " (" +
817                NicknameLookupColumns.NAME + " TEXT," +
818                NicknameLookupColumns.CLUSTER + " TEXT" +
819        ");");
820
821        db.execSQL("CREATE UNIQUE INDEX nickname_lookup_index ON " + Tables.NICKNAME_LOOKUP + " (" +
822                NicknameLookupColumns.NAME + ", " +
823                NicknameLookupColumns.CLUSTER +
824        ");");
825
826        // Groups table
827        db.execSQL("CREATE TABLE " + Tables.GROUPS + " (" +
828                Groups._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
829                GroupsColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
830                Groups.ACCOUNT_NAME + " STRING DEFAULT NULL, " +
831                Groups.ACCOUNT_TYPE + " STRING DEFAULT NULL, " +
832                Groups.SOURCE_ID + " TEXT," +
833                Groups.VERSION + " INTEGER NOT NULL DEFAULT 1," +
834                Groups.DIRTY + " INTEGER NOT NULL DEFAULT 0," +
835                Groups.TITLE + " TEXT," +
836                Groups.TITLE_RES + " INTEGER," +
837                Groups.NOTES + " TEXT," +
838                Groups.SYSTEM_ID + " TEXT," +
839                Groups.DELETED + " INTEGER NOT NULL DEFAULT 0," +
840                Groups.GROUP_VISIBLE + " INTEGER NOT NULL DEFAULT 0," +
841                Groups.SHOULD_SYNC + " INTEGER NOT NULL DEFAULT 1," +
842                Groups.SYNC1 + " TEXT, " +
843                Groups.SYNC2 + " TEXT, " +
844                Groups.SYNC3 + " TEXT, " +
845                Groups.SYNC4 + " TEXT " +
846        ");");
847
848        db.execSQL("CREATE INDEX groups_source_id_index ON " + Tables.GROUPS + " (" +
849                Groups.SOURCE_ID + ", " +
850                Groups.ACCOUNT_TYPE + ", " +
851                Groups.ACCOUNT_NAME +
852        ");");
853
854        db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.AGGREGATION_EXCEPTIONS + " (" +
855                AggregationExceptionColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
856                AggregationExceptions.TYPE + " INTEGER NOT NULL, " +
857                AggregationExceptions.RAW_CONTACT_ID1
858                        + " INTEGER REFERENCES raw_contacts(_id), " +
859                AggregationExceptions.RAW_CONTACT_ID2
860                        + " INTEGER REFERENCES raw_contacts(_id)" +
861        ");");
862
863        db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS aggregation_exception_index1 ON " +
864                Tables.AGGREGATION_EXCEPTIONS + " (" +
865                AggregationExceptions.RAW_CONTACT_ID1 + ", " +
866                AggregationExceptions.RAW_CONTACT_ID2 +
867        ");");
868
869        db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS aggregation_exception_index2 ON " +
870                Tables.AGGREGATION_EXCEPTIONS + " (" +
871                AggregationExceptions.RAW_CONTACT_ID2 + ", " +
872                AggregationExceptions.RAW_CONTACT_ID1 +
873        ");");
874
875        db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.SETTINGS + " (" +
876                Settings.ACCOUNT_NAME + " STRING NOT NULL," +
877                Settings.ACCOUNT_TYPE + " STRING NOT NULL," +
878                Settings.UNGROUPED_VISIBLE + " INTEGER NOT NULL DEFAULT 0," +
879                Settings.SHOULD_SYNC + " INTEGER NOT NULL DEFAULT 1, " +
880                "PRIMARY KEY (" + Settings.ACCOUNT_NAME + ", " +
881                    Settings.ACCOUNT_TYPE + ") ON CONFLICT REPLACE" +
882        ");");
883
884        // The table for recent calls is here so we can do table joins
885        // on people, phones, and calls all in one place.
886        db.execSQL("CREATE TABLE " + Tables.CALLS + " (" +
887                Calls._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
888                Calls.NUMBER + " TEXT," +
889                Calls.DATE + " INTEGER," +
890                Calls.DURATION + " INTEGER," +
891                Calls.TYPE + " INTEGER," +
892                Calls.NEW + " INTEGER," +
893                Calls.CACHED_NAME + " TEXT," +
894                Calls.CACHED_NUMBER_TYPE + " INTEGER," +
895                Calls.CACHED_NUMBER_LABEL + " TEXT" +
896        ");");
897
898        // Activities table
899        db.execSQL("CREATE TABLE " + Tables.ACTIVITIES + " (" +
900                Activities._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
901                ActivitiesColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
902                ActivitiesColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," +
903                Activities.RAW_ID + " TEXT," +
904                Activities.IN_REPLY_TO + " TEXT," +
905                Activities.AUTHOR_CONTACT_ID +  " INTEGER REFERENCES raw_contacts(_id)," +
906                Activities.TARGET_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
907                Activities.PUBLISHED + " INTEGER NOT NULL," +
908                Activities.THREAD_PUBLISHED + " INTEGER NOT NULL," +
909                Activities.TITLE + " TEXT NOT NULL," +
910                Activities.SUMMARY + " TEXT," +
911                Activities.LINK + " TEXT, " +
912                Activities.THUMBNAIL + " BLOB" +
913        ");");
914
915        db.execSQL("CREATE TABLE " + Tables.STATUS_UPDATES + " (" +
916                StatusUpdatesColumns.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," +
917                StatusUpdates.STATUS + " TEXT," +
918                StatusUpdates.STATUS_TIMESTAMP + " INTEGER," +
919                StatusUpdates.STATUS_RES_PACKAGE + " TEXT, " +
920                StatusUpdates.STATUS_LABEL + " INTEGER, " +
921                StatusUpdates.STATUS_ICON + " INTEGER" +
922        ");");
923
924        createContactsViews(db);
925        createGroupsView(db);
926        createContactEntitiesView(db);
927        createContactsTriggers(db);
928        createContactsIndexes(db);
929
930        loadNicknameLookupTable(db);
931
932        // Add the legacy API support views, etc
933        LegacyApiSupport.createDatabase(db);
934
935        // This will create a sqlite_stat1 table that is used for query optimization
936        db.execSQL("ANALYZE;");
937
938        updateSqliteStats(db);
939
940        // We need to close and reopen the database connection so that the stats are
941        // taken into account. Make a note of it and do the actual reopening in the
942        // getWritableDatabase method.
943        mReopenDatabase = true;
944
945        ContentResolver.requestSync(null /* all accounts */,
946                ContactsContract.AUTHORITY, new Bundle());
947    }
948
949    private static void createContactsTriggers(SQLiteDatabase db) {
950
951        /*
952         * Automatically delete Data rows when a raw contact is deleted.
953         */
954        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.RAW_CONTACTS + "_deleted;");
955        db.execSQL("CREATE TRIGGER " + Tables.RAW_CONTACTS + "_deleted "
956                + "   BEFORE DELETE ON " + Tables.RAW_CONTACTS
957                + " BEGIN "
958                + "   DELETE FROM " + Tables.DATA
959                + "     WHERE " + Data.RAW_CONTACT_ID
960                                + "=OLD." + RawContacts._ID + ";"
961                + "   DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS
962                + "     WHERE " + AggregationExceptions.RAW_CONTACT_ID1
963                                + "=OLD." + RawContacts._ID
964                + "        OR " + AggregationExceptions.RAW_CONTACT_ID2
965                                + "=OLD." + RawContacts._ID + ";"
966                + "   DELETE FROM " + Tables.CONTACTS
967                + "     WHERE " + Contacts._ID + "=OLD." + RawContacts.CONTACT_ID
968                + "       AND (SELECT COUNT(*) FROM " + Tables.RAW_CONTACTS
969                + "            WHERE " + RawContacts.CONTACT_ID + "=OLD." + RawContacts.CONTACT_ID
970                + "           )=1;"
971                + " END");
972
973
974        db.execSQL("DROP TRIGGER IF EXISTS contacts_times_contacted;");
975        db.execSQL("DROP TRIGGER IF EXISTS raw_contacts_times_contacted;");
976
977        /*
978         * Triggers that update {@link RawContacts#VERSION} when the contact is
979         * marked for deletion or any time a data row is inserted, updated or
980         * deleted.
981         */
982        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.RAW_CONTACTS + "_marked_deleted;");
983        db.execSQL("CREATE TRIGGER " + Tables.RAW_CONTACTS + "_marked_deleted "
984                + "   AFTER UPDATE ON " + Tables.RAW_CONTACTS
985                + " BEGIN "
986                + "   UPDATE " + Tables.RAW_CONTACTS
987                + "     SET "
988                +         RawContacts.VERSION + "=OLD." + RawContacts.VERSION + "+1 "
989                + "     WHERE " + RawContacts._ID + "=OLD." + RawContacts._ID
990                + "       AND NEW." + RawContacts.DELETED + "!= OLD." + RawContacts.DELETED + ";"
991                + " END");
992
993        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.DATA + "_updated;");
994        db.execSQL("CREATE TRIGGER " + Tables.DATA + "_updated AFTER UPDATE ON " + Tables.DATA
995                + " BEGIN "
996                + "   UPDATE " + Tables.DATA
997                + "     SET " + Data.DATA_VERSION + "=OLD." + Data.DATA_VERSION + "+1 "
998                + "     WHERE " + Data._ID + "=OLD." + Data._ID + ";"
999                + "   UPDATE " + Tables.RAW_CONTACTS
1000                + "     SET " + RawContacts.VERSION + "=" + RawContacts.VERSION + "+1 "
1001                + "     WHERE " + RawContacts._ID + "=OLD." + Data.RAW_CONTACT_ID + ";"
1002                + " END");
1003
1004        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.DATA + "_deleted;");
1005        db.execSQL("CREATE TRIGGER " + Tables.DATA + "_deleted BEFORE DELETE ON " + Tables.DATA
1006                + " BEGIN "
1007                + "   UPDATE " + Tables.RAW_CONTACTS
1008                + "     SET " + RawContacts.VERSION + "=" + RawContacts.VERSION + "+1 "
1009                + "     WHERE " + RawContacts._ID + "=OLD." + Data.RAW_CONTACT_ID + ";"
1010                + "   DELETE FROM " + Tables.PHONE_LOOKUP
1011                + "     WHERE " + PhoneLookupColumns.DATA_ID + "=OLD." + Data._ID + ";"
1012                + "   DELETE FROM " + Tables.STATUS_UPDATES
1013                + "     WHERE " + StatusUpdatesColumns.DATA_ID + "=OLD." + Data._ID + ";"
1014                + "   DELETE FROM " + Tables.NAME_LOOKUP
1015                + "     WHERE " + NameLookupColumns.DATA_ID + "=OLD." + Data._ID + ";"
1016                + " END");
1017
1018
1019        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.GROUPS + "_updated1;");
1020        db.execSQL("CREATE TRIGGER " + Tables.GROUPS + "_updated1 "
1021                + "   AFTER UPDATE ON " + Tables.GROUPS
1022                + " BEGIN "
1023                + "   UPDATE " + Tables.GROUPS
1024                + "     SET "
1025                +         Groups.VERSION + "=OLD." + Groups.VERSION + "+1"
1026                + "     WHERE " + Groups._ID + "=OLD." + Groups._ID + ";"
1027                + " END");
1028    }
1029
1030    private static void createContactsIndexes(SQLiteDatabase db) {
1031        db.execSQL("DROP INDEX IF EXISTS name_lookup_index");
1032        db.execSQL("CREATE INDEX name_lookup_index ON " + Tables.NAME_LOOKUP + " (" +
1033                NameLookupColumns.NORMALIZED_NAME + "," +
1034                NameLookupColumns.NAME_TYPE + ", " +
1035                NameLookupColumns.RAW_CONTACT_ID + ", " +
1036                NameLookupColumns.DATA_ID +
1037        ");");
1038
1039        db.execSQL("DROP INDEX IF EXISTS raw_contact_sort_key1_index");
1040        db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
1041                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1042                RawContacts.SORT_KEY_PRIMARY +
1043        ");");
1044
1045        db.execSQL("DROP INDEX IF EXISTS raw_contact_sort_key2_index");
1046        db.execSQL("CREATE INDEX raw_contact_sort_key2_index ON " + Tables.RAW_CONTACTS + " (" +
1047                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1048                RawContacts.SORT_KEY_ALTERNATIVE +
1049        ");");
1050    }
1051
1052    private static void createContactsViews(SQLiteDatabase db) {
1053        db.execSQL("DROP VIEW IF EXISTS " + Views.CONTACTS_ALL + ";");
1054        db.execSQL("DROP VIEW IF EXISTS " + Views.CONTACTS_RESTRICTED + ";");
1055        db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_ALL + ";");
1056        db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_RESTRICTED + ";");
1057        db.execSQL("DROP VIEW IF EXISTS " + Views.RAW_CONTACTS_ALL + ";");
1058        db.execSQL("DROP VIEW IF EXISTS " + Views.RAW_CONTACTS_RESTRICTED + ";");
1059
1060        String dataColumns =
1061                Data.IS_PRIMARY + ", "
1062                + Data.IS_SUPER_PRIMARY + ", "
1063                + Data.DATA_VERSION + ", "
1064                + PackagesColumns.PACKAGE + " AS " + Data.RES_PACKAGE + ","
1065                + MimetypesColumns.MIMETYPE + " AS " + Data.MIMETYPE + ", "
1066                + Data.DATA1 + ", "
1067                + Data.DATA2 + ", "
1068                + Data.DATA3 + ", "
1069                + Data.DATA4 + ", "
1070                + Data.DATA5 + ", "
1071                + Data.DATA6 + ", "
1072                + Data.DATA7 + ", "
1073                + Data.DATA8 + ", "
1074                + Data.DATA9 + ", "
1075                + Data.DATA10 + ", "
1076                + Data.DATA11 + ", "
1077                + Data.DATA12 + ", "
1078                + Data.DATA13 + ", "
1079                + Data.DATA14 + ", "
1080                + Data.DATA15 + ", "
1081                + Data.SYNC1 + ", "
1082                + Data.SYNC2 + ", "
1083                + Data.SYNC3 + ", "
1084                + Data.SYNC4;
1085
1086        String syncColumns =
1087                RawContactsColumns.CONCRETE_ACCOUNT_NAME + " AS " + RawContacts.ACCOUNT_NAME + ","
1088                + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " AS " + RawContacts.ACCOUNT_TYPE + ","
1089                + RawContactsColumns.CONCRETE_SOURCE_ID + " AS " + RawContacts.SOURCE_ID + ","
1090                + RawContactsColumns.CONCRETE_NAME_VERIFIED + " AS " + RawContacts.NAME_VERIFIED + ","
1091                + RawContactsColumns.CONCRETE_VERSION + " AS " + RawContacts.VERSION + ","
1092                + RawContactsColumns.CONCRETE_DIRTY + " AS " + RawContacts.DIRTY + ","
1093                + RawContactsColumns.CONCRETE_SYNC1 + " AS " + RawContacts.SYNC1 + ","
1094                + RawContactsColumns.CONCRETE_SYNC2 + " AS " + RawContacts.SYNC2 + ","
1095                + RawContactsColumns.CONCRETE_SYNC3 + " AS " + RawContacts.SYNC3 + ","
1096                + RawContactsColumns.CONCRETE_SYNC4 + " AS " + RawContacts.SYNC4;
1097
1098        String contactOptionColumns =
1099                ContactsColumns.CONCRETE_CUSTOM_RINGTONE
1100                        + " AS " + RawContacts.CUSTOM_RINGTONE + ","
1101                + ContactsColumns.CONCRETE_SEND_TO_VOICEMAIL
1102                        + " AS " + RawContacts.SEND_TO_VOICEMAIL + ","
1103                + ContactsColumns.CONCRETE_LAST_TIME_CONTACTED
1104                        + " AS " + RawContacts.LAST_TIME_CONTACTED + ","
1105                + ContactsColumns.CONCRETE_TIMES_CONTACTED
1106                        + " AS " + RawContacts.TIMES_CONTACTED + ","
1107                + ContactsColumns.CONCRETE_STARRED
1108                        + " AS " + RawContacts.STARRED;
1109
1110        String contactNameColumns =
1111                "name_raw_contact." + RawContacts.DISPLAY_NAME_SOURCE
1112                        + " AS " + Contacts.DISPLAY_NAME_SOURCE + ", "
1113                + "name_raw_contact." + RawContacts.DISPLAY_NAME_PRIMARY
1114                        + " AS " + Contacts.DISPLAY_NAME_PRIMARY + ", "
1115                + "name_raw_contact." + RawContacts.DISPLAY_NAME_ALTERNATIVE
1116                        + " AS " + Contacts.DISPLAY_NAME_ALTERNATIVE + ", "
1117                + "name_raw_contact." + RawContacts.PHONETIC_NAME
1118                        + " AS " + Contacts.PHONETIC_NAME + ", "
1119                + "name_raw_contact." + RawContacts.PHONETIC_NAME_STYLE
1120                        + " AS " + Contacts.PHONETIC_NAME_STYLE + ", "
1121                + "name_raw_contact." + RawContacts.SORT_KEY_PRIMARY
1122                        + " AS " + Contacts.SORT_KEY_PRIMARY + ", "
1123                + "name_raw_contact." + RawContacts.SORT_KEY_ALTERNATIVE
1124                        + " AS " + Contacts.SORT_KEY_ALTERNATIVE + ", "
1125                + "name_raw_contact." + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP
1126                        + " AS " + Contacts.IN_VISIBLE_GROUP;
1127
1128        String dataSelect = "SELECT "
1129                + DataColumns.CONCRETE_ID + " AS " + Data._ID + ","
1130                + Data.RAW_CONTACT_ID + ", "
1131                + RawContactsColumns.CONCRETE_CONTACT_ID + " AS " + RawContacts.CONTACT_ID + ", "
1132                + syncColumns + ", "
1133                + dataColumns + ", "
1134                + contactOptionColumns + ", "
1135                + contactNameColumns + ", "
1136                + Contacts.LOOKUP_KEY + ", "
1137                + Contacts.PHOTO_ID + ", "
1138                + ContactsColumns.LAST_STATUS_UPDATE_ID + ", "
1139                + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID
1140                + " FROM " + Tables.DATA
1141                + " JOIN " + Tables.MIMETYPES + " ON ("
1142                +   DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")"
1143                + " JOIN " + Tables.RAW_CONTACTS + " ON ("
1144                +   DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")"
1145                + " JOIN " + Tables.CONTACTS + " ON ("
1146                +   RawContactsColumns.CONCRETE_CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + ")"
1147                + " JOIN " + Tables.RAW_CONTACTS + " AS name_raw_contact ON("
1148                +   Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")"
1149                + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON ("
1150                +   DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")"
1151                + " LEFT OUTER JOIN " + Tables.GROUPS + " ON ("
1152                +   MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE
1153                +   "' AND " + GroupsColumns.CONCRETE_ID + "="
1154                        + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")";
1155
1156        db.execSQL("CREATE VIEW " + Views.DATA_ALL + " AS " + dataSelect);
1157        db.execSQL("CREATE VIEW " + Views.DATA_RESTRICTED + " AS " + dataSelect + " WHERE "
1158                + RawContactsColumns.CONCRETE_IS_RESTRICTED + "=0");
1159
1160        String rawContactOptionColumns =
1161                RawContacts.CUSTOM_RINGTONE + ","
1162                + RawContacts.SEND_TO_VOICEMAIL + ","
1163                + RawContacts.LAST_TIME_CONTACTED + ","
1164                + RawContacts.TIMES_CONTACTED + ","
1165                + RawContacts.STARRED;
1166
1167        String rawContactsSelect = "SELECT "
1168                + RawContactsColumns.CONCRETE_ID + " AS " + RawContacts._ID + ","
1169                + RawContacts.CONTACT_ID + ", "
1170                + RawContacts.AGGREGATION_MODE + ", "
1171                + RawContacts.DELETED + ", "
1172                + RawContacts.DISPLAY_NAME_SOURCE  + ", "
1173                + RawContacts.DISPLAY_NAME_PRIMARY  + ", "
1174                + RawContacts.DISPLAY_NAME_ALTERNATIVE  + ", "
1175                + RawContacts.PHONETIC_NAME  + ", "
1176                + RawContacts.PHONETIC_NAME_STYLE  + ", "
1177                + RawContacts.SORT_KEY_PRIMARY  + ", "
1178                + RawContacts.SORT_KEY_ALTERNATIVE + ", "
1179                + rawContactOptionColumns + ", "
1180                + syncColumns
1181                + " FROM " + Tables.RAW_CONTACTS;
1182
1183        db.execSQL("CREATE VIEW " + Views.RAW_CONTACTS_ALL + " AS " + rawContactsSelect);
1184        db.execSQL("CREATE VIEW " + Views.RAW_CONTACTS_RESTRICTED + " AS " + rawContactsSelect
1185                + " WHERE " + RawContacts.IS_RESTRICTED + "=0");
1186
1187        String contactsColumns =
1188                ContactsColumns.CONCRETE_CUSTOM_RINGTONE
1189                        + " AS " + Contacts.CUSTOM_RINGTONE + ", "
1190                + contactNameColumns + ", "
1191                + Contacts.HAS_PHONE_NUMBER + ", "
1192                + Contacts.LOOKUP_KEY + ", "
1193                + Contacts.PHOTO_ID + ", "
1194                + ContactsColumns.CONCRETE_LAST_TIME_CONTACTED
1195                        + " AS " + Contacts.LAST_TIME_CONTACTED + ", "
1196                + ContactsColumns.CONCRETE_SEND_TO_VOICEMAIL
1197                        + " AS " + Contacts.SEND_TO_VOICEMAIL + ", "
1198                + ContactsColumns.CONCRETE_STARRED
1199                        + " AS " + Contacts.STARRED + ", "
1200                + ContactsColumns.CONCRETE_TIMES_CONTACTED
1201                        + " AS " + Contacts.TIMES_CONTACTED + ", "
1202                + ContactsColumns.LAST_STATUS_UPDATE_ID;
1203
1204        String contactsSelect = "SELECT "
1205                + ContactsColumns.CONCRETE_ID + " AS " + Contacts._ID + ","
1206                + contactsColumns
1207                + " FROM " + Tables.CONTACTS
1208                + " JOIN " + Tables.RAW_CONTACTS + " AS name_raw_contact ON("
1209                +   Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")";
1210
1211        db.execSQL("CREATE VIEW " + Views.CONTACTS_ALL + " AS " + contactsSelect);
1212        db.execSQL("CREATE VIEW " + Views.CONTACTS_RESTRICTED + " AS " + contactsSelect
1213                + " WHERE " + ContactsColumns.SINGLE_IS_RESTRICTED + "=0");
1214    }
1215
1216    private static void createGroupsView(SQLiteDatabase db) {
1217        db.execSQL("DROP VIEW IF EXISTS " + Views.GROUPS_ALL + ";");
1218        String groupsColumns =
1219                Groups.ACCOUNT_NAME + ","
1220                + Groups.ACCOUNT_TYPE + ","
1221                + Groups.SOURCE_ID + ","
1222                + Groups.VERSION + ","
1223                + Groups.DIRTY + ","
1224                + Groups.TITLE + ","
1225                + Groups.TITLE_RES + ","
1226                + Groups.NOTES + ","
1227                + Groups.SYSTEM_ID + ","
1228                + Groups.DELETED + ","
1229                + Groups.GROUP_VISIBLE + ","
1230                + Groups.SHOULD_SYNC + ","
1231                + Groups.SYNC1 + ","
1232                + Groups.SYNC2 + ","
1233                + Groups.SYNC3 + ","
1234                + Groups.SYNC4 + ","
1235                + PackagesColumns.PACKAGE + " AS " + Groups.RES_PACKAGE;
1236
1237        String groupsSelect = "SELECT "
1238                + GroupsColumns.CONCRETE_ID + " AS " + Groups._ID + ","
1239                + groupsColumns
1240                + " FROM " + Tables.GROUPS_JOIN_PACKAGES;
1241
1242        db.execSQL("CREATE VIEW " + Views.GROUPS_ALL + " AS " + groupsSelect);
1243    }
1244
1245    private static void createContactEntitiesView(SQLiteDatabase db) {
1246        db.execSQL("DROP VIEW IF EXISTS " + Tables.CONTACT_ENTITIES + ";");
1247        db.execSQL("DROP VIEW IF EXISTS " + Tables.CONTACT_ENTITIES_RESTRICTED + ";");
1248
1249        String contactEntitiesSelect = "SELECT "
1250                + RawContactsColumns.CONCRETE_ACCOUNT_NAME + " AS " + RawContacts.ACCOUNT_NAME + ","
1251                + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " AS " + RawContacts.ACCOUNT_TYPE + ","
1252                + RawContactsColumns.CONCRETE_SOURCE_ID + " AS " + RawContacts.SOURCE_ID + ","
1253                + RawContactsColumns.CONCRETE_VERSION + " AS " + RawContacts.VERSION + ","
1254                + RawContactsColumns.CONCRETE_DIRTY + " AS " + RawContacts.DIRTY + ","
1255                + RawContactsColumns.CONCRETE_DELETED + " AS " + RawContacts.DELETED + ","
1256                + RawContactsColumns.CONCRETE_NAME_VERIFIED + " AS " + RawContacts.NAME_VERIFIED + ","
1257                + PackagesColumns.PACKAGE + " AS " + Data.RES_PACKAGE + ","
1258                + RawContacts.CONTACT_ID + ", "
1259                + RawContactsColumns.CONCRETE_SYNC1 + " AS " + RawContacts.SYNC1 + ", "
1260                + RawContactsColumns.CONCRETE_SYNC2 + " AS " + RawContacts.SYNC2 + ", "
1261                + RawContactsColumns.CONCRETE_SYNC3 + " AS " + RawContacts.SYNC3 + ", "
1262                + RawContactsColumns.CONCRETE_SYNC4 + " AS " + RawContacts.SYNC4 + ", "
1263                + Data.MIMETYPE + ", "
1264                + Data.DATA1 + ", "
1265                + Data.DATA2 + ", "
1266                + Data.DATA3 + ", "
1267                + Data.DATA4 + ", "
1268                + Data.DATA5 + ", "
1269                + Data.DATA6 + ", "
1270                + Data.DATA7 + ", "
1271                + Data.DATA8 + ", "
1272                + Data.DATA9 + ", "
1273                + Data.DATA10 + ", "
1274                + Data.DATA11 + ", "
1275                + Data.DATA12 + ", "
1276                + Data.DATA13 + ", "
1277                + Data.DATA14 + ", "
1278                + Data.DATA15 + ", "
1279                + Data.SYNC1 + ", "
1280                + Data.SYNC2 + ", "
1281                + Data.SYNC3 + ", "
1282                + Data.SYNC4 + ", "
1283                + RawContactsColumns.CONCRETE_ID + " AS " + RawContacts._ID + ", "
1284                + Data.IS_PRIMARY + ", "
1285                + Data.IS_SUPER_PRIMARY + ", "
1286                + Data.DATA_VERSION + ", "
1287                + DataColumns.CONCRETE_ID + " AS " + RawContacts.Entity.DATA_ID + ","
1288                + RawContactsColumns.CONCRETE_STARRED + " AS " + RawContacts.STARRED + ","
1289                + RawContactsColumns.CONCRETE_IS_RESTRICTED + " AS "
1290                        + RawContacts.IS_RESTRICTED + ","
1291                + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID
1292                + " FROM " + Tables.RAW_CONTACTS
1293                + " LEFT OUTER JOIN " + Tables.DATA + " ON ("
1294                +   DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")"
1295                + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON ("
1296                +   DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")"
1297                + " LEFT OUTER JOIN " + Tables.MIMETYPES + " ON ("
1298                +   DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")"
1299                + " LEFT OUTER JOIN " + Tables.GROUPS + " ON ("
1300                +   MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE
1301                +   "' AND " + GroupsColumns.CONCRETE_ID + "="
1302                + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")";
1303
1304        db.execSQL("CREATE VIEW " + Tables.CONTACT_ENTITIES + " AS "
1305                + contactEntitiesSelect);
1306        db.execSQL("CREATE VIEW " + Tables.CONTACT_ENTITIES_RESTRICTED + " AS "
1307                + contactEntitiesSelect + " WHERE " + RawContacts.IS_RESTRICTED + "=0");
1308    }
1309
1310    @Override
1311    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
1312        if (oldVersion < 99) {
1313            Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion
1314                    + ", data will be lost!");
1315
1316            db.execSQL("DROP TABLE IF EXISTS " + Tables.CONTACTS + ";");
1317            db.execSQL("DROP TABLE IF EXISTS " + Tables.RAW_CONTACTS + ";");
1318            db.execSQL("DROP TABLE IF EXISTS " + Tables.PACKAGES + ";");
1319            db.execSQL("DROP TABLE IF EXISTS " + Tables.MIMETYPES + ";");
1320            db.execSQL("DROP TABLE IF EXISTS " + Tables.DATA + ";");
1321            db.execSQL("DROP TABLE IF EXISTS " + Tables.PHONE_LOOKUP + ";");
1322            db.execSQL("DROP TABLE IF EXISTS " + Tables.NAME_LOOKUP + ";");
1323            db.execSQL("DROP TABLE IF EXISTS " + Tables.NICKNAME_LOOKUP + ";");
1324            db.execSQL("DROP TABLE IF EXISTS " + Tables.GROUPS + ";");
1325            db.execSQL("DROP TABLE IF EXISTS " + Tables.ACTIVITIES + ";");
1326            db.execSQL("DROP TABLE IF EXISTS " + Tables.CALLS + ";");
1327            db.execSQL("DROP TABLE IF EXISTS " + Tables.SETTINGS + ";");
1328            db.execSQL("DROP TABLE IF EXISTS " + Tables.STATUS_UPDATES + ";");
1329
1330            // TODO: we should not be dropping agg_exceptions and contact_options. In case that
1331            // table's schema changes, we should try to preserve the data, because it was entered
1332            // by the user and has never been synched to the server.
1333            db.execSQL("DROP TABLE IF EXISTS " + Tables.AGGREGATION_EXCEPTIONS + ";");
1334
1335            onCreate(db);
1336            return;
1337        }
1338
1339        Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion);
1340
1341        boolean upgradeViewsAndTriggers = false;
1342        boolean upgradeNameLookup = false;
1343
1344        if (oldVersion == 99) {
1345            upgradeViewsAndTriggers = true;
1346            oldVersion++;
1347        }
1348
1349        if (oldVersion == 100) {
1350            db.execSQL("CREATE INDEX IF NOT EXISTS mimetypes_mimetype_index ON "
1351                    + Tables.MIMETYPES + " ("
1352                            + MimetypesColumns.MIMETYPE + ","
1353                            + MimetypesColumns._ID + ");");
1354            updateIndexStats(db, Tables.MIMETYPES,
1355                    "mimetypes_mimetype_index", "50 1 1");
1356
1357            upgradeViewsAndTriggers = true;
1358            oldVersion++;
1359        }
1360
1361        if (oldVersion == 101) {
1362            upgradeViewsAndTriggers = true;
1363            oldVersion++;
1364        }
1365
1366        if (oldVersion == 102) {
1367            upgradeViewsAndTriggers = true;
1368            oldVersion++;
1369        }
1370
1371        if (oldVersion == 103) {
1372            upgradeViewsAndTriggers = true;
1373            oldVersion++;
1374        }
1375
1376        if (oldVersion == 104 || oldVersion == 201) {
1377            LegacyApiSupport.createSettingsTable(db);
1378            upgradeViewsAndTriggers = true;
1379            oldVersion++;
1380        }
1381
1382        if (oldVersion == 105) {
1383            upgradeToVersion202(db);
1384            upgradeNameLookup = true;
1385            oldVersion = 202;
1386        }
1387
1388        if (oldVersion == 202) {
1389            upgradeToVersion203(db);
1390            upgradeViewsAndTriggers = true;
1391            oldVersion++;
1392        }
1393
1394        if (oldVersion == 203) {
1395            upgradeViewsAndTriggers = true;
1396            oldVersion++;
1397        }
1398
1399        if (oldVersion == 204) {
1400            upgradeToVersion205(db);
1401            upgradeViewsAndTriggers = true;
1402            oldVersion++;
1403        }
1404
1405        if (oldVersion == 205) {
1406            upgrateToVersion206(db);
1407            upgradeViewsAndTriggers = true;
1408            oldVersion++;
1409        }
1410
1411        if (oldVersion == 206) {
1412            upgradeToVersion300(db);
1413            oldVersion = 300;
1414        }
1415
1416        if (oldVersion == 300) {
1417            upgradeViewsAndTriggers = true;
1418            oldVersion = 301;
1419        }
1420
1421        if (oldVersion == 301) {
1422            upgradeViewsAndTriggers = true;
1423            oldVersion = 302;
1424        }
1425
1426        if (oldVersion == 302) {
1427            upgradeEmailToVersion303(db);
1428            upgradeNicknameToVersion303(db);
1429            oldVersion = 303;
1430        }
1431
1432        if (oldVersion == 303) {
1433            upgradeToVersion304(db);
1434            oldVersion = 304;
1435        }
1436
1437        if (upgradeViewsAndTriggers) {
1438            createContactsViews(db);
1439            createGroupsView(db);
1440            createContactEntitiesView(db);
1441            createContactsTriggers(db);
1442            createContactsIndexes(db);
1443            LegacyApiSupport.createViews(db);
1444            updateSqliteStats(db);
1445            mReopenDatabase = true;
1446        }
1447
1448        if (upgradeNameLookup) {
1449            rebuildNameLookup(db);
1450        }
1451
1452        if (oldVersion != newVersion) {
1453            throw new IllegalStateException(
1454                    "error upgrading the database to version " + newVersion);
1455        }
1456    }
1457
1458    private void upgradeToVersion304(SQLiteDatabase db) {
1459        // Mimetype table requires an index on mime type
1460        db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS mime_type ON " + Tables.MIMETYPES + " (" +
1461                MimetypesColumns.MIMETYPE +
1462        ");");
1463    }
1464
1465    private void upgradeToVersion202(SQLiteDatabase db) {
1466        db.execSQL(
1467                "ALTER TABLE " + Tables.PHONE_LOOKUP +
1468                " ADD " + PhoneLookupColumns.MIN_MATCH + " TEXT;");
1469
1470        db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" +
1471                PhoneLookupColumns.MIN_MATCH + "," +
1472                PhoneLookupColumns.RAW_CONTACT_ID + "," +
1473                PhoneLookupColumns.DATA_ID +
1474        ");");
1475
1476        updateIndexStats(db, Tables.PHONE_LOOKUP,
1477                "phone_lookup_min_match_index", "10000 2 2 1");
1478
1479        SQLiteStatement update = db.compileStatement(
1480                "UPDATE " + Tables.PHONE_LOOKUP +
1481                " SET " + PhoneLookupColumns.MIN_MATCH + "=?" +
1482                " WHERE " + PhoneLookupColumns.DATA_ID + "=?");
1483
1484        // Populate the new column
1485        Cursor c = db.query(Tables.PHONE_LOOKUP + " JOIN " + Tables.DATA +
1486                " ON (" + PhoneLookupColumns.DATA_ID + "=" + DataColumns.CONCRETE_ID + ")",
1487                new String[]{Data._ID, Phone.NUMBER}, null, null, null, null, null);
1488        try {
1489            while (c.moveToNext()) {
1490                long dataId = c.getLong(0);
1491                String number = c.getString(1);
1492                if (!TextUtils.isEmpty(number)) {
1493                    update.bindString(1, PhoneNumberUtils.toCallerIDMinMatch(number));
1494                    update.bindLong(2, dataId);
1495                    update.execute();
1496                }
1497            }
1498        } finally {
1499            c.close();
1500        }
1501    }
1502
1503    private void upgradeToVersion203(SQLiteDatabase db) {
1504        db.execSQL(
1505                "ALTER TABLE " + Tables.CONTACTS +
1506                " ADD " + Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)");
1507        db.execSQL(
1508                "ALTER TABLE " + Tables.RAW_CONTACTS +
1509                " ADD " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP
1510                        + " INTEGER NOT NULL DEFAULT 0");
1511
1512        // For each Contact, find the RawContact that contributed the display name
1513        db.execSQL(
1514                "UPDATE " + Tables.CONTACTS +
1515                " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" +
1516                        " SELECT " + RawContacts._ID +
1517                        " FROM " + Tables.RAW_CONTACTS +
1518                        " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
1519                        " AND " + RawContactsColumns.CONCRETE_DISPLAY_NAME + "=" +
1520                                Tables.CONTACTS + "." + Contacts.DISPLAY_NAME +
1521                        " ORDER BY " + RawContacts._ID +
1522                        " LIMIT 1)"
1523        );
1524
1525        db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" +
1526                Contacts.NAME_RAW_CONTACT_ID +
1527        ");");
1528
1529        // If for some unknown reason we missed some names, let's make sure there are
1530        // no contacts without a name, picking a raw contact "at random".
1531        db.execSQL(
1532                "UPDATE " + Tables.CONTACTS +
1533                " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" +
1534                        " SELECT " + RawContacts._ID +
1535                        " FROM " + Tables.RAW_CONTACTS +
1536                        " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
1537                        " ORDER BY " + RawContacts._ID +
1538                        " LIMIT 1)" +
1539                " WHERE " + Contacts.NAME_RAW_CONTACT_ID + " IS NULL"
1540        );
1541
1542        // Wipe out DISPLAY_NAME on the Contacts table as it is no longer in use.
1543        db.execSQL(
1544                "UPDATE " + Tables.CONTACTS +
1545                " SET " + Contacts.DISPLAY_NAME + "=NULL"
1546        );
1547
1548        // Copy the IN_VISIBLE_GROUP flag down to all raw contacts to allow
1549        // indexing on (display_name, in_visible_group)
1550        db.execSQL(
1551                "UPDATE " + Tables.RAW_CONTACTS +
1552                " SET " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "=(" +
1553                        "SELECT " + Contacts.IN_VISIBLE_GROUP +
1554                        " FROM " + Tables.CONTACTS +
1555                        " WHERE " + Contacts._ID + "=" + RawContacts.CONTACT_ID + ")" +
1556                " WHERE " + RawContacts.CONTACT_ID + " NOT NULL"
1557        );
1558
1559        db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
1560                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1561                RawContactsColumns.DISPLAY_NAME + " COLLATE LOCALIZED ASC" +
1562        ");");
1563
1564        db.execSQL("DROP INDEX contacts_visible_index");
1565        db.execSQL("CREATE INDEX contacts_visible_index ON " + Tables.CONTACTS + " (" +
1566                Contacts.IN_VISIBLE_GROUP +
1567        ");");
1568    }
1569
1570    private void upgradeToVersion205(SQLiteDatabase db) {
1571        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1572                + " ADD " + RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT;");
1573        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1574                + " ADD " + RawContacts.PHONETIC_NAME + " TEXT;");
1575        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1576                + " ADD " + RawContacts.PHONETIC_NAME_STYLE + " INTEGER;");
1577        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1578                + " ADD " + RawContacts.SORT_KEY_PRIMARY + " TEXT COLLATE LOCALIZED;");
1579        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1580                + " ADD " + RawContacts.SORT_KEY_ALTERNATIVE + " TEXT COLLATE LOCALIZED;");
1581
1582        final Locale locale = Locale.getDefault();
1583
1584        NameSplitter splitter = new NameSplitter(
1585                mContext.getString(com.android.internal.R.string.common_name_prefixes),
1586                mContext.getString(com.android.internal.R.string.common_last_name_prefixes),
1587                mContext.getString(com.android.internal.R.string.common_name_suffixes),
1588                mContext.getString(com.android.internal.R.string.common_name_conjunctions),
1589                locale);
1590
1591        SQLiteStatement rawContactUpdate = db.compileStatement(
1592                "UPDATE " + Tables.RAW_CONTACTS +
1593                " SET " +
1594                        RawContacts.DISPLAY_NAME_PRIMARY + "=?," +
1595                        RawContacts.DISPLAY_NAME_ALTERNATIVE + "=?," +
1596                        RawContacts.PHONETIC_NAME + "=?," +
1597                        RawContacts.PHONETIC_NAME_STYLE + "=?," +
1598                        RawContacts.SORT_KEY_PRIMARY + "=?," +
1599                        RawContacts.SORT_KEY_ALTERNATIVE + "=?" +
1600                " WHERE " + RawContacts._ID + "=?");
1601
1602        upgradeStructuredNamesToVersion205(db, rawContactUpdate, splitter);
1603        upgradeOrganizationsToVersion205(db, rawContactUpdate, splitter);
1604
1605        db.execSQL("DROP INDEX raw_contact_sort_key1_index");
1606        db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
1607                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1608                RawContacts.SORT_KEY_PRIMARY +
1609        ");");
1610
1611        db.execSQL("CREATE INDEX raw_contact_sort_key2_index ON " + Tables.RAW_CONTACTS + " (" +
1612                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1613                RawContacts.SORT_KEY_ALTERNATIVE +
1614        ");");
1615    }
1616
1617    private interface StructName205Query {
1618        String TABLE = Tables.DATA_JOIN_RAW_CONTACTS;
1619
1620        String COLUMNS[] = {
1621                DataColumns.CONCRETE_ID,
1622                Data.RAW_CONTACT_ID,
1623                RawContacts.DISPLAY_NAME_SOURCE,
1624                RawContacts.DISPLAY_NAME_PRIMARY,
1625                StructuredName.PREFIX,
1626                StructuredName.GIVEN_NAME,
1627                StructuredName.MIDDLE_NAME,
1628                StructuredName.FAMILY_NAME,
1629                StructuredName.SUFFIX,
1630                StructuredName.PHONETIC_FAMILY_NAME,
1631                StructuredName.PHONETIC_MIDDLE_NAME,
1632                StructuredName.PHONETIC_GIVEN_NAME,
1633        };
1634
1635        int ID = 0;
1636        int RAW_CONTACT_ID = 1;
1637        int DISPLAY_NAME_SOURCE = 2;
1638        int DISPLAY_NAME = 3;
1639        int PREFIX = 4;
1640        int GIVEN_NAME = 5;
1641        int MIDDLE_NAME = 6;
1642        int FAMILY_NAME = 7;
1643        int SUFFIX = 8;
1644        int PHONETIC_FAMILY_NAME = 9;
1645        int PHONETIC_MIDDLE_NAME = 10;
1646        int PHONETIC_GIVEN_NAME = 11;
1647    }
1648
1649    private void upgradeStructuredNamesToVersion205(SQLiteDatabase db,
1650            SQLiteStatement rawContactUpdate, NameSplitter splitter) {
1651
1652        // Process structured names to detect the style of the full name and phonetic name
1653
1654        long mMimeType;
1655        try {
1656            mMimeType = DatabaseUtils.longForQuery(db,
1657                    "SELECT " + MimetypesColumns._ID +
1658                    " FROM " + Tables.MIMETYPES +
1659                    " WHERE " + MimetypesColumns.MIMETYPE
1660                            + "='" + StructuredName.CONTENT_ITEM_TYPE + "'", null);
1661        } catch (SQLiteDoneException e) {
1662            // No structured names in the database
1663            return;
1664        }
1665
1666        SQLiteStatement structuredNameUpdate = db.compileStatement(
1667                "UPDATE " + Tables.DATA +
1668                " SET " +
1669                        StructuredName.FULL_NAME_STYLE + "=?," +
1670                        StructuredName.DISPLAY_NAME + "=?," +
1671                        StructuredName.PHONETIC_NAME_STYLE + "=?" +
1672                " WHERE " + Data._ID + "=?");
1673
1674        NameSplitter.Name name = new NameSplitter.Name();
1675        StringBuilder sb = new StringBuilder();
1676        Cursor cursor = db.query(StructName205Query.TABLE,
1677                StructName205Query.COLUMNS,
1678                DataColumns.MIMETYPE_ID + "=" + mMimeType, null, null, null, null);
1679        try {
1680            while (cursor.moveToNext()) {
1681                long dataId = cursor.getLong(StructName205Query.ID);
1682                long rawContactId = cursor.getLong(StructName205Query.RAW_CONTACT_ID);
1683                int displayNameSource = cursor.getInt(StructName205Query.DISPLAY_NAME_SOURCE);
1684                String displayName = cursor.getString(StructName205Query.DISPLAY_NAME);
1685
1686                name.clear();
1687                name.prefix = cursor.getString(StructName205Query.PREFIX);
1688                name.givenNames = cursor.getString(StructName205Query.GIVEN_NAME);
1689                name.middleName = cursor.getString(StructName205Query.MIDDLE_NAME);
1690                name.familyName = cursor.getString(StructName205Query.FAMILY_NAME);
1691                name.suffix = cursor.getString(StructName205Query.SUFFIX);
1692                name.phoneticFamilyName = cursor.getString(StructName205Query.PHONETIC_FAMILY_NAME);
1693                name.phoneticMiddleName = cursor.getString(StructName205Query.PHONETIC_MIDDLE_NAME);
1694                name.phoneticGivenName = cursor.getString(StructName205Query.PHONETIC_GIVEN_NAME);
1695
1696                upgradeNameToVersion205(dataId, rawContactId, displayNameSource, displayName, name,
1697                        structuredNameUpdate, rawContactUpdate, splitter, sb);
1698            }
1699        } finally {
1700            cursor.close();
1701        }
1702    }
1703
1704    private void upgradeNameToVersion205(long dataId, long rawContactId, int displayNameSource,
1705            String currentDisplayName, NameSplitter.Name name,
1706            SQLiteStatement structuredNameUpdate, SQLiteStatement rawContactUpdate,
1707            NameSplitter splitter, StringBuilder sb) {
1708
1709        splitter.guessNameStyle(name);
1710        name.fullNameStyle = splitter.getAdjustedFullNameStyle(name.fullNameStyle);
1711        String displayName = splitter.join(name, true);
1712
1713        structuredNameUpdate.bindLong(1, name.fullNameStyle);
1714        DatabaseUtils.bindObjectToProgram(structuredNameUpdate, 2, displayName);
1715        structuredNameUpdate.bindLong(3, name.phoneticNameStyle);
1716        structuredNameUpdate.bindLong(4, dataId);
1717        structuredNameUpdate.execute();
1718
1719        if (displayNameSource == DisplayNameSources.STRUCTURED_NAME) {
1720            String displayNameAlternative = splitter.join(name, false);
1721            String phoneticName = splitter.joinPhoneticName(name);
1722            String sortKey = null;
1723            String sortKeyAlternative = null;
1724
1725            if (phoneticName != null) {
1726                sortKey = sortKeyAlternative = phoneticName;
1727            } else if (name.fullNameStyle == FullNameStyle.CHINESE) {
1728                sortKey = sortKeyAlternative = splitter.convertHanziToPinyin(displayName);
1729            }
1730
1731            if (sortKey == null) {
1732                sortKey = displayName;
1733                sortKeyAlternative = displayNameAlternative;
1734            }
1735
1736            updateRawContact205(rawContactUpdate, rawContactId, displayName,
1737                    displayNameAlternative, name.phoneticNameStyle, phoneticName, sortKey,
1738                    sortKeyAlternative);
1739        }
1740    }
1741
1742    private interface Organization205Query {
1743        String TABLE = Tables.DATA_JOIN_RAW_CONTACTS;
1744
1745        String COLUMNS[] = {
1746                DataColumns.CONCRETE_ID,
1747                Data.RAW_CONTACT_ID,
1748                Organization.COMPANY,
1749                Organization.PHONETIC_NAME,
1750        };
1751
1752        int ID = 0;
1753        int RAW_CONTACT_ID = 1;
1754        int COMPANY = 2;
1755        int PHONETIC_NAME = 3;
1756    }
1757
1758    private void upgradeOrganizationsToVersion205(SQLiteDatabase db,
1759            SQLiteStatement rawContactUpdate, NameSplitter splitter) {
1760        final long mimeType = lookupMimeTypeId(db, Organization.CONTENT_ITEM_TYPE);
1761
1762        SQLiteStatement organizationUpdate = db.compileStatement(
1763                "UPDATE " + Tables.DATA +
1764                " SET " +
1765                        Organization.PHONETIC_NAME_STYLE + "=?" +
1766                " WHERE " + Data._ID + "=?");
1767
1768        Cursor cursor = db.query(Organization205Query.TABLE, Organization205Query.COLUMNS,
1769                DataColumns.MIMETYPE_ID + "=" + mimeType + " AND "
1770                        + RawContacts.DISPLAY_NAME_SOURCE + "=" + DisplayNameSources.ORGANIZATION,
1771                null, null, null, null);
1772        try {
1773            while (cursor.moveToNext()) {
1774                long dataId = cursor.getLong(Organization205Query.ID);
1775                long rawContactId = cursor.getLong(Organization205Query.RAW_CONTACT_ID);
1776                String company = cursor.getString(Organization205Query.COMPANY);
1777                String phoneticName = cursor.getString(Organization205Query.PHONETIC_NAME);
1778
1779                int phoneticNameStyle = splitter.guessPhoneticNameStyle(phoneticName);
1780
1781                organizationUpdate.bindLong(1, phoneticNameStyle);
1782                organizationUpdate.bindLong(2, dataId);
1783                organizationUpdate.execute();
1784
1785                String sortKey = null;
1786                if (phoneticName == null && company != null) {
1787                    int nameStyle = splitter.guessFullNameStyle(company);
1788                    nameStyle = splitter.getAdjustedFullNameStyle(nameStyle);
1789                    if (nameStyle == FullNameStyle.CHINESE) {
1790                        sortKey = splitter.convertHanziToPinyin(company);
1791                    }
1792                }
1793
1794                if (sortKey == null) {
1795                    sortKey = company;
1796                }
1797
1798                updateRawContact205(rawContactUpdate, rawContactId, company,
1799                        company, phoneticNameStyle, phoneticName, sortKey, sortKey);
1800            }
1801        } finally {
1802            cursor.close();
1803        }
1804    }
1805
1806    private void updateRawContact205(SQLiteStatement rawContactUpdate, long rawContactId,
1807            String displayName, String displayNameAlternative, int phoneticNameStyle,
1808            String phoneticName, String sortKeyPrimary, String sortKeyAlternative) {
1809        bindString(rawContactUpdate, 1, displayName);
1810        bindString(rawContactUpdate, 2, displayNameAlternative);
1811        bindString(rawContactUpdate, 3, phoneticName);
1812        rawContactUpdate.bindLong(4, phoneticNameStyle);
1813        bindString(rawContactUpdate, 5, sortKeyPrimary);
1814        bindString(rawContactUpdate, 6, sortKeyAlternative);
1815        rawContactUpdate.bindLong(7, rawContactId);
1816        rawContactUpdate.execute();
1817    }
1818
1819    private void upgrateToVersion206(SQLiteDatabase db) {
1820        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1821                + " ADD " + RawContacts.NAME_VERIFIED + " INTEGER NOT NULL DEFAULT 0;");
1822    }
1823
1824    private interface Organization300Query {
1825        String TABLE = Tables.DATA;
1826
1827        String SELECTION = DataColumns.MIMETYPE_ID + "=?";
1828
1829        String COLUMNS[] = {
1830                Organization._ID,
1831                Organization.RAW_CONTACT_ID,
1832                Organization.COMPANY,
1833                Organization.TITLE
1834        };
1835
1836        int ID = 0;
1837        int RAW_CONTACT_ID = 1;
1838        int COMPANY = 2;
1839        int TITLE = 3;
1840    }
1841
1842    /**
1843     * Fix for the bug where name lookup records for organizations would get removed by
1844     * unrelated updates of the data rows.
1845     */
1846    private void upgradeToVersion300(SQLiteDatabase db) {
1847        final long mimeType = lookupMimeTypeId(db, Organization.CONTENT_ITEM_TYPE);
1848        if (mimeType == -1) {
1849            return;
1850        }
1851
1852        ContentValues values = new ContentValues();
1853
1854        // Find all data rows with the mime type "organization"
1855        Cursor cursor = db.query(Organization300Query.TABLE, Organization300Query.COLUMNS,
1856                Organization300Query.SELECTION, new String[] {String.valueOf(mimeType)},
1857                null, null, null);
1858        try {
1859            while (cursor.moveToNext()) {
1860                long dataId = cursor.getLong(Organization300Query.ID);
1861                long rawContactId = cursor.getLong(Organization300Query.RAW_CONTACT_ID);
1862                String company = cursor.getString(Organization300Query.COMPANY);
1863                String title = cursor.getString(Organization300Query.TITLE);
1864
1865                // First delete name lookup if there is any (chances are there won't be)
1866                db.delete(Tables.NAME_LOOKUP, NameLookupColumns.DATA_ID + "=?",
1867                        new String[]{String.valueOf(dataId)});
1868
1869                // Now insert two name lookup records: one for company name, one for title
1870                values.put(NameLookupColumns.DATA_ID, dataId);
1871                values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId);
1872                values.put(NameLookupColumns.NAME_TYPE, NameLookupType.ORGANIZATION);
1873
1874                if (!TextUtils.isEmpty(company)) {
1875                    values.put(NameLookupColumns.NORMALIZED_NAME,
1876                            NameNormalizer.normalize(company));
1877                    db.insert(Tables.NAME_LOOKUP, null, values);
1878                }
1879
1880                if (!TextUtils.isEmpty(title)) {
1881                    values.put(NameLookupColumns.NORMALIZED_NAME,
1882                            NameNormalizer.normalize(title));
1883                    db.insert(Tables.NAME_LOOKUP, null, values);
1884                }
1885            }
1886        } finally {
1887            cursor.close();
1888        }
1889    }
1890
1891    private static final class Upgrade303Query {
1892        public static final String TABLE = Tables.DATA;
1893
1894        public static final String SELECTION =
1895                DataColumns.MIMETYPE_ID + "=?" +
1896        		" AND " + Data._ID + " NOT IN " +
1897        		"(SELECT " + NameLookupColumns.DATA_ID + " FROM " + Tables.NAME_LOOKUP + ")" +
1898        		" AND " + Data.DATA1 + " NOT NULL";
1899
1900        public static final String COLUMNS[] = {
1901                Data._ID,
1902                Data.RAW_CONTACT_ID,
1903                Data.DATA1,
1904        };
1905
1906        public static final int ID = 0;
1907        public static final int RAW_CONTACT_ID = 1;
1908        public static final int DATA1 = 2;
1909    }
1910
1911    /**
1912     * The {@link ContactsProvider2#update} method was deleting name lookup for new
1913     * emails during the sync.  We need to restore the lost name lookup rows.
1914     */
1915    private void upgradeEmailToVersion303(SQLiteDatabase db) {
1916        final long mimeTypeId = lookupMimeTypeId(db, Email.CONTENT_ITEM_TYPE);
1917        if (mimeTypeId == -1) {
1918            return;
1919        }
1920
1921        ContentValues values = new ContentValues();
1922
1923        // Find all data rows with the mime type "email" that are missing name lookup
1924        Cursor cursor = db.query(Upgrade303Query.TABLE, Upgrade303Query.COLUMNS,
1925                Upgrade303Query.SELECTION, new String[] {String.valueOf(mimeTypeId)},
1926                null, null, null);
1927        try {
1928            while (cursor.moveToNext()) {
1929                long dataId = cursor.getLong(Upgrade303Query.ID);
1930                long rawContactId = cursor.getLong(Upgrade303Query.RAW_CONTACT_ID);
1931                String value = cursor.getString(Upgrade303Query.DATA1);
1932                value = extractHandleFromEmailAddress(value);
1933
1934                if (value != null) {
1935                    values.put(NameLookupColumns.DATA_ID, dataId);
1936                    values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId);
1937                    values.put(NameLookupColumns.NAME_TYPE, NameLookupType.EMAIL_BASED_NICKNAME);
1938                    values.put(NameLookupColumns.NORMALIZED_NAME, NameNormalizer.normalize(value));
1939                    db.insert(Tables.NAME_LOOKUP, null, values);
1940                }
1941            }
1942        } finally {
1943            cursor.close();
1944        }
1945    }
1946
1947    /**
1948     * The {@link ContactsProvider2#update} method was deleting name lookup for new
1949     * nicknames during the sync.  We need to restore the lost name lookup rows.
1950     */
1951    private void upgradeNicknameToVersion303(SQLiteDatabase db) {
1952        final long mimeTypeId = lookupMimeTypeId(db, Nickname.CONTENT_ITEM_TYPE);
1953        if (mimeTypeId == -1) {
1954            return;
1955        }
1956
1957        ContentValues values = new ContentValues();
1958
1959        // Find all data rows with the mime type "nickname" that are missing name lookup
1960        Cursor cursor = db.query(Upgrade303Query.TABLE, Upgrade303Query.COLUMNS,
1961                Upgrade303Query.SELECTION, new String[] {String.valueOf(mimeTypeId)},
1962                null, null, null);
1963        try {
1964            while (cursor.moveToNext()) {
1965                long dataId = cursor.getLong(Upgrade303Query.ID);
1966                long rawContactId = cursor.getLong(Upgrade303Query.RAW_CONTACT_ID);
1967                String value = cursor.getString(Upgrade303Query.DATA1);
1968
1969                values.put(NameLookupColumns.DATA_ID, dataId);
1970                values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId);
1971                values.put(NameLookupColumns.NAME_TYPE, NameLookupType.NICKNAME);
1972                values.put(NameLookupColumns.NORMALIZED_NAME, NameNormalizer.normalize(value));
1973                db.insert(Tables.NAME_LOOKUP, null, values);
1974            }
1975        } finally {
1976            cursor.close();
1977        }
1978    }
1979
1980    /**
1981     * Regenerate all rows in the nickname_lookup and name_lookup tables,
1982     * because sort key generation algorithm has been modified, e.g. as a result of
1983     * locale change.
1984     */
1985    private void rebuildNameLookup(SQLiteDatabase db) {
1986        db.execSQL("DELETE FROM " + Tables.NICKNAME_LOOKUP);
1987        loadNicknameLookupTable(db);
1988
1989        db.execSQL("DROP INDEX IF EXISTS name_lookup_index");
1990        db.execSQL("DELETE FROM " + Tables.NAME_LOOKUP);
1991
1992        SQLiteStatement nameLookupInsert = db.compileStatement(
1993                "INSERT OR IGNORE INTO " + Tables.NAME_LOOKUP + "("
1994                        + NameLookupColumns.RAW_CONTACT_ID + ","
1995                        + NameLookupColumns.DATA_ID + ","
1996                        + NameLookupColumns.NAME_TYPE + ","
1997                        + NameLookupColumns.NORMALIZED_NAME +
1998                ") VALUES (?,?,?,?)");
1999
2000        insertStructuredNameLookup(db, nameLookupInsert);
2001        insertOrganizationLookup(db, nameLookupInsert);
2002        insertEmailLookup(db, nameLookupInsert);
2003        insertNicknameLookup(db, nameLookupInsert);
2004
2005        createContactsIndexes(db);
2006    }
2007
2008    private static final class StructuredNameQuery {
2009        public static final String TABLE = Tables.DATA;
2010
2011        public static final String SELECTION =
2012                DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL";
2013
2014        public static final String COLUMNS[] = {
2015                StructuredName._ID,
2016                StructuredName.RAW_CONTACT_ID,
2017                StructuredName.DISPLAY_NAME,
2018        };
2019
2020        public static final int ID = 0;
2021        public static final int RAW_CONTACT_ID = 1;
2022        public static final int DISPLAY_NAME = 2;
2023    }
2024
2025    private class StructuredNameLookupBuilder extends NameLookupBuilder {
2026
2027        private final SQLiteStatement mNameLookupInsert;
2028        private final CommonNicknameCache mCommonNicknameCache;
2029
2030        public StructuredNameLookupBuilder(NameSplitter splitter,
2031                CommonNicknameCache commonNicknameCache, SQLiteStatement nameLookupInsert) {
2032            super(splitter);
2033            this.mCommonNicknameCache = commonNicknameCache;
2034            this.mNameLookupInsert = nameLookupInsert;
2035        }
2036
2037        @Override
2038        protected void insertNameLookup(long rawContactId, long dataId, int lookupType,
2039                String name) {
2040            if (!TextUtils.isEmpty(name)) {
2041                ContactsDatabaseHelper.this.insertNormalizedNameLookup(mNameLookupInsert,
2042                        rawContactId, dataId, lookupType, name);
2043            }
2044        }
2045
2046        @Override
2047        protected String[] getCommonNicknameClusters(String normalizedName) {
2048            return mCommonNicknameCache.getCommonNicknameClusters(normalizedName);
2049        }
2050    }
2051
2052    /**
2053     * Inserts name lookup rows for all structured names in the database.
2054     */
2055    private void insertStructuredNameLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) {
2056        NameSplitter nameSplitter = createNameSplitter();
2057        NameLookupBuilder nameLookupBuilder = new StructuredNameLookupBuilder(nameSplitter,
2058                new CommonNicknameCache(db), nameLookupInsert);
2059        final long mimeTypeId = lookupMimeTypeId(db, StructuredName.CONTENT_ITEM_TYPE);
2060        Cursor cursor = db.query(StructuredNameQuery.TABLE, StructuredNameQuery.COLUMNS,
2061                StructuredNameQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)},
2062                null, null, null);
2063        try {
2064            while (cursor.moveToNext()) {
2065                long dataId = cursor.getLong(StructuredNameQuery.ID);
2066                long rawContactId = cursor.getLong(StructuredNameQuery.RAW_CONTACT_ID);
2067                String name = cursor.getString(StructuredNameQuery.DISPLAY_NAME);
2068                int fullNameStyle = nameSplitter.guessFullNameStyle(name);
2069                nameLookupBuilder.insertNameLookup(rawContactId, dataId, name, fullNameStyle);
2070            }
2071        } finally {
2072            cursor.close();
2073        }
2074    }
2075
2076    private static final class OrganizationQuery {
2077        public static final String TABLE = Tables.DATA;
2078
2079        public static final String SELECTION =
2080                DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL";
2081
2082        public static final String COLUMNS[] = {
2083                Organization._ID,
2084                Organization.RAW_CONTACT_ID,
2085                Organization.COMPANY,
2086                Organization.TITLE,
2087        };
2088
2089        public static final int ID = 0;
2090        public static final int RAW_CONTACT_ID = 1;
2091        public static final int COMPANY = 2;
2092        public static final int TITLE = 3;
2093    }
2094
2095    /**
2096     * Inserts name lookup rows for all organizations in the database.
2097     */
2098    private void insertOrganizationLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) {
2099        final long mimeTypeId = lookupMimeTypeId(db, Organization.CONTENT_ITEM_TYPE);
2100        Cursor cursor = db.query(OrganizationQuery.TABLE, OrganizationQuery.COLUMNS,
2101                OrganizationQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)},
2102                null, null, null);
2103        try {
2104            while (cursor.moveToNext()) {
2105                long dataId = cursor.getLong(OrganizationQuery.ID);
2106                long rawContactId = cursor.getLong(OrganizationQuery.RAW_CONTACT_ID);
2107                String organization = cursor.getString(OrganizationQuery.COMPANY);
2108                String title = cursor.getString(OrganizationQuery.TITLE);
2109                insertNameLookup(nameLookupInsert, rawContactId, dataId,
2110                        NameLookupType.ORGANIZATION, organization);
2111                insertNameLookup(nameLookupInsert, rawContactId, dataId,
2112                        NameLookupType.ORGANIZATION, title);
2113            }
2114        } finally {
2115            cursor.close();
2116        }
2117    }
2118
2119    private static final class EmailQuery {
2120        public static final String TABLE = Tables.DATA;
2121
2122        public static final String SELECTION =
2123                DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL";
2124
2125        public static final String COLUMNS[] = {
2126                Email._ID,
2127                Email.RAW_CONTACT_ID,
2128                Email.ADDRESS,
2129        };
2130
2131        public static final int ID = 0;
2132        public static final int RAW_CONTACT_ID = 1;
2133        public static final int ADDRESS = 2;
2134    }
2135
2136    /**
2137     * Inserts name lookup rows for all email addresses in the database.
2138     */
2139    private void insertEmailLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) {
2140        final long mimeTypeId = lookupMimeTypeId(db, Email.CONTENT_ITEM_TYPE);
2141        Cursor cursor = db.query(EmailQuery.TABLE, EmailQuery.COLUMNS,
2142                EmailQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)},
2143                null, null, null);
2144        try {
2145            while (cursor.moveToNext()) {
2146                long dataId = cursor.getLong(EmailQuery.ID);
2147                long rawContactId = cursor.getLong(EmailQuery.RAW_CONTACT_ID);
2148                String address = cursor.getString(EmailQuery.ADDRESS);
2149                address = extractHandleFromEmailAddress(address);
2150                insertNameLookup(nameLookupInsert, rawContactId, dataId,
2151                        NameLookupType.EMAIL_BASED_NICKNAME, address);
2152            }
2153        } finally {
2154            cursor.close();
2155        }
2156    }
2157
2158    private static final class NicknameQuery {
2159        public static final String TABLE = Tables.DATA;
2160
2161        public static final String SELECTION =
2162                DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL";
2163
2164        public static final String COLUMNS[] = {
2165                Nickname._ID,
2166                Nickname.RAW_CONTACT_ID,
2167                Nickname.NAME,
2168        };
2169
2170        public static final int ID = 0;
2171        public static final int RAW_CONTACT_ID = 1;
2172        public static final int NAME = 2;
2173    }
2174
2175    /**
2176     * Inserts name lookup rows for all nicknames in the database.
2177     */
2178    private void insertNicknameLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) {
2179        final long mimeTypeId = lookupMimeTypeId(db, Nickname.CONTENT_ITEM_TYPE);
2180        Cursor cursor = db.query(NicknameQuery.TABLE, NicknameQuery.COLUMNS,
2181                NicknameQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)},
2182                null, null, null);
2183        try {
2184            while (cursor.moveToNext()) {
2185                long dataId = cursor.getLong(NicknameQuery.ID);
2186                long rawContactId = cursor.getLong(NicknameQuery.RAW_CONTACT_ID);
2187                String nickname = cursor.getString(NicknameQuery.NAME);
2188                insertNameLookup(nameLookupInsert, rawContactId, dataId,
2189                        NameLookupType.NICKNAME, nickname);
2190            }
2191        } finally {
2192            cursor.close();
2193        }
2194    }
2195
2196    /**
2197     * Inserts a record in the {@link Tables#NAME_LOOKUP} table.
2198     */
2199    public void insertNameLookup(SQLiteStatement stmt, long rawContactId, long dataId,
2200            int lookupType, String name) {
2201        if (TextUtils.isEmpty(name)) {
2202            return;
2203        }
2204
2205        String normalized = NameNormalizer.normalize(name);
2206        if (TextUtils.isEmpty(normalized)) {
2207            return;
2208        }
2209
2210        insertNormalizedNameLookup(stmt, rawContactId, dataId, lookupType, normalized);
2211    }
2212
2213    private void insertNormalizedNameLookup(SQLiteStatement stmt, long rawContactId, long dataId,
2214            int lookupType, String normalizedName) {
2215        stmt.bindLong(1, rawContactId);
2216        stmt.bindLong(2, dataId);
2217        stmt.bindLong(3, lookupType);
2218        stmt.bindString(4, normalizedName);
2219        stmt.executeInsert();
2220    }
2221
2222    public String extractHandleFromEmailAddress(String email) {
2223        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(email);
2224        if (tokens.length == 0) {
2225            return null;
2226        }
2227
2228        String address = tokens[0].getAddress();
2229        int at = address.indexOf('@');
2230        if (at != -1) {
2231            return address.substring(0, at);
2232        }
2233        return null;
2234    }
2235
2236    public String extractAddressFromEmailAddress(String email) {
2237        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(email);
2238        if (tokens.length == 0) {
2239            return null;
2240        }
2241
2242        return tokens[0].getAddress();
2243    }
2244
2245    private long lookupMimeTypeId(SQLiteDatabase db, String mimeType) {
2246        try {
2247            return DatabaseUtils.longForQuery(db,
2248                    "SELECT " + MimetypesColumns._ID +
2249                    " FROM " + Tables.MIMETYPES +
2250                    " WHERE " + MimetypesColumns.MIMETYPE
2251                            + "='" + mimeType + "'", null);
2252        } catch (SQLiteDoneException e) {
2253            // No rows of this type in the database
2254            return -1;
2255        }
2256    }
2257
2258    private void bindString(SQLiteStatement stmt, int index, String value) {
2259        if (value == null) {
2260            stmt.bindNull(index);
2261        } else {
2262            stmt.bindString(index, value);
2263        }
2264    }
2265
2266    /**
2267     * Adds index stats into the SQLite database to force it to always use the lookup indexes.
2268     */
2269    private void updateSqliteStats(SQLiteDatabase db) {
2270
2271        // Specific stats strings are based on an actual large database after running ANALYZE
2272        try {
2273            updateIndexStats(db, Tables.CONTACTS,
2274                    "contacts_restricted_index", "10000 9000");
2275            updateIndexStats(db, Tables.CONTACTS,
2276                    "contacts_has_phone_index", "10000 500");
2277            updateIndexStats(db, Tables.CONTACTS,
2278                    "contacts_visible_index", "10000 500 1");
2279
2280            updateIndexStats(db, Tables.RAW_CONTACTS,
2281                    "raw_contacts_source_id_index", "10000 1 1 1");
2282            updateIndexStats(db, Tables.RAW_CONTACTS,
2283                    "raw_contacts_contact_id_index", "10000 2");
2284
2285            updateIndexStats(db, Tables.NAME_LOOKUP,
2286                    "name_lookup_raw_contact_id_index", "10000 3");
2287            updateIndexStats(db, Tables.NAME_LOOKUP,
2288                    "name_lookup_index", "10000 3 2 2 1");
2289            updateIndexStats(db, Tables.NAME_LOOKUP,
2290                    "sqlite_autoindex_name_lookup_1", "10000 3 2 1");
2291
2292            updateIndexStats(db, Tables.PHONE_LOOKUP,
2293                    "phone_lookup_index", "10000 2 2 1");
2294            updateIndexStats(db, Tables.PHONE_LOOKUP,
2295                    "phone_lookup_min_match_index", "10000 2 2 1");
2296
2297            updateIndexStats(db, Tables.DATA,
2298                    "data_mimetype_data1_index", "60000 5000 2");
2299            updateIndexStats(db, Tables.DATA,
2300                    "data_raw_contact_id", "60000 10");
2301
2302            updateIndexStats(db, Tables.GROUPS,
2303                    "groups_source_id_index", "50 1 1 1");
2304
2305            updateIndexStats(db, Tables.NICKNAME_LOOKUP,
2306                    "sqlite_autoindex_name_lookup_1", "500 2 1");
2307
2308        } catch (SQLException e) {
2309            Log.e(TAG, "Could not update index stats", e);
2310        }
2311    }
2312
2313    /**
2314     * Stores statistics for a given index.
2315     *
2316     * @param stats has the following structure: the first index is the expected size of
2317     * the table.  The following integer(s) are the expected number of records selected with the
2318     * index.  There should be one integer per indexed column.
2319     */
2320    private void updateIndexStats(SQLiteDatabase db, String table, String index,
2321            String stats) {
2322        db.execSQL("DELETE FROM sqlite_stat1 WHERE tbl='" + table + "' AND idx='" + index + "';");
2323        db.execSQL("INSERT INTO sqlite_stat1 (tbl,idx,stat)"
2324                + " VALUES ('" + table + "','" + index + "','" + stats + "');");
2325    }
2326
2327    @Override
2328    public synchronized SQLiteDatabase getWritableDatabase() {
2329        SQLiteDatabase db = super.getWritableDatabase();
2330        if (mReopenDatabase) {
2331            mReopenDatabase = false;
2332            close();
2333            db = super.getWritableDatabase();
2334        }
2335        return db;
2336    }
2337
2338    /**
2339     * Wipes all data except mime type and package lookup tables.
2340     */
2341    public void wipeData() {
2342        SQLiteDatabase db = getWritableDatabase();
2343
2344        db.execSQL("DELETE FROM " + Tables.CONTACTS + ";");
2345        db.execSQL("DELETE FROM " + Tables.RAW_CONTACTS + ";");
2346        db.execSQL("DELETE FROM " + Tables.DATA + ";");
2347        db.execSQL("DELETE FROM " + Tables.PHONE_LOOKUP + ";");
2348        db.execSQL("DELETE FROM " + Tables.NAME_LOOKUP + ";");
2349        db.execSQL("DELETE FROM " + Tables.GROUPS + ";");
2350        db.execSQL("DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS + ";");
2351        db.execSQL("DELETE FROM " + Tables.SETTINGS + ";");
2352        db.execSQL("DELETE FROM " + Tables.ACTIVITIES + ";");
2353        db.execSQL("DELETE FROM " + Tables.CALLS + ";");
2354
2355        // Note: we are not removing reference data from Tables.NICKNAME_LOOKUP
2356    }
2357
2358    public NameSplitter createNameSplitter() {
2359        return new NameSplitter(
2360                mContext.getString(com.android.internal.R.string.common_name_prefixes),
2361                mContext.getString(com.android.internal.R.string.common_last_name_prefixes),
2362                mContext.getString(com.android.internal.R.string.common_name_suffixes),
2363                mContext.getString(com.android.internal.R.string.common_name_conjunctions),
2364                Locale.getDefault());
2365    }
2366
2367    /**
2368     * Return the {@link ApplicationInfo#uid} for the given package name.
2369     */
2370    public static int getUidForPackageName(PackageManager pm, String packageName) {
2371        try {
2372            ApplicationInfo clientInfo = pm.getApplicationInfo(packageName, 0 /* no flags */);
2373            return clientInfo.uid;
2374        } catch (NameNotFoundException e) {
2375            throw new RuntimeException(e);
2376        }
2377    }
2378
2379    /**
2380     * Perform an internal string-to-integer lookup using the compiled
2381     * {@link SQLiteStatement} provided, using the in-memory cache to speed up
2382     * lookups. If a mapping isn't found in cache or database, it will be
2383     * created. All new, uncached answers are added to the cache automatically.
2384     *
2385     * @param query Compiled statement used to query for the mapping.
2386     * @param insert Compiled statement used to insert a new mapping when no
2387     *            existing one is found in cache or from query.
2388     * @param value Value to find mapping for.
2389     * @param cache In-memory cache of previous answers.
2390     * @return An unique integer mapping for the given value.
2391     */
2392    private long getCachedId(SQLiteStatement query, SQLiteStatement insert,
2393            String value, HashMap<String, Long> cache) {
2394        // Try an in-memory cache lookup
2395        if (cache.containsKey(value)) {
2396            return cache.get(value);
2397        }
2398
2399        long id = -1;
2400        try {
2401            // Try searching database for mapping
2402            DatabaseUtils.bindObjectToProgram(query, 1, value);
2403            id = query.simpleQueryForLong();
2404        } catch (SQLiteDoneException e) {
2405            // Nothing found, so try inserting new mapping
2406            DatabaseUtils.bindObjectToProgram(insert, 1, value);
2407            id = insert.executeInsert();
2408        }
2409
2410        if (id != -1) {
2411            // Cache and return the new answer
2412            cache.put(value, id);
2413            return id;
2414        } else {
2415            // Otherwise throw if no mapping found or created
2416            throw new IllegalStateException("Couldn't find or create internal "
2417                    + "lookup table entry for value " + value);
2418        }
2419    }
2420
2421    /**
2422     * Convert a package name into an integer, using {@link Tables#PACKAGES} for
2423     * lookups and possible allocation of new IDs as needed.
2424     */
2425    public long getPackageId(String packageName) {
2426        // Make sure compiled statements are ready by opening database
2427        getReadableDatabase();
2428        return getCachedId(mPackageQuery, mPackageInsert, packageName, mPackageCache);
2429    }
2430
2431    /**
2432     * Convert a mimetype into an integer, using {@link Tables#MIMETYPES} for
2433     * lookups and possible allocation of new IDs as needed.
2434     */
2435    public long getMimeTypeId(String mimetype) {
2436        // Make sure compiled statements are ready by opening database
2437        getReadableDatabase();
2438        return getMimeTypeIdNoDbCheck(mimetype);
2439    }
2440
2441    private long getMimeTypeIdNoDbCheck(String mimetype) {
2442        return getCachedId(mMimetypeQuery, mMimetypeInsert, mimetype, mMimetypeCache);
2443    }
2444
2445    /**
2446     * Find the mimetype for the given {@link Data#_ID}.
2447     */
2448    public String getDataMimeType(long dataId) {
2449        // Make sure compiled statements are ready by opening database
2450        getReadableDatabase();
2451        try {
2452            // Try database query to find mimetype
2453            DatabaseUtils.bindObjectToProgram(mDataMimetypeQuery, 1, dataId);
2454            String mimetype = mDataMimetypeQuery.simpleQueryForString();
2455            return mimetype;
2456        } catch (SQLiteDoneException e) {
2457            // No valid mapping found, so return null
2458            return null;
2459        }
2460    }
2461
2462    /**
2463     * Find the mime-type for the given {@link Activities#_ID}.
2464     */
2465    public String getActivityMimeType(long activityId) {
2466        // Make sure compiled statements are ready by opening database
2467        getReadableDatabase();
2468        try {
2469            // Try database query to find mimetype
2470            DatabaseUtils.bindObjectToProgram(mActivitiesMimetypeQuery, 1, activityId);
2471            String mimetype = mActivitiesMimetypeQuery.simpleQueryForString();
2472            return mimetype;
2473        } catch (SQLiteDoneException e) {
2474            // No valid mapping found, so return null
2475            return null;
2476        }
2477    }
2478
2479    /**
2480     * Update {@link Contacts#IN_VISIBLE_GROUP} for all contacts.
2481     */
2482    public void updateAllVisible() {
2483        SQLiteDatabase db = getWritableDatabase();
2484        final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
2485        String[] selectionArgs = new String[]{String.valueOf(groupMembershipMimetypeId)};
2486
2487        // There are a couple questions that can be asked regarding the
2488        // following two update statements:
2489        //
2490        // Q: Why do we run these two queries separately? They seem like they could be combined.
2491        // A: This is a result of painstaking experimentation.  Turns out that the most
2492        // important optimization is to make sure we never update a value to its current value.
2493        // Changing 0 to 0 is unexpectedly expensive - SQLite actually writes the unchanged
2494        // rows back to disk.  The other consideration is that the CONTACT_IS_VISIBLE condition
2495        // is very complex and executing it twice in the same statement ("if contact_visible !=
2496        // CONTACT_IS_VISIBLE change it to CONTACT_IS_VISIBLE") is more expensive than running
2497        // two update statements.
2498        //
2499        // Q: How come we are using db.update instead of compiled statements?
2500        // A: This is a limitation of the compiled statement API. It does not return the
2501        // number of rows changed.  As you will see later in this method we really need
2502        // to know how many rows have been changed.
2503
2504        // First update contacts that are currently marked as invisible, but need to be visible
2505        ContentValues values = new ContentValues();
2506        values.put(Contacts.IN_VISIBLE_GROUP, 1);
2507        int countMadeVisible = db.update(Tables.CONTACTS, values,
2508                Contacts.IN_VISIBLE_GROUP + "=0" + " AND (" + Clauses.CONTACT_IS_VISIBLE + ")=1",
2509                selectionArgs);
2510
2511        // Next update contacts that are currently marked as visible, but need to be invisible
2512        values.put(Contacts.IN_VISIBLE_GROUP, 0);
2513        int countMadeInvisible = db.update(Tables.CONTACTS, values,
2514                Contacts.IN_VISIBLE_GROUP + "=1" + " AND (" + Clauses.CONTACT_IS_VISIBLE + ")=0",
2515                selectionArgs);
2516
2517        if (countMadeVisible != 0 || countMadeInvisible != 0) {
2518            // TODO break out the fields (contact_in_visible_group, sort_key, sort_key_alt) into
2519            // a separate table.
2520            // Rationale: The following statement will take a very long time on
2521            // a large database even though we are only changing one field from 0 to 1 or from
2522            // 1 to 0.  The reason for the slowness is that SQLite will need to write the whole
2523            // page even when only one bit on it changes. Changing the visibility of a
2524            // significant number of contacts will likely read and write almost the entire
2525            // raw_contacts table.  So, the solution is to break out into a separate table
2526            // the changing field along with the sort keys used for index-based sorting.
2527            // That table will occupy a smaller number of pages, so rewriting it would
2528            // not be as expensive.
2529            mVisibleUpdateRawContacts.execute();
2530        }
2531    }
2532
2533    /**
2534     * Update {@link Contacts#IN_VISIBLE_GROUP} for a specific contact.
2535     */
2536    public void updateContactVisible(long contactId) {
2537        final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
2538        mVisibleSpecificUpdate.bindLong(1, groupMembershipMimetypeId);
2539        mVisibleSpecificUpdate.bindLong(2, contactId);
2540        mVisibleSpecificUpdate.execute();
2541
2542        mVisibleSpecificUpdateRawContacts.bindLong(1, contactId);
2543        mVisibleSpecificUpdateRawContacts.execute();
2544    }
2545
2546    /**
2547     * Returns contact ID for the given contact or zero if it is NULL.
2548     */
2549    public long getContactId(long rawContactId) {
2550        getReadableDatabase();
2551        try {
2552            DatabaseUtils.bindObjectToProgram(mContactIdQuery, 1, rawContactId);
2553            return mContactIdQuery.simpleQueryForLong();
2554        } catch (SQLiteDoneException e) {
2555            // No valid mapping found, so return 0
2556            return 0;
2557        }
2558    }
2559
2560    public int getAggregationMode(long rawContactId) {
2561        getReadableDatabase();
2562        try {
2563            DatabaseUtils.bindObjectToProgram(mAggregationModeQuery, 1, rawContactId);
2564            return (int)mAggregationModeQuery.simpleQueryForLong();
2565        } catch (SQLiteDoneException e) {
2566            // No valid row found, so return "disabled"
2567            return RawContacts.AGGREGATION_MODE_DISABLED;
2568        }
2569    }
2570
2571    public void buildPhoneLookupAndRawContactQuery(SQLiteQueryBuilder qb, String number) {
2572        String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2573        qb.setTables(Tables.DATA_JOIN_RAW_CONTACTS +
2574                " JOIN " + Tables.PHONE_LOOKUP
2575                + " ON(" + DataColumns.CONCRETE_ID + "=" + PhoneLookupColumns.DATA_ID + ")");
2576
2577        StringBuilder sb = new StringBuilder();
2578        sb.append(PhoneLookupColumns.MIN_MATCH + "='");
2579        sb.append(minMatch);
2580        sb.append("' AND PHONE_NUMBERS_EQUAL(data." + Phone.NUMBER + ", ");
2581        DatabaseUtils.appendEscapedSQLString(sb, number);
2582        sb.append(mUseStrictPhoneNumberComparison ? ", 1)" : ", 0)");
2583
2584        qb.appendWhere(sb.toString());
2585    }
2586
2587    public void buildPhoneLookupAndContactQuery(SQLiteQueryBuilder qb, String number) {
2588        String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2589        StringBuilder sb = new StringBuilder();
2590        appendPhoneLookupTables(sb, minMatch, true);
2591        qb.setTables(sb.toString());
2592
2593        sb = new StringBuilder();
2594        appendPhoneLookupSelection(sb, number);
2595        qb.appendWhere(sb.toString());
2596    }
2597
2598    public String buildPhoneLookupAsNestedQuery(String number) {
2599        StringBuilder sb = new StringBuilder();
2600        final String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2601        sb.append("(SELECT DISTINCT raw_contact_id" + " FROM ");
2602        appendPhoneLookupTables(sb, minMatch, false);
2603        sb.append(" WHERE ");
2604        appendPhoneLookupSelection(sb, number);
2605        sb.append(")");
2606        return sb.toString();
2607    }
2608
2609    private void appendPhoneLookupTables(StringBuilder sb, final String minMatch,
2610            boolean joinContacts) {
2611        sb.append(Tables.RAW_CONTACTS);
2612        if (joinContacts) {
2613            sb.append(" JOIN " + getContactView() + " contacts_view"
2614                    + " ON (contacts_view._id = raw_contacts.contact_id)");
2615        }
2616        sb.append(", (SELECT data_id FROM phone_lookup "
2617                + "WHERE (" + Tables.PHONE_LOOKUP + "." + PhoneLookupColumns.MIN_MATCH + " = '");
2618        sb.append(minMatch);
2619        sb.append("')) AS lookup, " + Tables.DATA);
2620    }
2621
2622    private void appendPhoneLookupSelection(StringBuilder sb, String number) {
2623        sb.append("lookup.data_id=data._id AND data.raw_contact_id=raw_contacts._id"
2624                + " AND PHONE_NUMBERS_EQUAL(data." + Phone.NUMBER + ", ");
2625        DatabaseUtils.appendEscapedSQLString(sb, number);
2626        sb.append(mUseStrictPhoneNumberComparison ? ", 1)" : ", 0)");
2627    }
2628
2629    public String getUseStrictPhoneNumberComparisonParameter() {
2630        return mUseStrictPhoneNumberComparison ? "1" : "0";
2631    }
2632
2633    /**
2634     * Loads common nickname mappings into the database.
2635     */
2636    private void loadNicknameLookupTable(SQLiteDatabase db) {
2637        String[] strings = mContext.getResources().getStringArray(
2638                com.android.internal.R.array.common_nicknames);
2639        if (strings == null || strings.length == 0) {
2640            return;
2641        }
2642
2643        SQLiteStatement nicknameLookupInsert = db.compileStatement("INSERT INTO "
2644                + Tables.NICKNAME_LOOKUP + "(" + NicknameLookupColumns.NAME + ","
2645                + NicknameLookupColumns.CLUSTER + ") VALUES (?,?)");
2646
2647        for (int clusterId = 0; clusterId < strings.length; clusterId++) {
2648            String[] names = strings[clusterId].split(",");
2649            for (int j = 0; j < names.length; j++) {
2650                String name = NameNormalizer.normalize(names[j]);
2651                try {
2652                    DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 1, name);
2653                    DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 2,
2654                            String.valueOf(clusterId));
2655                    nicknameLookupInsert.executeInsert();
2656                } catch (SQLiteException e) {
2657
2658                    // Print the exception and keep going - this is not a fatal error
2659                    Log.e(TAG, "Cannot insert nickname: " + names[j], e);
2660                }
2661            }
2662        }
2663    }
2664
2665    public static void copyStringValue(ContentValues toValues, String toKey,
2666            ContentValues fromValues, String fromKey) {
2667        if (fromValues.containsKey(fromKey)) {
2668            toValues.put(toKey, fromValues.getAsString(fromKey));
2669        }
2670    }
2671
2672    public static void copyLongValue(ContentValues toValues, String toKey,
2673            ContentValues fromValues, String fromKey) {
2674        if (fromValues.containsKey(fromKey)) {
2675            long longValue;
2676            Object value = fromValues.get(fromKey);
2677            if (value instanceof Boolean) {
2678                if ((Boolean)value) {
2679                    longValue = 1;
2680                } else {
2681                    longValue = 0;
2682                }
2683            } else if (value instanceof String) {
2684                longValue = Long.parseLong((String)value);
2685            } else {
2686                longValue = ((Number)value).longValue();
2687            }
2688            toValues.put(toKey, longValue);
2689        }
2690    }
2691
2692    public SyncStateContentProviderHelper getSyncState() {
2693        return mSyncState;
2694    }
2695
2696    /**
2697     * Delete the aggregate contact if it has no constituent raw contacts other
2698     * than the supplied one.
2699     */
2700    public void removeContactIfSingleton(long rawContactId) {
2701        SQLiteDatabase db = getWritableDatabase();
2702
2703        // Obtain contact ID from the supplied raw contact ID
2704        String contactIdFromRawContactId = "(SELECT " + RawContacts.CONTACT_ID + " FROM "
2705                + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=" + rawContactId + ")";
2706
2707        // Find other raw contacts in the same aggregate contact
2708        String otherRawContacts = "(SELECT contacts1." + RawContacts._ID + " FROM "
2709                + Tables.RAW_CONTACTS + " contacts1 JOIN " + Tables.RAW_CONTACTS + " contacts2 ON ("
2710                + "contacts1." + RawContacts.CONTACT_ID + "=contacts2." + RawContacts.CONTACT_ID
2711                + ") WHERE contacts1." + RawContacts._ID + "!=" + rawContactId + ""
2712                + " AND contacts2." + RawContacts._ID + "=" + rawContactId + ")";
2713
2714        db.execSQL("DELETE FROM " + Tables.CONTACTS
2715                + " WHERE " + Contacts._ID + "=" + contactIdFromRawContactId
2716                + " AND NOT EXISTS " + otherRawContacts + ";");
2717    }
2718
2719    /**
2720     * Check if {@link Binder#getCallingUid()} should be allowed access to
2721     * {@link RawContacts#IS_RESTRICTED} data.
2722     */
2723    boolean hasAccessToRestrictedData() {
2724        final PackageManager pm = mContext.getPackageManager();
2725        final String[] callerPackages = pm.getPackagesForUid(Binder.getCallingUid());
2726
2727        // Has restricted access if caller matches any packages
2728        for (String callerPackage : callerPackages) {
2729            if (hasAccessToRestrictedData(callerPackage)) {
2730                return true;
2731            }
2732        }
2733        return false;
2734    }
2735
2736    /**
2737     * Check if requestingPackage should be allowed access to
2738     * {@link RawContacts#IS_RESTRICTED} data.
2739     */
2740    boolean hasAccessToRestrictedData(String requestingPackage) {
2741        if (mUnrestrictedPackages != null) {
2742            for (String allowedPackage : mUnrestrictedPackages) {
2743                if (allowedPackage.equals(requestingPackage)) {
2744                    return true;
2745                }
2746            }
2747        }
2748        return false;
2749    }
2750
2751    public String getDataView() {
2752        return getDataView(false);
2753    }
2754
2755    public String getDataView(boolean requireRestrictedView) {
2756        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2757                Views.DATA_ALL : Views.DATA_RESTRICTED;
2758    }
2759
2760    public String getRawContactView() {
2761        return getRawContactView(false);
2762    }
2763
2764    public String getRawContactView(boolean requireRestrictedView) {
2765        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2766                Views.RAW_CONTACTS_ALL : Views.RAW_CONTACTS_RESTRICTED;
2767    }
2768
2769    public String getContactView() {
2770        return getContactView(false);
2771    }
2772
2773    public String getContactView(boolean requireRestrictedView) {
2774        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2775                Views.CONTACTS_ALL : Views.CONTACTS_RESTRICTED;
2776    }
2777
2778    public String getGroupView() {
2779        return Views.GROUPS_ALL;
2780    }
2781
2782    public String getContactEntitiesView() {
2783        return getContactEntitiesView(false);
2784    }
2785
2786    public String getContactEntitiesView(boolean requireRestrictedView) {
2787        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2788                Tables.CONTACT_ENTITIES : Tables.CONTACT_ENTITIES_RESTRICTED;
2789    }
2790
2791    /**
2792     * Test if any of the columns appear in the given projection.
2793     */
2794    public boolean isInProjection(String[] projection, String... columns) {
2795        if (projection == null) {
2796            return true;
2797        }
2798
2799        // Optimized for a single-column test
2800        if (columns.length == 1) {
2801            String column = columns[0];
2802            for (String test : projection) {
2803                if (column.equals(test)) {
2804                    return true;
2805                }
2806            }
2807        } else {
2808            for (String test : projection) {
2809                for (String column : columns) {
2810                    if (column.equals(test)) {
2811                        return true;
2812                    }
2813                }
2814            }
2815        }
2816        return false;
2817    }
2818
2819    /**
2820     * Returns a detailed exception message for the supplied URI.  It includes the calling
2821     * user and calling package(s).
2822     */
2823    public String exceptionMessage(Uri uri) {
2824        return exceptionMessage(null, uri);
2825    }
2826
2827    /**
2828     * Returns a detailed exception message for the supplied URI.  It includes the calling
2829     * user and calling package(s).
2830     */
2831    public String exceptionMessage(String message, Uri uri) {
2832        StringBuilder sb = new StringBuilder();
2833        if (message != null) {
2834            sb.append(message).append("; ");
2835        }
2836        sb.append("URI: ").append(uri);
2837        final PackageManager pm = mContext.getPackageManager();
2838        int callingUid = Binder.getCallingUid();
2839        sb.append(", calling user: ");
2840        String userName = pm.getNameForUid(callingUid);
2841        if (userName != null) {
2842            sb.append(userName);
2843        } else {
2844            sb.append(callingUid);
2845        }
2846
2847        final String[] callerPackages = pm.getPackagesForUid(callingUid);
2848        if (callerPackages != null && callerPackages.length > 0) {
2849            if (callerPackages.length == 1) {
2850                sb.append(", calling package:");
2851                sb.append(callerPackages[0]);
2852            } else {
2853                sb.append(", calling package is one of: [");
2854                for (int i = 0; i < callerPackages.length; i++) {
2855                    if (i != 0) {
2856                        sb.append(", ");
2857                    }
2858                    sb.append(callerPackages[i]);
2859                }
2860                sb.append("]");
2861            }
2862        }
2863
2864        return sb.toString();
2865    }
2866}
2867