ContactsDatabaseHelper.java revision d91272b48f97243533c6580981e12a4847b5783f
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.DatabaseUtils;
29import android.database.SQLException;
30import android.database.sqlite.SQLiteDatabase;
31import android.database.sqlite.SQLiteDoneException;
32import android.database.sqlite.SQLiteException;
33import android.database.sqlite.SQLiteOpenHelper;
34import android.database.sqlite.SQLiteQueryBuilder;
35import android.database.sqlite.SQLiteStatement;
36import android.os.Binder;
37import android.os.Bundle;
38import android.provider.BaseColumns;
39import android.provider.ContactsContract;
40import android.provider.CallLog.Calls;
41import android.provider.ContactsContract.AggregationExceptions;
42import android.provider.ContactsContract.Contacts;
43import android.provider.ContactsContract.Data;
44import android.provider.ContactsContract.Groups;
45import android.provider.ContactsContract.RawContacts;
46import android.provider.ContactsContract.Settings;
47import android.provider.ContactsContract.StatusUpdates;
48import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
49import android.provider.ContactsContract.CommonDataKinds.Phone;
50import android.provider.SocialContract.Activities;
51import android.telephony.PhoneNumberUtils;
52import android.util.Log;
53
54import java.util.HashMap;
55
56/**
57 * Database helper for contacts. Designed as a singleton to make sure that all
58 * {@link android.content.ContentProvider} users get the same reference.
59 * Provides handy methods for maintaining package and mime-type lookup tables.
60 */
61/* package */ class ContactsDatabaseHelper extends SQLiteOpenHelper {
62    private static final String TAG = "ContactsDatabaseHelper";
63
64    private static final int DATABASE_VERSION = 98;
65
66    private static final String DATABASE_NAME = "contacts2.db";
67    private static final String DATABASE_PRESENCE = "presence_db";
68
69    public interface Tables {
70        public static final String CONTACTS = "contacts";
71        public static final String RAW_CONTACTS = "raw_contacts";
72        public static final String PACKAGES = "packages";
73        public static final String MIMETYPES = "mimetypes";
74        public static final String PHONE_LOOKUP = "phone_lookup";
75        public static final String NAME_LOOKUP = "name_lookup";
76        public static final String AGGREGATION_EXCEPTIONS = "agg_exceptions";
77        public static final String SETTINGS = "settings";
78        public static final String DATA = "data";
79        public static final String GROUPS = "groups";
80        public static final String PRESENCE = "presence";
81        public static final String AGGREGATED_PRESENCE = "agg_presence";
82        public static final String NICKNAME_LOOKUP = "nickname_lookup";
83        public static final String CALLS = "calls";
84        public static final String CONTACT_ENTITIES = "contact_entities_view";
85        public static final String CONTACT_ENTITIES_RESTRICTED = "contact_entities_view_restricted";
86        public static final String STATUS_UPDATES = "status_updates";
87
88        public static final String DATA_JOIN_MIMETYPES = "data "
89                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id)";
90
91        public static final String DATA_JOIN_RAW_CONTACTS = "data "
92                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)";
93
94        public static final String DATA_JOIN_MIMETYPE_RAW_CONTACTS = "data "
95                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
96                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)";
97
98        public static final String DATA_JOIN_RAW_CONTACTS_GROUPS = "data "
99                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)"
100                + "LEFT OUTER JOIN groups ON (groups._id = data." + GroupMembership.GROUP_ROW_ID
101                + ")";
102
103        public static final String DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS = "data "
104                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
105                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
106                + "LEFT OUTER JOIN packages ON (data.package_id = packages._id)";
107
108        public static final String DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_CONTACTS = "data "
109                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
110                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
111                + "LEFT OUTER JOIN packages ON (data.package_id = packages._id) "
112                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
113
114        public static final String RAW_CONTACTS_JOIN_CONTACTS = "raw_contacts "
115                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
116
117        // NOTE: This requires late binding of GroupMembership MIME-type
118        public static final String RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS = "raw_contacts "
119                + "LEFT OUTER JOIN settings ON ("
120                    + "raw_contacts.account_name = settings.account_name AND "
121                    + "raw_contacts.account_type = settings.account_type) "
122                + "LEFT OUTER JOIN data ON (data.mimetype_id=? AND "
123                    + "data.raw_contact_id = raw_contacts._id) "
124                + "LEFT OUTER JOIN groups ON (groups._id = data." + GroupMembership.GROUP_ROW_ID
125                + ")";
126
127        // NOTE: This requires late binding of GroupMembership MIME-type
128        public static final String SETTINGS_JOIN_RAW_CONTACTS_DATA_MIMETYPES_CONTACTS = "settings "
129                + "LEFT OUTER JOIN raw_contacts ON ("
130                    + "raw_contacts.account_name = settings.account_name AND "
131                    + "raw_contacts.account_type = settings.account_type) "
132                + "LEFT OUTER JOIN data ON (data.mimetype_id=? AND "
133                    + "data.raw_contact_id = raw_contacts._id) "
134                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
135
136        public static final String DATA_JOIN_MIMETYPES_RAW_CONTACTS_CONTACTS = "data "
137                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
138                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
139                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
140
141        public static final String DATA_INNER_JOIN_MIMETYPES_RAW_CONTACTS_CONTACTS = "data "
142                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
143                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
144                + "JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
145
146        public static final String DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_CONTACTS_GROUPS =
147                "data "
148                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
149                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
150                + "LEFT OUTER JOIN packages ON (data.package_id = packages._id) "
151                + "LEFT OUTER JOIN groups "
152                + "  ON (mimetypes.mimetype='" + GroupMembership.CONTENT_ITEM_TYPE + "' "
153                + "      AND groups._id = data." + GroupMembership.GROUP_ROW_ID + ") "
154                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
155
156        public static final String DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_GROUPS = "data "
157                + "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
158                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
159                + "LEFT OUTER JOIN packages ON (data.package_id = packages._id) "
160                + "LEFT OUTER JOIN groups "
161                + "  ON (mimetypes.mimetype='" + GroupMembership.CONTENT_ITEM_TYPE + "' "
162                + "      AND groups._id = data." + GroupMembership.GROUP_ROW_ID + ") ";
163
164        public static final String GROUPS_JOIN_PACKAGES = "groups "
165                + "LEFT OUTER JOIN packages ON (groups.package_id = packages._id)";
166
167        public static final String GROUPS_JOIN_PACKAGES_DATA_RAW_CONTACTS_CONTACTS = "groups "
168                + "LEFT OUTER JOIN packages ON (groups.package_id = packages._id) "
169                + "LEFT OUTER JOIN data "
170                + "  ON (groups._id = data." + GroupMembership.GROUP_ROW_ID + ") "
171                + "LEFT OUTER JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
172                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
173
174        public static final String ACTIVITIES = "activities";
175
176        public static final String ACTIVITIES_JOIN_MIMETYPES = "activities "
177                + "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id)";
178
179        public static final String ACTIVITIES_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_CONTACTS =
180                "activities "
181                + "LEFT OUTER JOIN packages ON (activities.package_id = packages._id) "
182                + "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id) "
183                + "LEFT OUTER JOIN raw_contacts ON (activities.author_contact_id = " +
184                		"raw_contacts._id) "
185                + "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
186
187        public static final String NAME_LOOKUP_JOIN_RAW_CONTACTS = "name_lookup "
188                + "INNER JOIN raw_contacts ON (name_lookup.raw_contact_id = raw_contacts._id)";
189    }
190
191    public interface Views {
192        public static final String DATA_ALL = "view_data";
193        public static final String DATA_RESTRICTED = "view_data_restricted";
194
195        public static final String RAW_CONTACTS_ALL = "view_raw_contacts";
196        public static final String RAW_CONTACTS_RESTRICTED = "view_raw_contacts_restricted";
197
198        public static final String CONTACTS_ALL = "view_contacts";
199        public static final String CONTACTS_RESTRICTED = "view_contacts_restricted";
200
201        public static final String GROUPS_ALL = "view_groups";
202    }
203
204    public interface Clauses {
205        final String MIMETYPE_IS_GROUP_MEMBERSHIP = MimetypesColumns.CONCRETE_MIMETYPE + "='"
206                + GroupMembership.CONTENT_ITEM_TYPE + "'";
207
208        final String BELONGS_TO_GROUP = DataColumns.CONCRETE_GROUP_ID + "="
209                + GroupsColumns.CONCRETE_ID;
210
211        final String HAVING_NO_GROUPS = "COUNT(" + DataColumns.CONCRETE_GROUP_ID + ") == 0";
212
213        final String GROUP_BY_ACCOUNT_CONTACT_ID = SettingsColumns.CONCRETE_ACCOUNT_NAME + ","
214                + SettingsColumns.CONCRETE_ACCOUNT_TYPE + "," + RawContacts.CONTACT_ID;
215
216        final String RAW_CONTACT_IS_LOCAL = RawContactsColumns.CONCRETE_ACCOUNT_NAME
217                + " IS NULL AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " IS NULL";
218
219        final String ZERO_GROUP_MEMBERSHIPS = "COUNT(" + GroupsColumns.CONCRETE_ID + ")=0";
220
221        final String OUTER_RAW_CONTACTS = "outer_raw_contacts";
222        final String OUTER_RAW_CONTACTS_ID = OUTER_RAW_CONTACTS + "." + RawContacts._ID;
223
224        final String CONTACT_IS_VISIBLE =
225                "SELECT " +
226                    "MAX((SELECT (CASE WHEN " +
227                        "(CASE" +
228                            " WHEN " + RAW_CONTACT_IS_LOCAL +
229                            " THEN 1 " +
230                            " WHEN " + ZERO_GROUP_MEMBERSHIPS +
231                            " THEN " + Settings.UNGROUPED_VISIBLE +
232                            " ELSE MAX(" + Groups.GROUP_VISIBLE + ")" +
233                         "END)=1 THEN 1 ELSE 0 END)" +
234                " FROM " + Tables.RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS +
235                " WHERE " + RawContactsColumns.CONCRETE_ID + "=" + OUTER_RAW_CONTACTS_ID + "))" +
236                " FROM " + Tables.RAW_CONTACTS + " AS " + OUTER_RAW_CONTACTS +
237                " WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
238                " GROUP BY " + RawContacts.CONTACT_ID;
239
240        final String GROUP_HAS_ACCOUNT_AND_SOURCE_ID = Groups.SOURCE_ID + "=? AND "
241                + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?";
242    }
243
244    public interface ContactsColumns {
245        /**
246         * This flag is set for a contact if it has only one constituent raw contact and
247         * it is restricted.
248         */
249        public static final String SINGLE_IS_RESTRICTED = "single_is_restricted";
250
251        public static final String LAST_STATUS_UPDATE_ID = "status_update_id";
252
253        public static final String CONCRETE_ID = Tables.CONTACTS + "." + BaseColumns._ID;
254        public static final String CONCRETE_DISPLAY_NAME = Tables.CONTACTS + "."
255                + Contacts.DISPLAY_NAME;
256
257        public static final String CONCRETE_TIMES_CONTACTED = Tables.CONTACTS + "."
258                + Contacts.TIMES_CONTACTED;
259        public static final String CONCRETE_LAST_TIME_CONTACTED = Tables.CONTACTS + "."
260                + Contacts.LAST_TIME_CONTACTED;
261        public static final String CONCRETE_STARRED = Tables.CONTACTS + "." + Contacts.STARRED;
262        public static final String CONCRETE_CUSTOM_RINGTONE = Tables.CONTACTS + "."
263                + Contacts.CUSTOM_RINGTONE;
264        public static final String CONCRETE_SEND_TO_VOICEMAIL = Tables.CONTACTS + "."
265                + Contacts.SEND_TO_VOICEMAIL;
266    }
267
268    public interface RawContactsColumns {
269        public static final String CONCRETE_ID =
270                Tables.RAW_CONTACTS + "." + BaseColumns._ID;
271        public static final String CONCRETE_ACCOUNT_NAME =
272                Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_NAME;
273        public static final String CONCRETE_ACCOUNT_TYPE =
274                Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_TYPE;
275        public static final String CONCRETE_SOURCE_ID =
276                Tables.RAW_CONTACTS + "." + RawContacts.SOURCE_ID;
277        public static final String CONCRETE_VERSION =
278                Tables.RAW_CONTACTS + "." + RawContacts.VERSION;
279        public static final String CONCRETE_DIRTY =
280                Tables.RAW_CONTACTS + "." + RawContacts.DIRTY;
281        public static final String CONCRETE_DELETED =
282                Tables.RAW_CONTACTS + "." + RawContacts.DELETED;
283        public static final String CONCRETE_SYNC1 =
284                Tables.RAW_CONTACTS + "." + RawContacts.SYNC1;
285        public static final String CONCRETE_SYNC2 =
286                Tables.RAW_CONTACTS + "." + RawContacts.SYNC2;
287        public static final String CONCRETE_SYNC3 =
288                Tables.RAW_CONTACTS + "." + RawContacts.SYNC3;
289        public static final String CONCRETE_SYNC4 =
290                Tables.RAW_CONTACTS + "." + RawContacts.SYNC4;
291        public static final String CONCRETE_STARRED =
292                Tables.RAW_CONTACTS + "." + RawContacts.STARRED;
293
294        public static final String DISPLAY_NAME = "display_name";
295        public static final String DISPLAY_NAME_SOURCE = "display_name_source";
296        public static final String AGGREGATION_NEEDED = "aggregation_needed";
297    }
298
299    /**
300     * Types of data used to produce the display name for a contact. Listed in the order
301     * of increasing priority.
302     */
303    public interface DisplayNameSources {
304        int UNDEFINED = 0;
305        int EMAIL = 10;
306        int PHONE = 20;
307        int ORGANIZATION = 30;
308        int NICKNAME = 35;
309        int STRUCTURED_NAME = 40;
310    }
311
312    public interface DataColumns {
313        public static final String PACKAGE_ID = "package_id";
314        public static final String MIMETYPE_ID = "mimetype_id";
315
316        public static final String CONCRETE_ID = Tables.DATA + "." + BaseColumns._ID;
317        public static final String CONCRETE_MIMETYPE_ID = Tables.DATA + "." + MIMETYPE_ID;
318        public static final String CONCRETE_RAW_CONTACT_ID = Tables.DATA + "."
319                + Data.RAW_CONTACT_ID;
320        public static final String CONCRETE_GROUP_ID = Tables.DATA + "."
321                + GroupMembership.GROUP_ROW_ID;
322
323        public static final String CONCRETE_DATA1 = Tables.DATA + "." + Data.DATA1;
324        public static final String CONCRETE_DATA2 = Tables.DATA + "." + Data.DATA2;
325        public static final String CONCRETE_DATA3 = Tables.DATA + "." + Data.DATA3;
326        public static final String CONCRETE_DATA4 = Tables.DATA + "." + Data.DATA4;
327        public static final String CONCRETE_DATA5 = Tables.DATA + "." + Data.DATA5;
328        public static final String CONCRETE_DATA6 = Tables.DATA + "." + Data.DATA6;
329        public static final String CONCRETE_DATA7 = Tables.DATA + "." + Data.DATA7;
330        public static final String CONCRETE_DATA8 = Tables.DATA + "." + Data.DATA8;
331        public static final String CONCRETE_DATA9 = Tables.DATA + "." + Data.DATA9;
332        public static final String CONCRETE_DATA10 = Tables.DATA + "." + Data.DATA10;
333        public static final String CONCRETE_DATA11 = Tables.DATA + "." + Data.DATA11;
334        public static final String CONCRETE_DATA12 = Tables.DATA + "." + Data.DATA12;
335        public static final String CONCRETE_DATA13 = Tables.DATA + "." + Data.DATA13;
336        public static final String CONCRETE_DATA14 = Tables.DATA + "." + Data.DATA14;
337        public static final String CONCRETE_DATA15 = Tables.DATA + "." + Data.DATA15;
338        public static final String CONCRETE_IS_PRIMARY = Tables.DATA + "." + Data.IS_PRIMARY;
339        public static final String CONCRETE_PACKAGE_ID = Tables.DATA + "." + PACKAGE_ID;
340    }
341
342    // Used only for legacy API support
343    public interface ExtensionsColumns {
344        public static final String NAME = Data.DATA1;
345        public static final String VALUE = Data.DATA2;
346    }
347
348    public interface GroupMembershipColumns {
349        public static final String RAW_CONTACT_ID = Data.RAW_CONTACT_ID;
350        public static final String GROUP_ROW_ID = GroupMembership.GROUP_ROW_ID;
351    }
352
353    public interface PhoneColumns {
354        public static final String NORMALIZED_NUMBER = Data.DATA4;
355        public static final String CONCRETE_NORMALIZED_NUMBER = DataColumns.CONCRETE_DATA4;
356    }
357
358    public interface GroupsColumns {
359        public static final String PACKAGE_ID = "package_id";
360
361        public static final String CONCRETE_ID = Tables.GROUPS + "." + BaseColumns._ID;
362        public static final String CONCRETE_SOURCE_ID = Tables.GROUPS + "." + Groups.SOURCE_ID;
363        public static final String CONCRETE_ACCOUNT_NAME = Tables.GROUPS + "." + Groups.ACCOUNT_NAME;
364        public static final String CONCRETE_ACCOUNT_TYPE = Tables.GROUPS + "." + Groups.ACCOUNT_TYPE;
365    }
366
367    public interface ActivitiesColumns {
368        public static final String PACKAGE_ID = "package_id";
369        public static final String MIMETYPE_ID = "mimetype_id";
370    }
371
372    public interface PhoneLookupColumns {
373        public static final String _ID = BaseColumns._ID;
374        public static final String DATA_ID = "data_id";
375        public static final String RAW_CONTACT_ID = "raw_contact_id";
376        public static final String NORMALIZED_NUMBER = "normalized_number";
377    }
378
379    public interface NameLookupColumns {
380        public static final String RAW_CONTACT_ID = "raw_contact_id";
381        public static final String DATA_ID = "data_id";
382        public static final String NORMALIZED_NAME = "normalized_name";
383        public static final String NAME_TYPE = "name_type";
384    }
385
386    public final static class NameLookupType {
387        public static final int NAME_EXACT = 0;
388        public static final int NAME_VARIANT = 1;
389        public static final int NAME_COLLATION_KEY = 2;
390        public static final int NICKNAME = 3;
391        public static final int EMAIL_BASED_NICKNAME = 4;
392        public static final int ORGANIZATION = 5;
393
394        // This is the highest name lookup type code plus one
395        public static final int TYPE_COUNT = 6;
396
397        public static boolean isBasedOnStructuredName(int nameLookupType) {
398            return nameLookupType == NameLookupType.NAME_EXACT
399                    || nameLookupType == NameLookupType.NAME_VARIANT
400                    || nameLookupType == NameLookupType.NAME_COLLATION_KEY;
401        }
402    }
403
404    public interface PackagesColumns {
405        public static final String _ID = BaseColumns._ID;
406        public static final String PACKAGE = "package";
407
408        public static final String CONCRETE_ID = Tables.PACKAGES + "." + _ID;
409    }
410
411    public interface MimetypesColumns {
412        public static final String _ID = BaseColumns._ID;
413        public static final String MIMETYPE = "mimetype";
414
415        public static final String CONCRETE_ID = Tables.MIMETYPES + "." + BaseColumns._ID;
416        public static final String CONCRETE_MIMETYPE = Tables.MIMETYPES + "." + MIMETYPE;
417    }
418
419    public interface AggregationExceptionColumns {
420        public static final String _ID = BaseColumns._ID;
421    }
422
423    public interface NicknameLookupColumns {
424        public static final String NAME = "name";
425        public static final String CLUSTER = "cluster";
426    }
427
428    public interface SettingsColumns {
429        public static final String CONCRETE_ACCOUNT_NAME = Tables.SETTINGS + "."
430                + Settings.ACCOUNT_NAME;
431        public static final String CONCRETE_ACCOUNT_TYPE = Tables.SETTINGS + "."
432                + Settings.ACCOUNT_TYPE;
433    }
434
435    public interface PresenceColumns {
436        String RAW_CONTACT_ID = "presence_raw_contact_id";
437        String CONTACT_ID = "presence_contact_id";
438    }
439
440    public interface AggregatedPresenceColumns {
441        String CONTACT_ID = "presence_contact_id";
442    }
443
444    public interface StatusUpdatesColumns {
445        String DATA_ID = "status_update_data_id";
446    }
447
448    /** In-memory cache of previously found mimetype mappings */
449    private final HashMap<String, Long> mMimetypeCache = new HashMap<String, Long>();
450    /** In-memory cache of previously found package name mappings */
451    private final HashMap<String, Long> mPackageCache = new HashMap<String, Long>();
452
453
454    /** Compiled statements for querying and inserting mappings */
455    private SQLiteStatement mMimetypeQuery;
456    private SQLiteStatement mPackageQuery;
457    private SQLiteStatement mContactIdQuery;
458    private SQLiteStatement mAggregationModeQuery;
459    private SQLiteStatement mMimetypeInsert;
460    private SQLiteStatement mPackageInsert;
461    private SQLiteStatement mDataMimetypeQuery;
462    private SQLiteStatement mActivitiesMimetypeQuery;
463
464    private final Context mContext;
465    private final SyncStateContentProviderHelper mSyncState;
466
467
468    /** Compiled statements for updating {@link Contacts#IN_VISIBLE_GROUP}. */
469    private SQLiteStatement mVisibleUpdate;
470    private SQLiteStatement mVisibleSpecificUpdate;
471
472    private boolean mReopenDatabase = false;
473
474    private static ContactsDatabaseHelper sSingleton = null;
475
476    private boolean mUseStrictPhoneNumberComparation;
477
478    /**
479     * List of package names with access to {@link RawContacts#IS_RESTRICTED} data.
480     */
481    private String[] mUnrestrictedPackages;
482
483    public static synchronized ContactsDatabaseHelper getInstance(Context context) {
484        if (sSingleton == null) {
485            sSingleton = new ContactsDatabaseHelper(context);
486        }
487        return sSingleton;
488    }
489
490    /**
491     * Private constructor, callers except unit tests should obtain an instance through
492     * {@link #getInstance(android.content.Context)} instead.
493     */
494    ContactsDatabaseHelper(Context context) {
495        super(context, DATABASE_NAME, null, DATABASE_VERSION);
496        Log.i(TAG, "Creating OpenHelper");
497        Resources resources = context.getResources();
498
499        mContext = context;
500        mSyncState = new SyncStateContentProviderHelper();
501        mUseStrictPhoneNumberComparation =
502                resources.getBoolean(
503                        com.android.internal.R.bool.config_use_strict_phone_number_comparation);
504        mUnrestrictedPackages = resources.getStringArray(
505                resources.getIdentifier("unrestricted_packages", "array",
506                        context.getPackageName()));
507    }
508
509    @Override
510    public void onOpen(SQLiteDatabase db) {
511        mSyncState.onDatabaseOpened(db);
512
513        // Create compiled statements for package and mimetype lookups
514        mMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns._ID + " FROM "
515                + Tables.MIMETYPES + " WHERE " + MimetypesColumns.MIMETYPE + "=?");
516        mPackageQuery = db.compileStatement("SELECT " + PackagesColumns._ID + " FROM "
517                + Tables.PACKAGES + " WHERE " + PackagesColumns.PACKAGE + "=?");
518        mContactIdQuery = db.compileStatement("SELECT " + RawContacts.CONTACT_ID + " FROM "
519                + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=?");
520        mAggregationModeQuery = db.compileStatement("SELECT " + RawContacts.AGGREGATION_MODE
521                + " FROM " + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=?");
522        mMimetypeInsert = db.compileStatement("INSERT INTO " + Tables.MIMETYPES + "("
523                + MimetypesColumns.MIMETYPE + ") VALUES (?)");
524        mPackageInsert = db.compileStatement("INSERT INTO " + Tables.PACKAGES + "("
525                + PackagesColumns.PACKAGE + ") VALUES (?)");
526
527        mDataMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns.MIMETYPE + " FROM "
528                + Tables.DATA_JOIN_MIMETYPES + " WHERE " + Tables.DATA + "." + Data._ID + "=?");
529        mActivitiesMimetypeQuery = db.compileStatement("SELECT " + MimetypesColumns.MIMETYPE
530                + " FROM " + Tables.ACTIVITIES_JOIN_MIMETYPES + " WHERE " + Tables.ACTIVITIES + "."
531                + Activities._ID + "=?");
532
533        // Compile statements for updating visibility
534        final String visibleUpdate = "UPDATE " + Tables.CONTACTS + " SET "
535                + Contacts.IN_VISIBLE_GROUP + "=(" + Clauses.CONTACT_IS_VISIBLE + ")";
536
537        mVisibleUpdate = db.compileStatement(visibleUpdate);
538        mVisibleSpecificUpdate = db.compileStatement(visibleUpdate + " WHERE "
539                + ContactsColumns.CONCRETE_ID + "=?");
540
541        db.execSQL("ATTACH DATABASE ':memory:' AS " + DATABASE_PRESENCE + ";");
542        db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_PRESENCE + "." + Tables.PRESENCE + " ("+
543                StatusUpdates.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," +
544                StatusUpdates.PROTOCOL + " INTEGER NOT NULL," +
545                StatusUpdates.CUSTOM_PROTOCOL + " TEXT," +
546                StatusUpdates.IM_HANDLE + " TEXT," +
547                StatusUpdates.IM_ACCOUNT + " TEXT," +
548                PresenceColumns.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," +
549                PresenceColumns.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
550                StatusUpdates.PRESENCE + " INTEGER," +
551                "UNIQUE(" + StatusUpdates.PROTOCOL + ", " + StatusUpdates.CUSTOM_PROTOCOL
552                    + ", " + StatusUpdates.IM_HANDLE + ", " + StatusUpdates.IM_ACCOUNT + ")" +
553        ");");
554
555        db.execSQL("CREATE INDEX IF NOT EXISTS " + DATABASE_PRESENCE + ".presenceIndex" + " ON "
556                + Tables.PRESENCE + " (" + PresenceColumns.RAW_CONTACT_ID + ");");
557
558        db.execSQL("CREATE TABLE IF NOT EXISTS "
559                        + DATABASE_PRESENCE + "." + Tables.AGGREGATED_PRESENCE + " ("+
560                AggregatedPresenceColumns.CONTACT_ID
561                        + " INTEGER PRIMARY KEY REFERENCES contacts(_id)," +
562                StatusUpdates.PRESENCE_STATUS + " INTEGER" +
563        ");");
564
565
566        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_deleted"
567                + " BEFORE DELETE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
568                + " BEGIN "
569                + "   DELETE FROM " + Tables.AGGREGATED_PRESENCE
570                + "     WHERE " + AggregatedPresenceColumns.CONTACT_ID + " = " +
571                        "(SELECT " + PresenceColumns.CONTACT_ID +
572                        " FROM " + Tables.PRESENCE +
573                        " WHERE " + PresenceColumns.RAW_CONTACT_ID
574                                + "=OLD." + PresenceColumns.RAW_CONTACT_ID +
575                        " AND NOT EXISTS" +
576                                "(SELECT " + PresenceColumns.RAW_CONTACT_ID +
577                                " FROM " + Tables.PRESENCE +
578                                " WHERE " + PresenceColumns.CONTACT_ID
579                                        + "=OLD." + PresenceColumns.CONTACT_ID +
580                                " AND " + PresenceColumns.RAW_CONTACT_ID
581                                        + "!=OLD." + PresenceColumns.RAW_CONTACT_ID + "));"
582                + " END");
583
584        String replaceAggregatePresenceSql =
585            "INSERT OR REPLACE INTO " + Tables.AGGREGATED_PRESENCE + "("
586                    + AggregatedPresenceColumns.CONTACT_ID + ", "
587                    + StatusUpdates.PRESENCE_STATUS + ")" +
588            " SELECT " + PresenceColumns.CONTACT_ID + ","
589                        + "MAX(" + StatusUpdates.PRESENCE_STATUS + ")" +
590                    " FROM " + Tables.PRESENCE +
591                    " WHERE " + PresenceColumns.CONTACT_ID
592                        + "=NEW." + PresenceColumns.CONTACT_ID + ";";
593
594        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_inserted"
595                + " AFTER INSERT ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
596                + " BEGIN "
597                + replaceAggregatePresenceSql
598                + " END");
599
600        db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_updated"
601                + " AFTER UPDATE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
602                + " BEGIN "
603                + replaceAggregatePresenceSql
604                + " END");
605    }
606
607    @Override
608    public void onCreate(SQLiteDatabase db) {
609        Log.i(TAG, "Bootstrapping database");
610
611        mSyncState.createDatabase(db);
612
613        // One row per group of contacts corresponding to the same person
614        db.execSQL("CREATE TABLE " + Tables.CONTACTS + " (" +
615                BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
616                Contacts.DISPLAY_NAME + " TEXT," +
617                Contacts.PHOTO_ID + " INTEGER REFERENCES data(_id)," +
618                Contacts.CUSTOM_RINGTONE + " TEXT," +
619                Contacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," +
620                Contacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
621                Contacts.LAST_TIME_CONTACTED + " INTEGER," +
622                Contacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
623                Contacts.IN_VISIBLE_GROUP + " INTEGER NOT NULL DEFAULT 1," +
624                Contacts.HAS_PHONE_NUMBER + " INTEGER NOT NULL DEFAULT 0," +
625                Contacts.LOOKUP_KEY + " TEXT," +
626                ContactsColumns.LAST_STATUS_UPDATE_ID + " INTEGER REFERENCES data(_id)," +
627                ContactsColumns.SINGLE_IS_RESTRICTED + " INTEGER NOT NULL DEFAULT 0" +
628        ");");
629
630        db.execSQL("CREATE TRIGGER contacts_times_contacted UPDATE OF " +
631                Contacts.LAST_TIME_CONTACTED + " ON " + Tables.CONTACTS + " " +
632            "BEGIN " +
633                "UPDATE " + Tables.CONTACTS + " SET "
634                    + Contacts.TIMES_CONTACTED + " = " + "" +
635                            "(new." + Contacts.TIMES_CONTACTED + " + 1)"
636                    + " WHERE _id = new._id;" +
637            "END");
638
639        db.execSQL("CREATE INDEX contacts_visible_index ON " + Tables.CONTACTS + " (" +
640                Contacts.IN_VISIBLE_GROUP + "," +
641                Contacts.DISPLAY_NAME + " COLLATE LOCALIZED" +
642        ");");
643
644        db.execSQL("CREATE INDEX contacts_has_phone_index ON " + Tables.CONTACTS + " (" +
645                Contacts.HAS_PHONE_NUMBER +
646        ");");
647
648        db.execSQL("CREATE INDEX contacts_restricted_index ON " + Tables.CONTACTS + " (" +
649                ContactsColumns.SINGLE_IS_RESTRICTED +
650        ");");
651
652        // Contacts table
653        db.execSQL("CREATE TABLE " + Tables.RAW_CONTACTS + " (" +
654                RawContacts._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
655                RawContacts.IS_RESTRICTED + " INTEGER DEFAULT 0," +
656                RawContacts.ACCOUNT_NAME + " STRING DEFAULT NULL, " +
657                RawContacts.ACCOUNT_TYPE + " STRING DEFAULT NULL, " +
658                RawContacts.SOURCE_ID + " TEXT," +
659                RawContacts.VERSION + " INTEGER NOT NULL DEFAULT 1," +
660                RawContacts.DIRTY + " INTEGER NOT NULL DEFAULT 0," +
661                RawContacts.DELETED + " INTEGER NOT NULL DEFAULT 0," +
662                RawContacts.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," +
663                RawContacts.AGGREGATION_MODE + " INTEGER NOT NULL DEFAULT " +
664                        RawContacts.AGGREGATION_MODE_DEFAULT + "," +
665                RawContactsColumns.AGGREGATION_NEEDED + " INTEGER NOT NULL DEFAULT 1," +
666                RawContacts.CUSTOM_RINGTONE + " TEXT," +
667                RawContacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," +
668                RawContacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
669                RawContacts.LAST_TIME_CONTACTED + " INTEGER," +
670                RawContacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
671                RawContactsColumns.DISPLAY_NAME + " TEXT," +
672                RawContactsColumns.DISPLAY_NAME_SOURCE + " INTEGER NOT NULL DEFAULT " +
673                        DisplayNameSources.UNDEFINED + "," +
674                RawContacts.SYNC1 + " TEXT, " +
675                RawContacts.SYNC2 + " TEXT, " +
676                RawContacts.SYNC3 + " TEXT, " +
677                RawContacts.SYNC4 + " TEXT " +
678        ");");
679
680        db.execSQL("CREATE TRIGGER raw_contacts_times_contacted UPDATE OF " +
681                    RawContacts.LAST_TIME_CONTACTED + " ON " + Tables.RAW_CONTACTS + " " +
682                "BEGIN " +
683                    "UPDATE " + Tables.RAW_CONTACTS + " SET "
684                        + RawContacts.TIMES_CONTACTED + " = " + "" +
685                            "(new." + RawContacts.TIMES_CONTACTED + " + 1)"
686                        + " WHERE _id = new._id;" +
687                "END");
688
689        db.execSQL("CREATE INDEX raw_contacts_contact_id_index ON " + Tables.RAW_CONTACTS + " (" +
690                RawContacts.CONTACT_ID +
691        ");");
692
693        db.execSQL("CREATE INDEX raw_contacts_source_id_index ON " + Tables.RAW_CONTACTS + " (" +
694                RawContacts.SOURCE_ID + ", " +
695                RawContacts.ACCOUNT_TYPE + ", " +
696                RawContacts.ACCOUNT_NAME +
697        ");");
698
699        // TODO readd the index and investigate a controlled use of it
700//        db.execSQL("CREATE INDEX raw_contacts_agg_index ON " + Tables.RAW_CONTACTS + " (" +
701//                RawContactsColumns.AGGREGATION_NEEDED +
702//        ");");
703
704        // Package name mapping table
705        db.execSQL("CREATE TABLE " + Tables.PACKAGES + " (" +
706                PackagesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
707                PackagesColumns.PACKAGE + " TEXT NOT NULL" +
708        ");");
709
710        // Mimetype mapping table
711        db.execSQL("CREATE TABLE " + Tables.MIMETYPES + " (" +
712                MimetypesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
713                MimetypesColumns.MIMETYPE + " TEXT NOT NULL" +
714        ");");
715
716        // Public generic data table
717        db.execSQL("CREATE TABLE " + Tables.DATA + " (" +
718                Data._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
719                DataColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
720                DataColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," +
721                Data.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
722                Data.IS_PRIMARY + " INTEGER NOT NULL DEFAULT 0," +
723                Data.IS_SUPER_PRIMARY + " INTEGER NOT NULL DEFAULT 0," +
724                Data.DATA_VERSION + " INTEGER NOT NULL DEFAULT 0," +
725                Data.DATA1 + " TEXT," +
726                Data.DATA2 + " TEXT," +
727                Data.DATA3 + " TEXT," +
728                Data.DATA4 + " TEXT," +
729                Data.DATA5 + " TEXT," +
730                Data.DATA6 + " TEXT," +
731                Data.DATA7 + " TEXT," +
732                Data.DATA8 + " TEXT," +
733                Data.DATA9 + " TEXT," +
734                Data.DATA10 + " TEXT," +
735                Data.DATA11 + " TEXT," +
736                Data.DATA12 + " TEXT," +
737                Data.DATA13 + " TEXT," +
738                Data.DATA14 + " TEXT," +
739                Data.DATA15 + " TEXT," +
740                Data.SYNC1 + " TEXT, " +
741                Data.SYNC2 + " TEXT, " +
742                Data.SYNC3 + " TEXT, " +
743                Data.SYNC4 + " TEXT " +
744        ");");
745
746        db.execSQL("CREATE INDEX data_raw_contact_id ON " + Tables.DATA + " (" +
747                Data.RAW_CONTACT_ID +
748        ");");
749
750        /**
751         * For email lookup and similar queries.
752         */
753        db.execSQL("CREATE INDEX data_mimetype_data1_index ON " + Tables.DATA + " (" +
754                DataColumns.MIMETYPE_ID + "," +
755                Data.DATA1 +
756        ");");
757
758        /**
759         * Automatically delete Data rows when a raw contact is deleted.
760         */
761        db.execSQL("CREATE TRIGGER " + Tables.RAW_CONTACTS + "_deleted "
762                + "   BEFORE DELETE ON " + Tables.RAW_CONTACTS
763                + " BEGIN "
764                + "   DELETE FROM " + Tables.DATA
765                + "     WHERE " + Data.RAW_CONTACT_ID
766                                + "=OLD." + RawContacts._ID + ";"
767                + "   DELETE FROM " + Tables.PHONE_LOOKUP
768                + "     WHERE " + PhoneLookupColumns.RAW_CONTACT_ID
769                                + "=OLD." + RawContacts._ID + ";"
770                + "   DELETE FROM " + Tables.NAME_LOOKUP
771                + "     WHERE " + NameLookupColumns.RAW_CONTACT_ID
772                                + "=OLD." + RawContacts._ID + ";"
773                + "   DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS
774                + "     WHERE " + AggregationExceptions.RAW_CONTACT_ID1
775                                + "=OLD." + RawContacts._ID
776                + "        OR " + AggregationExceptions.RAW_CONTACT_ID2
777                                + "=OLD." + RawContacts._ID + ";"
778                + "   DELETE FROM " + Tables.CONTACTS
779                + "     WHERE " + Contacts._ID + "=OLD." + RawContacts.CONTACT_ID
780                + "       AND (SELECT COUNT(*) FROM " + Tables.RAW_CONTACTS
781                + "            WHERE " + RawContacts.CONTACT_ID + "=OLD." + RawContacts.CONTACT_ID
782                + "           )=1;"
783                + " END");
784
785        /**
786         * Triggers that update {@link RawContacts#VERSION} when the contact is
787         * marked for deletion or any time a data row is inserted, updated or
788         * deleted.
789         */
790        db.execSQL("CREATE TRIGGER " + Tables.RAW_CONTACTS + "_marked_deleted "
791                + "   BEFORE UPDATE ON " + Tables.RAW_CONTACTS
792                + " BEGIN "
793                + "   UPDATE " + Tables.RAW_CONTACTS
794                + "     SET "
795                +         RawContacts.VERSION + "=OLD." + RawContacts.VERSION + "+1 "
796                + "     WHERE " + RawContacts._ID + "=OLD." + RawContacts._ID
797                + "       AND NEW." + RawContacts.DELETED + "!= OLD." + RawContacts.DELETED + ";"
798                + " END");
799
800        db.execSQL("CREATE TRIGGER " + Tables.DATA + "_updated BEFORE UPDATE ON " + Tables.DATA
801                + " BEGIN "
802                + "   UPDATE " + Tables.DATA
803                + "     SET " + Data.DATA_VERSION + "=OLD." + Data.DATA_VERSION + "+1 "
804                + "     WHERE " + Data._ID + "=OLD." + Data._ID + ";"
805                + "   UPDATE " + Tables.RAW_CONTACTS
806                + "     SET " +	RawContacts.VERSION + "=" + RawContacts.VERSION + "+1 "
807                + "     WHERE " + RawContacts._ID + "=OLD." + Data.RAW_CONTACT_ID + ";"
808                + " END");
809
810        db.execSQL("CREATE TRIGGER " + Tables.DATA + "_deleted BEFORE DELETE ON " + Tables.DATA
811                + " BEGIN "
812                + "   UPDATE " + Tables.RAW_CONTACTS
813                + "     SET " + RawContacts.VERSION + "=" + RawContacts.VERSION + "+1 "
814                + "     WHERE " + RawContacts._ID + "=OLD." + Data.RAW_CONTACT_ID + ";"
815                + "   DELETE FROM " + Tables.PHONE_LOOKUP
816                + "     WHERE " + PhoneLookupColumns.DATA_ID + "=OLD." + Data._ID + ";"
817                + " END");
818
819        // Private phone numbers table used for lookup
820        db.execSQL("CREATE TABLE " + Tables.PHONE_LOOKUP + " (" +
821                PhoneLookupColumns.DATA_ID
822                        + " INTEGER PRIMARY KEY REFERENCES data(_id) NOT NULL," +
823                PhoneLookupColumns.RAW_CONTACT_ID
824                        + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
825                PhoneLookupColumns.NORMALIZED_NUMBER + " TEXT NOT NULL" +
826        ");");
827
828        db.execSQL("CREATE INDEX phone_lookup_index ON " + Tables.PHONE_LOOKUP + " (" +
829                PhoneLookupColumns.NORMALIZED_NUMBER + "," +
830                PhoneLookupColumns.RAW_CONTACT_ID + "," +
831                PhoneLookupColumns.DATA_ID +
832        ");");
833
834        // Private name/nickname table used for lookup
835        db.execSQL("CREATE TABLE " + Tables.NAME_LOOKUP + " (" +
836                NameLookupColumns.DATA_ID
837                        + " INTEGER REFERENCES data(_id) NOT NULL," +
838                NameLookupColumns.RAW_CONTACT_ID
839                        + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
840                NameLookupColumns.NORMALIZED_NAME + " TEXT NOT NULL," +
841                NameLookupColumns.NAME_TYPE + " INTEGER NOT NULL," +
842                "PRIMARY KEY ("
843                        + NameLookupColumns.DATA_ID + ", "
844                        + NameLookupColumns.NORMALIZED_NAME + ", "
845                        + NameLookupColumns.NAME_TYPE + ")" +
846        ");");
847
848        db.execSQL("CREATE INDEX name_lookup_index ON " + Tables.NAME_LOOKUP + " (" +
849                NameLookupColumns.NORMALIZED_NAME + "," +
850                NameLookupColumns.NAME_TYPE + ", " +
851                NameLookupColumns.RAW_CONTACT_ID +
852        ");");
853
854        db.execSQL("CREATE INDEX name_lookup_raw_contact_id_index ON " + Tables.NAME_LOOKUP + " (" +
855                NameLookupColumns.RAW_CONTACT_ID +
856        ");");
857
858        db.execSQL("CREATE TABLE " + Tables.NICKNAME_LOOKUP + " (" +
859                NicknameLookupColumns.NAME + " TEXT," +
860                NicknameLookupColumns.CLUSTER + " TEXT" +
861        ");");
862
863        db.execSQL("CREATE UNIQUE INDEX nickname_lookup_index ON " + Tables.NICKNAME_LOOKUP + " (" +
864                NicknameLookupColumns.NAME + ", " +
865                NicknameLookupColumns.CLUSTER +
866        ");");
867
868        // Groups table
869        db.execSQL("CREATE TABLE " + Tables.GROUPS + " (" +
870                Groups._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
871                GroupsColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
872                Groups.ACCOUNT_NAME + " STRING DEFAULT NULL, " +
873                Groups.ACCOUNT_TYPE + " STRING DEFAULT NULL, " +
874                Groups.SOURCE_ID + " TEXT," +
875                Groups.VERSION + " INTEGER NOT NULL DEFAULT 1," +
876                Groups.DIRTY + " INTEGER NOT NULL DEFAULT 0," +
877                Groups.TITLE + " TEXT," +
878                Groups.TITLE_RES + " INTEGER," +
879                Groups.NOTES + " TEXT," +
880                Groups.SYSTEM_ID + " TEXT," +
881                Groups.DELETED + " INTEGER NOT NULL DEFAULT 0," +
882                Groups.GROUP_VISIBLE + " INTEGER NOT NULL DEFAULT 0," +
883                Groups.SHOULD_SYNC + " INTEGER NOT NULL DEFAULT 1," +
884                Groups.SYNC1 + " TEXT, " +
885                Groups.SYNC2 + " TEXT, " +
886                Groups.SYNC3 + " TEXT, " +
887                Groups.SYNC4 + " TEXT " +
888        ");");
889
890        db.execSQL("CREATE INDEX groups_source_id_index ON " + Tables.GROUPS + " (" +
891                Groups.SOURCE_ID + ", " +
892                Groups.ACCOUNT_TYPE + ", " +
893                Groups.ACCOUNT_NAME +
894        ");");
895
896        db.execSQL("CREATE TRIGGER " + Tables.GROUPS + "_updated1 "
897                + "   BEFORE UPDATE ON " + Tables.GROUPS
898                + " BEGIN "
899                + "   UPDATE " + Tables.GROUPS
900                + "     SET "
901                +         Groups.VERSION + "=OLD." + Groups.VERSION + "+1"
902                + "     WHERE " + Groups._ID + "=OLD." + Groups._ID + ";"
903                + " END");
904
905        db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.AGGREGATION_EXCEPTIONS + " (" +
906                AggregationExceptionColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
907                AggregationExceptions.TYPE + " INTEGER NOT NULL, " +
908                AggregationExceptions.RAW_CONTACT_ID1
909                        + " INTEGER REFERENCES raw_contacts(_id), " +
910                AggregationExceptions.RAW_CONTACT_ID2
911                        + " INTEGER REFERENCES raw_contacts(_id)" +
912        ");");
913
914        db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS aggregation_exception_index1 ON " +
915                Tables.AGGREGATION_EXCEPTIONS + " (" +
916                AggregationExceptions.RAW_CONTACT_ID1 + ", " +
917                AggregationExceptions.RAW_CONTACT_ID2 +
918        ");");
919
920        db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS aggregation_exception_index2 ON " +
921                Tables.AGGREGATION_EXCEPTIONS + " (" +
922                AggregationExceptions.RAW_CONTACT_ID2 + ", " +
923                AggregationExceptions.RAW_CONTACT_ID1 +
924        ");");
925
926        db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.SETTINGS + " (" +
927                Settings.ACCOUNT_NAME + " STRING NOT NULL," +
928                Settings.ACCOUNT_TYPE + " STRING NOT NULL," +
929                Settings.UNGROUPED_VISIBLE + " INTEGER NOT NULL DEFAULT 0," +
930                Settings.SHOULD_SYNC + " INTEGER NOT NULL DEFAULT 1, " +
931                "PRIMARY KEY (" + Settings.ACCOUNT_NAME + ", " +
932                    Settings.ACCOUNT_TYPE + ") ON CONFLICT REPLACE" +
933        ");");
934
935        // The table for recent calls is here so we can do table joins
936        // on people, phones, and calls all in one place.
937        db.execSQL("CREATE TABLE " + Tables.CALLS + " (" +
938                Calls._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
939                Calls.NUMBER + " TEXT," +
940                Calls.DATE + " INTEGER," +
941                Calls.DURATION + " INTEGER," +
942                Calls.TYPE + " INTEGER," +
943                Calls.NEW + " INTEGER," +
944                Calls.CACHED_NAME + " TEXT," +
945                Calls.CACHED_NUMBER_TYPE + " INTEGER," +
946                Calls.CACHED_NUMBER_LABEL + " TEXT" +
947        ");");
948
949        // Activities table
950        db.execSQL("CREATE TABLE " + Tables.ACTIVITIES + " (" +
951                Activities._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
952                ActivitiesColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
953                ActivitiesColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," +
954                Activities.RAW_ID + " TEXT," +
955                Activities.IN_REPLY_TO + " TEXT," +
956                Activities.AUTHOR_CONTACT_ID +  " INTEGER REFERENCES raw_contacts(_id)," +
957                Activities.TARGET_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
958                Activities.PUBLISHED + " INTEGER NOT NULL," +
959                Activities.THREAD_PUBLISHED + " INTEGER NOT NULL," +
960                Activities.TITLE + " TEXT NOT NULL," +
961                Activities.SUMMARY + " TEXT," +
962                Activities.LINK + " TEXT, " +
963                Activities.THUMBNAIL + " BLOB" +
964        ");");
965
966        db.execSQL("CREATE TABLE " + Tables.STATUS_UPDATES + " (" +
967                StatusUpdatesColumns.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," +
968                StatusUpdates.STATUS + " TEXT," +
969                StatusUpdates.STATUS_TIMESTAMP + " INTEGER," +
970                StatusUpdates.STATUS_RES_PACKAGE + " TEXT, " +
971                StatusUpdates.STATUS_LABEL + " INTEGER, " +
972                StatusUpdates.STATUS_ICON + " INTEGER" +
973        ");");
974
975        String contactEntitiesSelect = "SELECT "
976                + RawContactsColumns.CONCRETE_ACCOUNT_NAME + " AS " + RawContacts.ACCOUNT_NAME + ","
977                + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " AS " + RawContacts.ACCOUNT_TYPE + ","
978                + RawContactsColumns.CONCRETE_SOURCE_ID + " AS " + RawContacts.SOURCE_ID + ","
979                + RawContactsColumns.CONCRETE_VERSION + " AS " + RawContacts.VERSION + ","
980                + RawContactsColumns.CONCRETE_DIRTY + " AS " + RawContacts.DIRTY + ","
981                + RawContactsColumns.CONCRETE_DELETED + " AS " + RawContacts.DELETED + ","
982                + PackagesColumns.PACKAGE + " AS " + Data.RES_PACKAGE + ","
983                + RawContacts.CONTACT_ID + ", "
984                + RawContactsColumns.CONCRETE_SYNC1 + " AS " + RawContacts.SYNC1 + ", "
985                + RawContactsColumns.CONCRETE_SYNC2 + " AS " + RawContacts.SYNC2 + ", "
986                + RawContactsColumns.CONCRETE_SYNC3 + " AS " + RawContacts.SYNC3 + ", "
987                + RawContactsColumns.CONCRETE_SYNC4 + " AS " + RawContacts.SYNC4 + ", "
988                + Data.MIMETYPE + ", "
989                + Data.DATA1 + ", "
990                + Data.DATA2 + ", "
991                + Data.DATA3 + ", "
992                + Data.DATA4 + ", "
993                + Data.DATA5 + ", "
994                + Data.DATA6 + ", "
995                + Data.DATA7 + ", "
996                + Data.DATA8 + ", "
997                + Data.DATA9 + ", "
998                + Data.DATA10 + ", "
999                + Data.DATA11 + ", "
1000                + Data.DATA12 + ", "
1001                + Data.DATA13 + ", "
1002                + Data.DATA14 + ", "
1003                + Data.DATA15 + ", "
1004                + Data.SYNC1 + ", "
1005                + Data.SYNC2 + ", "
1006                + Data.SYNC3 + ", "
1007                + Data.SYNC4 + ", "
1008                + RawContactsColumns.CONCRETE_ID + " AS " + Data.RAW_CONTACT_ID + ", "
1009                + Data.IS_PRIMARY + ", "
1010                + Data.IS_SUPER_PRIMARY + ", "
1011                + Data.DATA_VERSION + ", "
1012                + DataColumns.CONCRETE_ID + " AS " + RawContacts._ID + ","
1013                + RawContactsColumns.CONCRETE_STARRED + " AS " + RawContacts.STARRED + ","
1014                + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID
1015                + " FROM " + Tables.RAW_CONTACTS
1016                + " LEFT OUTER JOIN " + Tables.DATA + " ON ("
1017                +   DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")"
1018                + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON ("
1019                +   DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")"
1020                + " LEFT OUTER JOIN " + Tables.MIMETYPES + " ON ("
1021                +   DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")"
1022                + " LEFT OUTER JOIN " + Tables.GROUPS + " ON ("
1023                +   MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE
1024                +   "' AND " + GroupsColumns.CONCRETE_ID + "="
1025                + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")";
1026
1027        db.execSQL("CREATE VIEW " + Tables.CONTACT_ENTITIES + " AS "
1028                + contactEntitiesSelect);
1029        db.execSQL("CREATE VIEW " + Tables.CONTACT_ENTITIES_RESTRICTED + " AS "
1030                + contactEntitiesSelect + " WHERE " + RawContacts.IS_RESTRICTED + "=0");
1031
1032        String dataColumns =
1033                Data.IS_PRIMARY + ", "
1034                + Data.IS_SUPER_PRIMARY + ", "
1035                + Data.DATA_VERSION + ", "
1036                + PackagesColumns.PACKAGE + " AS " + Data.RES_PACKAGE + ","
1037                + MimetypesColumns.MIMETYPE + " AS " + Data.MIMETYPE + ", "
1038                + Data.DATA1 + ", "
1039                + Data.DATA2 + ", "
1040                + Data.DATA3 + ", "
1041                + Data.DATA4 + ", "
1042                + Data.DATA5 + ", "
1043                + Data.DATA6 + ", "
1044                + Data.DATA7 + ", "
1045                + Data.DATA8 + ", "
1046                + Data.DATA9 + ", "
1047                + Data.DATA10 + ", "
1048                + Data.DATA11 + ", "
1049                + Data.DATA12 + ", "
1050                + Data.DATA13 + ", "
1051                + Data.DATA14 + ", "
1052                + Data.DATA15 + ", "
1053                + Data.SYNC1 + ", "
1054                + Data.SYNC2 + ", "
1055                + Data.SYNC3 + ", "
1056                + Data.SYNC4;
1057
1058        String syncColumns =
1059                RawContactsColumns.CONCRETE_ACCOUNT_NAME + " AS " + RawContacts.ACCOUNT_NAME + ","
1060                + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " AS " + RawContacts.ACCOUNT_TYPE + ","
1061                + RawContactsColumns.CONCRETE_SOURCE_ID + " AS " + RawContacts.SOURCE_ID + ","
1062                + RawContactsColumns.CONCRETE_VERSION + " AS " + RawContacts.VERSION + ","
1063                + RawContactsColumns.CONCRETE_DIRTY + " AS " + RawContacts.DIRTY + ","
1064                + RawContactsColumns.CONCRETE_SYNC1 + " AS " + RawContacts.SYNC1 + ","
1065                + RawContactsColumns.CONCRETE_SYNC2 + " AS " + RawContacts.SYNC2 + ","
1066                + RawContactsColumns.CONCRETE_SYNC3 + " AS " + RawContacts.SYNC3 + ","
1067                + RawContactsColumns.CONCRETE_SYNC4 + " AS " + RawContacts.SYNC4;
1068
1069        String contactOptionColumns =
1070                ContactsColumns.CONCRETE_CUSTOM_RINGTONE
1071                        + " AS " + RawContacts.CUSTOM_RINGTONE + ","
1072                + ContactsColumns.CONCRETE_SEND_TO_VOICEMAIL
1073                        + " AS " + RawContacts.SEND_TO_VOICEMAIL + ","
1074                + ContactsColumns.CONCRETE_LAST_TIME_CONTACTED
1075                        + " AS " + RawContacts.LAST_TIME_CONTACTED + ","
1076                + ContactsColumns.CONCRETE_TIMES_CONTACTED
1077                        + " AS " + RawContacts.TIMES_CONTACTED + ","
1078                + ContactsColumns.CONCRETE_STARRED
1079                        + " AS " + RawContacts.STARRED;
1080
1081        String dataSelect = "SELECT "
1082                + DataColumns.CONCRETE_ID + " AS " + Data._ID + ","
1083                + Data.RAW_CONTACT_ID + ", "
1084                + RawContacts.CONTACT_ID + ", "
1085                + syncColumns + ", "
1086                + dataColumns + ", "
1087                + contactOptionColumns + ", "
1088                + ContactsColumns.CONCRETE_DISPLAY_NAME + " AS " + Contacts.DISPLAY_NAME + ", "
1089                + Contacts.LOOKUP_KEY + ", "
1090                + Contacts.PHOTO_ID + ", "
1091                + ContactsColumns.LAST_STATUS_UPDATE_ID + ", "
1092                + Tables.GROUPS + "." + Groups.SOURCE_ID + " AS " + GroupMembership.GROUP_SOURCE_ID
1093                + " FROM " + Tables.DATA
1094                + " LEFT OUTER JOIN " + Tables.PACKAGES + " ON ("
1095                +   DataColumns.CONCRETE_PACKAGE_ID + "=" + PackagesColumns.CONCRETE_ID + ")"
1096                + " LEFT OUTER JOIN " + Tables.MIMETYPES + " ON ("
1097                +   DataColumns.CONCRETE_MIMETYPE_ID + "=" + MimetypesColumns.CONCRETE_ID + ")"
1098                + " LEFT OUTER JOIN " + Tables.RAW_CONTACTS + " ON ("
1099                +   DataColumns.CONCRETE_RAW_CONTACT_ID + "=" + RawContactsColumns.CONCRETE_ID + ")"
1100                + " LEFT OUTER JOIN " + Tables.GROUPS + " ON ("
1101                +   MimetypesColumns.CONCRETE_MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE
1102                +   "' AND " + GroupsColumns.CONCRETE_ID + "="
1103                        + Tables.DATA + "." + GroupMembership.GROUP_ROW_ID + ")"
1104                + " LEFT OUTER JOIN " + Tables.CONTACTS + " ON ("
1105                +   RawContacts.CONTACT_ID + "=" + Tables.CONTACTS + "." + Contacts._ID + ")";
1106
1107        db.execSQL("CREATE VIEW " + Views.DATA_ALL + " AS " + dataSelect);
1108        db.execSQL("CREATE VIEW " + Views.DATA_RESTRICTED + " AS " + dataSelect + " WHERE "
1109                + RawContacts.IS_RESTRICTED + "=0");
1110
1111        String rawContactOptionColumns =
1112                RawContacts.CUSTOM_RINGTONE + ","
1113                + RawContacts.SEND_TO_VOICEMAIL + ","
1114                + RawContacts.LAST_TIME_CONTACTED + ","
1115                + RawContacts.TIMES_CONTACTED + ","
1116                + RawContacts.STARRED;
1117
1118        String rawContactsSelect = "SELECT "
1119                + RawContactsColumns.CONCRETE_ID + " AS " + RawContacts._ID + ","
1120                + RawContacts.CONTACT_ID + ", "
1121                + RawContacts.AGGREGATION_MODE + ", "
1122                + RawContacts.DELETED + ", "
1123                + rawContactOptionColumns + ", "
1124                + syncColumns
1125                + " FROM " + Tables.RAW_CONTACTS;
1126
1127        db.execSQL("CREATE VIEW " + Views.RAW_CONTACTS_ALL + " AS " + rawContactsSelect);
1128        db.execSQL("CREATE VIEW " + Views.RAW_CONTACTS_RESTRICTED + " AS " + rawContactsSelect
1129                + " WHERE " + RawContacts.IS_RESTRICTED + "=0");
1130
1131        String contactsColumns =
1132                ContactsColumns.CONCRETE_CUSTOM_RINGTONE
1133                        + " AS " + Contacts.CUSTOM_RINGTONE + ", "
1134                + ContactsColumns.CONCRETE_DISPLAY_NAME
1135                        + " AS " + Contacts.DISPLAY_NAME + ", "
1136                + Contacts.IN_VISIBLE_GROUP + ", "
1137                + Contacts.HAS_PHONE_NUMBER + ", "
1138                + Contacts.LOOKUP_KEY + ", "
1139                + Contacts.PHOTO_ID + ", "
1140                + ContactsColumns.CONCRETE_LAST_TIME_CONTACTED
1141                        + " AS " + Contacts.LAST_TIME_CONTACTED + ", "
1142                + ContactsColumns.CONCRETE_SEND_TO_VOICEMAIL
1143                        + " AS " + Contacts.SEND_TO_VOICEMAIL + ", "
1144                + ContactsColumns.CONCRETE_STARRED
1145                        + " AS " + Contacts.STARRED + ", "
1146                + ContactsColumns.CONCRETE_TIMES_CONTACTED
1147                        + " AS " + Contacts.TIMES_CONTACTED + ", "
1148                + ContactsColumns.LAST_STATUS_UPDATE_ID;
1149
1150        String contactsSelect = "SELECT "
1151                + ContactsColumns.CONCRETE_ID + " AS " + Contacts._ID + ","
1152                + contactsColumns
1153                + " FROM " + Tables.CONTACTS;
1154
1155        String restrictedContactsSelect = "SELECT "
1156                + ContactsColumns.CONCRETE_ID + " AS " + Contacts._ID + ","
1157                + contactsColumns
1158                + " FROM " + Tables.CONTACTS
1159                + " WHERE " + ContactsColumns.SINGLE_IS_RESTRICTED + "=0";
1160
1161        db.execSQL("CREATE VIEW " + Views.CONTACTS_ALL + " AS " + contactsSelect);
1162        db.execSQL("CREATE VIEW " + Views.CONTACTS_RESTRICTED + " AS " + restrictedContactsSelect);
1163
1164        String groupsColumns =
1165                Groups.ACCOUNT_NAME + ","
1166                + Groups.ACCOUNT_TYPE + ","
1167                + Groups.SOURCE_ID + ","
1168                + Groups.VERSION + ","
1169                + Groups.DIRTY + ","
1170                + Groups.TITLE + ","
1171                + Groups.TITLE_RES + ","
1172                + Groups.NOTES + ","
1173                + Groups.SYSTEM_ID + ","
1174                + Groups.DELETED + ","
1175                + Groups.GROUP_VISIBLE + ","
1176                + Groups.SHOULD_SYNC + ","
1177                + Groups.SYNC1 + ","
1178                + Groups.SYNC2 + ","
1179                + Groups.SYNC3 + ","
1180                + Groups.SYNC4 + ","
1181                + PackagesColumns.PACKAGE + " AS " + Groups.RES_PACKAGE;
1182
1183        String groupsSelect = "SELECT "
1184                + GroupsColumns.CONCRETE_ID + " AS " + Groups._ID + ","
1185                + groupsColumns
1186                + " FROM " + Tables.GROUPS_JOIN_PACKAGES;
1187
1188        db.execSQL("CREATE VIEW " + Views.GROUPS_ALL + " AS " + groupsSelect);
1189
1190        loadNicknameLookupTable(db);
1191
1192        // Add the legacy API support views, etc
1193        LegacyApiSupport.createDatabase(db);
1194
1195        // This will create a sqlite_stat1 table that is used for query optimization
1196        db.execSQL("ANALYZE;");
1197
1198        updateSqliteStats(db);
1199
1200        // We need to close and reopen the database connection so that the stats are
1201        // taken into account. Make a note of it and do the actual reopening in the
1202        // getWritableDatabase method.
1203        mReopenDatabase = true;
1204
1205        ContentResolver.requestSync(null /* all accounts */,
1206                ContactsContract.AUTHORITY, new Bundle());
1207    }
1208
1209    @Override
1210    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
1211
1212        Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion
1213                + ", data will be lost!");
1214
1215        db.execSQL("DROP TABLE IF EXISTS " + Tables.CONTACTS + ";");
1216        db.execSQL("DROP TABLE IF EXISTS " + Tables.RAW_CONTACTS + ";");
1217        db.execSQL("DROP TABLE IF EXISTS " + Tables.PACKAGES + ";");
1218        db.execSQL("DROP TABLE IF EXISTS " + Tables.MIMETYPES + ";");
1219        db.execSQL("DROP TABLE IF EXISTS " + Tables.DATA + ";");
1220        db.execSQL("DROP TABLE IF EXISTS " + Tables.PHONE_LOOKUP + ";");
1221        db.execSQL("DROP TABLE IF EXISTS " + Tables.NAME_LOOKUP + ";");
1222        db.execSQL("DROP TABLE IF EXISTS " + Tables.NICKNAME_LOOKUP + ";");
1223        db.execSQL("DROP TABLE IF EXISTS " + Tables.GROUPS + ";");
1224        db.execSQL("DROP TABLE IF EXISTS " + Tables.ACTIVITIES + ";");
1225        db.execSQL("DROP TABLE IF EXISTS " + Tables.CALLS + ";");
1226        db.execSQL("DROP TABLE IF EXISTS " + Tables.SETTINGS + ";");
1227        db.execSQL("DROP TABLE IF EXISTS " + Tables.STATUS_UPDATES + ";");
1228
1229        db.execSQL("DROP VIEW IF EXISTS " + Tables.CONTACT_ENTITIES + ";");
1230        db.execSQL("DROP VIEW IF EXISTS " + Tables.CONTACT_ENTITIES_RESTRICTED + ";");
1231        db.execSQL("DROP VIEW IF EXISTS " + Views.CONTACTS_ALL + ";");
1232        db.execSQL("DROP VIEW IF EXISTS " + Views.CONTACTS_RESTRICTED + ";");
1233        db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_ALL + ";");
1234        db.execSQL("DROP VIEW IF EXISTS " + Views.DATA_RESTRICTED + ";");
1235        db.execSQL("DROP VIEW IF EXISTS " + Views.RAW_CONTACTS_ALL + ";");
1236        db.execSQL("DROP VIEW IF EXISTS " + Views.RAW_CONTACTS_RESTRICTED + ";");
1237        db.execSQL("DROP VIEW IF EXISTS " + Views.GROUPS_ALL + ";");
1238
1239        // TODO: we should not be dropping agg_exceptions and contact_options. In case that table's
1240        // schema changes, we should try to preserve the data, because it was entered by the user
1241        // and has never been synched to the server.
1242        db.execSQL("DROP TABLE IF EXISTS " + Tables.AGGREGATION_EXCEPTIONS + ";");
1243
1244        onCreate(db);
1245    }
1246
1247    /**
1248     * Adds index stats into the SQLite database to force it to always use the lookup indexes.
1249     */
1250    private void updateSqliteStats(SQLiteDatabase db) {
1251
1252        // Specific stats strings are based on an actual large database after running ANALYZE
1253        try {
1254            updateIndexStats(db, Tables.CONTACTS,
1255                    "contacts_restricted_index", "10000 9000");
1256            updateIndexStats(db, Tables.CONTACTS,
1257                    "contacts_has_phone_index", "10000 500");
1258            updateIndexStats(db, Tables.CONTACTS,
1259                    "contacts_visible_index", "10000 500 1");
1260
1261            updateIndexStats(db, Tables.RAW_CONTACTS,
1262                    "raw_contacts_source_id_index", "10000 1 1 1");
1263            updateIndexStats(db, Tables.RAW_CONTACTS,
1264                    "raw_contacts_contact_id_index", "10000 2");
1265
1266            updateIndexStats(db, Tables.NAME_LOOKUP,
1267                    "name_lookup_raw_contact_id_index", "10000 3");
1268            updateIndexStats(db, Tables.NAME_LOOKUP,
1269                    "name_lookup_index", "10000 3 2 2");
1270            updateIndexStats(db, Tables.NAME_LOOKUP,
1271                    "sqlite_autoindex_name_lookup_1", "10000 3 2 1");
1272
1273            updateIndexStats(db, Tables.PHONE_LOOKUP,
1274                    "phone_lookup_index", "10000 2 2 1");
1275
1276            updateIndexStats(db, Tables.DATA,
1277                    "data_mimetype_data1_index", "60000 5000 2");
1278            updateIndexStats(db, Tables.DATA,
1279                    "data_raw_contact_id", "60000 10");
1280
1281            updateIndexStats(db, Tables.GROUPS,
1282                    "groups_source_id_index", "50 1 1 1");
1283
1284            updateIndexStats(db, Tables.NICKNAME_LOOKUP,
1285                    "sqlite_autoindex_name_lookup_1", "500 2 1");
1286
1287        } catch (SQLException e) {
1288            Log.e(TAG, "Could not update index stats", e);
1289        }
1290    }
1291
1292    /**
1293     * Stores statistics for a given index.
1294     *
1295     * @param stats has the following structure: the first index is the expected size of
1296     * the table.  The following integer(s) are the expected number of records selected with the
1297     * index.  There should be one integer per indexed column.
1298     */
1299    private void updateIndexStats(SQLiteDatabase db, String table, String index, String stats) {
1300        db.execSQL("DELETE FROM sqlite_stat1 WHERE tbl='" + table + "' AND idx='" + index + "';");
1301        db.execSQL("INSERT INTO sqlite_stat1 (tbl,idx,stat)"
1302                + " VALUES ('" + table + "','" + index + "','" + stats + "');");
1303    }
1304
1305    @Override
1306    public synchronized SQLiteDatabase getWritableDatabase() {
1307        SQLiteDatabase db = super.getWritableDatabase();
1308        if (mReopenDatabase) {
1309            mReopenDatabase = false;
1310            close();
1311            db = super.getWritableDatabase();
1312        }
1313        return db;
1314    }
1315
1316    /**
1317     * Wipes all data except mime type and package lookup tables.
1318     */
1319    public void wipeData() {
1320        SQLiteDatabase db = getWritableDatabase();
1321
1322        db.execSQL("DELETE FROM " + Tables.CONTACTS + ";");
1323        db.execSQL("DELETE FROM " + Tables.RAW_CONTACTS + ";");
1324        db.execSQL("DELETE FROM " + Tables.DATA + ";");
1325        db.execSQL("DELETE FROM " + Tables.PHONE_LOOKUP + ";");
1326        db.execSQL("DELETE FROM " + Tables.NAME_LOOKUP + ";");
1327        db.execSQL("DELETE FROM " + Tables.GROUPS + ";");
1328        db.execSQL("DELETE FROM " + Tables.AGGREGATION_EXCEPTIONS + ";");
1329        db.execSQL("DELETE FROM " + Tables.SETTINGS + ";");
1330        db.execSQL("DELETE FROM " + Tables.ACTIVITIES + ";");
1331        db.execSQL("DELETE FROM " + Tables.CALLS + ";");
1332
1333        // Note: we are not removing reference data from Tables.NICKNAME_LOOKUP
1334
1335        db.execSQL("VACUUM;");
1336    }
1337
1338    /**
1339     * Return the {@link ApplicationInfo#uid} for the given package name.
1340     */
1341    public static int getUidForPackageName(PackageManager pm, String packageName) {
1342        try {
1343            ApplicationInfo clientInfo = pm.getApplicationInfo(packageName, 0 /* no flags */);
1344            return clientInfo.uid;
1345        } catch (NameNotFoundException e) {
1346            throw new RuntimeException(e);
1347        }
1348    }
1349
1350    /**
1351     * Perform an internal string-to-integer lookup using the compiled
1352     * {@link SQLiteStatement} provided, using the in-memory cache to speed up
1353     * lookups. If a mapping isn't found in cache or database, it will be
1354     * created. All new, uncached answers are added to the cache automatically.
1355     *
1356     * @param query Compiled statement used to query for the mapping.
1357     * @param insert Compiled statement used to insert a new mapping when no
1358     *            existing one is found in cache or from query.
1359     * @param value Value to find mapping for.
1360     * @param cache In-memory cache of previous answers.
1361     * @return An unique integer mapping for the given value.
1362     */
1363    private synchronized long getCachedId(SQLiteStatement query, SQLiteStatement insert,
1364            String value, HashMap<String, Long> cache) {
1365        // Try an in-memory cache lookup
1366        if (cache.containsKey(value)) {
1367            return cache.get(value);
1368        }
1369
1370        long id = -1;
1371        try {
1372            // Try searching database for mapping
1373            DatabaseUtils.bindObjectToProgram(query, 1, value);
1374            id = query.simpleQueryForLong();
1375        } catch (SQLiteDoneException e) {
1376            // Nothing found, so try inserting new mapping
1377            DatabaseUtils.bindObjectToProgram(insert, 1, value);
1378            id = insert.executeInsert();
1379        }
1380
1381        if (id != -1) {
1382            // Cache and return the new answer
1383            cache.put(value, id);
1384            return id;
1385        } else {
1386            // Otherwise throw if no mapping found or created
1387            throw new IllegalStateException("Couldn't find or create internal "
1388                    + "lookup table entry for value " + value);
1389        }
1390    }
1391
1392    /**
1393     * Convert a package name into an integer, using {@link Tables#PACKAGES} for
1394     * lookups and possible allocation of new IDs as needed.
1395     */
1396    public long getPackageId(String packageName) {
1397        // Make sure compiled statements are ready by opening database
1398        getReadableDatabase();
1399        return getCachedId(mPackageQuery, mPackageInsert, packageName, mPackageCache);
1400    }
1401
1402    /**
1403     * Convert a mimetype into an integer, using {@link Tables#MIMETYPES} for
1404     * lookups and possible allocation of new IDs as needed.
1405     */
1406    public long getMimeTypeId(String mimetype) {
1407        // Make sure compiled statements are ready by opening database
1408        getReadableDatabase();
1409        return getCachedId(mMimetypeQuery, mMimetypeInsert, mimetype, mMimetypeCache);
1410    }
1411
1412    /**
1413     * Find the mimetype for the given {@link Data#_ID}.
1414     */
1415    public String getDataMimeType(long dataId) {
1416        // Make sure compiled statements are ready by opening database
1417        getReadableDatabase();
1418        try {
1419            // Try database query to find mimetype
1420            DatabaseUtils.bindObjectToProgram(mDataMimetypeQuery, 1, dataId);
1421            String mimetype = mDataMimetypeQuery.simpleQueryForString();
1422            return mimetype;
1423        } catch (SQLiteDoneException e) {
1424            // No valid mapping found, so return null
1425            return null;
1426        }
1427    }
1428
1429    /**
1430     * Find the mime-type for the given {@link Activities#_ID}.
1431     */
1432    public String getActivityMimeType(long activityId) {
1433        // Make sure compiled statements are ready by opening database
1434        getReadableDatabase();
1435        try {
1436            // Try database query to find mimetype
1437            DatabaseUtils.bindObjectToProgram(mActivitiesMimetypeQuery, 1, activityId);
1438            String mimetype = mActivitiesMimetypeQuery.simpleQueryForString();
1439            return mimetype;
1440        } catch (SQLiteDoneException e) {
1441            // No valid mapping found, so return null
1442            return null;
1443        }
1444    }
1445
1446    /**
1447     * Update {@link Contacts#IN_VISIBLE_GROUP} for all contacts.
1448     */
1449    public void updateAllVisible() {
1450        final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
1451        mVisibleUpdate.bindLong(1, groupMembershipMimetypeId);
1452        mVisibleUpdate.execute();
1453    }
1454
1455    /**
1456     * Update {@link Contacts#IN_VISIBLE_GROUP} for a specific contact.
1457     */
1458    public void updateContactVisible(long aggId) {
1459        final long groupMembershipMimetypeId = getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
1460        mVisibleSpecificUpdate.bindLong(1, groupMembershipMimetypeId);
1461        mVisibleSpecificUpdate.bindLong(2, aggId);
1462        mVisibleSpecificUpdate.execute();
1463    }
1464
1465    /**
1466     * Returns contact ID for the given contact or zero if it is NULL.
1467     */
1468    public long getContactId(long rawContactId) {
1469        getReadableDatabase();
1470        try {
1471            DatabaseUtils.bindObjectToProgram(mContactIdQuery, 1, rawContactId);
1472            return mContactIdQuery.simpleQueryForLong();
1473        } catch (SQLiteDoneException e) {
1474            // No valid mapping found, so return 0
1475            return 0;
1476        }
1477    }
1478
1479    public int getAggregationMode(long rawContactId) {
1480        getReadableDatabase();
1481        try {
1482            DatabaseUtils.bindObjectToProgram(mAggregationModeQuery, 1, rawContactId);
1483            return (int)mAggregationModeQuery.simpleQueryForLong();
1484        } catch (SQLiteDoneException e) {
1485            // No valid row found, so return "disabled"
1486            return RawContacts.AGGREGATION_MODE_DISABLED;
1487        }
1488    }
1489
1490    public void buildPhoneLookupAndRawContactQuery(SQLiteQueryBuilder qb, String number) {
1491        String normalizedNumber = PhoneNumberUtils.toCallerIDMinMatch(number);
1492        qb.setTables(Tables.DATA_JOIN_RAW_CONTACTS +
1493                " JOIN " + Tables.PHONE_LOOKUP
1494                + " ON(" + DataColumns.CONCRETE_ID + "=" + PhoneLookupColumns.DATA_ID + ")");
1495
1496        StringBuilder sb = new StringBuilder();
1497        sb.append(PhoneLookupColumns.NORMALIZED_NUMBER + " GLOB '");
1498        sb.append(normalizedNumber);
1499        sb.append("*' AND PHONE_NUMBERS_EQUAL(data." + Phone.NUMBER + ", ");
1500        DatabaseUtils.appendEscapedSQLString(sb, number);
1501        sb.append(mUseStrictPhoneNumberComparation ? ", 1)" : ", 0)");
1502
1503        qb.appendWhere(sb.toString());
1504    }
1505
1506    public void buildPhoneLookupAndContactQuery(SQLiteQueryBuilder qb, String number) {
1507        String normalizedNumber = PhoneNumberUtils.toCallerIDMinMatch(number);
1508        StringBuilder sb = new StringBuilder();
1509        appendPhoneLookupTables(sb, normalizedNumber, true);
1510        qb.setTables(sb.toString());
1511
1512        sb = new StringBuilder();
1513        appendPhoneLookupSelection(sb, number);
1514        qb.appendWhere(sb.toString());
1515    }
1516
1517    public String buildPhoneLookupAsNestedQuery(String number) {
1518        StringBuilder sb = new StringBuilder();
1519        final String normalizedNumber = PhoneNumberUtils.toCallerIDMinMatch(number);
1520        sb.append("(SELECT DISTINCT raw_contact_id" + " FROM ");
1521        appendPhoneLookupTables(sb, normalizedNumber, false);
1522        sb.append(" WHERE ");
1523        appendPhoneLookupSelection(sb, number);
1524        sb.append(")");
1525        return sb.toString();
1526    }
1527
1528    private void appendPhoneLookupTables(StringBuilder sb, final String normalizedNumber,
1529            boolean joinContacts) {
1530        sb.append(Tables.RAW_CONTACTS);
1531        if (joinContacts) {
1532            sb.append(" JOIN " + getContactView() + " contacts"
1533                    + " ON (contacts._id = raw_contacts.contact_id)");
1534        }
1535        sb.append(", (SELECT data_id FROM phone_lookup "
1536                + "WHERE (phone_lookup.normalized_number GLOB '");
1537        sb.append(normalizedNumber);
1538        sb.append("*')) AS lookup, " + Tables.DATA);
1539    }
1540
1541    private void appendPhoneLookupSelection(StringBuilder sb, String number) {
1542        sb.append("lookup.data_id=data._id AND data.raw_contact_id=raw_contacts._id"
1543                + " AND PHONE_NUMBERS_EQUAL(data." + Phone.NUMBER + ", ");
1544        DatabaseUtils.appendEscapedSQLString(sb, number);
1545        sb.append(mUseStrictPhoneNumberComparation ? ", 1)" : ", 0)");
1546    }
1547
1548    /**
1549     * Loads common nickname mappings into the database.
1550     */
1551    private void loadNicknameLookupTable(SQLiteDatabase db) {
1552        String[] strings = mContext.getResources().getStringArray(
1553                com.android.internal.R.array.common_nicknames);
1554        if (strings == null || strings.length == 0) {
1555            return;
1556        }
1557
1558        SQLiteStatement nicknameLookupInsert = db.compileStatement("INSERT INTO "
1559                + Tables.NICKNAME_LOOKUP + "(" + NicknameLookupColumns.NAME + ","
1560                + NicknameLookupColumns.CLUSTER + ") VALUES (?,?)");
1561
1562        for (int clusterId = 0; clusterId < strings.length; clusterId++) {
1563            String[] names = strings[clusterId].split(",");
1564            for (int j = 0; j < names.length; j++) {
1565                String name = NameNormalizer.normalize(names[j]);
1566                try {
1567                    DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 1, name);
1568                    DatabaseUtils.bindObjectToProgram(nicknameLookupInsert, 2,
1569                            String.valueOf(clusterId));
1570                    nicknameLookupInsert.executeInsert();
1571                } catch (SQLiteException e) {
1572
1573                    // Print the exception and keep going - this is not a fatal error
1574                    Log.e(TAG, "Cannot insert nickname: " + names[j], e);
1575                }
1576            }
1577        }
1578    }
1579
1580    public static void copyStringValue(ContentValues toValues, String toKey,
1581            ContentValues fromValues, String fromKey) {
1582        if (fromValues.containsKey(fromKey)) {
1583            toValues.put(toKey, fromValues.getAsString(fromKey));
1584        }
1585    }
1586
1587    public static void copyLongValue(ContentValues toValues, String toKey,
1588            ContentValues fromValues, String fromKey) {
1589        if (fromValues.containsKey(fromKey)) {
1590            long longValue;
1591            Object value = fromValues.get(fromKey);
1592            if (value instanceof Boolean) {
1593                if ((Boolean)value) {
1594                    longValue = 1;
1595                } else {
1596                    longValue = 0;
1597                }
1598            } else if (value instanceof String) {
1599                longValue = Long.parseLong((String)value);
1600            } else {
1601                longValue = ((Number)value).longValue();
1602            }
1603            toValues.put(toKey, longValue);
1604        }
1605    }
1606
1607    public SyncStateContentProviderHelper getSyncState() {
1608        return mSyncState;
1609    }
1610
1611    /**
1612     * Delete the aggregate contact if it has no constituent raw contacts other
1613     * than the supplied one.
1614     */
1615    public void removeContactIfSingleton(long rawContactId) {
1616        SQLiteDatabase db = getWritableDatabase();
1617
1618        // Obtain contact ID from the supplied raw contact ID
1619        String contactIdFromRawContactId = "(SELECT " + RawContacts.CONTACT_ID + " FROM "
1620                + Tables.RAW_CONTACTS + " WHERE " + RawContacts._ID + "=" + rawContactId + ")";
1621
1622        // Find other raw contacts in the same aggregate contact
1623        String otherRawContacts = "(SELECT contacts1." + RawContacts._ID + " FROM "
1624                + Tables.RAW_CONTACTS + " contacts1 JOIN " + Tables.RAW_CONTACTS + " contacts2 ON ("
1625                + "contacts1." + RawContacts.CONTACT_ID + "=contacts2." + RawContacts.CONTACT_ID
1626                + ") WHERE contacts1." + RawContacts._ID + "!=" + rawContactId + ""
1627                + " AND contacts2." + RawContacts._ID + "=" + rawContactId + ")";
1628
1629        db.execSQL("DELETE FROM " + Tables.CONTACTS
1630                + " WHERE " + Contacts._ID + "=" + contactIdFromRawContactId
1631                + " AND NOT EXISTS " + otherRawContacts + ";");
1632    }
1633
1634    /**
1635     * Check if {@link Binder#getCallingUid()} should be allowed access to
1636     * {@link RawContacts#IS_RESTRICTED} data.
1637     */
1638    boolean hasAccessToRestrictedData() {
1639        final PackageManager pm = mContext.getPackageManager();
1640        final String[] callerPackages = pm.getPackagesForUid(Binder.getCallingUid());
1641
1642        // Has restricted access if caller matches any packages
1643        for (String callerPackage : callerPackages) {
1644            if (hasAccessToRestrictedData(callerPackage)) {
1645                return true;
1646            }
1647        }
1648        return false;
1649    }
1650
1651    /**
1652     * Check if requestingPackage should be allowed access to
1653     * {@link RawContacts#IS_RESTRICTED} data.
1654     */
1655    boolean hasAccessToRestrictedData(String requestingPackage) {
1656        if (mUnrestrictedPackages != null) {
1657            for (String allowedPackage : mUnrestrictedPackages) {
1658                if (allowedPackage.equals(requestingPackage)) {
1659                    return true;
1660                }
1661            }
1662        }
1663        return false;
1664    }
1665
1666    public String getDataView() {
1667        return getDataView(false);
1668    }
1669
1670    public String getDataView(boolean requireRestrictedView) {
1671        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
1672                Views.DATA_ALL : Views.DATA_RESTRICTED;
1673    }
1674
1675    public String getRawContactView() {
1676        return getRawContactView(false);
1677    }
1678
1679    public String getRawContactView(boolean requireRestrictedView) {
1680        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
1681                Views.RAW_CONTACTS_ALL : Views.RAW_CONTACTS_RESTRICTED;
1682    }
1683
1684    public String getContactView() {
1685        return getContactView(false);
1686    }
1687
1688    public String getContactView(boolean requireRestrictedView) {
1689        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
1690                Views.CONTACTS_ALL : Views.CONTACTS_RESTRICTED;
1691    }
1692
1693    public String getGroupView() {
1694        return Views.GROUPS_ALL;
1695    }
1696
1697    public String getContactEntitiesView() {
1698        return getContactEntitiesView(false);
1699    }
1700
1701    public String getContactEntitiesView(boolean requireRestrictedView) {
1702        return (hasAccessToRestrictedData() && !requireRestrictedView) ?
1703                Tables.CONTACT_ENTITIES : Tables.CONTACT_ENTITIES_RESTRICTED;
1704    }
1705
1706    /**
1707     * Test if any of the columns appear in the given projection.
1708     */
1709    public boolean isInProjection(String[] projection, String... columns) {
1710        if (projection == null) {
1711            return true;
1712        }
1713
1714        // Optimized for a single-column test
1715        if (columns.length == 1) {
1716            String column = columns[0];
1717            for (String test : projection) {
1718                if (column.equals(test)) {
1719                    return true;
1720                }
1721            }
1722        } else {
1723            for (String test : projection) {
1724                for (String column : columns) {
1725                    if (column.equals(test)) {
1726                        return true;
1727                    }
1728                }
1729            }
1730        }
1731        return false;
1732    }
1733}
1734