ContactsDatabaseHelper.java revision 5086b63bf3de5f26f495b640e85259c0ebf5ca47
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 = 305;
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 (upgradeViewsAndTriggers) {
1444            createContactsViews(db);
1445            createGroupsView(db);
1446            createContactEntitiesView(db);
1447            createContactsTriggers(db);
1448            createContactsIndexes(db);
1449            LegacyApiSupport.createViews(db);
1450            updateSqliteStats(db);
1451            mReopenDatabase = true;
1452        }
1453
1454        if (upgradeNameLookup) {
1455            rebuildNameLookup(db);
1456        }
1457
1458        if (oldVersion != newVersion) {
1459            throw new IllegalStateException(
1460                    "error upgrading the database to version " + newVersion);
1461        }
1462    }
1463
1464    private void upgradeToVersion304(SQLiteDatabase db) {
1465        // Mimetype table requires an index on mime type
1466        db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS mime_type ON " + Tables.MIMETYPES + " (" +
1467                MimetypesColumns.MIMETYPE +
1468        ");");
1469    }
1470
1471    private void upgradeToVersion202(SQLiteDatabase db) {
1472        db.execSQL(
1473                "ALTER TABLE " + Tables.PHONE_LOOKUP +
1474                " ADD " + PhoneLookupColumns.MIN_MATCH + " TEXT;");
1475
1476        db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" +
1477                PhoneLookupColumns.MIN_MATCH + "," +
1478                PhoneLookupColumns.RAW_CONTACT_ID + "," +
1479                PhoneLookupColumns.DATA_ID +
1480        ");");
1481
1482        updateIndexStats(db, Tables.PHONE_LOOKUP,
1483                "phone_lookup_min_match_index", "10000 2 2 1");
1484
1485        SQLiteStatement update = db.compileStatement(
1486                "UPDATE " + Tables.PHONE_LOOKUP +
1487                " SET " + PhoneLookupColumns.MIN_MATCH + "=?" +
1488                " WHERE " + PhoneLookupColumns.DATA_ID + "=?");
1489
1490        // Populate the new column
1491        Cursor c = db.query(Tables.PHONE_LOOKUP + " JOIN " + Tables.DATA +
1492                " ON (" + PhoneLookupColumns.DATA_ID + "=" + DataColumns.CONCRETE_ID + ")",
1493                new String[]{Data._ID, Phone.NUMBER}, null, null, null, null, null);
1494        try {
1495            while (c.moveToNext()) {
1496                long dataId = c.getLong(0);
1497                String number = c.getString(1);
1498                if (!TextUtils.isEmpty(number)) {
1499                    update.bindString(1, PhoneNumberUtils.toCallerIDMinMatch(number));
1500                    update.bindLong(2, dataId);
1501                    update.execute();
1502                }
1503            }
1504        } finally {
1505            c.close();
1506        }
1507    }
1508
1509    private void upgradeToVersion203(SQLiteDatabase db) {
1510        db.execSQL(
1511                "ALTER TABLE " + Tables.CONTACTS +
1512                " ADD " + Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)");
1513        db.execSQL(
1514                "ALTER TABLE " + Tables.RAW_CONTACTS +
1515                " ADD " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP
1516                        + " INTEGER NOT NULL DEFAULT 0");
1517
1518        // For each Contact, find the RawContact that contributed the display name
1519        db.execSQL(
1520                "UPDATE " + Tables.CONTACTS +
1521                " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" +
1522                        " SELECT " + RawContacts._ID +
1523                        " FROM " + Tables.RAW_CONTACTS +
1524                        " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
1525                        " AND " + RawContactsColumns.CONCRETE_DISPLAY_NAME + "=" +
1526                                Tables.CONTACTS + "." + Contacts.DISPLAY_NAME +
1527                        " ORDER BY " + RawContacts._ID +
1528                        " LIMIT 1)"
1529        );
1530
1531        db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" +
1532                Contacts.NAME_RAW_CONTACT_ID +
1533        ");");
1534
1535        // If for some unknown reason we missed some names, let's make sure there are
1536        // no contacts without a name, picking a raw contact "at random".
1537        db.execSQL(
1538                "UPDATE " + Tables.CONTACTS +
1539                " SET " + Contacts.NAME_RAW_CONTACT_ID + "=(" +
1540                        " SELECT " + RawContacts._ID +
1541                        " FROM " + Tables.RAW_CONTACTS +
1542                        " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
1543                        " ORDER BY " + RawContacts._ID +
1544                        " LIMIT 1)" +
1545                " WHERE " + Contacts.NAME_RAW_CONTACT_ID + " IS NULL"
1546        );
1547
1548        // Wipe out DISPLAY_NAME on the Contacts table as it is no longer in use.
1549        db.execSQL(
1550                "UPDATE " + Tables.CONTACTS +
1551                " SET " + Contacts.DISPLAY_NAME + "=NULL"
1552        );
1553
1554        // Copy the IN_VISIBLE_GROUP flag down to all raw contacts to allow
1555        // indexing on (display_name, in_visible_group)
1556        db.execSQL(
1557                "UPDATE " + Tables.RAW_CONTACTS +
1558                " SET " + RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "=(" +
1559                        "SELECT " + Contacts.IN_VISIBLE_GROUP +
1560                        " FROM " + Tables.CONTACTS +
1561                        " WHERE " + Contacts._ID + "=" + RawContacts.CONTACT_ID + ")" +
1562                " WHERE " + RawContacts.CONTACT_ID + " NOT NULL"
1563        );
1564
1565        db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
1566                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1567                RawContactsColumns.DISPLAY_NAME + " COLLATE LOCALIZED ASC" +
1568        ");");
1569
1570        db.execSQL("DROP INDEX contacts_visible_index");
1571        db.execSQL("CREATE INDEX contacts_visible_index ON " + Tables.CONTACTS + " (" +
1572                Contacts.IN_VISIBLE_GROUP +
1573        ");");
1574    }
1575
1576    private void upgradeToVersion205(SQLiteDatabase db) {
1577        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1578                + " ADD " + RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT;");
1579        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1580                + " ADD " + RawContacts.PHONETIC_NAME + " TEXT;");
1581        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1582                + " ADD " + RawContacts.PHONETIC_NAME_STYLE + " INTEGER;");
1583        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1584                + " ADD " + RawContacts.SORT_KEY_PRIMARY + " TEXT COLLATE LOCALIZED;");
1585        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1586                + " ADD " + RawContacts.SORT_KEY_ALTERNATIVE + " TEXT COLLATE LOCALIZED;");
1587
1588        final Locale locale = Locale.getDefault();
1589
1590        NameSplitter splitter = new NameSplitter(
1591                mContext.getString(com.android.internal.R.string.common_name_prefixes),
1592                mContext.getString(com.android.internal.R.string.common_last_name_prefixes),
1593                mContext.getString(com.android.internal.R.string.common_name_suffixes),
1594                mContext.getString(com.android.internal.R.string.common_name_conjunctions),
1595                locale);
1596
1597        SQLiteStatement rawContactUpdate = db.compileStatement(
1598                "UPDATE " + Tables.RAW_CONTACTS +
1599                " SET " +
1600                        RawContacts.DISPLAY_NAME_PRIMARY + "=?," +
1601                        RawContacts.DISPLAY_NAME_ALTERNATIVE + "=?," +
1602                        RawContacts.PHONETIC_NAME + "=?," +
1603                        RawContacts.PHONETIC_NAME_STYLE + "=?," +
1604                        RawContacts.SORT_KEY_PRIMARY + "=?," +
1605                        RawContacts.SORT_KEY_ALTERNATIVE + "=?" +
1606                " WHERE " + RawContacts._ID + "=?");
1607
1608        upgradeStructuredNamesToVersion205(db, rawContactUpdate, splitter);
1609        upgradeOrganizationsToVersion205(db, rawContactUpdate, splitter);
1610
1611        db.execSQL("DROP INDEX raw_contact_sort_key1_index");
1612        db.execSQL("CREATE INDEX raw_contact_sort_key1_index ON " + Tables.RAW_CONTACTS + " (" +
1613                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1614                RawContacts.SORT_KEY_PRIMARY +
1615        ");");
1616
1617        db.execSQL("CREATE INDEX raw_contact_sort_key2_index ON " + Tables.RAW_CONTACTS + " (" +
1618                RawContactsColumns.CONTACT_IN_VISIBLE_GROUP + "," +
1619                RawContacts.SORT_KEY_ALTERNATIVE +
1620        ");");
1621    }
1622
1623    private interface StructName205Query {
1624        String TABLE = Tables.DATA_JOIN_RAW_CONTACTS;
1625
1626        String COLUMNS[] = {
1627                DataColumns.CONCRETE_ID,
1628                Data.RAW_CONTACT_ID,
1629                RawContacts.DISPLAY_NAME_SOURCE,
1630                RawContacts.DISPLAY_NAME_PRIMARY,
1631                StructuredName.PREFIX,
1632                StructuredName.GIVEN_NAME,
1633                StructuredName.MIDDLE_NAME,
1634                StructuredName.FAMILY_NAME,
1635                StructuredName.SUFFIX,
1636                StructuredName.PHONETIC_FAMILY_NAME,
1637                StructuredName.PHONETIC_MIDDLE_NAME,
1638                StructuredName.PHONETIC_GIVEN_NAME,
1639        };
1640
1641        int ID = 0;
1642        int RAW_CONTACT_ID = 1;
1643        int DISPLAY_NAME_SOURCE = 2;
1644        int DISPLAY_NAME = 3;
1645        int PREFIX = 4;
1646        int GIVEN_NAME = 5;
1647        int MIDDLE_NAME = 6;
1648        int FAMILY_NAME = 7;
1649        int SUFFIX = 8;
1650        int PHONETIC_FAMILY_NAME = 9;
1651        int PHONETIC_MIDDLE_NAME = 10;
1652        int PHONETIC_GIVEN_NAME = 11;
1653    }
1654
1655    private void upgradeStructuredNamesToVersion205(SQLiteDatabase db,
1656            SQLiteStatement rawContactUpdate, NameSplitter splitter) {
1657
1658        // Process structured names to detect the style of the full name and phonetic name
1659
1660        long mMimeType;
1661        try {
1662            mMimeType = DatabaseUtils.longForQuery(db,
1663                    "SELECT " + MimetypesColumns._ID +
1664                    " FROM " + Tables.MIMETYPES +
1665                    " WHERE " + MimetypesColumns.MIMETYPE
1666                            + "='" + StructuredName.CONTENT_ITEM_TYPE + "'", null);
1667        } catch (SQLiteDoneException e) {
1668            // No structured names in the database
1669            return;
1670        }
1671
1672        SQLiteStatement structuredNameUpdate = db.compileStatement(
1673                "UPDATE " + Tables.DATA +
1674                " SET " +
1675                        StructuredName.FULL_NAME_STYLE + "=?," +
1676                        StructuredName.DISPLAY_NAME + "=?," +
1677                        StructuredName.PHONETIC_NAME_STYLE + "=?" +
1678                " WHERE " + Data._ID + "=?");
1679
1680        NameSplitter.Name name = new NameSplitter.Name();
1681        StringBuilder sb = new StringBuilder();
1682        Cursor cursor = db.query(StructName205Query.TABLE,
1683                StructName205Query.COLUMNS,
1684                DataColumns.MIMETYPE_ID + "=" + mMimeType, null, null, null, null);
1685        try {
1686            while (cursor.moveToNext()) {
1687                long dataId = cursor.getLong(StructName205Query.ID);
1688                long rawContactId = cursor.getLong(StructName205Query.RAW_CONTACT_ID);
1689                int displayNameSource = cursor.getInt(StructName205Query.DISPLAY_NAME_SOURCE);
1690                String displayName = cursor.getString(StructName205Query.DISPLAY_NAME);
1691
1692                name.clear();
1693                name.prefix = cursor.getString(StructName205Query.PREFIX);
1694                name.givenNames = cursor.getString(StructName205Query.GIVEN_NAME);
1695                name.middleName = cursor.getString(StructName205Query.MIDDLE_NAME);
1696                name.familyName = cursor.getString(StructName205Query.FAMILY_NAME);
1697                name.suffix = cursor.getString(StructName205Query.SUFFIX);
1698                name.phoneticFamilyName = cursor.getString(StructName205Query.PHONETIC_FAMILY_NAME);
1699                name.phoneticMiddleName = cursor.getString(StructName205Query.PHONETIC_MIDDLE_NAME);
1700                name.phoneticGivenName = cursor.getString(StructName205Query.PHONETIC_GIVEN_NAME);
1701
1702                upgradeNameToVersion205(dataId, rawContactId, displayNameSource, displayName, name,
1703                        structuredNameUpdate, rawContactUpdate, splitter, sb);
1704            }
1705        } finally {
1706            cursor.close();
1707        }
1708    }
1709
1710    private void upgradeNameToVersion205(long dataId, long rawContactId, int displayNameSource,
1711            String currentDisplayName, NameSplitter.Name name,
1712            SQLiteStatement structuredNameUpdate, SQLiteStatement rawContactUpdate,
1713            NameSplitter splitter, StringBuilder sb) {
1714
1715        splitter.guessNameStyle(name);
1716        name.fullNameStyle = splitter.getAdjustedFullNameStyle(name.fullNameStyle);
1717        String displayName = splitter.join(name, true);
1718
1719        structuredNameUpdate.bindLong(1, name.fullNameStyle);
1720        DatabaseUtils.bindObjectToProgram(structuredNameUpdate, 2, displayName);
1721        structuredNameUpdate.bindLong(3, name.phoneticNameStyle);
1722        structuredNameUpdate.bindLong(4, dataId);
1723        structuredNameUpdate.execute();
1724
1725        if (displayNameSource == DisplayNameSources.STRUCTURED_NAME) {
1726            String displayNameAlternative = splitter.join(name, false);
1727            String phoneticName = splitter.joinPhoneticName(name);
1728            String sortKey = null;
1729            String sortKeyAlternative = null;
1730
1731            if (phoneticName != null) {
1732                sortKey = sortKeyAlternative = phoneticName;
1733            } else if (name.fullNameStyle == FullNameStyle.CHINESE) {
1734                sortKey = sortKeyAlternative = splitter.convertHanziToPinyin(displayName);
1735            }
1736
1737            if (sortKey == null) {
1738                sortKey = displayName;
1739                sortKeyAlternative = displayNameAlternative;
1740            }
1741
1742            updateRawContact205(rawContactUpdate, rawContactId, displayName,
1743                    displayNameAlternative, name.phoneticNameStyle, phoneticName, sortKey,
1744                    sortKeyAlternative);
1745        }
1746    }
1747
1748    private interface Organization205Query {
1749        String TABLE = Tables.DATA_JOIN_RAW_CONTACTS;
1750
1751        String COLUMNS[] = {
1752                DataColumns.CONCRETE_ID,
1753                Data.RAW_CONTACT_ID,
1754                Organization.COMPANY,
1755                Organization.PHONETIC_NAME,
1756        };
1757
1758        int ID = 0;
1759        int RAW_CONTACT_ID = 1;
1760        int COMPANY = 2;
1761        int PHONETIC_NAME = 3;
1762    }
1763
1764    private void upgradeOrganizationsToVersion205(SQLiteDatabase db,
1765            SQLiteStatement rawContactUpdate, NameSplitter splitter) {
1766        final long mimeType = lookupMimeTypeId(db, Organization.CONTENT_ITEM_TYPE);
1767
1768        SQLiteStatement organizationUpdate = db.compileStatement(
1769                "UPDATE " + Tables.DATA +
1770                " SET " +
1771                        Organization.PHONETIC_NAME_STYLE + "=?" +
1772                " WHERE " + Data._ID + "=?");
1773
1774        Cursor cursor = db.query(Organization205Query.TABLE, Organization205Query.COLUMNS,
1775                DataColumns.MIMETYPE_ID + "=" + mimeType + " AND "
1776                        + RawContacts.DISPLAY_NAME_SOURCE + "=" + DisplayNameSources.ORGANIZATION,
1777                null, null, null, null);
1778        try {
1779            while (cursor.moveToNext()) {
1780                long dataId = cursor.getLong(Organization205Query.ID);
1781                long rawContactId = cursor.getLong(Organization205Query.RAW_CONTACT_ID);
1782                String company = cursor.getString(Organization205Query.COMPANY);
1783                String phoneticName = cursor.getString(Organization205Query.PHONETIC_NAME);
1784
1785                int phoneticNameStyle = splitter.guessPhoneticNameStyle(phoneticName);
1786
1787                organizationUpdate.bindLong(1, phoneticNameStyle);
1788                organizationUpdate.bindLong(2, dataId);
1789                organizationUpdate.execute();
1790
1791                String sortKey = null;
1792                if (phoneticName == null && company != null) {
1793                    int nameStyle = splitter.guessFullNameStyle(company);
1794                    nameStyle = splitter.getAdjustedFullNameStyle(nameStyle);
1795                    if (nameStyle == FullNameStyle.CHINESE) {
1796                        sortKey = splitter.convertHanziToPinyin(company);
1797                    }
1798                }
1799
1800                if (sortKey == null) {
1801                    sortKey = company;
1802                }
1803
1804                updateRawContact205(rawContactUpdate, rawContactId, company,
1805                        company, phoneticNameStyle, phoneticName, sortKey, sortKey);
1806            }
1807        } finally {
1808            cursor.close();
1809        }
1810    }
1811
1812    private void updateRawContact205(SQLiteStatement rawContactUpdate, long rawContactId,
1813            String displayName, String displayNameAlternative, int phoneticNameStyle,
1814            String phoneticName, String sortKeyPrimary, String sortKeyAlternative) {
1815        bindString(rawContactUpdate, 1, displayName);
1816        bindString(rawContactUpdate, 2, displayNameAlternative);
1817        bindString(rawContactUpdate, 3, phoneticName);
1818        rawContactUpdate.bindLong(4, phoneticNameStyle);
1819        bindString(rawContactUpdate, 5, sortKeyPrimary);
1820        bindString(rawContactUpdate, 6, sortKeyAlternative);
1821        rawContactUpdate.bindLong(7, rawContactId);
1822        rawContactUpdate.execute();
1823    }
1824
1825    private void upgrateToVersion206(SQLiteDatabase db) {
1826        db.execSQL("ALTER TABLE " + Tables.RAW_CONTACTS
1827                + " ADD " + RawContacts.NAME_VERIFIED + " INTEGER NOT NULL DEFAULT 0;");
1828    }
1829
1830    private interface Organization300Query {
1831        String TABLE = Tables.DATA;
1832
1833        String SELECTION = DataColumns.MIMETYPE_ID + "=?";
1834
1835        String COLUMNS[] = {
1836                Organization._ID,
1837                Organization.RAW_CONTACT_ID,
1838                Organization.COMPANY,
1839                Organization.TITLE
1840        };
1841
1842        int ID = 0;
1843        int RAW_CONTACT_ID = 1;
1844        int COMPANY = 2;
1845        int TITLE = 3;
1846    }
1847
1848    /**
1849     * Fix for the bug where name lookup records for organizations would get removed by
1850     * unrelated updates of the data rows.
1851     */
1852    private void upgradeToVersion300(SQLiteDatabase db) {
1853        final long mimeType = lookupMimeTypeId(db, Organization.CONTENT_ITEM_TYPE);
1854        if (mimeType == -1) {
1855            return;
1856        }
1857
1858        ContentValues values = new ContentValues();
1859
1860        // Find all data rows with the mime type "organization"
1861        Cursor cursor = db.query(Organization300Query.TABLE, Organization300Query.COLUMNS,
1862                Organization300Query.SELECTION, new String[] {String.valueOf(mimeType)},
1863                null, null, null);
1864        try {
1865            while (cursor.moveToNext()) {
1866                long dataId = cursor.getLong(Organization300Query.ID);
1867                long rawContactId = cursor.getLong(Organization300Query.RAW_CONTACT_ID);
1868                String company = cursor.getString(Organization300Query.COMPANY);
1869                String title = cursor.getString(Organization300Query.TITLE);
1870
1871                // First delete name lookup if there is any (chances are there won't be)
1872                db.delete(Tables.NAME_LOOKUP, NameLookupColumns.DATA_ID + "=?",
1873                        new String[]{String.valueOf(dataId)});
1874
1875                // Now insert two name lookup records: one for company name, one for title
1876                values.put(NameLookupColumns.DATA_ID, dataId);
1877                values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId);
1878                values.put(NameLookupColumns.NAME_TYPE, NameLookupType.ORGANIZATION);
1879
1880                if (!TextUtils.isEmpty(company)) {
1881                    values.put(NameLookupColumns.NORMALIZED_NAME,
1882                            NameNormalizer.normalize(company));
1883                    db.insert(Tables.NAME_LOOKUP, null, values);
1884                }
1885
1886                if (!TextUtils.isEmpty(title)) {
1887                    values.put(NameLookupColumns.NORMALIZED_NAME,
1888                            NameNormalizer.normalize(title));
1889                    db.insert(Tables.NAME_LOOKUP, null, values);
1890                }
1891            }
1892        } finally {
1893            cursor.close();
1894        }
1895    }
1896
1897    private static final class Upgrade303Query {
1898        public static final String TABLE = Tables.DATA;
1899
1900        public static final String SELECTION =
1901                DataColumns.MIMETYPE_ID + "=?" +
1902        		" AND " + Data._ID + " NOT IN " +
1903        		"(SELECT " + NameLookupColumns.DATA_ID + " FROM " + Tables.NAME_LOOKUP + ")" +
1904        		" AND " + Data.DATA1 + " NOT NULL";
1905
1906        public static final String COLUMNS[] = {
1907                Data._ID,
1908                Data.RAW_CONTACT_ID,
1909                Data.DATA1,
1910        };
1911
1912        public static final int ID = 0;
1913        public static final int RAW_CONTACT_ID = 1;
1914        public static final int DATA1 = 2;
1915    }
1916
1917    /**
1918     * The {@link ContactsProvider2#update} method was deleting name lookup for new
1919     * emails during the sync.  We need to restore the lost name lookup rows.
1920     */
1921    private void upgradeEmailToVersion303(SQLiteDatabase db) {
1922        final long mimeTypeId = lookupMimeTypeId(db, Email.CONTENT_ITEM_TYPE);
1923        if (mimeTypeId == -1) {
1924            return;
1925        }
1926
1927        ContentValues values = new ContentValues();
1928
1929        // Find all data rows with the mime type "email" that are missing name lookup
1930        Cursor cursor = db.query(Upgrade303Query.TABLE, Upgrade303Query.COLUMNS,
1931                Upgrade303Query.SELECTION, new String[] {String.valueOf(mimeTypeId)},
1932                null, null, null);
1933        try {
1934            while (cursor.moveToNext()) {
1935                long dataId = cursor.getLong(Upgrade303Query.ID);
1936                long rawContactId = cursor.getLong(Upgrade303Query.RAW_CONTACT_ID);
1937                String value = cursor.getString(Upgrade303Query.DATA1);
1938                value = extractHandleFromEmailAddress(value);
1939
1940                if (value != null) {
1941                    values.put(NameLookupColumns.DATA_ID, dataId);
1942                    values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId);
1943                    values.put(NameLookupColumns.NAME_TYPE, NameLookupType.EMAIL_BASED_NICKNAME);
1944                    values.put(NameLookupColumns.NORMALIZED_NAME, NameNormalizer.normalize(value));
1945                    db.insert(Tables.NAME_LOOKUP, null, values);
1946                }
1947            }
1948        } finally {
1949            cursor.close();
1950        }
1951    }
1952
1953    /**
1954     * The {@link ContactsProvider2#update} method was deleting name lookup for new
1955     * nicknames during the sync.  We need to restore the lost name lookup rows.
1956     */
1957    private void upgradeNicknameToVersion303(SQLiteDatabase db) {
1958        final long mimeTypeId = lookupMimeTypeId(db, Nickname.CONTENT_ITEM_TYPE);
1959        if (mimeTypeId == -1) {
1960            return;
1961        }
1962
1963        ContentValues values = new ContentValues();
1964
1965        // Find all data rows with the mime type "nickname" that are missing name lookup
1966        Cursor cursor = db.query(Upgrade303Query.TABLE, Upgrade303Query.COLUMNS,
1967                Upgrade303Query.SELECTION, new String[] {String.valueOf(mimeTypeId)},
1968                null, null, null);
1969        try {
1970            while (cursor.moveToNext()) {
1971                long dataId = cursor.getLong(Upgrade303Query.ID);
1972                long rawContactId = cursor.getLong(Upgrade303Query.RAW_CONTACT_ID);
1973                String value = cursor.getString(Upgrade303Query.DATA1);
1974
1975                values.put(NameLookupColumns.DATA_ID, dataId);
1976                values.put(NameLookupColumns.RAW_CONTACT_ID, rawContactId);
1977                values.put(NameLookupColumns.NAME_TYPE, NameLookupType.NICKNAME);
1978                values.put(NameLookupColumns.NORMALIZED_NAME, NameNormalizer.normalize(value));
1979                db.insert(Tables.NAME_LOOKUP, null, values);
1980            }
1981        } finally {
1982            cursor.close();
1983        }
1984    }
1985
1986    /**
1987     * Regenerate all rows in the nickname_lookup and name_lookup tables,
1988     * because sort key generation algorithm has been modified, e.g. as a result of
1989     * locale change.
1990     */
1991    private void rebuildNameLookup(SQLiteDatabase db) {
1992        db.execSQL("DELETE FROM " + Tables.NICKNAME_LOOKUP);
1993        loadNicknameLookupTable(db);
1994
1995        db.execSQL("DROP INDEX IF EXISTS name_lookup_index");
1996        db.execSQL("DELETE FROM " + Tables.NAME_LOOKUP);
1997
1998        SQLiteStatement nameLookupInsert = db.compileStatement(
1999                "INSERT OR IGNORE INTO " + Tables.NAME_LOOKUP + "("
2000                        + NameLookupColumns.RAW_CONTACT_ID + ","
2001                        + NameLookupColumns.DATA_ID + ","
2002                        + NameLookupColumns.NAME_TYPE + ","
2003                        + NameLookupColumns.NORMALIZED_NAME +
2004                ") VALUES (?,?,?,?)");
2005
2006        insertStructuredNameLookup(db, nameLookupInsert);
2007        insertOrganizationLookup(db, nameLookupInsert);
2008        insertEmailLookup(db, nameLookupInsert);
2009        insertNicknameLookup(db, nameLookupInsert);
2010
2011        createContactsIndexes(db);
2012    }
2013
2014    private static final class StructuredNameQuery {
2015        public static final String TABLE = Tables.DATA;
2016
2017        public static final String SELECTION =
2018                DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL";
2019
2020        public static final String COLUMNS[] = {
2021                StructuredName._ID,
2022                StructuredName.RAW_CONTACT_ID,
2023                StructuredName.DISPLAY_NAME,
2024        };
2025
2026        public static final int ID = 0;
2027        public static final int RAW_CONTACT_ID = 1;
2028        public static final int DISPLAY_NAME = 2;
2029    }
2030
2031    private class StructuredNameLookupBuilder extends NameLookupBuilder {
2032
2033        private final SQLiteStatement mNameLookupInsert;
2034        private final CommonNicknameCache mCommonNicknameCache;
2035
2036        public StructuredNameLookupBuilder(NameSplitter splitter,
2037                CommonNicknameCache commonNicknameCache, SQLiteStatement nameLookupInsert) {
2038            super(splitter);
2039            this.mCommonNicknameCache = commonNicknameCache;
2040            this.mNameLookupInsert = nameLookupInsert;
2041        }
2042
2043        @Override
2044        protected void insertNameLookup(long rawContactId, long dataId, int lookupType,
2045                String name) {
2046            if (!TextUtils.isEmpty(name)) {
2047                ContactsDatabaseHelper.this.insertNormalizedNameLookup(mNameLookupInsert,
2048                        rawContactId, dataId, lookupType, name);
2049            }
2050        }
2051
2052        @Override
2053        protected String[] getCommonNicknameClusters(String normalizedName) {
2054            return mCommonNicknameCache.getCommonNicknameClusters(normalizedName);
2055        }
2056    }
2057
2058    /**
2059     * Inserts name lookup rows for all structured names in the database.
2060     */
2061    private void insertStructuredNameLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) {
2062        NameSplitter nameSplitter = createNameSplitter();
2063        NameLookupBuilder nameLookupBuilder = new StructuredNameLookupBuilder(nameSplitter,
2064                new CommonNicknameCache(db), nameLookupInsert);
2065        final long mimeTypeId = lookupMimeTypeId(db, StructuredName.CONTENT_ITEM_TYPE);
2066        Cursor cursor = db.query(StructuredNameQuery.TABLE, StructuredNameQuery.COLUMNS,
2067                StructuredNameQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)},
2068                null, null, null);
2069        try {
2070            while (cursor.moveToNext()) {
2071                long dataId = cursor.getLong(StructuredNameQuery.ID);
2072                long rawContactId = cursor.getLong(StructuredNameQuery.RAW_CONTACT_ID);
2073                String name = cursor.getString(StructuredNameQuery.DISPLAY_NAME);
2074                int fullNameStyle = nameSplitter.guessFullNameStyle(name);
2075                nameLookupBuilder.insertNameLookup(rawContactId, dataId, name, fullNameStyle);
2076            }
2077        } finally {
2078            cursor.close();
2079        }
2080    }
2081
2082    private static final class OrganizationQuery {
2083        public static final String TABLE = Tables.DATA;
2084
2085        public static final String SELECTION =
2086                DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL";
2087
2088        public static final String COLUMNS[] = {
2089                Organization._ID,
2090                Organization.RAW_CONTACT_ID,
2091                Organization.COMPANY,
2092                Organization.TITLE,
2093        };
2094
2095        public static final int ID = 0;
2096        public static final int RAW_CONTACT_ID = 1;
2097        public static final int COMPANY = 2;
2098        public static final int TITLE = 3;
2099    }
2100
2101    /**
2102     * Inserts name lookup rows for all organizations in the database.
2103     */
2104    private void insertOrganizationLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) {
2105        final long mimeTypeId = lookupMimeTypeId(db, Organization.CONTENT_ITEM_TYPE);
2106        Cursor cursor = db.query(OrganizationQuery.TABLE, OrganizationQuery.COLUMNS,
2107                OrganizationQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)},
2108                null, null, null);
2109        try {
2110            while (cursor.moveToNext()) {
2111                long dataId = cursor.getLong(OrganizationQuery.ID);
2112                long rawContactId = cursor.getLong(OrganizationQuery.RAW_CONTACT_ID);
2113                String organization = cursor.getString(OrganizationQuery.COMPANY);
2114                String title = cursor.getString(OrganizationQuery.TITLE);
2115                insertNameLookup(nameLookupInsert, rawContactId, dataId,
2116                        NameLookupType.ORGANIZATION, organization);
2117                insertNameLookup(nameLookupInsert, rawContactId, dataId,
2118                        NameLookupType.ORGANIZATION, title);
2119            }
2120        } finally {
2121            cursor.close();
2122        }
2123    }
2124
2125    private static final class EmailQuery {
2126        public static final String TABLE = Tables.DATA;
2127
2128        public static final String SELECTION =
2129                DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL";
2130
2131        public static final String COLUMNS[] = {
2132                Email._ID,
2133                Email.RAW_CONTACT_ID,
2134                Email.ADDRESS,
2135        };
2136
2137        public static final int ID = 0;
2138        public static final int RAW_CONTACT_ID = 1;
2139        public static final int ADDRESS = 2;
2140    }
2141
2142    /**
2143     * Inserts name lookup rows for all email addresses in the database.
2144     */
2145    private void insertEmailLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) {
2146        final long mimeTypeId = lookupMimeTypeId(db, Email.CONTENT_ITEM_TYPE);
2147        Cursor cursor = db.query(EmailQuery.TABLE, EmailQuery.COLUMNS,
2148                EmailQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)},
2149                null, null, null);
2150        try {
2151            while (cursor.moveToNext()) {
2152                long dataId = cursor.getLong(EmailQuery.ID);
2153                long rawContactId = cursor.getLong(EmailQuery.RAW_CONTACT_ID);
2154                String address = cursor.getString(EmailQuery.ADDRESS);
2155                address = extractHandleFromEmailAddress(address);
2156                insertNameLookup(nameLookupInsert, rawContactId, dataId,
2157                        NameLookupType.EMAIL_BASED_NICKNAME, address);
2158            }
2159        } finally {
2160            cursor.close();
2161        }
2162    }
2163
2164    private static final class NicknameQuery {
2165        public static final String TABLE = Tables.DATA;
2166
2167        public static final String SELECTION =
2168                DataColumns.MIMETYPE_ID + "=? AND " + Data.DATA1 + " NOT NULL";
2169
2170        public static final String COLUMNS[] = {
2171                Nickname._ID,
2172                Nickname.RAW_CONTACT_ID,
2173                Nickname.NAME,
2174        };
2175
2176        public static final int ID = 0;
2177        public static final int RAW_CONTACT_ID = 1;
2178        public static final int NAME = 2;
2179    }
2180
2181    /**
2182     * Inserts name lookup rows for all nicknames in the database.
2183     */
2184    private void insertNicknameLookup(SQLiteDatabase db, SQLiteStatement nameLookupInsert) {
2185        final long mimeTypeId = lookupMimeTypeId(db, Nickname.CONTENT_ITEM_TYPE);
2186        Cursor cursor = db.query(NicknameQuery.TABLE, NicknameQuery.COLUMNS,
2187                NicknameQuery.SELECTION, new String[] {String.valueOf(mimeTypeId)},
2188                null, null, null);
2189        try {
2190            while (cursor.moveToNext()) {
2191                long dataId = cursor.getLong(NicknameQuery.ID);
2192                long rawContactId = cursor.getLong(NicknameQuery.RAW_CONTACT_ID);
2193                String nickname = cursor.getString(NicknameQuery.NAME);
2194                insertNameLookup(nameLookupInsert, rawContactId, dataId,
2195                        NameLookupType.NICKNAME, nickname);
2196            }
2197        } finally {
2198            cursor.close();
2199        }
2200    }
2201
2202    /**
2203     * Inserts a record in the {@link Tables#NAME_LOOKUP} table.
2204     */
2205    public void insertNameLookup(SQLiteStatement stmt, long rawContactId, long dataId,
2206            int lookupType, String name) {
2207        if (TextUtils.isEmpty(name)) {
2208            return;
2209        }
2210
2211        String normalized = NameNormalizer.normalize(name);
2212        if (TextUtils.isEmpty(normalized)) {
2213            return;
2214        }
2215
2216        insertNormalizedNameLookup(stmt, rawContactId, dataId, lookupType, normalized);
2217    }
2218
2219    private void insertNormalizedNameLookup(SQLiteStatement stmt, long rawContactId, long dataId,
2220            int lookupType, String normalizedName) {
2221        stmt.bindLong(1, rawContactId);
2222        stmt.bindLong(2, dataId);
2223        stmt.bindLong(3, lookupType);
2224        stmt.bindString(4, normalizedName);
2225        stmt.executeInsert();
2226    }
2227
2228    public String extractHandleFromEmailAddress(String email) {
2229        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(email);
2230        if (tokens.length == 0) {
2231            return null;
2232        }
2233
2234        String address = tokens[0].getAddress();
2235        int at = address.indexOf('@');
2236        if (at != -1) {
2237            return address.substring(0, at);
2238        }
2239        return null;
2240    }
2241
2242    public String extractAddressFromEmailAddress(String email) {
2243        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(email);
2244        if (tokens.length == 0) {
2245            return null;
2246        }
2247
2248        return tokens[0].getAddress();
2249    }
2250
2251    private long lookupMimeTypeId(SQLiteDatabase db, String mimeType) {
2252        try {
2253            return DatabaseUtils.longForQuery(db,
2254                    "SELECT " + MimetypesColumns._ID +
2255                    " FROM " + Tables.MIMETYPES +
2256                    " WHERE " + MimetypesColumns.MIMETYPE
2257                            + "='" + mimeType + "'", null);
2258        } catch (SQLiteDoneException e) {
2259            // No rows of this type in the database
2260            return -1;
2261        }
2262    }
2263
2264    private void bindString(SQLiteStatement stmt, int index, String value) {
2265        if (value == null) {
2266            stmt.bindNull(index);
2267        } else {
2268            stmt.bindString(index, value);
2269        }
2270    }
2271
2272    /**
2273     * Adds index stats into the SQLite database to force it to always use the lookup indexes.
2274     */
2275    private void updateSqliteStats(SQLiteDatabase db) {
2276
2277        // Specific stats strings are based on an actual large database after running ANALYZE
2278        try {
2279            updateIndexStats(db, Tables.CONTACTS,
2280                    "contacts_restricted_index", "10000 9000");
2281            updateIndexStats(db, Tables.CONTACTS,
2282                    "contacts_has_phone_index", "10000 500");
2283            updateIndexStats(db, Tables.CONTACTS,
2284                    "contacts_visible_index", "10000 500 1");
2285
2286            updateIndexStats(db, Tables.RAW_CONTACTS,
2287                    "raw_contacts_source_id_index", "10000 1 1 1");
2288            updateIndexStats(db, Tables.RAW_CONTACTS,
2289                    "raw_contacts_contact_id_index", "10000 2");
2290
2291            updateIndexStats(db, Tables.NAME_LOOKUP,
2292                    "name_lookup_raw_contact_id_index", "10000 3");
2293            updateIndexStats(db, Tables.NAME_LOOKUP,
2294                    "name_lookup_index", "10000 3 2 2 1");
2295            updateIndexStats(db, Tables.NAME_LOOKUP,
2296                    "sqlite_autoindex_name_lookup_1", "10000 3 2 1");
2297
2298            updateIndexStats(db, Tables.PHONE_LOOKUP,
2299                    "phone_lookup_index", "10000 2 2 1");
2300            updateIndexStats(db, Tables.PHONE_LOOKUP,
2301                    "phone_lookup_min_match_index", "10000 2 2 1");
2302
2303            updateIndexStats(db, Tables.DATA,
2304                    "data_mimetype_data1_index", "60000 5000 2");
2305            updateIndexStats(db, Tables.DATA,
2306                    "data_raw_contact_id", "60000 10");
2307
2308            updateIndexStats(db, Tables.GROUPS,
2309                    "groups_source_id_index", "50 1 1 1");
2310
2311            updateIndexStats(db, Tables.NICKNAME_LOOKUP,
2312                    "sqlite_autoindex_name_lookup_1", "500 2 1");
2313
2314        } catch (SQLException e) {
2315            Log.e(TAG, "Could not update index stats", e);
2316        }
2317    }
2318
2319    /**
2320     * Stores statistics for a given index.
2321     *
2322     * @param stats has the following structure: the first index is the expected size of
2323     * the table.  The following integer(s) are the expected number of records selected with the
2324     * index.  There should be one integer per indexed column.
2325     */
2326    private void updateIndexStats(SQLiteDatabase db, String table, String index,
2327            String stats) {
2328        db.execSQL("DELETE FROM sqlite_stat1 WHERE tbl='" + table + "' AND idx='" + index + "';");
2329        db.execSQL("INSERT INTO sqlite_stat1 (tbl,idx,stat)"
2330                + " VALUES ('" + table + "','" + index + "','" + stats + "');");
2331    }
2332
2333    @Override
2334    public synchronized SQLiteDatabase getWritableDatabase() {
2335        SQLiteDatabase db = super.getWritableDatabase();
2336        if (mReopenDatabase) {
2337            mReopenDatabase = false;
2338            close();
2339            db = super.getWritableDatabase();
2340        }
2341        return db;
2342    }
2343
2344    /**
2345     * Wipes all data except mime type and package lookup tables.
2346     */
2347    public void wipeData() {
2348        SQLiteDatabase db = getWritableDatabase();
2349
2350        db.execSQL("DELETE FROM " + Tables.CONTACTS + ";");
2351        db.execSQL("DELETE FROM " + Tables.RAW_CONTACTS + ";");
2352        db.execSQL("DELETE FROM " + Tables.DATA + ";");
2353        db.execSQL("DELETE FROM " + Tables.PHONE_LOOKUP + ";");
2354        db.execSQL("DELETE FROM " + Tables.NAME_LOOKUP + ";");
2355        db.execSQL("DELETE FROM " + Tables.GROUPS + ";");
2356        db.execSQL("DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS + ";");
2357        db.execSQL("DELETE FROM " + Tables.SETTINGS + ";");
2358        db.execSQL("DELETE FROM " + Tables.ACTIVITIES + ";");
2359        db.execSQL("DELETE FROM " + Tables.CALLS + ";");
2360
2361        // Note: we are not removing reference data from Tables.NICKNAME_LOOKUP
2362    }
2363
2364    public NameSplitter createNameSplitter() {
2365        return new NameSplitter(
2366                mContext.getString(com.android.internal.R.string.common_name_prefixes),
2367                mContext.getString(com.android.internal.R.string.common_last_name_prefixes),
2368                mContext.getString(com.android.internal.R.string.common_name_suffixes),
2369                mContext.getString(com.android.internal.R.string.common_name_conjunctions),
2370                Locale.getDefault());
2371    }
2372
2373    /**
2374     * Return the {@link ApplicationInfo#uid} for the given package name.
2375     */
2376    public static int getUidForPackageName(PackageManager pm, String packageName) {
2377        try {
2378            ApplicationInfo clientInfo = pm.getApplicationInfo(packageName, 0 /* no flags */);
2379            return clientInfo.uid;
2380        } catch (NameNotFoundException e) {
2381            throw new RuntimeException(e);
2382        }
2383    }
2384
2385    /**
2386     * Perform an internal string-to-integer lookup using the compiled
2387     * {@link SQLiteStatement} provided, using the in-memory cache to speed up
2388     * lookups. If a mapping isn't found in cache or database, it will be
2389     * created. All new, uncached answers are added to the cache automatically.
2390     *
2391     * @param query Compiled statement used to query for the mapping.
2392     * @param insert Compiled statement used to insert a new mapping when no
2393     *            existing one is found in cache or from query.
2394     * @param value Value to find mapping for.
2395     * @param cache In-memory cache of previous answers.
2396     * @return An unique integer mapping for the given value.
2397     */
2398    private long getCachedId(SQLiteStatement query, SQLiteStatement insert,
2399            String value, HashMap<String, Long> cache) {
2400        // Try an in-memory cache lookup
2401        if (cache.containsKey(value)) {
2402            return cache.get(value);
2403        }
2404
2405        long id = -1;
2406        try {
2407            // Try searching database for mapping
2408            DatabaseUtils.bindObjectToProgram(query, 1, value);
2409            id = query.simpleQueryForLong();
2410        } catch (SQLiteDoneException e) {
2411            // Nothing found, so try inserting new mapping
2412            DatabaseUtils.bindObjectToProgram(insert, 1, value);
2413            id = insert.executeInsert();
2414        }
2415
2416        if (id != -1) {
2417            // Cache and return the new answer
2418            cache.put(value, id);
2419            return id;
2420        } else {
2421            // Otherwise throw if no mapping found or created
2422            throw new IllegalStateException("Couldn't find or create internal "
2423                    + "lookup table entry for value " + value);
2424        }
2425    }
2426
2427    /**
2428     * Convert a package name into an integer, using {@link Tables#PACKAGES} for
2429     * lookups and possible allocation of new IDs as needed.
2430     */
2431    public long getPackageId(String packageName) {
2432        // Make sure compiled statements are ready by opening database
2433        getReadableDatabase();
2434        return getCachedId(mPackageQuery, mPackageInsert, packageName, mPackageCache);
2435    }
2436
2437    /**
2438     * Convert a mimetype into an integer, using {@link Tables#MIMETYPES} for
2439     * lookups and possible allocation of new IDs as needed.
2440     */
2441    public long getMimeTypeId(String mimetype) {
2442        // Make sure compiled statements are ready by opening database
2443        getReadableDatabase();
2444        return getMimeTypeIdNoDbCheck(mimetype);
2445    }
2446
2447    private long getMimeTypeIdNoDbCheck(String mimetype) {
2448        return getCachedId(mMimetypeQuery, mMimetypeInsert, mimetype, mMimetypeCache);
2449    }
2450
2451    /**
2452     * Find the mimetype for the given {@link Data#_ID}.
2453     */
2454    public String getDataMimeType(long dataId) {
2455        // Make sure compiled statements are ready by opening database
2456        getReadableDatabase();
2457        try {
2458            // Try database query to find mimetype
2459            DatabaseUtils.bindObjectToProgram(mDataMimetypeQuery, 1, dataId);
2460            String mimetype = mDataMimetypeQuery.simpleQueryForString();
2461            return mimetype;
2462        } catch (SQLiteDoneException e) {
2463            // No valid mapping found, so return null
2464            return null;
2465        }
2466    }
2467
2468    /**
2469     * Find the mime-type for the given {@link Activities#_ID}.
2470     */
2471    public String getActivityMimeType(long activityId) {
2472        // Make sure compiled statements are ready by opening database
2473        getReadableDatabase();
2474        try {
2475            // Try database query to find mimetype
2476            DatabaseUtils.bindObjectToProgram(mActivitiesMimetypeQuery, 1, activityId);
2477            String mimetype = mActivitiesMimetypeQuery.simpleQueryForString();
2478            return mimetype;
2479        } catch (SQLiteDoneException e) {
2480            // No valid mapping found, so return null
2481            return null;
2482        }
2483    }
2484
2485    /**
2486     * Update {@link Contacts#IN_VISIBLE_GROUP} for all contacts.
2487     */
2488    public void updateAllVisible() {
2489        SQLiteDatabase db = getWritableDatabase();
2490        final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
2491        String[] selectionArgs = new String[]{String.valueOf(groupMembershipMimetypeId)};
2492
2493        // There are a couple questions that can be asked regarding the
2494        // following two update statements:
2495        //
2496        // Q: Why do we run these two queries separately? They seem like they could be combined.
2497        // A: This is a result of painstaking experimentation.  Turns out that the most
2498        // important optimization is to make sure we never update a value to its current value.
2499        // Changing 0 to 0 is unexpectedly expensive - SQLite actually writes the unchanged
2500        // rows back to disk.  The other consideration is that the CONTACT_IS_VISIBLE condition
2501        // is very complex and executing it twice in the same statement ("if contact_visible !=
2502        // CONTACT_IS_VISIBLE change it to CONTACT_IS_VISIBLE") is more expensive than running
2503        // two update statements.
2504        //
2505        // Q: How come we are using db.update instead of compiled statements?
2506        // A: This is a limitation of the compiled statement API. It does not return the
2507        // number of rows changed.  As you will see later in this method we really need
2508        // to know how many rows have been changed.
2509
2510        // First update contacts that are currently marked as invisible, but need to be visible
2511        ContentValues values = new ContentValues();
2512        values.put(Contacts.IN_VISIBLE_GROUP, 1);
2513        int countMadeVisible = db.update(Tables.CONTACTS, values,
2514                Contacts.IN_VISIBLE_GROUP + "=0" + " AND (" + Clauses.CONTACT_IS_VISIBLE + ")=1",
2515                selectionArgs);
2516
2517        // Next update contacts that are currently marked as visible, but need to be invisible
2518        values.put(Contacts.IN_VISIBLE_GROUP, 0);
2519        int countMadeInvisible = db.update(Tables.CONTACTS, values,
2520                Contacts.IN_VISIBLE_GROUP + "=1" + " AND (" + Clauses.CONTACT_IS_VISIBLE + ")=0",
2521                selectionArgs);
2522
2523        if (countMadeVisible != 0 || countMadeInvisible != 0) {
2524            // TODO break out the fields (contact_in_visible_group, sort_key, sort_key_alt) into
2525            // a separate table.
2526            // Rationale: The following statement will take a very long time on
2527            // a large database even though we are only changing one field from 0 to 1 or from
2528            // 1 to 0.  The reason for the slowness is that SQLite will need to write the whole
2529            // page even when only one bit on it changes. Changing the visibility of a
2530            // significant number of contacts will likely read and write almost the entire
2531            // raw_contacts table.  So, the solution is to break out into a separate table
2532            // the changing field along with the sort keys used for index-based sorting.
2533            // That table will occupy a smaller number of pages, so rewriting it would
2534            // not be as expensive.
2535            mVisibleUpdateRawContacts.execute();
2536        }
2537    }
2538
2539    /**
2540     * Update {@link Contacts#IN_VISIBLE_GROUP} for a specific contact.
2541     */
2542    public void updateContactVisible(long contactId) {
2543        final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
2544        mVisibleSpecificUpdate.bindLong(1, groupMembershipMimetypeId);
2545        mVisibleSpecificUpdate.bindLong(2, contactId);
2546        mVisibleSpecificUpdate.execute();
2547
2548        mVisibleSpecificUpdateRawContacts.bindLong(1, contactId);
2549        mVisibleSpecificUpdateRawContacts.execute();
2550    }
2551
2552    /**
2553     * Returns contact ID for the given contact or zero if it is NULL.
2554     */
2555    public long getContactId(long rawContactId) {
2556        getReadableDatabase();
2557        try {
2558            DatabaseUtils.bindObjectToProgram(mContactIdQuery, 1, rawContactId);
2559            return mContactIdQuery.simpleQueryForLong();
2560        } catch (SQLiteDoneException e) {
2561            // No valid mapping found, so return 0
2562            return 0;
2563        }
2564    }
2565
2566    public int getAggregationMode(long rawContactId) {
2567        getReadableDatabase();
2568        try {
2569            DatabaseUtils.bindObjectToProgram(mAggregationModeQuery, 1, rawContactId);
2570            return (int)mAggregationModeQuery.simpleQueryForLong();
2571        } catch (SQLiteDoneException e) {
2572            // No valid row found, so return "disabled"
2573            return RawContacts.AGGREGATION_MODE_DISABLED;
2574        }
2575    }
2576
2577    public void buildPhoneLookupAndRawContactQuery(SQLiteQueryBuilder qb, String number) {
2578        String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2579        qb.setTables(Tables.DATA_JOIN_RAW_CONTACTS +
2580                " JOIN " + Tables.PHONE_LOOKUP
2581                + " ON(" + DataColumns.CONCRETE_ID + "=" + PhoneLookupColumns.DATA_ID + ")");
2582
2583        StringBuilder sb = new StringBuilder();
2584        sb.append(PhoneLookupColumns.MIN_MATCH + "='");
2585        sb.append(minMatch);
2586        sb.append("' AND PHONE_NUMBERS_EQUAL(data." + Phone.NUMBER + ", ");
2587        DatabaseUtils.appendEscapedSQLString(sb, number);
2588        sb.append(mUseStrictPhoneNumberComparison ? ", 1)" : ", 0)");
2589
2590        qb.appendWhere(sb.toString());
2591    }
2592
2593    public void buildPhoneLookupAndContactQuery(SQLiteQueryBuilder qb, String number) {
2594        String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2595        StringBuilder sb = new StringBuilder();
2596        appendPhoneLookupTables(sb, minMatch, true);
2597        qb.setTables(sb.toString());
2598
2599        sb = new StringBuilder();
2600        appendPhoneLookupSelection(sb, number);
2601        qb.appendWhere(sb.toString());
2602    }
2603
2604    public String buildPhoneLookupAsNestedQuery(String number) {
2605        StringBuilder sb = new StringBuilder();
2606        final String minMatch = PhoneNumberUtils.toCallerIDMinMatch(number);
2607        sb.append("(SELECT DISTINCT raw_contact_id" + " FROM ");
2608        appendPhoneLookupTables(sb, minMatch, false);
2609        sb.append(" WHERE ");
2610        appendPhoneLookupSelection(sb, number);
2611        sb.append(")");
2612        return sb.toString();
2613    }
2614
2615    private void appendPhoneLookupTables(StringBuilder sb, final String minMatch,
2616            boolean joinContacts) {
2617        sb.append(Tables.RAW_CONTACTS);
2618        if (joinContacts) {
2619            sb.append(" JOIN " + getContactView() + " contacts_view"
2620                    + " ON (contacts_view._id = raw_contacts.contact_id)");
2621        }
2622        sb.append(", (SELECT data_id FROM phone_lookup "
2623                + "WHERE (" + Tables.PHONE_LOOKUP + "." + PhoneLookupColumns.MIN_MATCH + " = '");
2624        sb.append(minMatch);
2625        sb.append("')) AS lookup, " + Tables.DATA);
2626    }
2627
2628    private void appendPhoneLookupSelection(StringBuilder sb, String number) {
2629        sb.append("lookup.data_id=data._id AND data.raw_contact_id=raw_contacts._id"
2630                + " AND PHONE_NUMBERS_EQUAL(data." + Phone.NUMBER + ", ");
2631        DatabaseUtils.appendEscapedSQLString(sb, number);
2632        sb.append(mUseStrictPhoneNumberComparison ? ", 1)" : ", 0)");
2633    }
2634
2635    public String getUseStrictPhoneNumberComparisonParameter() {
2636        return mUseStrictPhoneNumberComparison ? "1" : "0";
2637    }
2638
2639    /**
2640     * Loads common nickname mappings into the database.
2641     */
2642    private void loadNicknameLookupTable(SQLiteDatabase db) {
2643        String[] strings = mContext.getResources().getStringArray(
2644                com.android.internal.R.array.common_nicknames);
2645        if (strings == null || strings.length == 0) {
2646            return;
2647        }
2648
2649        SQLiteStatement nicknameLookupInsert = db.compileStatement("INSERT INTO "
2650                + Tables.NICKNAME_LOOKUP + "(" + NicknameLookupColumns.NAME + ","
2651                + NicknameLookupColumns.CLUSTER + ") VALUES (?,?)");
2652
2653        for (int clusterId = 0; clusterId < strings.length; clusterId++) {
2654            String[] names = strings[clusterId].split(",");
2655            for (int j = 0; j < names.length; j++) {
2656                String name = NameNormalizer.normalize(names[j]);
2657                try {
2658                    DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 1, name);
2659                    DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 2,
2660                            String.valueOf(clusterId));
2661                    nicknameLookupInsert.executeInsert();
2662                } catch (SQLiteException e) {
2663
2664                    // Print the exception and keep going - this is not a fatal error
2665                    Log.e(TAG, "Cannot insert nickname: " + names[j], e);
2666                }
2667            }
2668        }
2669    }
2670
2671    public static void copyStringValue(ContentValues toValues, String toKey,
2672            ContentValues fromValues, String fromKey) {
2673        if (fromValues.containsKey(fromKey)) {
2674            toValues.put(toKey, fromValues.getAsString(fromKey));
2675        }
2676    }
2677
2678    public static void copyLongValue(ContentValues toValues, String toKey,
2679            ContentValues fromValues, String fromKey) {
2680        if (fromValues.containsKey(fromKey)) {
2681            long longValue;
2682            Object value = fromValues.get(fromKey);
2683            if (value instanceof Boolean) {
2684                if ((Boolean)value) {
2685                    longValue = 1;
2686                } else {
2687                    longValue = 0;
2688                }
2689            } else if (value instanceof String) {
2690                longValue = Long.parseLong((String)value);
2691            } else {
2692                longValue = ((Number)value).longValue();
2693            }
2694            toValues.put(toKey, longValue);
2695        }
2696    }
2697
2698    public SyncStateContentProviderHelper getSyncState() {
2699        return mSyncState;
2700    }
2701
2702    /**
2703     * Delete the aggregate contact if it has no constituent raw contacts other
2704     * than the supplied one.
2705     */
2706    public void removeContactIfSingleton(long rawContactId) {
2707        SQLiteDatabase db = getWritableDatabase();
2708
2709        // Obtain contact ID from the supplied raw contact ID
2710        String contactIdFromRawContactId = "(SELECT " + RawContacts.CONTACT_ID + " FROM "
2711                + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=" + rawContactId + ")";
2712
2713        // Find other raw contacts in the same aggregate contact
2714        String otherRawContacts = "(SELECT contacts1." + RawContacts._ID + " FROM "
2715                + Tables.RAW_CONTACTS + " contacts1 JOIN " + Tables.RAW_CONTACTS + " contacts2 ON ("
2716                + "contacts1." + RawContacts.CONTACT_ID + "=contacts2." + RawContacts.CONTACT_ID
2717                + ") WHERE contacts1." + RawContacts._ID + "!=" + rawContactId + ""
2718                + " AND contacts2." + RawContacts._ID + "=" + rawContactId + ")";
2719
2720        db.execSQL("DELETE FROM " + Tables.CONTACTS
2721                + " WHERE " + Contacts._ID + "=" + contactIdFromRawContactId
2722                + " AND NOT EXISTS " + otherRawContacts + ";");
2723    }
2724
2725    /**
2726     * Check if {@link Binder#getCallingUid()} should be allowed access to
2727     * {@link RawContacts#IS_RESTRICTED} data.
2728     */
2729    boolean hasAccessToRestrictedData() {
2730        final PackageManager pm = mContext.getPackageManager();
2731        final String[] callerPackages = pm.getPackagesForUid(Binder.getCallingUid());
2732
2733        // Has restricted access if caller matches any packages
2734        for (String callerPackage : callerPackages) {
2735            if (hasAccessToRestrictedData(callerPackage)) {
2736                return true;
2737            }
2738        }
2739        return false;
2740    }
2741
2742    /**
2743     * Check if requestingPackage should be allowed access to
2744     * {@link RawContacts#IS_RESTRICTED} data.
2745     */
2746    boolean hasAccessToRestrictedData(String requestingPackage) {
2747        if (mUnrestrictedPackages != null) {
2748            for (String allowedPackage : mUnrestrictedPackages) {
2749                if (allowedPackage.equals(requestingPackage)) {
2750                    return true;
2751                }
2752            }
2753        }
2754        return false;
2755    }
2756
2757    public String getDataView() {
2758        return getDataView(false);
2759    }
2760
2761    public String getDataView(boolean requireRestrictedView) {
2762        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2763                Views.DATA_ALL : Views.DATA_RESTRICTED;
2764    }
2765
2766    public String getRawContactView() {
2767        return getRawContactView(false);
2768    }
2769
2770    public String getRawContactView(boolean requireRestrictedView) {
2771        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2772                Views.RAW_CONTACTS_ALL : Views.RAW_CONTACTS_RESTRICTED;
2773    }
2774
2775    public String getContactView() {
2776        return getContactView(false);
2777    }
2778
2779    public String getContactView(boolean requireRestrictedView) {
2780        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2781                Views.CONTACTS_ALL : Views.CONTACTS_RESTRICTED;
2782    }
2783
2784    public String getGroupView() {
2785        return Views.GROUPS_ALL;
2786    }
2787
2788    public String getContactEntitiesView() {
2789        return getContactEntitiesView(false);
2790    }
2791
2792    public String getContactEntitiesView(boolean requireRestrictedView) {
2793        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
2794                Tables.CONTACT_ENTITIES : Tables.CONTACT_ENTITIES_RESTRICTED;
2795    }
2796
2797    /**
2798     * Test if any of the columns appear in the given projection.
2799     */
2800    public boolean isInProjection(String[] projection, String... columns) {
2801        if (projection == null) {
2802            return true;
2803        }
2804
2805        // Optimized for a single-column test
2806        if (columns.length == 1) {
2807            String column = columns[0];
2808            for (String test : projection) {
2809                if (column.equals(test)) {
2810                    return true;
2811                }
2812            }
2813        } else {
2814            for (String test : projection) {
2815                for (String column : columns) {
2816                    if (column.equals(test)) {
2817                        return true;
2818                    }
2819                }
2820            }
2821        }
2822        return false;
2823    }
2824
2825    /**
2826     * Returns a detailed exception message for the supplied URI.  It includes the calling
2827     * user and calling package(s).
2828     */
2829    public String exceptionMessage(Uri uri) {
2830        return exceptionMessage(null, uri);
2831    }
2832
2833    /**
2834     * Returns a detailed exception message for the supplied URI.  It includes the calling
2835     * user and calling package(s).
2836     */
2837    public String exceptionMessage(String message, Uri uri) {
2838        StringBuilder sb = new StringBuilder();
2839        if (message != null) {
2840            sb.append(message).append("; ");
2841        }
2842        sb.append("URI: ").append(uri);
2843        final PackageManager pm = mContext.getPackageManager();
2844        int callingUid = Binder.getCallingUid();
2845        sb.append(", calling user: ");
2846        String userName = pm.getNameForUid(callingUid);
2847        if (userName != null) {
2848            sb.append(userName);
2849        } else {
2850            sb.append(callingUid);
2851        }
2852
2853        final String[] callerPackages = pm.getPackagesForUid(callingUid);
2854        if (callerPackages != null && callerPackages.length > 0) {
2855            if (callerPackages.length == 1) {
2856                sb.append(", calling package:");
2857                sb.append(callerPackages[0]);
2858            } else {
2859                sb.append(", calling package is one of: [");
2860                for (int i = 0; i < callerPackages.length; i++) {
2861                    if (i != 0) {
2862                        sb.append(", ");
2863                    }
2864                    sb.append(callerPackages[i]);
2865                }
2866                sb.append("]");
2867            }
2868        }
2869
2870        return sb.toString();
2871    }
2872}
2873