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