ContactsDatabaseHelper.java revision b0812f94fa50c54d06978cdd65651a487c717dff
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.os.Binder;
38import android.os.Bundle;
39import android.provider.BaseColumns;
40import android.provider.ContactsContract;
41import android.provider.CallLog.Calls;
42import android.provider.ContactsContract.AggregationExceptions;
43import android.provider.ContactsContract.Contacts;
44import android.provider.ContactsContract.Data;
45import android.provider.ContactsContract.DisplayNameSources;
46import android.provider.ContactsContract.FullNameStyle;
47import android.provider.ContactsContract.Groups;
48import android.provider.ContactsContract.RawContacts;
49import android.provider.ContactsContract.Settings;
50import android.provider.ContactsContract.StatusUpdates;
51import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
52import android.provider.ContactsContract.CommonDataKinds.Organization;
53import android.provider.ContactsContract.CommonDataKinds.Phone;
54import android.provider.ContactsContract.CommonDataKinds.StructuredName;
55import android.provider.SocialContract.Activities;
56import android.telephony.PhoneNumberUtils;
57import android.text.TextUtils;
58import android.util.Log;
59
60import java.util.HashMap;
61import java.util.Locale;
62
63/**
64 * Database helper for contacts. Designed as a singleton to make sure that all
65 * {@link android.content.ContentProvider} users get the same reference.
66 * Provides handy methods for maintaining package and mime-type lookup tables.
67 */
68/* package */ class ContactsDatabaseHelper extends SQLiteOpenHelper {
69    private static final String TAG = "ContactsDatabaseHelper";
70
71    private static final int DATABASE_VERSION = 205;
72
73    private static final String DATABASE_NAME = "contacts2.db";
74    private static final String DATABASE_PRESENCE = "presence_db";
75
76    /** size of the compiled-sql statement cache mainatained by {@link SQLiteDatabase} */
77    private static final int MAX_CACHE_SIZE_FOR_CONTACTS_DB = 250;
78
79    public interface Tables {
80        public static final String CONTACTS = "contacts";
81        public static final String RAW_CONTACTS = "raw_contacts";
82        public static final String PACKAGES = "packages";
83        public static final String MIMETYPES = "mimetypes";
84        public static final String PHONE_LOOKUP = "phone_lookup";
85        public static final String NAME_LOOKUP = "name_lookup";
86        public static final String AGGREGATION_EXCEPTIONS = "agg_exceptions";
87        public static final String SETTINGS = "settings";
88        public static final String DATA = "data";
89        public static final String GROUPS = "groups";
90        public static final String PRESENCE = "presence";
91        public static final String AGGREGATED_PRESENCE = "agg_presence";
92        public static final String NICKNAME_LOOKUP = "nickname_lookup";
93        public static final String CALLS = "calls";
94        public static final String CONTACT_ENTITIES = "contact_entities_view";
95        public static final String CONTACT_ENTITIES_RESTRICTED = "contact_entities_view_restricted";
96        public static final String STATUS_UPDATES = "status_updates";
97
98        public static final String DATA_JOIN_MIMETYPES = "data "
99                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id)";
100
101        public static final String DATA_JOIN_RAW_CONTACTS = "data "
102                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)";
103
104        public static final String DATA_JOIN_MIMETYPE_RAW_CONTACTS = "data "
105                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
106                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)";
107
108        // NOTE: This requires late binding of GroupMembership MIME-type
109        public static final String RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS = "raw_contacts "
110                + "LEFT OUTER JOIN settings ON ("
111                    + "raw_contacts.account_name = settings.account_name AND "
112                    + "raw_contacts.account_type = settings.account_type) "
113                + "LEFT OUTER JOIN data ON (data.mimetype_id=? AND "
114                    + "data.raw_contact_id = raw_contacts._id) "
115                + "LEFT OUTER JOIN groups ON (groups._id = data." + GroupMembership.GROUP_ROW_ID
116                + ")";
117
118        // NOTE: This requires late binding of GroupMembership MIME-type
119        public static final String SETTINGS_JOIN_RAW_CONTACTS_DATA_MIMETYPES_CONTACTS = "settings "
120                + "LEFT OUTER JOIN raw_contacts ON ("
121                    + "raw_contacts.account_name = settings.account_name AND "
122                    + "raw_contacts.account_type = settings.account_type) "
123                + "LEFT OUTER JOIN data ON (data.mimetype_id=? AND "
124                    + "data.raw_contact_id = raw_contacts._id) "
125                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
126
127        public static final String DATA_JOIN_MIMETYPES_RAW_CONTACTS_CONTACTS = "data "
128                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
129                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
130                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
131
132        public static final String DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_GROUPS = "data "
133                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
134                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
135                + "LEFT OUTER JOIN packages ON (data.package_id = packages._id) "
136                + "LEFT OUTER JOIN groups "
137                + "  ON (mimetypes.mimetype='" + GroupMembership.CONTENT_ITEM_TYPE + "' "
138                + "      AND groups._id = data." + GroupMembership.GROUP_ROW_ID + ") ";
139
140        public static final String GROUPS_JOIN_PACKAGES = "groups "
141                + "LEFT OUTER JOIN packages ON (groups.package_id = packages._id)";
142
143        public static final String ACTIVITIES = "activities";
144
145        public static final String ACTIVITIES_JOIN_MIMETYPES = "activities "
146                + "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id)";
147
148        public static final String ACTIVITIES_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_CONTACTS =
149                "activities "
150                + "LEFT OUTER JOIN packages ON (activities.package_id = packages._id) "
151                + "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id) "
152                + "LEFT OUTER JOIN raw_contacts ON (activities.author_contact_id = " +
153                        "raw_contacts._id) "
154                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
155
156        public static final String NAME_LOOKUP_JOIN_RAW_CONTACTS = "name_lookup "
157                + "INNER JOIN raw_contacts ON (name_lookup.raw_contact_id = raw_contacts._id)";
158    }
159
160    public interface Views {
161        public static final String DATA_ALL = "view_data";
162        public static final String DATA_RESTRICTED = "view_data_restricted";
163
164        public static final String RAW_CONTACTS_ALL = "view_raw_contacts";
165        public static final String RAW_CONTACTS_RESTRICTED = "view_raw_contacts_restricted";
166
167        public static final String CONTACTS_ALL = "view_contacts";
168        public static final String CONTACTS_RESTRICTED = "view_contacts_restricted";
169
170        public static final String GROUPS_ALL = "view_groups";
171    }
172
173    public interface Clauses {
174        final String MIMETYPE_IS_GROUP_MEMBERSHIP = MimetypesColumns.CONCRETE_MIMETYPE + "='"
175                + GroupMembership.CONTENT_ITEM_TYPE + "'";
176
177        final String BELONGS_TO_GROUP = DataColumns.CONCRETE_GROUP_ID + "="
178                + GroupsColumns.CONCRETE_ID;
179
180        final String HAVING_NO_GROUPS = "COUNT(" + DataColumns.CONCRETE_GROUP_ID + ") == 0";
181
182        final String GROUP_BY_ACCOUNT_CONTACT_ID = SettingsColumns.CONCRETE_ACCOUNT_NAME + ","
183                + SettingsColumns.CONCRETE_ACCOUNT_TYPE + "," + RawContacts.CONTACT_ID;
184
185        final String RAW_CONTACT_IS_LOCAL = RawContactsColumns.CONCRETE_ACCOUNT_NAME
186                + " IS NULL AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " IS NULL";
187
188        final String ZERO_GROUP_MEMBERSHIPS = "COUNT(" + GroupsColumns.CONCRETE_ID + ")=0";
189
190        final String OUTER_RAW_CONTACTS = "outer_raw_contacts";
191        final String OUTER_RAW_CONTACTS_ID = OUTER_RAW_CONTACTS + "." + RawContacts._ID;
192
193        final String CONTACT_IS_VISIBLE =
194                "SELECT " +
195                    "MAX((SELECT (CASE WHEN " +
196                        "(CASE" +
197                            " WHEN " + RAW_CONTACT_IS_LOCAL +
198                            " THEN 1 " +
199                            " WHEN " + ZERO_GROUP_MEMBERSHIPS +
200                            " THEN " + Settings.UNGROUPED_VISIBLE +
201                            " ELSE MAX(" + Groups.GROUP_VISIBLE + ")" +
202                         "END)=1 THEN 1 ELSE 0 END)" +
203                " FROM " + Tables.RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS +
204                " WHERE " + RawContactsColumns.CONCRETE_ID + "=" + OUTER_RAW_CONTACTS_ID + "))" +
205                " FROM " + Tables.RAW_CONTACTS + " AS " + OUTER_RAW_CONTACTS +
206                " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
207                " GROUP BY " + RawContacts.CONTACT_ID;
208
209        final String GROUP_HAS_ACCOUNT_AND_SOURCE_ID = Groups.SOURCE_ID + "=? AND "
210                + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?";
211    }
212
213    public interface ContactsColumns {
214        /**
215         * This flag is set for a contact if it has only one constituent raw contact and
216         * it is restricted.
217         */
218        public static final String SINGLE_IS_RESTRICTED = "single_is_restricted";
219
220        public static final String LAST_STATUS_UPDATE_ID = "status_update_id";
221
222        public static final String CONCRETE_ID = Tables.CONTACTS + "." + BaseColumns._ID;
223
224        public static final String CONCRETE_TIMES_CONTACTED = Tables.CONTACTS + "."
225                + Contacts.TIMES_CONTACTED;
226        public static final String CONCRETE_LAST_TIME_CONTACTED = Tables.CONTACTS + "."
227                + Contacts.LAST_TIME_CONTACTED;
228        public static final String CONCRETE_STARRED = Tables.CONTACTS + "." + Contacts.STARRED;
229        public static final String CONCRETE_CUSTOM_RINGTONE = Tables.CONTACTS + "."
230                + Contacts.CUSTOM_RINGTONE;
231        public static final String CONCRETE_SEND_TO_VOICEMAIL = Tables.CONTACTS + "."
232                + Contacts.SEND_TO_VOICEMAIL;
233    }
234
235    public interface RawContactsColumns {
236        public static final String CONCRETE_ID =
237                Tables.RAW_CONTACTS + "." + BaseColumns._ID;
238        public static final String CONCRETE_ACCOUNT_NAME =
239                Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_NAME;
240        public static final String CONCRETE_ACCOUNT_TYPE =
241                Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_TYPE;
242        public static final String CONCRETE_SOURCE_ID =
243                Tables.RAW_CONTACTS + "." + RawContacts.SOURCE_ID;
244        public static final String CONCRETE_VERSION =
245                Tables.RAW_CONTACTS + "." + RawContacts.VERSION;
246        public static final String CONCRETE_DIRTY =
247                Tables.RAW_CONTACTS + "." + RawContacts.DIRTY;
248        public static final String CONCRETE_DELETED =
249                Tables.RAW_CONTACTS + "." + RawContacts.DELETED;
250        public static final String CONCRETE_SYNC1 =
251                Tables.RAW_CONTACTS + "." + RawContacts.SYNC1;
252        public static final String CONCRETE_SYNC2 =
253                Tables.RAW_CONTACTS + "." + RawContacts.SYNC2;
254        public static final String CONCRETE_SYNC3 =
255                Tables.RAW_CONTACTS + "." + RawContacts.SYNC3;
256        public static final String CONCRETE_SYNC4 =
257                Tables.RAW_CONTACTS + "." + RawContacts.SYNC4;
258        public static final String CONCRETE_STARRED =
259                Tables.RAW_CONTACTS + "." + RawContacts.STARRED;
260        public static final String CONCRETE_IS_RESTRICTED =
261                Tables.RAW_CONTACTS + "." + RawContacts.IS_RESTRICTED;
262
263        public static final String DISPLAY_NAME = RawContacts.DISPLAY_NAME_PRIMARY;
264        public static final String DISPLAY_NAME_SOURCE = RawContacts.DISPLAY_NAME_SOURCE;
265        public static final String AGGREGATION_NEEDED = "aggregation_needed";
266        public static final String CONTACT_IN_VISIBLE_GROUP = "contact_in_visible_group";
267
268        public static final String CONCRETE_DISPLAY_NAME =
269                Tables.RAW_CONTACTS + "." + DISPLAY_NAME;
270        public static final String CONCRETE_CONTACT_ID =
271                Tables.RAW_CONTACTS + "." + RawContacts.CONTACT_ID;
272    }
273
274    public interface DataColumns {
275        public static final String PACKAGE_ID = "package_id";
276        public static final String MIMETYPE_ID = "mimetype_id";
277
278        public static final String CONCRETE_ID = Tables.DATA + "." + BaseColumns._ID;
279        public static final String CONCRETE_MIMETYPE_ID = Tables.DATA + "." + MIMETYPE_ID;
280        public static final String CONCRETE_RAW_CONTACT_ID = Tables.DATA + "."
281                + Data.RAW_CONTACT_ID;
282        public static final String CONCRETE_GROUP_ID = Tables.DATA + "."
283                + GroupMembership.GROUP_ROW_ID;
284
285        public static final String CONCRETE_DATA1 = Tables.DATA + "." + Data.DATA1;
286        public static final String CONCRETE_DATA2 = Tables.DATA + "." + Data.DATA2;
287        public static final String CONCRETE_DATA3 = Tables.DATA + "." + Data.DATA3;
288        public static final String CONCRETE_DATA4 = Tables.DATA + "." + Data.DATA4;
289        public static final String CONCRETE_DATA5 = Tables.DATA + "." + Data.DATA5;
290        public static final String CONCRETE_DATA6 = Tables.DATA + "." + Data.DATA6;
291        public static final String CONCRETE_DATA7 = Tables.DATA + "." + Data.DATA7;
292        public static final String CONCRETE_DATA8 = Tables.DATA + "." + Data.DATA8;
293        public static final String CONCRETE_DATA9 = Tables.DATA + "." + Data.DATA9;
294        public static final String CONCRETE_DATA10 = Tables.DATA + "." + Data.DATA10;
295        public static final String CONCRETE_DATA11 = Tables.DATA + "." + Data.DATA11;
296        public static final String CONCRETE_DATA12 = Tables.DATA + "." + Data.DATA12;
297        public static final String CONCRETE_DATA13 = Tables.DATA + "." + Data.DATA13;
298        public static final String CONCRETE_DATA14 = Tables.DATA + "." + Data.DATA14;
299        public static final String CONCRETE_DATA15 = Tables.DATA + "." + Data.DATA15;
300        public static final String CONCRETE_IS_PRIMARY = Tables.DATA + "." + Data.IS_PRIMARY;
301        public static final String CONCRETE_PACKAGE_ID = Tables.DATA + "." + PACKAGE_ID;
302    }
303
304    // Used only for legacy API support
305    public interface ExtensionsColumns {
306        public static final String NAME = Data.DATA1;
307        public static final String VALUE = Data.DATA2;
308    }
309
310    public interface GroupMembershipColumns {
311        public static final String RAW_CONTACT_ID = Data.RAW_CONTACT_ID;
312        public static final String GROUP_ROW_ID = GroupMembership.GROUP_ROW_ID;
313    }
314
315    public interface PhoneColumns {
316        public static final String NORMALIZED_NUMBER = Data.DATA4;
317        public static final String CONCRETE_NORMALIZED_NUMBER = DataColumns.CONCRETE_DATA4;
318    }
319
320    public interface GroupsColumns {
321        public static final String PACKAGE_ID = "package_id";
322
323        public static final String CONCRETE_ID = Tables.GROUPS + "." + BaseColumns._ID;
324        public static final String CONCRETE_SOURCE_ID = Tables.GROUPS + "." + Groups.SOURCE_ID;
325        public static final String CONCRETE_ACCOUNT_NAME = Tables.GROUPS + "." + Groups.ACCOUNT_NAME;
326        public static final String CONCRETE_ACCOUNT_TYPE = Tables.GROUPS + "." + Groups.ACCOUNT_TYPE;
327    }
328
329    public interface ActivitiesColumns {
330        public static final String PACKAGE_ID = "package_id";
331        public static final String MIMETYPE_ID = "mimetype_id";
332    }
333
334    public interface PhoneLookupColumns {
335        public static final String _ID = BaseColumns._ID;
336        public static final String DATA_ID = "data_id";
337        public static final String RAW_CONTACT_ID = "raw_contact_id";
338        public static final String NORMALIZED_NUMBER = "normalized_number";
339        public static final String MIN_MATCH = "min_match";
340    }
341
342    public interface NameLookupColumns {
343        public static final String RAW_CONTACT_ID = "raw_contact_id";
344        public static final String DATA_ID = "data_id";
345        public static final String NORMALIZED_NAME = "normalized_name";
346        public static final String NAME_TYPE = "name_type";
347    }
348
349    public final static class NameLookupType {
350        public static final int NAME_EXACT = 0;
351        public static final int NAME_VARIANT = 1;
352        public static final int NAME_COLLATION_KEY = 2;
353        public static final int NICKNAME = 3;
354        public static final int EMAIL_BASED_NICKNAME = 4;
355        public static final int ORGANIZATION = 5;
356
357        // This is the highest name lookup type code plus one
358        public static final int TYPE_COUNT = 6;
359
360        public static boolean isBasedOnStructuredName(int nameLookupType) {
361            return nameLookupType == NameLookupType.NAME_EXACT
362                    || nameLookupType == NameLookupType.NAME_VARIANT
363                    || nameLookupType == NameLookupType.NAME_COLLATION_KEY;
364        }
365    }
366
367    public interface PackagesColumns {
368        public static final String _ID = BaseColumns._ID;
369        public static final String PACKAGE = "package";
370
371        public static final String CONCRETE_ID = Tables.PACKAGES + "." + _ID;
372    }
373
374    public interface MimetypesColumns {
375        public static final String _ID = BaseColumns._ID;
376        public static final String MIMETYPE = "mimetype";
377
378        public static final String CONCRETE_ID = Tables.MIMETYPES + "." + BaseColumns._ID;
379        public static final String CONCRETE_MIMETYPE = Tables.MIMETYPES + "." + MIMETYPE;
380    }
381
382    public interface AggregationExceptionColumns {
383        public static final String _ID = BaseColumns._ID;
384    }
385
386    public interface NicknameLookupColumns {
387        public static final String NAME = "name";
388        public static final String CLUSTER = "cluster";
389    }
390
391    public interface SettingsColumns {
392        public static final String CONCRETE_ACCOUNT_NAME = Tables.SETTINGS + "."
393                + Settings.ACCOUNT_NAME;
394        public static final String CONCRETE_ACCOUNT_TYPE = Tables.SETTINGS + "."
395                + Settings.ACCOUNT_TYPE;
396    }
397
398    public interface PresenceColumns {
399        String RAW_CONTACT_ID = "presence_raw_contact_id";
400        String CONTACT_ID = "presence_contact_id";
401    }
402
403    public interface AggregatedPresenceColumns {
404        String CONTACT_ID = "presence_contact_id";
405
406        String CONCRETE_CONTACT_ID = Tables.AGGREGATED_PRESENCE + "." + CONTACT_ID;
407    }
408
409    public interface StatusUpdatesColumns {
410        String DATA_ID = "status_update_data_id";
411
412        String CONCRETE_DATA_ID = Tables.STATUS_UPDATES + "." + DATA_ID;
413
414        String CONCRETE_PRESENCE = Tables.STATUS_UPDATES + "." + StatusUpdates.PRESENCE;
415        String CONCRETE_STATUS = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS;
416        String CONCRETE_STATUS_TIMESTAMP = Tables.STATUS_UPDATES + "."
417                + StatusUpdates.STATUS_TIMESTAMP;
418        String CONCRETE_STATUS_RES_PACKAGE = Tables.STATUS_UPDATES + "."
419                + StatusUpdates.STATUS_RES_PACKAGE;
420        String CONCRETE_STATUS_LABEL = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS_LABEL;
421        String CONCRETE_STATUS_ICON = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS_ICON;
422    }
423
424    public interface ContactsStatusUpdatesColumns {
425        String ALIAS = "contacts_" + Tables.STATUS_UPDATES;
426
427        String CONCRETE_DATA_ID = ALIAS + "." + StatusUpdatesColumns.DATA_ID;
428
429        String CONCRETE_PRESENCE = ALIAS + "." + StatusUpdates.PRESENCE;
430        String CONCRETE_STATUS = ALIAS + "." + StatusUpdates.STATUS;
431        String CONCRETE_STATUS_TIMESTAMP = ALIAS + "." + StatusUpdates.STATUS_TIMESTAMP;
432        String CONCRETE_STATUS_RES_PACKAGE = ALIAS + "." + StatusUpdates.STATUS_RES_PACKAGE;
433        String CONCRETE_STATUS_LABEL = ALIAS + "." + StatusUpdates.STATUS_LABEL;
434        String CONCRETE_STATUS_ICON = ALIAS + "." + StatusUpdates.STATUS_ICON;
435    }
436
437    /** In-memory cache of previously found MIME-type mappings */
438    private final HashMap<String, Long> mMimetypeCache = new HashMap<String, Long>();
439    /** In-memory cache of previously found package name mappings */
440    private final HashMap<String, Long> mPackageCache = new HashMap<String, Long>();
441
442
443    /** Compiled statements for querying and inserting mappings */
444    private SQLiteStatement mMimetypeQuery;
445    private SQLiteStatement mPackageQuery;
446    private SQLiteStatement mContactIdQuery;
447    private SQLiteStatement mAggregationModeQuery;
448    private SQLiteStatement mMimetypeInsert;
449    private SQLiteStatement mPackageInsert;
450    private SQLiteStatement mDataMimetypeQuery;
451    private SQLiteStatement mActivitiesMimetypeQuery;
452
453    private final Context mContext;
454    private final SyncStateContentProviderHelper mSyncState;
455
456
457    /** Compiled statements for updating {@link Contacts#IN_VISIBLE_GROUP}. */
458    private SQLiteStatement mVisibleUpdate;
459    private SQLiteStatement mVisibleSpecificUpdate;
460    private SQLiteStatement mVisibleUpdateRawContacts;
461    private SQLiteStatement mVisibleSpecificUpdateRawContacts;
462
463    private boolean mReopenDatabase = false;
464
465    private static ContactsDatabaseHelper sSingleton = null;
466
467    private boolean mUseStrictPhoneNumberComparison;
468
469    /**
470     * List of package names with access to {@link RawContacts#IS_RESTRICTED} data.
471     */
472    private String[] mUnrestrictedPackages;
473
474    public static synchronized ContactsDatabaseHelper getInstance(Context context) {
475        if (sSingleton == null) {
476            sSingleton = new ContactsDatabaseHelper(context);
477        }
478        return sSingleton;
479    }
480
481    /**
482     * Private constructor, callers except unit tests should obtain an instance through
483     * {@link #getInstance(android.content.Context)} instead.
484     */
485    ContactsDatabaseHelper(Context context) {
486        super(context, DATABASE_NAME, null, DATABASE_VERSION);
487        if (false) Log.i(TAG, "Creating OpenHelper");
488        Resources resources = context.getResources();
489
490        mContext = context;
491        mSyncState = new SyncStateContentProviderHelper();
492        mUseStrictPhoneNumberComparison =
493                resources.getBoolean(
494                        com.android.internal.R.bool.config_use_strict_phone_number_comparation);
495        int resourceId = resources.getIdentifier("unrestricted_packages", "array",
496                context.getPackageName());
497        if (resourceId != 0) {
498            mUnrestrictedPackages = resources.getStringArray(resourceId);
499        } else {
500            mUnrestrictedPackages = new String[0];
501        }
502    }
503
504    @Override
505    public void onOpen(SQLiteDatabase db) {
506        mSyncState.onDatabaseOpened(db);
507
508        // Create compiled statements for package and mimetype lookups
509        mMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns._ID + " FROM "
510                + Tables.MIMETYPES + " WHERE " + MimetypesColumns.MIMETYPE + "=?");
511        mPackageQuery = db.compileStatement("SELECT " + PackagesColumns._ID + " FROM "
512                + Tables.PACKAGES + " WHERE " + PackagesColumns.PACKAGE + "=?");
513        mContactIdQuery = db.compileStatement("SELECT " + RawContacts.CONTACT_ID + " FROM "
514                + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=?");
515        mAggregationModeQuery = db.compileStatement("SELECT " + RawContacts.AGGREGATION_MODE
516                + " FROM " + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=?");
517        mMimetypeInsert = db.compileStatement("INSERT INTO " + Tables.MIMETYPES + "("
518                + MimetypesColumns.MIMETYPE + ") VALUES (?)");
519        mPackageInsert = db.compileStatement("INSERT INTO " + Tables.PACKAGES + "("
520                + PackagesColumns.PACKAGE + ") VALUES (?)");
521
522        mDataMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns.MIMETYPE + " FROM "
523                + Tables.DATA_JOIN_MIMETYPES + " WHERE " + Tables.DATA + "." + Data._ID + "=?");
524        mActivitiesMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns.MIMETYPE
525                + " FROM " + Tables.ACTIVITIES_JOIN_MIMETYPES + " WHERE " + Tables.ACTIVITIES + "."
526                + Activities._ID + "=?");
527
528        // Compile statements for updating visibility
529        final String visibleUpdate = "UPDATE " + Tables.CONTACTS + " SET "
530                + Contacts.IN_VISIBLE_GROUP + "=(" + Clauses.CONTACT_IS_VISIBLE + ")";
531
532        mVisibleUpdate = db.compileStatement(visibleUpdate);
533        mVisibleSpecificUpdate = db.compileStatement(visibleUpdate + " WHERE "
534                + ContactsColumns.CONCRETE_ID + "=?");
535
536        String visibleUpdateRawContacts =
537                "UPDATE " + Tables.RAW_CONTACTS +
538                " SET " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "=(" +
539                        "SELECT " + Contacts.IN_VISIBLE_GROUP +
540                        " FROM " + Tables.CONTACTS +
541                        " WHERE " + Contacts._ID + "=" + RawContacts.CONTACT_ID + ")" +
542                " WHERE " + RawContacts.DELETED + "=0";
543
544        mVisibleUpdateRawContacts = db.compileStatement(visibleUpdateRawContacts);
545        mVisibleSpecificUpdateRawContacts = db.compileStatement(visibleUpdateRawContacts +
546                    " AND " + RawContacts.CONTACT_ID + "=?");
547
548        db.execSQL("ATTACH DATABASE ':memory:' AS " + DATABASE_PRESENCE + ";");
549        db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_PRESENCE + "." + Tables.PRESENCE + " ("+
550                StatusUpdates.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," +
551                StatusUpdates.PROTOCOL + " INTEGER NOT NULL," +
552                StatusUpdates.CUSTOM_PROTOCOL + " TEXT," +
553                StatusUpdates.IM_HANDLE + " TEXT," +
554                StatusUpdates.IM_ACCOUNT + " TEXT," +
555                PresenceColumns.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," +
556                PresenceColumns.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
557                StatusUpdates.PRESENCE + " INTEGER," +
558                "UNIQUE(" + StatusUpdates.PROTOCOL + ", " + StatusUpdates.CUSTOM_PROTOCOL
559                    + ", " + StatusUpdates.IM_HANDLE + ", " + StatusUpdates.IM_ACCOUNT + ")" +
560        ");");
561
562        db.execSQL("CREATE INDEX IF NOT EXISTS " + DATABASE_PRESENCE + ".presenceIndex" + " ON "
563                + Tables.PRESENCE + " (" + PresenceColumns.RAW_CONTACT_ID + ");");
564
565        db.execSQL("CREATE TABLE IF NOT EXISTS "
566                        + DATABASE_PRESENCE + "." + Tables.AGGREGATED_PRESENCE + " ("+
567                AggregatedPresenceColumns.CONTACT_ID
568                        + " INTEGER PRIMARY KEY REFERENCES contacts(_id)," +
569                StatusUpdates.PRESENCE_STATUS + " INTEGER" +
570        ");");
571
572
573        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_deleted"
574                + " BEFORE DELETE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
575                + " BEGIN "
576                + "   DELETE FROM " + Tables.AGGREGATED_PRESENCE
577                + "     WHERE " + AggregatedPresenceColumns.CONTACT_ID + " = " +
578                        "(SELECT " + PresenceColumns.CONTACT_ID +
579                        " FROM " + Tables.PRESENCE +
580                        " WHERE " + PresenceColumns.RAW_CONTACT_ID
581                                + "=OLD." + PresenceColumns.RAW_CONTACT_ID +
582                        " AND NOT EXISTS" +
583                                "(SELECT " + PresenceColumns.RAW_CONTACT_ID +
584                                " FROM " + Tables.PRESENCE +
585                                " WHERE " + PresenceColumns.CONTACT_ID
586                                        + "=OLD." + PresenceColumns.CONTACT_ID +
587                                " AND " + PresenceColumns.RAW_CONTACT_ID
588                                        + "!=OLD." + PresenceColumns.RAW_CONTACT_ID + "));"
589                + " END");
590
591        String replaceAggregatePresenceSql =
592            "INSERT OR REPLACE INTO " + Tables.AGGREGATED_PRESENCE + "("
593                    + AggregatedPresenceColumns.CONTACT_ID + ", "
594                    + StatusUpdates.PRESENCE_STATUS + ")" +
595            " SELECT " + PresenceColumns.CONTACT_ID + ","
596                        + "MAX(" + StatusUpdates.PRESENCE_STATUS + ")" +
597                    " FROM " + Tables.PRESENCE +
598                    " WHERE " + PresenceColumns.CONTACT_ID
599                        + "=NEW." + PresenceColumns.CONTACT_ID + ";";
600
601        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_inserted"
602                + " AFTER INSERT ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
603                + " BEGIN "
604                + replaceAggregatePresenceSql
605                + " END");
606
607        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_updated"
608                + " AFTER UPDATE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
609                + " BEGIN "
610                + replaceAggregatePresenceSql
611                + " END");
612    }
613
614    @Override
615    public void onCreate(SQLiteDatabase db) {
616        Log.i(TAG, "Bootstrapping database");
617
618        mSyncState.createDatabase(db);
619
620        // One row per group of contacts corresponding to the same person
621        db.execSQL("CREATE TABLE " + Tables.CONTACTS + " (" +
622                BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
623                Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
624                Contacts.PHOTO_ID + " INTEGER REFERENCES data(_id)," +
625                Contacts.CUSTOM_RINGTONE + " TEXT," +
626                Contacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," +
627                Contacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
628                Contacts.LAST_TIME_CONTACTED + " INTEGER," +
629                Contacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
630                Contacts.IN_VISIBLE_GROUP + " INTEGER NOT NULL DEFAULT 1," +
631                Contacts.HAS_PHONE_NUMBER + " INTEGER NOT NULL DEFAULT 0," +
632                Contacts.LOOKUP_KEY + " TEXT," +
633                ContactsColumns.LAST_STATUS_UPDATE_ID + " INTEGER REFERENCES data(_id)," +
634                ContactsColumns.SINGLE_IS_RESTRICTED + " INTEGER NOT NULL DEFAULT 0" +
635        ");");
636
637        db.execSQL("CREATE INDEX contacts_visible_index ON " + Tables.CONTACTS + " (" +
638                Contacts.IN_VISIBLE_GROUP +
639        ");");
640
641        db.execSQL("CREATE INDEX contacts_has_phone_index ON " + Tables.CONTACTS + " (" +
642                Contacts.HAS_PHONE_NUMBER +
643        ");");
644
645        db.execSQL("CREATE INDEX contacts_restricted_index ON " + Tables.CONTACTS + " (" +
646                ContactsColumns.SINGLE_IS_RESTRICTED +
647        ");");
648
649        db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" +
650                Contacts.NAME_RAW_CONTACT_ID +
651        ");");
652
653        // Contacts table
654        db.execSQL("CREATE TABLE " + Tables.RAW_CONTACTS + " (" +
655                RawContacts._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
656                RawContacts.IS_RESTRICTED + " INTEGER DEFAULT 0," +
657                RawContacts.ACCOUNT_NAME + " STRING DEFAULT NULL, " +
658                RawContacts.ACCOUNT_TYPE + " STRING DEFAULT NULL, " +
659                RawContacts.SOURCE_ID + " TEXT," +
660                RawContacts.VERSION + " INTEGER NOT NULL DEFAULT 1," +
661                RawContacts.DIRTY + " INTEGER NOT NULL DEFAULT 0," +
662                RawContacts.DELETED + " INTEGER NOT NULL DEFAULT 0," +
663                RawContacts.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," +
664                RawContacts.AGGREGATION_MODE + " INTEGER NOT NULL DEFAULT " +
665                        RawContacts.AGGREGATION_MODE_DEFAULT + "," +
666                RawContactsColumns.AGGREGATION_NEEDED + " INTEGER NOT NULL DEFAULT 1," +
667                RawContacts.CUSTOM_RINGTONE + " TEXT," +
668                RawContacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," +
669                RawContacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
670                RawContacts.LAST_TIME_CONTACTED + " INTEGER," +
671                RawContacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
672                RawContacts.DISPLAY_NAME_PRIMARY + " TEXT," +
673                RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT," +
674                RawContacts.DISPLAY_NAME_SOURCE + " INTEGER NOT NULL DEFAULT " +
675                        DisplayNameSources.UNDEFINED + "," +
676                RawContacts.PHONETIC_NAME + " TEXT," +
677                RawContacts.PHONETIC_NAME_STYLE + " TEXT," +
678                RawContacts.SORT_KEY_PRIMARY + " TEXT COLLATE LOCALIZED," +
679                RawContacts.SORT_KEY_ALTERNATIVE + " TEXT COLLATE LOCALIZED," +
680                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + " INTEGER NOT NULL DEFAULT 0," +
681                RawContacts.SYNC1 + " TEXT, " +
682                RawContacts.SYNC2 + " TEXT, " +
683                RawContacts.SYNC3 + " TEXT, " +
684                RawContacts.SYNC4 + " TEXT " +
685        ");");
686
687        db.execSQL("CREATE INDEX raw_contacts_contact_id_index ON " + Tables.RAW_CONTACTS + " (" +
688                RawContacts.CONTACT_ID +
689        ");");
690
691        db.execSQL("CREATE INDEX raw_contacts_source_id_index ON " + Tables.RAW_CONTACTS + " (" +
692                RawContacts.SOURCE_ID + ", " +
693                RawContacts.ACCOUNT_TYPE + ", " +
694                RawContacts.ACCOUNT_NAME +
695        ");");
696
697        // TODO readd the index and investigate a controlled use of it
698//        db.execSQL("CREATE INDEX raw_contacts_agg_index ON " + Tables.RAW_CONTACTS + " (" +
699//                RawContactsColumns.AGGREGATION_NEEDED +
700//        ");");
701
702        db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
703                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
704                RawContacts.SORT_KEY_PRIMARY +
705        ");");
706
707        db.execSQL("CREATE INDEX raw_contact_sort_key2_index ON " + Tables.RAW_CONTACTS + " (" +
708                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
709                RawContacts.SORT_KEY_ALTERNATIVE +
710        ");");
711
712        // Package name mapping table
713        db.execSQL("CREATE TABLE " + Tables.PACKAGES + " (" +
714                PackagesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
715                PackagesColumns.PACKAGE + " TEXT NOT NULL" +
716        ");");
717
718        // Mimetype mapping table
719        db.execSQL("CREATE TABLE " + Tables.MIMETYPES + " (" +
720                MimetypesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
721                MimetypesColumns.MIMETYPE + " TEXT NOT NULL" +
722        ");");
723
724        // Public generic data table
725        db.execSQL("CREATE TABLE " + Tables.DATA + " (" +
726                Data._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
727                DataColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
728                DataColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," +
729                Data.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
730                Data.IS_PRIMARY + " INTEGER NOT NULL DEFAULT 0," +
731                Data.IS_SUPER_PRIMARY + " INTEGER NOT NULL DEFAULT 0," +
732                Data.DATA_VERSION + " INTEGER NOT NULL DEFAULT 0," +
733                Data.DATA1 + " TEXT," +
734                Data.DATA2 + " TEXT," +
735                Data.DATA3 + " TEXT," +
736                Data.DATA4 + " TEXT," +
737                Data.DATA5 + " TEXT," +
738                Data.DATA6 + " TEXT," +
739                Data.DATA7 + " TEXT," +
740                Data.DATA8 + " TEXT," +
741                Data.DATA9 + " TEXT," +
742                Data.DATA10 + " TEXT," +
743                Data.DATA11 + " TEXT," +
744                Data.DATA12 + " TEXT," +
745                Data.DATA13 + " TEXT," +
746                Data.DATA14 + " TEXT," +
747                Data.DATA15 + " TEXT," +
748                Data.SYNC1 + " TEXT, " +
749                Data.SYNC2 + " TEXT, " +
750                Data.SYNC3 + " TEXT, " +
751                Data.SYNC4 + " TEXT " +
752        ");");
753
754        db.execSQL("CREATE INDEX data_raw_contact_id ON " + Tables.DATA + " (" +
755                Data.RAW_CONTACT_ID +
756        ");");
757
758        /**
759         * For email lookup and similar queries.
760         */
761        db.execSQL("CREATE INDEX data_mimetype_data1_index ON " + Tables.DATA + " (" +
762                DataColumns.MIMETYPE_ID + "," +
763                Data.DATA1 +
764        ");");
765
766        // Private phone numbers table used for lookup
767        db.execSQL("CREATE TABLE " + Tables.PHONE_LOOKUP + " (" +
768                PhoneLookupColumns.DATA_ID
769                        + " INTEGER PRIMARY KEY REFERENCES data(_id) NOT NULL," +
770                PhoneLookupColumns.RAW_CONTACT_ID
771                        + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
772                PhoneLookupColumns.NORMALIZED_NUMBER + " TEXT NOT NULL," +
773                PhoneLookupColumns.MIN_MATCH + " TEXT NOT NULL" +
774        ");");
775
776        db.execSQL("CREATE INDEX phone_lookup_index ON " + Tables.PHONE_LOOKUP + " (" +
777                PhoneLookupColumns.NORMALIZED_NUMBER + "," +
778                PhoneLookupColumns.RAW_CONTACT_ID + "," +
779                PhoneLookupColumns.DATA_ID +
780        ");");
781
782        db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" +
783                PhoneLookupColumns.MIN_MATCH + "," +
784                PhoneLookupColumns.RAW_CONTACT_ID + "," +
785                PhoneLookupColumns.DATA_ID +
786        ");");
787
788        // Private name/nickname table used for lookup
789        db.execSQL("CREATE TABLE " + Tables.NAME_LOOKUP + " (" +
790                NameLookupColumns.DATA_ID
791                        + " INTEGER REFERENCES data(_id) NOT NULL," +
792                NameLookupColumns.RAW_CONTACT_ID
793                        + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
794                NameLookupColumns.NORMALIZED_NAME + " TEXT NOT NULL," +
795                NameLookupColumns.NAME_TYPE + " INTEGER NOT NULL," +
796                "PRIMARY KEY ("
797                        + NameLookupColumns.DATA_ID + ", "
798                        + NameLookupColumns.NORMALIZED_NAME + ", "
799                        + NameLookupColumns.NAME_TYPE + ")" +
800        ");");
801
802        db.execSQL("CREATE INDEX name_lookup_index ON " + Tables.NAME_LOOKUP + " (" +
803                NameLookupColumns.NORMALIZED_NAME + "," +
804                NameLookupColumns.NAME_TYPE + ", " +
805                NameLookupColumns.RAW_CONTACT_ID +
806        ");");
807
808        db.execSQL("CREATE INDEX name_lookup_raw_contact_id_index ON " + Tables.NAME_LOOKUP + " (" +
809                NameLookupColumns.RAW_CONTACT_ID +
810        ");");
811
812        db.execSQL("CREATE TABLE " + Tables.NICKNAME_LOOKUP + " (" +
813                NicknameLookupColumns.NAME + " TEXT," +
814                NicknameLookupColumns.CLUSTER + " TEXT" +
815        ");");
816
817        db.execSQL("CREATE UNIQUE INDEX nickname_lookup_index ON " + Tables.NICKNAME_LOOKUP + " (" +
818                NicknameLookupColumns.NAME + ", " +
819                NicknameLookupColumns.CLUSTER +
820        ");");
821
822        // Groups table
823        db.execSQL("CREATE TABLE " + Tables.GROUPS + " (" +
824                Groups._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
825                GroupsColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
826                Groups.ACCOUNT_NAME + " STRING DEFAULT NULL, " +
827                Groups.ACCOUNT_TYPE + " STRING DEFAULT NULL, " +
828                Groups.SOURCE_ID + " TEXT," +
829                Groups.VERSION + " INTEGER NOT NULL DEFAULT 1," +
830                Groups.DIRTY + " INTEGER NOT NULL DEFAULT 0," +
831                Groups.TITLE + " TEXT," +
832                Groups.TITLE_RES + " INTEGER," +
833                Groups.NOTES + " TEXT," +
834                Groups.SYSTEM_ID + " TEXT," +
835                Groups.DELETED + " INTEGER NOT NULL DEFAULT 0," +
836                Groups.GROUP_VISIBLE + " INTEGER NOT NULL DEFAULT 0," +
837                Groups.SHOULD_SYNC + " INTEGER NOT NULL DEFAULT 1," +
838                Groups.SYNC1 + " TEXT, " +
839                Groups.SYNC2 + " TEXT, " +
840                Groups.SYNC3 + " TEXT, " +
841                Groups.SYNC4 + " TEXT " +
842        ");");
843
844        db.execSQL("CREATE INDEX groups_source_id_index ON " + Tables.GROUPS + " (" +
845                Groups.SOURCE_ID + ", " +
846                Groups.ACCOUNT_TYPE + ", " +
847                Groups.ACCOUNT_NAME +
848        ");");
849
850        db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.AGGREGATION_EXCEPTIONS + " (" +
851                AggregationExceptionColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
852                AggregationExceptions.TYPE + " INTEGER NOT NULL, " +
853                AggregationExceptions.RAW_CONTACT_ID1
854                        + " INTEGER REFERENCES raw_contacts(_id), " +
855                AggregationExceptions.RAW_CONTACT_ID2
856                        + " INTEGER REFERENCES raw_contacts(_id)" +
857        ");");
858
859        db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS aggregation_exception_index1 ON " +
860                Tables.AGGREGATION_EXCEPTIONS + " (" +
861                AggregationExceptions.RAW_CONTACT_ID1 + ", " +
862                AggregationExceptions.RAW_CONTACT_ID2 +
863        ");");
864
865        db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS aggregation_exception_index2 ON " +
866                Tables.AGGREGATION_EXCEPTIONS + " (" +
867                AggregationExceptions.RAW_CONTACT_ID2 + ", " +
868                AggregationExceptions.RAW_CONTACT_ID1 +
869        ");");
870
871        db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.SETTINGS + " (" +
872                Settings.ACCOUNT_NAME + " STRING NOT NULL," +
873                Settings.ACCOUNT_TYPE + " STRING NOT NULL," +
874                Settings.UNGROUPED_VISIBLE + " INTEGER NOT NULL DEFAULT 0," +
875                Settings.SHOULD_SYNC + " INTEGER NOT NULL DEFAULT 1, " +
876                "PRIMARY KEY (" + Settings.ACCOUNT_NAME + ", " +
877                    Settings.ACCOUNT_TYPE + ") ON CONFLICT REPLACE" +
878        ");");
879
880        // The table for recent calls is here so we can do table joins
881        // on people, phones, and calls all in one place.
882        db.execSQL("CREATE TABLE " + Tables.CALLS + " (" +
883                Calls._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
884                Calls.NUMBER + " TEXT," +
885                Calls.DATE + " INTEGER," +
886                Calls.DURATION + " INTEGER," +
887                Calls.TYPE + " INTEGER," +
888                Calls.NEW + " INTEGER," +
889                Calls.CACHED_NAME + " TEXT," +
890                Calls.CACHED_NUMBER_TYPE + " INTEGER," +
891                Calls.CACHED_NUMBER_LABEL + " TEXT" +
892        ");");
893
894        // Activities table
895        db.execSQL("CREATE TABLE " + Tables.ACTIVITIES + " (" +
896                Activities._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
897                ActivitiesColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
898                ActivitiesColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," +
899                Activities.RAW_ID + " TEXT," +
900                Activities.IN_REPLY_TO + " TEXT," +
901                Activities.AUTHOR_CONTACT_ID +  " INTEGER REFERENCES raw_contacts(_id)," +
902                Activities.TARGET_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
903                Activities.PUBLISHED + " INTEGER NOT NULL," +
904                Activities.THREAD_PUBLISHED + " INTEGER NOT NULL," +
905                Activities.TITLE + " TEXT NOT NULL," +
906                Activities.SUMMARY + " TEXT," +
907                Activities.LINK + " TEXT, " +
908                Activities.THUMBNAIL + " BLOB" +
909        ");");
910
911        db.execSQL("CREATE TABLE " + Tables.STATUS_UPDATES + " (" +
912                StatusUpdatesColumns.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," +
913                StatusUpdates.STATUS + " TEXT," +
914                StatusUpdates.STATUS_TIMESTAMP + " INTEGER," +
915                StatusUpdates.STATUS_RES_PACKAGE + " TEXT, " +
916                StatusUpdates.STATUS_LABEL + " INTEGER, " +
917                StatusUpdates.STATUS_ICON + " INTEGER" +
918        ");");
919
920        createContactsViews(db);
921        createGroupsView(db);
922        createContactEntitiesView(db);
923        createContactsTriggers(db);
924
925        loadNicknameLookupTable(db);
926
927        // Add the legacy API support views, etc
928        LegacyApiSupport.createDatabase(db);
929
930        // This will create a sqlite_stat1 table that is used for query optimization
931        db.execSQL("ANALYZE;");
932
933        updateSqliteStats(db);
934
935        // We need to close and reopen the database connection so that the stats are
936        // taken into account. Make a note of it and do the actual reopening in the
937        // getWritableDatabase method.
938        mReopenDatabase = true;
939
940        ContentResolver.requestSync(null /* all accounts */,
941                ContactsContract.AUTHORITY, new Bundle());
942    }
943
944    private void createContactsTriggers(SQLiteDatabase db) {
945
946        /*
947         * Automatically delete Data rows when a raw contact is deleted.
948         */
949        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.RAW_CONTACTS + "_deleted;");
950        db.execSQL("CREATE TRIGGER " + Tables.RAW_CONTACTS + "_deleted "
951                + "   BEFORE DELETE ON " + Tables.RAW_CONTACTS
952                + " BEGIN "
953                + "   DELETE FROM " + Tables.DATA
954                + "     WHERE " + Data.RAW_CONTACT_ID
955                                + "=OLD." + RawContacts._ID + ";"
956                + "   DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS
957                + "     WHERE " + AggregationExceptions.RAW_CONTACT_ID1
958                                + "=OLD." + RawContacts._ID
959                + "        OR " + AggregationExceptions.RAW_CONTACT_ID2
960                                + "=OLD." + RawContacts._ID + ";"
961                + "   DELETE FROM " + Tables.CONTACTS
962                + "     WHERE " + Contacts._ID + "=OLD." + RawContacts.CONTACT_ID
963                + "       AND (SELECT COUNT(*) FROM " + Tables.RAW_CONTACTS
964                + "            WHERE " + RawContacts.CONTACT_ID + "=OLD." + RawContacts.CONTACT_ID
965                + "           )=1;"
966                + " END");
967
968
969        db.execSQL("DROP TRIGGER IF EXISTS contacts_times_contacted;");
970
971        /*
972         * Triggers that update {@link RawContacts#VERSION} when the contact is
973         * marked for deletion or any time a data row is inserted, updated or
974         * deleted.
975         */
976        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.RAW_CONTACTS + "_marked_deleted;");
977        db.execSQL("CREATE TRIGGER " + Tables.RAW_CONTACTS + "_marked_deleted "
978                + "   AFTER UPDATE ON " + Tables.RAW_CONTACTS
979                + " BEGIN "
980                + "   UPDATE " + Tables.RAW_CONTACTS
981                + "     SET "
982                +         RawContacts.VERSION + "=OLD." + RawContacts.VERSION + "+1 "
983                + "     WHERE " + RawContacts._ID + "=OLD." + RawContacts._ID
984                + "       AND NEW." + RawContacts.DELETED + "!= OLD." + RawContacts.DELETED + ";"
985                + " END");
986
987        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.DATA + "_updated;");
988        db.execSQL("CREATE TRIGGER " + Tables.DATA + "_updated AFTER UPDATE ON " + Tables.DATA
989                + " BEGIN "
990                + "   UPDATE " + Tables.DATA
991                + "     SET " + Data.DATA_VERSION + "=OLD." + Data.DATA_VERSION + "+1 "
992                + "     WHERE " + Data._ID + "=OLD." + Data._ID + ";"
993                + "   UPDATE " + Tables.RAW_CONTACTS
994                + "     SET " + RawContacts.VERSION + "=" + RawContacts.VERSION + "+1 "
995                + "     WHERE " + RawContacts._ID + "=OLD." + Data.RAW_CONTACT_ID + ";"
996                + " END");
997
998        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.DATA + "_deleted;");
999        db.execSQL("CREATE TRIGGER " + Tables.DATA + "_deleted BEFORE DELETE ON " + Tables.DATA
1000                + " BEGIN "
1001                + "   UPDATE " + Tables.RAW_CONTACTS
1002                + "     SET " + RawContacts.VERSION + "=" + RawContacts.VERSION + "+1 "
1003                + "     WHERE " + RawContacts._ID + "=OLD." + Data.RAW_CONTACT_ID + ";"
1004                + "   DELETE FROM " + Tables.PHONE_LOOKUP
1005                + "     WHERE " + PhoneLookupColumns.DATA_ID + "=OLD." + Data._ID + ";"
1006                + "   DELETE FROM " + Tables.STATUS_UPDATES
1007                + "     WHERE " + StatusUpdatesColumns.DATA_ID + "=OLD." + Data._ID + ";"
1008                + "   DELETE FROM " + Tables.NAME_LOOKUP
1009                + "     WHERE " + NameLookupColumns.DATA_ID + "=OLD." + Data._ID + ";"
1010                + " END");
1011
1012
1013        db.execSQL("DROP TRIGGER IF EXISTS " + Tables.GROUPS + "_updated1;");
1014        db.execSQL("CREATE TRIGGER " + Tables.GROUPS + "_updated1 "
1015                + "   AFTER UPDATE ON " + Tables.GROUPS
1016                + " BEGIN "
1017                + "   UPDATE " + Tables.GROUPS
1018                + "     SET "
1019                +         Groups.VERSION + "=OLD." + Groups.VERSION + "+1"
1020                + "     WHERE " + Groups._ID + "=OLD." + Groups._ID + ";"
1021                + " END");
1022    }
1023
1024    private static void createContactsViews(SQLiteDatabase db) {
1025        db.execSQL("DROP VIEW IF EXISTS " + Views.CONTACTS_ALL + ";");
1026        db.execSQL("DROP VIEW IF EXISTS " + Views.CONTACTS_RESTRICTED + ";");
1027        db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_ALL + ";");
1028        db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_RESTRICTED + ";");
1029        db.execSQL("DROP VIEW IF EXISTS " + Views.RAW_CONTACTS_ALL + ";");
1030        db.execSQL("DROP VIEW IF EXISTS " + Views.RAW_CONTACTS_RESTRICTED + ";");
1031
1032        String dataColumns =
1033                Data.IS_PRIMARY + ", "
1034                + Data.IS_SUPER_PRIMARY + ", "
1035                + Data.DATA_VERSION + ", "
1036                + PackagesColumns.PACKAGE + " AS " + Data.RES_PACKAGE + ","
1037                + MimetypesColumns.MIMETYPE + " AS " + Data.MIMETYPE + ", "
1038                + Data.DATA1 + ", "
1039                + Data.DATA2 + ", "
1040                + Data.DATA3 + ", "
1041                + Data.DATA4 + ", "
1042                + Data.DATA5 + ", "
1043                + Data.DATA6 + ", "
1044                + Data.DATA7 + ", "
1045                + Data.DATA8 + ", "
1046                + Data.DATA9 + ", "
1047                + Data.DATA10 + ", "
1048                + Data.DATA11 + ", "
1049                + Data.DATA12 + ", "
1050                + Data.DATA13 + ", "
1051                + Data.DATA14 + ", "
1052                + Data.DATA15 + ", "
1053                + Data.SYNC1 + ", "
1054                + Data.SYNC2 + ", "
1055                + Data.SYNC3 + ", "
1056                + Data.SYNC4;
1057
1058        String syncColumns =
1059                RawContactsColumns.CONCRETE_ACCOUNT_NAME + " AS " + RawContacts.ACCOUNT_NAME + ","
1060                + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " AS " + RawContacts.ACCOUNT_TYPE + ","
1061                + RawContactsColumns.CONCRETE_SOURCE_ID + " AS " + RawContacts.SOURCE_ID + ","
1062                + RawContactsColumns.CONCRETE_VERSION + " AS " + RawContacts.VERSION + ","
1063                + RawContactsColumns.CONCRETE_DIRTY + " AS " + RawContacts.DIRTY + ","
1064                + RawContactsColumns.CONCRETE_SYNC1 + " AS " + RawContacts.SYNC1 + ","
1065                + RawContactsColumns.CONCRETE_SYNC2 + " AS " + RawContacts.SYNC2 + ","
1066                + RawContactsColumns.CONCRETE_SYNC3 + " AS " + RawContacts.SYNC3 + ","
1067                + RawContactsColumns.CONCRETE_SYNC4 + " AS " + RawContacts.SYNC4;
1068
1069        String contactOptionColumns =
1070                ContactsColumns.CONCRETE_CUSTOM_RINGTONE
1071                        + " AS " + RawContacts.CUSTOM_RINGTONE + ","
1072                + ContactsColumns.CONCRETE_SEND_TO_VOICEMAIL
1073                        + " AS " + RawContacts.SEND_TO_VOICEMAIL + ","
1074                + ContactsColumns.CONCRETE_LAST_TIME_CONTACTED
1075                        + " AS " + RawContacts.LAST_TIME_CONTACTED + ","
1076                + ContactsColumns.CONCRETE_TIMES_CONTACTED
1077                        + " AS " + RawContacts.TIMES_CONTACTED + ","
1078                + ContactsColumns.CONCRETE_STARRED
1079                        + " AS " + RawContacts.STARRED;
1080
1081        String contactNameColumns =
1082                "name_raw_contact." + RawContacts.DISPLAY_NAME_SOURCE
1083                        + " AS " + Contacts.DISPLAY_NAME_SOURCE + ", "
1084                + "name_raw_contact." + RawContacts.DISPLAY_NAME_PRIMARY
1085                        + " AS " + Contacts.DISPLAY_NAME_PRIMARY + ", "
1086                + "name_raw_contact." + RawContacts.DISPLAY_NAME_ALTERNATIVE
1087                        + " AS " + Contacts.DISPLAY_NAME_ALTERNATIVE + ", "
1088                + "name_raw_contact." + RawContacts.PHONETIC_NAME
1089                        + " AS " + Contacts.PHONETIC_NAME + ", "
1090                + "name_raw_contact." + RawContacts.PHONETIC_NAME_STYLE
1091                        + " AS " + Contacts.PHONETIC_NAME_STYLE + ", "
1092                + "name_raw_contact." + RawContacts.SORT_KEY_PRIMARY
1093                        + " AS " + Contacts.SORT_KEY_PRIMARY + ", "
1094                + "name_raw_contact." + RawContacts.SORT_KEY_ALTERNATIVE
1095                        + " AS " + Contacts.SORT_KEY_ALTERNATIVE + ", "
1096                + "name_raw_contact." + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP
1097                        + " AS " + Contacts.IN_VISIBLE_GROUP;
1098
1099        String dataSelect = "SELECT "
1100                + DataColumns.CONCRETE_ID + " AS " + Data._ID + ","
1101                + Data.RAW_CONTACT_ID + ", "
1102                + RawContactsColumns.CONCRETE_CONTACT_ID + " AS " + RawContacts.CONTACT_ID + ", "
1103                + syncColumns + ", "
1104                + dataColumns + ", "
1105                + contactOptionColumns + ", "
1106                + contactNameColumns + ", "
1107                + Contacts.LOOKUP_KEY + ", "
1108                + Contacts.PHOTO_ID + ", "
1109                + ContactsColumns.LAST_STATUS_UPDATE_ID + ", "
1110                + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID
1111                + " FROM " + Tables.DATA
1112                + " JOIN " + Tables.MIMETYPES + " ON ("
1113                +   DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")"
1114                + " JOIN " + Tables.RAW_CONTACTS + " ON ("
1115                +   DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")"
1116                + " JOIN " + Tables.CONTACTS + " ON ("
1117                +   RawContactsColumns.CONCRETE_CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID + ")"
1118                + " JOIN " + Tables.RAW_CONTACTS + " AS name_raw_contact ON("
1119                +   Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")"
1120                + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON ("
1121                +   DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")"
1122                + " LEFT OUTER JOIN " + Tables.GROUPS + " ON ("
1123                +   MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE
1124                +   "' AND " + GroupsColumns.CONCRETE_ID + "="
1125                        + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")";
1126
1127        db.execSQL("CREATE VIEW " + Views.DATA_ALL + " AS " + dataSelect);
1128        db.execSQL("CREATE VIEW " + Views.DATA_RESTRICTED + " AS " + dataSelect + " WHERE "
1129                + RawContactsColumns.CONCRETE_IS_RESTRICTED + "=0");
1130
1131        String rawContactOptionColumns =
1132                RawContacts.CUSTOM_RINGTONE + ","
1133                + RawContacts.SEND_TO_VOICEMAIL + ","
1134                + RawContacts.LAST_TIME_CONTACTED + ","
1135                + RawContacts.TIMES_CONTACTED + ","
1136                + RawContacts.STARRED;
1137
1138        String rawContactsSelect = "SELECT "
1139                + RawContactsColumns.CONCRETE_ID + " AS " + RawContacts._ID + ","
1140                + RawContacts.CONTACT_ID + ", "
1141                + RawContacts.AGGREGATION_MODE + ", "
1142                + RawContacts.DELETED + ", "
1143                + RawContacts.DISPLAY_NAME_SOURCE  + ", "
1144                + RawContacts.DISPLAY_NAME_PRIMARY  + ", "
1145                + RawContacts.DISPLAY_NAME_ALTERNATIVE  + ", "
1146                + RawContacts.PHONETIC_NAME  + ", "
1147                + RawContacts.PHONETIC_NAME_STYLE  + ", "
1148                + RawContacts.SORT_KEY_PRIMARY  + ", "
1149                + RawContacts.SORT_KEY_ALTERNATIVE + ", "
1150                + rawContactOptionColumns + ", "
1151                + syncColumns
1152                + " FROM " + Tables.RAW_CONTACTS;
1153
1154        db.execSQL("CREATE VIEW " + Views.RAW_CONTACTS_ALL + " AS " + rawContactsSelect);
1155        db.execSQL("CREATE VIEW " + Views.RAW_CONTACTS_RESTRICTED + " AS " + rawContactsSelect
1156                + " WHERE " + RawContacts.IS_RESTRICTED + "=0");
1157
1158        String contactsColumns =
1159                ContactsColumns.CONCRETE_CUSTOM_RINGTONE
1160                        + " AS " + Contacts.CUSTOM_RINGTONE + ", "
1161                + contactNameColumns + ", "
1162                + Contacts.HAS_PHONE_NUMBER + ", "
1163                + Contacts.LOOKUP_KEY + ", "
1164                + Contacts.PHOTO_ID + ", "
1165                + ContactsColumns.CONCRETE_LAST_TIME_CONTACTED
1166                        + " AS " + Contacts.LAST_TIME_CONTACTED + ", "
1167                + ContactsColumns.CONCRETE_SEND_TO_VOICEMAIL
1168                        + " AS " + Contacts.SEND_TO_VOICEMAIL + ", "
1169                + ContactsColumns.CONCRETE_STARRED
1170                        + " AS " + Contacts.STARRED + ", "
1171                + ContactsColumns.CONCRETE_TIMES_CONTACTED
1172                        + " AS " + Contacts.TIMES_CONTACTED + ", "
1173                + ContactsColumns.LAST_STATUS_UPDATE_ID;
1174
1175        String contactsSelect = "SELECT "
1176                + ContactsColumns.CONCRETE_ID + " AS " + Contacts._ID + ","
1177                + contactsColumns
1178                + " FROM " + Tables.CONTACTS
1179                + " JOIN " + Tables.RAW_CONTACTS + " AS name_raw_contact ON("
1180                +   Contacts.NAME_RAW_CONTACT_ID + "=name_raw_contact." + RawContacts._ID + ")";
1181
1182        db.execSQL("CREATE VIEW " + Views.CONTACTS_ALL + " AS " + contactsSelect);
1183        db.execSQL("CREATE VIEW " + Views.CONTACTS_RESTRICTED + " AS " + contactsSelect
1184                + " WHERE " + ContactsColumns.SINGLE_IS_RESTRICTED + "=0");
1185    }
1186
1187    private static void createGroupsView(SQLiteDatabase db) {
1188        db.execSQL("DROP VIEW IF EXISTS " + Views.GROUPS_ALL + ";");
1189        String groupsColumns =
1190                Groups.ACCOUNT_NAME + ","
1191                + Groups.ACCOUNT_TYPE + ","
1192                + Groups.SOURCE_ID + ","
1193                + Groups.VERSION + ","
1194                + Groups.DIRTY + ","
1195                + Groups.TITLE + ","
1196                + Groups.TITLE_RES + ","
1197                + Groups.NOTES + ","
1198                + Groups.SYSTEM_ID + ","
1199                + Groups.DELETED + ","
1200                + Groups.GROUP_VISIBLE + ","
1201                + Groups.SHOULD_SYNC + ","
1202                + Groups.SYNC1 + ","
1203                + Groups.SYNC2 + ","
1204                + Groups.SYNC3 + ","
1205                + Groups.SYNC4 + ","
1206                + PackagesColumns.PACKAGE + " AS " + Groups.RES_PACKAGE;
1207
1208        String groupsSelect = "SELECT "
1209                + GroupsColumns.CONCRETE_ID + " AS " + Groups._ID + ","
1210                + groupsColumns
1211                + " FROM " + Tables.GROUPS_JOIN_PACKAGES;
1212
1213        db.execSQL("CREATE VIEW " + Views.GROUPS_ALL + " AS " + groupsSelect);
1214    }
1215
1216    private static void createContactEntitiesView(SQLiteDatabase db) {
1217        db.execSQL("DROP VIEW IF EXISTS " + Tables.CONTACT_ENTITIES + ";");
1218        db.execSQL("DROP VIEW IF EXISTS " + Tables.CONTACT_ENTITIES_RESTRICTED + ";");
1219
1220        String contactEntitiesSelect = "SELECT "
1221                + RawContactsColumns.CONCRETE_ACCOUNT_NAME + " AS " + RawContacts.ACCOUNT_NAME + ","
1222                + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " AS " + RawContacts.ACCOUNT_TYPE + ","
1223                + RawContactsColumns.CONCRETE_SOURCE_ID + " AS " + RawContacts.SOURCE_ID + ","
1224                + RawContactsColumns.CONCRETE_VERSION + " AS " + RawContacts.VERSION + ","
1225                + RawContactsColumns.CONCRETE_DIRTY + " AS " + RawContacts.DIRTY + ","
1226                + RawContactsColumns.CONCRETE_DELETED + " AS " + RawContacts.DELETED + ","
1227                + PackagesColumns.PACKAGE + " AS " + Data.RES_PACKAGE + ","
1228                + RawContacts.CONTACT_ID + ", "
1229                + RawContactsColumns.CONCRETE_SYNC1 + " AS " + RawContacts.SYNC1 + ", "
1230                + RawContactsColumns.CONCRETE_SYNC2 + " AS " + RawContacts.SYNC2 + ", "
1231                + RawContactsColumns.CONCRETE_SYNC3 + " AS " + RawContacts.SYNC3 + ", "
1232                + RawContactsColumns.CONCRETE_SYNC4 + " AS " + RawContacts.SYNC4 + ", "
1233                + Data.MIMETYPE + ", "
1234                + Data.DATA1 + ", "
1235                + Data.DATA2 + ", "
1236                + Data.DATA3 + ", "
1237                + Data.DATA4 + ", "
1238                + Data.DATA5 + ", "
1239                + Data.DATA6 + ", "
1240                + Data.DATA7 + ", "
1241                + Data.DATA8 + ", "
1242                + Data.DATA9 + ", "
1243                + Data.DATA10 + ", "
1244                + Data.DATA11 + ", "
1245                + Data.DATA12 + ", "
1246                + Data.DATA13 + ", "
1247                + Data.DATA14 + ", "
1248                + Data.DATA15 + ", "
1249                + Data.SYNC1 + ", "
1250                + Data.SYNC2 + ", "
1251                + Data.SYNC3 + ", "
1252                + Data.SYNC4 + ", "
1253                + RawContactsColumns.CONCRETE_ID + " AS " + RawContacts._ID + ", "
1254                + Data.IS_PRIMARY + ", "
1255                + Data.IS_SUPER_PRIMARY + ", "
1256                + Data.DATA_VERSION + ", "
1257                + DataColumns.CONCRETE_ID + " AS " + RawContacts.Entity.DATA_ID + ","
1258                + RawContactsColumns.CONCRETE_STARRED + " AS " + RawContacts.STARRED + ","
1259                + RawContactsColumns.CONCRETE_IS_RESTRICTED + " AS "
1260                        + RawContacts.IS_RESTRICTED + ","
1261                + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID
1262                + " FROM " + Tables.RAW_CONTACTS
1263                + " LEFT OUTER JOIN " + Tables.DATA + " ON ("
1264                +   DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")"
1265                + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON ("
1266                +   DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")"
1267                + " LEFT OUTER JOIN " + Tables.MIMETYPES + " ON ("
1268                +   DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")"
1269                + " LEFT OUTER JOIN " + Tables.GROUPS + " ON ("
1270                +   MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE
1271                +   "' AND " + GroupsColumns.CONCRETE_ID + "="
1272                + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")";
1273
1274        db.execSQL("CREATE VIEW " + Tables.CONTACT_ENTITIES + " AS "
1275                + contactEntitiesSelect);
1276        db.execSQL("CREATE VIEW " + Tables.CONTACT_ENTITIES_RESTRICTED + " AS "
1277                + contactEntitiesSelect + " WHERE " + RawContacts.IS_RESTRICTED + "=0");
1278    }
1279
1280    @Override
1281    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
1282        if (oldVersion < 99) {
1283            Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion
1284                    + ", data will be lost!");
1285
1286            db.execSQL("DROP TABLE IF EXISTS " + Tables.CONTACTS + ";");
1287            db.execSQL("DROP TABLE IF EXISTS " + Tables.RAW_CONTACTS + ";");
1288            db.execSQL("DROP TABLE IF EXISTS " + Tables.PACKAGES + ";");
1289            db.execSQL("DROP TABLE IF EXISTS " + Tables.MIMETYPES + ";");
1290            db.execSQL("DROP TABLE IF EXISTS " + Tables.DATA + ";");
1291            db.execSQL("DROP TABLE IF EXISTS " + Tables.PHONE_LOOKUP + ";");
1292            db.execSQL("DROP TABLE IF EXISTS " + Tables.NAME_LOOKUP + ";");
1293            db.execSQL("DROP TABLE IF EXISTS " + Tables.NICKNAME_LOOKUP + ";");
1294            db.execSQL("DROP TABLE IF EXISTS " + Tables.GROUPS + ";");
1295            db.execSQL("DROP TABLE IF EXISTS " + Tables.ACTIVITIES + ";");
1296            db.execSQL("DROP TABLE IF EXISTS " + Tables.CALLS + ";");
1297            db.execSQL("DROP TABLE IF EXISTS " + Tables.SETTINGS + ";");
1298            db.execSQL("DROP TABLE IF EXISTS " + Tables.STATUS_UPDATES + ";");
1299
1300            // TODO: we should not be dropping agg_exceptions and contact_options. In case that
1301            // table's schema changes, we should try to preserve the data, because it was entered
1302            // by the user and has never been synched to the server.
1303            db.execSQL("DROP TABLE IF EXISTS " + Tables.AGGREGATION_EXCEPTIONS + ";");
1304
1305            onCreate(db);
1306            return;
1307        }
1308
1309        Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion);
1310
1311        if (oldVersion == 99) {
1312            createContactEntitiesView(db);
1313            oldVersion++;
1314        }
1315
1316        if (oldVersion == 100) {
1317            db.execSQL("CREATE INDEX IF NOT EXISTS mimetypes_mimetype_index ON "
1318                    + Tables.MIMETYPES + " ("
1319                            + MimetypesColumns.MIMETYPE + ","
1320                            + MimetypesColumns._ID + ");");
1321            updateIndexStats(db, Tables.MIMETYPES,
1322                    "mimetypes_mimetype_index", "50 1 1");
1323
1324            createContactsViews(db);
1325            oldVersion++;
1326        }
1327
1328        if (oldVersion == 101) {
1329            createContactsTriggers(db);
1330            oldVersion++;
1331        }
1332
1333        if (oldVersion == 102) {
1334            LegacyApiSupport.createViews(db);
1335            oldVersion++;
1336        }
1337
1338        if (oldVersion == 103) {
1339            createContactEntitiesView(db);
1340            oldVersion++;
1341        }
1342
1343        if (oldVersion == 104 || oldVersion == 201) {
1344            LegacyApiSupport.createViews(db);
1345            LegacyApiSupport.createSettingsTable(db);
1346            oldVersion++;
1347        }
1348
1349        if (oldVersion == 105) {
1350            upgradeToVersion202(db);
1351            oldVersion = 202;
1352        }
1353
1354        if (oldVersion == 202) {
1355            upgradeToVersion203(db);
1356            createContactsViews(db);
1357            oldVersion++;
1358        }
1359
1360        if (oldVersion == 203) {
1361            createContactsTriggers(db);
1362            oldVersion++;
1363        }
1364
1365        if (oldVersion == 204) {
1366            upgradeToVersion205(db);
1367            createContactsViews(db);
1368            oldVersion++;
1369        }
1370
1371        if (oldVersion != newVersion) {
1372            throw new IllegalStateException(
1373                    "error upgrading the database to version " + newVersion);
1374        }
1375    }
1376
1377    private void upgradeToVersion202(SQLiteDatabase db) {
1378        db.execSQL(
1379                "ALTER TABLE " + Tables.PHONE_LOOKUP +
1380                " ADD " + PhoneLookupColumns.MIN_MATCH + " TEXT;");
1381
1382        db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" +
1383                PhoneLookupColumns.MIN_MATCH + "," +
1384                PhoneLookupColumns.RAW_CONTACT_ID + "," +
1385                PhoneLookupColumns.DATA_ID +
1386        ");");
1387
1388        updateIndexStats(db, Tables.PHONE_LOOKUP,
1389                "phone_lookup_min_match_index", "10000 2 2 1");
1390
1391        SQLiteStatement update = db.compileStatement(
1392                "UPDATE " + Tables.PHONE_LOOKUP +
1393                " SET " + PhoneLookupColumns.MIN_MATCH + "=?" +
1394                " WHERE " + PhoneLookupColumns.DATA_ID + "=?");
1395
1396        // Populate the new column
1397        Cursor c = db.query(Tables.PHONE_LOOKUP + " JOIN " + Tables.DATA +
1398                " ON (" + PhoneLookupColumns.DATA_ID + "=" + DataColumns.CONCRETE_ID + ")",
1399                new String[]{Data._ID, Phone.NUMBER}, null, null, null, null, null);
1400        try {
1401            while (c.moveToNext()) {
1402                long dataId = c.getLong(0);
1403                String number = c.getString(1);
1404                if (!TextUtils.isEmpty(number)) {
1405                    update.bindString(1, PhoneNumberUtils.toCallerIDMinMatch(number));
1406                    update.bindLong(2, dataId);
1407                    update.execute();
1408                }
1409            }
1410        } finally {
1411            c.close();
1412        }
1413    }
1414
1415    private void upgradeToVersion203(SQLiteDatabase db) {
1416        db.execSQL(
1417                "ALTER TABLE " + Tables.CONTACTS +
1418                " ADD " + Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)");
1419        db.execSQL(
1420                "ALTER TABLE " + Tables.RAW_CONTACTS +
1421                " ADD " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP
1422                        + " INTEGER NOT NULL DEFAULT 0");
1423
1424        // For each Contact, find the RawContact that contributed the display name
1425        db.execSQL(
1426                "UPDATE " + Tables.CONTACTS +
1427                " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" +
1428                        " SELECT " + RawContacts._ID +
1429                        " FROM " + Tables.RAW_CONTACTS +
1430                        " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
1431                        " AND " + RawContactsColumns.CONCRETE_DISPLAY_NAME + "=" +
1432                                Tables.CONTACTS + "." + Contacts.DISPLAY_NAME +
1433                        " ORDER BY " + RawContacts._ID +
1434                        " LIMIT 1)"
1435        );
1436
1437        db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" +
1438                Contacts.NAME_RAW_CONTACT_ID +
1439        ");");
1440
1441        // If for some unknown reason we missed some names, let's make sure there are
1442        // no contacts without a name, picking a raw contact "at random".
1443        db.execSQL(
1444                "UPDATE " + Tables.CONTACTS +
1445                " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" +
1446                        " SELECT " + RawContacts._ID +
1447                        " FROM " + Tables.RAW_CONTACTS +
1448                        " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
1449                        " ORDER BY " + RawContacts._ID +
1450                        " LIMIT 1)" +
1451                " WHERE " + Contacts.NAME_RAW_CONTACT_ID + " IS NULL"
1452        );
1453
1454        // Wipe out DISPLAY_NAME on the Contacts table as it is no longer in use.
1455        db.execSQL(
1456                "UPDATE " + Tables.CONTACTS +
1457                " SET " + Contacts.DISPLAY_NAME + "=NULL"
1458        );
1459
1460        // Copy the IN_VISIBLE_GROUP flag down to all raw contacts to allow
1461        // indexing on (display_name, in_visible_group)
1462        db.execSQL(
1463                "UPDATE " + Tables.RAW_CONTACTS +
1464                " SET " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "=(" +
1465                        "SELECT " + Contacts.IN_VISIBLE_GROUP +
1466                        " FROM " + Tables.CONTACTS +
1467                        " WHERE " + Contacts._ID + "=" + RawContacts.CONTACT_ID + ")"
1468        );
1469
1470        db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
1471                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1472                RawContactsColumns.DISPLAY_NAME + " COLLATE LOCALIZED ASC" +
1473        ");");
1474
1475        db.execSQL("DROP INDEX contacts_visible_index");
1476        db.execSQL("CREATE INDEX contacts_visible_index ON " + Tables.CONTACTS + " (" +
1477                Contacts.IN_VISIBLE_GROUP +
1478        ");");
1479    }
1480
1481    private void upgradeToVersion205(SQLiteDatabase db) {
1482        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1483                + " ADD " + RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT;");
1484        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1485                + " ADD " + RawContacts.PHONETIC_NAME + " TEXT;");
1486        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1487                + " ADD " + RawContacts.PHONETIC_NAME_STYLE + " INTEGER;");
1488        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1489                + " ADD " + RawContacts.SORT_KEY_PRIMARY + " TEXT COLLATE LOCALIZED;");
1490        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1491                + " ADD " + RawContacts.SORT_KEY_ALTERNATIVE + " TEXT COLLATE LOCALIZED;");
1492
1493        final Locale locale = Locale.getDefault();
1494
1495        NameSplitter splitter = new NameSplitter(
1496                mContext.getString(com.android.internal.R.string.common_name_prefixes),
1497                mContext.getString(com.android.internal.R.string.common_last_name_prefixes),
1498                mContext.getString(com.android.internal.R.string.common_name_suffixes),
1499                mContext.getString(com.android.internal.R.string.common_name_conjunctions),
1500                locale);
1501
1502        SQLiteStatement rawContactUpdate = db.compileStatement(
1503                "UPDATE " + Tables.RAW_CONTACTS +
1504                " SET " +
1505                        RawContacts.DISPLAY_NAME_PRIMARY + "=?," +
1506                        RawContacts.DISPLAY_NAME_ALTERNATIVE + "=?," +
1507                        RawContacts.PHONETIC_NAME + "=?," +
1508                        RawContacts.PHONETIC_NAME_STYLE + "=?," +
1509                        RawContacts.SORT_KEY_PRIMARY + "=?," +
1510                        RawContacts.SORT_KEY_ALTERNATIVE + "=?" +
1511                " WHERE " + RawContacts._ID + "=?");
1512
1513        upgradeStructuredNamesToVersion205(db, rawContactUpdate, splitter);
1514        upgradeOrganizationsToVersion205(db, rawContactUpdate, splitter);
1515
1516        db.execSQL("DROP INDEX raw_contact_sort_key1_index");
1517        db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
1518                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1519                RawContacts.SORT_KEY_PRIMARY +
1520        ");");
1521
1522        db.execSQL("CREATE INDEX raw_contact_sort_key2_index ON " + Tables.RAW_CONTACTS + " (" +
1523                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1524                RawContacts.SORT_KEY_ALTERNATIVE +
1525        ");");
1526    }
1527
1528    private interface StructName205Query {
1529        String TABLE = Tables.DATA_JOIN_RAW_CONTACTS;
1530
1531        String COLUMNS[] = {
1532                DataColumns.CONCRETE_ID,
1533                Data.RAW_CONTACT_ID,
1534                RawContacts.DISPLAY_NAME_SOURCE,
1535                RawContacts.DISPLAY_NAME_PRIMARY,
1536                StructuredName.PREFIX,
1537                StructuredName.GIVEN_NAME,
1538                StructuredName.MIDDLE_NAME,
1539                StructuredName.FAMILY_NAME,
1540                StructuredName.SUFFIX,
1541                StructuredName.PHONETIC_FAMILY_NAME,
1542                StructuredName.PHONETIC_MIDDLE_NAME,
1543                StructuredName.PHONETIC_GIVEN_NAME,
1544        };
1545
1546        int ID = 0;
1547        int RAW_CONTACT_ID = 1;
1548        int DISPLAY_NAME_SOURCE = 2;
1549        int DISPLAY_NAME = 3;
1550        int PREFIX = 4;
1551        int GIVEN_NAME = 5;
1552        int MIDDLE_NAME = 6;
1553        int FAMILY_NAME = 7;
1554        int SUFFIX = 8;
1555        int PHONETIC_FAMILY_NAME = 9;
1556        int PHONETIC_MIDDLE_NAME = 10;
1557        int PHONETIC_GIVEN_NAME = 11;
1558    }
1559
1560    private void upgradeStructuredNamesToVersion205(SQLiteDatabase db,
1561            SQLiteStatement rawContactUpdate, NameSplitter splitter) {
1562
1563        // Process structured names to detect the style of the full name and phonetic name
1564
1565        long mMimeType;
1566        try {
1567            mMimeType = DatabaseUtils.longForQuery(db,
1568                    "SELECT " + MimetypesColumns._ID +
1569                    " FROM " + Tables.MIMETYPES +
1570                    " WHERE " + MimetypesColumns.MIMETYPE
1571                            + "='" + StructuredName.CONTENT_ITEM_TYPE + "'", null);
1572        } catch (SQLiteDoneException e) {
1573            // No structured names in the database
1574            return;
1575        }
1576
1577        SQLiteStatement structuredNameUpdate = db.compileStatement(
1578                "UPDATE " + Tables.DATA +
1579                " SET " +
1580                        StructuredName.FULL_NAME_STYLE + "=?," +
1581                        StructuredName.DISPLAY_NAME + "=?," +
1582                        StructuredName.PHONETIC_NAME_STYLE + "=?" +
1583                " WHERE " + Data._ID + "=?");
1584
1585        NameSplitter.Name name = new NameSplitter.Name();
1586        StringBuilder sb = new StringBuilder();
1587        Cursor cursor = db.query(StructName205Query.TABLE,
1588                StructName205Query.COLUMNS,
1589                DataColumns.MIMETYPE_ID + "=" + mMimeType, null, null, null, null);
1590        try {
1591            while (cursor.moveToNext()) {
1592                long dataId = cursor.getLong(StructName205Query.ID);
1593                long rawContactId = cursor.getLong(StructName205Query.RAW_CONTACT_ID);
1594                int displayNameSource = cursor.getInt(StructName205Query.DISPLAY_NAME_SOURCE);
1595                String displayName = cursor.getString(StructName205Query.DISPLAY_NAME);
1596
1597                name.clear();
1598                name.prefix = cursor.getString(StructName205Query.PREFIX);
1599                name.givenNames = cursor.getString(StructName205Query.GIVEN_NAME);
1600                name.middleName = cursor.getString(StructName205Query.MIDDLE_NAME);
1601                name.familyName = cursor.getString(StructName205Query.FAMILY_NAME);
1602                name.suffix = cursor.getString(StructName205Query.SUFFIX);
1603                name.phoneticFamilyName = cursor.getString(StructName205Query.PHONETIC_FAMILY_NAME);
1604                name.phoneticMiddleName = cursor.getString(StructName205Query.PHONETIC_MIDDLE_NAME);
1605                name.phoneticGivenName = cursor.getString(StructName205Query.PHONETIC_GIVEN_NAME);
1606
1607                upgradeNameToVersion205(dataId, rawContactId, displayNameSource, displayName, name,
1608                        structuredNameUpdate, rawContactUpdate, splitter, sb);
1609            }
1610        } finally {
1611            cursor.close();
1612        }
1613    }
1614
1615    private void upgradeNameToVersion205(long dataId, long rawContactId, int displayNameSource,
1616            String currentDisplayName, NameSplitter.Name name,
1617            SQLiteStatement structuredNameUpdate, SQLiteStatement rawContactUpdate,
1618            NameSplitter splitter, StringBuilder sb) {
1619
1620        splitter.guessNameStyle(name);
1621        name.fullNameStyle = splitter.getAdjustedFullNameStyle(name.fullNameStyle);
1622        String displayName = splitter.join(name, true);
1623
1624        structuredNameUpdate.bindLong(1, name.fullNameStyle);
1625        DatabaseUtils.bindObjectToProgram(structuredNameUpdate, 2, displayName);
1626        structuredNameUpdate.bindLong(3, name.phoneticNameStyle);
1627        structuredNameUpdate.bindLong(4, dataId);
1628        structuredNameUpdate.execute();
1629
1630        if (displayNameSource == DisplayNameSources.STRUCTURED_NAME) {
1631            String displayNameAlternative = splitter.join(name, false);
1632            String phoneticName = splitter.joinPhoneticName(name);
1633            String sortKey = null;
1634            String sortKeyAlternative = null;
1635
1636            if (phoneticName != null) {
1637                sortKey = sortKeyAlternative = phoneticName;
1638            } else if (name.fullNameStyle == FullNameStyle.CHINESE) {
1639                sortKey = sortKeyAlternative = splitter.convertHanziToPinyin(displayName);
1640            }
1641
1642            if (sortKey == null) {
1643                sortKey = displayName;
1644                sortKeyAlternative = displayNameAlternative;
1645            }
1646
1647            updateRawContact205(rawContactUpdate, rawContactId, displayName,
1648                    displayNameAlternative, name.phoneticNameStyle, phoneticName, sortKey,
1649                    sortKeyAlternative);
1650        }
1651    }
1652
1653    private interface Organization205Query {
1654        String TABLE = Tables.DATA_JOIN_RAW_CONTACTS;
1655
1656        String COLUMNS[] = {
1657                DataColumns.CONCRETE_ID,
1658                Data.RAW_CONTACT_ID,
1659                Organization.COMPANY,
1660                Organization.PHONETIC_NAME,
1661        };
1662
1663        int ID = 0;
1664        int RAW_CONTACT_ID = 1;
1665        int COMPANY = 2;
1666        int PHONETIC_NAME = 3;
1667    }
1668
1669    private void upgradeOrganizationsToVersion205(SQLiteDatabase db,
1670            SQLiteStatement rawContactUpdate, NameSplitter splitter) {
1671
1672        final long mMimeType;
1673        try {
1674            mMimeType = DatabaseUtils.longForQuery(db,
1675                    "SELECT " + MimetypesColumns._ID +
1676                    " FROM " + Tables.MIMETYPES +
1677                    " WHERE " + MimetypesColumns.MIMETYPE
1678                            + "='" + Organization.CONTENT_ITEM_TYPE + "'", null);
1679        } catch (SQLiteDoneException e) {
1680            // No organizations in the database
1681            return;
1682        }
1683
1684        SQLiteStatement organizationUpdate = db.compileStatement(
1685                "UPDATE " + Tables.DATA +
1686                " SET " +
1687                        Organization.PHONETIC_NAME_STYLE + "=?" +
1688                " WHERE " + Data._ID + "=?");
1689
1690        Cursor cursor = db.query(Organization205Query.TABLE, Organization205Query.COLUMNS,
1691                DataColumns.MIMETYPE_ID + "=" + mMimeType + " AND "
1692                        + RawContacts.DISPLAY_NAME_SOURCE + "=" + DisplayNameSources.ORGANIZATION,
1693                null, null, null, null);
1694        try {
1695            while (cursor.moveToNext()) {
1696                long dataId = cursor.getLong(Organization205Query.ID);
1697                long rawContactId = cursor.getLong(Organization205Query.RAW_CONTACT_ID);
1698                String company = cursor.getString(Organization205Query.COMPANY);
1699                String phoneticName = cursor.getString(Organization205Query.PHONETIC_NAME);
1700
1701                int phoneticNameStyle = splitter.guessPhoneticNameStyle(phoneticName);
1702
1703                organizationUpdate.bindLong(1, phoneticNameStyle);
1704                organizationUpdate.bindLong(2, dataId);
1705                organizationUpdate.execute();
1706
1707                String sortKey = null;
1708                if (phoneticName == null && company != null) {
1709                    int nameStyle = splitter.guessFullNameStyle(company);
1710                    nameStyle = splitter.getAdjustedFullNameStyle(nameStyle);
1711                    if (nameStyle == FullNameStyle.CHINESE) {
1712                        sortKey = splitter.convertHanziToPinyin(company);
1713                    }
1714                }
1715
1716                if (sortKey == null) {
1717                    sortKey = company;
1718                }
1719
1720                updateRawContact205(rawContactUpdate, rawContactId, company,
1721                        company, phoneticNameStyle, phoneticName, sortKey, sortKey);
1722            }
1723        } finally {
1724            cursor.close();
1725        }
1726    }
1727
1728    private void updateRawContact205(SQLiteStatement rawContactUpdate, long rawContactId,
1729            String displayName, String displayNameAlternative, int phoneticNameStyle,
1730            String phoneticName, String sortKeyPrimary, String sortKeyAlternative) {
1731        bindString(rawContactUpdate, 1, displayName);
1732        bindString(rawContactUpdate, 2, displayNameAlternative);
1733        bindString(rawContactUpdate, 3, phoneticName);
1734        rawContactUpdate.bindLong(4, phoneticNameStyle);
1735        bindString(rawContactUpdate, 5, sortKeyPrimary);
1736        bindString(rawContactUpdate, 6, sortKeyAlternative);
1737        rawContactUpdate.bindLong(7, rawContactId);
1738        rawContactUpdate.execute();
1739    }
1740
1741    private void bindString(SQLiteStatement stmt, int index, String value) {
1742        if (value == null) {
1743            stmt.bindNull(index);
1744        } else {
1745            stmt.bindString(index, value);
1746        }
1747    }
1748
1749    /**
1750     * Adds index stats into the SQLite database to force it to always use the lookup indexes.
1751     */
1752    private void updateSqliteStats(SQLiteDatabase db) {
1753
1754        // Specific stats strings are based on an actual large database after running ANALYZE
1755        try {
1756            updateIndexStats(db, Tables.CONTACTS,
1757                    "contacts_restricted_index", "10000 9000");
1758            updateIndexStats(db, Tables.CONTACTS,
1759                    "contacts_has_phone_index", "10000 500");
1760            updateIndexStats(db, Tables.CONTACTS,
1761                    "contacts_visible_index", "10000 500 1");
1762
1763            updateIndexStats(db, Tables.RAW_CONTACTS,
1764                    "raw_contacts_source_id_index", "10000 1 1 1");
1765            updateIndexStats(db, Tables.RAW_CONTACTS,
1766                    "raw_contacts_contact_id_index", "10000 2");
1767
1768            updateIndexStats(db, Tables.NAME_LOOKUP,
1769                    "name_lookup_raw_contact_id_index", "10000 3");
1770            updateIndexStats(db, Tables.NAME_LOOKUP,
1771                    "name_lookup_index", "10000 3 2 2");
1772            updateIndexStats(db, Tables.NAME_LOOKUP,
1773                    "sqlite_autoindex_name_lookup_1", "10000 3 2 1");
1774
1775            updateIndexStats(db, Tables.PHONE_LOOKUP,
1776                    "phone_lookup_index", "10000 2 2 1");
1777            updateIndexStats(db, Tables.PHONE_LOOKUP,
1778                    "phone_lookup_min_match_index", "10000 2 2 1");
1779
1780            updateIndexStats(db, Tables.DATA,
1781                    "data_mimetype_data1_index", "60000 5000 2");
1782            updateIndexStats(db, Tables.DATA,
1783                    "data_raw_contact_id", "60000 10");
1784
1785            updateIndexStats(db, Tables.GROUPS,
1786                    "groups_source_id_index", "50 1 1 1");
1787
1788            updateIndexStats(db, Tables.NICKNAME_LOOKUP,
1789                    "sqlite_autoindex_name_lookup_1", "500 2 1");
1790
1791        } catch (SQLException e) {
1792            Log.e(TAG, "Could not update index stats", e);
1793        }
1794    }
1795
1796    /**
1797     * Stores statistics for a given index.
1798     *
1799     * @param stats has the following structure: the first index is the expected size of
1800     * the table.  The following integer(s) are the expected number of records selected with the
1801     * index.  There should be one integer per indexed column.
1802     */
1803    private void updateIndexStats(SQLiteDatabase db, String table, String index,
1804            String stats) {
1805        db.execSQL("DELETE FROM sqlite_stat1 WHERE tbl='" + table + "' AND idx='" + index + "';");
1806        db.execSQL("INSERT INTO sqlite_stat1 (tbl,idx,stat)"
1807                + " VALUES ('" + table + "','" + index + "','" + stats + "');");
1808    }
1809
1810    @Override
1811    public synchronized SQLiteDatabase getWritableDatabase() {
1812        SQLiteDatabase db = super.getWritableDatabase();
1813        if (mReopenDatabase) {
1814            mReopenDatabase = false;
1815            close();
1816            db = super.getWritableDatabase();
1817        }
1818        // let {@link SQLiteDatabase} cache my compiled-sql statements.
1819        db.setMaxSqlCacheSize(MAX_CACHE_SIZE_FOR_CONTACTS_DB);
1820        return db;
1821    }
1822
1823    /**
1824     * Wipes all data except mime type and package lookup tables.
1825     */
1826    public void wipeData() {
1827        SQLiteDatabase db = getWritableDatabase();
1828
1829        db.execSQL("DELETE FROM " + Tables.CONTACTS + ";");
1830        db.execSQL("DELETE FROM " + Tables.RAW_CONTACTS + ";");
1831        db.execSQL("DELETE FROM " + Tables.DATA + ";");
1832        db.execSQL("DELETE FROM " + Tables.PHONE_LOOKUP + ";");
1833        db.execSQL("DELETE FROM " + Tables.NAME_LOOKUP + ";");
1834        db.execSQL("DELETE FROM " + Tables.GROUPS + ";");
1835        db.execSQL("DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS + ";");
1836        db.execSQL("DELETE FROM " + Tables.SETTINGS + ";");
1837        db.execSQL("DELETE FROM " + Tables.ACTIVITIES + ";");
1838        db.execSQL("DELETE FROM " + Tables.CALLS + ";");
1839
1840        // Note: we are not removing reference data from Tables.NICKNAME_LOOKUP
1841    }
1842
1843    /**
1844     * Return the {@link ApplicationInfo#uid} for the given package name.
1845     */
1846    public static int getUidForPackageName(PackageManager pm, String packageName) {
1847        try {
1848            ApplicationInfo clientInfo = pm.getApplicationInfo(packageName, 0 /* no flags */);
1849            return clientInfo.uid;
1850        } catch (NameNotFoundException e) {
1851            throw new RuntimeException(e);
1852        }
1853    }
1854
1855    /**
1856     * Perform an internal string-to-integer lookup using the compiled
1857     * {@link SQLiteStatement} provided, using the in-memory cache to speed up
1858     * lookups. If a mapping isn't found in cache or database, it will be
1859     * created. All new, uncached answers are added to the cache automatically.
1860     *
1861     * @param query Compiled statement used to query for the mapping.
1862     * @param insert Compiled statement used to insert a new mapping when no
1863     *            existing one is found in cache or from query.
1864     * @param value Value to find mapping for.
1865     * @param cache In-memory cache of previous answers.
1866     * @return An unique integer mapping for the given value.
1867     */
1868    private synchronized long getCachedId(SQLiteStatement query, SQLiteStatement insert,
1869            String value, HashMap<String, Long> cache) {
1870        // Try an in-memory cache lookup
1871        if (cache.containsKey(value)) {
1872            return cache.get(value);
1873        }
1874
1875        long id = -1;
1876        try {
1877            // Try searching database for mapping
1878            DatabaseUtils.bindObjectToProgram(query, 1, value);
1879            id = query.simpleQueryForLong();
1880        } catch (SQLiteDoneException e) {
1881            // Nothing found, so try inserting new mapping
1882            DatabaseUtils.bindObjectToProgram(insert, 1, value);
1883            id = insert.executeInsert();
1884        }
1885
1886        if (id != -1) {
1887            // Cache and return the new answer
1888            cache.put(value, id);
1889            return id;
1890        } else {
1891            // Otherwise throw if no mapping found or created
1892            throw new IllegalStateException("Couldn't find or create internal "
1893                    + "lookup table entry for value " + value);
1894        }
1895    }
1896
1897    /**
1898     * Convert a package name into an integer, using {@link Tables#PACKAGES} for
1899     * lookups and possible allocation of new IDs as needed.
1900     */
1901    public long getPackageId(String packageName) {
1902        // Make sure compiled statements are ready by opening database
1903        getReadableDatabase();
1904        return getCachedId(mPackageQuery, mPackageInsert, packageName, mPackageCache);
1905    }
1906
1907    /**
1908     * Convert a mimetype into an integer, using {@link Tables#MIMETYPES} for
1909     * lookups and possible allocation of new IDs as needed.
1910     */
1911    public long getMimeTypeId(String mimetype) {
1912        // Make sure compiled statements are ready by opening database
1913        getReadableDatabase();
1914        return getMimeTypeIdNoDbCheck(mimetype);
1915    }
1916
1917    private long getMimeTypeIdNoDbCheck(String mimetype) {
1918        return getCachedId(mMimetypeQuery, mMimetypeInsert, mimetype, mMimetypeCache);
1919    }
1920
1921    /**
1922     * Find the mimetype for the given {@link Data#_ID}.
1923     */
1924    public String getDataMimeType(long dataId) {
1925        // Make sure compiled statements are ready by opening database
1926        getReadableDatabase();
1927        try {
1928            // Try database query to find mimetype
1929            DatabaseUtils.bindObjectToProgram(mDataMimetypeQuery, 1, dataId);
1930            String mimetype = mDataMimetypeQuery.simpleQueryForString();
1931            return mimetype;
1932        } catch (SQLiteDoneException e) {
1933            // No valid mapping found, so return null
1934            return null;
1935        }
1936    }
1937
1938    /**
1939     * Find the mime-type for the given {@link Activities#_ID}.
1940     */
1941    public String getActivityMimeType(long activityId) {
1942        // Make sure compiled statements are ready by opening database
1943        getReadableDatabase();
1944        try {
1945            // Try database query to find mimetype
1946            DatabaseUtils.bindObjectToProgram(mActivitiesMimetypeQuery, 1, activityId);
1947            String mimetype = mActivitiesMimetypeQuery.simpleQueryForString();
1948            return mimetype;
1949        } catch (SQLiteDoneException e) {
1950            // No valid mapping found, so return null
1951            return null;
1952        }
1953    }
1954
1955    /**
1956     * Update {@link Contacts#IN_VISIBLE_GROUP} for all contacts.
1957     */
1958    public void updateAllVisible() {
1959        final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
1960        mVisibleUpdate.bindLong(1, groupMembershipMimetypeId);
1961        mVisibleUpdate.execute();
1962        mVisibleUpdateRawContacts.execute();
1963    }
1964
1965    /**
1966     * Update {@link Contacts#IN_VISIBLE_GROUP} for a specific contact.
1967     */
1968    public void updateContactVisible(long contactId) {
1969        final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
1970        mVisibleSpecificUpdate.bindLong(1, groupMembershipMimetypeId);
1971        mVisibleSpecificUpdate.bindLong(2, contactId);
1972        mVisibleSpecificUpdate.execute();
1973
1974        mVisibleSpecificUpdateRawContacts.bindLong(1, contactId);
1975        mVisibleSpecificUpdateRawContacts.execute();
1976    }
1977
1978    /**
1979     * Returns contact ID for the given contact or zero if it is NULL.
1980     */
1981    public long getContactId(long rawContactId) {
1982        getReadableDatabase();
1983        try {
1984            DatabaseUtils.bindObjectToProgram(mContactIdQuery, 1, rawContactId);
1985            return mContactIdQuery.simpleQueryForLong();
1986        } catch (SQLiteDoneException e) {
1987            // No valid mapping found, so return 0
1988            return 0;
1989        }
1990    }
1991
1992    public int getAggregationMode(long rawContactId) {
1993        getReadableDatabase();
1994        try {
1995            DatabaseUtils.bindObjectToProgram(mAggregationModeQuery, 1, rawContactId);
1996            return (int)mAggregationModeQuery.simpleQueryForLong();
1997        } catch (SQLiteDoneException e) {
1998            // No valid row found, so return "disabled"
1999            return RawContacts.AGGREGATION_MODE_DISABLED;
2000        }
2001    }
2002
2003    public void buildPhoneLookupAndRawContactQuery(SQLiteQueryBuilder qb, String number) {
2004        String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2005        qb.setTables(Tables.DATA_JOIN_RAW_CONTACTS +
2006                " JOIN " + Tables.PHONE_LOOKUP
2007                + " ON(" + DataColumns.CONCRETE_ID + "=" + PhoneLookupColumns.DATA_ID + ")");
2008
2009        StringBuilder sb = new StringBuilder();
2010        sb.append(PhoneLookupColumns.MIN_MATCH + "='");
2011        sb.append(minMatch);
2012        sb.append("' AND PHONE_NUMBERS_EQUAL(data." + Phone.NUMBER + ", ");
2013        DatabaseUtils.appendEscapedSQLString(sb, number);
2014        sb.append(mUseStrictPhoneNumberComparison ? ", 1)" : ", 0)");
2015
2016        qb.appendWhere(sb.toString());
2017    }
2018
2019    public void buildPhoneLookupAndContactQuery(SQLiteQueryBuilder qb, String number) {
2020        String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2021        StringBuilder sb = new StringBuilder();
2022        appendPhoneLookupTables(sb, minMatch, true);
2023        qb.setTables(sb.toString());
2024
2025        sb = new StringBuilder();
2026        appendPhoneLookupSelection(sb, number);
2027        qb.appendWhere(sb.toString());
2028    }
2029
2030    public String buildPhoneLookupAsNestedQuery(String number) {
2031        StringBuilder sb = new StringBuilder();
2032        final String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2033        sb.append("(SELECT DISTINCT raw_contact_id" + " FROM ");
2034        appendPhoneLookupTables(sb, minMatch, false);
2035        sb.append(" WHERE ");
2036        appendPhoneLookupSelection(sb, number);
2037        sb.append(")");
2038        return sb.toString();
2039    }
2040
2041    private void appendPhoneLookupTables(StringBuilder sb, final String minMatch,
2042            boolean joinContacts) {
2043        sb.append(Tables.RAW_CONTACTS);
2044        if (joinContacts) {
2045            sb.append(" JOIN " + getContactView() + " contacts_view"
2046                    + " ON (contacts_view._id = raw_contacts.contact_id)");
2047        }
2048        sb.append(", (SELECT data_id FROM phone_lookup "
2049                + "WHERE (" + Tables.PHONE_LOOKUP + "." + PhoneLookupColumns.MIN_MATCH + " = '");
2050        sb.append(minMatch);
2051        sb.append("')) AS lookup, " + Tables.DATA);
2052    }
2053
2054    private void appendPhoneLookupSelection(StringBuilder sb, String number) {
2055        sb.append("lookup.data_id=data._id AND data.raw_contact_id=raw_contacts._id"
2056                + " AND PHONE_NUMBERS_EQUAL(data." + Phone.NUMBER + ", ");
2057        DatabaseUtils.appendEscapedSQLString(sb, number);
2058        sb.append(mUseStrictPhoneNumberComparison ? ", 1)" : ", 0)");
2059    }
2060
2061    public String getUseStrictPhoneNumberComparisonParameter() {
2062        return mUseStrictPhoneNumberComparison ? "1" : "0";
2063    }
2064
2065    /**
2066     * Loads common nickname mappings into the database.
2067     */
2068    private void loadNicknameLookupTable(SQLiteDatabase db) {
2069        String[] strings = mContext.getResources().getStringArray(
2070                com.android.internal.R.array.common_nicknames);
2071        if (strings == null || strings.length == 0) {
2072            return;
2073        }
2074
2075        SQLiteStatement nicknameLookupInsert = db.compileStatement("INSERT INTO "
2076                + Tables.NICKNAME_LOOKUP + "(" + NicknameLookupColumns.NAME + ","
2077                + NicknameLookupColumns.CLUSTER + ") VALUES (?,?)");
2078
2079        for (int clusterId = 0; clusterId < strings.length; clusterId++) {
2080            String[] names = strings[clusterId].split(",");
2081            for (int j = 0; j < names.length; j++) {
2082                String name = NameNormalizer.normalize(names[j]);
2083                try {
2084                    DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 1, name);
2085                    DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 2,
2086                            String.valueOf(clusterId));
2087                    nicknameLookupInsert.executeInsert();
2088                } catch (SQLiteException e) {
2089
2090                    // Print the exception and keep going - this is not a fatal error
2091                    Log.e(TAG, "Cannot insert nickname: " + names[j], e);
2092                }
2093            }
2094        }
2095    }
2096
2097    public static void copyStringValue(ContentValues toValues, String toKey,
2098            ContentValues fromValues, String fromKey) {
2099        if (fromValues.containsKey(fromKey)) {
2100            toValues.put(toKey, fromValues.getAsString(fromKey));
2101        }
2102    }
2103
2104    public static void copyLongValue(ContentValues toValues, String toKey,
2105            ContentValues fromValues, String fromKey) {
2106        if (fromValues.containsKey(fromKey)) {
2107            long longValue;
2108            Object value = fromValues.get(fromKey);
2109            if (value instanceof Boolean) {
2110                if ((Boolean)value) {
2111                    longValue = 1;
2112                } else {
2113                    longValue = 0;
2114                }
2115            } else if (value instanceof String) {
2116                longValue = Long.parseLong((String)value);
2117            } else {
2118                longValue = ((Number)value).longValue();
2119            }
2120            toValues.put(toKey, longValue);
2121        }
2122    }
2123
2124    public SyncStateContentProviderHelper getSyncState() {
2125        return mSyncState;
2126    }
2127
2128    /**
2129     * Delete the aggregate contact if it has no constituent raw contacts other
2130     * than the supplied one.
2131     */
2132    public void removeContactIfSingleton(long rawContactId) {
2133        SQLiteDatabase db = getWritableDatabase();
2134
2135        // Obtain contact ID from the supplied raw contact ID
2136        String contactIdFromRawContactId = "(SELECT " + RawContacts.CONTACT_ID + " FROM "
2137                + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=" + rawContactId + ")";
2138
2139        // Find other raw contacts in the same aggregate contact
2140        String otherRawContacts = "(SELECT contacts1." + RawContacts._ID + " FROM "
2141                + Tables.RAW_CONTACTS + " contacts1 JOIN " + Tables.RAW_CONTACTS + " contacts2 ON ("
2142                + "contacts1." + RawContacts.CONTACT_ID + "=contacts2." + RawContacts.CONTACT_ID
2143                + ") WHERE contacts1." + RawContacts._ID + "!=" + rawContactId + ""
2144                + " AND contacts2." + RawContacts._ID + "=" + rawContactId + ")";
2145
2146        db.execSQL("DELETE FROM " + Tables.CONTACTS
2147                + " WHERE " + Contacts._ID + "=" + contactIdFromRawContactId
2148                + " AND NOT EXISTS " + otherRawContacts + ";");
2149    }
2150
2151    /**
2152     * Check if {@link Binder#getCallingUid()} should be allowed access to
2153     * {@link RawContacts#IS_RESTRICTED} data.
2154     */
2155    boolean hasAccessToRestrictedData() {
2156        final PackageManager pm = mContext.getPackageManager();
2157        final String[] callerPackages = pm.getPackagesForUid(Binder.getCallingUid());
2158
2159        // Has restricted access if caller matches any packages
2160        for (String callerPackage : callerPackages) {
2161            if (hasAccessToRestrictedData(callerPackage)) {
2162                return true;
2163            }
2164        }
2165        return false;
2166    }
2167
2168    /**
2169     * Check if requestingPackage should be allowed access to
2170     * {@link RawContacts#IS_RESTRICTED} data.
2171     */
2172    boolean hasAccessToRestrictedData(String requestingPackage) {
2173        if (mUnrestrictedPackages != null) {
2174            for (String allowedPackage : mUnrestrictedPackages) {
2175                if (allowedPackage.equals(requestingPackage)) {
2176                    return true;
2177                }
2178            }
2179        }
2180        return false;
2181    }
2182
2183    public String getDataView() {
2184        return getDataView(false);
2185    }
2186
2187    public String getDataView(boolean requireRestrictedView) {
2188        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2189                Views.DATA_ALL : Views.DATA_RESTRICTED;
2190    }
2191
2192    public String getRawContactView() {
2193        return getRawContactView(false);
2194    }
2195
2196    public String getRawContactView(boolean requireRestrictedView) {
2197        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2198                Views.RAW_CONTACTS_ALL : Views.RAW_CONTACTS_RESTRICTED;
2199    }
2200
2201    public String getContactView() {
2202        return getContactView(false);
2203    }
2204
2205    public String getContactView(boolean requireRestrictedView) {
2206        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2207                Views.CONTACTS_ALL : Views.CONTACTS_RESTRICTED;
2208    }
2209
2210    public String getGroupView() {
2211        return Views.GROUPS_ALL;
2212    }
2213
2214    public String getContactEntitiesView() {
2215        return getContactEntitiesView(false);
2216    }
2217
2218    public String getContactEntitiesView(boolean requireRestrictedView) {
2219        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2220                Tables.CONTACT_ENTITIES : Tables.CONTACT_ENTITIES_RESTRICTED;
2221    }
2222
2223    /**
2224     * Test if any of the columns appear in the given projection.
2225     */
2226    public boolean isInProjection(String[] projection, String... columns) {
2227        if (projection == null) {
2228            return true;
2229        }
2230
2231        // Optimized for a single-column test
2232        if (columns.length == 1) {
2233            String column = columns[0];
2234            for (String test : projection) {
2235                if (column.equals(test)) {
2236                    return true;
2237                }
2238            }
2239        } else {
2240            for (String test : projection) {
2241                for (String column : columns) {
2242                    if (column.equals(test)) {
2243                        return true;
2244                    }
2245                }
2246            }
2247        }
2248        return false;
2249    }
2250}
2251