ContactsProvider2.java revision 33fd566fb6eebdd40a900c0c8a2f6dca894d7829
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;
20import com.android.providers.contacts.ContactLookupKey.LookupKeySegment;
21import com.android.providers.contacts.ContactsDatabaseHelper.AggregatedPresenceColumns;
22import com.android.providers.contacts.ContactsDatabaseHelper.AggregationExceptionColumns;
23import com.android.providers.contacts.ContactsDatabaseHelper.Clauses;
24import com.android.providers.contacts.ContactsDatabaseHelper.ContactsColumns;
25import com.android.providers.contacts.ContactsDatabaseHelper.ContactsStatusUpdatesColumns;
26import com.android.providers.contacts.ContactsDatabaseHelper.DataColumns;
27import com.android.providers.contacts.ContactsDatabaseHelper.GroupsColumns;
28import com.android.providers.contacts.ContactsDatabaseHelper.MimetypesColumns;
29import com.android.providers.contacts.ContactsDatabaseHelper.NameLookupColumns;
30import com.android.providers.contacts.ContactsDatabaseHelper.NameLookupType;
31import com.android.providers.contacts.ContactsDatabaseHelper.PhoneColumns;
32import com.android.providers.contacts.ContactsDatabaseHelper.PhoneLookupColumns;
33import com.android.providers.contacts.ContactsDatabaseHelper.PresenceColumns;
34import com.android.providers.contacts.ContactsDatabaseHelper.RawContactsColumns;
35import com.android.providers.contacts.ContactsDatabaseHelper.SettingsColumns;
36import com.android.providers.contacts.ContactsDatabaseHelper.StatusUpdatesColumns;
37import com.android.providers.contacts.ContactsDatabaseHelper.Tables;
38import com.google.android.collect.Lists;
39import com.google.android.collect.Maps;
40import com.google.android.collect.Sets;
41
42import android.accounts.Account;
43import android.accounts.AccountManager;
44import android.accounts.OnAccountsUpdateListener;
45import android.app.Notification;
46import android.app.NotificationManager;
47import android.app.PendingIntent;
48import android.app.SearchManager;
49import android.content.ContentProviderOperation;
50import android.content.ContentProviderResult;
51import android.content.ContentResolver;
52import android.content.ContentUris;
53import android.content.ContentValues;
54import android.content.Context;
55import android.content.IContentService;
56import android.content.Intent;
57import android.content.OperationApplicationException;
58import android.content.SharedPreferences;
59import android.content.SyncAdapterType;
60import android.content.UriMatcher;
61import android.content.SharedPreferences.Editor;
62import android.content.res.AssetFileDescriptor;
63import android.content.res.Configuration;
64import android.database.CharArrayBuffer;
65import android.database.Cursor;
66import android.database.CursorWrapper;
67import android.database.DatabaseUtils;
68import android.database.MatrixCursor;
69import android.database.MatrixCursor.RowBuilder;
70import android.database.sqlite.SQLiteConstraintException;
71import android.database.sqlite.SQLiteContentHelper;
72import android.database.sqlite.SQLiteDatabase;
73import android.database.sqlite.SQLiteQueryBuilder;
74import android.database.sqlite.SQLiteStatement;
75import android.net.Uri;
76import android.os.AsyncTask;
77import android.os.Bundle;
78import android.os.MemoryFile;
79import android.os.RemoteException;
80import android.os.SystemProperties;
81import android.pim.vcard.VCardComposer;
82import android.pim.vcard.VCardConfig;
83import android.preference.PreferenceManager;
84import android.provider.BaseColumns;
85import android.provider.ContactsContract;
86import android.provider.LiveFolders;
87import android.provider.OpenableColumns;
88import android.provider.SyncStateContract;
89import android.provider.ContactsContract.AggregationExceptions;
90import android.provider.ContactsContract.ContactCounts;
91import android.provider.ContactsContract.Contacts;
92import android.provider.ContactsContract.Data;
93import android.provider.ContactsContract.DisplayNameSources;
94import android.provider.ContactsContract.FullNameStyle;
95import android.provider.ContactsContract.Groups;
96import android.provider.ContactsContract.Intents;
97import android.provider.ContactsContract.PhoneLookup;
98import android.provider.ContactsContract.PhoneticNameStyle;
99import android.provider.ContactsContract.ProviderStatus;
100import android.provider.ContactsContract.RawContacts;
101import android.provider.ContactsContract.SearchSnippetColumns;
102import android.provider.ContactsContract.Settings;
103import android.provider.ContactsContract.StatusUpdates;
104import android.provider.ContactsContract.CommonDataKinds.BaseTypes;
105import android.provider.ContactsContract.CommonDataKinds.Email;
106import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
107import android.provider.ContactsContract.CommonDataKinds.Im;
108import android.provider.ContactsContract.CommonDataKinds.Nickname;
109import android.provider.ContactsContract.CommonDataKinds.Organization;
110import android.provider.ContactsContract.CommonDataKinds.Phone;
111import android.provider.ContactsContract.CommonDataKinds.Photo;
112import android.provider.ContactsContract.CommonDataKinds.StructuredName;
113import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
114import android.telephony.PhoneNumberUtils;
115import android.text.TextUtils;
116import android.util.Log;
117
118import java.io.ByteArrayOutputStream;
119import java.io.FileNotFoundException;
120import java.io.IOException;
121import java.io.OutputStream;
122import java.text.SimpleDateFormat;
123import java.util.ArrayList;
124import java.util.Collections;
125import java.util.Date;
126import java.util.HashMap;
127import java.util.HashSet;
128import java.util.List;
129import java.util.Locale;
130import java.util.Map;
131import java.util.Set;
132import java.util.concurrent.CountDownLatch;
133
134/**
135 * Contacts content provider. The contract between this provider and applications
136 * is defined in {@link ContactsContract}.
137 */
138public class ContactsProvider2 extends SQLiteContentProvider implements OnAccountsUpdateListener {
139
140    private static final String TAG = "ContactsProvider";
141
142    private static final boolean VERBOSE_LOGGING = Log.isLoggable(TAG, Log.VERBOSE);
143
144    // TODO: carefully prevent all incoming nested queries; they can be gaping security holes
145    // TODO: check for restricted flag during insert(), update(), and delete() calls
146
147    /** Default for the maximum number of returned aggregation suggestions. */
148    private static final int DEFAULT_MAX_SUGGESTIONS = 5;
149
150    private static final String GOOGLE_MY_CONTACTS_GROUP_TITLE = "My Contacts";
151    /**
152     * Property key for the legacy contact import version. The need for a version
153     * as opposed to a boolean flag is that if we discover bugs in the contact import process,
154     * we can trigger re-import by incrementing the import version.
155     */
156    private static final String PROPERTY_CONTACTS_IMPORTED = "contacts_imported_v1";
157    private static final int PROPERTY_CONTACTS_IMPORT_VERSION = 1;
158    private static final String PREF_LOCALE = "locale";
159
160    private static final String AGGREGATE_CONTACTS = "sync.contacts.aggregate";
161
162    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
163
164    private static final String TIMES_CONTACED_SORT_COLUMN = "times_contacted_sort";
165
166    private static final String STREQUENT_ORDER_BY = Contacts.STARRED + " DESC, "
167            + TIMES_CONTACED_SORT_COLUMN + " DESC, "
168            + Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
169    private static final String STREQUENT_LIMIT =
170            "(SELECT COUNT(1) FROM " + Tables.CONTACTS + " WHERE "
171            + Contacts.STARRED + "=1) + 25";
172
173    /* package */ static final String UPDATE_TIMES_CONTACTED_CONTACTS_TABLE =
174            "UPDATE " + Tables.CONTACTS + " SET " + Contacts.TIMES_CONTACTED + "=" +
175            " CASE WHEN " + Contacts.TIMES_CONTACTED + " IS NULL THEN 1 ELSE " +
176            " (" + Contacts.TIMES_CONTACTED + " + 1) END WHERE " + Contacts._ID + "=?";
177
178    /* package */ static final String UPDATE_TIMES_CONTACTED_RAWCONTACTS_TABLE =
179            "UPDATE " + Tables.RAW_CONTACTS + " SET " + RawContacts.TIMES_CONTACTED + "=" +
180            " CASE WHEN " + RawContacts.TIMES_CONTACTED + " IS NULL THEN 1 ELSE " +
181            " (" + RawContacts.TIMES_CONTACTED + " + 1) END WHERE " + RawContacts.CONTACT_ID + "=?";
182
183    /* package */ static final String PHONEBOOK_COLLATOR_NAME = "PHONEBOOK";
184
185    private static final int CONTACTS = 1000;
186    private static final int CONTACTS_ID = 1001;
187    private static final int CONTACTS_LOOKUP = 1002;
188    private static final int CONTACTS_LOOKUP_ID = 1003;
189    private static final int CONTACTS_DATA = 1004;
190    private static final int CONTACTS_FILTER = 1005;
191    private static final int CONTACTS_STREQUENT = 1006;
192    private static final int CONTACTS_STREQUENT_FILTER = 1007;
193    private static final int CONTACTS_GROUP = 1008;
194    private static final int CONTACTS_PHOTO = 1009;
195    private static final int CONTACTS_AS_VCARD = 1010;
196    private static final int CONTACTS_AS_MULTI_VCARD = 1011;
197
198    private static final int RAW_CONTACTS = 2002;
199    private static final int RAW_CONTACTS_ID = 2003;
200    private static final int RAW_CONTACTS_DATA = 2004;
201    private static final int RAW_CONTACT_ENTITY_ID = 2005;
202
203    private static final int DATA = 3000;
204    private static final int DATA_ID = 3001;
205    private static final int PHONES = 3002;
206    private static final int PHONES_ID = 3003;
207    private static final int PHONES_FILTER = 3004;
208    private static final int EMAILS = 3005;
209    private static final int EMAILS_ID = 3006;
210    private static final int EMAILS_LOOKUP = 3007;
211    private static final int EMAILS_FILTER = 3008;
212    private static final int POSTALS = 3009;
213    private static final int POSTALS_ID = 3010;
214
215    private static final int PHONE_LOOKUP = 4000;
216
217    private static final int AGGREGATION_EXCEPTIONS = 6000;
218    private static final int AGGREGATION_EXCEPTION_ID = 6001;
219
220    private static final int STATUS_UPDATES = 7000;
221    private static final int STATUS_UPDATES_ID = 7001;
222
223    private static final int AGGREGATION_SUGGESTIONS = 8000;
224
225    private static final int SETTINGS = 9000;
226
227    private static final int GROUPS = 10000;
228    private static final int GROUPS_ID = 10001;
229    private static final int GROUPS_SUMMARY = 10003;
230
231    private static final int SYNCSTATE = 11000;
232    private static final int SYNCSTATE_ID = 11001;
233
234    private static final int SEARCH_SUGGESTIONS = 12001;
235    private static final int SEARCH_SHORTCUT = 12002;
236
237    private static final int LIVE_FOLDERS_CONTACTS = 14000;
238    private static final int LIVE_FOLDERS_CONTACTS_WITH_PHONES = 14001;
239    private static final int LIVE_FOLDERS_CONTACTS_FAVORITES = 14002;
240    private static final int LIVE_FOLDERS_CONTACTS_GROUP_NAME = 14003;
241
242    private static final int RAW_CONTACT_ENTITIES = 15001;
243
244    private static final int PROVIDER_STATUS = 16001;
245
246    private interface DataContactsQuery {
247        public static final String TABLE = "data "
248                + "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
249                + "JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
250
251        public static final String[] PROJECTION = new String[] {
252            RawContactsColumns.CONCRETE_ID,
253            DataColumns.CONCRETE_ID,
254            ContactsColumns.CONCRETE_ID
255        };
256
257        public static final int RAW_CONTACT_ID = 0;
258        public static final int DATA_ID = 1;
259        public static final int CONTACT_ID = 2;
260    }
261
262    private interface DataDeleteQuery {
263        public static final String TABLE = Tables.DATA_JOIN_MIMETYPES;
264
265        public static final String[] CONCRETE_COLUMNS = new String[] {
266            DataColumns.CONCRETE_ID,
267            MimetypesColumns.MIMETYPE,
268            Data.RAW_CONTACT_ID,
269            Data.IS_PRIMARY,
270            Data.DATA1,
271        };
272
273        public static final String[] COLUMNS = new String[] {
274            Data._ID,
275            MimetypesColumns.MIMETYPE,
276            Data.RAW_CONTACT_ID,
277            Data.IS_PRIMARY,
278            Data.DATA1,
279        };
280
281        public static final int _ID = 0;
282        public static final int MIMETYPE = 1;
283        public static final int RAW_CONTACT_ID = 2;
284        public static final int IS_PRIMARY = 3;
285        public static final int DATA1 = 4;
286    }
287
288    private interface DataUpdateQuery {
289        String[] COLUMNS = { Data._ID, Data.RAW_CONTACT_ID, Data.MIMETYPE };
290
291        int _ID = 0;
292        int RAW_CONTACT_ID = 1;
293        int MIMETYPE = 2;
294    }
295
296
297    private interface RawContactsQuery {
298        String TABLE = Tables.RAW_CONTACTS;
299
300        String[] COLUMNS = new String[] {
301                RawContacts.DELETED,
302                RawContacts.ACCOUNT_TYPE,
303                RawContacts.ACCOUNT_NAME,
304        };
305
306        int DELETED = 0;
307        int ACCOUNT_TYPE = 1;
308        int ACCOUNT_NAME = 2;
309    }
310
311    public static final String DEFAULT_ACCOUNT_TYPE = "com.google";
312    public static final String FEATURE_LEGACY_HOSTED_OR_GOOGLE = "legacy_hosted_or_google";
313
314    /** Sql where statement for filtering on groups. */
315    private static final String CONTACTS_IN_GROUP_SELECT =
316            Contacts._ID + " IN "
317                    + "(SELECT " + RawContacts.CONTACT_ID
318                    + " FROM " + Tables.RAW_CONTACTS
319                    + " WHERE " + RawContactsColumns.CONCRETE_ID + " IN "
320                            + "(SELECT " + DataColumns.CONCRETE_RAW_CONTACT_ID
321                            + " FROM " + Tables.DATA_JOIN_MIMETYPES
322                            + " WHERE " + Data.MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE
323                                    + "' AND " + GroupMembership.GROUP_ROW_ID + "="
324                                    + "(SELECT " + Tables.GROUPS + "." + Groups._ID
325                                    + " FROM " + Tables.GROUPS
326                                    + " WHERE " + Groups.TITLE + "=?)))";
327
328    /** Sql for updating DIRTY flag on multiple raw contacts */
329    private static final String UPDATE_RAW_CONTACT_SET_DIRTY_SQL =
330            "UPDATE " + Tables.RAW_CONTACTS +
331            " SET " + RawContacts.DIRTY + "=1" +
332            " WHERE " + RawContacts._ID + " IN (";
333
334    /** Sql for updating VERSION on multiple raw contacts */
335    private static final String UPDATE_RAW_CONTACT_SET_VERSION_SQL =
336            "UPDATE " + Tables.RAW_CONTACTS +
337            " SET " + RawContacts.VERSION + " = " + RawContacts.VERSION + " + 1" +
338            " WHERE " + RawContacts._ID + " IN (";
339
340    /** Name lookup types used for contact filtering */
341    private static final String CONTACT_LOOKUP_NAME_TYPES =
342            NameLookupType.NAME_COLLATION_KEY + "," +
343            NameLookupType.EMAIL_BASED_NICKNAME + "," +
344            NameLookupType.NICKNAME + "," +
345            NameLookupType.NAME_SHORTHAND + "," +
346            NameLookupType.ORGANIZATION + "," +
347            NameLookupType.NAME_CONSONANTS;
348
349
350    /** Contains just BaseColumns._COUNT */
351    private static final HashMap<String, String> sCountProjectionMap;
352    /** Contains just the contacts columns */
353    private static final HashMap<String, String> sContactsProjectionMap;
354    /** Contains just the contacts columns */
355    private static final HashMap<String, String> sContactsProjectionWithSnippetMap;
356
357    /** Used for pushing starred contacts to the top of a times contacted list **/
358    private static final HashMap<String, String> sStrequentStarredProjectionMap;
359    private static final HashMap<String, String> sStrequentFrequentProjectionMap;
360    /** Contains just the contacts vCard columns */
361    private static final HashMap<String, String> sContactsVCardProjectionMap;
362    /** Contains just the raw contacts columns */
363    private static final HashMap<String, String> sRawContactsProjectionMap;
364    /** Contains the columns from the raw contacts entity view*/
365    private static final HashMap<String, String> sRawContactsEntityProjectionMap;
366    /** Contains columns from the data view */
367    private static final HashMap<String, String> sDataProjectionMap;
368    /** Contains columns from the data view */
369    private static final HashMap<String, String> sDistinctDataProjectionMap;
370    /** Contains the data and contacts columns, for joined tables */
371    private static final HashMap<String, String> sPhoneLookupProjectionMap;
372    /** Contains the just the {@link Groups} columns */
373    private static final HashMap<String, String> sGroupsProjectionMap;
374    /** Contains {@link Groups} columns along with summary details */
375    private static final HashMap<String, String> sGroupsSummaryProjectionMap;
376    /** Contains the agg_exceptions columns */
377    private static final HashMap<String, String> sAggregationExceptionsProjectionMap;
378    /** Contains the agg_exceptions columns */
379    private static final HashMap<String, String> sSettingsProjectionMap;
380    /** Contains StatusUpdates columns */
381    private static final HashMap<String, String> sStatusUpdatesProjectionMap;
382    /** Contains Live Folders columns */
383    private static final HashMap<String, String> sLiveFoldersProjectionMap;
384
385    // where clause to update the status_updates table
386    private static final String WHERE_CLAUSE_FOR_STATUS_UPDATES_TABLE =
387            StatusUpdatesColumns.DATA_ID + " IN (SELECT Distinct " + StatusUpdates.DATA_ID +
388            " FROM " + Tables.STATUS_UPDATES + " LEFT OUTER JOIN " + Tables.PRESENCE +
389            " ON " + StatusUpdatesColumns.DATA_ID + " = " + StatusUpdates.DATA_ID + " WHERE ";
390
391    private static final String[] EMPTY_STRING_ARRAY = new String[0];
392
393    /**
394     * Notification ID for failure to import contacts.
395     */
396    private static final int LEGACY_IMPORT_FAILED_NOTIFICATION = 1;
397
398    /** Precompiled sql statement for setting a data record to the primary. */
399    private SQLiteStatement mSetPrimaryStatement;
400    /** Precompiled sql statement for setting a data record to the super primary. */
401    private SQLiteStatement mSetSuperPrimaryStatement;
402    /** Precompiled sql statement for updating a contact display name */
403    private SQLiteStatement mRawContactDisplayNameUpdate;
404    /** Precompiled sql statement for updating an aggregated status update */
405    private SQLiteStatement mLastStatusUpdate;
406    private SQLiteStatement mNameLookupInsert;
407    private SQLiteStatement mNameLookupDelete;
408    private SQLiteStatement mStatusUpdateAutoTimestamp;
409    private SQLiteStatement mStatusUpdateInsert;
410    private SQLiteStatement mStatusUpdateReplace;
411    private SQLiteStatement mStatusAttributionUpdate;
412    private SQLiteStatement mStatusUpdateDelete;
413    private SQLiteStatement mResetNameVerifiedForOtherRawContacts;
414
415    private long mMimeTypeIdEmail;
416    private long mMimeTypeIdIm;
417    private long mMimeTypeIdStructuredName;
418    private long mMimeTypeIdOrganization;
419    private long mMimeTypeIdNickname;
420    private long mMimeTypeIdPhone;
421    private StringBuilder mSb = new StringBuilder();
422    private String[] mSelectionArgs1 = new String[1];
423    private String[] mSelectionArgs2 = new String[2];
424    private ArrayList<String> mSelectionArgs = Lists.newArrayList();
425
426    private Account mAccount;
427
428    static {
429        // Contacts URI matching table
430        final UriMatcher matcher = sUriMatcher;
431        matcher.addURI(ContactsContract.AUTHORITY, "contacts", CONTACTS);
432        matcher.addURI(ContactsContract.AUTHORITY, "contacts/#", CONTACTS_ID);
433        matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/data", CONTACTS_DATA);
434        matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/suggestions",
435                AGGREGATION_SUGGESTIONS);
436        matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/suggestions/*",
437                AGGREGATION_SUGGESTIONS);
438        matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/photo", CONTACTS_PHOTO);
439        matcher.addURI(ContactsContract.AUTHORITY, "contacts/filter/*", CONTACTS_FILTER);
440        matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", CONTACTS_LOOKUP);
441        matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*/#", CONTACTS_LOOKUP_ID);
442        matcher.addURI(ContactsContract.AUTHORITY, "contacts/as_vcard/*", CONTACTS_AS_VCARD);
443        matcher.addURI(ContactsContract.AUTHORITY, "contacts/as_multi_vcard/*",
444                CONTACTS_AS_MULTI_VCARD);
445        matcher.addURI(ContactsContract.AUTHORITY, "contacts/strequent/", CONTACTS_STREQUENT);
446        matcher.addURI(ContactsContract.AUTHORITY, "contacts/strequent/filter/*",
447                CONTACTS_STREQUENT_FILTER);
448        matcher.addURI(ContactsContract.AUTHORITY, "contacts/group/*", CONTACTS_GROUP);
449
450        matcher.addURI(ContactsContract.AUTHORITY, "raw_contacts", RAW_CONTACTS);
451        matcher.addURI(ContactsContract.AUTHORITY, "raw_contacts/#", RAW_CONTACTS_ID);
452        matcher.addURI(ContactsContract.AUTHORITY, "raw_contacts/#/data", RAW_CONTACTS_DATA);
453        matcher.addURI(ContactsContract.AUTHORITY, "raw_contacts/#/entity", RAW_CONTACT_ENTITY_ID);
454
455        matcher.addURI(ContactsContract.AUTHORITY, "raw_contact_entities", RAW_CONTACT_ENTITIES);
456
457        matcher.addURI(ContactsContract.AUTHORITY, "data", DATA);
458        matcher.addURI(ContactsContract.AUTHORITY, "data/#", DATA_ID);
459        matcher.addURI(ContactsContract.AUTHORITY, "data/phones", PHONES);
460        matcher.addURI(ContactsContract.AUTHORITY, "data/phones/#", PHONES_ID);
461        matcher.addURI(ContactsContract.AUTHORITY, "data/phones/filter", PHONES_FILTER);
462        matcher.addURI(ContactsContract.AUTHORITY, "data/phones/filter/*", PHONES_FILTER);
463        matcher.addURI(ContactsContract.AUTHORITY, "data/emails", EMAILS);
464        matcher.addURI(ContactsContract.AUTHORITY, "data/emails/#", EMAILS_ID);
465        matcher.addURI(ContactsContract.AUTHORITY, "data/emails/lookup/*", EMAILS_LOOKUP);
466        matcher.addURI(ContactsContract.AUTHORITY, "data/emails/filter", EMAILS_FILTER);
467        matcher.addURI(ContactsContract.AUTHORITY, "data/emails/filter/*", EMAILS_FILTER);
468        matcher.addURI(ContactsContract.AUTHORITY, "data/postals", POSTALS);
469        matcher.addURI(ContactsContract.AUTHORITY, "data/postals/#", POSTALS_ID);
470
471        matcher.addURI(ContactsContract.AUTHORITY, "groups", GROUPS);
472        matcher.addURI(ContactsContract.AUTHORITY, "groups/#", GROUPS_ID);
473        matcher.addURI(ContactsContract.AUTHORITY, "groups_summary", GROUPS_SUMMARY);
474
475        matcher.addURI(ContactsContract.AUTHORITY, SyncStateContentProviderHelper.PATH, SYNCSTATE);
476        matcher.addURI(ContactsContract.AUTHORITY, SyncStateContentProviderHelper.PATH + "/#",
477                SYNCSTATE_ID);
478
479        matcher.addURI(ContactsContract.AUTHORITY, "phone_lookup/*", PHONE_LOOKUP);
480        matcher.addURI(ContactsContract.AUTHORITY, "aggregation_exceptions",
481                AGGREGATION_EXCEPTIONS);
482        matcher.addURI(ContactsContract.AUTHORITY, "aggregation_exceptions/*",
483                AGGREGATION_EXCEPTION_ID);
484
485        matcher.addURI(ContactsContract.AUTHORITY, "settings", SETTINGS);
486
487        matcher.addURI(ContactsContract.AUTHORITY, "status_updates", STATUS_UPDATES);
488        matcher.addURI(ContactsContract.AUTHORITY, "status_updates/#", STATUS_UPDATES_ID);
489
490        matcher.addURI(ContactsContract.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,
491                SEARCH_SUGGESTIONS);
492        matcher.addURI(ContactsContract.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
493                SEARCH_SUGGESTIONS);
494        matcher.addURI(ContactsContract.AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT + "/*",
495                SEARCH_SHORTCUT);
496
497        matcher.addURI(ContactsContract.AUTHORITY, "live_folders/contacts",
498                LIVE_FOLDERS_CONTACTS);
499        matcher.addURI(ContactsContract.AUTHORITY, "live_folders/contacts/*",
500                LIVE_FOLDERS_CONTACTS_GROUP_NAME);
501        matcher.addURI(ContactsContract.AUTHORITY, "live_folders/contacts_with_phones",
502                LIVE_FOLDERS_CONTACTS_WITH_PHONES);
503        matcher.addURI(ContactsContract.AUTHORITY, "live_folders/favorites",
504                LIVE_FOLDERS_CONTACTS_FAVORITES);
505
506        matcher.addURI(ContactsContract.AUTHORITY, "provider_status", PROVIDER_STATUS);
507    }
508
509    static {
510        sCountProjectionMap = new HashMap<String, String>();
511        sCountProjectionMap.put(BaseColumns._COUNT, "COUNT(*)");
512
513        sContactsProjectionMap = new HashMap<String, String>();
514        sContactsProjectionMap.put(Contacts._ID, Contacts._ID);
515        sContactsProjectionMap.put(Contacts.DISPLAY_NAME, Contacts.DISPLAY_NAME_PRIMARY);
516        sContactsProjectionMap.put(Contacts.DISPLAY_NAME_ALTERNATIVE,
517                Contacts.DISPLAY_NAME_ALTERNATIVE);
518        sContactsProjectionMap.put(Contacts.DISPLAY_NAME_SOURCE, Contacts.DISPLAY_NAME_SOURCE);
519        sContactsProjectionMap.put(Contacts.PHONETIC_NAME, Contacts.PHONETIC_NAME);
520        sContactsProjectionMap.put(Contacts.PHONETIC_NAME_STYLE, Contacts.PHONETIC_NAME_STYLE);
521        sContactsProjectionMap.put(Contacts.SORT_KEY_PRIMARY, Contacts.SORT_KEY_PRIMARY);
522        sContactsProjectionMap.put(Contacts.SORT_KEY_ALTERNATIVE, Contacts.SORT_KEY_ALTERNATIVE);
523        sContactsProjectionMap.put(Contacts.LAST_TIME_CONTACTED, Contacts.LAST_TIME_CONTACTED);
524        sContactsProjectionMap.put(Contacts.TIMES_CONTACTED, Contacts.TIMES_CONTACTED);
525        sContactsProjectionMap.put(Contacts.STARRED, Contacts.STARRED);
526        sContactsProjectionMap.put(Contacts.IN_VISIBLE_GROUP, Contacts.IN_VISIBLE_GROUP);
527        sContactsProjectionMap.put(Contacts.PHOTO_ID, Contacts.PHOTO_ID);
528        sContactsProjectionMap.put(Contacts.CUSTOM_RINGTONE, Contacts.CUSTOM_RINGTONE);
529        sContactsProjectionMap.put(Contacts.HAS_PHONE_NUMBER, Contacts.HAS_PHONE_NUMBER);
530        sContactsProjectionMap.put(Contacts.SEND_TO_VOICEMAIL, Contacts.SEND_TO_VOICEMAIL);
531        sContactsProjectionMap.put(Contacts.LOOKUP_KEY, Contacts.LOOKUP_KEY);
532
533        // Handle projections for Contacts-level statuses
534        addProjection(sContactsProjectionMap, Contacts.CONTACT_PRESENCE,
535                Tables.AGGREGATED_PRESENCE + "." + StatusUpdates.PRESENCE);
536        addProjection(sContactsProjectionMap, Contacts.CONTACT_STATUS,
537                ContactsStatusUpdatesColumns.CONCRETE_STATUS);
538        addProjection(sContactsProjectionMap, Contacts.CONTACT_STATUS_TIMESTAMP,
539                ContactsStatusUpdatesColumns.CONCRETE_STATUS_TIMESTAMP);
540        addProjection(sContactsProjectionMap, Contacts.CONTACT_STATUS_RES_PACKAGE,
541                ContactsStatusUpdatesColumns.CONCRETE_STATUS_RES_PACKAGE);
542        addProjection(sContactsProjectionMap, Contacts.CONTACT_STATUS_LABEL,
543                ContactsStatusUpdatesColumns.CONCRETE_STATUS_LABEL);
544        addProjection(sContactsProjectionMap, Contacts.CONTACT_STATUS_ICON,
545                ContactsStatusUpdatesColumns.CONCRETE_STATUS_ICON);
546
547        sContactsProjectionWithSnippetMap = new HashMap<String, String>();
548        sContactsProjectionWithSnippetMap.putAll(sContactsProjectionMap);
549        sContactsProjectionWithSnippetMap.put(SearchSnippetColumns.SNIPPET_MIMETYPE,
550                SearchSnippetColumns.SNIPPET_MIMETYPE);
551        sContactsProjectionWithSnippetMap.put(SearchSnippetColumns.SNIPPET_DATA_ID,
552                SearchSnippetColumns.SNIPPET_DATA_ID);
553        sContactsProjectionWithSnippetMap.put(SearchSnippetColumns.SNIPPET_DATA1,
554                SearchSnippetColumns.SNIPPET_DATA1);
555        sContactsProjectionWithSnippetMap.put(SearchSnippetColumns.SNIPPET_DATA2,
556                SearchSnippetColumns.SNIPPET_DATA2);
557        sContactsProjectionWithSnippetMap.put(SearchSnippetColumns.SNIPPET_DATA3,
558                SearchSnippetColumns.SNIPPET_DATA3);
559        sContactsProjectionWithSnippetMap.put(SearchSnippetColumns.SNIPPET_DATA4,
560                SearchSnippetColumns.SNIPPET_DATA4);
561
562        sStrequentStarredProjectionMap = new HashMap<String, String>(sContactsProjectionMap);
563        sStrequentStarredProjectionMap.put(TIMES_CONTACED_SORT_COLUMN,
564                  Long.MAX_VALUE + " AS " + TIMES_CONTACED_SORT_COLUMN);
565
566        sStrequentFrequentProjectionMap = new HashMap<String, String>(sContactsProjectionMap);
567        sStrequentFrequentProjectionMap.put(TIMES_CONTACED_SORT_COLUMN,
568                  Contacts.TIMES_CONTACTED + " AS " + TIMES_CONTACED_SORT_COLUMN);
569
570        sContactsVCardProjectionMap = Maps.newHashMap();
571        sContactsVCardProjectionMap.put(OpenableColumns.DISPLAY_NAME, Contacts.DISPLAY_NAME
572                + " || '.vcf' AS " + OpenableColumns.DISPLAY_NAME);
573        sContactsVCardProjectionMap.put(OpenableColumns.SIZE, "NULL AS " + OpenableColumns.SIZE);
574
575        sRawContactsProjectionMap = new HashMap<String, String>();
576        sRawContactsProjectionMap.put(RawContacts._ID, RawContacts._ID);
577        sRawContactsProjectionMap.put(RawContacts.CONTACT_ID, RawContacts.CONTACT_ID);
578        sRawContactsProjectionMap.put(RawContacts.ACCOUNT_NAME, RawContacts.ACCOUNT_NAME);
579        sRawContactsProjectionMap.put(RawContacts.ACCOUNT_TYPE, RawContacts.ACCOUNT_TYPE);
580        sRawContactsProjectionMap.put(RawContacts.SOURCE_ID, RawContacts.SOURCE_ID);
581        sRawContactsProjectionMap.put(RawContacts.VERSION, RawContacts.VERSION);
582        sRawContactsProjectionMap.put(RawContacts.DIRTY, RawContacts.DIRTY);
583        sRawContactsProjectionMap.put(RawContacts.DELETED, RawContacts.DELETED);
584        sRawContactsProjectionMap.put(RawContacts.DISPLAY_NAME_PRIMARY,
585                RawContacts.DISPLAY_NAME_PRIMARY);
586        sRawContactsProjectionMap.put(RawContacts.DISPLAY_NAME_ALTERNATIVE,
587                RawContacts.DISPLAY_NAME_ALTERNATIVE);
588        sRawContactsProjectionMap.put(RawContacts.DISPLAY_NAME_SOURCE,
589                RawContacts.DISPLAY_NAME_SOURCE);
590        sRawContactsProjectionMap.put(RawContacts.PHONETIC_NAME,
591                RawContacts.PHONETIC_NAME);
592        sRawContactsProjectionMap.put(RawContacts.PHONETIC_NAME_STYLE,
593                RawContacts.PHONETIC_NAME_STYLE);
594        sRawContactsProjectionMap.put(RawContacts.NAME_VERIFIED,
595                RawContacts.NAME_VERIFIED);
596        sRawContactsProjectionMap.put(RawContacts.SORT_KEY_PRIMARY,
597                RawContacts.SORT_KEY_PRIMARY);
598        sRawContactsProjectionMap.put(RawContacts.SORT_KEY_ALTERNATIVE,
599                RawContacts.SORT_KEY_ALTERNATIVE);
600        sRawContactsProjectionMap.put(RawContacts.TIMES_CONTACTED, RawContacts.TIMES_CONTACTED);
601        sRawContactsProjectionMap.put(RawContacts.LAST_TIME_CONTACTED,
602                RawContacts.LAST_TIME_CONTACTED);
603        sRawContactsProjectionMap.put(RawContacts.CUSTOM_RINGTONE, RawContacts.CUSTOM_RINGTONE);
604        sRawContactsProjectionMap.put(RawContacts.SEND_TO_VOICEMAIL, RawContacts.SEND_TO_VOICEMAIL);
605        sRawContactsProjectionMap.put(RawContacts.STARRED, RawContacts.STARRED);
606        sRawContactsProjectionMap.put(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE);
607        sRawContactsProjectionMap.put(RawContacts.SYNC1, RawContacts.SYNC1);
608        sRawContactsProjectionMap.put(RawContacts.SYNC2, RawContacts.SYNC2);
609        sRawContactsProjectionMap.put(RawContacts.SYNC3, RawContacts.SYNC3);
610        sRawContactsProjectionMap.put(RawContacts.SYNC4, RawContacts.SYNC4);
611
612        sDataProjectionMap = new HashMap<String, String>();
613        sDataProjectionMap.put(Data._ID, Data._ID);
614        sDataProjectionMap.put(Data.RAW_CONTACT_ID, Data.RAW_CONTACT_ID);
615        sDataProjectionMap.put(Data.DATA_VERSION, Data.DATA_VERSION);
616        sDataProjectionMap.put(Data.IS_PRIMARY, Data.IS_PRIMARY);
617        sDataProjectionMap.put(Data.IS_SUPER_PRIMARY, Data.IS_SUPER_PRIMARY);
618        sDataProjectionMap.put(Data.RES_PACKAGE, Data.RES_PACKAGE);
619        sDataProjectionMap.put(Data.MIMETYPE, Data.MIMETYPE);
620        sDataProjectionMap.put(Data.DATA1, Data.DATA1);
621        sDataProjectionMap.put(Data.DATA2, Data.DATA2);
622        sDataProjectionMap.put(Data.DATA3, Data.DATA3);
623        sDataProjectionMap.put(Data.DATA4, Data.DATA4);
624        sDataProjectionMap.put(Data.DATA5, Data.DATA5);
625        sDataProjectionMap.put(Data.DATA6, Data.DATA6);
626        sDataProjectionMap.put(Data.DATA7, Data.DATA7);
627        sDataProjectionMap.put(Data.DATA8, Data.DATA8);
628        sDataProjectionMap.put(Data.DATA9, Data.DATA9);
629        sDataProjectionMap.put(Data.DATA10, Data.DATA10);
630        sDataProjectionMap.put(Data.DATA11, Data.DATA11);
631        sDataProjectionMap.put(Data.DATA12, Data.DATA12);
632        sDataProjectionMap.put(Data.DATA13, Data.DATA13);
633        sDataProjectionMap.put(Data.DATA14, Data.DATA14);
634        sDataProjectionMap.put(Data.DATA15, Data.DATA15);
635        sDataProjectionMap.put(Data.SYNC1, Data.SYNC1);
636        sDataProjectionMap.put(Data.SYNC2, Data.SYNC2);
637        sDataProjectionMap.put(Data.SYNC3, Data.SYNC3);
638        sDataProjectionMap.put(Data.SYNC4, Data.SYNC4);
639        sDataProjectionMap.put(Data.CONTACT_ID, Data.CONTACT_ID);
640        sDataProjectionMap.put(RawContacts.ACCOUNT_NAME, RawContacts.ACCOUNT_NAME);
641        sDataProjectionMap.put(RawContacts.ACCOUNT_TYPE, RawContacts.ACCOUNT_TYPE);
642        sDataProjectionMap.put(RawContacts.SOURCE_ID, RawContacts.SOURCE_ID);
643        sDataProjectionMap.put(RawContacts.VERSION, RawContacts.VERSION);
644        sDataProjectionMap.put(RawContacts.DIRTY, RawContacts.DIRTY);
645        sDataProjectionMap.put(RawContacts.NAME_VERIFIED, RawContacts.NAME_VERIFIED);
646        sDataProjectionMap.put(Contacts.LOOKUP_KEY, Contacts.LOOKUP_KEY);
647        sDataProjectionMap.put(Contacts.DISPLAY_NAME, Contacts.DISPLAY_NAME);
648        sDataProjectionMap.put(Contacts.DISPLAY_NAME_ALTERNATIVE,
649                Contacts.DISPLAY_NAME_ALTERNATIVE);
650        sDataProjectionMap.put(Contacts.DISPLAY_NAME_SOURCE, Contacts.DISPLAY_NAME_SOURCE);
651        sDataProjectionMap.put(Contacts.PHONETIC_NAME, Contacts.PHONETIC_NAME);
652        sDataProjectionMap.put(Contacts.PHONETIC_NAME_STYLE, Contacts.PHONETIC_NAME_STYLE);
653        sDataProjectionMap.put(Contacts.SORT_KEY_PRIMARY, Contacts.SORT_KEY_PRIMARY);
654        sDataProjectionMap.put(Contacts.SORT_KEY_ALTERNATIVE, Contacts.SORT_KEY_ALTERNATIVE);
655        sDataProjectionMap.put(Contacts.CUSTOM_RINGTONE, Contacts.CUSTOM_RINGTONE);
656        sDataProjectionMap.put(Contacts.SEND_TO_VOICEMAIL, Contacts.SEND_TO_VOICEMAIL);
657        sDataProjectionMap.put(Contacts.LAST_TIME_CONTACTED, Contacts.LAST_TIME_CONTACTED);
658        sDataProjectionMap.put(Contacts.TIMES_CONTACTED, Contacts.TIMES_CONTACTED);
659        sDataProjectionMap.put(Contacts.STARRED, Contacts.STARRED);
660        sDataProjectionMap.put(Contacts.PHOTO_ID, Contacts.PHOTO_ID);
661        sDataProjectionMap.put(Contacts.IN_VISIBLE_GROUP, Contacts.IN_VISIBLE_GROUP);
662        sDataProjectionMap.put(Contacts.NAME_RAW_CONTACT_ID, Contacts.NAME_RAW_CONTACT_ID);
663        sDataProjectionMap.put(GroupMembership.GROUP_SOURCE_ID, GroupMembership.GROUP_SOURCE_ID);
664
665        HashMap<String, String> columns;
666        columns = new HashMap<String, String>();
667        columns.put(RawContacts._ID, RawContacts._ID);
668        columns.put(RawContacts.CONTACT_ID, RawContacts.CONTACT_ID);
669        columns.put(RawContacts.ACCOUNT_NAME, RawContacts.ACCOUNT_NAME);
670        columns.put(RawContacts.ACCOUNT_TYPE, RawContacts.ACCOUNT_TYPE);
671        columns.put(RawContacts.SOURCE_ID, RawContacts.SOURCE_ID);
672        columns.put(RawContacts.VERSION, RawContacts.VERSION);
673        columns.put(RawContacts.DIRTY, RawContacts.DIRTY);
674        columns.put(RawContacts.DELETED, RawContacts.DELETED);
675        columns.put(RawContacts.IS_RESTRICTED, RawContacts.IS_RESTRICTED);
676        columns.put(RawContacts.SYNC1, RawContacts.SYNC1);
677        columns.put(RawContacts.SYNC2, RawContacts.SYNC2);
678        columns.put(RawContacts.SYNC3, RawContacts.SYNC3);
679        columns.put(RawContacts.SYNC4, RawContacts.SYNC4);
680        columns.put(RawContacts.NAME_VERIFIED, RawContacts.NAME_VERIFIED);
681        columns.put(Data.RES_PACKAGE, Data.RES_PACKAGE);
682        columns.put(Data.MIMETYPE, Data.MIMETYPE);
683        columns.put(Data.DATA1, Data.DATA1);
684        columns.put(Data.DATA2, Data.DATA2);
685        columns.put(Data.DATA3, Data.DATA3);
686        columns.put(Data.DATA4, Data.DATA4);
687        columns.put(Data.DATA5, Data.DATA5);
688        columns.put(Data.DATA6, Data.DATA6);
689        columns.put(Data.DATA7, Data.DATA7);
690        columns.put(Data.DATA8, Data.DATA8);
691        columns.put(Data.DATA9, Data.DATA9);
692        columns.put(Data.DATA10, Data.DATA10);
693        columns.put(Data.DATA11, Data.DATA11);
694        columns.put(Data.DATA12, Data.DATA12);
695        columns.put(Data.DATA13, Data.DATA13);
696        columns.put(Data.DATA14, Data.DATA14);
697        columns.put(Data.DATA15, Data.DATA15);
698        columns.put(Data.SYNC1, Data.SYNC1);
699        columns.put(Data.SYNC2, Data.SYNC2);
700        columns.put(Data.SYNC3, Data.SYNC3);
701        columns.put(Data.SYNC4, Data.SYNC4);
702        columns.put(RawContacts.Entity.DATA_ID, RawContacts.Entity.DATA_ID);
703        columns.put(Data.STARRED, Data.STARRED);
704        columns.put(Data.DATA_VERSION, Data.DATA_VERSION);
705        columns.put(Data.IS_PRIMARY, Data.IS_PRIMARY);
706        columns.put(Data.IS_SUPER_PRIMARY, Data.IS_SUPER_PRIMARY);
707        columns.put(GroupMembership.GROUP_SOURCE_ID, GroupMembership.GROUP_SOURCE_ID);
708        sRawContactsEntityProjectionMap = columns;
709
710        // Handle projections for Contacts-level statuses
711        addProjection(sDataProjectionMap, Contacts.CONTACT_PRESENCE,
712                Tables.AGGREGATED_PRESENCE + "." + StatusUpdates.PRESENCE);
713        addProjection(sDataProjectionMap, Contacts.CONTACT_STATUS,
714                ContactsStatusUpdatesColumns.CONCRETE_STATUS);
715        addProjection(sDataProjectionMap, Contacts.CONTACT_STATUS_TIMESTAMP,
716                ContactsStatusUpdatesColumns.CONCRETE_STATUS_TIMESTAMP);
717        addProjection(sDataProjectionMap, Contacts.CONTACT_STATUS_RES_PACKAGE,
718                ContactsStatusUpdatesColumns.CONCRETE_STATUS_RES_PACKAGE);
719        addProjection(sDataProjectionMap, Contacts.CONTACT_STATUS_LABEL,
720                ContactsStatusUpdatesColumns.CONCRETE_STATUS_LABEL);
721        addProjection(sDataProjectionMap, Contacts.CONTACT_STATUS_ICON,
722                ContactsStatusUpdatesColumns.CONCRETE_STATUS_ICON);
723
724        // Handle projections for Data-level statuses
725        addProjection(sDataProjectionMap, Data.PRESENCE,
726                Tables.PRESENCE + "." + StatusUpdates.PRESENCE);
727        addProjection(sDataProjectionMap, Data.STATUS,
728                StatusUpdatesColumns.CONCRETE_STATUS);
729        addProjection(sDataProjectionMap, Data.STATUS_TIMESTAMP,
730                StatusUpdatesColumns.CONCRETE_STATUS_TIMESTAMP);
731        addProjection(sDataProjectionMap, Data.STATUS_RES_PACKAGE,
732                StatusUpdatesColumns.CONCRETE_STATUS_RES_PACKAGE);
733        addProjection(sDataProjectionMap, Data.STATUS_LABEL,
734                StatusUpdatesColumns.CONCRETE_STATUS_LABEL);
735        addProjection(sDataProjectionMap, Data.STATUS_ICON,
736                StatusUpdatesColumns.CONCRETE_STATUS_ICON);
737
738        // Projection map for data grouped by contact (not raw contact) and some data field(s)
739        sDistinctDataProjectionMap = new HashMap<String, String>();
740        sDistinctDataProjectionMap.put(Data._ID,
741                "MIN(" + Data._ID + ") AS " + Data._ID);
742        sDistinctDataProjectionMap.put(Data.DATA_VERSION, Data.DATA_VERSION);
743        sDistinctDataProjectionMap.put(Data.IS_PRIMARY, Data.IS_PRIMARY);
744        sDistinctDataProjectionMap.put(Data.IS_SUPER_PRIMARY, Data.IS_SUPER_PRIMARY);
745        sDistinctDataProjectionMap.put(Data.RES_PACKAGE, Data.RES_PACKAGE);
746        sDistinctDataProjectionMap.put(Data.MIMETYPE, Data.MIMETYPE);
747        sDistinctDataProjectionMap.put(Data.DATA1, Data.DATA1);
748        sDistinctDataProjectionMap.put(Data.DATA2, Data.DATA2);
749        sDistinctDataProjectionMap.put(Data.DATA3, Data.DATA3);
750        sDistinctDataProjectionMap.put(Data.DATA4, Data.DATA4);
751        sDistinctDataProjectionMap.put(Data.DATA5, Data.DATA5);
752        sDistinctDataProjectionMap.put(Data.DATA6, Data.DATA6);
753        sDistinctDataProjectionMap.put(Data.DATA7, Data.DATA7);
754        sDistinctDataProjectionMap.put(Data.DATA8, Data.DATA8);
755        sDistinctDataProjectionMap.put(Data.DATA9, Data.DATA9);
756        sDistinctDataProjectionMap.put(Data.DATA10, Data.DATA10);
757        sDistinctDataProjectionMap.put(Data.DATA11, Data.DATA11);
758        sDistinctDataProjectionMap.put(Data.DATA12, Data.DATA12);
759        sDistinctDataProjectionMap.put(Data.DATA13, Data.DATA13);
760        sDistinctDataProjectionMap.put(Data.DATA14, Data.DATA14);
761        sDistinctDataProjectionMap.put(Data.DATA15, Data.DATA15);
762        sDistinctDataProjectionMap.put(Data.SYNC1, Data.SYNC1);
763        sDistinctDataProjectionMap.put(Data.SYNC2, Data.SYNC2);
764        sDistinctDataProjectionMap.put(Data.SYNC3, Data.SYNC3);
765        sDistinctDataProjectionMap.put(Data.SYNC4, Data.SYNC4);
766        sDistinctDataProjectionMap.put(RawContacts.CONTACT_ID, RawContacts.CONTACT_ID);
767        sDistinctDataProjectionMap.put(Contacts.LOOKUP_KEY, Contacts.LOOKUP_KEY);
768        sDistinctDataProjectionMap.put(Contacts.DISPLAY_NAME, Contacts.DISPLAY_NAME);
769        sDistinctDataProjectionMap.put(Contacts.DISPLAY_NAME_ALTERNATIVE,
770                Contacts.DISPLAY_NAME_ALTERNATIVE);
771        sDistinctDataProjectionMap.put(Contacts.DISPLAY_NAME_SOURCE, Contacts.DISPLAY_NAME_SOURCE);
772        sDistinctDataProjectionMap.put(Contacts.PHONETIC_NAME, Contacts.PHONETIC_NAME);
773        sDistinctDataProjectionMap.put(Contacts.PHONETIC_NAME_STYLE, Contacts.PHONETIC_NAME_STYLE);
774        sDistinctDataProjectionMap.put(Contacts.SORT_KEY_PRIMARY, Contacts.SORT_KEY_PRIMARY);
775        sDistinctDataProjectionMap.put(Contacts.SORT_KEY_ALTERNATIVE,
776                Contacts.SORT_KEY_ALTERNATIVE);
777        sDistinctDataProjectionMap.put(Contacts.CUSTOM_RINGTONE, Contacts.CUSTOM_RINGTONE);
778        sDistinctDataProjectionMap.put(Contacts.SEND_TO_VOICEMAIL, Contacts.SEND_TO_VOICEMAIL);
779        sDistinctDataProjectionMap.put(Contacts.LAST_TIME_CONTACTED, Contacts.LAST_TIME_CONTACTED);
780        sDistinctDataProjectionMap.put(Contacts.TIMES_CONTACTED, Contacts.TIMES_CONTACTED);
781        sDistinctDataProjectionMap.put(Contacts.STARRED, Contacts.STARRED);
782        sDistinctDataProjectionMap.put(Contacts.PHOTO_ID, Contacts.PHOTO_ID);
783        sDistinctDataProjectionMap.put(Contacts.IN_VISIBLE_GROUP, Contacts.IN_VISIBLE_GROUP);
784        sDistinctDataProjectionMap.put(GroupMembership.GROUP_SOURCE_ID,
785                GroupMembership.GROUP_SOURCE_ID);
786
787        // Handle projections for Contacts-level statuses
788        addProjection(sDistinctDataProjectionMap, Contacts.CONTACT_PRESENCE,
789                Tables.AGGREGATED_PRESENCE + "." + StatusUpdates.PRESENCE);
790        addProjection(sDistinctDataProjectionMap, Contacts.CONTACT_STATUS,
791                ContactsStatusUpdatesColumns.CONCRETE_STATUS);
792        addProjection(sDistinctDataProjectionMap, Contacts.CONTACT_STATUS_TIMESTAMP,
793                ContactsStatusUpdatesColumns.CONCRETE_STATUS_TIMESTAMP);
794        addProjection(sDistinctDataProjectionMap, Contacts.CONTACT_STATUS_RES_PACKAGE,
795                ContactsStatusUpdatesColumns.CONCRETE_STATUS_RES_PACKAGE);
796        addProjection(sDistinctDataProjectionMap, Contacts.CONTACT_STATUS_LABEL,
797                ContactsStatusUpdatesColumns.CONCRETE_STATUS_LABEL);
798        addProjection(sDistinctDataProjectionMap, Contacts.CONTACT_STATUS_ICON,
799                ContactsStatusUpdatesColumns.CONCRETE_STATUS_ICON);
800
801        // Handle projections for Data-level statuses
802        addProjection(sDistinctDataProjectionMap, Data.PRESENCE,
803                Tables.PRESENCE + "." + StatusUpdates.PRESENCE);
804        addProjection(sDistinctDataProjectionMap, Data.STATUS,
805                StatusUpdatesColumns.CONCRETE_STATUS);
806        addProjection(sDistinctDataProjectionMap, Data.STATUS_TIMESTAMP,
807                StatusUpdatesColumns.CONCRETE_STATUS_TIMESTAMP);
808        addProjection(sDistinctDataProjectionMap, Data.STATUS_RES_PACKAGE,
809                StatusUpdatesColumns.CONCRETE_STATUS_RES_PACKAGE);
810        addProjection(sDistinctDataProjectionMap, Data.STATUS_LABEL,
811                StatusUpdatesColumns.CONCRETE_STATUS_LABEL);
812        addProjection(sDistinctDataProjectionMap, Data.STATUS_ICON,
813                StatusUpdatesColumns.CONCRETE_STATUS_ICON);
814
815        sPhoneLookupProjectionMap = new HashMap<String, String>();
816        sPhoneLookupProjectionMap.put(PhoneLookup._ID,
817                "contacts_view." + Contacts._ID
818                        + " AS " + PhoneLookup._ID);
819        sPhoneLookupProjectionMap.put(PhoneLookup.LOOKUP_KEY,
820                "contacts_view." + Contacts.LOOKUP_KEY
821                        + " AS " + PhoneLookup.LOOKUP_KEY);
822        sPhoneLookupProjectionMap.put(PhoneLookup.DISPLAY_NAME,
823                "contacts_view." + Contacts.DISPLAY_NAME
824                        + " AS " + PhoneLookup.DISPLAY_NAME);
825        sPhoneLookupProjectionMap.put(PhoneLookup.LAST_TIME_CONTACTED,
826                "contacts_view." + Contacts.LAST_TIME_CONTACTED
827                        + " AS " + PhoneLookup.LAST_TIME_CONTACTED);
828        sPhoneLookupProjectionMap.put(PhoneLookup.TIMES_CONTACTED,
829                "contacts_view." + Contacts.TIMES_CONTACTED
830                        + " AS " + PhoneLookup.TIMES_CONTACTED);
831        sPhoneLookupProjectionMap.put(PhoneLookup.STARRED,
832                "contacts_view." + Contacts.STARRED
833                        + " AS " + PhoneLookup.STARRED);
834        sPhoneLookupProjectionMap.put(PhoneLookup.IN_VISIBLE_GROUP,
835                "contacts_view." + Contacts.IN_VISIBLE_GROUP
836                        + " AS " + PhoneLookup.IN_VISIBLE_GROUP);
837        sPhoneLookupProjectionMap.put(PhoneLookup.PHOTO_ID,
838                "contacts_view." + Contacts.PHOTO_ID
839                        + " AS " + PhoneLookup.PHOTO_ID);
840        sPhoneLookupProjectionMap.put(PhoneLookup.CUSTOM_RINGTONE,
841                "contacts_view." + Contacts.CUSTOM_RINGTONE
842                        + " AS " + PhoneLookup.CUSTOM_RINGTONE);
843        sPhoneLookupProjectionMap.put(PhoneLookup.HAS_PHONE_NUMBER,
844                "contacts_view." + Contacts.HAS_PHONE_NUMBER
845                        + " AS " + PhoneLookup.HAS_PHONE_NUMBER);
846        sPhoneLookupProjectionMap.put(PhoneLookup.SEND_TO_VOICEMAIL,
847                "contacts_view." + Contacts.SEND_TO_VOICEMAIL
848                        + " AS " + PhoneLookup.SEND_TO_VOICEMAIL);
849        sPhoneLookupProjectionMap.put(PhoneLookup.NUMBER,
850                Phone.NUMBER + " AS " + PhoneLookup.NUMBER);
851        sPhoneLookupProjectionMap.put(PhoneLookup.TYPE,
852                Phone.TYPE + " AS " + PhoneLookup.TYPE);
853        sPhoneLookupProjectionMap.put(PhoneLookup.LABEL,
854                Phone.LABEL + " AS " + PhoneLookup.LABEL);
855
856        // Groups projection map
857        columns = new HashMap<String, String>();
858        columns.put(Groups._ID, Groups._ID);
859        columns.put(Groups.ACCOUNT_NAME, Groups.ACCOUNT_NAME);
860        columns.put(Groups.ACCOUNT_TYPE, Groups.ACCOUNT_TYPE);
861        columns.put(Groups.SOURCE_ID, Groups.SOURCE_ID);
862        columns.put(Groups.DIRTY, Groups.DIRTY);
863        columns.put(Groups.VERSION, Groups.VERSION);
864        columns.put(Groups.RES_PACKAGE, Groups.RES_PACKAGE);
865        columns.put(Groups.TITLE, Groups.TITLE);
866        columns.put(Groups.TITLE_RES, Groups.TITLE_RES);
867        columns.put(Groups.GROUP_VISIBLE, Groups.GROUP_VISIBLE);
868        columns.put(Groups.SYSTEM_ID, Groups.SYSTEM_ID);
869        columns.put(Groups.DELETED, Groups.DELETED);
870        columns.put(Groups.NOTES, Groups.NOTES);
871        columns.put(Groups.SHOULD_SYNC, Groups.SHOULD_SYNC);
872        columns.put(Groups.SYNC1, Groups.SYNC1);
873        columns.put(Groups.SYNC2, Groups.SYNC2);
874        columns.put(Groups.SYNC3, Groups.SYNC3);
875        columns.put(Groups.SYNC4, Groups.SYNC4);
876        sGroupsProjectionMap = columns;
877
878        // RawContacts and groups projection map
879        columns = new HashMap<String, String>();
880        columns.putAll(sGroupsProjectionMap);
881        columns.put(Groups.SUMMARY_COUNT, "(SELECT COUNT(DISTINCT " + ContactsColumns.CONCRETE_ID
882                + ") FROM " + Tables.DATA_JOIN_MIMETYPES_RAW_CONTACTS_CONTACTS + " WHERE "
883                + Clauses.MIMETYPE_IS_GROUP_MEMBERSHIP + " AND " + Clauses.BELONGS_TO_GROUP
884                + ") AS " + Groups.SUMMARY_COUNT);
885        columns.put(Groups.SUMMARY_WITH_PHONES, "(SELECT COUNT(DISTINCT "
886                + ContactsColumns.CONCRETE_ID + ") FROM "
887                + Tables.DATA_JOIN_MIMETYPES_RAW_CONTACTS_CONTACTS + " WHERE "
888                + Clauses.MIMETYPE_IS_GROUP_MEMBERSHIP + " AND " + Clauses.BELONGS_TO_GROUP
889                + " AND " + Contacts.HAS_PHONE_NUMBER + ") AS " + Groups.SUMMARY_WITH_PHONES);
890        sGroupsSummaryProjectionMap = columns;
891
892        // Aggregate exception projection map
893        columns = new HashMap<String, String>();
894        columns.put(AggregationExceptionColumns._ID, Tables.AGGREGATION_EXCEPTIONS + "._id AS _id");
895        columns.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE);
896        columns.put(AggregationExceptions.RAW_CONTACT_ID1, AggregationExceptions.RAW_CONTACT_ID1);
897        columns.put(AggregationExceptions.RAW_CONTACT_ID2, AggregationExceptions.RAW_CONTACT_ID2);
898        sAggregationExceptionsProjectionMap = columns;
899
900        // Settings projection map
901        columns = new HashMap<String, String>();
902        columns.put(Settings.ACCOUNT_NAME, Settings.ACCOUNT_NAME);
903        columns.put(Settings.ACCOUNT_TYPE, Settings.ACCOUNT_TYPE);
904        columns.put(Settings.UNGROUPED_VISIBLE, Settings.UNGROUPED_VISIBLE);
905        columns.put(Settings.SHOULD_SYNC, Settings.SHOULD_SYNC);
906        columns.put(Settings.ANY_UNSYNCED, "(CASE WHEN MIN(" + Settings.SHOULD_SYNC
907                + ",(SELECT (CASE WHEN MIN(" + Groups.SHOULD_SYNC + ") IS NULL THEN 1 ELSE MIN("
908                + Groups.SHOULD_SYNC + ") END) FROM " + Tables.GROUPS + " WHERE "
909                + GroupsColumns.CONCRETE_ACCOUNT_NAME + "=" + SettingsColumns.CONCRETE_ACCOUNT_NAME
910                + " AND " + GroupsColumns.CONCRETE_ACCOUNT_TYPE + "="
911                + SettingsColumns.CONCRETE_ACCOUNT_TYPE + "))=0 THEN 1 ELSE 0 END) AS "
912                + Settings.ANY_UNSYNCED);
913        columns.put(Settings.UNGROUPED_COUNT, "(SELECT COUNT(*) FROM (SELECT 1 FROM "
914                + Tables.SETTINGS_JOIN_RAW_CONTACTS_DATA_MIMETYPES_CONTACTS + " GROUP BY "
915                + Clauses.GROUP_BY_ACCOUNT_CONTACT_ID + " HAVING " + Clauses.HAVING_NO_GROUPS
916                + ")) AS " + Settings.UNGROUPED_COUNT);
917        columns.put(Settings.UNGROUPED_WITH_PHONES, "(SELECT COUNT(*) FROM (SELECT 1 FROM "
918                + Tables.SETTINGS_JOIN_RAW_CONTACTS_DATA_MIMETYPES_CONTACTS + " WHERE "
919                + Contacts.HAS_PHONE_NUMBER + " GROUP BY " + Clauses.GROUP_BY_ACCOUNT_CONTACT_ID
920                + " HAVING " + Clauses.HAVING_NO_GROUPS + ")) AS "
921                + Settings.UNGROUPED_WITH_PHONES);
922        sSettingsProjectionMap = columns;
923
924        columns = new HashMap<String, String>();
925        columns.put(PresenceColumns.RAW_CONTACT_ID, PresenceColumns.RAW_CONTACT_ID);
926        columns.put(StatusUpdates.DATA_ID,
927                DataColumns.CONCRETE_ID + " AS " + StatusUpdates.DATA_ID);
928        columns.put(StatusUpdates.IM_ACCOUNT, StatusUpdates.IM_ACCOUNT);
929        columns.put(StatusUpdates.IM_HANDLE, StatusUpdates.IM_HANDLE);
930        columns.put(StatusUpdates.PROTOCOL, StatusUpdates.PROTOCOL);
931        // We cannot allow a null in the custom protocol field, because SQLite3 does not
932        // properly enforce uniqueness of null values
933        columns.put(StatusUpdates.CUSTOM_PROTOCOL, "(CASE WHEN " + StatusUpdates.CUSTOM_PROTOCOL
934                + "='' THEN NULL ELSE " + StatusUpdates.CUSTOM_PROTOCOL + " END) AS "
935                + StatusUpdates.CUSTOM_PROTOCOL);
936        columns.put(StatusUpdates.PRESENCE, StatusUpdates.PRESENCE);
937        columns.put(StatusUpdates.STATUS, StatusUpdates.STATUS);
938        columns.put(StatusUpdates.STATUS_TIMESTAMP, StatusUpdates.STATUS_TIMESTAMP);
939        columns.put(StatusUpdates.STATUS_RES_PACKAGE, StatusUpdates.STATUS_RES_PACKAGE);
940        columns.put(StatusUpdates.STATUS_ICON, StatusUpdates.STATUS_ICON);
941        columns.put(StatusUpdates.STATUS_LABEL, StatusUpdates.STATUS_LABEL);
942        sStatusUpdatesProjectionMap = columns;
943
944        // Live folder projection
945        sLiveFoldersProjectionMap = new HashMap<String, String>();
946        sLiveFoldersProjectionMap.put(LiveFolders._ID,
947                Contacts._ID + " AS " + LiveFolders._ID);
948        sLiveFoldersProjectionMap.put(LiveFolders.NAME,
949                Contacts.DISPLAY_NAME + " AS " + LiveFolders.NAME);
950        // TODO: Put contact photo back when we have a way to display a default icon
951        // for contacts without a photo
952        // sLiveFoldersProjectionMap.put(LiveFolders.ICON_BITMAP,
953        //      Photos.DATA + " AS " + LiveFolders.ICON_BITMAP);
954    }
955
956    private static void addProjection(HashMap<String, String> map, String toField, String fromField) {
957        map.put(toField, fromField + " AS " + toField);
958    }
959
960    /**
961     * Handles inserts and update for a specific Data type.
962     */
963    private abstract class DataRowHandler {
964
965        protected final String mMimetype;
966        protected long mMimetypeId;
967
968        @SuppressWarnings("all")
969        public DataRowHandler(String mimetype) {
970            mMimetype = mimetype;
971
972            // To ensure the data column position. This is dead code if properly configured.
973            if (StructuredName.DISPLAY_NAME != Data.DATA1 || Nickname.NAME != Data.DATA1
974                    || Organization.COMPANY != Data.DATA1 || Phone.NUMBER != Data.DATA1
975                    || Email.DATA != Data.DATA1) {
976                throw new AssertionError("Some of ContactsContract.CommonDataKinds class primary"
977                        + " data is not in DATA1 column");
978            }
979        }
980
981        protected long getMimeTypeId() {
982            if (mMimetypeId == 0) {
983                mMimetypeId = mDbHelper.getMimeTypeId(mMimetype);
984            }
985            return mMimetypeId;
986        }
987
988        /**
989         * Inserts a row into the {@link Data} table.
990         */
991        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
992            final long dataId = db.insert(Tables.DATA, null, values);
993
994            Integer primary = values.getAsInteger(Data.IS_PRIMARY);
995            if (primary != null && primary != 0) {
996                setIsPrimary(rawContactId, dataId, getMimeTypeId());
997            }
998
999            return dataId;
1000        }
1001
1002        /**
1003         * Validates data and updates a {@link Data} row using the cursor, which contains
1004         * the current data.
1005         *
1006         * @return true if update changed something
1007         */
1008        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1009                boolean callerIsSyncAdapter) {
1010            long dataId = c.getLong(DataUpdateQuery._ID);
1011            long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
1012
1013            if (values.containsKey(Data.IS_SUPER_PRIMARY)) {
1014                long mimeTypeId = getMimeTypeId();
1015                setIsSuperPrimary(rawContactId, dataId, mimeTypeId);
1016                setIsPrimary(rawContactId, dataId, mimeTypeId);
1017
1018                // Now that we've taken care of setting these, remove them from "values".
1019                values.remove(Data.IS_SUPER_PRIMARY);
1020                values.remove(Data.IS_PRIMARY);
1021            } else if (values.containsKey(Data.IS_PRIMARY)) {
1022                setIsPrimary(rawContactId, dataId, getMimeTypeId());
1023
1024                // Now that we've taken care of setting this, remove it from "values".
1025                values.remove(Data.IS_PRIMARY);
1026            }
1027
1028            if (values.size() > 0) {
1029                mSelectionArgs1[0] = String.valueOf(dataId);
1030                mDb.update(Tables.DATA, values, Data._ID + " =?", mSelectionArgs1);
1031            }
1032
1033            if (!callerIsSyncAdapter) {
1034                setRawContactDirty(rawContactId);
1035            }
1036
1037            return true;
1038        }
1039
1040        public int delete(SQLiteDatabase db, Cursor c) {
1041            long dataId = c.getLong(DataDeleteQuery._ID);
1042            long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
1043            boolean primary = c.getInt(DataDeleteQuery.IS_PRIMARY) != 0;
1044            mSelectionArgs1[0] = String.valueOf(dataId);
1045            int count = db.delete(Tables.DATA, Data._ID + "=?", mSelectionArgs1);
1046            mSelectionArgs1[0] = String.valueOf(rawContactId);
1047            db.delete(Tables.PRESENCE, PresenceColumns.RAW_CONTACT_ID + "=?", mSelectionArgs1);
1048            if (count != 0 && primary) {
1049                fixPrimary(db, rawContactId);
1050            }
1051            return count;
1052        }
1053
1054        private void fixPrimary(SQLiteDatabase db, long rawContactId) {
1055            long mimeTypeId = getMimeTypeId();
1056            long primaryId = -1;
1057            int primaryType = -1;
1058            mSelectionArgs1[0] = String.valueOf(rawContactId);
1059            Cursor c = db.query(DataDeleteQuery.TABLE,
1060                    DataDeleteQuery.CONCRETE_COLUMNS,
1061                    Data.RAW_CONTACT_ID + "=?" +
1062                        " AND " + DataColumns.MIMETYPE_ID + "=" + mimeTypeId,
1063                    mSelectionArgs1, null, null, null);
1064            try {
1065                while (c.moveToNext()) {
1066                    long dataId = c.getLong(DataDeleteQuery._ID);
1067                    int type = c.getInt(DataDeleteQuery.DATA1);
1068                    if (primaryType == -1 || getTypeRank(type) < getTypeRank(primaryType)) {
1069                        primaryId = dataId;
1070                        primaryType = type;
1071                    }
1072                }
1073            } finally {
1074                c.close();
1075            }
1076            if (primaryId != -1) {
1077                setIsPrimary(rawContactId, primaryId, mimeTypeId);
1078            }
1079        }
1080
1081        /**
1082         * Returns the rank of a specific record type to be used in determining the primary
1083         * row. Lower number represents higher priority.
1084         */
1085        protected int getTypeRank(int type) {
1086            return 0;
1087        }
1088
1089        protected void fixRawContactDisplayName(SQLiteDatabase db, long rawContactId) {
1090            if (!isNewRawContact(rawContactId)) {
1091                updateRawContactDisplayName(db, rawContactId);
1092                mContactAggregator.updateDisplayNameForRawContact(db, rawContactId);
1093            }
1094        }
1095
1096        /**
1097         * Return set of values, using current values at given {@link Data#_ID}
1098         * as baseline, but augmented with any updates.  Returns null if there is
1099         * no change.
1100         */
1101        public ContentValues getAugmentedValues(SQLiteDatabase db, long dataId,
1102                ContentValues update) {
1103            boolean changing = false;
1104            final ContentValues values = new ContentValues();
1105            mSelectionArgs1[0] = String.valueOf(dataId);
1106            final Cursor cursor = db.query(Tables.DATA, null, Data._ID + "=?",
1107                    mSelectionArgs1, null, null, null);
1108            try {
1109                if (cursor.moveToFirst()) {
1110                    for (int i = 0; i < cursor.getColumnCount(); i++) {
1111                        final String key = cursor.getColumnName(i);
1112                        final String value = cursor.getString(i);
1113                        if (!changing && update.containsKey(key)) {
1114                            Object newValue = update.get(key);
1115                            String newString = newValue == null ? null : newValue.toString();
1116                            changing |= !TextUtils.equals(newString, value);
1117                        }
1118                        values.put(key, value);
1119                    }
1120                }
1121            } finally {
1122                cursor.close();
1123            }
1124            if (!changing) {
1125                return null;
1126            }
1127
1128            values.putAll(update);
1129            return values;
1130        }
1131    }
1132
1133    public class CustomDataRowHandler extends DataRowHandler {
1134
1135        public CustomDataRowHandler(String mimetype) {
1136            super(mimetype);
1137        }
1138    }
1139
1140    public class StructuredNameRowHandler extends DataRowHandler {
1141        private final NameSplitter mSplitter;
1142
1143        public StructuredNameRowHandler(NameSplitter splitter) {
1144            super(StructuredName.CONTENT_ITEM_TYPE);
1145            mSplitter = splitter;
1146        }
1147
1148        @Override
1149        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1150            fixStructuredNameComponents(values, values);
1151
1152            long dataId = super.insert(db, rawContactId, values);
1153
1154            String name = values.getAsString(StructuredName.DISPLAY_NAME);
1155            Integer fullNameStyle = values.getAsInteger(StructuredName.FULL_NAME_STYLE);
1156            insertNameLookupForStructuredName(rawContactId, dataId, name,
1157                    fullNameStyle != null
1158                            ? mNameSplitter.getAdjustedFullNameStyle(fullNameStyle)
1159                            : FullNameStyle.UNDEFINED);
1160            insertNameLookupForPhoneticName(rawContactId, dataId, values);
1161            fixRawContactDisplayName(db, rawContactId);
1162            triggerAggregation(rawContactId);
1163            return dataId;
1164        }
1165
1166        @Override
1167        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1168                boolean callerIsSyncAdapter) {
1169            final long dataId = c.getLong(DataUpdateQuery._ID);
1170            final long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
1171
1172            final ContentValues augmented = getAugmentedValues(db, dataId, values);
1173            if (augmented == null) {  // No change
1174                return false;
1175            }
1176
1177            fixStructuredNameComponents(augmented, values);
1178
1179            super.update(db, values, c, callerIsSyncAdapter);
1180            if (values.containsKey(StructuredName.DISPLAY_NAME) ||
1181                    values.containsKey(StructuredName.PHONETIC_FAMILY_NAME) ||
1182                    values.containsKey(StructuredName.PHONETIC_MIDDLE_NAME) ||
1183                    values.containsKey(StructuredName.PHONETIC_GIVEN_NAME)) {
1184                augmented.putAll(values);
1185                String name = augmented.getAsString(StructuredName.DISPLAY_NAME);
1186                deleteNameLookup(dataId);
1187                Integer fullNameStyle = augmented.getAsInteger(StructuredName.FULL_NAME_STYLE);
1188                insertNameLookupForStructuredName(rawContactId, dataId, name,
1189                        fullNameStyle != null
1190                                ? mNameSplitter.getAdjustedFullNameStyle(fullNameStyle)
1191                                : FullNameStyle.UNDEFINED);
1192                insertNameLookupForPhoneticName(rawContactId, dataId, augmented);
1193            }
1194            fixRawContactDisplayName(db, rawContactId);
1195            triggerAggregation(rawContactId);
1196            return true;
1197        }
1198
1199        @Override
1200        public int delete(SQLiteDatabase db, Cursor c) {
1201            long dataId = c.getLong(DataDeleteQuery._ID);
1202            long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
1203
1204            int count = super.delete(db, c);
1205
1206            deleteNameLookup(dataId);
1207            fixRawContactDisplayName(db, rawContactId);
1208            triggerAggregation(rawContactId);
1209            return count;
1210        }
1211
1212        /**
1213         * Specific list of structured fields.
1214         */
1215        private final String[] STRUCTURED_FIELDS = new String[] {
1216                StructuredName.PREFIX, StructuredName.GIVEN_NAME, StructuredName.MIDDLE_NAME,
1217                StructuredName.FAMILY_NAME, StructuredName.SUFFIX
1218        };
1219
1220        /**
1221         * Parses the supplied display name, but only if the incoming values do
1222         * not already contain structured name parts. Also, if the display name
1223         * is not provided, generate one by concatenating first name and last
1224         * name.
1225         */
1226        private void fixStructuredNameComponents(ContentValues augmented, ContentValues update) {
1227            final String unstruct = update.getAsString(StructuredName.DISPLAY_NAME);
1228
1229            final boolean touchedUnstruct = !TextUtils.isEmpty(unstruct);
1230            final boolean touchedStruct = !areAllEmpty(update, STRUCTURED_FIELDS);
1231
1232            if (touchedUnstruct && !touchedStruct) {
1233                NameSplitter.Name name = new NameSplitter.Name();
1234                mSplitter.split(name, unstruct);
1235                name.toValues(update);
1236            } else if (!touchedUnstruct
1237                    && (touchedStruct || areAnySpecified(update, STRUCTURED_FIELDS))) {
1238                // We need to update the display name when any structured components
1239                // are specified, even when they are null, which is why we are checking
1240                // areAnySpecified.  The touchedStruct in the condition is an optimization:
1241                // if there are non-null values, we know for a fact that some values are present.
1242                NameSplitter.Name name = new NameSplitter.Name();
1243                name.fromValues(augmented);
1244                // As the name could be changed, let's guess the name style again.
1245                name.fullNameStyle = FullNameStyle.UNDEFINED;
1246                mSplitter.guessNameStyle(name);
1247
1248                final String joined = mSplitter.join(name, true);
1249                update.put(StructuredName.DISPLAY_NAME, joined);
1250
1251                update.put(StructuredName.FULL_NAME_STYLE, name.fullNameStyle);
1252                update.put(StructuredName.PHONETIC_NAME_STYLE, name.phoneticNameStyle);
1253            } else if (touchedUnstruct && touchedStruct){
1254                if (!update.containsKey(StructuredName.FULL_NAME_STYLE)) {
1255                    update.put(StructuredName.FULL_NAME_STYLE,
1256                            mSplitter.guessFullNameStyle(unstruct));
1257                }
1258                if (!update.containsKey(StructuredName.PHONETIC_NAME_STYLE)) {
1259                    update.put(StructuredName.PHONETIC_NAME_STYLE,
1260                            mSplitter.guessPhoneticNameStyle(unstruct));
1261                }
1262            }
1263        }
1264    }
1265
1266    public class StructuredPostalRowHandler extends DataRowHandler {
1267        private PostalSplitter mSplitter;
1268
1269        public StructuredPostalRowHandler(PostalSplitter splitter) {
1270            super(StructuredPostal.CONTENT_ITEM_TYPE);
1271            mSplitter = splitter;
1272        }
1273
1274        @Override
1275        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1276            fixStructuredPostalComponents(values, values);
1277            return super.insert(db, rawContactId, values);
1278        }
1279
1280        @Override
1281        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1282                boolean callerIsSyncAdapter) {
1283            final long dataId = c.getLong(DataUpdateQuery._ID);
1284            final ContentValues augmented = getAugmentedValues(db, dataId, values);
1285            if (augmented == null) {    // No change
1286                return false;
1287            }
1288
1289            fixStructuredPostalComponents(augmented, values);
1290            super.update(db, values, c, callerIsSyncAdapter);
1291            return true;
1292        }
1293
1294        /**
1295         * Specific list of structured fields.
1296         */
1297        private final String[] STRUCTURED_FIELDS = new String[] {
1298                StructuredPostal.STREET, StructuredPostal.POBOX, StructuredPostal.NEIGHBORHOOD,
1299                StructuredPostal.CITY, StructuredPostal.REGION, StructuredPostal.POSTCODE,
1300                StructuredPostal.COUNTRY,
1301        };
1302
1303        /**
1304         * Prepares the given {@link StructuredPostal} row, building
1305         * {@link StructuredPostal#FORMATTED_ADDRESS} to match the structured
1306         * values when missing. When structured components are missing, the
1307         * unstructured value is assigned to {@link StructuredPostal#STREET}.
1308         */
1309        private void fixStructuredPostalComponents(ContentValues augmented, ContentValues update) {
1310            final String unstruct = update.getAsString(StructuredPostal.FORMATTED_ADDRESS);
1311
1312            final boolean touchedUnstruct = !TextUtils.isEmpty(unstruct);
1313            final boolean touchedStruct = !areAllEmpty(update, STRUCTURED_FIELDS);
1314
1315            final PostalSplitter.Postal postal = new PostalSplitter.Postal();
1316
1317            if (touchedUnstruct && !touchedStruct) {
1318                mSplitter.split(postal, unstruct);
1319                postal.toValues(update);
1320            } else if (!touchedUnstruct
1321                    && (touchedStruct || areAnySpecified(update, STRUCTURED_FIELDS))) {
1322                // See comment in
1323                postal.fromValues(augmented);
1324                final String joined = mSplitter.join(postal);
1325                update.put(StructuredPostal.FORMATTED_ADDRESS, joined);
1326            }
1327        }
1328    }
1329
1330    public class CommonDataRowHandler extends DataRowHandler {
1331
1332        private final String mTypeColumn;
1333        private final String mLabelColumn;
1334
1335        public CommonDataRowHandler(String mimetype, String typeColumn, String labelColumn) {
1336            super(mimetype);
1337            mTypeColumn = typeColumn;
1338            mLabelColumn = labelColumn;
1339        }
1340
1341        @Override
1342        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1343            enforceTypeAndLabel(values, values);
1344            return super.insert(db, rawContactId, values);
1345        }
1346
1347        @Override
1348        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1349                boolean callerIsSyncAdapter) {
1350            final long dataId = c.getLong(DataUpdateQuery._ID);
1351            final ContentValues augmented = getAugmentedValues(db, dataId, values);
1352            if (augmented == null) {        // No change
1353                return false;
1354            }
1355            enforceTypeAndLabel(augmented, values);
1356            return super.update(db, values, c, callerIsSyncAdapter);
1357        }
1358
1359        /**
1360         * If the given {@link ContentValues} defines {@link #mTypeColumn},
1361         * enforce that {@link #mLabelColumn} only appears when type is
1362         * {@link BaseTypes#TYPE_CUSTOM}. Exception is thrown otherwise.
1363         */
1364        private void enforceTypeAndLabel(ContentValues augmented, ContentValues update) {
1365            final boolean hasType = !TextUtils.isEmpty(augmented.getAsString(mTypeColumn));
1366            final boolean hasLabel = !TextUtils.isEmpty(augmented.getAsString(mLabelColumn));
1367
1368            if (hasLabel && !hasType) {
1369                // When label exists, assert that some type is defined
1370                throw new IllegalArgumentException(mTypeColumn + " must be specified when "
1371                        + mLabelColumn + " is defined.");
1372            }
1373        }
1374    }
1375
1376    public class OrganizationDataRowHandler extends CommonDataRowHandler {
1377
1378        public OrganizationDataRowHandler() {
1379            super(Organization.CONTENT_ITEM_TYPE, Organization.TYPE, Organization.LABEL);
1380        }
1381
1382        @Override
1383        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1384            String company = values.getAsString(Organization.COMPANY);
1385            String title = values.getAsString(Organization.TITLE);
1386
1387            long dataId = super.insert(db, rawContactId, values);
1388
1389            fixRawContactDisplayName(db, rawContactId);
1390            insertNameLookupForOrganization(rawContactId, dataId, company, title);
1391            return dataId;
1392        }
1393
1394        @Override
1395        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1396                boolean callerIsSyncAdapter) {
1397            if (!super.update(db, values, c, callerIsSyncAdapter)) {
1398                return false;
1399            }
1400
1401            boolean containsCompany = values.containsKey(Organization.COMPANY);
1402            boolean containsTitle = values.containsKey(Organization.TITLE);
1403            if (containsCompany || containsTitle) {
1404                long dataId = c.getLong(DataUpdateQuery._ID);
1405                long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
1406
1407                String company;
1408
1409                if (containsCompany) {
1410                    company = values.getAsString(Organization.COMPANY);
1411                } else {
1412                    mSelectionArgs1[0] = String.valueOf(dataId);
1413                    company = DatabaseUtils.stringForQuery(db,
1414                            "SELECT " + Organization.COMPANY +
1415                            " FROM " + Tables.DATA +
1416                            " WHERE " + Data._ID + "=?", mSelectionArgs1);
1417                }
1418
1419                String title;
1420                if (containsTitle) {
1421                    title = values.getAsString(Organization.TITLE);
1422                } else {
1423                    mSelectionArgs1[0] = String.valueOf(dataId);
1424                    title = DatabaseUtils.stringForQuery(db,
1425                            "SELECT " + Organization.TITLE +
1426                            " FROM " + Tables.DATA +
1427                            " WHERE " + Data._ID + "=?", mSelectionArgs1);
1428                }
1429
1430                deleteNameLookup(dataId);
1431                insertNameLookupForOrganization(rawContactId, dataId, company, title);
1432
1433                fixRawContactDisplayName(db, rawContactId);
1434            }
1435            return true;
1436        }
1437
1438        @Override
1439        public int delete(SQLiteDatabase db, Cursor c) {
1440            long dataId = c.getLong(DataUpdateQuery._ID);
1441            long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
1442
1443            int count = super.delete(db, c);
1444            fixRawContactDisplayName(db, rawContactId);
1445            deleteNameLookup(dataId);
1446            return count;
1447        }
1448
1449        @Override
1450        protected int getTypeRank(int type) {
1451            switch (type) {
1452                case Organization.TYPE_WORK: return 0;
1453                case Organization.TYPE_CUSTOM: return 1;
1454                case Organization.TYPE_OTHER: return 2;
1455                default: return 1000;
1456            }
1457        }
1458    }
1459
1460    public class EmailDataRowHandler extends CommonDataRowHandler {
1461
1462        public EmailDataRowHandler() {
1463            super(Email.CONTENT_ITEM_TYPE, Email.TYPE, Email.LABEL);
1464        }
1465
1466        @Override
1467        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1468            String email = values.getAsString(Email.DATA);
1469
1470            long dataId = super.insert(db, rawContactId, values);
1471
1472            fixRawContactDisplayName(db, rawContactId);
1473            String address = insertNameLookupForEmail(rawContactId, dataId, email);
1474            if (address != null) {
1475                triggerAggregation(rawContactId);
1476            }
1477            return dataId;
1478        }
1479
1480        @Override
1481        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1482                boolean callerIsSyncAdapter) {
1483            if (!super.update(db, values, c, callerIsSyncAdapter)) {
1484                return false;
1485            }
1486
1487            if (values.containsKey(Email.DATA)) {
1488                long dataId = c.getLong(DataUpdateQuery._ID);
1489                long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
1490
1491                String address = values.getAsString(Email.DATA);
1492                deleteNameLookup(dataId);
1493                insertNameLookupForEmail(rawContactId, dataId, address);
1494                fixRawContactDisplayName(db, rawContactId);
1495                triggerAggregation(rawContactId);
1496            }
1497
1498            return true;
1499        }
1500
1501        @Override
1502        public int delete(SQLiteDatabase db, Cursor c) {
1503            long dataId = c.getLong(DataDeleteQuery._ID);
1504            long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
1505
1506            int count = super.delete(db, c);
1507
1508            deleteNameLookup(dataId);
1509            fixRawContactDisplayName(db, rawContactId);
1510            triggerAggregation(rawContactId);
1511            return count;
1512        }
1513
1514        @Override
1515        protected int getTypeRank(int type) {
1516            switch (type) {
1517                case Email.TYPE_HOME: return 0;
1518                case Email.TYPE_WORK: return 1;
1519                case Email.TYPE_CUSTOM: return 2;
1520                case Email.TYPE_OTHER: return 3;
1521                default: return 1000;
1522            }
1523        }
1524    }
1525
1526    public class NicknameDataRowHandler extends CommonDataRowHandler {
1527
1528        public NicknameDataRowHandler() {
1529            super(Nickname.CONTENT_ITEM_TYPE, Nickname.TYPE, Nickname.LABEL);
1530        }
1531
1532        @Override
1533        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1534            String nickname = values.getAsString(Nickname.NAME);
1535
1536            long dataId = super.insert(db, rawContactId, values);
1537
1538            if (!TextUtils.isEmpty(nickname)) {
1539                fixRawContactDisplayName(db, rawContactId);
1540                insertNameLookupForNickname(rawContactId, dataId, nickname);
1541                triggerAggregation(rawContactId);
1542            }
1543            return dataId;
1544        }
1545
1546        @Override
1547        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1548                boolean callerIsSyncAdapter) {
1549            long dataId = c.getLong(DataUpdateQuery._ID);
1550            long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
1551
1552            if (!super.update(db, values, c, callerIsSyncAdapter)) {
1553                return false;
1554            }
1555
1556            if (values.containsKey(Nickname.NAME)) {
1557                String nickname = values.getAsString(Nickname.NAME);
1558                deleteNameLookup(dataId);
1559                insertNameLookupForNickname(rawContactId, dataId, nickname);
1560                fixRawContactDisplayName(db, rawContactId);
1561                triggerAggregation(rawContactId);
1562            }
1563
1564            return true;
1565        }
1566
1567        @Override
1568        public int delete(SQLiteDatabase db, Cursor c) {
1569            long dataId = c.getLong(DataDeleteQuery._ID);
1570            long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
1571
1572            int count = super.delete(db, c);
1573
1574            deleteNameLookup(dataId);
1575            fixRawContactDisplayName(db, rawContactId);
1576            triggerAggregation(rawContactId);
1577            return count;
1578        }
1579    }
1580
1581    public class PhoneDataRowHandler extends CommonDataRowHandler {
1582
1583        public PhoneDataRowHandler() {
1584            super(Phone.CONTENT_ITEM_TYPE, Phone.TYPE, Phone.LABEL);
1585        }
1586
1587        @Override
1588        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1589            long dataId;
1590            if (values.containsKey(Phone.NUMBER)) {
1591                String number = values.getAsString(Phone.NUMBER);
1592                String normalizedNumber = computeNormalizedNumber(number);
1593                values.put(PhoneColumns.NORMALIZED_NUMBER, normalizedNumber);
1594                dataId = super.insert(db, rawContactId, values);
1595
1596                updatePhoneLookup(db, rawContactId, dataId, number, normalizedNumber);
1597                mContactAggregator.updateHasPhoneNumber(db, rawContactId);
1598                fixRawContactDisplayName(db, rawContactId);
1599                if (normalizedNumber != null) {
1600                    triggerAggregation(rawContactId);
1601                }
1602            } else {
1603                dataId = super.insert(db, rawContactId, values);
1604            }
1605            return dataId;
1606        }
1607
1608        @Override
1609        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1610                boolean callerIsSyncAdapter) {
1611            String number = null;
1612            String normalizedNumber = null;
1613            if (values.containsKey(Phone.NUMBER)) {
1614                number = values.getAsString(Phone.NUMBER);
1615                normalizedNumber = computeNormalizedNumber(number);
1616                values.put(PhoneColumns.NORMALIZED_NUMBER, normalizedNumber);
1617            }
1618
1619            if (!super.update(db, values, c, callerIsSyncAdapter)) {
1620                return false;
1621            }
1622
1623            if (values.containsKey(Phone.NUMBER)) {
1624                long dataId = c.getLong(DataUpdateQuery._ID);
1625                long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
1626                updatePhoneLookup(db, rawContactId, dataId, number, normalizedNumber);
1627                mContactAggregator.updateHasPhoneNumber(db, rawContactId);
1628                fixRawContactDisplayName(db, rawContactId);
1629                triggerAggregation(rawContactId);
1630            }
1631            return true;
1632        }
1633
1634        @Override
1635        public int delete(SQLiteDatabase db, Cursor c) {
1636            long dataId = c.getLong(DataDeleteQuery._ID);
1637            long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
1638
1639            int count = super.delete(db, c);
1640
1641            updatePhoneLookup(db, rawContactId, dataId, null, null);
1642            mContactAggregator.updateHasPhoneNumber(db, rawContactId);
1643            fixRawContactDisplayName(db, rawContactId);
1644            triggerAggregation(rawContactId);
1645            return count;
1646        }
1647
1648        private String computeNormalizedNumber(String number) {
1649            String normalizedNumber = null;
1650            if (number != null) {
1651                normalizedNumber = PhoneNumberUtils.getStrippedReversed(number);
1652            }
1653            return normalizedNumber;
1654        }
1655
1656        private void updatePhoneLookup(SQLiteDatabase db, long rawContactId, long dataId,
1657                String number, String normalizedNumber) {
1658            if (number != null) {
1659                ContentValues phoneValues = new ContentValues();
1660                phoneValues.put(PhoneLookupColumns.RAW_CONTACT_ID, rawContactId);
1661                phoneValues.put(PhoneLookupColumns.DATA_ID, dataId);
1662                phoneValues.put(PhoneLookupColumns.NORMALIZED_NUMBER, normalizedNumber);
1663                phoneValues.put(PhoneLookupColumns.MIN_MATCH,
1664                        PhoneNumberUtils.toCallerIDMinMatch(number));
1665
1666                db.replace(Tables.PHONE_LOOKUP, null, phoneValues);
1667            } else {
1668                mSelectionArgs1[0] = String.valueOf(dataId);
1669                db.delete(Tables.PHONE_LOOKUP, PhoneLookupColumns.DATA_ID + "=?", mSelectionArgs1);
1670            }
1671        }
1672
1673        @Override
1674        protected int getTypeRank(int type) {
1675            switch (type) {
1676                case Phone.TYPE_MOBILE: return 0;
1677                case Phone.TYPE_WORK: return 1;
1678                case Phone.TYPE_HOME: return 2;
1679                case Phone.TYPE_PAGER: return 3;
1680                case Phone.TYPE_CUSTOM: return 4;
1681                case Phone.TYPE_OTHER: return 5;
1682                case Phone.TYPE_FAX_WORK: return 6;
1683                case Phone.TYPE_FAX_HOME: return 7;
1684                default: return 1000;
1685            }
1686        }
1687    }
1688
1689    public class GroupMembershipRowHandler extends DataRowHandler {
1690
1691        public GroupMembershipRowHandler() {
1692            super(GroupMembership.CONTENT_ITEM_TYPE);
1693        }
1694
1695        @Override
1696        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1697            resolveGroupSourceIdInValues(rawContactId, db, values, true);
1698            long dataId = super.insert(db, rawContactId, values);
1699            updateVisibility(rawContactId);
1700            return dataId;
1701        }
1702
1703        @Override
1704        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1705                boolean callerIsSyncAdapter) {
1706            long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
1707            resolveGroupSourceIdInValues(rawContactId, db, values, false);
1708            if (!super.update(db, values, c, callerIsSyncAdapter)) {
1709                return false;
1710            }
1711            updateVisibility(rawContactId);
1712            return true;
1713        }
1714
1715        @Override
1716        public int delete(SQLiteDatabase db, Cursor c) {
1717            long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
1718            int count = super.delete(db, c);
1719            updateVisibility(rawContactId);
1720            return count;
1721        }
1722
1723        private void updateVisibility(long rawContactId) {
1724            long contactId = mDbHelper.getContactId(rawContactId);
1725            if (contactId != 0) {
1726                mDbHelper.updateContactVisible(contactId);
1727            }
1728        }
1729
1730        private void resolveGroupSourceIdInValues(long rawContactId, SQLiteDatabase db,
1731                ContentValues values, boolean isInsert) {
1732            boolean containsGroupSourceId = values.containsKey(GroupMembership.GROUP_SOURCE_ID);
1733            boolean containsGroupId = values.containsKey(GroupMembership.GROUP_ROW_ID);
1734            if (containsGroupSourceId && containsGroupId) {
1735                throw new IllegalArgumentException(
1736                        "you are not allowed to set both the GroupMembership.GROUP_SOURCE_ID "
1737                                + "and GroupMembership.GROUP_ROW_ID");
1738            }
1739
1740            if (!containsGroupSourceId && !containsGroupId) {
1741                if (isInsert) {
1742                    throw new IllegalArgumentException(
1743                            "you must set exactly one of GroupMembership.GROUP_SOURCE_ID "
1744                                    + "and GroupMembership.GROUP_ROW_ID");
1745                } else {
1746                    return;
1747                }
1748            }
1749
1750            if (containsGroupSourceId) {
1751                final String sourceId = values.getAsString(GroupMembership.GROUP_SOURCE_ID);
1752                final long groupId = getOrMakeGroup(db, rawContactId, sourceId,
1753                        mInsertedRawContacts.get(rawContactId));
1754                values.remove(GroupMembership.GROUP_SOURCE_ID);
1755                values.put(GroupMembership.GROUP_ROW_ID, groupId);
1756            }
1757        }
1758    }
1759
1760    public class PhotoDataRowHandler extends DataRowHandler {
1761
1762        public PhotoDataRowHandler() {
1763            super(Photo.CONTENT_ITEM_TYPE);
1764        }
1765
1766        @Override
1767        public long insert(SQLiteDatabase db, long rawContactId, ContentValues values) {
1768            long dataId = super.insert(db, rawContactId, values);
1769            if (!isNewRawContact(rawContactId)) {
1770                mContactAggregator.updatePhotoId(db, rawContactId);
1771            }
1772            return dataId;
1773        }
1774
1775        @Override
1776        public boolean update(SQLiteDatabase db, ContentValues values, Cursor c,
1777                boolean callerIsSyncAdapter) {
1778            long rawContactId = c.getLong(DataUpdateQuery.RAW_CONTACT_ID);
1779            if (!super.update(db, values, c, callerIsSyncAdapter)) {
1780                return false;
1781            }
1782
1783            mContactAggregator.updatePhotoId(db, rawContactId);
1784            return true;
1785        }
1786
1787        @Override
1788        public int delete(SQLiteDatabase db, Cursor c) {
1789            long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
1790            int count = super.delete(db, c);
1791            mContactAggregator.updatePhotoId(db, rawContactId);
1792            return count;
1793        }
1794    }
1795
1796    /**
1797     * An entry in group id cache. It maps the combination of (account type, account name
1798     * and source id) to group row id.
1799     */
1800    public class GroupIdCacheEntry {
1801        String accountType;
1802        String accountName;
1803        String sourceId;
1804        long groupId;
1805    }
1806
1807    private HashMap<String, DataRowHandler> mDataRowHandlers;
1808    private ContactsDatabaseHelper mDbHelper;
1809
1810    private NameSplitter mNameSplitter;
1811    private NameLookupBuilder mNameLookupBuilder;
1812
1813    private PostalSplitter mPostalSplitter;
1814
1815    // We don't need a soft cache for groups - the assumption is that there will only
1816    // be a small number of contact groups. The cache is keyed off source id.  The value
1817    // is a list of groups with this group id.
1818    private HashMap<String, ArrayList<GroupIdCacheEntry>> mGroupIdCache = Maps.newHashMap();
1819
1820    private ContactAggregator mContactAggregator;
1821    private LegacyApiSupport mLegacyApiSupport;
1822    private GlobalSearchSupport mGlobalSearchSupport;
1823    private CommonNicknameCache mCommonNicknameCache;
1824
1825    private ContentValues mValues = new ContentValues();
1826    private CharArrayBuffer mCharArrayBuffer = new CharArrayBuffer(128);
1827    private NameSplitter.Name mName = new NameSplitter.Name();
1828    private HashMap<String, Boolean> mAccountWritability = Maps.newHashMap();
1829
1830    private int mProviderStatus = ProviderStatus.STATUS_NORMAL;
1831    private long mEstimatedStorageRequirement = 0;
1832    private volatile CountDownLatch mAccessLatch;
1833
1834    private HashMap<Long, Account> mInsertedRawContacts = Maps.newHashMap();
1835    private HashSet<Long> mUpdatedRawContacts = Sets.newHashSet();
1836    private HashSet<Long> mDirtyRawContacts = Sets.newHashSet();
1837    private HashMap<Long, Object> mUpdatedSyncStates = Maps.newHashMap();
1838
1839    private boolean mVisibleTouched = false;
1840
1841    private boolean mSyncToNetwork;
1842
1843    private Locale mCurrentLocale;
1844
1845
1846    @Override
1847    public boolean onCreate() {
1848        super.onCreate();
1849        try {
1850            return initialize();
1851        } catch (RuntimeException e) {
1852            Log.e(TAG, "Cannot start provider", e);
1853            return false;
1854        }
1855    }
1856
1857    private boolean initialize() {
1858        final Context context = getContext();
1859        mDbHelper = (ContactsDatabaseHelper)getDatabaseHelper();
1860        mGlobalSearchSupport = new GlobalSearchSupport(this);
1861        mLegacyApiSupport = new LegacyApiSupport(context, mDbHelper, this, mGlobalSearchSupport);
1862        mContactAggregator = new ContactAggregator(this, mDbHelper,
1863                createPhotoPriorityResolver(context));
1864        mContactAggregator.setEnabled(SystemProperties.getBoolean(AGGREGATE_CONTACTS, true));
1865
1866        mDb = mDbHelper.getWritableDatabase();
1867
1868        initForDefaultLocale();
1869
1870        mSetPrimaryStatement = mDb.compileStatement(
1871                "UPDATE " + Tables.DATA +
1872                " SET " + Data.IS_PRIMARY + "=(_id=?)" +
1873                " WHERE " + DataColumns.MIMETYPE_ID + "=?" +
1874                "   AND " + Data.RAW_CONTACT_ID + "=?");
1875
1876        mSetSuperPrimaryStatement = mDb.compileStatement(
1877                "UPDATE " + Tables.DATA +
1878                " SET " + Data.IS_SUPER_PRIMARY + "=(" + Data._ID + "=?)" +
1879                " WHERE " + DataColumns.MIMETYPE_ID + "=?" +
1880                "   AND " + Data.RAW_CONTACT_ID + " IN (" +
1881                        "SELECT " + RawContacts._ID +
1882                        " FROM " + Tables.RAW_CONTACTS +
1883                        " WHERE " + RawContacts.CONTACT_ID + " =(" +
1884                                "SELECT " + RawContacts.CONTACT_ID +
1885                                " FROM " + Tables.RAW_CONTACTS +
1886                                " WHERE " + RawContacts._ID + "=?))");
1887
1888        mRawContactDisplayNameUpdate = mDb.compileStatement(
1889                "UPDATE " + Tables.RAW_CONTACTS +
1890                " SET " +
1891                        RawContacts.DISPLAY_NAME_SOURCE + "=?," +
1892                        RawContacts.DISPLAY_NAME_PRIMARY + "=?," +
1893                        RawContacts.DISPLAY_NAME_ALTERNATIVE + "=?," +
1894                        RawContacts.PHONETIC_NAME + "=?," +
1895                        RawContacts.PHONETIC_NAME_STYLE + "=?," +
1896                        RawContacts.SORT_KEY_PRIMARY + "=?," +
1897                        RawContacts.SORT_KEY_ALTERNATIVE + "=?" +
1898                " WHERE " + RawContacts._ID + "=?");
1899
1900        mLastStatusUpdate = mDb.compileStatement(
1901                "UPDATE " + Tables.CONTACTS +
1902                " SET " + ContactsColumns.LAST_STATUS_UPDATE_ID + "=" +
1903                        "(SELECT " + DataColumns.CONCRETE_ID +
1904                        " FROM " + Tables.STATUS_UPDATES +
1905                        " JOIN " + Tables.DATA +
1906                        "   ON (" + StatusUpdatesColumns.DATA_ID + "="
1907                                + DataColumns.CONCRETE_ID + ")" +
1908                        " JOIN " + Tables.RAW_CONTACTS +
1909                        "   ON (" + DataColumns.CONCRETE_RAW_CONTACT_ID + "="
1910                                + RawContactsColumns.CONCRETE_ID + ")" +
1911                        " WHERE " + RawContacts.CONTACT_ID + "=?" +
1912                        " ORDER BY " + StatusUpdates.STATUS_TIMESTAMP + " DESC,"
1913                                + StatusUpdates.STATUS +
1914                        " LIMIT 1)" +
1915                " WHERE " + ContactsColumns.CONCRETE_ID + "=?");
1916
1917        mNameLookupInsert = mDb.compileStatement("INSERT OR IGNORE INTO " + Tables.NAME_LOOKUP + "("
1918                + NameLookupColumns.RAW_CONTACT_ID + "," + NameLookupColumns.DATA_ID + ","
1919                + NameLookupColumns.NAME_TYPE + "," + NameLookupColumns.NORMALIZED_NAME
1920                + ") VALUES (?,?,?,?)");
1921        mNameLookupDelete = mDb.compileStatement("DELETE FROM " + Tables.NAME_LOOKUP + " WHERE "
1922                + NameLookupColumns.DATA_ID + "=?");
1923
1924        mStatusUpdateInsert = mDb.compileStatement(
1925                "INSERT INTO " + Tables.STATUS_UPDATES + "("
1926                        + StatusUpdatesColumns.DATA_ID + ", "
1927                        + StatusUpdates.STATUS + ","
1928                        + StatusUpdates.STATUS_RES_PACKAGE + ","
1929                        + StatusUpdates.STATUS_ICON + ","
1930                        + StatusUpdates.STATUS_LABEL + ")" +
1931                " VALUES (?,?,?,?,?)");
1932
1933        mStatusUpdateReplace = mDb.compileStatement(
1934                "INSERT OR REPLACE INTO " + Tables.STATUS_UPDATES + "("
1935                        + StatusUpdatesColumns.DATA_ID + ", "
1936                        + StatusUpdates.STATUS_TIMESTAMP + ","
1937                        + StatusUpdates.STATUS + ","
1938                        + StatusUpdates.STATUS_RES_PACKAGE + ","
1939                        + StatusUpdates.STATUS_ICON + ","
1940                        + StatusUpdates.STATUS_LABEL + ")" +
1941                " VALUES (?,?,?,?,?,?)");
1942
1943        mStatusUpdateAutoTimestamp = mDb.compileStatement(
1944                "UPDATE " + Tables.STATUS_UPDATES +
1945                " SET " + StatusUpdates.STATUS_TIMESTAMP + "=?,"
1946                        + StatusUpdates.STATUS + "=?" +
1947                " WHERE " + StatusUpdatesColumns.DATA_ID + "=?"
1948                        + " AND " + StatusUpdates.STATUS + "!=?");
1949
1950        mStatusAttributionUpdate = mDb.compileStatement(
1951                "UPDATE " + Tables.STATUS_UPDATES +
1952                " SET " + StatusUpdates.STATUS_RES_PACKAGE + "=?,"
1953                        + StatusUpdates.STATUS_ICON + "=?,"
1954                        + StatusUpdates.STATUS_LABEL + "=?" +
1955                " WHERE " + StatusUpdatesColumns.DATA_ID + "=?");
1956
1957        mStatusUpdateDelete = mDb.compileStatement(
1958                "DELETE FROM " + Tables.STATUS_UPDATES +
1959                " WHERE " + StatusUpdatesColumns.DATA_ID + "=?");
1960
1961        // When setting NAME_VERIFIED to 1 on a raw contact, reset it to 0
1962        // on all other raw contacts in the same aggregate
1963        mResetNameVerifiedForOtherRawContacts = mDb.compileStatement(
1964                "UPDATE " + Tables.RAW_CONTACTS +
1965                " SET " + RawContacts.NAME_VERIFIED + "=0" +
1966                " WHERE " + RawContacts.CONTACT_ID + "=(" +
1967                        "SELECT " + RawContacts.CONTACT_ID +
1968                        " FROM " + Tables.RAW_CONTACTS +
1969                        " WHERE " + RawContacts._ID + "=?)" +
1970                " AND " + RawContacts._ID + "!=?");
1971
1972        mDataRowHandlers = new HashMap<String, DataRowHandler>();
1973
1974        mDataRowHandlers.put(Email.CONTENT_ITEM_TYPE, new EmailDataRowHandler());
1975        mDataRowHandlers.put(Im.CONTENT_ITEM_TYPE,
1976                new CommonDataRowHandler(Im.CONTENT_ITEM_TYPE, Im.TYPE, Im.LABEL));
1977        mDataRowHandlers.put(Nickname.CONTENT_ITEM_TYPE, new CommonDataRowHandler(
1978                StructuredPostal.CONTENT_ITEM_TYPE, StructuredPostal.TYPE, StructuredPostal.LABEL));
1979        mDataRowHandlers.put(Organization.CONTENT_ITEM_TYPE, new OrganizationDataRowHandler());
1980        mDataRowHandlers.put(Phone.CONTENT_ITEM_TYPE, new PhoneDataRowHandler());
1981        mDataRowHandlers.put(Nickname.CONTENT_ITEM_TYPE, new NicknameDataRowHandler());
1982        mDataRowHandlers.put(StructuredName.CONTENT_ITEM_TYPE,
1983                new StructuredNameRowHandler(mNameSplitter));
1984        mDataRowHandlers.put(StructuredPostal.CONTENT_ITEM_TYPE,
1985                new StructuredPostalRowHandler(mPostalSplitter));
1986        mDataRowHandlers.put(GroupMembership.CONTENT_ITEM_TYPE, new GroupMembershipRowHandler());
1987        mDataRowHandlers.put(Photo.CONTENT_ITEM_TYPE, new PhotoDataRowHandler());
1988
1989        mMimeTypeIdEmail = mDbHelper.getMimeTypeId(Email.CONTENT_ITEM_TYPE);
1990        mMimeTypeIdIm = mDbHelper.getMimeTypeId(Im.CONTENT_ITEM_TYPE);
1991        mMimeTypeIdStructuredName = mDbHelper.getMimeTypeId(StructuredName.CONTENT_ITEM_TYPE);
1992        mMimeTypeIdOrganization = mDbHelper.getMimeTypeId(Organization.CONTENT_ITEM_TYPE);
1993        mMimeTypeIdNickname = mDbHelper.getMimeTypeId(Nickname.CONTENT_ITEM_TYPE);
1994        mMimeTypeIdPhone = mDbHelper.getMimeTypeId(Phone.CONTENT_ITEM_TYPE);
1995
1996        verifyAccounts();
1997        verifyLocale();
1998
1999        if (isLegacyContactImportNeeded()) {
2000            importLegacyContactsAsync();
2001        }
2002
2003        return (mDb != null);
2004    }
2005
2006    /**
2007     * Visible for testing.
2008     */
2009    /* package */ PhotoPriorityResolver createPhotoPriorityResolver(Context context) {
2010        return new PhotoPriorityResolver(context);
2011    }
2012
2013    /**
2014     * (Re)allocates all locale-sensitive structures.
2015     */
2016    private void initForDefaultLocale() {
2017        mCurrentLocale = getLocale();
2018        mNameSplitter = mDbHelper.createNameSplitter();
2019        mNameLookupBuilder = new StructuredNameLookupBuilder(mNameSplitter);
2020        mPostalSplitter = new PostalSplitter(mCurrentLocale);
2021        mCommonNicknameCache = new CommonNicknameCache(mDbHelper.getReadableDatabase());
2022        ContactLocaleUtils.getIntance().setLocale(mCurrentLocale);
2023    }
2024
2025    @Override
2026    public void onConfigurationChanged(Configuration newConfig) {
2027        initForDefaultLocale();
2028        verifyLocale();
2029    }
2030
2031    protected void verifyAccounts() {
2032        AccountManager.get(getContext()).addOnAccountsUpdatedListener(this, null, false);
2033        onAccountsUpdated(AccountManager.get(getContext()).getAccounts());
2034    }
2035
2036    /**
2037     * Verifies that the contacts database is properly configured for the current locale.
2038     * If not, changes the database locale to the current locale using an asynchronous task.
2039     * This needs to be done asynchronously because the process involves rebuilding
2040     * large data structures (name lookup, sort keys), which can take minutes on
2041     * a large set of contacts.
2042     */
2043    protected void verifyLocale() {
2044
2045        // The process is already running - postpone the change
2046        if (mProviderStatus == ProviderStatus.STATUS_CHANGING_LOCALE) {
2047            return;
2048        }
2049
2050        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
2051        final String providerLocale = prefs.getString(PREF_LOCALE, null);
2052        if (providerLocale == null) {
2053            // The provider has just been created for the first time. There are no
2054            // contacts in the database, so we can safely set locale on the UI thread.
2055            mDbHelper.setLocale(ContactsProvider2.this, mCurrentLocale);
2056            prefs.edit().putString(PREF_LOCALE, mCurrentLocale.toString()).commit();
2057            return;
2058        }
2059
2060        final Locale currentLocale = mCurrentLocale;
2061        if (currentLocale.toString().equals(providerLocale)) {
2062            return;
2063        }
2064
2065        int providerStatus = mProviderStatus;
2066        setProviderStatus(ProviderStatus.STATUS_CHANGING_LOCALE);
2067
2068        AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
2069
2070            int savedProviderStatus;
2071
2072            @Override
2073            protected Void doInBackground(Integer... params) {
2074                savedProviderStatus = params[0];
2075                mDbHelper.setLocale(ContactsProvider2.this, currentLocale);
2076                return null;
2077            }
2078
2079            @Override
2080            protected void onPostExecute(Void result) {
2081                prefs.edit().putString(PREF_LOCALE, currentLocale.toString()).commit();
2082                setProviderStatus(savedProviderStatus);
2083
2084                // Recursive invocation, needed to cover the case where locale
2085                // changes once and then changes again before the db upgrade is completed.
2086                verifyLocale();
2087            }
2088        };
2089
2090        task.execute(providerStatus);
2091    }
2092
2093    /* Visible for testing */
2094    @Override
2095    protected ContactsDatabaseHelper getDatabaseHelper(final Context context) {
2096        return ContactsDatabaseHelper.getInstance(context);
2097    }
2098
2099    /* package */ NameSplitter getNameSplitter() {
2100        return mNameSplitter;
2101    }
2102
2103    /* Visible for testing */
2104    protected Locale getLocale() {
2105        return Locale.getDefault();
2106    }
2107
2108    protected boolean isLegacyContactImportNeeded() {
2109        int version = Integer.parseInt(mDbHelper.getProperty(PROPERTY_CONTACTS_IMPORTED, "0"));
2110        return version < PROPERTY_CONTACTS_IMPORT_VERSION;
2111    }
2112
2113    protected LegacyContactImporter getLegacyContactImporter() {
2114        return new LegacyContactImporter(getContext(), this);
2115    }
2116
2117    /**
2118     * Imports legacy contacts in a separate thread.  As long as the import process is running
2119     * all other access to the contacts is blocked.
2120     */
2121    private void importLegacyContactsAsync() {
2122        Log.v(TAG, "Importing legacy contacts");
2123        setProviderStatus(ProviderStatus.STATUS_UPGRADING);
2124        if (mAccessLatch == null) {
2125            mAccessLatch = new CountDownLatch(1);
2126        }
2127
2128        Thread importThread = new Thread("LegacyContactImport") {
2129            @Override
2130            public void run() {
2131                LegacyContactImporter importer = getLegacyContactImporter();
2132                if (importLegacyContacts(importer)) {
2133                    onLegacyContactImportSuccess();
2134                } else {
2135                    onLegacyContactImportFailure();
2136                }
2137            }
2138        };
2139
2140        importThread.start();
2141    }
2142
2143    /**
2144     * Unlocks the provider and declares that the import process is complete.
2145     */
2146    private void onLegacyContactImportSuccess() {
2147        NotificationManager nm =
2148            (NotificationManager)getContext().getSystemService(Context.NOTIFICATION_SERVICE);
2149        nm.cancel(LEGACY_IMPORT_FAILED_NOTIFICATION);
2150
2151        // Store a property in the database indicating that the conversion process succeeded
2152        mDbHelper.setProperty(PROPERTY_CONTACTS_IMPORTED,
2153                String.valueOf(PROPERTY_CONTACTS_IMPORT_VERSION));
2154        setProviderStatus(ProviderStatus.STATUS_NORMAL);
2155        mAccessLatch.countDown();
2156        mAccessLatch = null;
2157        Log.v(TAG, "Completed import of legacy contacts");
2158    }
2159
2160    /**
2161     * Announces the provider status and keeps the provider locked.
2162     */
2163    private void onLegacyContactImportFailure() {
2164        Context context = getContext();
2165        NotificationManager nm =
2166            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
2167
2168        // Show a notification
2169        Notification n = new Notification(android.R.drawable.stat_notify_error,
2170                context.getString(R.string.upgrade_out_of_memory_notification_ticker),
2171                System.currentTimeMillis());
2172        n.setLatestEventInfo(context,
2173                context.getString(R.string.upgrade_out_of_memory_notification_title),
2174                context.getString(R.string.upgrade_out_of_memory_notification_text),
2175                PendingIntent.getActivity(context, 0, new Intent(Intents.UI.LIST_DEFAULT), 0));
2176        n.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
2177
2178        nm.notify(LEGACY_IMPORT_FAILED_NOTIFICATION, n);
2179
2180        setProviderStatus(ProviderStatus.STATUS_UPGRADE_OUT_OF_MEMORY);
2181        Log.v(TAG, "Failed to import legacy contacts");
2182    }
2183
2184    /* Visible for testing */
2185    /* package */ boolean importLegacyContacts(LegacyContactImporter importer) {
2186        boolean aggregatorEnabled = mContactAggregator.isEnabled();
2187        mContactAggregator.setEnabled(false);
2188        try {
2189            if (importer.importContacts()) {
2190
2191                // TODO aggregate all newly added raw contacts
2192                mContactAggregator.setEnabled(aggregatorEnabled);
2193                return true;
2194            }
2195        } catch (Throwable e) {
2196           Log.e(TAG, "Legacy contact import failed", e);
2197        }
2198        mEstimatedStorageRequirement = importer.getEstimatedStorageRequirement();
2199        return false;
2200    }
2201
2202    /**
2203     * Wipes all data from the contacts database.
2204     */
2205    /* package */ void wipeData() {
2206        mDbHelper.wipeData();
2207    }
2208
2209    /**
2210     * While importing and aggregating contacts, this content provider will
2211     * block all attempts to change contacts data. In particular, it will hold
2212     * up all contact syncs. As soon as the import process is complete, all
2213     * processes waiting to write to the provider are unblocked and can proceed
2214     * to compete for the database transaction monitor.
2215     */
2216    private void waitForAccess() {
2217        CountDownLatch latch = mAccessLatch;
2218        if (latch != null) {
2219            while (true) {
2220                try {
2221                    latch.await();
2222                    mAccessLatch = null;
2223                    return;
2224                } catch (InterruptedException e) {
2225                    Thread.currentThread().interrupt();
2226                }
2227            }
2228        }
2229    }
2230
2231    @Override
2232    public Uri insert(Uri uri, ContentValues values) {
2233        waitForAccess();
2234        return super.insert(uri, values);
2235    }
2236
2237    @Override
2238    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
2239        if (mAccessLatch != null) {
2240            // We are stuck trying to upgrade contacts db.  The only update request
2241            // allowed in this case is an update of provider status, which will trigger
2242            // an attempt to upgrade contacts again.
2243            int match = sUriMatcher.match(uri);
2244            if (match == PROVIDER_STATUS && isLegacyContactImportNeeded()) {
2245                Integer newStatus = values.getAsInteger(ProviderStatus.STATUS);
2246                if (newStatus != null && newStatus == ProviderStatus.STATUS_UPGRADING) {
2247                    importLegacyContactsAsync();
2248                    return 1;
2249                } else {
2250                    return 0;
2251                }
2252            }
2253        }
2254        waitForAccess();
2255        return super.update(uri, values, selection, selectionArgs);
2256    }
2257
2258    @Override
2259    public int delete(Uri uri, String selection, String[] selectionArgs) {
2260        waitForAccess();
2261        return super.delete(uri, selection, selectionArgs);
2262    }
2263
2264    @Override
2265    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
2266            throws OperationApplicationException {
2267        waitForAccess();
2268        return super.applyBatch(operations);
2269    }
2270
2271    @Override
2272    protected void onBeginTransaction() {
2273        if (VERBOSE_LOGGING) {
2274            Log.v(TAG, "onBeginTransaction");
2275        }
2276        super.onBeginTransaction();
2277        mContactAggregator.clearPendingAggregations();
2278        clearTransactionalChanges();
2279    }
2280
2281    private void clearTransactionalChanges() {
2282        mInsertedRawContacts.clear();
2283        mUpdatedRawContacts.clear();
2284        mUpdatedSyncStates.clear();
2285        mDirtyRawContacts.clear();
2286    }
2287
2288    @Override
2289    protected void beforeTransactionCommit() {
2290
2291        if (VERBOSE_LOGGING) {
2292            Log.v(TAG, "beforeTransactionCommit");
2293        }
2294        super.beforeTransactionCommit();
2295        flushTransactionalChanges();
2296        mContactAggregator.aggregateInTransaction(mDb);
2297        if (mVisibleTouched) {
2298            mVisibleTouched = false;
2299            mDbHelper.updateAllVisible();
2300        }
2301    }
2302
2303    private void flushTransactionalChanges() {
2304        if (VERBOSE_LOGGING) {
2305            Log.v(TAG, "flushTransactionChanges");
2306        }
2307
2308        for (long rawContactId : mInsertedRawContacts.keySet()) {
2309            updateRawContactDisplayName(mDb, rawContactId);
2310            mContactAggregator.onRawContactInsert(mDb, rawContactId);
2311        }
2312
2313        if (!mDirtyRawContacts.isEmpty()) {
2314            mSb.setLength(0);
2315            mSb.append(UPDATE_RAW_CONTACT_SET_DIRTY_SQL);
2316            appendIds(mSb, mDirtyRawContacts);
2317            mSb.append(")");
2318            mDb.execSQL(mSb.toString());
2319        }
2320
2321        if (!mUpdatedRawContacts.isEmpty()) {
2322            mSb.setLength(0);
2323            mSb.append(UPDATE_RAW_CONTACT_SET_VERSION_SQL);
2324            appendIds(mSb, mUpdatedRawContacts);
2325            mSb.append(")");
2326            mDb.execSQL(mSb.toString());
2327        }
2328
2329        for (Map.Entry<Long, Object> entry : mUpdatedSyncStates.entrySet()) {
2330            long id = entry.getKey();
2331            if (mDbHelper.getSyncState().update(mDb, id, entry.getValue()) <= 0) {
2332                throw new IllegalStateException(
2333                        "unable to update sync state, does it still exist?");
2334            }
2335        }
2336
2337        clearTransactionalChanges();
2338    }
2339
2340    /**
2341     * Appends comma separated ids.
2342     * @param ids Should not be empty
2343     */
2344    private void appendIds(StringBuilder sb, HashSet<Long> ids) {
2345        for (long id : ids) {
2346            sb.append(id).append(',');
2347        }
2348
2349        sb.setLength(sb.length() - 1); // Yank the last comma
2350    }
2351
2352    @Override
2353    protected void notifyChange() {
2354        notifyChange(mSyncToNetwork);
2355        mSyncToNetwork = false;
2356    }
2357
2358    protected void notifyChange(boolean syncToNetwork) {
2359        getContext().getContentResolver().notifyChange(ContactsContract.AUTHORITY_URI, null,
2360                syncToNetwork);
2361    }
2362
2363    protected void setProviderStatus(int status) {
2364        mProviderStatus = status;
2365        getContext().getContentResolver().notifyChange(ContactsContract.ProviderStatus.CONTENT_URI,
2366                null, false);
2367    }
2368
2369    private boolean isNewRawContact(long rawContactId) {
2370        return mInsertedRawContacts.containsKey(rawContactId);
2371    }
2372
2373    private DataRowHandler getDataRowHandler(final String mimeType) {
2374        DataRowHandler handler = mDataRowHandlers.get(mimeType);
2375        if (handler == null) {
2376            handler = new CustomDataRowHandler(mimeType);
2377            mDataRowHandlers.put(mimeType, handler);
2378        }
2379        return handler;
2380    }
2381
2382    @Override
2383    protected Uri insertInTransaction(Uri uri, ContentValues values) {
2384        if (VERBOSE_LOGGING) {
2385            Log.v(TAG, "insertInTransaction: " + uri + " " + values);
2386        }
2387
2388        final boolean callerIsSyncAdapter =
2389                readBooleanQueryParameter(uri, ContactsContract.CALLER_IS_SYNCADAPTER, false);
2390
2391        final int match = sUriMatcher.match(uri);
2392        long id = 0;
2393
2394        switch (match) {
2395            case SYNCSTATE:
2396                id = mDbHelper.getSyncState().insert(mDb, values);
2397                break;
2398
2399            case CONTACTS: {
2400                insertContact(values);
2401                break;
2402            }
2403
2404            case RAW_CONTACTS: {
2405                id = insertRawContact(uri, values);
2406                mSyncToNetwork |= !callerIsSyncAdapter;
2407                break;
2408            }
2409
2410            case RAW_CONTACTS_DATA: {
2411                values.put(Data.RAW_CONTACT_ID, uri.getPathSegments().get(1));
2412                id = insertData(values, callerIsSyncAdapter);
2413                mSyncToNetwork |= !callerIsSyncAdapter;
2414                break;
2415            }
2416
2417            case DATA: {
2418                id = insertData(values, callerIsSyncAdapter);
2419                mSyncToNetwork |= !callerIsSyncAdapter;
2420                break;
2421            }
2422
2423            case GROUPS: {
2424                id = insertGroup(uri, values, callerIsSyncAdapter);
2425                mSyncToNetwork |= !callerIsSyncAdapter;
2426                break;
2427            }
2428
2429            case SETTINGS: {
2430                id = insertSettings(uri, values);
2431                mSyncToNetwork |= !callerIsSyncAdapter;
2432                break;
2433            }
2434
2435            case STATUS_UPDATES: {
2436                id = insertStatusUpdate(values);
2437                break;
2438            }
2439
2440            default:
2441                mSyncToNetwork = true;
2442                return mLegacyApiSupport.insert(uri, values);
2443        }
2444
2445        if (id < 0) {
2446            return null;
2447        }
2448
2449        return ContentUris.withAppendedId(uri, id);
2450    }
2451
2452    /**
2453     * If account is non-null then store it in the values. If the account is
2454     * already specified in the values then it must be consistent with the
2455     * account, if it is non-null.
2456     *
2457     * @param uri Current {@link Uri} being operated on.
2458     * @param values {@link ContentValues} to read and possibly update.
2459     * @throws IllegalArgumentException when only one of
2460     *             {@link RawContacts#ACCOUNT_NAME} or
2461     *             {@link RawContacts#ACCOUNT_TYPE} is specified, leaving the
2462     *             other undefined.
2463     * @throws IllegalArgumentException when {@link RawContacts#ACCOUNT_NAME}
2464     *             and {@link RawContacts#ACCOUNT_TYPE} are inconsistent between
2465     *             the given {@link Uri} and {@link ContentValues}.
2466     */
2467    private Account resolveAccount(Uri uri, ContentValues values) throws IllegalArgumentException {
2468        String accountName = getQueryParameter(uri, RawContacts.ACCOUNT_NAME);
2469        String accountType = getQueryParameter(uri, RawContacts.ACCOUNT_TYPE);
2470        final boolean partialUri = TextUtils.isEmpty(accountName) ^ TextUtils.isEmpty(accountType);
2471
2472        String valueAccountName = values.getAsString(RawContacts.ACCOUNT_NAME);
2473        String valueAccountType = values.getAsString(RawContacts.ACCOUNT_TYPE);
2474        final boolean partialValues = TextUtils.isEmpty(valueAccountName)
2475                ^ TextUtils.isEmpty(valueAccountType);
2476
2477        if (partialUri || partialValues) {
2478            // Throw when either account is incomplete
2479            throw new IllegalArgumentException(mDbHelper.exceptionMessage(
2480                    "Must specify both or neither of ACCOUNT_NAME and ACCOUNT_TYPE", uri));
2481        }
2482
2483        // Accounts are valid by only checking one parameter, since we've
2484        // already ruled out partial accounts.
2485        final boolean validUri = !TextUtils.isEmpty(accountName);
2486        final boolean validValues = !TextUtils.isEmpty(valueAccountName);
2487
2488        if (validValues && validUri) {
2489            // Check that accounts match when both present
2490            final boolean accountMatch = TextUtils.equals(accountName, valueAccountName)
2491                    && TextUtils.equals(accountType, valueAccountType);
2492            if (!accountMatch) {
2493                throw new IllegalArgumentException(mDbHelper.exceptionMessage(
2494                        "When both specified, ACCOUNT_NAME and ACCOUNT_TYPE must match", uri));
2495            }
2496        } else if (validUri) {
2497            // Fill values from Uri when not present
2498            values.put(RawContacts.ACCOUNT_NAME, accountName);
2499            values.put(RawContacts.ACCOUNT_TYPE, accountType);
2500        } else if (validValues) {
2501            accountName = valueAccountName;
2502            accountType = valueAccountType;
2503        } else {
2504            return null;
2505        }
2506
2507        // Use cached Account object when matches, otherwise create
2508        if (mAccount == null
2509                || !mAccount.name.equals(accountName)
2510                || !mAccount.type.equals(accountType)) {
2511            mAccount = new Account(accountName, accountType);
2512        }
2513
2514        return mAccount;
2515    }
2516
2517    /**
2518     * Inserts an item in the contacts table
2519     *
2520     * @param values the values for the new row
2521     * @return the row ID of the newly created row
2522     */
2523    private long insertContact(ContentValues values) {
2524        throw new UnsupportedOperationException("Aggregate contacts are created automatically");
2525    }
2526
2527    /**
2528     * Inserts an item in the contacts table
2529     *
2530     * @param uri the values for the new row
2531     * @param values the account this contact should be associated with. may be null.
2532     * @return the row ID of the newly created row
2533     */
2534    private long insertRawContact(Uri uri, ContentValues values) {
2535        mValues.clear();
2536        mValues.putAll(values);
2537        mValues.putNull(RawContacts.CONTACT_ID);
2538
2539        final Account account = resolveAccount(uri, mValues);
2540
2541        if (values.containsKey(RawContacts.DELETED)
2542                && values.getAsInteger(RawContacts.DELETED) != 0) {
2543            mValues.put(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DISABLED);
2544        }
2545
2546        long rawContactId = mDb.insert(Tables.RAW_CONTACTS, RawContacts.CONTACT_ID, mValues);
2547        int aggregationMode = RawContacts.AGGREGATION_MODE_DEFAULT;
2548        if (mValues.containsKey(RawContacts.AGGREGATION_MODE)) {
2549            aggregationMode = mValues.getAsInteger(RawContacts.AGGREGATION_MODE);
2550        }
2551        mContactAggregator.markNewForAggregation(rawContactId, aggregationMode);
2552
2553        // Trigger creation of a Contact based on this RawContact at the end of transaction
2554        mInsertedRawContacts.put(rawContactId, account);
2555
2556        return rawContactId;
2557    }
2558
2559    /**
2560     * Inserts an item in the data table
2561     *
2562     * @param values the values for the new row
2563     * @return the row ID of the newly created row
2564     */
2565    private long insertData(ContentValues values, boolean callerIsSyncAdapter) {
2566        long id = 0;
2567        mValues.clear();
2568        mValues.putAll(values);
2569
2570        long rawContactId = mValues.getAsLong(Data.RAW_CONTACT_ID);
2571
2572        // Replace package with internal mapping
2573        final String packageName = mValues.getAsString(Data.RES_PACKAGE);
2574        if (packageName != null) {
2575            mValues.put(DataColumns.PACKAGE_ID, mDbHelper.getPackageId(packageName));
2576        }
2577        mValues.remove(Data.RES_PACKAGE);
2578
2579        // Replace mimetype with internal mapping
2580        final String mimeType = mValues.getAsString(Data.MIMETYPE);
2581        if (TextUtils.isEmpty(mimeType)) {
2582            throw new IllegalArgumentException(Data.MIMETYPE + " is required");
2583        }
2584
2585        mValues.put(DataColumns.MIMETYPE_ID, mDbHelper.getMimeTypeId(mimeType));
2586        mValues.remove(Data.MIMETYPE);
2587
2588        DataRowHandler rowHandler = getDataRowHandler(mimeType);
2589        id = rowHandler.insert(mDb, rawContactId, mValues);
2590        if (!callerIsSyncAdapter) {
2591            setRawContactDirty(rawContactId);
2592        }
2593        mUpdatedRawContacts.add(rawContactId);
2594        return id;
2595    }
2596
2597    private void triggerAggregation(long rawContactId) {
2598        if (!mContactAggregator.isEnabled()) {
2599            return;
2600        }
2601
2602        int aggregationMode = mDbHelper.getAggregationMode(rawContactId);
2603        switch (aggregationMode) {
2604            case RawContacts.AGGREGATION_MODE_DISABLED:
2605                break;
2606
2607            case RawContacts.AGGREGATION_MODE_DEFAULT: {
2608                mContactAggregator.markForAggregation(rawContactId, aggregationMode);
2609                break;
2610            }
2611
2612            case RawContacts.AGGREGATION_MODE_SUSPENDED: {
2613                long contactId = mDbHelper.getContactId(rawContactId);
2614
2615                if (contactId != 0) {
2616                    mContactAggregator.updateAggregateData(contactId);
2617                }
2618                break;
2619            }
2620
2621            case RawContacts.AGGREGATION_MODE_IMMEDIATE: {
2622                long contactId = mDbHelper.getContactId(rawContactId);
2623                mContactAggregator.aggregateContact(mDb, rawContactId, contactId);
2624                break;
2625            }
2626        }
2627    }
2628
2629    /**
2630     * Returns the group id of the group with sourceId and the same account as rawContactId.
2631     * If the group doesn't already exist then it is first created,
2632     * @param db SQLiteDatabase to use for this operation
2633     * @param rawContactId the contact this group is associated with
2634     * @param sourceId the sourceIf of the group to query or create
2635     * @return the group id of the existing or created group
2636     * @throws IllegalArgumentException if the contact is not associated with an account
2637     * @throws IllegalStateException if a group needs to be created but the creation failed
2638     */
2639    private long getOrMakeGroup(SQLiteDatabase db, long rawContactId, String sourceId,
2640            Account account) {
2641
2642        if (account == null) {
2643            mSelectionArgs1[0] = String.valueOf(rawContactId);
2644            Cursor c = db.query(RawContactsQuery.TABLE, RawContactsQuery.COLUMNS,
2645                    RawContacts._ID + "=?", mSelectionArgs1, null, null, null);
2646            try {
2647                if (c.moveToFirst()) {
2648                    String accountName = c.getString(RawContactsQuery.ACCOUNT_NAME);
2649                    String accountType = c.getString(RawContactsQuery.ACCOUNT_TYPE);
2650                    if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) {
2651                        account = new Account(accountName, accountType);
2652                    }
2653                }
2654            } finally {
2655                c.close();
2656            }
2657        }
2658
2659        if (account == null) {
2660            throw new IllegalArgumentException("if the groupmembership only "
2661                    + "has a sourceid the the contact must be associated with "
2662                    + "an account");
2663        }
2664
2665        ArrayList<GroupIdCacheEntry> entries = mGroupIdCache.get(sourceId);
2666        if (entries == null) {
2667            entries = new ArrayList<GroupIdCacheEntry>(1);
2668            mGroupIdCache.put(sourceId, entries);
2669        }
2670
2671        int count = entries.size();
2672        for (int i = 0; i < count; i++) {
2673            GroupIdCacheEntry entry = entries.get(i);
2674            if (entry.accountName.equals(account.name) && entry.accountType.equals(account.type)) {
2675                return entry.groupId;
2676            }
2677        }
2678
2679        GroupIdCacheEntry entry = new GroupIdCacheEntry();
2680        entry.accountName = account.name;
2681        entry.accountType = account.type;
2682        entry.sourceId = sourceId;
2683        entries.add(0, entry);
2684
2685        // look up the group that contains this sourceId and has the same account name and type
2686        // as the contact refered to by rawContactId
2687        Cursor c = db.query(Tables.GROUPS, new String[]{RawContacts._ID},
2688                Clauses.GROUP_HAS_ACCOUNT_AND_SOURCE_ID,
2689                new String[]{sourceId, account.name, account.type}, null, null, null);
2690        try {
2691            if (c.moveToFirst()) {
2692                entry.groupId = c.getLong(0);
2693            } else {
2694                ContentValues groupValues = new ContentValues();
2695                groupValues.put(Groups.ACCOUNT_NAME, account.name);
2696                groupValues.put(Groups.ACCOUNT_TYPE, account.type);
2697                groupValues.put(Groups.SOURCE_ID, sourceId);
2698                long groupId = db.insert(Tables.GROUPS, Groups.ACCOUNT_NAME, groupValues);
2699                if (groupId < 0) {
2700                    throw new IllegalStateException("unable to create a new group with "
2701                            + "this sourceid: " + groupValues);
2702                }
2703                entry.groupId = groupId;
2704            }
2705        } finally {
2706            c.close();
2707        }
2708
2709        return entry.groupId;
2710    }
2711
2712    private interface DisplayNameQuery {
2713        public static final String RAW_SQL =
2714                "SELECT "
2715                        + DataColumns.MIMETYPE_ID + ","
2716                        + Data.IS_PRIMARY + ","
2717                        + Data.DATA1 + ","
2718                        + Data.DATA2 + ","
2719                        + Data.DATA3 + ","
2720                        + Data.DATA4 + ","
2721                        + Data.DATA5 + ","
2722                        + Data.DATA6 + ","
2723                        + Data.DATA7 + ","
2724                        + Data.DATA8 + ","
2725                        + Data.DATA9 + ","
2726                        + Data.DATA10 + ","
2727                        + Data.DATA11 +
2728                " FROM " + Tables.DATA +
2729                " WHERE " + Data.RAW_CONTACT_ID + "=?" +
2730                        " AND (" + Data.DATA1 + " NOT NULL OR " +
2731                                Organization.TITLE + " NOT NULL)";
2732
2733        public static final int MIMETYPE = 0;
2734        public static final int IS_PRIMARY = 1;
2735        public static final int DATA1 = 2;
2736        public static final int GIVEN_NAME = 3;                         // data2
2737        public static final int FAMILY_NAME = 4;                        // data3
2738        public static final int PREFIX = 5;                             // data4
2739        public static final int TITLE = 5;                              // data4
2740        public static final int MIDDLE_NAME = 6;                        // data5
2741        public static final int SUFFIX = 7;                             // data6
2742        public static final int PHONETIC_GIVEN_NAME = 8;                // data7
2743        public static final int PHONETIC_MIDDLE_NAME = 9;               // data8
2744        public static final int ORGANIZATION_PHONETIC_NAME = 9;         // data8
2745        public static final int PHONETIC_FAMILY_NAME = 10;              // data9
2746        public static final int FULL_NAME_STYLE = 11;                   // data10
2747        public static final int ORGANIZATION_PHONETIC_NAME_STYLE = 11;  // data10
2748        public static final int PHONETIC_NAME_STYLE = 12;               // data11
2749    }
2750
2751    /**
2752     * Updates a raw contact display name based on data rows, e.g. structured name,
2753     * organization, email etc.
2754     */
2755    public void updateRawContactDisplayName(SQLiteDatabase db, long rawContactId) {
2756        int bestDisplayNameSource = DisplayNameSources.UNDEFINED;
2757        NameSplitter.Name bestName = null;
2758        String bestDisplayName = null;
2759        String bestPhoneticName = null;
2760        int bestPhoneticNameStyle = PhoneticNameStyle.UNDEFINED;
2761
2762        mSelectionArgs1[0] = String.valueOf(rawContactId);
2763        Cursor c = db.rawQuery(DisplayNameQuery.RAW_SQL, mSelectionArgs1);
2764        try {
2765            while (c.moveToNext()) {
2766                int mimeType = c.getInt(DisplayNameQuery.MIMETYPE);
2767                int source = getDisplayNameSource(mimeType);
2768                if (source < bestDisplayNameSource || source == DisplayNameSources.UNDEFINED) {
2769                    continue;
2770                }
2771
2772                if (source == bestDisplayNameSource && c.getInt(DisplayNameQuery.IS_PRIMARY) == 0) {
2773                    continue;
2774                }
2775
2776                if (mimeType == mMimeTypeIdStructuredName) {
2777                    NameSplitter.Name name;
2778                    if (bestName != null) {
2779                        name = new NameSplitter.Name();
2780                    } else {
2781                        name = mName;
2782                        name.clear();
2783                    }
2784                    name.prefix = c.getString(DisplayNameQuery.PREFIX);
2785                    name.givenNames = c.getString(DisplayNameQuery.GIVEN_NAME);
2786                    name.middleName = c.getString(DisplayNameQuery.MIDDLE_NAME);
2787                    name.familyName = c.getString(DisplayNameQuery.FAMILY_NAME);
2788                    name.suffix = c.getString(DisplayNameQuery.SUFFIX);
2789                    name.fullNameStyle = c.isNull(DisplayNameQuery.FULL_NAME_STYLE)
2790                            ? FullNameStyle.UNDEFINED
2791                            : c.getInt(DisplayNameQuery.FULL_NAME_STYLE);
2792                    name.phoneticFamilyName = c.getString(DisplayNameQuery.PHONETIC_FAMILY_NAME);
2793                    name.phoneticMiddleName = c.getString(DisplayNameQuery.PHONETIC_MIDDLE_NAME);
2794                    name.phoneticGivenName = c.getString(DisplayNameQuery.PHONETIC_GIVEN_NAME);
2795                    name.phoneticNameStyle = c.isNull(DisplayNameQuery.PHONETIC_NAME_STYLE)
2796                            ? PhoneticNameStyle.UNDEFINED
2797                            : c.getInt(DisplayNameQuery.PHONETIC_NAME_STYLE);
2798                    if (!name.isEmpty()) {
2799                        bestDisplayNameSource = source;
2800                        bestName = name;
2801                    }
2802                } else if (mimeType == mMimeTypeIdOrganization) {
2803                    mCharArrayBuffer.sizeCopied = 0;
2804                    c.copyStringToBuffer(DisplayNameQuery.DATA1, mCharArrayBuffer);
2805                    if (mCharArrayBuffer.sizeCopied != 0) {
2806                        bestDisplayNameSource = source;
2807                        bestDisplayName = new String(mCharArrayBuffer.data, 0,
2808                                mCharArrayBuffer.sizeCopied);
2809                        bestPhoneticName = c.getString(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME);
2810                        bestPhoneticNameStyle =
2811                                c.isNull(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME_STYLE)
2812                                    ? PhoneticNameStyle.UNDEFINED
2813                                    : c.getInt(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME_STYLE);
2814                    } else {
2815                        c.copyStringToBuffer(DisplayNameQuery.TITLE, mCharArrayBuffer);
2816                        if (mCharArrayBuffer.sizeCopied != 0) {
2817                            bestDisplayNameSource = source;
2818                            bestDisplayName = new String(mCharArrayBuffer.data, 0,
2819                                    mCharArrayBuffer.sizeCopied);
2820                            bestPhoneticName = null;
2821                            bestPhoneticNameStyle = PhoneticNameStyle.UNDEFINED;
2822                        }
2823                    }
2824                } else {
2825                    // Display name is at DATA1 in all other types.
2826                    // This is ensured in the constructor.
2827
2828                    mCharArrayBuffer.sizeCopied = 0;
2829                    c.copyStringToBuffer(DisplayNameQuery.DATA1, mCharArrayBuffer);
2830                    if (mCharArrayBuffer.sizeCopied != 0) {
2831                        bestDisplayNameSource = source;
2832                        bestDisplayName = new String(mCharArrayBuffer.data, 0,
2833                                mCharArrayBuffer.sizeCopied);
2834                        bestPhoneticName = null;
2835                        bestPhoneticNameStyle = PhoneticNameStyle.UNDEFINED;
2836                    }
2837                }
2838            }
2839
2840        } finally {
2841            c.close();
2842        }
2843
2844        String displayNamePrimary;
2845        String displayNameAlternative;
2846        String sortKeyPrimary = null;
2847        String sortKeyAlternative = null;
2848        int displayNameStyle = FullNameStyle.UNDEFINED;
2849
2850        if (bestDisplayNameSource == DisplayNameSources.STRUCTURED_NAME) {
2851            displayNameStyle = bestName.fullNameStyle;
2852            if (displayNameStyle == FullNameStyle.CJK
2853                    || displayNameStyle == FullNameStyle.UNDEFINED) {
2854                displayNameStyle = mNameSplitter.getAdjustedFullNameStyle(displayNameStyle);
2855                bestName.fullNameStyle = displayNameStyle;
2856            }
2857
2858            displayNamePrimary = mNameSplitter.join(bestName, true);
2859            displayNameAlternative = mNameSplitter.join(bestName, false);
2860
2861            bestPhoneticName = mNameSplitter.joinPhoneticName(bestName);
2862            bestPhoneticNameStyle = bestName.phoneticNameStyle;
2863        } else {
2864            displayNamePrimary = displayNameAlternative = bestDisplayName;
2865        }
2866
2867        if (bestPhoneticName != null) {
2868            sortKeyPrimary = sortKeyAlternative = bestPhoneticName;
2869            if (bestPhoneticNameStyle == PhoneticNameStyle.UNDEFINED) {
2870                bestPhoneticNameStyle = mNameSplitter.guessPhoneticNameStyle(bestPhoneticName);
2871            }
2872        } else {
2873            if (displayNameStyle == FullNameStyle.UNDEFINED) {
2874                displayNameStyle = mNameSplitter.guessFullNameStyle(bestDisplayName);
2875                if (displayNameStyle == FullNameStyle.UNDEFINED
2876                        || displayNameStyle == FullNameStyle.CJK) {
2877                    displayNameStyle = mNameSplitter.getAdjustedNameStyleBasedOnPhoneticNameStyle(
2878                            displayNameStyle, bestPhoneticNameStyle);
2879                }
2880                displayNameStyle = mNameSplitter.getAdjustedFullNameStyle(displayNameStyle);
2881            }
2882            if (displayNameStyle == FullNameStyle.CHINESE) {
2883                sortKeyPrimary = sortKeyAlternative =
2884                        ContactLocaleUtils.getIntance().getSortKey(
2885                                displayNamePrimary, FullNameStyle.CHINESE);
2886            }
2887        }
2888
2889        if (sortKeyPrimary == null) {
2890            sortKeyPrimary = displayNamePrimary;
2891            sortKeyAlternative = displayNameAlternative;
2892        }
2893
2894        setDisplayName(rawContactId, bestDisplayNameSource, displayNamePrimary,
2895                displayNameAlternative, bestPhoneticName, bestPhoneticNameStyle,
2896                sortKeyPrimary, sortKeyAlternative);
2897    }
2898
2899    private int getDisplayNameSource(int mimeTypeId) {
2900        if (mimeTypeId == mMimeTypeIdStructuredName) {
2901            return DisplayNameSources.STRUCTURED_NAME;
2902        } else if (mimeTypeId == mMimeTypeIdEmail) {
2903            return DisplayNameSources.EMAIL;
2904        } else if (mimeTypeId == mMimeTypeIdPhone) {
2905            return DisplayNameSources.PHONE;
2906        } else if (mimeTypeId == mMimeTypeIdOrganization) {
2907            return DisplayNameSources.ORGANIZATION;
2908        } else if (mimeTypeId == mMimeTypeIdNickname) {
2909            return DisplayNameSources.NICKNAME;
2910        } else {
2911            return DisplayNameSources.UNDEFINED;
2912        }
2913    }
2914
2915    /**
2916     * Delete data row by row so that fixing of primaries etc work correctly.
2917     */
2918    private int deleteData(String selection, String[] selectionArgs, boolean callerIsSyncAdapter) {
2919        int count = 0;
2920
2921        // Note that the query will return data according to the access restrictions,
2922        // so we don't need to worry about deleting data we don't have permission to read.
2923        Cursor c = query(Data.CONTENT_URI, DataDeleteQuery.COLUMNS, selection, selectionArgs, null);
2924        try {
2925            while(c.moveToNext()) {
2926                long rawContactId = c.getLong(DataDeleteQuery.RAW_CONTACT_ID);
2927                String mimeType = c.getString(DataDeleteQuery.MIMETYPE);
2928                DataRowHandler rowHandler = getDataRowHandler(mimeType);
2929                count += rowHandler.delete(mDb, c);
2930                if (!callerIsSyncAdapter) {
2931                    setRawContactDirty(rawContactId);
2932                }
2933            }
2934        } finally {
2935            c.close();
2936        }
2937
2938        return count;
2939    }
2940
2941    /**
2942     * Delete a data row provided that it is one of the allowed mime types.
2943     */
2944    public int deleteData(long dataId, String[] allowedMimeTypes) {
2945
2946        // Note that the query will return data according to the access restrictions,
2947        // so we don't need to worry about deleting data we don't have permission to read.
2948        mSelectionArgs1[0] = String.valueOf(dataId);
2949        Cursor c = query(Data.CONTENT_URI, DataDeleteQuery.COLUMNS, Data._ID + "=?",
2950                mSelectionArgs1, null);
2951
2952        try {
2953            if (!c.moveToFirst()) {
2954                return 0;
2955            }
2956
2957            String mimeType = c.getString(DataDeleteQuery.MIMETYPE);
2958            boolean valid = false;
2959            for (int i = 0; i < allowedMimeTypes.length; i++) {
2960                if (TextUtils.equals(mimeType, allowedMimeTypes[i])) {
2961                    valid = true;
2962                    break;
2963                }
2964            }
2965
2966            if (!valid) {
2967                throw new IllegalArgumentException("Data type mismatch: expected "
2968                        + Lists.newArrayList(allowedMimeTypes));
2969            }
2970
2971            DataRowHandler rowHandler = getDataRowHandler(mimeType);
2972            return rowHandler.delete(mDb, c);
2973        } finally {
2974            c.close();
2975        }
2976    }
2977
2978    /**
2979     * Inserts an item in the groups table
2980     */
2981    private long insertGroup(Uri uri, ContentValues values, boolean callerIsSyncAdapter) {
2982        mValues.clear();
2983        mValues.putAll(values);
2984
2985        final Account account = resolveAccount(uri, mValues);
2986
2987        // Replace package with internal mapping
2988        final String packageName = mValues.getAsString(Groups.RES_PACKAGE);
2989        if (packageName != null) {
2990            mValues.put(GroupsColumns.PACKAGE_ID, mDbHelper.getPackageId(packageName));
2991        }
2992        mValues.remove(Groups.RES_PACKAGE);
2993
2994        if (!callerIsSyncAdapter) {
2995            mValues.put(Groups.DIRTY, 1);
2996        }
2997
2998        long result = mDb.insert(Tables.GROUPS, Groups.TITLE, mValues);
2999
3000        if (mValues.containsKey(Groups.GROUP_VISIBLE)) {
3001            mVisibleTouched = true;
3002        }
3003
3004        return result;
3005    }
3006
3007    private long insertSettings(Uri uri, ContentValues values) {
3008        final long id = mDb.insert(Tables.SETTINGS, null, values);
3009
3010        if (values.containsKey(Settings.UNGROUPED_VISIBLE)) {
3011            mVisibleTouched = true;
3012        }
3013
3014        return id;
3015    }
3016
3017    /**
3018     * Inserts a status update.
3019     */
3020    public long insertStatusUpdate(ContentValues values) {
3021        final String handle = values.getAsString(StatusUpdates.IM_HANDLE);
3022        final Integer protocol = values.getAsInteger(StatusUpdates.PROTOCOL);
3023        String customProtocol = null;
3024
3025        if (protocol != null && protocol == Im.PROTOCOL_CUSTOM) {
3026            customProtocol = values.getAsString(StatusUpdates.CUSTOM_PROTOCOL);
3027            if (TextUtils.isEmpty(customProtocol)) {
3028                throw new IllegalArgumentException(
3029                        "CUSTOM_PROTOCOL is required when PROTOCOL=PROTOCOL_CUSTOM");
3030            }
3031        }
3032
3033        long rawContactId = -1;
3034        long contactId = -1;
3035        Long dataId = values.getAsLong(StatusUpdates.DATA_ID);
3036        mSb.setLength(0);
3037        mSelectionArgs.clear();
3038        if (dataId != null) {
3039            // Lookup the contact info for the given data row.
3040
3041            mSb.append(Tables.DATA + "." + Data._ID + "=?");
3042            mSelectionArgs.add(String.valueOf(dataId));
3043        } else {
3044            // Lookup the data row to attach this presence update to
3045
3046            if (TextUtils.isEmpty(handle) || protocol == null) {
3047                throw new IllegalArgumentException("PROTOCOL and IM_HANDLE are required");
3048            }
3049
3050            // TODO: generalize to allow other providers to match against email
3051            boolean matchEmail = Im.PROTOCOL_GOOGLE_TALK == protocol;
3052
3053            String mimeTypeIdIm = String.valueOf(mMimeTypeIdIm);
3054            if (matchEmail) {
3055                String mimeTypeIdEmail = String.valueOf(mMimeTypeIdEmail);
3056
3057                // The following hack forces SQLite to use the (mimetype_id,data1) index, otherwise
3058                // the "OR" conjunction confuses it and it switches to a full scan of
3059                // the raw_contacts table.
3060
3061                // This code relies on the fact that Im.DATA and Email.DATA are in fact the same
3062                // column - Data.DATA1
3063                mSb.append(DataColumns.MIMETYPE_ID + " IN (?,?)" +
3064                        " AND " + Data.DATA1 + "=?" +
3065                        " AND ((" + DataColumns.MIMETYPE_ID + "=? AND " + Im.PROTOCOL + "=?");
3066                mSelectionArgs.add(mimeTypeIdEmail);
3067                mSelectionArgs.add(mimeTypeIdIm);
3068                mSelectionArgs.add(handle);
3069                mSelectionArgs.add(mimeTypeIdIm);
3070                mSelectionArgs.add(String.valueOf(protocol));
3071                if (customProtocol != null) {
3072                    mSb.append(" AND " + Im.CUSTOM_PROTOCOL + "=?");
3073                    mSelectionArgs.add(customProtocol);
3074                }
3075                mSb.append(") OR (" + DataColumns.MIMETYPE_ID + "=?))");
3076                mSelectionArgs.add(mimeTypeIdEmail);
3077            } else {
3078                mSb.append(DataColumns.MIMETYPE_ID + "=?" +
3079                        " AND " + Im.PROTOCOL + "=?" +
3080                        " AND " + Im.DATA + "=?");
3081                mSelectionArgs.add(mimeTypeIdIm);
3082                mSelectionArgs.add(String.valueOf(protocol));
3083                mSelectionArgs.add(handle);
3084                if (customProtocol != null) {
3085                    mSb.append(" AND " + Im.CUSTOM_PROTOCOL + "=?");
3086                    mSelectionArgs.add(customProtocol);
3087                }
3088            }
3089
3090            if (values.containsKey(StatusUpdates.DATA_ID)) {
3091                mSb.append(" AND " + DataColumns.CONCRETE_ID + "=?");
3092                mSelectionArgs.add(values.getAsString(StatusUpdates.DATA_ID));
3093            }
3094        }
3095        mSb.append(" AND ").append(getContactsRestrictions());
3096
3097        Cursor cursor = null;
3098        try {
3099            cursor = mDb.query(DataContactsQuery.TABLE, DataContactsQuery.PROJECTION,
3100                    mSb.toString(), mSelectionArgs.toArray(EMPTY_STRING_ARRAY), null, null,
3101                    Contacts.IN_VISIBLE_GROUP + " DESC, " + Data.RAW_CONTACT_ID);
3102            if (cursor.moveToFirst()) {
3103                dataId = cursor.getLong(DataContactsQuery.DATA_ID);
3104                rawContactId = cursor.getLong(DataContactsQuery.RAW_CONTACT_ID);
3105                contactId = cursor.getLong(DataContactsQuery.CONTACT_ID);
3106            } else {
3107                // No contact found, return a null URI
3108                return -1;
3109            }
3110        } finally {
3111            if (cursor != null) {
3112                cursor.close();
3113            }
3114        }
3115
3116        if (values.containsKey(StatusUpdates.PRESENCE)) {
3117            if (customProtocol == null) {
3118                // We cannot allow a null in the custom protocol field, because SQLite3 does not
3119                // properly enforce uniqueness of null values
3120                customProtocol = "";
3121            }
3122
3123            mValues.clear();
3124            mValues.put(StatusUpdates.DATA_ID, dataId);
3125            mValues.put(PresenceColumns.RAW_CONTACT_ID, rawContactId);
3126            mValues.put(PresenceColumns.CONTACT_ID, contactId);
3127            mValues.put(StatusUpdates.PROTOCOL, protocol);
3128            mValues.put(StatusUpdates.CUSTOM_PROTOCOL, customProtocol);
3129            mValues.put(StatusUpdates.IM_HANDLE, handle);
3130            if (values.containsKey(StatusUpdates.IM_ACCOUNT)) {
3131                mValues.put(StatusUpdates.IM_ACCOUNT, values.getAsString(StatusUpdates.IM_ACCOUNT));
3132            }
3133            mValues.put(StatusUpdates.PRESENCE,
3134                    values.getAsString(StatusUpdates.PRESENCE));
3135
3136            // Insert the presence update
3137            mDb.replace(Tables.PRESENCE, null, mValues);
3138        }
3139
3140
3141        if (values.containsKey(StatusUpdates.STATUS)) {
3142            String status = values.getAsString(StatusUpdates.STATUS);
3143            String resPackage = values.getAsString(StatusUpdates.STATUS_RES_PACKAGE);
3144            Integer labelResource = values.getAsInteger(StatusUpdates.STATUS_LABEL);
3145
3146            if (TextUtils.isEmpty(resPackage)
3147                    && (labelResource == null || labelResource == 0)
3148                    && protocol != null) {
3149                labelResource = Im.getProtocolLabelResource(protocol);
3150            }
3151
3152            Long iconResource = values.getAsLong(StatusUpdates.STATUS_ICON);
3153            // TODO compute the default icon based on the protocol
3154
3155            if (TextUtils.isEmpty(status)) {
3156                mStatusUpdateDelete.bindLong(1, dataId);
3157                mStatusUpdateDelete.execute();
3158            } else if (values.containsKey(StatusUpdates.STATUS_TIMESTAMP)) {
3159                long timestamp = values.getAsLong(StatusUpdates.STATUS_TIMESTAMP);
3160                mStatusUpdateReplace.bindLong(1, dataId);
3161                mStatusUpdateReplace.bindLong(2, timestamp);
3162                bindString(mStatusUpdateReplace, 3, status);
3163                bindString(mStatusUpdateReplace, 4, resPackage);
3164                bindLong(mStatusUpdateReplace, 5, iconResource);
3165                bindLong(mStatusUpdateReplace, 6, labelResource);
3166                mStatusUpdateReplace.execute();
3167            } else {
3168
3169                try {
3170                    mStatusUpdateInsert.bindLong(1, dataId);
3171                    bindString(mStatusUpdateInsert, 2, status);
3172                    bindString(mStatusUpdateInsert, 3, resPackage);
3173                    bindLong(mStatusUpdateInsert, 4, iconResource);
3174                    bindLong(mStatusUpdateInsert, 5, labelResource);
3175                    mStatusUpdateInsert.executeInsert();
3176                } catch (SQLiteConstraintException e) {
3177                    // The row already exists - update it
3178                    long timestamp = System.currentTimeMillis();
3179                    mStatusUpdateAutoTimestamp.bindLong(1, timestamp);
3180                    bindString(mStatusUpdateAutoTimestamp, 2, status);
3181                    mStatusUpdateAutoTimestamp.bindLong(3, dataId);
3182                    bindString(mStatusUpdateAutoTimestamp, 4, status);
3183                    mStatusUpdateAutoTimestamp.execute();
3184
3185                    bindString(mStatusAttributionUpdate, 1, resPackage);
3186                    bindLong(mStatusAttributionUpdate, 2, iconResource);
3187                    bindLong(mStatusAttributionUpdate, 3, labelResource);
3188                    mStatusAttributionUpdate.bindLong(4, dataId);
3189                    mStatusAttributionUpdate.execute();
3190                }
3191            }
3192        }
3193
3194        if (contactId != -1) {
3195            mLastStatusUpdate.bindLong(1, contactId);
3196            mLastStatusUpdate.bindLong(2, contactId);
3197            mLastStatusUpdate.execute();
3198        }
3199
3200        return dataId;
3201    }
3202
3203    @Override
3204    protected int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) {
3205        if (VERBOSE_LOGGING) {
3206            Log.v(TAG, "deleteInTransaction: " + uri);
3207        }
3208        flushTransactionalChanges();
3209        final boolean callerIsSyncAdapter =
3210                readBooleanQueryParameter(uri, ContactsContract.CALLER_IS_SYNCADAPTER, false);
3211        final int match = sUriMatcher.match(uri);
3212        switch (match) {
3213            case SYNCSTATE:
3214                return mDbHelper.getSyncState().delete(mDb, selection, selectionArgs);
3215
3216            case SYNCSTATE_ID:
3217                String selectionWithId =
3218                        (SyncStateContract.Columns._ID + "=" + ContentUris.parseId(uri) + " ")
3219                        + (selection == null ? "" : " AND (" + selection + ")");
3220                return mDbHelper.getSyncState().delete(mDb, selectionWithId, selectionArgs);
3221
3222            case CONTACTS: {
3223                // TODO
3224                return 0;
3225            }
3226
3227            case CONTACTS_ID: {
3228                long contactId = ContentUris.parseId(uri);
3229                return deleteContact(contactId);
3230            }
3231
3232            case CONTACTS_LOOKUP: {
3233                final List<String> pathSegments = uri.getPathSegments();
3234                final int segmentCount = pathSegments.size();
3235                if (segmentCount < 3) {
3236                    throw new IllegalArgumentException(mDbHelper.exceptionMessage(
3237                            "Missing a lookup key", uri));
3238                }
3239                final String lookupKey = pathSegments.get(2);
3240                final long contactId = lookupContactIdByLookupKey(mDb, lookupKey);
3241                return deleteContact(contactId);
3242            }
3243
3244            case CONTACTS_LOOKUP_ID: {
3245                // lookup contact by id and lookup key to see if they still match the actual record
3246                long contactId = ContentUris.parseId(uri);
3247                final List<String> pathSegments = uri.getPathSegments();
3248                final String lookupKey = pathSegments.get(2);
3249                SQLiteQueryBuilder lookupQb = new SQLiteQueryBuilder();
3250                setTablesAndProjectionMapForContacts(lookupQb, uri, null);
3251                String[] args;
3252                if (selectionArgs == null) {
3253                    args = new String[2];
3254                } else {
3255                    args = new String[selectionArgs.length + 2];
3256                    System.arraycopy(selectionArgs, 0, args, 2, selectionArgs.length);
3257                }
3258                args[0] = String.valueOf(contactId);
3259                args[1] = Uri.encode(lookupKey);
3260                lookupQb.appendWhere(Contacts._ID + "=? AND " + Contacts.LOOKUP_KEY + "=?");
3261                final SQLiteDatabase db = mDbHelper.getReadableDatabase();
3262                Cursor c = query(db, lookupQb, null, selection, args, null, null, null);
3263                try {
3264                    if (c.getCount() == 1) {
3265                        // contact was unmodified so go ahead and delete it
3266                        return deleteContact(contactId);
3267                    } else {
3268                        // row was changed (e.g. the merging might have changed), we got multiple
3269                        // rows or the supplied selection filtered the record out
3270                        return 0;
3271                    }
3272                } finally {
3273                    c.close();
3274                }
3275            }
3276
3277            case RAW_CONTACTS: {
3278                int numDeletes = 0;
3279                Cursor c = mDb.query(Tables.RAW_CONTACTS,
3280                        new String[]{RawContacts._ID, RawContacts.CONTACT_ID},
3281                        appendAccountToSelection(uri, selection), selectionArgs, null, null, null);
3282                try {
3283                    while (c.moveToNext()) {
3284                        final long rawContactId = c.getLong(0);
3285                        long contactId = c.getLong(1);
3286                        numDeletes += deleteRawContact(rawContactId, contactId,
3287                                callerIsSyncAdapter);
3288                    }
3289                } finally {
3290                    c.close();
3291                }
3292                return numDeletes;
3293            }
3294
3295            case RAW_CONTACTS_ID: {
3296                final long rawContactId = ContentUris.parseId(uri);
3297                return deleteRawContact(rawContactId, mDbHelper.getContactId(rawContactId),
3298                        callerIsSyncAdapter);
3299            }
3300
3301            case DATA: {
3302                mSyncToNetwork |= !callerIsSyncAdapter;
3303                return deleteData(appendAccountToSelection(uri, selection), selectionArgs,
3304                        callerIsSyncAdapter);
3305            }
3306
3307            case DATA_ID:
3308            case PHONES_ID:
3309            case EMAILS_ID:
3310            case POSTALS_ID: {
3311                long dataId = ContentUris.parseId(uri);
3312                mSyncToNetwork |= !callerIsSyncAdapter;
3313                mSelectionArgs1[0] = String.valueOf(dataId);
3314                return deleteData(Data._ID + "=?", mSelectionArgs1, callerIsSyncAdapter);
3315            }
3316
3317            case GROUPS_ID: {
3318                mSyncToNetwork |= !callerIsSyncAdapter;
3319                return deleteGroup(uri, ContentUris.parseId(uri), callerIsSyncAdapter);
3320            }
3321
3322            case GROUPS: {
3323                int numDeletes = 0;
3324                Cursor c = mDb.query(Tables.GROUPS, new String[]{Groups._ID},
3325                        appendAccountToSelection(uri, selection), selectionArgs, null, null, null);
3326                try {
3327                    while (c.moveToNext()) {
3328                        numDeletes += deleteGroup(uri, c.getLong(0), callerIsSyncAdapter);
3329                    }
3330                } finally {
3331                    c.close();
3332                }
3333                if (numDeletes > 0) {
3334                    mSyncToNetwork |= !callerIsSyncAdapter;
3335                }
3336                return numDeletes;
3337            }
3338
3339            case SETTINGS: {
3340                mSyncToNetwork |= !callerIsSyncAdapter;
3341                return deleteSettings(uri, appendAccountToSelection(uri, selection), selectionArgs);
3342            }
3343
3344            case STATUS_UPDATES: {
3345                return deleteStatusUpdates(selection, selectionArgs);
3346            }
3347
3348            default: {
3349                mSyncToNetwork = true;
3350                return mLegacyApiSupport.delete(uri, selection, selectionArgs);
3351            }
3352        }
3353    }
3354
3355    public int deleteGroup(Uri uri, long groupId, boolean callerIsSyncAdapter) {
3356        mGroupIdCache.clear();
3357        final long groupMembershipMimetypeId = mDbHelper
3358                .getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE);
3359        mDb.delete(Tables.DATA, DataColumns.MIMETYPE_ID + "="
3360                + groupMembershipMimetypeId + " AND " + GroupMembership.GROUP_ROW_ID + "="
3361                + groupId, null);
3362
3363        try {
3364            if (callerIsSyncAdapter) {
3365                return mDb.delete(Tables.GROUPS, Groups._ID + "=" + groupId, null);
3366            } else {
3367                mValues.clear();
3368                mValues.put(Groups.DELETED, 1);
3369                mValues.put(Groups.DIRTY, 1);
3370                return mDb.update(Tables.GROUPS, mValues, Groups._ID + "=" + groupId, null);
3371            }
3372        } finally {
3373            mVisibleTouched = true;
3374        }
3375    }
3376
3377    private int deleteSettings(Uri uri, String selection, String[] selectionArgs) {
3378        final int count = mDb.delete(Tables.SETTINGS, selection, selectionArgs);
3379        mVisibleTouched = true;
3380        return count;
3381    }
3382
3383    private int deleteContact(long contactId) {
3384        mSelectionArgs1[0] = Long.toString(contactId);
3385        Cursor c = mDb.query(Tables.RAW_CONTACTS, new String[]{RawContacts._ID},
3386                RawContacts.CONTACT_ID + "=?", mSelectionArgs1,
3387                null, null, null);
3388        try {
3389            while (c.moveToNext()) {
3390                long rawContactId = c.getLong(0);
3391                markRawContactAsDeleted(rawContactId);
3392            }
3393        } finally {
3394            c.close();
3395        }
3396
3397        return mDb.delete(Tables.CONTACTS, Contacts._ID + "=" + contactId, null);
3398    }
3399
3400    public int deleteRawContact(long rawContactId, long contactId, boolean callerIsSyncAdapter) {
3401        mContactAggregator.invalidateAggregationExceptionCache();
3402        if (callerIsSyncAdapter) {
3403            mDb.delete(Tables.PRESENCE, PresenceColumns.RAW_CONTACT_ID + "=" + rawContactId, null);
3404            int count = mDb.delete(Tables.RAW_CONTACTS, RawContacts._ID + "=" + rawContactId, null);
3405            mContactAggregator.updateDisplayNameForContact(mDb, contactId);
3406            return count;
3407        } else {
3408            mDbHelper.removeContactIfSingleton(rawContactId);
3409            return markRawContactAsDeleted(rawContactId);
3410        }
3411    }
3412
3413    private int deleteStatusUpdates(String selection, String[] selectionArgs) {
3414      // delete from both tables: presence and status_updates
3415      // TODO should account type/name be appended to the where clause?
3416      if (VERBOSE_LOGGING) {
3417          Log.v(TAG, "deleting data from status_updates for " + selection);
3418      }
3419      mDb.delete(Tables.STATUS_UPDATES, getWhereClauseForStatusUpdatesTable(selection),
3420          selectionArgs);
3421      return mDb.delete(Tables.PRESENCE, selection, selectionArgs);
3422    }
3423
3424    private int markRawContactAsDeleted(long rawContactId) {
3425        mSyncToNetwork = true;
3426
3427        mValues.clear();
3428        mValues.put(RawContacts.DELETED, 1);
3429        mValues.put(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DISABLED);
3430        mValues.put(RawContactsColumns.AGGREGATION_NEEDED, 1);
3431        mValues.putNull(RawContacts.CONTACT_ID);
3432        mValues.put(RawContacts.DIRTY, 1);
3433        return updateRawContact(rawContactId, mValues);
3434    }
3435
3436    @Override
3437    protected int updateInTransaction(Uri uri, ContentValues values, String selection,
3438            String[] selectionArgs) {
3439        if (VERBOSE_LOGGING) {
3440            Log.v(TAG, "updateInTransaction: " + uri);
3441        }
3442
3443        int count = 0;
3444
3445        final int match = sUriMatcher.match(uri);
3446        if (match == SYNCSTATE_ID && selection == null) {
3447            long rowId = ContentUris.parseId(uri);
3448            Object data = values.get(ContactsContract.SyncState.DATA);
3449            mUpdatedSyncStates.put(rowId, data);
3450            return 1;
3451        }
3452        flushTransactionalChanges();
3453        final boolean callerIsSyncAdapter =
3454                readBooleanQueryParameter(uri, ContactsContract.CALLER_IS_SYNCADAPTER, false);
3455        switch(match) {
3456            case SYNCSTATE:
3457                return mDbHelper.getSyncState().update(mDb, values,
3458                        appendAccountToSelection(uri, selection), selectionArgs);
3459
3460            case SYNCSTATE_ID: {
3461                selection = appendAccountToSelection(uri, selection);
3462                String selectionWithId =
3463                        (SyncStateContract.Columns._ID + "=" + ContentUris.parseId(uri) + " ")
3464                        + (selection == null ? "" : " AND (" + selection + ")");
3465                return mDbHelper.getSyncState().update(mDb, values,
3466                        selectionWithId, selectionArgs);
3467            }
3468
3469            case CONTACTS: {
3470                count = updateContactOptions(values, selection, selectionArgs);
3471                break;
3472            }
3473
3474            case CONTACTS_ID: {
3475                count = updateContactOptions(ContentUris.parseId(uri), values);
3476                break;
3477            }
3478
3479            case CONTACTS_LOOKUP:
3480            case CONTACTS_LOOKUP_ID: {
3481                final List<String> pathSegments = uri.getPathSegments();
3482                final int segmentCount = pathSegments.size();
3483                if (segmentCount < 3) {
3484                    throw new IllegalArgumentException(mDbHelper.exceptionMessage(
3485                            "Missing a lookup key", uri));
3486                }
3487                final String lookupKey = pathSegments.get(2);
3488                final long contactId = lookupContactIdByLookupKey(mDb, lookupKey);
3489                count = updateContactOptions(contactId, values);
3490                break;
3491            }
3492
3493            case RAW_CONTACTS_DATA: {
3494                final String rawContactId = uri.getPathSegments().get(1);
3495                String selectionWithId = (Data.RAW_CONTACT_ID + "=" + rawContactId + " ")
3496                    + (selection == null ? "" : " AND " + selection);
3497
3498                count = updateData(uri, values, selectionWithId, selectionArgs, callerIsSyncAdapter);
3499
3500                break;
3501            }
3502
3503            case DATA: {
3504                count = updateData(uri, values, appendAccountToSelection(uri, selection),
3505                        selectionArgs, callerIsSyncAdapter);
3506                if (count > 0) {
3507                    mSyncToNetwork |= !callerIsSyncAdapter;
3508                }
3509                break;
3510            }
3511
3512            case DATA_ID:
3513            case PHONES_ID:
3514            case EMAILS_ID:
3515            case POSTALS_ID: {
3516                count = updateData(uri, values, selection, selectionArgs, callerIsSyncAdapter);
3517                if (count > 0) {
3518                    mSyncToNetwork |= !callerIsSyncAdapter;
3519                }
3520                break;
3521            }
3522
3523            case RAW_CONTACTS: {
3524                selection = appendAccountToSelection(uri, selection);
3525                count = updateRawContacts(values, selection, selectionArgs);
3526                break;
3527            }
3528
3529            case RAW_CONTACTS_ID: {
3530                long rawContactId = ContentUris.parseId(uri);
3531                if (selection != null) {
3532                    selectionArgs = insertSelectionArg(selectionArgs, String.valueOf(rawContactId));
3533                    count = updateRawContacts(values, RawContacts._ID + "=?"
3534                                    + " AND(" + selection + ")", selectionArgs);
3535                } else {
3536                    mSelectionArgs1[0] = String.valueOf(rawContactId);
3537                    count = updateRawContacts(values, RawContacts._ID + "=?", mSelectionArgs1);
3538                }
3539                break;
3540            }
3541
3542            case GROUPS: {
3543                count = updateGroups(uri, values, appendAccountToSelection(uri, selection),
3544                        selectionArgs, callerIsSyncAdapter);
3545                if (count > 0) {
3546                    mSyncToNetwork |= !callerIsSyncAdapter;
3547                }
3548                break;
3549            }
3550
3551            case GROUPS_ID: {
3552                long groupId = ContentUris.parseId(uri);
3553                selectionArgs = insertSelectionArg(selectionArgs, String.valueOf(groupId));
3554                String selectionWithId = Groups._ID + "=? "
3555                        + (selection == null ? "" : " AND " + selection);
3556                count = updateGroups(uri, values, selectionWithId, selectionArgs,
3557                        callerIsSyncAdapter);
3558                if (count > 0) {
3559                    mSyncToNetwork |= !callerIsSyncAdapter;
3560                }
3561                break;
3562            }
3563
3564            case AGGREGATION_EXCEPTIONS: {
3565                count = updateAggregationException(mDb, values);
3566                break;
3567            }
3568
3569            case SETTINGS: {
3570                count = updateSettings(uri, values, appendAccountToSelection(uri, selection),
3571                        selectionArgs);
3572                mSyncToNetwork |= !callerIsSyncAdapter;
3573                break;
3574            }
3575
3576            case STATUS_UPDATES: {
3577                count = updateStatusUpdate(uri, values, selection, selectionArgs);
3578                break;
3579            }
3580
3581            default: {
3582                mSyncToNetwork = true;
3583                return mLegacyApiSupport.update(uri, values, selection, selectionArgs);
3584            }
3585        }
3586
3587        return count;
3588    }
3589
3590    private int updateStatusUpdate(Uri uri, ContentValues values, String selection,
3591        String[] selectionArgs) {
3592        // update status_updates table, if status is provided
3593        // TODO should account type/name be appended to the where clause?
3594        int updateCount = 0;
3595        ContentValues settableValues = getSettableColumnsForStatusUpdatesTable(values);
3596        if (settableValues.size() > 0) {
3597          updateCount = mDb.update(Tables.STATUS_UPDATES,
3598                    settableValues,
3599                    getWhereClauseForStatusUpdatesTable(selection),
3600                    selectionArgs);
3601        }
3602
3603        // now update the Presence table
3604        settableValues = getSettableColumnsForPresenceTable(values);
3605        if (settableValues.size() > 0) {
3606          updateCount = mDb.update(Tables.PRESENCE, settableValues,
3607                    selection, selectionArgs);
3608        }
3609        // TODO updateCount is not entirely a valid count of updated rows because 2 tables could
3610        // potentially get updated in this method.
3611        return updateCount;
3612    }
3613
3614    /**
3615     * Build a where clause to select the rows to be updated in status_updates table.
3616     */
3617    private String getWhereClauseForStatusUpdatesTable(String selection) {
3618        mSb.setLength(0);
3619        mSb.append(WHERE_CLAUSE_FOR_STATUS_UPDATES_TABLE);
3620        mSb.append(selection);
3621        mSb.append(")");
3622        return mSb.toString();
3623    }
3624
3625    private ContentValues getSettableColumnsForStatusUpdatesTable(ContentValues values) {
3626        mValues.clear();
3627        ContactsDatabaseHelper.copyStringValue(mValues, StatusUpdates.STATUS, values,
3628            StatusUpdates.STATUS);
3629        ContactsDatabaseHelper.copyStringValue(mValues, StatusUpdates.STATUS_TIMESTAMP, values,
3630            StatusUpdates.STATUS_TIMESTAMP);
3631        ContactsDatabaseHelper.copyStringValue(mValues, StatusUpdates.STATUS_RES_PACKAGE, values,
3632            StatusUpdates.STATUS_RES_PACKAGE);
3633        ContactsDatabaseHelper.copyStringValue(mValues, StatusUpdates.STATUS_LABEL, values,
3634            StatusUpdates.STATUS_LABEL);
3635        ContactsDatabaseHelper.copyStringValue(mValues, StatusUpdates.STATUS_ICON, values,
3636            StatusUpdates.STATUS_ICON);
3637        return mValues;
3638    }
3639
3640    private ContentValues getSettableColumnsForPresenceTable(ContentValues values) {
3641        mValues.clear();
3642        ContactsDatabaseHelper.copyStringValue(mValues, StatusUpdates.PRESENCE, values,
3643            StatusUpdates.PRESENCE);
3644        return mValues;
3645    }
3646
3647    private int updateGroups(Uri uri, ContentValues values, String selectionWithId,
3648            String[] selectionArgs, boolean callerIsSyncAdapter) {
3649
3650        mGroupIdCache.clear();
3651
3652        ContentValues updatedValues;
3653        if (!callerIsSyncAdapter && !values.containsKey(Groups.DIRTY)) {
3654            updatedValues = mValues;
3655            updatedValues.clear();
3656            updatedValues.putAll(values);
3657            updatedValues.put(Groups.DIRTY, 1);
3658        } else {
3659            updatedValues = values;
3660        }
3661
3662        int count = mDb.update(Tables.GROUPS, updatedValues, selectionWithId, selectionArgs);
3663        if (updatedValues.containsKey(Groups.GROUP_VISIBLE)) {
3664            mVisibleTouched = true;
3665        }
3666        if (updatedValues.containsKey(Groups.SHOULD_SYNC)
3667                && updatedValues.getAsInteger(Groups.SHOULD_SYNC) != 0) {
3668            Cursor c = mDb.query(Tables.GROUPS, new String[]{Groups.ACCOUNT_NAME,
3669                    Groups.ACCOUNT_TYPE}, selectionWithId, selectionArgs, null,
3670                    null, null);
3671            String accountName;
3672            String accountType;
3673            try {
3674                while (c.moveToNext()) {
3675                    accountName = c.getString(0);
3676                    accountType = c.getString(1);
3677                    if(!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) {
3678                        Account account = new Account(accountName, accountType);
3679                        ContentResolver.requestSync(account, ContactsContract.AUTHORITY,
3680                                new Bundle());
3681                        break;
3682                    }
3683                }
3684            } finally {
3685                c.close();
3686            }
3687        }
3688        return count;
3689    }
3690
3691    private int updateSettings(Uri uri, ContentValues values, String selection,
3692            String[] selectionArgs) {
3693        final int count = mDb.update(Tables.SETTINGS, values, selection, selectionArgs);
3694        if (values.containsKey(Settings.UNGROUPED_VISIBLE)) {
3695            mVisibleTouched = true;
3696        }
3697        return count;
3698    }
3699
3700    private int updateRawContacts(ContentValues values, String selection, String[] selectionArgs) {
3701        if (values.containsKey(RawContacts.CONTACT_ID)) {
3702            throw new IllegalArgumentException(RawContacts.CONTACT_ID + " should not be included " +
3703                    "in content values. Contact IDs are assigned automatically");
3704        }
3705
3706        int count = 0;
3707        Cursor cursor = mDb.query(mDbHelper.getRawContactView(),
3708                new String[] { RawContacts._ID }, selection,
3709                selectionArgs, null, null, null);
3710        try {
3711            while (cursor.moveToNext()) {
3712                long rawContactId = cursor.getLong(0);
3713                updateRawContact(rawContactId, values);
3714                count++;
3715            }
3716        } finally {
3717            cursor.close();
3718        }
3719
3720        return count;
3721    }
3722
3723    private int updateRawContact(long rawContactId, ContentValues values) {
3724        final String selection = RawContacts._ID + " = ?";
3725        mSelectionArgs1[0] = Long.toString(rawContactId);
3726        final boolean requestUndoDelete = (values.containsKey(RawContacts.DELETED)
3727                && values.getAsInteger(RawContacts.DELETED) == 0);
3728        int previousDeleted = 0;
3729        String accountType = null;
3730        String accountName = null;
3731        if (requestUndoDelete) {
3732            Cursor cursor = mDb.query(RawContactsQuery.TABLE, RawContactsQuery.COLUMNS, selection,
3733                    mSelectionArgs1, null, null, null);
3734            try {
3735                if (cursor.moveToFirst()) {
3736                    previousDeleted = cursor.getInt(RawContactsQuery.DELETED);
3737                    accountType = cursor.getString(RawContactsQuery.ACCOUNT_TYPE);
3738                    accountName = cursor.getString(RawContactsQuery.ACCOUNT_NAME);
3739                }
3740            } finally {
3741                cursor.close();
3742            }
3743            values.put(ContactsContract.RawContacts.AGGREGATION_MODE,
3744                    ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT);
3745        }
3746
3747        int count = mDb.update(Tables.RAW_CONTACTS, values, selection, mSelectionArgs1);
3748        if (count != 0) {
3749            if (values.containsKey(RawContacts.AGGREGATION_MODE)) {
3750                int aggregationMode = values.getAsInteger(RawContacts.AGGREGATION_MODE);
3751
3752                // As per ContactsContract documentation, changing aggregation mode
3753                // to DEFAULT should not trigger aggregation
3754                if (aggregationMode != RawContacts.AGGREGATION_MODE_DEFAULT) {
3755                    mContactAggregator.markForAggregation(rawContactId, aggregationMode);
3756                }
3757            }
3758            if (values.containsKey(RawContacts.STARRED)) {
3759                mContactAggregator.updateStarred(rawContactId);
3760            }
3761            if (values.containsKey(RawContacts.SOURCE_ID)) {
3762                mContactAggregator.updateLookupKeyForRawContact(mDb, rawContactId);
3763            }
3764            if (values.containsKey(RawContacts.NAME_VERIFIED)) {
3765
3766                // If setting NAME_VERIFIED for this raw contact, reset it for all
3767                // other raw contacts in the same aggregate
3768                if (values.getAsInteger(RawContacts.NAME_VERIFIED) != 0) {
3769                    mResetNameVerifiedForOtherRawContacts.bindLong(1, rawContactId);
3770                    mResetNameVerifiedForOtherRawContacts.bindLong(2, rawContactId);
3771                    mResetNameVerifiedForOtherRawContacts.execute();
3772                }
3773                mContactAggregator.updateDisplayNameForRawContact(mDb, rawContactId);
3774            }
3775            if (requestUndoDelete && previousDeleted == 1) {
3776                // undo delete, needs aggregation again.
3777                mInsertedRawContacts.put(rawContactId, new Account(accountName, accountType));
3778            }
3779        }
3780        return count;
3781    }
3782
3783    private int updateData(Uri uri, ContentValues values, String selection,
3784            String[] selectionArgs, boolean callerIsSyncAdapter) {
3785        mValues.clear();
3786        mValues.putAll(values);
3787        mValues.remove(Data._ID);
3788        mValues.remove(Data.RAW_CONTACT_ID);
3789        mValues.remove(Data.MIMETYPE);
3790
3791        String packageName = values.getAsString(Data.RES_PACKAGE);
3792        if (packageName != null) {
3793            mValues.remove(Data.RES_PACKAGE);
3794            mValues.put(DataColumns.PACKAGE_ID, mDbHelper.getPackageId(packageName));
3795        }
3796
3797        boolean containsIsSuperPrimary = mValues.containsKey(Data.IS_SUPER_PRIMARY);
3798        boolean containsIsPrimary = mValues.containsKey(Data.IS_PRIMARY);
3799
3800        // Remove primary or super primary values being set to 0. This is disallowed by the
3801        // content provider.
3802        if (containsIsSuperPrimary && mValues.getAsInteger(Data.IS_SUPER_PRIMARY) == 0) {
3803            containsIsSuperPrimary = false;
3804            mValues.remove(Data.IS_SUPER_PRIMARY);
3805        }
3806        if (containsIsPrimary && mValues.getAsInteger(Data.IS_PRIMARY) == 0) {
3807            containsIsPrimary = false;
3808            mValues.remove(Data.IS_PRIMARY);
3809        }
3810
3811        int count = 0;
3812
3813        // Note that the query will return data according to the access restrictions,
3814        // so we don't need to worry about updating data we don't have permission to read.
3815        Cursor c = query(uri, DataUpdateQuery.COLUMNS, selection, selectionArgs, null);
3816        try {
3817            while(c.moveToNext()) {
3818                count += updateData(mValues, c, callerIsSyncAdapter);
3819            }
3820        } finally {
3821            c.close();
3822        }
3823
3824        return count;
3825    }
3826
3827    private int updateData(ContentValues values, Cursor c, boolean callerIsSyncAdapter) {
3828        if (values.size() == 0) {
3829            return 0;
3830        }
3831
3832        final String mimeType = c.getString(DataUpdateQuery.MIMETYPE);
3833        DataRowHandler rowHandler = getDataRowHandler(mimeType);
3834        if (rowHandler.update(mDb, values, c, callerIsSyncAdapter)) {
3835            return 1;
3836        } else {
3837            return 0;
3838        }
3839    }
3840
3841    private int updateContactOptions(ContentValues values, String selection,
3842            String[] selectionArgs) {
3843        int count = 0;
3844        Cursor cursor = mDb.query(mDbHelper.getContactView(),
3845                new String[] { Contacts._ID }, selection,
3846                selectionArgs, null, null, null);
3847        try {
3848            while (cursor.moveToNext()) {
3849                long contactId = cursor.getLong(0);
3850                updateContactOptions(contactId, values);
3851                count++;
3852            }
3853        } finally {
3854            cursor.close();
3855        }
3856
3857        return count;
3858    }
3859
3860    private int updateContactOptions(long contactId, ContentValues values) {
3861
3862        mValues.clear();
3863        ContactsDatabaseHelper.copyStringValue(mValues, RawContacts.CUSTOM_RINGTONE,
3864                values, Contacts.CUSTOM_RINGTONE);
3865        ContactsDatabaseHelper.copyLongValue(mValues, RawContacts.SEND_TO_VOICEMAIL,
3866                values, Contacts.SEND_TO_VOICEMAIL);
3867        ContactsDatabaseHelper.copyLongValue(mValues, RawContacts.LAST_TIME_CONTACTED,
3868                values, Contacts.LAST_TIME_CONTACTED);
3869        ContactsDatabaseHelper.copyLongValue(mValues, RawContacts.TIMES_CONTACTED,
3870                values, Contacts.TIMES_CONTACTED);
3871        ContactsDatabaseHelper.copyLongValue(mValues, RawContacts.STARRED,
3872                values, Contacts.STARRED);
3873
3874        // Nothing to update - just return
3875        if (mValues.size() == 0) {
3876            return 0;
3877        }
3878
3879        if (mValues.containsKey(RawContacts.STARRED)) {
3880            // Mark dirty when changing starred to trigger sync
3881            mValues.put(RawContacts.DIRTY, 1);
3882        }
3883
3884        mSelectionArgs1[0] = String.valueOf(contactId);
3885        mDb.update(Tables.RAW_CONTACTS, mValues, RawContacts.CONTACT_ID + "=?", mSelectionArgs1);
3886
3887        // Copy changeable values to prevent automatically managed fields from
3888        // being explicitly updated by clients.
3889        mValues.clear();
3890        ContactsDatabaseHelper.copyStringValue(mValues, RawContacts.CUSTOM_RINGTONE,
3891                values, Contacts.CUSTOM_RINGTONE);
3892        ContactsDatabaseHelper.copyLongValue(mValues, RawContacts.SEND_TO_VOICEMAIL,
3893                values, Contacts.SEND_TO_VOICEMAIL);
3894        ContactsDatabaseHelper.copyLongValue(mValues, RawContacts.LAST_TIME_CONTACTED,
3895                values, Contacts.LAST_TIME_CONTACTED);
3896        ContactsDatabaseHelper.copyLongValue(mValues, RawContacts.TIMES_CONTACTED,
3897                values, Contacts.TIMES_CONTACTED);
3898        ContactsDatabaseHelper.copyLongValue(mValues, RawContacts.STARRED,
3899                values, Contacts.STARRED);
3900
3901        int rslt = mDb.update(Tables.CONTACTS, mValues, Contacts._ID + "=?", mSelectionArgs1);
3902
3903        if (values.containsKey(Contacts.LAST_TIME_CONTACTED) &&
3904                !values.containsKey(Contacts.TIMES_CONTACTED)) {
3905            mDb.execSQL(UPDATE_TIMES_CONTACTED_CONTACTS_TABLE, mSelectionArgs1);
3906            mDb.execSQL(UPDATE_TIMES_CONTACTED_RAWCONTACTS_TABLE, mSelectionArgs1);
3907        }
3908        return rslt;
3909    }
3910
3911    private int updateAggregationException(SQLiteDatabase db, ContentValues values) {
3912        int exceptionType = values.getAsInteger(AggregationExceptions.TYPE);
3913        long rcId1 = values.getAsInteger(AggregationExceptions.RAW_CONTACT_ID1);
3914        long rcId2 = values.getAsInteger(AggregationExceptions.RAW_CONTACT_ID2);
3915
3916        long rawContactId1, rawContactId2;
3917        if (rcId1 < rcId2) {
3918            rawContactId1 = rcId1;
3919            rawContactId2 = rcId2;
3920        } else {
3921            rawContactId2 = rcId1;
3922            rawContactId1 = rcId2;
3923        }
3924
3925        if (exceptionType == AggregationExceptions.TYPE_AUTOMATIC) {
3926            mSelectionArgs2[0] = String.valueOf(rawContactId1);
3927            mSelectionArgs2[1] = String.valueOf(rawContactId2);
3928            db.delete(Tables.AGGREGATION_EXCEPTIONS,
3929                    AggregationExceptions.RAW_CONTACT_ID1 + "=? AND "
3930                    + AggregationExceptions.RAW_CONTACT_ID2 + "=?", mSelectionArgs2);
3931        } else {
3932            ContentValues exceptionValues = new ContentValues(3);
3933            exceptionValues.put(AggregationExceptions.TYPE, exceptionType);
3934            exceptionValues.put(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1);
3935            exceptionValues.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2);
3936            db.replace(Tables.AGGREGATION_EXCEPTIONS, AggregationExceptions._ID,
3937                    exceptionValues);
3938        }
3939
3940        mContactAggregator.invalidateAggregationExceptionCache();
3941        mContactAggregator.markForAggregation(rawContactId1, RawContacts.AGGREGATION_MODE_DEFAULT);
3942        mContactAggregator.markForAggregation(rawContactId2, RawContacts.AGGREGATION_MODE_DEFAULT);
3943
3944        long contactId1 = mDbHelper.getContactId(rawContactId1);
3945        mContactAggregator.aggregateContact(db, rawContactId1, contactId1);
3946
3947        long contactId2 = mDbHelper.getContactId(rawContactId2);
3948        mContactAggregator.aggregateContact(db, rawContactId2, contactId2);
3949
3950        // The return value is fake - we just confirm that we made a change, not count actual
3951        // rows changed.
3952        return 1;
3953    }
3954
3955    /**
3956     * Check whether GOOGLE_MY_CONTACTS_GROUP exists, otherwise create it.
3957     *
3958     * @return the group id
3959     */
3960    private long getOrCreateMyContactsGroupInTransaction(String accountName, String accountType) {
3961        Cursor cursor = mDb.query(Tables.GROUPS, new String[] {"_id"},
3962                Groups.ACCOUNT_NAME + " =? AND " + Groups.ACCOUNT_TYPE + " =? AND "
3963                    + Groups.TITLE + " =?",
3964                new String[] {accountName, accountType, GOOGLE_MY_CONTACTS_GROUP_TITLE},
3965                null, null, null);
3966        try {
3967            if(cursor.moveToNext()) {
3968                return cursor.getLong(0);
3969            }
3970        } finally {
3971            cursor.close();
3972        }
3973
3974        ContentValues values = new ContentValues();
3975        values.put(Groups.TITLE, GOOGLE_MY_CONTACTS_GROUP_TITLE);
3976        values.put(Groups.ACCOUNT_NAME, accountName);
3977        values.put(Groups.ACCOUNT_TYPE, accountType);
3978        values.put(Groups.GROUP_VISIBLE, "1");
3979        return mDb.insert(Tables.GROUPS, null, values);
3980    }
3981
3982    public void onAccountsUpdated(Account[] accounts) {
3983        // TODO : Check the unit test.
3984        HashSet<Account> existingAccounts = new HashSet<Account>();
3985        boolean hasUnassignedContacts[] = new boolean[]{false};
3986        mDb.beginTransaction();
3987        try {
3988            findValidAccounts(existingAccounts, hasUnassignedContacts);
3989
3990            // Add a row to the ACCOUNTS table for each new account
3991            for (Account account : accounts) {
3992                if (!existingAccounts.contains(account)) {
3993                    mDb.execSQL("INSERT INTO " + Tables.ACCOUNTS + " (" + RawContacts.ACCOUNT_NAME
3994                            + ", " + RawContacts.ACCOUNT_TYPE + ") VALUES (?, ?)",
3995                            new String[] {account.name, account.type});
3996                }
3997            }
3998
3999            // Remove all valid accounts from the existing account set. What is left
4000            // in the accountsToDelete set will be extra accounts whose data must be deleted.
4001            HashSet<Account> accountsToDelete = new HashSet<Account>(existingAccounts);
4002            for (Account account : accounts) {
4003                accountsToDelete.remove(account);
4004            }
4005
4006            for (Account account : accountsToDelete) {
4007                Log.d(TAG, "removing data for removed account " + account);
4008                String[] params = new String[] {account.name, account.type};
4009                mDb.execSQL(
4010                        "DELETE FROM " + Tables.GROUPS +
4011                        " WHERE " + Groups.ACCOUNT_NAME + " = ?" +
4012                                " AND " + Groups.ACCOUNT_TYPE + " = ?", params);
4013                mDb.execSQL(
4014                        "DELETE FROM " + Tables.PRESENCE +
4015                        " WHERE " + PresenceColumns.RAW_CONTACT_ID + " IN (" +
4016                                "SELECT " + RawContacts._ID +
4017                                " FROM " + Tables.RAW_CONTACTS +
4018                                " WHERE " + RawContacts.ACCOUNT_NAME + " = ?" +
4019                                " AND " + RawContacts.ACCOUNT_TYPE + " = ?)", params);
4020                mDb.execSQL(
4021                        "DELETE FROM " + Tables.RAW_CONTACTS +
4022                        " WHERE " + RawContacts.ACCOUNT_NAME + " = ?" +
4023                        " AND " + RawContacts.ACCOUNT_TYPE + " = ?", params);
4024                mDb.execSQL(
4025                        "DELETE FROM " + Tables.SETTINGS +
4026                        " WHERE " + Settings.ACCOUNT_NAME + " = ?" +
4027                        " AND " + Settings.ACCOUNT_TYPE + " = ?", params);
4028                mDb.execSQL(
4029                        "DELETE FROM " + Tables.ACCOUNTS +
4030                        " WHERE " + RawContacts.ACCOUNT_NAME + "=?" +
4031                        " AND " + RawContacts.ACCOUNT_TYPE + "=?", params);
4032            }
4033
4034            if (!accountsToDelete.isEmpty()) {
4035                // Find all aggregated contacts that used to contain the raw contacts
4036                // we have just deleted and see if they are still referencing the deleted
4037                // names of photos.  If so, fix up those contacts.
4038                HashSet<Long> orphanContactIds = Sets.newHashSet();
4039                Cursor cursor = mDb.rawQuery("SELECT " + Contacts._ID +
4040                        " FROM " + Tables.CONTACTS +
4041                        " WHERE (" + Contacts.NAME_RAW_CONTACT_ID + " NOT NULL AND " +
4042                                Contacts.NAME_RAW_CONTACT_ID + " NOT IN" +
4043                                        " (SELECT " + RawContacts._ID +
4044                                		" FROM " + Tables.RAW_CONTACTS + "))" +
4045                        " OR (" + Contacts.PHOTO_ID + " NOT NULL AND " +
4046                                Contacts.PHOTO_ID + " NOT IN " +
4047                                		"(SELECT " + Data._ID +
4048                                		" FROM " + Tables.DATA + "))", null);
4049                try {
4050                    while (cursor.moveToNext()) {
4051                        orphanContactIds.add(cursor.getLong(0));
4052                    }
4053                } finally {
4054                    cursor.close();
4055                }
4056
4057                for (Long contactId : orphanContactIds) {
4058                    mContactAggregator.updateAggregateData(contactId);
4059                }
4060            }
4061
4062            if (hasUnassignedContacts[0]) {
4063
4064                Account primaryAccount = null;
4065                for (Account account : accounts) {
4066                    if (isWritableAccount(account.type)) {
4067                        primaryAccount = account;
4068                        break;
4069                    }
4070                }
4071
4072                if (primaryAccount != null) {
4073                    String[] params = new String[] {primaryAccount.name, primaryAccount.type};
4074                    if (primaryAccount.type.equals(DEFAULT_ACCOUNT_TYPE)) {
4075                        long groupId = getOrCreateMyContactsGroupInTransaction(
4076                                primaryAccount.name, primaryAccount.type);
4077                        if (groupId != -1) {
4078                            long mimeTypeId = mDbHelper.getMimeTypeId(
4079                                    GroupMembership.CONTENT_ITEM_TYPE);
4080                            mDb.execSQL(
4081                                    "INSERT INTO " + Tables.DATA + "(" + DataColumns.MIMETYPE_ID +
4082                                        ", " + Data.RAW_CONTACT_ID + ", "
4083                                        + GroupMembership.GROUP_ROW_ID + ") " +
4084                                    "SELECT " + mimeTypeId + ", "
4085                                            + RawContacts._ID + ", " + groupId +
4086                                    " FROM " + Tables.RAW_CONTACTS +
4087                                    " WHERE " + RawContacts.ACCOUNT_NAME + " IS NULL" +
4088                                    " AND " + RawContacts.ACCOUNT_TYPE + " IS NULL"
4089                            );
4090                        }
4091                    }
4092                    mDb.execSQL(
4093                            "UPDATE " + Tables.RAW_CONTACTS +
4094                            " SET " + RawContacts.ACCOUNT_NAME + "=?,"
4095                                    + RawContacts.ACCOUNT_TYPE + "=?" +
4096                            " WHERE " + RawContacts.ACCOUNT_NAME + " IS NULL" +
4097                            " AND " + RawContacts.ACCOUNT_TYPE + " IS NULL", params);
4098
4099                    // We don't currently support groups for unsynced accounts, so this is for
4100                    // the future
4101                    mDb.execSQL(
4102                            "UPDATE " + Tables.GROUPS +
4103                            " SET " + Groups.ACCOUNT_NAME + "=?,"
4104                                    + Groups.ACCOUNT_TYPE + "=?" +
4105                            " WHERE " + Groups.ACCOUNT_NAME + " IS NULL" +
4106                            " AND " + Groups.ACCOUNT_TYPE + " IS NULL", params);
4107
4108                    mDb.execSQL(
4109                            "DELETE FROM " + Tables.ACCOUNTS +
4110                            " WHERE " + RawContacts.ACCOUNT_NAME + " IS NULL" +
4111                            " AND " + RawContacts.ACCOUNT_TYPE + " IS NULL");
4112                }
4113            }
4114
4115            mDbHelper.updateAllVisible();
4116
4117            mDbHelper.getSyncState().onAccountsChanged(mDb, accounts);
4118            mDb.setTransactionSuccessful();
4119        } finally {
4120            mDb.endTransaction();
4121        }
4122        mAccountWritability.clear();
4123    }
4124
4125    /**
4126     * Finds all distinct accounts present in the specified table.
4127     */
4128    private void findValidAccounts(Set<Account> validAccounts, boolean[] hasUnassignedContacts) {
4129        Cursor c = mDb.rawQuery(
4130                "SELECT " + RawContacts.ACCOUNT_NAME + "," + RawContacts.ACCOUNT_TYPE +
4131                " FROM " + Tables.ACCOUNTS, null);
4132        try {
4133            while (c.moveToNext()) {
4134                if (c.isNull(0) && c.isNull(1)) {
4135                    hasUnassignedContacts[0] = true;
4136                } else {
4137                    validAccounts.add(new Account(c.getString(0), c.getString(1)));
4138                }
4139            }
4140        } finally {
4141            c.close();
4142        }
4143    }
4144
4145    /**
4146     * Test all against {@link TextUtils#isEmpty(CharSequence)}.
4147     */
4148    private static boolean areAllEmpty(ContentValues values, String[] keys) {
4149        for (String key : keys) {
4150            if (!TextUtils.isEmpty(values.getAsString(key))) {
4151                return false;
4152            }
4153        }
4154        return true;
4155    }
4156
4157    /**
4158     * Returns true if a value (possibly null) is specified for at least one of the supplied keys.
4159     */
4160    private static boolean areAnySpecified(ContentValues values, String[] keys) {
4161        for (String key : keys) {
4162            if (values.containsKey(key)) {
4163                return true;
4164            }
4165        }
4166        return false;
4167    }
4168
4169    @Override
4170    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
4171            String sortOrder) {
4172        if (VERBOSE_LOGGING) {
4173            Log.v(TAG, "query: " + uri);
4174        }
4175
4176        final SQLiteDatabase db = mDbHelper.getReadableDatabase();
4177
4178        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
4179        String groupBy = null;
4180        String limit = getLimit(uri);
4181
4182        // TODO: Consider writing a test case for RestrictionExceptions when you
4183        // write a new query() block to make sure it protects restricted data.
4184        final int match = sUriMatcher.match(uri);
4185        switch (match) {
4186            case SYNCSTATE:
4187                return mDbHelper.getSyncState().query(db, projection, selection,  selectionArgs,
4188                        sortOrder);
4189
4190            case CONTACTS: {
4191                setTablesAndProjectionMapForContacts(qb, uri, projection);
4192                break;
4193            }
4194
4195            case CONTACTS_ID: {
4196                long contactId = ContentUris.parseId(uri);
4197                setTablesAndProjectionMapForContacts(qb, uri, projection);
4198                selectionArgs = insertSelectionArg(selectionArgs, String.valueOf(contactId));
4199                qb.appendWhere(Contacts._ID + "=?");
4200                break;
4201            }
4202
4203            case CONTACTS_LOOKUP:
4204            case CONTACTS_LOOKUP_ID: {
4205                List<String> pathSegments = uri.getPathSegments();
4206                int segmentCount = pathSegments.size();
4207                if (segmentCount < 3) {
4208                    throw new IllegalArgumentException(mDbHelper.exceptionMessage(
4209                            "Missing a lookup key", uri));
4210                }
4211                String lookupKey = pathSegments.get(2);
4212                if (segmentCount == 4) {
4213                    // TODO: pull this out into a method and generalize to not require contactId
4214                    long contactId = Long.parseLong(pathSegments.get(3));
4215                    SQLiteQueryBuilder lookupQb = new SQLiteQueryBuilder();
4216                    setTablesAndProjectionMapForContacts(lookupQb, uri, projection);
4217                    String[] args;
4218                    if (selectionArgs == null) {
4219                        args = new String[2];
4220                    } else {
4221                        args = new String[selectionArgs.length + 2];
4222                        System.arraycopy(selectionArgs, 0, args, 2, selectionArgs.length);
4223                    }
4224                    args[0] = String.valueOf(contactId);
4225                    args[1] = Uri.encode(lookupKey);
4226                    lookupQb.appendWhere(Contacts._ID + "=? AND " + Contacts.LOOKUP_KEY + "=?");
4227                    Cursor c = query(db, lookupQb, projection, selection, args, sortOrder,
4228                            groupBy, limit);
4229                    if (c.getCount() != 0) {
4230                        return c;
4231                    }
4232
4233                    c.close();
4234                }
4235
4236                setTablesAndProjectionMapForContacts(qb, uri, projection);
4237                selectionArgs = insertSelectionArg(selectionArgs,
4238                        String.valueOf(lookupContactIdByLookupKey(db, lookupKey)));
4239                qb.appendWhere(Contacts._ID + "=?");
4240                break;
4241            }
4242
4243            case CONTACTS_AS_VCARD: {
4244                // When reading as vCard always use restricted view
4245                final String lookupKey = Uri.encode(uri.getPathSegments().get(2));
4246                qb.setTables(mDbHelper.getContactView(true /* require restricted */));
4247                qb.setProjectionMap(sContactsVCardProjectionMap);
4248                selectionArgs = insertSelectionArg(selectionArgs,
4249                        String.valueOf(lookupContactIdByLookupKey(db, lookupKey)));
4250                qb.appendWhere(Contacts._ID + "=?");
4251                break;
4252            }
4253
4254            case CONTACTS_AS_MULTI_VCARD: {
4255                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
4256                String currentDateString = dateFormat.format(new Date()).toString();
4257                return db.rawQuery(
4258                    "SELECT" +
4259                    " 'vcards_' || ? || '.vcf' AS " + OpenableColumns.DISPLAY_NAME + "," +
4260                    " NULL AS " + OpenableColumns.SIZE,
4261                    new String[] { currentDateString });
4262            }
4263
4264            case CONTACTS_FILTER: {
4265                String filterParam = "";
4266                if (uri.getPathSegments().size() > 2) {
4267                    filterParam = uri.getLastPathSegment();
4268                }
4269                setTablesAndProjectionMapForContactsWithSnippet(qb, uri, projection, filterParam);
4270                break;
4271            }
4272
4273            case CONTACTS_STREQUENT_FILTER:
4274            case CONTACTS_STREQUENT: {
4275                String filterSql = null;
4276                if (match == CONTACTS_STREQUENT_FILTER
4277                        && uri.getPathSegments().size() > 3) {
4278                    String filterParam = uri.getLastPathSegment();
4279                    StringBuilder sb = new StringBuilder();
4280                    sb.append(Contacts._ID + " IN ");
4281                    appendContactFilterAsNestedQuery(sb, filterParam);
4282                    filterSql = sb.toString();
4283                }
4284
4285                setTablesAndProjectionMapForContacts(qb, uri, projection);
4286
4287                String[] starredProjection = null;
4288                String[] frequentProjection = null;
4289                if (projection != null) {
4290                    starredProjection = appendProjectionArg(projection, TIMES_CONTACED_SORT_COLUMN);
4291                    frequentProjection = appendProjectionArg(projection, TIMES_CONTACED_SORT_COLUMN);
4292                }
4293
4294                // Build the first query for starred
4295                if (filterSql != null) {
4296                    qb.appendWhere(filterSql);
4297                }
4298                qb.setProjectionMap(sStrequentStarredProjectionMap);
4299                final String starredQuery = qb.buildQuery(starredProjection, Contacts.STARRED + "=1",
4300                        null, Contacts._ID, null, null, null);
4301
4302                // Build the second query for frequent
4303                qb = new SQLiteQueryBuilder();
4304                setTablesAndProjectionMapForContacts(qb, uri, projection);
4305                if (filterSql != null) {
4306                    qb.appendWhere(filterSql);
4307                }
4308                qb.setProjectionMap(sStrequentFrequentProjectionMap);
4309                final String frequentQuery = qb.buildQuery(frequentProjection,
4310                        Contacts.TIMES_CONTACTED + " > 0 AND (" + Contacts.STARRED
4311                        + " = 0 OR " + Contacts.STARRED + " IS NULL)",
4312                        null, Contacts._ID, null, null, null);
4313
4314                // Put them together
4315                final String query = qb.buildUnionQuery(new String[] {starredQuery, frequentQuery},
4316                        STREQUENT_ORDER_BY, STREQUENT_LIMIT);
4317                Cursor c = db.rawQuery(query, null);
4318                if (c != null) {
4319                    c.setNotificationUri(getContext().getContentResolver(),
4320                            ContactsContract.AUTHORITY_URI);
4321                }
4322                return c;
4323            }
4324
4325            case CONTACTS_GROUP: {
4326                setTablesAndProjectionMapForContacts(qb, uri, projection);
4327                if (uri.getPathSegments().size() > 2) {
4328                    qb.appendWhere(CONTACTS_IN_GROUP_SELECT);
4329                    selectionArgs = insertSelectionArg(selectionArgs, uri.getLastPathSegment());
4330                }
4331                break;
4332            }
4333
4334            case CONTACTS_DATA: {
4335                long contactId = Long.parseLong(uri.getPathSegments().get(1));
4336                setTablesAndProjectionMapForData(qb, uri, projection, false);
4337                selectionArgs = insertSelectionArg(selectionArgs, String.valueOf(contactId));
4338                qb.appendWhere(" AND " + RawContacts.CONTACT_ID + "=?");
4339                break;
4340            }
4341
4342            case CONTACTS_PHOTO: {
4343                long contactId = Long.parseLong(uri.getPathSegments().get(1));
4344                setTablesAndProjectionMapForData(qb, uri, projection, false);
4345                selectionArgs = insertSelectionArg(selectionArgs, String.valueOf(contactId));
4346                qb.appendWhere(" AND " + RawContacts.CONTACT_ID + "=?");
4347                qb.appendWhere(" AND " + Data._ID + "=" + Contacts.PHOTO_ID);
4348                break;
4349            }
4350
4351            case PHONES: {
4352                setTablesAndProjectionMapForData(qb, uri, projection, false);
4353                qb.appendWhere(" AND " + Data.MIMETYPE + " = '" + Phone.CONTENT_ITEM_TYPE + "'");
4354                break;
4355            }
4356
4357            case PHONES_ID: {
4358                setTablesAndProjectionMapForData(qb, uri, projection, false);
4359                selectionArgs = insertSelectionArg(selectionArgs, uri.getLastPathSegment());
4360                qb.appendWhere(" AND " + Data.MIMETYPE + " = '" + Phone.CONTENT_ITEM_TYPE + "'");
4361                qb.appendWhere(" AND " + Data._ID + "=?");
4362                break;
4363            }
4364
4365            case PHONES_FILTER: {
4366                setTablesAndProjectionMapForData(qb, uri, projection, true);
4367                qb.appendWhere(" AND " + Data.MIMETYPE + " = '" + Phone.CONTENT_ITEM_TYPE + "'");
4368                if (uri.getPathSegments().size() > 2) {
4369                    String filterParam = uri.getLastPathSegment();
4370                    StringBuilder sb = new StringBuilder();
4371                    sb.append(" AND (");
4372
4373                    boolean orNeeded = false;
4374                    String normalizedName = NameNormalizer.normalize(filterParam);
4375                    if (normalizedName.length() > 0) {
4376                        sb.append(Data.RAW_CONTACT_ID + " IN ");
4377                        appendRawContactsByNormalizedNameFilter(sb, normalizedName, false);
4378                        orNeeded = true;
4379                    }
4380
4381                    if (isPhoneNumber(filterParam)) {
4382                        if (orNeeded) {
4383                            sb.append(" OR ");
4384                        }
4385                        String number = PhoneNumberUtils.convertKeypadLettersToDigits(filterParam);
4386                        String reversed = PhoneNumberUtils.getStrippedReversed(number);
4387                        sb.append(Data._ID +
4388                                " IN (SELECT " + PhoneLookupColumns.DATA_ID
4389                                  + " FROM " + Tables.PHONE_LOOKUP
4390                                  + " WHERE " + PhoneLookupColumns.NORMALIZED_NUMBER + " LIKE '%");
4391                        sb.append(reversed);
4392                        sb.append("')");
4393                    }
4394                    sb.append(")");
4395                    qb.appendWhere(sb);
4396                }
4397                groupBy = PhoneColumns.NORMALIZED_NUMBER + "," + RawContacts.CONTACT_ID;
4398                if (sortOrder == null) {
4399                    sortOrder = Contacts.IN_VISIBLE_GROUP + " DESC, " + RawContacts.CONTACT_ID;
4400                }
4401                break;
4402            }
4403
4404            case EMAILS: {
4405                setTablesAndProjectionMapForData(qb, uri, projection, false);
4406                qb.appendWhere(" AND " + Data.MIMETYPE + " = '" + Email.CONTENT_ITEM_TYPE + "'");
4407                break;
4408            }
4409
4410            case EMAILS_ID: {
4411                setTablesAndProjectionMapForData(qb, uri, projection, false);
4412                selectionArgs = insertSelectionArg(selectionArgs, uri.getLastPathSegment());
4413                qb.appendWhere(" AND " + Data.MIMETYPE + " = '" + Email.CONTENT_ITEM_TYPE + "'"
4414                        + " AND " + Data._ID + "=?");
4415                break;
4416            }
4417
4418            case EMAILS_LOOKUP: {
4419                setTablesAndProjectionMapForData(qb, uri, projection, false);
4420                qb.appendWhere(" AND " + Data.MIMETYPE + " = '" + Email.CONTENT_ITEM_TYPE + "'");
4421                if (uri.getPathSegments().size() > 2) {
4422                    String email = uri.getLastPathSegment();
4423                    String address = mDbHelper.extractAddressFromEmailAddress(email);
4424                    selectionArgs = insertSelectionArg(selectionArgs, address);
4425                    qb.appendWhere(" AND UPPER(" + Email.DATA + ")=UPPER(?)");
4426                }
4427                break;
4428            }
4429
4430            case EMAILS_FILTER: {
4431                setTablesAndProjectionMapForData(qb, uri, projection, true);
4432                String filterParam = null;
4433                if (uri.getPathSegments().size() > 3) {
4434                    filterParam = uri.getLastPathSegment();
4435                    if (TextUtils.isEmpty(filterParam)) {
4436                        filterParam = null;
4437                    }
4438                }
4439
4440                if (filterParam == null) {
4441                    // If the filter is unspecified, return nothing
4442                    qb.appendWhere(" AND 0");
4443                } else {
4444                    StringBuilder sb = new StringBuilder();
4445                    sb.append(" AND " + Data._ID + " IN (");
4446                    sb.append(
4447                            "SELECT " + Data._ID +
4448                            " FROM " + Tables.DATA +
4449                            " WHERE " + DataColumns.MIMETYPE_ID + "=" + mMimeTypeIdEmail +
4450                            " AND " + Data.DATA1 + " LIKE ");
4451                    DatabaseUtils.appendEscapedSQLString(sb, filterParam + '%');
4452                    if (!filterParam.contains("@")) {
4453                        String normalizedName = NameNormalizer.normalize(filterParam);
4454                        if (normalizedName.length() > 0) {
4455
4456                            /*
4457                             * Using a UNION instead of an "OR" to make SQLite use the right
4458                             * indexes. We need it to use the (mimetype,data1) index for the
4459                             * email lookup (see above), but not for the name lookup.
4460                             * SQLite is not smart enough to use the index on one side of an OR
4461                             * but not on the other. Using two separate nested queries
4462                             * and a UNION between them does the job.
4463                             */
4464                            sb.append(
4465                                    " UNION SELECT " + Data._ID +
4466                                    " FROM " + Tables.DATA +
4467                                    " WHERE +" + DataColumns.MIMETYPE_ID + "=" + mMimeTypeIdEmail +
4468                                    " AND " + Data.RAW_CONTACT_ID + " IN ");
4469                            appendRawContactsByNormalizedNameFilter(sb, normalizedName, false);
4470                        }
4471                    }
4472                    sb.append(")");
4473                    qb.appendWhere(sb);
4474                }
4475                groupBy = Email.DATA + "," + RawContacts.CONTACT_ID;
4476                if (sortOrder == null) {
4477                    sortOrder = Contacts.IN_VISIBLE_GROUP + " DESC, " + RawContacts.CONTACT_ID;
4478                }
4479                break;
4480            }
4481
4482            case POSTALS: {
4483                setTablesAndProjectionMapForData(qb, uri, projection, false);
4484                qb.appendWhere(" AND " + Data.MIMETYPE + " = '"
4485                        + StructuredPostal.CONTENT_ITEM_TYPE + "'");
4486                break;
4487            }
4488
4489            case POSTALS_ID: {
4490                setTablesAndProjectionMapForData(qb, uri, projection, false);
4491                selectionArgs = insertSelectionArg(selectionArgs, uri.getLastPathSegment());
4492                qb.appendWhere(" AND " + Data.MIMETYPE + " = '"
4493                        + StructuredPostal.CONTENT_ITEM_TYPE + "'");
4494                qb.appendWhere(" AND " + Data._ID + "=?");
4495                break;
4496            }
4497
4498            case RAW_CONTACTS: {
4499                setTablesAndProjectionMapForRawContacts(qb, uri);
4500                break;
4501            }
4502
4503            case RAW_CONTACTS_ID: {
4504                long rawContactId = ContentUris.parseId(uri);
4505                setTablesAndProjectionMapForRawContacts(qb, uri);
4506                selectionArgs = insertSelectionArg(selectionArgs, String.valueOf(rawContactId));
4507                qb.appendWhere(" AND " + RawContacts._ID + "=?");
4508                break;
4509            }
4510
4511            case RAW_CONTACTS_DATA: {
4512                long rawContactId = Long.parseLong(uri.getPathSegments().get(1));
4513                setTablesAndProjectionMapForData(qb, uri, projection, false);
4514                selectionArgs = insertSelectionArg(selectionArgs, String.valueOf(rawContactId));
4515                qb.appendWhere(" AND " + Data.RAW_CONTACT_ID + "=?");
4516                break;
4517            }
4518
4519            case DATA: {
4520                setTablesAndProjectionMapForData(qb, uri, projection, false);
4521                break;
4522            }
4523
4524            case DATA_ID: {
4525                setTablesAndProjectionMapForData(qb, uri, projection, false);
4526                selectionArgs = insertSelectionArg(selectionArgs, uri.getLastPathSegment());
4527                qb.appendWhere(" AND " + Data._ID + "=?");
4528                break;
4529            }
4530
4531            case PHONE_LOOKUP: {
4532
4533                if (TextUtils.isEmpty(sortOrder)) {
4534                    // Default the sort order to something reasonable so we get consistent
4535                    // results when callers don't request an ordering
4536                    sortOrder = RawContactsColumns.CONCRETE_ID;
4537                }
4538
4539                String number = uri.getPathSegments().size() > 1 ? uri.getLastPathSegment() : "";
4540                mDbHelper.buildPhoneLookupAndContactQuery(qb, number);
4541                qb.setProjectionMap(sPhoneLookupProjectionMap);
4542
4543                // Phone lookup cannot be combined with a selection
4544                selection = null;
4545                selectionArgs = null;
4546                break;
4547            }
4548
4549            case GROUPS: {
4550                qb.setTables(mDbHelper.getGroupView());
4551                qb.setProjectionMap(sGroupsProjectionMap);
4552                appendAccountFromParameter(qb, uri);
4553                break;
4554            }
4555
4556            case GROUPS_ID: {
4557                qb.setTables(mDbHelper.getGroupView());
4558                qb.setProjectionMap(sGroupsProjectionMap);
4559                selectionArgs = insertSelectionArg(selectionArgs, uri.getLastPathSegment());
4560                qb.appendWhere(Groups._ID + "=?");
4561                break;
4562            }
4563
4564            case GROUPS_SUMMARY: {
4565                qb.setTables(mDbHelper.getGroupView() + " AS groups");
4566                qb.setProjectionMap(sGroupsSummaryProjectionMap);
4567                appendAccountFromParameter(qb, uri);
4568                groupBy = Groups._ID;
4569                break;
4570            }
4571
4572            case AGGREGATION_EXCEPTIONS: {
4573                qb.setTables(Tables.AGGREGATION_EXCEPTIONS);
4574                qb.setProjectionMap(sAggregationExceptionsProjectionMap);
4575                break;
4576            }
4577
4578            case AGGREGATION_SUGGESTIONS: {
4579                long contactId = Long.parseLong(uri.getPathSegments().get(1));
4580                String filter = null;
4581                if (uri.getPathSegments().size() > 3) {
4582                    filter = uri.getPathSegments().get(3);
4583                }
4584                final int maxSuggestions;
4585                if (limit != null) {
4586                    maxSuggestions = Integer.parseInt(limit);
4587                } else {
4588                    maxSuggestions = DEFAULT_MAX_SUGGESTIONS;
4589                }
4590
4591                setTablesAndProjectionMapForContacts(qb, uri, projection);
4592
4593                return mContactAggregator.queryAggregationSuggestions(qb, projection, contactId,
4594                        maxSuggestions, filter);
4595            }
4596
4597            case SETTINGS: {
4598                qb.setTables(Tables.SETTINGS);
4599                qb.setProjectionMap(sSettingsProjectionMap);
4600                appendAccountFromParameter(qb, uri);
4601
4602                // When requesting specific columns, this query requires
4603                // late-binding of the GroupMembership MIME-type.
4604                final String groupMembershipMimetypeId = Long.toString(mDbHelper
4605                        .getMimeTypeId(GroupMembership.CONTENT_ITEM_TYPE));
4606                if (projection != null && projection.length != 0 &&
4607                        mDbHelper.isInProjection(projection, Settings.UNGROUPED_COUNT)) {
4608                    selectionArgs = insertSelectionArg(selectionArgs, groupMembershipMimetypeId);
4609                }
4610                if (projection != null && projection.length != 0 &&
4611                        mDbHelper.isInProjection(projection, Settings.UNGROUPED_WITH_PHONES)) {
4612                    selectionArgs = insertSelectionArg(selectionArgs, groupMembershipMimetypeId);
4613                }
4614
4615                break;
4616            }
4617
4618            case STATUS_UPDATES: {
4619                setTableAndProjectionMapForStatusUpdates(qb, projection);
4620                break;
4621            }
4622
4623            case STATUS_UPDATES_ID: {
4624                setTableAndProjectionMapForStatusUpdates(qb, projection);
4625                selectionArgs = insertSelectionArg(selectionArgs, uri.getLastPathSegment());
4626                qb.appendWhere(DataColumns.CONCRETE_ID + "=?");
4627                break;
4628            }
4629
4630            case SEARCH_SUGGESTIONS: {
4631                return mGlobalSearchSupport.handleSearchSuggestionsQuery(db, uri, limit);
4632            }
4633
4634            case SEARCH_SHORTCUT: {
4635                String lookupKey = uri.getLastPathSegment();
4636                return mGlobalSearchSupport.handleSearchShortcutRefresh(db, lookupKey, projection);
4637            }
4638
4639            case LIVE_FOLDERS_CONTACTS:
4640                qb.setTables(mDbHelper.getContactView());
4641                qb.setProjectionMap(sLiveFoldersProjectionMap);
4642                break;
4643
4644            case LIVE_FOLDERS_CONTACTS_WITH_PHONES:
4645                qb.setTables(mDbHelper.getContactView());
4646                qb.setProjectionMap(sLiveFoldersProjectionMap);
4647                qb.appendWhere(Contacts.HAS_PHONE_NUMBER + "=1");
4648                break;
4649
4650            case LIVE_FOLDERS_CONTACTS_FAVORITES:
4651                qb.setTables(mDbHelper.getContactView());
4652                qb.setProjectionMap(sLiveFoldersProjectionMap);
4653                qb.appendWhere(Contacts.STARRED + "=1");
4654                break;
4655
4656            case LIVE_FOLDERS_CONTACTS_GROUP_NAME:
4657                qb.setTables(mDbHelper.getContactView());
4658                qb.setProjectionMap(sLiveFoldersProjectionMap);
4659                qb.appendWhere(CONTACTS_IN_GROUP_SELECT);
4660                selectionArgs = insertSelectionArg(selectionArgs, uri.getLastPathSegment());
4661                break;
4662
4663            case RAW_CONTACT_ENTITIES: {
4664                setTablesAndProjectionMapForRawContactsEntities(qb, uri);
4665                break;
4666            }
4667
4668            case RAW_CONTACT_ENTITY_ID: {
4669                long rawContactId = Long.parseLong(uri.getPathSegments().get(1));
4670                setTablesAndProjectionMapForRawContactsEntities(qb, uri);
4671                selectionArgs = insertSelectionArg(selectionArgs, String.valueOf(rawContactId));
4672                qb.appendWhere(" AND " + RawContacts._ID + "=?");
4673                break;
4674            }
4675
4676            case PROVIDER_STATUS: {
4677                return queryProviderStatus(uri, projection);
4678            }
4679
4680            default:
4681                return mLegacyApiSupport.query(uri, projection, selection, selectionArgs,
4682                        sortOrder, limit);
4683        }
4684
4685        Cursor cursor =
4686                query(db, qb, projection, selection, selectionArgs, sortOrder, groupBy, limit);
4687        if (readBooleanQueryParameter(uri, ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, false)) {
4688            cursor = bundleLetterCountExtras(cursor, db, qb, selection, selectionArgs, sortOrder);
4689        }
4690        return cursor;
4691    }
4692
4693    private Cursor query(final SQLiteDatabase db, SQLiteQueryBuilder qb, String[] projection,
4694            String selection, String[] selectionArgs, String sortOrder, String groupBy,
4695            String limit) {
4696        if (projection != null && projection.length == 1
4697                && BaseColumns._COUNT.equals(projection[0])) {
4698            qb.setProjectionMap(sCountProjectionMap);
4699        }
4700        final Cursor c = qb.query(db, projection, selection, selectionArgs, groupBy, null,
4701                sortOrder, limit);
4702        if (c != null) {
4703            c.setNotificationUri(getContext().getContentResolver(), ContactsContract.AUTHORITY_URI);
4704        }
4705        return c;
4706    }
4707
4708    /**
4709     * Creates a single-row cursor containing the current status of the provider.
4710     */
4711    private Cursor queryProviderStatus(Uri uri, String[] projection) {
4712        MatrixCursor cursor = new MatrixCursor(projection);
4713        RowBuilder row = cursor.newRow();
4714        for (int i = 0; i < projection.length; i++) {
4715            if (ProviderStatus.STATUS.equals(projection[i])) {
4716                row.add(mProviderStatus);
4717            } else if (ProviderStatus.DATA1.equals(projection[i])) {
4718                row.add(mEstimatedStorageRequirement);
4719            }
4720        }
4721        return cursor;
4722    }
4723
4724
4725    private static final class AddressBookIndexQuery {
4726        public static final String LETTER = "letter";
4727        public static final String TITLE = "title";
4728        public static final String COUNT = "count";
4729
4730        public static final String[] COLUMNS = new String[] {
4731                LETTER, TITLE, COUNT
4732        };
4733
4734        public static final int COLUMN_LETTER = 0;
4735        public static final int COLUMN_TITLE = 1;
4736        public static final int COLUMN_COUNT = 2;
4737
4738        public static final String ORDER_BY = LETTER + " COLLATE " + PHONEBOOK_COLLATOR_NAME;
4739    }
4740
4741    /**
4742     * Computes counts by the address book index titles and adds the resulting tally
4743     * to the returned cursor as a bundle of extras.
4744     */
4745    private Cursor bundleLetterCountExtras(Cursor cursor, final SQLiteDatabase db,
4746            SQLiteQueryBuilder qb, String selection, String[] selectionArgs, String sortOrder) {
4747        String sortKey;
4748
4749        // The sort order suffix could be something like "DESC".
4750        // We want to preserve it in the query even though we will change
4751        // the sort column itself.
4752        String sortOrderSuffix = "";
4753        if (sortOrder != null) {
4754            int spaceIndex = sortOrder.indexOf(' ');
4755            if (spaceIndex != -1) {
4756                sortKey = sortOrder.substring(0, spaceIndex);
4757                sortOrderSuffix = sortOrder.substring(spaceIndex);
4758            } else {
4759                sortKey = sortOrder;
4760            }
4761        } else {
4762            sortKey = Contacts.SORT_KEY_PRIMARY;
4763        }
4764
4765        String locale = getLocale().toString();
4766        HashMap<String, String> projectionMap = Maps.newHashMap();
4767        projectionMap.put(AddressBookIndexQuery.LETTER,
4768                "SUBSTR(" + sortKey + ",1,1) AS " + AddressBookIndexQuery.LETTER);
4769
4770        /**
4771         * Use the GET_PHONEBOOK_INDEX function, which is an android extension for SQLite3,
4772         * to map the first letter of the sort key to a character that is traditionally
4773         * used in phonebooks to represent that letter.  For example, in Korean it will
4774         * be the first consonant in the letter; for Japanese it will be Hiragana rather
4775         * than Katakana.
4776         */
4777        projectionMap.put(AddressBookIndexQuery.TITLE,
4778                "GET_PHONEBOOK_INDEX(SUBSTR(" + sortKey + ",1,1),'" + locale + "')"
4779                        + " AS " + AddressBookIndexQuery.TITLE);
4780        projectionMap.put(AddressBookIndexQuery.COUNT,
4781                "COUNT(" + Contacts._ID + ") AS " + AddressBookIndexQuery.COUNT);
4782        qb.setProjectionMap(projectionMap);
4783
4784        Cursor indexCursor = qb.query(db, AddressBookIndexQuery.COLUMNS, selection, selectionArgs,
4785                AddressBookIndexQuery.ORDER_BY, null /* having */,
4786                AddressBookIndexQuery.ORDER_BY + sortOrderSuffix);
4787
4788        try {
4789            int groupCount = indexCursor.getCount();
4790            String titles[] = new String[groupCount];
4791            int counts[] = new int[groupCount];
4792            int indexCount = 0;
4793            String currentTitle = null;
4794
4795            // Since GET_PHONEBOOK_INDEX is a many-to-1 function, we may end up
4796            // with multiple entries for the same title.  The following code
4797            // collapses those duplicates.
4798            for (int i = 0; i < groupCount; i++) {
4799                indexCursor.moveToNext();
4800                String title = indexCursor.getString(AddressBookIndexQuery.COLUMN_TITLE);
4801                int count = indexCursor.getInt(AddressBookIndexQuery.COLUMN_COUNT);
4802                if (indexCount == 0 || !TextUtils.equals(title, currentTitle)) {
4803                    titles[indexCount] = currentTitle = title;
4804                    counts[indexCount] = count;
4805                    indexCount++;
4806                } else {
4807                    counts[indexCount - 1] += count;
4808                }
4809            }
4810
4811            if (indexCount < groupCount) {
4812                String[] newTitles = new String[indexCount];
4813                System.arraycopy(titles, 0, newTitles, 0, indexCount);
4814                titles = newTitles;
4815
4816                int[] newCounts = new int[indexCount];
4817                System.arraycopy(counts, 0, newCounts, 0, indexCount);
4818                counts = newCounts;
4819            }
4820
4821            final Bundle bundle = new Bundle();
4822            bundle.putStringArray(ContactCounts.EXTRA_ADDRESS_BOOK_INDEX_TITLES, titles);
4823            bundle.putIntArray(ContactCounts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS, counts);
4824            return new CursorWrapper(cursor) {
4825
4826                @Override
4827                public Bundle getExtras() {
4828                    return bundle;
4829                }
4830            };
4831        } finally {
4832            indexCursor.close();
4833        }
4834    }
4835
4836    /**
4837     * Returns the contact Id for the contact identified by the lookupKey.
4838     * Robust against changes in the lookup key: if the key has changed, will
4839     * look up the contact by the raw contact IDs or name encoded in the lookup
4840     * key.
4841     */
4842    public long lookupContactIdByLookupKey(SQLiteDatabase db, String lookupKey) {
4843        ContactLookupKey key = new ContactLookupKey();
4844        ArrayList<LookupKeySegment> segments = key.parse(lookupKey);
4845
4846        long contactId = -1;
4847        if (lookupKeyContainsType(segments, ContactLookupKey.LOOKUP_TYPE_SOURCE_ID)) {
4848            contactId = lookupContactIdBySourceIds(db, segments);
4849            if (contactId != -1) {
4850                return contactId;
4851            }
4852        }
4853
4854        boolean hasRawContactIds =
4855                lookupKeyContainsType(segments, ContactLookupKey.LOOKUP_TYPE_RAW_CONTACT_ID);
4856        if (hasRawContactIds) {
4857            contactId = lookupContactIdByRawContactIds(db, segments);
4858            if (contactId != -1) {
4859                return contactId;
4860            }
4861        }
4862
4863        if (hasRawContactIds
4864                || lookupKeyContainsType(segments, ContactLookupKey.LOOKUP_TYPE_DISPLAY_NAME)) {
4865            contactId = lookupContactIdByDisplayNames(db, segments);
4866        }
4867
4868        return contactId;
4869    }
4870
4871    private interface LookupBySourceIdQuery {
4872        String TABLE = Tables.RAW_CONTACTS;
4873
4874        String COLUMNS[] = {
4875                RawContacts.CONTACT_ID,
4876                RawContacts.ACCOUNT_TYPE,
4877                RawContacts.ACCOUNT_NAME,
4878                RawContacts.SOURCE_ID
4879        };
4880
4881        int CONTACT_ID = 0;
4882        int ACCOUNT_TYPE = 1;
4883        int ACCOUNT_NAME = 2;
4884        int SOURCE_ID = 3;
4885    }
4886
4887    private long lookupContactIdBySourceIds(SQLiteDatabase db,
4888                ArrayList<LookupKeySegment> segments) {
4889        StringBuilder sb = new StringBuilder();
4890        sb.append(RawContacts.SOURCE_ID + " IN (");
4891        for (int i = 0; i < segments.size(); i++) {
4892            LookupKeySegment segment = segments.get(i);
4893            if (segment.lookupType == ContactLookupKey.LOOKUP_TYPE_SOURCE_ID) {
4894                DatabaseUtils.appendEscapedSQLString(sb, segment.key);
4895                sb.append(",");
4896            }
4897        }
4898        sb.setLength(sb.length() - 1);      // Last comma
4899        sb.append(") AND " + RawContacts.CONTACT_ID + " NOT NULL");
4900
4901        Cursor c = db.query(LookupBySourceIdQuery.TABLE, LookupBySourceIdQuery.COLUMNS,
4902                 sb.toString(), null, null, null, null);
4903        try {
4904            while (c.moveToNext()) {
4905                String accountType = c.getString(LookupBySourceIdQuery.ACCOUNT_TYPE);
4906                String accountName = c.getString(LookupBySourceIdQuery.ACCOUNT_NAME);
4907                int accountHashCode =
4908                        ContactLookupKey.getAccountHashCode(accountType, accountName);
4909                String sourceId = c.getString(LookupBySourceIdQuery.SOURCE_ID);
4910                for (int i = 0; i < segments.size(); i++) {
4911                    LookupKeySegment segment = segments.get(i);
4912                    if (segment.lookupType == ContactLookupKey.LOOKUP_TYPE_SOURCE_ID
4913                            && accountHashCode == segment.accountHashCode
4914                            && segment.key.equals(sourceId)) {
4915                        segment.contactId = c.getLong(LookupBySourceIdQuery.CONTACT_ID);
4916                        break;
4917                    }
4918                }
4919            }
4920        } finally {
4921            c.close();
4922        }
4923
4924        return getMostReferencedContactId(segments);
4925    }
4926
4927    private interface LookupByRawContactIdQuery {
4928        String TABLE = Tables.RAW_CONTACTS;
4929
4930        String COLUMNS[] = {
4931                RawContacts.CONTACT_ID,
4932                RawContacts.ACCOUNT_TYPE,
4933                RawContacts.ACCOUNT_NAME,
4934                RawContacts._ID,
4935        };
4936
4937        int CONTACT_ID = 0;
4938        int ACCOUNT_TYPE = 1;
4939        int ACCOUNT_NAME = 2;
4940        int ID = 3;
4941    }
4942
4943    private long lookupContactIdByRawContactIds(SQLiteDatabase db,
4944            ArrayList<LookupKeySegment> segments) {
4945        StringBuilder sb = new StringBuilder();
4946        sb.append(RawContacts._ID + " IN (");
4947        for (int i = 0; i < segments.size(); i++) {
4948            LookupKeySegment segment = segments.get(i);
4949            if (segment.lookupType == ContactLookupKey.LOOKUP_TYPE_RAW_CONTACT_ID) {
4950                sb.append(segment.rawContactId);
4951                sb.append(",");
4952            }
4953        }
4954        sb.setLength(sb.length() - 1);      // Last comma
4955        sb.append(") AND " + RawContacts.CONTACT_ID + " NOT NULL");
4956
4957        Cursor c = db.query(LookupByRawContactIdQuery.TABLE, LookupByRawContactIdQuery.COLUMNS,
4958                 sb.toString(), null, null, null, null);
4959        try {
4960            while (c.moveToNext()) {
4961                String accountType = c.getString(LookupByRawContactIdQuery.ACCOUNT_TYPE);
4962                String accountName = c.getString(LookupByRawContactIdQuery.ACCOUNT_NAME);
4963                int accountHashCode =
4964                        ContactLookupKey.getAccountHashCode(accountType, accountName);
4965                String rawContactId = c.getString(LookupByRawContactIdQuery.ID);
4966                for (int i = 0; i < segments.size(); i++) {
4967                    LookupKeySegment segment = segments.get(i);
4968                    if (segment.lookupType == ContactLookupKey.LOOKUP_TYPE_RAW_CONTACT_ID
4969                            && accountHashCode == segment.accountHashCode
4970                            && segment.rawContactId.equals(rawContactId)) {
4971                        segment.contactId = c.getLong(LookupByRawContactIdQuery.CONTACT_ID);
4972                        break;
4973                    }
4974                }
4975            }
4976        } finally {
4977            c.close();
4978        }
4979
4980        return getMostReferencedContactId(segments);
4981    }
4982
4983    private interface LookupByDisplayNameQuery {
4984        String TABLE = Tables.NAME_LOOKUP_JOIN_RAW_CONTACTS;
4985
4986        String COLUMNS[] = {
4987                RawContacts.CONTACT_ID,
4988                RawContacts.ACCOUNT_TYPE,
4989                RawContacts.ACCOUNT_NAME,
4990                NameLookupColumns.NORMALIZED_NAME
4991        };
4992
4993        int CONTACT_ID = 0;
4994        int ACCOUNT_TYPE = 1;
4995        int ACCOUNT_NAME = 2;
4996        int NORMALIZED_NAME = 3;
4997    }
4998
4999    private long lookupContactIdByDisplayNames(SQLiteDatabase db,
5000                ArrayList<LookupKeySegment> segments) {
5001        StringBuilder sb = new StringBuilder();
5002        sb.append(NameLookupColumns.NORMALIZED_NAME + " IN (");
5003        for (int i = 0; i < segments.size(); i++) {
5004            LookupKeySegment segment = segments.get(i);
5005            if (segment.lookupType == ContactLookupKey.LOOKUP_TYPE_DISPLAY_NAME
5006                    || segment.lookupType == ContactLookupKey.LOOKUP_TYPE_RAW_CONTACT_ID) {
5007                DatabaseUtils.appendEscapedSQLString(sb, segment.key);
5008                sb.append(",");
5009            }
5010        }
5011        sb.setLength(sb.length() - 1);      // Last comma
5012        sb.append(") AND " + NameLookupColumns.NAME_TYPE + "=" + NameLookupType.NAME_COLLATION_KEY
5013                + " AND " + RawContacts.CONTACT_ID + " NOT NULL");
5014
5015        Cursor c = db.query(LookupByDisplayNameQuery.TABLE, LookupByDisplayNameQuery.COLUMNS,
5016                 sb.toString(), null, null, null, null);
5017        try {
5018            while (c.moveToNext()) {
5019                String accountType = c.getString(LookupByDisplayNameQuery.ACCOUNT_TYPE);
5020                String accountName = c.getString(LookupByDisplayNameQuery.ACCOUNT_NAME);
5021                int accountHashCode =
5022                        ContactLookupKey.getAccountHashCode(accountType, accountName);
5023                String name = c.getString(LookupByDisplayNameQuery.NORMALIZED_NAME);
5024                for (int i = 0; i < segments.size(); i++) {
5025                    LookupKeySegment segment = segments.get(i);
5026                    if ((segment.lookupType == ContactLookupKey.LOOKUP_TYPE_DISPLAY_NAME
5027                            || segment.lookupType == ContactLookupKey.LOOKUP_TYPE_RAW_CONTACT_ID)
5028                            && accountHashCode == segment.accountHashCode
5029                            && segment.key.equals(name)) {
5030                        segment.contactId = c.getLong(LookupByDisplayNameQuery.CONTACT_ID);
5031                        break;
5032                    }
5033                }
5034            }
5035        } finally {
5036            c.close();
5037        }
5038
5039        return getMostReferencedContactId(segments);
5040    }
5041
5042    private boolean lookupKeyContainsType(ArrayList<LookupKeySegment> segments, int lookupType) {
5043        for (int i = 0; i < segments.size(); i++) {
5044            LookupKeySegment segment = segments.get(i);
5045            if (segment.lookupType == lookupType) {
5046                return true;
5047            }
5048        }
5049
5050        return false;
5051    }
5052
5053    public void updateLookupKeyForRawContact(SQLiteDatabase db, long rawContactId) {
5054        mContactAggregator.updateLookupKeyForRawContact(db, rawContactId);
5055    }
5056
5057    /**
5058     * Returns the contact ID that is mentioned the highest number of times.
5059     */
5060    private long getMostReferencedContactId(ArrayList<LookupKeySegment> segments) {
5061        Collections.sort(segments);
5062
5063        long bestContactId = -1;
5064        int bestRefCount = 0;
5065
5066        long contactId = -1;
5067        int count = 0;
5068
5069        int segmentCount = segments.size();
5070        for (int i = 0; i < segmentCount; i++) {
5071            LookupKeySegment segment = segments.get(i);
5072            if (segment.contactId != -1) {
5073                if (segment.contactId == contactId) {
5074                    count++;
5075                } else {
5076                    if (count > bestRefCount) {
5077                        bestContactId = contactId;
5078                        bestRefCount = count;
5079                    }
5080                    contactId = segment.contactId;
5081                    count = 1;
5082                }
5083            }
5084        }
5085        if (count > bestRefCount) {
5086            return contactId;
5087        } else {
5088            return bestContactId;
5089        }
5090    }
5091
5092    private void setTablesAndProjectionMapForContacts(SQLiteQueryBuilder qb, Uri uri,
5093            String[] projection) {
5094        StringBuilder sb = new StringBuilder();
5095        appendContactsTables(sb, uri, projection);
5096        qb.setTables(sb.toString());
5097        qb.setProjectionMap(sContactsProjectionMap);
5098    }
5099
5100    /**
5101     * Finds name lookup records matching the supplied filter, picks one arbitrary match per
5102     * contact and joins that with other contacts tables.
5103     */
5104    private void setTablesAndProjectionMapForContactsWithSnippet(SQLiteQueryBuilder qb, Uri uri,
5105            String[] projection, String filter) {
5106
5107        StringBuilder sb = new StringBuilder();
5108        appendContactsTables(sb, uri, projection);
5109
5110        sb.append(" JOIN (SELECT " +
5111                RawContacts.CONTACT_ID + " AS snippet_contact_id");
5112
5113        if (mDbHelper.isInProjection(projection, SearchSnippetColumns.SNIPPET_DATA_ID)) {
5114            sb.append(", " + DataColumns.CONCRETE_ID + " AS "
5115                    + SearchSnippetColumns.SNIPPET_DATA_ID);
5116        }
5117
5118        if (mDbHelper.isInProjection(projection, SearchSnippetColumns.SNIPPET_DATA1)) {
5119            sb.append(", " + Data.DATA1 + " AS " + SearchSnippetColumns.SNIPPET_DATA1);
5120        }
5121
5122        if (mDbHelper.isInProjection(projection, SearchSnippetColumns.SNIPPET_DATA2)) {
5123            sb.append(", " + Data.DATA2 + " AS " + SearchSnippetColumns.SNIPPET_DATA2);
5124        }
5125
5126        if (mDbHelper.isInProjection(projection, SearchSnippetColumns.SNIPPET_DATA3)) {
5127            sb.append(", " + Data.DATA3 + " AS " + SearchSnippetColumns.SNIPPET_DATA3);
5128        }
5129
5130        if (mDbHelper.isInProjection(projection, SearchSnippetColumns.SNIPPET_DATA4)) {
5131            sb.append(", " + Data.DATA4 + " AS " + SearchSnippetColumns.SNIPPET_DATA4);
5132        }
5133
5134        if (mDbHelper.isInProjection(projection, SearchSnippetColumns.SNIPPET_MIMETYPE)) {
5135            sb.append(", (" +
5136                    "SELECT " + MimetypesColumns.MIMETYPE +
5137                    " FROM " + Tables.MIMETYPES +
5138                    " WHERE " + MimetypesColumns._ID + "=" + DataColumns.MIMETYPE_ID +
5139                    ") AS " + SearchSnippetColumns.SNIPPET_MIMETYPE);
5140        }
5141
5142        sb.append(" FROM " + Tables.DATA_JOIN_RAW_CONTACTS +
5143                " WHERE " + DataColumns.CONCRETE_ID +
5144                " IN (");
5145
5146        // Construct a query that gives us exactly one data _id per matching contact.
5147        // MIN stands in for ANY in this context.
5148        sb.append(
5149                "SELECT MIN(" + Tables.NAME_LOOKUP + "." + NameLookupColumns.DATA_ID + ")" +
5150                " FROM " + Tables.NAME_LOOKUP +
5151                " JOIN " + Tables.RAW_CONTACTS +
5152                " ON (" + RawContactsColumns.CONCRETE_ID
5153                        + "=" + Tables.NAME_LOOKUP + "." + NameLookupColumns.RAW_CONTACT_ID + ")" +
5154                " WHERE " + NameLookupColumns.NORMALIZED_NAME + " GLOB '");
5155        sb.append(NameNormalizer.normalize(filter));
5156        sb.append("*' AND " + NameLookupColumns.NAME_TYPE +
5157                    " IN(" + CONTACT_LOOKUP_NAME_TYPES + ")" +
5158                " GROUP BY " + RawContactsColumns.CONCRETE_CONTACT_ID);
5159
5160        sb.append(")) ON (" + Contacts._ID + "=snippet_contact_id)");
5161
5162        qb.setTables(sb.toString());
5163        qb.setProjectionMap(sContactsProjectionWithSnippetMap);
5164    }
5165
5166    private void appendContactsTables(StringBuilder sb, Uri uri, String[] projection) {
5167        boolean excludeRestrictedData = false;
5168        String requestingPackage = getQueryParameter(uri,
5169                ContactsContract.REQUESTING_PACKAGE_PARAM_KEY);
5170        if (requestingPackage != null) {
5171            excludeRestrictedData = !mDbHelper.hasAccessToRestrictedData(requestingPackage);
5172        }
5173        sb.append(mDbHelper.getContactView(excludeRestrictedData));
5174        if (mDbHelper.isInProjection(projection,
5175                Contacts.CONTACT_PRESENCE)) {
5176            sb.append(" LEFT OUTER JOIN " + Tables.AGGREGATED_PRESENCE +
5177                    " ON (" + Contacts._ID + " = " + AggregatedPresenceColumns.CONTACT_ID + ")");
5178        }
5179        if (mDbHelper.isInProjection(projection,
5180                Contacts.CONTACT_STATUS,
5181                Contacts.CONTACT_STATUS_RES_PACKAGE,
5182                Contacts.CONTACT_STATUS_ICON,
5183                Contacts.CONTACT_STATUS_LABEL,
5184                Contacts.CONTACT_STATUS_TIMESTAMP)) {
5185            sb.append(" LEFT OUTER JOIN " + Tables.STATUS_UPDATES + " "
5186                    + ContactsStatusUpdatesColumns.ALIAS +
5187                    " ON (" + ContactsColumns.LAST_STATUS_UPDATE_ID + "="
5188                            + ContactsStatusUpdatesColumns.CONCRETE_DATA_ID + ")");
5189        }
5190    }
5191
5192    private void setTablesAndProjectionMapForRawContacts(SQLiteQueryBuilder qb, Uri uri) {
5193        StringBuilder sb = new StringBuilder();
5194        boolean excludeRestrictedData = false;
5195        String requestingPackage = getQueryParameter(uri,
5196                ContactsContract.REQUESTING_PACKAGE_PARAM_KEY);
5197        if (requestingPackage != null) {
5198            excludeRestrictedData = !mDbHelper.hasAccessToRestrictedData(requestingPackage);
5199        }
5200        sb.append(mDbHelper.getRawContactView(excludeRestrictedData));
5201        qb.setTables(sb.toString());
5202        qb.setProjectionMap(sRawContactsProjectionMap);
5203        appendAccountFromParameter(qb, uri);
5204    }
5205
5206    private void setTablesAndProjectionMapForRawContactsEntities(SQLiteQueryBuilder qb, Uri uri) {
5207        // Note: currently, "export only" equals to "restricted", but may not in the future.
5208        boolean excludeRestrictedData = readBooleanQueryParameter(uri,
5209                Data.FOR_EXPORT_ONLY, false);
5210
5211        String requestingPackage = getQueryParameter(uri,
5212                ContactsContract.REQUESTING_PACKAGE_PARAM_KEY);
5213        if (requestingPackage != null) {
5214            excludeRestrictedData = excludeRestrictedData
5215                    || !mDbHelper.hasAccessToRestrictedData(requestingPackage);
5216        }
5217        qb.setTables(mDbHelper.getContactEntitiesView(excludeRestrictedData));
5218        qb.setProjectionMap(sRawContactsEntityProjectionMap);
5219        appendAccountFromParameter(qb, uri);
5220    }
5221
5222    private void setTablesAndProjectionMapForData(SQLiteQueryBuilder qb, Uri uri,
5223            String[] projection, boolean distinct) {
5224        StringBuilder sb = new StringBuilder();
5225        // Note: currently, "export only" equals to "restricted", but may not in the future.
5226        boolean excludeRestrictedData = readBooleanQueryParameter(uri,
5227                Data.FOR_EXPORT_ONLY, false);
5228
5229        String requestingPackage = getQueryParameter(uri,
5230                ContactsContract.REQUESTING_PACKAGE_PARAM_KEY);
5231        if (requestingPackage != null) {
5232            excludeRestrictedData = excludeRestrictedData
5233                    || !mDbHelper.hasAccessToRestrictedData(requestingPackage);
5234        }
5235
5236        sb.append(mDbHelper.getDataView(excludeRestrictedData));
5237        sb.append(" data");
5238
5239        // Include aggregated presence when requested
5240        if (mDbHelper.isInProjection(projection, Data.CONTACT_PRESENCE)) {
5241            sb.append(" LEFT OUTER JOIN " + Tables.AGGREGATED_PRESENCE +
5242                    " ON (" + AggregatedPresenceColumns.CONCRETE_CONTACT_ID + "="
5243                    + RawContacts.CONTACT_ID + ")");
5244        }
5245
5246        // Include aggregated status updates when requested
5247        if (mDbHelper.isInProjection(projection,
5248                Data.CONTACT_STATUS,
5249                Data.CONTACT_STATUS_RES_PACKAGE,
5250                Data.CONTACT_STATUS_ICON,
5251                Data.CONTACT_STATUS_LABEL,
5252                Data.CONTACT_STATUS_TIMESTAMP)) {
5253            sb.append(" LEFT OUTER JOIN " + Tables.STATUS_UPDATES + " "
5254                    + ContactsStatusUpdatesColumns.ALIAS +
5255                    " ON (" + ContactsColumns.LAST_STATUS_UPDATE_ID + "="
5256                            + ContactsStatusUpdatesColumns.CONCRETE_DATA_ID + ")");
5257        }
5258
5259        // Include individual presence when requested
5260        if (mDbHelper.isInProjection(projection, Data.PRESENCE)) {
5261            sb.append(" LEFT OUTER JOIN " + Tables.PRESENCE +
5262                    " ON (" + StatusUpdates.DATA_ID + "="
5263                    + DataColumns.CONCRETE_ID + ")");
5264        }
5265
5266        // Include individual status updates when requested
5267        if (mDbHelper.isInProjection(projection,
5268                Data.STATUS,
5269                Data.STATUS_RES_PACKAGE,
5270                Data.STATUS_ICON,
5271                Data.STATUS_LABEL,
5272                Data.STATUS_TIMESTAMP)) {
5273            sb.append(" LEFT OUTER JOIN " + Tables.STATUS_UPDATES +
5274                    " ON (" + StatusUpdatesColumns.CONCRETE_DATA_ID + "="
5275                            + DataColumns.CONCRETE_ID + ")");
5276        }
5277
5278        qb.setTables(sb.toString());
5279        qb.setProjectionMap(distinct ? sDistinctDataProjectionMap : sDataProjectionMap);
5280        appendAccountFromParameter(qb, uri);
5281    }
5282
5283    private void setTableAndProjectionMapForStatusUpdates(SQLiteQueryBuilder qb,
5284            String[] projection) {
5285        StringBuilder sb = new StringBuilder();
5286        sb.append(mDbHelper.getDataView());
5287        sb.append(" data");
5288
5289        if (mDbHelper.isInProjection(projection, StatusUpdates.PRESENCE)) {
5290            sb.append(" LEFT OUTER JOIN " + Tables.PRESENCE +
5291                    " ON(" + Tables.PRESENCE + "." + StatusUpdates.DATA_ID
5292                    + "=" + DataColumns.CONCRETE_ID + ")");
5293        }
5294
5295        if (mDbHelper.isInProjection(projection,
5296                StatusUpdates.STATUS,
5297                StatusUpdates.STATUS_RES_PACKAGE,
5298                StatusUpdates.STATUS_ICON,
5299                StatusUpdates.STATUS_LABEL,
5300                StatusUpdates.STATUS_TIMESTAMP)) {
5301            sb.append(" LEFT OUTER JOIN " + Tables.STATUS_UPDATES +
5302                    " ON(" + Tables.STATUS_UPDATES + "." + StatusUpdatesColumns.DATA_ID
5303                    + "=" + DataColumns.CONCRETE_ID + ")");
5304        }
5305        qb.setTables(sb.toString());
5306        qb.setProjectionMap(sStatusUpdatesProjectionMap);
5307    }
5308
5309    private void appendAccountFromParameter(SQLiteQueryBuilder qb, Uri uri) {
5310        final String accountName = getQueryParameter(uri, RawContacts.ACCOUNT_NAME);
5311        final String accountType = getQueryParameter(uri, RawContacts.ACCOUNT_TYPE);
5312
5313        final boolean partialUri = TextUtils.isEmpty(accountName) ^ TextUtils.isEmpty(accountType);
5314        if (partialUri) {
5315            // Throw when either account is incomplete
5316            throw new IllegalArgumentException(mDbHelper.exceptionMessage(
5317                    "Must specify both or neither of ACCOUNT_NAME and ACCOUNT_TYPE", uri));
5318        }
5319
5320        // Accounts are valid by only checking one parameter, since we've
5321        // already ruled out partial accounts.
5322        final boolean validAccount = !TextUtils.isEmpty(accountName);
5323        if (validAccount) {
5324            qb.appendWhere(RawContacts.ACCOUNT_NAME + "="
5325                    + DatabaseUtils.sqlEscapeString(accountName) + " AND "
5326                    + RawContacts.ACCOUNT_TYPE + "="
5327                    + DatabaseUtils.sqlEscapeString(accountType));
5328        } else {
5329            qb.appendWhere("1");
5330        }
5331    }
5332
5333    private String appendAccountToSelection(Uri uri, String selection) {
5334        final String accountName = getQueryParameter(uri, RawContacts.ACCOUNT_NAME);
5335        final String accountType = getQueryParameter(uri, RawContacts.ACCOUNT_TYPE);
5336
5337        final boolean partialUri = TextUtils.isEmpty(accountName) ^ TextUtils.isEmpty(accountType);
5338        if (partialUri) {
5339            // Throw when either account is incomplete
5340            throw new IllegalArgumentException(mDbHelper.exceptionMessage(
5341                    "Must specify both or neither of ACCOUNT_NAME and ACCOUNT_TYPE", uri));
5342        }
5343
5344        // Accounts are valid by only checking one parameter, since we've
5345        // already ruled out partial accounts.
5346        final boolean validAccount = !TextUtils.isEmpty(accountName);
5347        if (validAccount) {
5348            StringBuilder selectionSb = new StringBuilder(RawContacts.ACCOUNT_NAME + "="
5349                    + DatabaseUtils.sqlEscapeString(accountName) + " AND "
5350                    + RawContacts.ACCOUNT_TYPE + "="
5351                    + DatabaseUtils.sqlEscapeString(accountType));
5352            if (!TextUtils.isEmpty(selection)) {
5353                selectionSb.append(" AND (");
5354                selectionSb.append(selection);
5355                selectionSb.append(')');
5356            }
5357            return selectionSb.toString();
5358        } else {
5359            return selection;
5360        }
5361    }
5362
5363    /**
5364     * Gets the value of the "limit" URI query parameter.
5365     *
5366     * @return A string containing a non-negative integer, or <code>null</code> if
5367     *         the parameter is not set, or is set to an invalid value.
5368     */
5369    private String getLimit(Uri uri) {
5370        String limitParam = getQueryParameter(uri, "limit");
5371        if (limitParam == null) {
5372            return null;
5373        }
5374        // make sure that the limit is a non-negative integer
5375        try {
5376            int l = Integer.parseInt(limitParam);
5377            if (l < 0) {
5378                Log.w(TAG, "Invalid limit parameter: " + limitParam);
5379                return null;
5380            }
5381            return String.valueOf(l);
5382        } catch (NumberFormatException ex) {
5383            Log.w(TAG, "Invalid limit parameter: " + limitParam);
5384            return null;
5385        }
5386    }
5387
5388    /**
5389     * Returns true if all the characters are meaningful as digits
5390     * in a phone number -- letters, digits, and a few punctuation marks.
5391     */
5392    private boolean isPhoneNumber(CharSequence cons) {
5393        int len = cons.length();
5394
5395        for (int i = 0; i < len; i++) {
5396            char c = cons.charAt(i);
5397
5398            if ((c >= '0') && (c <= '9')) {
5399                continue;
5400            }
5401            if ((c == ' ') || (c == '-') || (c == '(') || (c == ')') || (c == '.') || (c == '+')
5402                    || (c == '#') || (c == '*')) {
5403                continue;
5404            }
5405            if ((c >= 'A') && (c <= 'Z')) {
5406                continue;
5407            }
5408            if ((c >= 'a') && (c <= 'z')) {
5409                continue;
5410            }
5411
5412            return false;
5413        }
5414
5415        return true;
5416    }
5417
5418    String getContactsRestrictions() {
5419        if (mDbHelper.hasAccessToRestrictedData()) {
5420            return "1";
5421        } else {
5422            return RawContactsColumns.CONCRETE_IS_RESTRICTED + "=0";
5423        }
5424    }
5425
5426    public String getContactsRestrictionExceptionAsNestedQuery(String contactIdColumn) {
5427        if (mDbHelper.hasAccessToRestrictedData()) {
5428            return "1";
5429        } else {
5430            return "(SELECT " + RawContacts.IS_RESTRICTED + " FROM " + Tables.RAW_CONTACTS
5431                    + " WHERE " + RawContactsColumns.CONCRETE_ID + "=" + contactIdColumn + ")=0";
5432        }
5433    }
5434
5435    @Override
5436    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
5437        int match = sUriMatcher.match(uri);
5438        switch (match) {
5439            case CONTACTS_PHOTO: {
5440                return openPhotoAssetFile(uri, mode,
5441                        Data._ID + "=" + Contacts.PHOTO_ID + " AND " + RawContacts.CONTACT_ID + "=?",
5442                        new String[]{uri.getPathSegments().get(1)});
5443            }
5444
5445            case DATA_ID: {
5446                return openPhotoAssetFile(uri, mode,
5447                        Data._ID + "=? AND " + Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE + "'",
5448                        new String[]{uri.getPathSegments().get(1)});
5449            }
5450
5451            case CONTACTS_AS_VCARD: {
5452                final String lookupKey = Uri.encode(uri.getPathSegments().get(2));
5453                mSelectionArgs1[0] = String.valueOf(lookupContactIdByLookupKey(mDb, lookupKey));
5454                final String selection = Contacts._ID + "=?";
5455
5456                // When opening a contact as file, we pass back contents as a
5457                // vCard-encoded stream. We build into a local buffer first,
5458                // then pipe into MemoryFile once the exact size is known.
5459                final ByteArrayOutputStream localStream = new ByteArrayOutputStream();
5460                outputRawContactsAsVCard(localStream, selection, mSelectionArgs1);
5461                return buildAssetFileDescriptor(localStream);
5462            }
5463
5464            case CONTACTS_AS_MULTI_VCARD: {
5465                final String lookupKeys = uri.getPathSegments().get(2);
5466                final String[] loopupKeyList = lookupKeys.split(":");
5467                final StringBuilder inBuilder = new StringBuilder();
5468                int index = 0;
5469                // SQLite has limits on how many parameters can be used
5470                // so the IDs are concatenated to a query string here instead
5471                for (String lookupKey : loopupKeyList) {
5472                    if (index == 0) {
5473                        inBuilder.append("(");
5474                    } else {
5475                        inBuilder.append(",");
5476                    }
5477                    inBuilder.append(lookupContactIdByLookupKey(mDb, lookupKey));
5478                    index++;
5479                }
5480                inBuilder.append(')');
5481                final String selection = Contacts._ID + " IN " + inBuilder.toString();
5482
5483                // When opening a contact as file, we pass back contents as a
5484                // vCard-encoded stream. We build into a local buffer first,
5485                // then pipe into MemoryFile once the exact size is known.
5486                final ByteArrayOutputStream localStream = new ByteArrayOutputStream();
5487                outputRawContactsAsVCard(localStream, selection, null);
5488                return buildAssetFileDescriptor(localStream);
5489            }
5490
5491            default:
5492                throw new FileNotFoundException(mDbHelper.exceptionMessage("File does not exist",
5493                        uri));
5494        }
5495    }
5496
5497    private AssetFileDescriptor openPhotoAssetFile(Uri uri, String mode, String selection,
5498            String[] selectionArgs)
5499            throws FileNotFoundException {
5500        if (!"r".equals(mode)) {
5501            throw new FileNotFoundException(mDbHelper.exceptionMessage("Mode " + mode
5502                    + " not supported.", uri));
5503        }
5504
5505        String sql =
5506                "SELECT " + Photo.PHOTO + " FROM " + mDbHelper.getDataView() +
5507                " WHERE " + selection;
5508        SQLiteDatabase db = mDbHelper.getReadableDatabase();
5509        return SQLiteContentHelper.getBlobColumnAsAssetFile(db, sql,
5510                selectionArgs);
5511    }
5512
5513    private static final String CONTACT_MEMORY_FILE_NAME = "contactAssetFile";
5514
5515    /**
5516     * Build a {@link AssetFileDescriptor} through a {@link MemoryFile} with the
5517     * contents of the given {@link ByteArrayOutputStream}.
5518     */
5519    private AssetFileDescriptor buildAssetFileDescriptor(ByteArrayOutputStream stream) {
5520        AssetFileDescriptor fd = null;
5521        try {
5522            stream.flush();
5523
5524            final byte[] byteData = stream.toByteArray();
5525            final int size = byteData.length;
5526
5527            final MemoryFile memoryFile = new MemoryFile(CONTACT_MEMORY_FILE_NAME, size);
5528            memoryFile.writeBytes(byteData, 0, 0, size);
5529            memoryFile.deactivate();
5530
5531            fd = AssetFileDescriptor.fromMemoryFile(memoryFile);
5532        } catch (IOException e) {
5533            Log.w(TAG, "Problem writing stream into an AssetFileDescriptor: " + e.toString());
5534        }
5535        return fd;
5536    }
5537
5538    /**
5539     * Output {@link RawContacts} matching the requested selection in the vCard
5540     * format to the given {@link OutputStream}. This method returns silently if
5541     * any errors encountered.
5542     */
5543    private void outputRawContactsAsVCard(OutputStream stream, String selection,
5544            String[] selectionArgs) {
5545        final Context context = this.getContext();
5546        final VCardComposer composer =
5547                new VCardComposer(context, VCardConfig.VCARD_TYPE_DEFAULT, false);
5548        composer.addHandler(composer.new HandlerForOutputStream(stream));
5549
5550        // No extra checks since composer always uses restricted views
5551        if (!composer.init(selection, selectionArgs)) {
5552            Log.w(TAG, "Failed to init VCardComposer");
5553            return;
5554        }
5555
5556        while (!composer.isAfterLast()) {
5557            if (!composer.createOneEntry()) {
5558                Log.w(TAG, "Failed to output a contact.");
5559            }
5560        }
5561        composer.terminate();
5562    }
5563
5564    @Override
5565    public String getType(Uri uri) {
5566        final int match = sUriMatcher.match(uri);
5567        switch (match) {
5568            case CONTACTS:
5569                return Contacts.CONTENT_TYPE;
5570            case CONTACTS_LOOKUP:
5571            case CONTACTS_ID:
5572            case CONTACTS_LOOKUP_ID:
5573                return Contacts.CONTENT_ITEM_TYPE;
5574            case CONTACTS_AS_VCARD:
5575            case CONTACTS_AS_MULTI_VCARD:
5576                return Contacts.CONTENT_VCARD_TYPE;
5577            case RAW_CONTACTS:
5578                return RawContacts.CONTENT_TYPE;
5579            case RAW_CONTACTS_ID:
5580                return RawContacts.CONTENT_ITEM_TYPE;
5581            case DATA_ID:
5582                return mDbHelper.getDataMimeType(ContentUris.parseId(uri));
5583            case PHONES:
5584                return Phone.CONTENT_TYPE;
5585            case PHONES_ID:
5586                return Phone.CONTENT_ITEM_TYPE;
5587            case PHONE_LOOKUP:
5588                return PhoneLookup.CONTENT_TYPE;
5589            case EMAILS:
5590                return Email.CONTENT_TYPE;
5591            case EMAILS_ID:
5592                return Email.CONTENT_ITEM_TYPE;
5593            case POSTALS:
5594                return StructuredPostal.CONTENT_TYPE;
5595            case POSTALS_ID:
5596                return StructuredPostal.CONTENT_ITEM_TYPE;
5597            case AGGREGATION_EXCEPTIONS:
5598                return AggregationExceptions.CONTENT_TYPE;
5599            case AGGREGATION_EXCEPTION_ID:
5600                return AggregationExceptions.CONTENT_ITEM_TYPE;
5601            case SETTINGS:
5602                return Settings.CONTENT_TYPE;
5603            case AGGREGATION_SUGGESTIONS:
5604                return Contacts.CONTENT_TYPE;
5605            case SEARCH_SUGGESTIONS:
5606                return SearchManager.SUGGEST_MIME_TYPE;
5607            case SEARCH_SHORTCUT:
5608                return SearchManager.SHORTCUT_MIME_TYPE;
5609
5610            default:
5611                return mLegacyApiSupport.getType(uri);
5612        }
5613    }
5614
5615    private void setDisplayName(long rawContactId, int displayNameSource,
5616            String displayNamePrimary, String displayNameAlternative, String phoneticName,
5617            int phoneticNameStyle, String sortKeyPrimary, String sortKeyAlternative) {
5618        mRawContactDisplayNameUpdate.bindLong(1, displayNameSource);
5619        bindString(mRawContactDisplayNameUpdate, 2, displayNamePrimary);
5620        bindString(mRawContactDisplayNameUpdate, 3, displayNameAlternative);
5621        bindString(mRawContactDisplayNameUpdate, 4, phoneticName);
5622        mRawContactDisplayNameUpdate.bindLong(5, phoneticNameStyle);
5623        bindString(mRawContactDisplayNameUpdate, 6, sortKeyPrimary);
5624        bindString(mRawContactDisplayNameUpdate, 7, sortKeyAlternative);
5625        mRawContactDisplayNameUpdate.bindLong(8, rawContactId);
5626        mRawContactDisplayNameUpdate.execute();
5627    }
5628
5629    /**
5630     * Sets the {@link RawContacts#DIRTY} for the specified raw contact.
5631     */
5632    private void setRawContactDirty(long rawContactId) {
5633        mDirtyRawContacts.add(rawContactId);
5634    }
5635
5636    /*
5637     * Sets the given dataId record in the "data" table to primary, and resets all data records of
5638     * the same mimetype and under the same contact to not be primary.
5639     *
5640     * @param dataId the id of the data record to be set to primary.
5641     */
5642    private void setIsPrimary(long rawContactId, long dataId, long mimeTypeId) {
5643        mSetPrimaryStatement.bindLong(1, dataId);
5644        mSetPrimaryStatement.bindLong(2, mimeTypeId);
5645        mSetPrimaryStatement.bindLong(3, rawContactId);
5646        mSetPrimaryStatement.execute();
5647    }
5648
5649    /*
5650     * Sets the given dataId record in the "data" table to "super primary", and resets all data
5651     * records of the same mimetype and under the same aggregate to not be "super primary".
5652     *
5653     * @param dataId the id of the data record to be set to primary.
5654     */
5655    private void setIsSuperPrimary(long rawContactId, long dataId, long mimeTypeId) {
5656        mSetSuperPrimaryStatement.bindLong(1, dataId);
5657        mSetSuperPrimaryStatement.bindLong(2, mimeTypeId);
5658        mSetSuperPrimaryStatement.bindLong(3, rawContactId);
5659        mSetSuperPrimaryStatement.execute();
5660    }
5661
5662    public String insertNameLookupForEmail(long rawContactId, long dataId, String email) {
5663        if (TextUtils.isEmpty(email)) {
5664            return null;
5665        }
5666
5667        String address = mDbHelper.extractHandleFromEmailAddress(email);
5668        if (address == null) {
5669            return null;
5670        }
5671
5672        insertNameLookup(rawContactId, dataId,
5673                NameLookupType.EMAIL_BASED_NICKNAME, NameNormalizer.normalize(address));
5674        return address;
5675    }
5676
5677    /**
5678     * Normalizes the nickname and inserts it in the name lookup table.
5679     */
5680    public void insertNameLookupForNickname(long rawContactId, long dataId, String nickname) {
5681        if (TextUtils.isEmpty(nickname)) {
5682            return;
5683        }
5684
5685        insertNameLookup(rawContactId, dataId,
5686                NameLookupType.NICKNAME, NameNormalizer.normalize(nickname));
5687    }
5688
5689    public void insertNameLookupForOrganization(long rawContactId, long dataId, String company,
5690            String title) {
5691        if (!TextUtils.isEmpty(company)) {
5692            insertNameLookup(rawContactId, dataId,
5693                    NameLookupType.ORGANIZATION, NameNormalizer.normalize(company));
5694        }
5695        if (!TextUtils.isEmpty(title)) {
5696            insertNameLookup(rawContactId, dataId,
5697                    NameLookupType.ORGANIZATION, NameNormalizer.normalize(title));
5698        }
5699    }
5700
5701    public void insertNameLookupForStructuredName(long rawContactId, long dataId, String name,
5702            int fullNameStyle) {
5703        mNameLookupBuilder.insertNameLookup(rawContactId, dataId, name, fullNameStyle);
5704    }
5705
5706    private class StructuredNameLookupBuilder extends NameLookupBuilder {
5707
5708        public StructuredNameLookupBuilder(NameSplitter splitter) {
5709            super(splitter);
5710        }
5711
5712        @Override
5713        protected void insertNameLookup(long rawContactId, long dataId, int lookupType,
5714                String name) {
5715            ContactsProvider2.this.insertNameLookup(rawContactId, dataId, lookupType, name);
5716        }
5717
5718        @Override
5719        protected String[] getCommonNicknameClusters(String normalizedName) {
5720            return mCommonNicknameCache.getCommonNicknameClusters(normalizedName);
5721        }
5722    }
5723
5724    public void insertNameLookupForPhoneticName(long rawContactId, long dataId,
5725            ContentValues values) {
5726        if (values.containsKey(StructuredName.PHONETIC_FAMILY_NAME)
5727                || values.containsKey(StructuredName.PHONETIC_GIVEN_NAME)
5728                || values.containsKey(StructuredName.PHONETIC_MIDDLE_NAME)) {
5729            insertNameLookupForPhoneticName(rawContactId, dataId,
5730                    values.getAsString(StructuredName.PHONETIC_FAMILY_NAME),
5731                    values.getAsString(StructuredName.PHONETIC_MIDDLE_NAME),
5732                    values.getAsString(StructuredName.PHONETIC_GIVEN_NAME));
5733        }
5734    }
5735
5736    public void insertNameLookupForPhoneticName(long rawContactId, long dataId, String familyName,
5737            String middleName, String givenName) {
5738        mSb.setLength(0);
5739        if (familyName != null) {
5740            mSb.append(familyName.trim());
5741        }
5742        if (middleName != null) {
5743            mSb.append(middleName.trim());
5744        }
5745        if (givenName != null) {
5746            mSb.append(givenName.trim());
5747        }
5748
5749        if (mSb.length() > 0) {
5750            insertNameLookup(rawContactId, dataId, NameLookupType.NAME_COLLATION_KEY,
5751                    NameNormalizer.normalize(mSb.toString()));
5752        }
5753
5754        if (givenName != null) {
5755            // We want the phonetic given name to be used for search, but not for aggregation,
5756            // which is why we are using NAME_SHORTHAND rather than NAME_COLLATION_KEY
5757            insertNameLookup(rawContactId, dataId, NameLookupType.NAME_SHORTHAND,
5758                    NameNormalizer.normalize(givenName.trim()));
5759        }
5760    }
5761
5762    /**
5763     * Inserts a record in the {@link Tables#NAME_LOOKUP} table.
5764     */
5765    public void insertNameLookup(long rawContactId, long dataId, int lookupType, String name) {
5766        mNameLookupInsert.bindLong(1, rawContactId);
5767        mNameLookupInsert.bindLong(2, dataId);
5768        mNameLookupInsert.bindLong(3, lookupType);
5769        bindString(mNameLookupInsert, 4, name);
5770        mNameLookupInsert.executeInsert();
5771    }
5772
5773    /**
5774     * Deletes all {@link Tables#NAME_LOOKUP} table rows associated with the specified data element.
5775     */
5776    public void deleteNameLookup(long dataId) {
5777        mNameLookupDelete.bindLong(1, dataId);
5778        mNameLookupDelete.execute();
5779    }
5780
5781    public void appendContactFilterAsNestedQuery(StringBuilder sb, String filterParam) {
5782        sb.append("(" +
5783                "SELECT DISTINCT " + RawContacts.CONTACT_ID +
5784                " FROM " + Tables.RAW_CONTACTS +
5785                " JOIN " + Tables.NAME_LOOKUP +
5786                " ON(" + RawContactsColumns.CONCRETE_ID + "="
5787                        + NameLookupColumns.RAW_CONTACT_ID + ")" +
5788                " WHERE normalized_name GLOB '");
5789        sb.append(NameNormalizer.normalize(filterParam));
5790        sb.append("*' AND " + NameLookupColumns.NAME_TYPE +
5791                    " IN(" + CONTACT_LOOKUP_NAME_TYPES + "))");
5792    }
5793
5794    public String getRawContactsByFilterAsNestedQuery(String filterParam) {
5795        StringBuilder sb = new StringBuilder();
5796        appendRawContactsByFilterAsNestedQuery(sb, filterParam);
5797        return sb.toString();
5798    }
5799
5800    public void appendRawContactsByFilterAsNestedQuery(StringBuilder sb, String filterParam) {
5801        appendRawContactsByNormalizedNameFilter(sb, NameNormalizer.normalize(filterParam), true);
5802    }
5803
5804    private void appendRawContactsByNormalizedNameFilter(StringBuilder sb, String normalizedName,
5805            boolean allowEmailMatch) {
5806        sb.append("(" +
5807                "SELECT " + NameLookupColumns.RAW_CONTACT_ID +
5808                " FROM " + Tables.NAME_LOOKUP +
5809                " WHERE " + NameLookupColumns.NORMALIZED_NAME +
5810                " GLOB '");
5811        sb.append(normalizedName);
5812        sb.append("*' AND " + NameLookupColumns.NAME_TYPE + " IN ("
5813                + NameLookupType.NAME_COLLATION_KEY + ","
5814                + NameLookupType.NICKNAME + ","
5815                + NameLookupType.NAME_SHORTHAND + ","
5816                + NameLookupType.ORGANIZATION + ","
5817                + NameLookupType.NAME_CONSONANTS);
5818        if (allowEmailMatch) {
5819            sb.append("," + NameLookupType.EMAIL_BASED_NICKNAME);
5820        }
5821        sb.append("))");
5822    }
5823
5824    /**
5825     * Inserts an argument at the beginning of the selection arg list.
5826     */
5827    private String[] insertSelectionArg(String[] selectionArgs, String arg) {
5828        if (selectionArgs == null) {
5829            return new String[] {arg};
5830        } else {
5831            int newLength = selectionArgs.length + 1;
5832            String[] newSelectionArgs = new String[newLength];
5833            newSelectionArgs[0] = arg;
5834            System.arraycopy(selectionArgs, 0, newSelectionArgs, 1, selectionArgs.length);
5835            return newSelectionArgs;
5836        }
5837    }
5838
5839    private String[] appendProjectionArg(String[] projection, String arg) {
5840        if (projection == null) {
5841            return null;
5842        }
5843        final int length = projection.length;
5844        String[] newProjection = new String[length + 1];
5845        System.arraycopy(projection, 0, newProjection, 0, length);
5846        newProjection[length] = arg;
5847        return newProjection;
5848    }
5849
5850    protected Account getDefaultAccount() {
5851        AccountManager accountManager = AccountManager.get(getContext());
5852        try {
5853            Account[] accounts = accountManager.getAccountsByTypeAndFeatures(DEFAULT_ACCOUNT_TYPE,
5854                    new String[] {FEATURE_LEGACY_HOSTED_OR_GOOGLE}, null, null).getResult();
5855            if (accounts != null && accounts.length > 0) {
5856                return accounts[0];
5857            }
5858        } catch (Throwable e) {
5859            Log.e(TAG, "Cannot determine the default account for contacts compatibility", e);
5860        }
5861        return null;
5862    }
5863
5864    /**
5865     * Returns true if the specified account type is writable.
5866     */
5867    protected boolean isWritableAccount(String accountType) {
5868        if (accountType == null) {
5869            return true;
5870        }
5871
5872        Boolean writable = mAccountWritability.get(accountType);
5873        if (writable != null) {
5874            return writable;
5875        }
5876
5877        IContentService contentService = ContentResolver.getContentService();
5878        try {
5879            for (SyncAdapterType sync : contentService.getSyncAdapterTypes()) {
5880                if (ContactsContract.AUTHORITY.equals(sync.authority) &&
5881                        accountType.equals(sync.accountType)) {
5882                    writable = sync.supportsUploading();
5883                    break;
5884                }
5885            }
5886        } catch (RemoteException e) {
5887            Log.e(TAG, "Could not acquire sync adapter types");
5888        }
5889
5890        if (writable == null) {
5891            writable = false;
5892        }
5893
5894        mAccountWritability.put(accountType, writable);
5895        return writable;
5896    }
5897
5898    /* package */ static boolean readBooleanQueryParameter(Uri uri, String parameter,
5899            boolean defaultValue) {
5900
5901        // Manually parse the query, which is much faster than calling uri.getQueryParameter
5902        String query = uri.getEncodedQuery();
5903        if (query == null) {
5904            return defaultValue;
5905        }
5906
5907        int index = query.indexOf(parameter);
5908        if (index == -1) {
5909            return defaultValue;
5910        }
5911
5912        index += parameter.length();
5913
5914        return !matchQueryParameter(query, index, "=0", false)
5915                && !matchQueryParameter(query, index, "=false", true);
5916    }
5917
5918    private static boolean matchQueryParameter(String query, int index, String value,
5919            boolean ignoreCase) {
5920        int length = value.length();
5921        return query.regionMatches(ignoreCase, index, value, 0, length)
5922                && (query.length() == index + length || query.charAt(index + length) == '&');
5923    }
5924
5925    /**
5926     * A fast re-implementation of {@link Uri#getQueryParameter}
5927     */
5928    /* package */ static String getQueryParameter(Uri uri, String parameter) {
5929        String query = uri.getEncodedQuery();
5930        if (query == null) {
5931            return null;
5932        }
5933
5934        int queryLength = query.length();
5935        int parameterLength = parameter.length();
5936
5937        String value;
5938        int index = 0;
5939        while (true) {
5940            index = query.indexOf(parameter, index);
5941            if (index == -1) {
5942                return null;
5943            }
5944
5945            index += parameterLength;
5946
5947            if (queryLength == index) {
5948                return null;
5949            }
5950
5951            if (query.charAt(index) == '=') {
5952                index++;
5953                break;
5954            }
5955        }
5956
5957        int ampIndex = query.indexOf('&', index);
5958        if (ampIndex == -1) {
5959            value = query.substring(index);
5960        } else {
5961            value = query.substring(index, ampIndex);
5962        }
5963
5964        return Uri.decode(value);
5965    }
5966
5967    private void bindString(SQLiteStatement stmt, int index, String value) {
5968        if (value == null) {
5969            stmt.bindNull(index);
5970        } else {
5971            stmt.bindString(index, value);
5972        }
5973    }
5974
5975    private void bindLong(SQLiteStatement stmt, int index, Number value) {
5976        if (value == null) {
5977            stmt.bindNull(index);
5978        } else {
5979            stmt.bindLong(index, value.longValue());
5980        }
5981    }
5982}
5983